package nckd.jimin.jyyy.hr.wtc.wtabm.opplugin.web.vaapply;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.util.StringUtils;
import kd.hr.hdm.business.domain.parttime.ParttimeServiceHelper;
import kd.hr.hdm.business.domain.transfer.service.external.PersonExternalService;
import kd.sdk.plugin.Plugin;

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

/**
 * Tyx 2025-04-29
 * 休假变更审批插件
 * 如果是长假单据，审批后反写员工状态为员工当前用工关系状态，同时要撤销该请假员工主职上的所有兼职；
 */
public class VaChangeBillAuditOp extends AbstractOperationServicePlugIn implements Plugin {

    private static String vaUpdate_entity = "wtabm_vaupdate";
    private static String vaApply_entity = "wtabm_vaapply";

    private static final Log log = LogFactory.getLog(VaChangeBillAuditOp.class);

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("nckd_islongleave");
        e.getFieldKeys().add("nckd_iscancelleave");
        e.getFieldKeys().add("parentid");
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
//        DynamicObject[] bills = e.getDataEntities();
//        for(DynamicObject bill : bills) {
//            if(!bill.getBoolean("nckd_islongleave"))
//                continue;
//            Long personId = bill.getLong("personid.id");
//            //查询请假人当前任职用工关系状态
//            String typeNumber = VaApplyBillUtil.getDepEmpInfo(personId);
//            if(!StringUtils.isBlank(typeNumber)) {
//                VaApplyBillUtil.updatePersonType(typeNumber, personId);
//                bill.set("nckd_iscancelleave",true);
//            }
//        }
//        SaveServiceHelper.save(bills);
        super.afterExecuteOperationTransaction(e);
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        DynamicObject[] bills = e.getDataEntities();
        for(DynamicObject bill : bills) {
            if(!bill.getBoolean("nckd_islongleave"))
                continue;
            Long personId = bill.getLong("personid.id");
            //查询请假人当前任职用工关系状态
            String typeNumber = VaApplyBillUtil.getLaborStatusNumber(personId);
            if(!StringUtils.isBlank(typeNumber)) {
                VaApplyBillUtil.updatePersonType(typeNumber, personId);
                //反写源单是否已销假为是
                Long parentId = bill.getLong("parentid");
                DynamicObject parentBill = BusinessDataServiceHelper.loadSingle(parentId, vaApply_entity);
                parentBill.set("nckd_iscancelleave",true);
                SaveServiceHelper.save(new DynamicObject[]{parentBill});
            }

            DynamicObject position = VaApplyBillUtil.getPersonPrimaryPosition(personId);
            List<Long> positionIds = new ArrayList<Long>();
            positionIds.add(position.getLong("id"));
            DynamicObject[] ermanFiles = VaApplyBillUtil.getErmanFilesByPosition(positionIds);
            //转成List<Long>
            List<Long> ermanFileIds = Arrays.stream(ermanFiles).map(dyx -> dyx.getLong("id")).collect(Collectors.toList());
            //调用标准方法获取ermanFile
            List<Map<String, Object>> erManFiles = PersonExternalService.getInstance().invokeGetCardFields(ermanFileIds);
            //actionId 默认201100_S
            QFilter filter = new QFilter("number", "=", "201100_S");
            DynamicObject action = BusinessDataServiceHelper.loadSingle("hpfs_chgaction",new QFilter[]{filter});
            Date partEndDate = new Date();
            //调用标准终止兼职方法
            ParttimeServiceHelper.batchEndParttime(action.getLong("id"), partEndDate, erManFiles);
        }
        super.beginOperationTransaction(e);
    }
}