package nckd.jxccl.hr.psms.plugin.operate.other;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
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.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.enums.psms.TypeStateEnum;
import nckd.jxccl.base.common.exception.ValidationException;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.hr.psms.common.PositionStructureConstant;

import java.util.Date;

/**
 * 终止管理人员任命
 * 实体标识：nckd_mgrappointmgmt
 * @author W.Y.C
 * @date 2025/12/4 21:25
 * @version 1.0
 */
public class EndAppointMentOpPlugin 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() {
                for (ExtendedDataEntity rowDataEntity : getDataEntities()) {
                    DynamicObject data = rowDataEntity.getDataEntity();
                    DynamicObject person = data.getDynamicObject(PositionStructureConstant.NCKD_PERSON);
                    String name = person.getString(PositionStructureConstant.NAME_KEY);
                    String typeState = data.getString(PositionStructureConstant.NCKD_TYPESTATE);
                    String appointStatus = data.getString(PositionStructureConstant.NCKD_APPOINTSTATUS);
                    if(!"1".equalsIgnoreCase(appointStatus)){
                        this.addErrorMessage(rowDataEntity, StrFormatter.format("【{}】状态为【已终止】，忽略此条处理！",name));
                    }
                }
            }
        });
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        for (DynamicObject dataEntity : e.getDataEntities()) {
            dataEntity.set(PositionStructureConstant.NCKD_APPOINTSTATUS, "0");
            Date endDate = dataEntity.getDate(PositionStructureConstant.NCKD_ENDDATE);
            if (endDate == null) {
                dataEntity.set(PositionStructureConstant.NCKD_ENDDATE, new Date());
            }else{
                dataEntity.set(PositionStructureConstant.NCKD_ENDDATE, endDate);
            }
        }
        OperationResult operationResult = SaveServiceHelper.saveOperate(PositionStructureConstant.PERSONPOSFILE_ENTITYID, e.getDataEntities(), OperateOption.create());
        if (!operationResult.isSuccess()) {
            throw new ValidationException(operationResult.getMessage());
        }
    }
}