package com.kingdee.eas.custom.perfweb.planexecute.handler;

import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsHeader;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.perfweb.app.base.evalplan.*;
import com.kingdee.shr.perfweb.app.common.*;
import com.kingdee.shr.perfweb.bean.MsgBean;
import com.kingdee.shr.perfweb.bean.MsgConfigBean;
import com.kingdee.shr.perfweb.bean.ReMsgBean;
import com.kingdee.shr.perfweb.planexecute.handler.EvaResultSummaryManagerListHandler;
import com.kingdee.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description 考核结果管理ListHandler扩展
 * @Date 2025/4/9 15:53
 * @Created by Heyuan
 */
public class EvaResultSummaryManagerListHandlerEx extends EvaResultSummaryManagerListHandler {
    private static Logger logger = Logger.getLogger(EvaResultSummaryManagerListHandlerEx.class);

    /**
     * 推送面谈通知
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     */
    public void sendInterviewNoticeAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap,
            BatchMessageTipsHeader batchMessageTipsHeader
    ) throws SHRWebException {

        Context ctx = SHRContext.getInstance().getContext();
        String billIds = this.getBillId(request);
        logger.error("sendInterviewNoticeAction -> billIds : " + billIds);
        if (StringUtils.isEmpty(billIds)) {
            throw new ShrWebBizException("请先选中数据!");
        } else {
            try {
                MsgConfigBean config = new MsgConfigBean();
                //初始化配置
                this.initMsgConfig(config, request);
                Set<String> set = AtsUtil.toSet(billIds);
                //消息发送任务池
                IPerfSendMessageTaskPool iPerfSendMessageTaskPool = PerfSendMessageTaskPoolFactory.getLocalInstance(ctx);
                IPerfMessageRecord iPerfMessageRecord = PerfMessageRecordFactory.getLocalInstance(ctx);
                IEvaResultSummary iEvaResultSummary = EvaResultSummaryFactory.getLocalInstance(ctx);
                //考核结果
                SelectorItemCollection sic = new SelectorItemCollection();
                sic.add("evaObj.*");
                sic.add("evaObj.perfFile.adminOrgUnit.responPosition");
                sic.add("evaObj.perfFile.position.id");
                sic.add("evaObj.hrUnitOrg.id");
                sic.add("evaPlan.id");
                sic.add("evaPlan.name");
                sic.add("period.id");
                sic.add("period.name");
                sic.add("lastGradeLevel");
                FilterInfo fi = new FilterInfo();
                fi.getFilterItems().add(new FilterItemInfo("id", set, CompareType.INCLUDE));
                EvaResultSummaryCollection summaryColl =
                        iEvaResultSummary.getEvaResultSummaryCollection(EntityViewInfo.getInstance(fi, sic, null));
                MsgBean msgBean = new MsgBean(config.getMsgTempId(), false);
                for (int i = 0; i < summaryColl.size(); ++i) {
                    EvaResultSummaryInfo info = summaryColl.get(i);
                    //获取接收人
                    HashSet<String> receivers = this.getEvaResultMessageReceiver(ctx, info.getEvaObj(), config.getParams());
                    HashMap<String, Object> param = new HashMap();
                    HashMap<String, String> customParam = new HashMap();
                    //考核周期
                    param.put("perfperiod", info.getPeriod().getId().toString());
                    //考核计划
                    param.put("perfplan", info.getEvaPlan().getId().toString());
                    //考核对象
                    param.put("objPerson", info.getEvaObj().getPerson().getId().toString());
                    //HR组织
                    param.put("hrOrgUnitId", info.getEvaObj().getHrUnitOrg() != null ? info.getEvaObj().getHrUnitOrg().getId().toString() : "");
                    //
                    param.put("evaObjId", info.getEvaObj() != null ? info.getEvaObj().getId().toString() : "");
                    //自定义参数
                    customParam.put("[[绩效等级]]", info.getLastGradeLevel() != null ? info.getLastGradeLevel() : "");
                    for (String receiver : receivers) {
                        config.setPhoneContent("【" + info.getEvaPlan().getName() + "-" + info.getPeriod().getName() + "】");
                        param.put("config", config);
                        msgBean.add(info.getEvaObj().getId().toString(), customParam, param, receiver);
                    }
                }
                //设置消息内容
                Collection<ReMsgBean> reMsgBeans = msgBean.excute(ctx);
                CoreBaseCollection cool = new CoreBaseCollection();
                CoreBaseCollection poolColl = new CoreBaseCollection();
                for (ReMsgBean reMsgBean : reMsgBeans) {
                    //组装消息
                    ReminderMessageFacadeControllerBean.asmbleMsg(ctx, poolColl, cool, reMsgBean);
                }
                //消息记录
                iPerfMessageRecord.addnew(cool);
                //消息发送任务池
                iPerfSendMessageTaskPool.addnew(poolColl);
            } catch (Exception e) {
                logger.error("sendInterviewNotice failure: ", e);
                List<String> ids = AtsUtil.getListFromStringArr(billIds.split(","));
                addBatchMessageTips(ids, "面谈通知发送失败", batchMessageTipsHeader);
            }
        }
    }
}
