package nckd.jxccl.swc.mas.plugin.operate.empmgt;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.entity.validate.ValidatePriority;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;

/**
* 子企业负责人任职情况-保存插件
* 实体标识：nckd_subcoheadservice
* @author W.Y.C
* @date 2025/11/27 22:25
* @version 1.0
*/
public class SubCoHeadServiceOpPlugin extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        //通过“载入党政职务履历”
        Boolean isLoad = ConvertUtil.toBoolean(this.getOption().getVariableValue("load", "false"));
        e.addValidator(new AbstractValidator() {

            @Override
            public void setValidatePriority(ValidatePriority validatePriority) {
                super.setValidatePriority(ValidatePriority.Last);
            }

            @Override
            public void validate() {
                String operateKey = this.getOperateKey();
                for (ExtendedDataEntity dataEntity : this.getDataEntities()) {
                    DynamicObject data = dataEntity.getDataEntity();
                    DynamicObject employee = data.getDynamicObject(FormConstant.NCKD_EMPLOYEE);
                    if(employee != null) {
                        //设置每行对应的员工名称，用于准确显示每个错误属于哪个员工的
                        getOperationResult().getCustomData().put("DataEntityIndex_"+dataEntity.getDataEntityIndex(), "【" + employee.getString(FormConstant.NAME_KEY) + "】");
                    }
                }
            }
        });
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        String operationKey = e.getOperationKey();
        for (DynamicObject dataEntity : e.getDataEntities()) {
            long id = dataEntity.getLong(FormConstant.ID_KEY);
            DynamicObject dynamicObject = dataEntity.getDynamicObject(FormConstant.NCKD_EMPLOYEE);
            if(dynamicObject != null) {
                this.getOperationResult().getCustomData().put(id + "", "【" + dynamicObject.getString(FormConstant.NAME_KEY) + "】");
            }
        }
    }
}