package nckd.jimin.jyyy.hr.swc.hsas.util;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.*;
import java.util.stream.Collectors;

public class CalPersonUtil {

    private static final Log log = LogFactory.getLog(CalPersonUtil.class);
    public static String SALARYFILE_ENTITY = "hsas_salaryfile";
    public static String PAYSETTING_ENTITY = "hsas_paysetting";
    public static String PERBANKCARD_ENTITY = "hrpi_perbankcard";
    public static String CONTRACT_ENTITY = "hlcm_contract";
    public static List<String> periodTypeList = new ArrayList();
    static {
        periodTypeList.add("1020_S");
        periodTypeList.add("1030_S");
    }


    /**
     * 根据薪酬档案ID获取银行信息
     * @param salaryFileIds 薪酬档案ID
     * @return
     */
    public static Map<Long, DynamicObject> getBankInfoByFileID(List<Long> salaryFileIds) {
        QFilter filter = new QFilter("salaryfile.id", QCP.in, salaryFileIds);
        filter.and("iscurrentversion", QCP.equals, "1");
        filter.and("datastatus", QCP.equals, "1");
        String selectFields = "salaryfile.id,entryentity.perbankcard.id," +
                "entryentity.perbankcard.bankcardnum,entryentity.perbankcard.bankdeposit,entryentity.perbankcard.username";
        DynamicObjectCollection paySettingCols = QueryServiceHelper.query(PAYSETTING_ENTITY, selectFields, new QFilter[]{filter});
        //提取成Map,k = 薪酬档案ID，v = DynamicObject
        Map paySettingMap = paySettingCols.stream().collect(Collectors.toMap((dyx) -> {
            return dyx.getLong("salaryfile.id");
        }, (dyx) -> {
            return dyx;
        }, (key1, key2) -> {
            return key2;
        }));
        return paySettingMap;
    }

    /**
     * 根据人员获取薪酬档案银行卡信息
     * @param personIds
     * @return
     */
    public static Map<Long, DynamicObject> getBankInfoByPersonID(List<Long> personIds) {
        QFilter filter = new QFilter("salaryfile.person.id", QCP.in, personIds);
        filter.and("iscurrentversion", QCP.equals, "1");
        filter.and("datastatus", QCP.equals, "1");
        String selectFields = "salaryfile.person.id,entryentity.perbankcard.id," +
                "entryentity.perbankcard.bankcardnum,entryentity.perbankcard.bankdeposit,entryentity.perbankcard.username";
        DynamicObjectCollection paySettingCols = QueryServiceHelper.query(PAYSETTING_ENTITY, selectFields, new QFilter[]{filter});
        //提取成Map,k = 薪酬档案ID，v = DynamicObject
        Map paySettingMap = paySettingCols.stream().collect(Collectors.toMap((dyx) -> {
            return dyx.getLong("salaryfile.person.id");
        }, (dyx) -> {
            return dyx;
        }, (key1, key2) -> {
            return key2;
        }));
        return paySettingMap;
    }

    /**
     * 根据Person获取核算期间上月合同档案的实签单位
     * @param personIds
     * @param date
     * @return Map, k = 人员ID， v = 实签单位ID
     */
    public static Map<Long, Long> getAContractInfoByPersonID (List<Long> personIds, Date date) {
        Date lastDate = getLastMonthDay(date);
        //人员
        QFilter personFilter = new QFilter("person.id", QCP.in, personIds);
        personFilter.and("protocoltype", QCP.equals, "1");  //合同
        personFilter.and("iscurrentversion", QCP.equals, "1");
        personFilter.and("datastatus", QCP.equals, "1");
        personFilter.and("signstatus", QCP.not_equals2, "7");
        //固定期限：
        QFilter filter1 = new QFilter("periodtype.number", QCP.equals, "1010_S");
        filter1.and("startdate", QCP.less_equals, lastDate);
        filter1.and("enddate", QCP.large_equals, lastDate);
        //无固定期限：
        QFilter filter2 = new QFilter("periodtype.number", QCP.in, periodTypeList);
        filter2.and("startdate", QCP.less_equals, lastDate);
        QFilter dateFilter = filter1.or(filter2);
        QFilter filter = personFilter.and(dateFilter);
        String selectFields = "person.id,actualsigncompany.id,startdate,signstatus,signeddate";
        DynamicObjectCollection contractCols = QueryServiceHelper.query(CONTRACT_ENTITY, selectFields, new QFilter[]{filter},"startdate desc,signstatus desc");
        //按照person.id提取
        Map contractMap = contractCols.stream().collect(Collectors.toMap((dyx) -> {
            return dyx.getLong("person.id");
        }, (dyx) -> {
            return dyx.getLong("actualsigncompany.id");
        }, (value1, value2) -> {
            return value1;
        }));
        return contractMap;
    }

    /**
     * 根据Person获取核算期间合同档案的实签单位
     * @param personIds
     * @param date
     * @return Map, k = 人员ID， v = 实签单位ID
     */
    public static Map<Long, Long> getBContractInfoByPersonID (List<Long> personIds, Date date) {
        //人员
        QFilter personFilter = new QFilter("person.id", QCP.in, personIds);
        personFilter.and("protocoltype", QCP.equals, "1");  //合同
        personFilter.and("iscurrentversion", QCP.equals, "1");
        personFilter.and("datastatus", QCP.equals, "1");
        personFilter.and("signstatus", QCP.not_equals2, "7");
        //固定期限：
        QFilter filter1 = new QFilter("periodtype.number", QCP.equals, "1010_S");
        filter1.and("startdate", QCP.less_equals, date);
        filter1.and("enddate", QCP.large_equals, date);
        //无固定期限：
        QFilter filter2 = new QFilter("periodtype.number", QCP.in, periodTypeList);
        filter2.and("startdate", QCP.less_equals, date);
        QFilter dateFilter = filter1.or(filter2);
        QFilter filter = personFilter.and(dateFilter);
        String selectFields = "person.id,actualsigncompany.id,startdate,signstatus,signeddate";
        DynamicObjectCollection contractCols = QueryServiceHelper.query(CONTRACT_ENTITY, selectFields, new QFilter[]{filter},"startdate desc,signstatus desc");
        //按照person.id提取
        Map contractMap = contractCols.stream().collect(Collectors.toMap((dyx) -> {
            return dyx.getLong("person.id");
        }, (dyx) -> {
            return dyx.getLong("actualsigncompany.id");
        }, (value1, value2) -> {
            return value1;
        }));
        return contractMap;
    }

    /**
     * 根据入参获取上个月最后一天
     * @param date
     * @return
     */
    public static Date getLastMonthDay (Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

}
