package nckd.jxccl.hr.psms.plugin.operate.other;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.constant.StatusEnum;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.hrpi.helper.EmpPosOrgRelHelper;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;
import nckd.jxccl.hr.psms.helper.PositionFileHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* 配置有职位津贴的管理人员-保存op
* 实体标识：nckd_managerallowance
* @author W.Y.C
* @date 2025/12/6 14:44
* @version 1.0
*/
public class ManagerAllowanceSaveOp extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().addAll(this.billEntityType.getAllFields().keySet());
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        String variableValue = this.getOption().getVariableValue("importtype", "false");
        boolean isImport = !variableValue.equalsIgnoreCase("false");
        List<Long> personIdList = new ArrayList<>();
        for (DynamicObject dataEntity : e.getDataEntities()) {
            long personId = dataEntity.getLong(String.join(".", FormConstant.NCKD_PERSON, FormConstant.ID_KEY));
            personIdList.add(personId);

        }
        Map<Long, DynamicObject> empPosOrgRelMap = new HashMap<>();
        Map<Long, DynamicObject> latestPersonPosFileMap = new HashMap<>();
        if(!personIdList.isEmpty()){
            empPosOrgRelMap = EmpPosOrgRelHelper.queryEmpPosOrgRelByEmployeesMap(personIdList);

            DynamicObject[] newestPersonPosFileByPerson = PositionFileHelper.getNewestPersonPosFileByPerson(personIdList.toArray(new Long[0]));
            latestPersonPosFileMap = Arrays.stream(newestPersonPosFileByPerson)
                    .collect(Collectors.toMap(personPosFile -> personPosFile.getLong(String.join(".", FormConstant.NCKD_PERSON, FormConstant.ID_KEY)), Function.identity(), BinaryOperator.maxBy(Comparator.comparing(
                            personPosFile -> personPosFile.getDate(PositionStructureConstant.NCKD_BEGINDATE)))));

        }

        for (DynamicObject dataEntity : e.getDataEntities()) {
            long personId = dataEntity.getLong(String.join(".", FormConstant.NCKD_PERSON, FormConstant.ID_KEY));
            DynamicObject empPosOrgRel = empPosOrgRelMap.get(personId);
            if(dataEntity.getDynamicObject(FormConstant.NCKD_DEP) == null){
                dataEntity.set(FormConstant.NCKD_DEP, empPosOrgRel.get(FormConstant.ADMINORG));
            }
            if(dataEntity.getDynamicObject(PositionStructureConstant.NCKD_POSITIONHR) == null){
                dataEntity.set(PositionStructureConstant.NCKD_POSITIONHR, empPosOrgRel.get(FormConstant.POSITION_KEY));
            }
            if(isImport){
                //导入的默认未“已确认”
                dataEntity.set(FormConstant.STATUS, StatusEnum.B.toString());
            }
            DynamicObject personPosFile = latestPersonPosFileMap.get(personId);
            if(personPosFile != null){
                if(dataEntity.getDynamicObject(PositionStructureConstant.NCKD_JOBLEVELHR) == null){
                    dataEntity.set(PositionStructureConstant.NCKD_JOBLEVELHR, personPosFile.get(PositionStructureConstant.NCKD_JOBLEVELHR));
                }
                if(dataEntity.getDynamicObject(PositionStructureConstant.NCKD_PROTITLELEVEL) == null){
                    dataEntity.set(PositionStructureConstant.NCKD_PROTITLELEVEL, personPosFile.get(PositionStructureConstant.NCKD_PROTITLELEVEL));
                }
            }
        }
    }
}