package nckd.jxccl.hr.excells.plugin.operate.validate;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.entity.validate.BillStatus;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.base.hrpi.helper.EmpPosOrgRelHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 优秀生津贴验证
* 实体标识：nckd_excellentssallowance
* @author W.Y.C
* @date 2026/1/3 16:34
* @version 1.0
*/
public class ExcellentssAllowanceValiDate extends AbstractValidator {

    @Override
    public void validate() {
        List<Long> personIds = new ArrayList<>();
        for (ExtendedDataEntity dataEntity : this.getDataEntities()) {
            DynamicObject data = dataEntity.getDataEntity();
            DynamicObjectCollection entrys = data.getDynamicObjectCollection(FormConstant.NCKD_ENTRYENTITY);
            for (DynamicObject entry : entrys) {
                personIds.add(entry.getLong(String.join( ".", FormConstant.NCKD_PERSON, FormConstant.ID_KEY)));
            }
        }
        if(!personIds.isEmpty()) {
            //获取员工最新任职
            Map<Long, DynamicObject> empPosOrgRelByEmployeesMap = EmpPosOrgRelHelper.queryEmpPosOrgRelByEmployeesMap(personIds);
            List<Long> allEmpPosOrgRelIds = empPosOrgRelByEmployeesMap.values().stream()
                    .map(result -> result.getLong(FormConstant.ID_KEY))
                    .collect(Collectors.toList());

            Map<String, Object> adjFileParams = new HashMap<>();
            adjFileParams.put("employees", personIds);
            List<String> status = new ArrayList<>();
            status.add(BillStatus.C.toString());
            adjFileParams.put("status", status);
            //获取人员定薪档案
            Map<String, Object> adjFileResult = DispatchServiceHelper.invokeBizService("swc", "hcdm", "IAdjFileInfoService", "queryAdjFileBoByEmp", adjFileParams);
            Map<Long, Long> adjFileIdMap = new HashMap<>();
            if (ConvertUtil.toBoolean(adjFileResult.get("success"))) {
                List<Map> list = ConvertUtil.toList(adjFileResult.get("data"), ArrayList::new);
                for (Map map : list) {
                    Long id = ConvertUtil.toLong(map.get(FormConstant.ID_KEY));
                    //根据员工任职匹配
                    Long empPosOrgRelId = ConvertUtil.toLong(map.get("empposorgrel_id"));
                    if (allEmpPosOrgRelIds.contains(empPosOrgRelId)) {
                        adjFileIdMap.put(ConvertUtil.toLong(map.get("employee_id")), id);
                    }
                }
            }
            for (ExtendedDataEntity dataEntity : this.getDataEntities()) {
                DynamicObject data = dataEntity.getDataEntity();
                DynamicObjectCollection entrys = data.getDynamicObjectCollection(FormConstant.NCKD_ENTRYENTITY);
                for (DynamicObject entry : entrys) {
                    DynamicObject person = entry.getDynamicObject(FormConstant.NCKD_PERSON);
                    if(person != null) {
                        long personId = person.getLong(FormConstant.ID_KEY);
                        String personName = person.getString(FormConstant.NAME_KEY);
                        if (adjFileIdMap.get(personId) == null) {
                            this.addFatalErrorMessage(dataEntity, StrFormatter.format("人员【{}】定薪档案不存在", personName));
                        }
                    }
                }
            }
        }
    }
}