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

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
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;
import nckd.jxccl.hr.hstu.plugin.operate.validate.EvalResultPerfScoreValidate;

import java.util.Arrays;
import java.util.Date;

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

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

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        //评价分数校验
        e.addValidator(new EvalResultPerfScoreValidate());
    }

    @Override
    public void beginOperationTransaction(BeginOperationTransactionArgs e) {
        logger.info("【优秀生考评】-问卷提交评分-开始", Arrays.toString(e.getDataEntities()));
        //开始事务前填充需要设置的数据
        DynamicObject[] dataEntities = e.getDataEntities();
        String userAgent = RequestContext.get().getUserAgent();
        logger.info("【优秀生考评】-问卷提交评分-userAgent：{}",userAgent);
        for (DynamicObject dataEntity : dataEntities) {
            //审批时同步修改评分状态为：已评分
            dataEntity.set(HonorStudentConstant.NCKD_PERFPLANSTATE, "2");
            if(userAgent.contains("Windows")) {
                dataEntity.set(HonorStudentConstant.NCKD_HOWDOMARK, "PC端提交评分");
            }else{
                dataEntity.set(HonorStudentConstant.NCKD_HOWDOMARK, "移动端提交评分");
            }
            //设置评分时间
            dataEntity.set(HonorStudentConstant.NCKD_SCORETIME, new Date());
            //选择不了解清空分数
            boolean unclear = dataEntity.getBoolean(HonorStudentConstant.NCKD_UNCLEAR);
            if(unclear){
                DynamicObjectCollection dynamicObjectCollection = dataEntity.getDynamicObjectCollection(HonorStudentConstant.NCKD_EVALRESULTENTRY);
                for (DynamicObject dynamicObject : dynamicObjectCollection) {
                    dynamicObject.set(HonorStudentConstant.NCKD_PERFSCORES, 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);
        logger.info("【优秀生考评】-问卷结果提交评分-结束");
    }
}