package nckd.jxccl.swc.init.plugin.operate;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.ext.fi.bei.util.EmptyUtil;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.util.StringUtils;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.swc.helper.SWCHelper;

import java.util.ArrayList;

/**
 * 定调薪档案操作数据校验
 * @author: turborao
 * @date: 2025/11/27 10:09
 */
public class SalaryAdjDelivaryDateValidator extends AbstractValidator {

    /**
     * 协作业务对象   定调薪档案
     */
    private static final String CoordBizObject = "hcdm_adjfileinfo";  //协作业务对象   定调薪档案


    @Override
    public void validate() {

        //变动大类
        ArrayList<Long> chgeventList = new ArrayList<>();
        chgeventList.add(1010L);  //入职
        chgeventList.add(1020L);  //调动

        ExtendedDataEntity[] dataEntities = this.getDataEntities();//获取操作执行时的所有单据数据的集合
        String billType = "";
        String hrorgid = "";   ///薪酬管理组织
        for(ExtendedDataEntity obj :dataEntities) {
            DynamicObject bill = obj.getDataEntity();//获取当前单据的数据包
            String employeeName = bill.getString("nckd_employeefield.name");

            String billstatus = bill.getString("billstatus");
            if(!billstatus.equals("A")){
                this.addErrorMessage(obj,"员工：" + employeeName + "，单据状态不是待处理状态，请重新选择！");
            }
            if(StringUtils.isNotEmpty(billType) && !billType.equals(bill.getString("nckd_billtype"))){
                this.addErrorMessage(obj,"员工：" + employeeName + "，定调薪类型不一致，请重新选择！");
            }
            if(StringUtils.isNotEmpty(hrorgid) && !hrorgid.equals(bill.getString("nckd_hrorg.id"))){
                this.addErrorMessage(obj,"员工：" + employeeName + "，薪酬管理组织不一致，请重新选择！");
            }
            billType = bill.getString("nckd_billtype");
            hrorgid = bill.getString("nckd_hrorg.id");

            Long positionId = bill.getLong("nckd_newposition.id");
            Long employeeId = bill.getLong("nckd_employeefield.id");
            QFilter qFilter = new QFilter("employeev.boid", QCP.equals, employeeId);
            QFilter qFilter1 = new QFilter("coordruleparam.chgevent_z.id", QCP.in, chgeventList); // 启用
            QFilter qFilter2 = new QFilter("dealresult", QCP.equals, "8"); // 变动状态  已处理
            QFilter qFilter3 = new QFilter("coordbizobject.number", QCP.equals, CoordBizObject);  //协作业务对象   定调薪档案
            String selectField = "id,employeev.boid,coordruleparam.chgevent_z.id,dealresult";
            DynamicObject employeesFiles = QueryServiceHelper.queryOne(FormConstant.HPDI_EMPCOORDVERIFBILL2, selectField, new QFilter[]{qFilter,qFilter1,qFilter2,qFilter3});
            if(EmptyUtil.isEmpty(employeesFiles) ){
                this.addErrorMessage(obj,"员工：" + employeeName + "，定调薪档案不存在，请先完成员工变动及处理！");
            }else{
                DynamicObject salaryfile  = SWCHelper.getSalaryFileInfo(employeeId,positionId);
                if(EmptyUtil.isEmpty(salaryfile)){
                    this.addErrorMessage(obj,"员工：" + employeeName + "，定调薪档案不存在，请先完成员工档案！");
                }
            }
        }
    }
}
