package nckd.jxccl.swc.stm.plugin.operate.grpttlwg;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.entity.validate.BillStatus;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.QueryFieldBuilder;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.swc.constants.StmConstant;

/**
* 集团工资总额预算申报调整-保存
* 实体标识：nckd_grpttlwgbgtadj
* @author W.Y.C
* @date 2025/12/13 13:06
* @version 1.0
*/
public class GrpTtlWgGgtSubSaveOpPlugin 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() {

            @Override
            public void validate() {
                String entityKey = this.entityKey;
                if(entityKey.equalsIgnoreCase(StmConstant.GRPTTLWGBGTADJ_ENTITYID)) {
                    for (ExtendedDataEntity dataEntity : this.getDataEntities()) {
                        DynamicObject data = dataEntity.getDataEntity();
                        long id = data.getLong(FormConstant.ID_KEY);
                        DynamicObject unitStBudgetApply = data.getDynamicObject(StmConstant.GRPTTLWGBGTSUB_ENTITYID);
                        if (unitStBudgetApply != null) {
                            // 校验有没有暂存或进行中的调整单
                            QueryFieldBuilder queryFieldBuilder = QueryFieldBuilder.create()
                                    .add(FormConstant.BILL_NO_KEY);
                            QFilter filter = new QFilter(FormConstant.BILL_STATUS_KEY, QCP.not_equals, BillStatus.C.toString())
                                    .and(String.join(".", StmConstant.GRPTTLWGBGTSUB_ENTITYID, FormConstant.ID_KEY), QCP.in, unitStBudgetApply.getLong(FormConstant.ID_KEY));
                            if (id > 0) {
                                filter.and(FormConstant.ID_KEY, QCP.not_equals, id);
                            }
                            DynamicObjectCollection query = QueryServiceHelper.query(StmConstant.GRPTTLWGBGTADJ_ENTITYID, queryFieldBuilder.buildSelect(), new QFilter[]{filter});
                            if (!query.isEmpty()) {
                                String number = query.get(0).getString(FormConstant.BILL_NO_KEY);
                                this.addFatalErrorMessage(dataEntity, StrFormatter.format("已存在暂存或进行中的调整单【单据号：{}】，请勿重复申请。", number));
                            }
                        }
                    }
                }
            }
        });
    }
}