package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.common.LocalDateRange;
import com.engine.salary.entity.datacollection.dto.AttendQuoteListDTO;
import com.engine.salary.entity.datacollection.param.AttendQuoteCheckOperationParam;
import com.engine.salary.entity.datacollection.param.AttendQuoteQueryParam;
import com.engine.salary.entity.datacollection.param.QuerySalaryCycleAndAttendCycleParam;
import com.engine.salary.entity.salarysob.dto.SalarySobCycleDTO;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.enums.datacollection.AttendQuoteSourceTypeEnum;
import com.engine.salary.service.AttendQuoteService;
import com.engine.salary.service.SalarySobService;
import com.engine.salary.service.impl.AttendQuoteServiceImpl;
import com.engine.salary.service.impl.SalarySobServiceImpl;
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.valid.ValidUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import weaver.hrm.User;

import java.time.YearMonth;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 考勤引用
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class AttendQuoteWrapper extends Service {

    private AttendQuoteService getAttendQuoteService(User user) {
        return  ServiceUtil.getService(AttendQuoteServiceImpl.class, user);
    }

    private SalarySobService getSalarySobService(User user) {
        return  ServiceUtil.getService(SalarySobServiceImpl.class, user);
    }


    /**
     * 考勤引用列表
     *
     * @param queryParam
     * @return
     */
    public PageInfo<AttendQuoteListDTO> list(AttendQuoteQueryParam queryParam) {
        PageInfo<AttendQuoteListDTO> page = getAttendQuoteService(user).listPage(queryParam);

        List<AttendQuoteListDTO> list = page.getList();
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(m -> m.setSourceType(AttendQuoteSourceTypeEnum.getDefaultLabelByValue(Integer.valueOf(m.getSourceType()))));
            page.setList(list);
        }

        return page;
    }

    /**
     * 获取引用考勤表单
     *
     * @param employeeId
     * @return
     */
//    public WeaForm getSyncForm(Long employeeId) {
//        // 查询所有启用的薪资账套
//        List<SalarySobPO> salarySobs = getSalarySobService(user).listByDisable(NumberUtils.INTEGER_ZERO);
//        List<WeaFormOption> salarySobOptions = salarySobs.stream()
//                .map(salarySobPO -> new WeaFormOption("" + salarySobPO.getId(), salarySobPO.getName()))
//                .collect(Collectors.toList());
//        // 构建表单
//        WeaForm weaForm = FormatManager.<AttendQuoteSyncDataFormDTO>getInstance().genForm(AttendQuoteSyncDataFormDTO.class, AttendQuoteSyncDataFormDTO.builder().salarySobOptions(salarySobOptions).build());
//
//        WeaFormItem descriptionItem = weaForm.getItems().get("description");
//        Map<String, Object> otherParams = Maps.newHashMap();
//        otherParams.put("showCount", true);
//        otherParams.put("placeholder", SalaryI18nUtil.getI18nLabel(tenantKey, employeeId, 85987, "请输入"));
//        descriptionItem.setOtherParams(otherParams);
//        weaForm.getItems().put("description", descriptionItem);
//
//        WeaFormItem salaryYearMonthItem = weaForm.getItems().get("salaryYearMonth");
//        otherParams = Maps.newHashMap();
//        otherParams.put("type", "month");
//        salaryYearMonthItem.setOtherParams(otherParams);
//        weaForm.getItems().put("salaryYearMonth", salaryYearMonthItem);
//
//        return weaForm;
//    }

    /**
     * 检查是否可以操作
     *
     * @return
     */
    public Boolean checkOperation(AttendQuoteCheckOperationParam checkOperationParam) {
        ValidUtil.doValidator(checkOperationParam);

        String salaryYearMonthStr = checkOperationParam.getSalaryYearMonthStr();
        YearMonth yearMonth = SalaryDateUtil.String2YearMonth(salaryYearMonthStr);
        checkOperationParam.setSalaryYearMonth(yearMonth);
        return getAttendQuoteService(user).checkOperation(checkOperationParam.getSalaryYearMonth(), checkOperationParam.getSalarySobId());
    }

    /**
     * 获取薪资周期和考勤周期
     *
     * @return
     */
    public Map<String, Object> getSalaryCycleAndAttendCycle(QuerySalaryCycleAndAttendCycleParam param) {
        Long salarySobId = param.getSalarySobId();
        YearMonth salaryYearMonth = param.getSalaryYearMonth();
        SalarySobCycleDTO salarySobCycleDTO = getSalarySobService(user).getSalarySobCycle(salarySobId, salaryYearMonth);

        LocalDateRange salaryCycleRange = salarySobCycleDTO.getSalaryCycle();
        LocalDateRange attendCycleRange = salarySobCycleDTO.getAttendCycle();

        Map<String, Object> salaryCycleAndAttendCycle = new HashMap<>(2);
        salaryCycleAndAttendCycle.put("salaryCycle", SalaryDateUtil.getFormatLocalDate(salaryCycleRange.getFromDate()) + " ~ " + SalaryDateUtil.getFormatLocalDate(salaryCycleRange.getEndDate()));
        salaryCycleAndAttendCycle.put("attendCycle", SalaryDateUtil.getFormatLocalDate(attendCycleRange.getFromDate()) + " ~ " + SalaryDateUtil.getFormatLocalDate(attendCycleRange.getEndDate()));

        return salaryCycleAndAttendCycle;
    }

    /**
     * 获取薪资账套下拉列表
     *
     * @return
     */
    public List<Map<String, Object>> selectSalarySobList() {
        // 查询所有启用的薪资账套
        List<SalarySobPO> salarySobs = getSalarySobService(user).listByDisable(NumberUtils.INTEGER_ZERO);

        return salarySobs.stream().map(m -> {
            Map<String, Object> map = new HashMap<>(2);
            map.put("id", String.valueOf(m.getId()));
            map.put("content", m.getName());
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 删除考勤引用
     *
     * @param ids
     * @return
     */
    public String delete(Collection<Long> ids) {
        return getAttendQuoteService(user).delete(ids);
    }
}
