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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.constant.StatusEnum;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeSaveAuditLogArg;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.hr.hstu.business.helper.HonorStudentHelper;
import nckd.jxccl.hr.hstu.common.HonorStudentConstant;

/**
* 【优秀生】-综合测评答卷-反审核操作插件
* @author W.Y.C
* @date 2025/8/1 17:28
* @version 1.0
*/
public class EvalResultUnAuditOperationPlugin extends AbstractOperationServicePlugIn implements Plugin {

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

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        //非预置字段需要用到要到这声明才能用
        e.getFieldKeys().add(HonorStudentConstant.NCKD_PERFPLANSTATE);
        e.getFieldKeys().add(HonorStudentConstant.NCKD_HOWDOMARK);
        e.getFieldKeys().add(HonorStudentConstant.NCKD_EVALQUEST);
    }

    @Override
    public void beforeSaveAuditLog(BeforeSaveAuditLogArg e) {
        super.beforeSaveAuditLog(e);
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntity : dataEntities) {
            //反审批审批时同步修改评分状态为：待评分
            dataEntity.set(HonorStudentConstant.STATUS, StatusEnum.B.toString());
            dataEntity.set(HonorStudentConstant.NCKD_PERFPLANSTATE, "1");
            dataEntity.set(HonorStudentConstant.NCKD_HOWDOMARK, null);
        }
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        //数据已入库但还未提交（才会包含当前保存的答卷）前计算同级评分人数、上级评分人数、上级评分人数、同级评分总分、上级评分总分、领导评分总分、已评分总分、已评分人数、未评分人数
        DynamicObject[] dataEntities = e.getDataEntities();
        Long[] evalQuestIds = new Long[dataEntities.length];
        for (int i = 0; i < dataEntities.length; i++) {
            DynamicObject dataEntity = dataEntities[i];
            long evalQuestId = dataEntity.getDynamicObject(HonorStudentConstant.NCKD_EVALQUEST).getLong(FormConstant.ID_KEY);
            evalQuestIds[i] = evalQuestId;
        }
        //更新问卷评分结果
        //原SHR逻辑只更新：已评分总分、领导评分人数、领导评分总分、上级评分人数、上级评分总分、同级评分人数、同级评分总分、已评分总分、已评分人数(含不了解)、未评分人数
        HonorStudentHelper.updateQuestResult(evalQuestIds);
    }
}