package nckd.jxccl.wtc.wtabm.web.vaapply;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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.EndOperationTransactionArgs;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.wtc.wtabm.web.common.constant.WtcConstant;

import java.util.Date;

/**
 * Tyx
 * 2025-09-28
 * 休假申请审核操作，执行以下操作：
 * 1.如果是休婚假，回写人员档案-基本信息-婚姻状况为二婚，回写人员档案-基本信息-婚姻登记日期为单据上二开填写字段婚姻日期；
 */
public class VaApplyAuditOpPluginEx extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("entryentity.nckd_weddingdate");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);

    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        DynamicObject[] bills = e.getDataEntities();
        for (DynamicObject bill : bills) {
            DynamicObjectCollection entryCols = bill.getDynamicObjectCollection("entryentity");
            String vaTypeNumber = entryCols.get(0).getString("entryvacationtype.number");
            // 如果请婚假才执行
            if(vaTypeNumber.equals(WtcConstant.MARRIAGE_NUMBER)) {
                // 查询人员信息
                Long employeeId = bill.getLong("attfilebasef7.employee.id");
                DynamicObject employeeDyn = WtcConstant.EMPLOYEE_HELPER.loadOne(employeeId);
                // 结婚日期
                Date weddingDate = entryCols.get(0).getDate("nckd_weddingdate");
                employeeDyn.set("marriageregistdate", weddingDate);
                employeeDyn.set("marriagestatus", WtcConstant.STATUS_MARRIAGE);
                SaveServiceHelper.update(employeeDyn);
            }
        }
    }
}