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

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.operate.result.OperationResult;
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.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.DateUtil;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.swc.constants.SwcConstant;
import nckd.jxccl.swc.onlineperf.common.OnlinePerfConstant;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 解锁在线即时考核
* 实体标识：nckd_onlineperf
* @author W.Y.C
* @date 2025/12/17 21:21
* @version 1.0
*/
public class UnLockOnlinePerfOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

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

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        e.addValidator(new AbstractValidator() {
            /*已创建	1
            未计算	2
            处理中	3
            计算完成	9
            已审核	7
            审批处理中	8
            审批不通过	5
            审批通过	4
            已废弃	6*/
//            1、2、5、6
            @Override
            public void validate() {

                for (ExtendedDataEntity dataEntity : this.getDataEntities()) {
                    DynamicObject data = dataEntity.getDataEntity();
                    boolean isCalDynLock = data.getBoolean(OnlinePerfConstant.NCKD_ISCALDYNLOCK);
                    if(isCalDynLock) {
                        long calRuleId = data.getLong(String.join(".", OnlinePerfConstant.NCKD_CALRULE, FormConstant.ID_KEY));
                        int periodYear = data.getInt(OnlinePerfConstant.NCKD_PERIODYEAR);
                        int periodMonth = data.getInt(OnlinePerfConstant.NCKD_PERIODMONTH);
                        long orgId = data.getLong(String.join(".", OnlinePerfConstant.NCKD_ADMINORGUNIT, FormConstant.ID_KEY));
                        LocalDateTime periodDate = LocalDateTime.of(periodYear, periodMonth, 1, 0, 0);
                        LocalDateTime beginOfMonth = DateUtil.beginOfMonth(periodDate);
                        LocalDateTime endOfMonth = DateUtil.endOfMonth(periodDate);

                        QFilter qFilter = new QFilter(String.join(".", "calrule", FormConstant.ID_KEY), QCP.equals, calRuleId)
                                .and("payrolldate", QCP.large_equals, DateUtil.toDate(beginOfMonth))
                                .and("payrolldate", QCP.less_equals, DateUtil.toDate(endOfMonth))
                                .and(String.join(".", FormConstant.ORG_KEY, FormConstant.ID_KEY), QCP.equals, orgId)
                                .and("taskstatus", QCP.not_in, new String[]{"1", "2", "5", "6"});

                        String selectProperties = QueryFieldBuilder.create().add(FormConstant.ID_KEY).add("taskstatus").buildSelect();
                        DynamicObject[] load = SwcConstant.CALPAYROLLTASK_HELPER.load(selectProperties, new QFilter[]{qFilter});
                        if (load != null && load.length > 0) {
                            this.addFatalErrorMessage(dataEntity, "在线即时考核已被应用于薪酬核算，不能解锁");
                        }
                    }else{
                        this.addFatalErrorMessage(dataEntity, "在线即时考核未锁定，不能解锁");
                    }
                }
            }
        });
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        for (DynamicObject dataEntity : e.getDataEntities()) {
            dataEntity.set(OnlinePerfConstant.NCKD_ISCALDYNLOCK, false);
        }
        SaveServiceHelper.saveOperate(OnlinePerfConstant.ONLINEPERF_ENTITYID, e.getDataEntities(), OperateOption.create());
    }
}