package nckd.jxccl.hr.hstu.plugin.form;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.constant.StatusEnum;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.operate.Delete;
import kd.bos.entity.operate.OperateOptionConst;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.hr.hstu.common.HonorStudentConstant;

import java.util.Date;

/**
* 优秀生-评价规则
* @author W.Y.C
* @date 2025/6/18 14:26
* @version 1.0
*/
public class EvaluationRuleListPlugin extends AbstractListPlugin implements Plugin {


    private final static Log logger = LogFactory.getLog(EvaluationRuleListPlugin.class);

    /** 标记开始 */
    public static final String OP_BEGIN = "begin";
    /** 标记结束 */
    public static final String OP_END = "end";
    /** 查看优秀生评价 */
    public static final String OP_PERFPLAN = "PerfPlan";

    private final String[] PERIOD_STATE = {"未开始","进行中","已结束","已就绪","已废弃"};

    //开始：触发->提交、审核，设置开始时间为当前
    //结束：触发->禁用，判断是否存在未评分的评价对象->设置结束时间
    //删除：判断是否是未开始的状态，判断是否被使用
    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        FormOperate operate = (FormOperate) e.getSource();
        String operateKey = operate.getOperateKey();
        //获取选中行数据的《使用状态》和《数据状态》
        ListSelectedRowCollection selectedRows = e.getListSelectedData();
        if(!selectedRows.isEmpty()) {
            Long billId = (Long) selectedRows.get(0).getPrimaryKeyValue();
            OperateOption option = OperateOption.create();
            //提交、审批op事件不提示消息
            option.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, Boolean.FALSE+"");
            if (OP_BEGIN.equals(operateKey)) {
                //标记开始：触发-启用->自动提交->审核->赋值开始时间
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(HonorStudentConstant.EVALUATIONRULE_ENTITYID, new QFilter[]{new QFilter("id", QCP.equals, billId)});
                String periodState = dynamicObject.getString(HonorStudentConstant.NCKD_PERIODSTATE);
                if(!"0".equals(periodState)){
                    this.getView().showMessage(StrFormatter.format("该规则【{}】，不能标记开始",PERIOD_STATE[ConvertUtil.toInt(periodState)]));
                    e.setCancel(true);
                    return;
                }
                String status = dynamicObject.getString(HonorStudentConstant.STATUS);
                String enable = dynamicObject.getString(HonorStudentConstant.ENABLE);
                this.getView().invokeOperation(HonorStudentConstant.ENABLE_OP, option);
                this.getView().invokeOperation(HonorStudentConstant.SUBMIT_OP, option);
                this.getView().invokeOperation(HonorStudentConstant.AUDIT_OP, option);
                dynamicObject.set(HonorStudentConstant.NCKD_STARTTIME,new Date());
                dynamicObject.set(HonorStudentConstant.NCKD_PERIODSTATE,new Date());
                dynamicObject.set(HonorStudentConstant.NCKD_PERIODSTATE, StatusEnum.B.ordinal());
                SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
                //刷新列表
                this.getView().invokeOperation(HonorStudentConstant.REFRESH_OP);
                this.getView().showSuccessNotification("操作成功！");
            }else if (OP_END.equals(operateKey)) {
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(HonorStudentConstant.EVALUATIONRULE_ENTITYID, new QFilter[]{new QFilter("id", QCP.equals, billId)});
                String periodState = dynamicObject.getString(HonorStudentConstant.NCKD_PERIODSTATE);
                if(!"1".equals(periodState)){
                    this.getView().showMessage(StrFormatter.format("该规则【{}】，不能标记结束",PERIOD_STATE[ConvertUtil.toInt(periodState)]));
                    e.setCancel(true);
                    return;
                }
                dynamicObject.set(HonorStudentConstant.NCKD_ENDTIME,new Date());
                dynamicObject.set(HonorStudentConstant.NCKD_PERIODSTATE, StatusEnum.C.ordinal());
                SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
                //刷新列表
                this.getView().invokeOperation(HonorStudentConstant.REFRESH_OP);
                this.getView().showSuccessNotification("操作成功！");
            }
        }
        if(operate instanceof Delete){
            //TODO 校验规则是否被使用
        }
    }
}