package nckd.jxccl.wtc.wtom.opplugin.web.otapplybill;

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.BeginOperationTransactionArgs;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.wtc.helper.WTCHelper;
import nckd.jxccl.wtc.wtabm.web.common.constant.WtcConstant;

import java.util.Date;

/**
 * Tyx 2025-12-02 <br/>
 * 加班单校验假期操作插件 <br/>
 * 1、新建假期基础资料 nckd_holidayforot <br/>
 * 2、加班单提交判断是否在假期范围内 <br/>
 * 3、如果是则判断假期范围内的加班天数是否超过节假日天数，如果超过，则按休息日加班提交，否则按节假日加班提交 <br/>
 */
public class OtApplyCheckHolidayOp extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        super.beginOperationTransaction(e);
        DynamicObject[] bills = e.getDataEntities();
        for(DynamicObject bill : bills) {
            // 考勤档案
            DynamicObject attFile = bill.getDynamicObject("attfile");
            // 单据ID
            Long billId = bill.getLong("id");
            // 分录
            DynamicObjectCollection entryCols = bill.getDynamicObjectCollection("sdentry");
            for(DynamicObject entry : entryCols) {
                // 加班归属日期
                Date otDutyDate = entry.getDate("otdutydate");
                DynamicObject holidayDyn = WTCHelper.getHolidayByOtDutyDate(otDutyDate);
                if(holidayDyn == null)
                    continue;
                // 如果查询到在假期范围内，则查询假期范围内总共请了多少天，排除本张加班单
                Date startDate = holidayDyn.getDate("nckd_startdate");
                Date endDate = holidayDyn.getDate("nckd_enddate");
                // 已加班天数
                int otDays = WTCHelper.getOTDays(startDate, endDate, attFile.getLong("id"), billId);
                // 当前单据加班天数
                int curOtDays = 1;
                // 如果已加班天数 + 当前单据加班天数 > 假期天数，则按休息日加班提交，否则按节假日加班提交
                if(otDays + curOtDays > holidayDyn.getInt("nckd_holidays")) {
                    DynamicObject otType = WTCHelper.getSdOtType(WtcConstant.OTTYPE_1020S);
                    entry.set("sdottype", otType);
                }
                else {

                }
            }
        }
    }
}