package nckd.jxccl.sit.hcsi.business.file.attach;

import kd.bos.algo.DataSet;
import kd.bos.algo.JoinType;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import nckd.jxccl.base.orm.helper.QFilterCommonHelper;
import nckd.jxccl.sit.hcsi.common.constant.SitConstant;

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

public class AdjustSIBaseService {

    private static final Log logger = LogFactory.getLog(AdjustSIBaseService.class);
    DynamicObject welfarePayer;
    Date year;

    public AdjustSIBaseService(DynamicObject welfarePayer, Date year) {
        this.welfarePayer = welfarePayer;
        this.year = year;
    }

    /**
     * <p>执行查询方法
     * <p>1.查询参保单位下所有员工
     * <p>2.查询员工现在各险种的基数
     * <p>3.查询员工的年收入证明统计单， 时间 = year, 单据状态 = 已确认
     *
     * @return
     */
    public DataSet query() {
        // 查询社保档案 keySet为employee.id
        Map<Long, DynamicObject> sinsurFileMap = querySinsurFileDyns();
        List<Long> employeeIds = sinsurFileMap.keySet().stream().collect(Collectors.toList());
        // 根据employeeIds 查询员工现在各险种的基数
        DataSet SIBaseDs = querySIBaseDyns(employeeIds);
        // 查询员工的年收入证明统计单， 时间 = year, 单据状态 = 已确认
        DataSet incomeBillDs = queryIncomeBillDyns(employeeIds);
        // 根据employeeIds处理数据
        return dealData(sinsurFileMap, SIBaseDs, incomeBillDs);
    }


    /**
     * 处理数据
     *
     * @param sinsurFileMap
     * @param SIBaseDs
     * @param incomeBillDs
     * @return
     */
    public DataSet dealData(Map<Long, DynamicObject> sinsurFileMap, DataSet SIBaseDs, DataSet incomeBillDs) {
        String[] leftFields = new String[]{"nckd_employee.id as employeeId", "nckd_totalamount as totalamount"};
        String[] rightFields = new String[]{"sinsurfile.id as sinsurfileId", "welfaretype.id as welfaretypeId", "paybaseofemployee", "paybaseofcompany", "nckd_sinsurdimension.id as sinsurdimensionId"};
        return incomeBillDs.join(SIBaseDs, JoinType.INNER).on("nckd_employee.id", "employee.id").select(leftFields, rightFields).finish();
    }



    /**
     * 查询社保档案
     * @return
     */
    public Map<Long, DynamicObject> querySinsurFileDyns() {
        QFilter filter = new QFilter("welfarepayer.id", QCP.equals, welfarePayer.getLong("id"));
        filter.and(QFilterCommonHelper.getCurrentVersionFilter());
        filter.and(QFilterCommonHelper.getDataStatusFilter());
        DynamicObjectCollection cols = SitConstant.SINSURFILE_HELPER.queryOriginalCollection("id,employee.id,org.id", new QFilter[]{filter});
        return cols.stream().collect(Collectors.toMap((dyx) -> {
            return dyx.getLong("employee.id");
        }, (dyx) -> {
            return dyx;
        }, (key1, key2) -> {
            return key2;
        }));
    }

    /**
     * 查询员工现在各险种的基数
     *
     * @param employeeIds
     * @return
     */
    public DataSet querySIBaseDyns(List<Long> employeeIds) {
        QFilter filter = new QFilter("employee.id", QCP.in, employeeIds);
        filter.and("iscurrentdata", QCP.equals, "1");
        filter.and("insured", QCP.equals, true);
        String selectFields = "sinsurfile.id, employee.id, welfaretype.id, paybaseofemployee, paybaseofcompany, nckd_sinsurdimension.id";
        return SitConstant.SIBASE_HELPER.queryDataSet("AdjustSIBaseService-querySIBase",selectFields, new QFilter[]{filter});
    }

    /**
     * 根据employeeIds+年度查询员工年收入证明统计单数据
     *
     * @param employeeIds
     * @return
     */
    private DataSet queryIncomeBillDyns(List<Long> employeeIds) {
        QFilter filter = new QFilter("nckd_employee.id", QCP.in, employeeIds);
        filter.and("nckd_year", QCP.equals, year);
        filter.and("billstatus", QCP.in, new HashSet<>(Arrays.asList("C", "H")));
        String selectFields = "nckd_employee.id, nckd_totalamount";
        return SitConstant.SITINCOMEBILL_HELPER.queryDataSet("AdjustSIBaseService-queryIncomeBill", selectFields, new QFilter[]{filter});
    }

}
