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

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.sdk.swc.hsas.business.extpoint.calperson.IAddCalPersonExtService;
import kd.sdk.swc.hsas.common.events.calperson.AddJudeUpdateFieldsEvent;
import kd.sdk.swc.hsas.common.events.calperson.AfterAddCalpersonEvent;
import kd.sdk.swc.hsas.common.events.calperson.AfterQueryDataForRuleEvent;
import nckd.jimin.jyyy.hr.swc.hsas.util.CalPersonUtil;
import nckd.jimin.jyyy.hr.task.util.SyncOAHelper;

import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 2025-07-24
 * Tyx
 * 核算名单扩展类
 * 业务扩展接口类：kd.sdk.swc.hsas.business.extpoint.calperson.IAddCalPersonExtService
 * 增加更新人员判断的字段   kd.sdk.swc.hsas.business.extpoint.calperson.IAddCalPersonExtService#addJudeUpdateFields
 * 核算名单添加人员二开埋点 kd.sdk.swc.hsas.business.extpoint.calperson.IAddCalPersonExtService#afterPackagePerson
 * 核算名单规则解析二开取值 kd.sdk.swc.hsas.business.extpoint.calperson.IAddCalPersonExtService#afterQueryDataForRule
 */
public class CalPersonExtService implements IAddCalPersonExtService {

    private static final Log log = LogFactory.getLog(CalPersonExtService.class);
    @Override
    public void afterPackagePerson(AfterAddCalpersonEvent event) {
        DynamicObjectCollection calPersons = event.getCalPersons();
        //提取所有薪酬档案ID和PersonId
        List<Long> salaryFileIds = calPersons.stream().map(o -> o.getLong("salaryfile")).collect(Collectors.toList());
        List<Long> personIds = calPersons.stream().map(o -> o.getLong("salaryfilev.person.id")).collect(Collectors.toList());
        //核算期间开始日期-结束日期
        Date endDate = event.getEndDate();
        //批量获取银行卡信息
        Map<Long, DynamicObject> paySettingMap = CalPersonUtil.getBankInfoByFileID(salaryFileIds);
        //批量获取核算期间上月实签单位
        Map<Long, Long> aContractMap = CalPersonUtil.getAContractInfoByPersonID(personIds, endDate);
        //批量获取核算期间所属月份实签单位
        Map<Long, Long> bContractMap = CalPersonUtil.getBContractInfoByPersonID(personIds, endDate);
        for(DynamicObject calPerson : calPersons) {
            Long salaryFileId = calPerson.getLong("salaryfile");
            DynamicObject paySettingDyn = paySettingMap.get(salaryFileId);
            if(!ObjectUtils.isEmpty(paySettingDyn)){
                calPerson.set("nckd_perbankcard", paySettingDyn.getLong("entryentity.perbankcard.id"));
            }
            Long personId = calPerson.getLong("salaryfilev.person.id");
            Long aContractId = aContractMap.get(personId);
            if(aContractId != null) {
                calPerson.set("nckd_asigncompany", aContractId);
            }
            Long bContractId = bContractMap.get(personId);
            if(bContractId != null) {
                calPerson.set("nckd_bsigncompany", bContractId);
            }
        }
    }

    /**
     * 添加二开字段，供核算名单规则过滤使用
     * @param event
     */
    @Override
    public void afterQueryDataForRule(AfterQueryDataForRuleEvent event) {
        IAddCalPersonExtService.super.afterQueryDataForRule(event);
        Map<String, Set<String>> entityFiledMap = event.getEntityFiledMap();
        Set<String> perBankCardFields = entityFiledMap.get("hrpi_perbankcard");
        Set<String> contractFields = entityFiledMap.get("hlcm_contract");
        List<Map<String, Object>> datas = event.getDatas();
        if(datas.size() == 0)
            return;
        //先批量获取银行卡信息
        List<Long> personIds = datas.stream().map(map -> Long.valueOf(map.get("person.id").toString())).collect(Collectors.toList());
        Map<Long, DynamicObject> paySettingMap = CalPersonUtil.getBankInfoByPersonID(personIds);
        Date date = (Date) datas.get(0).get("caltask.enddate");
        //批量获取核算期间上月实签单位
        Map<Long, Long> aContractMap = CalPersonUtil.getAContractInfoByPersonID(personIds, date);
        //批量获取核算期间所属月份实签单位
        Map<Long, Long> bContractMap = CalPersonUtil.getBContractInfoByPersonID(personIds, date);

        for(Map<String,Object> data : datas) {
            Long personId = Long.valueOf(data.get("person.id").toString());
            DynamicObject paySettingDyn = paySettingMap.get(personId);
            if(!ObjectUtils.isEmpty(paySettingDyn)) {
                //银行卡信息塞值
                if (perBankCardFields != null) {
                    for (String field : perBankCardFields) {
                        Object fieldValue = paySettingDyn.get("entryentity.perbankcard." + field);
                        data.put("hrpi_perbankcard" + "." + field, fieldValue);
                    }
                }
            }
            //合同信息塞值
            if(contractFields != null) {
                for (String field : contractFields) {
                    Long aContractId = aContractMap.get(personId);
                    if (aContractId != null) {
                        data.put("hlcm_contract" + "." + field, aContractId);
                    }
                    Long bContractId = bContractMap.get(personId);
                    if (bContractId != null) {
                        data.put("hlcm_contract" + "." + field, bContractId);
                    }
                }
            }
        }
    }

    @Override
    public void addJudeUpdateFields(AddJudeUpdateFieldsEvent event) {
        event.getFields().add("nckd_perbankcard.id");
        event.getFields().add("nckd_asigncompany.id");
        event.getFields().add("nckd_bsigncompany.id");
    }
}
