package nckd.jxccl.hr.hstu.task;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.exception.KDBizException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.QueryServiceHelper;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.schedule.ProgressCallback;
import nckd.jxccl.base.common.utils.ConvertUtil;
import nckd.jxccl.base.org.helper.PersonHelper;
import nckd.jxccl.base.orm.helper.QFilterCommonHelper;
import nckd.jxccl.hr.hstu.business.CreateEvalQuestService;
import nckd.jxccl.hr.hstu.common.HonorStudentConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 生成评价对象异步任务
 * @author W.Y.C
 * @date 2025/7/1 15:26
 * @version 1.0
 */
public class CreateEvalQuestTask extends AbstractTask implements ProgressCallback {

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

    private int totalItems;

    @Override
    public void execute(RequestContext ctx, Map<String, Object> params) {
        try {

            Map<String, Object> evalQuestTplMap = ConvertUtil.toMap(params.get(HonorStudentConstant.NCKD_EVALQUESTTPL));
            Map<String, Object> selectedEvalRuleMap = ConvertUtil.toMap(params.get(HonorStudentConstant.NCKD_SELECTEDEVALRULE));
            List<Map<String,Object>> selectedPersonList = ConvertUtil.toList(params.get(HonorStudentConstant.NCKD_SELECTEDPERSON));
            Long crrUserId = ConvertUtil.toLong(params.get(HonorStudentConstant.KEY_CURR_USER_ID));
            Long currOrgId = ConvertUtil.toLong(params.get(HonorStudentConstant.CURR_ORG_ID));
            totalItems = selectedPersonList.size();


            DynamicObject evalQuestTpl = QueryServiceHelper.queryOne(HonorStudentConstant.NCKD_EVALQUESTTPL_ENTITYID, FormConstant.ID_KEY, new QFilter[]{QFilterCommonHelper.getIdEqFilter(evalQuestTplMap.get(HonorStudentConstant.ID_KEY))});
            DynamicObject selectedEvalRule = QueryServiceHelper.queryOne(HonorStudentConstant.EVALUATIONRULE_ENTITYID, FormConstant.ID_KEY, new QFilter[]{QFilterCommonHelper.getIdEqFilter(selectedEvalRuleMap.get(HonorStudentConstant.ID_KEY))});
            List<Long> idList = new ArrayList<>();
            selectedPersonList.forEach(map ->{
                Map<String, Object> fbasedataid = (Map<String, Object>) map.get(HonorStudentConstant.BASEDATAID_KEY);
                idList.add(ConvertUtil.toLong(fbasedataid.get(HonorStudentConstant.ID_KEY)));
            } );
            DynamicObjectCollection selectedPerson = QueryServiceHelper.query(PersonHelper.DEP_EMP_ENTITY_ID, "id,person.name",new QFilter[]{QFilterCommonHelper.getIdInFilter(idList)});
            CreateEvalQuestService.batchGenerateEvalQuest(evalQuestTpl, selectedEvalRule, selectedPerson, this, crrUserId, currOrgId);

            feedbackProgress(100, "任务完成", null);
        } catch (Exception e) {
            logger.error("【优秀生考评】-生成综合评测问卷-系统异常：{}",e);
            throw new KDBizException("任务执行失败：" + e.getMessage());
        }
    }


    @Override
    public void onProgress(int processedCount) {
        int progress = (processedCount * 100) / totalItems;
        String desc = "生成中：" + processedCount + "/" + totalItems;
        feedbackProgress(progress, desc, null);
    }
}
