package com.kingdee.eas.custom.performancenew.task;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.notify.NotifyTodoFactory;
import com.kingdee.eas.custom.notify.SendContextInfo;
import com.kingdee.eas.custom.performancenew.*;
import com.kingdee.eas.custom.utils.PropertiesUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.kingdee.bos.*;

import com.kingdee.eas.common.EASBizException;

import java.lang.String;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.*;

public class PerformancenewFacadeControllerBean extends AbstractPerformancenewFacadeControllerBean {
    private static Logger logger =
            Logger.getLogger("com.kingdee.eas.custom.performancenew.task.PerformancenewFacadeControllerBean");
    public String linkTemplate = "%s/dynamic.do?uipk=%s&inFrame=true&examineGradePersonId=%s";

    /**
     * 绩效考核正态分布 催办
     *
     * @param ctx
     * @param periodId 绩效考核周期ID
     * @return
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected String _warnScorer(Context ctx, String periodId) throws BOSException, EASBizException {
        //获取shr域名
        String shrAddress = PropertiesUtil.getConfigValue("shrAddress");
        //计算正态分布得分Uipk
        String examineGradeUipk = PropertiesUtil.getConfigValue("examineGradeUipk");
        StringBuilder resultMsg = new StringBuilder();
        int success = 0;
        int fail = 0;
        IExamineGradePersonEntry iExamineGradePersonEntry = ExamineGradePersonEntryFactory.getLocalInstance(ctx);
        FilterInfo filterInfo = new FilterInfo();
        FilterItemCollection filterItems = filterInfo.getFilterItems();
        filterItems.add(new FilterItemInfo("orderStatuc", 1));
        if (!StringUtils.isEmpty(periodId)) {
            filterItems.add(new FilterItemInfo("parent.id", periodId));
        }
        SelectorItemCollection sic = new SelectorItemCollection();
        sic.add("scorer.oaUserName");
        sic.add("scorer.name");
        sic.add("scorer.id");
        sic.add("adminOrg.number");
        sic.add("adminOrg.name");
        sic.add("parent.year");
        sic.add("parent.quarter");
        EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, sic, null);
        ExamineGradePersonEntryCollection examineGradePersonEntryCollection =
                iExamineGradePersonEntry.getExamineGradePersonEntryCollection(entityViewInfo);
        Set<String> set = new HashSet<>();
        List<ExamineGradePersonEntryInfo> list = new ArrayList();
        for (int i = 0; i < examineGradePersonEntryCollection.size(); i++) {
            ExamineGradePersonEntryInfo entry = examineGradePersonEntryCollection.get(i);
            String scorerId = entry.getScorer().getId().toString();
            String adminOrgId = entry.getAdminOrg().getId().toString();
            if (!set.contains(scorerId + adminOrgId)) {
                set.add(scorerId + adminOrgId);
                list.add(entry);
            }
        }
        for (ExamineGradePersonEntryInfo entryInfo : list) {
            try {
                ExamineGradePersonInfo parent = entryInfo.getParent();
                //周期年度季度
                int year = parent.getYear();
                int quarter = parent.getQuarter();
                String msg = year + "年" + quarter + "季度";
                PersonInfo scorer = entryInfo.getScorer();
                String scorerName = scorer.getName();
                //oa用户名
                String oaUserName = scorer.getString("oaUserName");
                if (StringUtils.isEmpty(oaUserName)) {
                    throw new BOSException("周期[" + msg + "]，评分人[" + scorerName + "]的oa用户名为空!");
                }
                //发送待办
                SendContextInfo context = new SendContextInfo();
                context.setAppName("shr");
                context.setModelName("shr_正态分布模块");
                context.setModelId(UUID.randomUUID().toString());
                StringBuilder subject = new StringBuilder();
                subject.append("请您尽快提交 周期[" + msg + "]，计算正态分布得分的考核人员工明细!");
                context.setSubject(subject.toString());
                String periodIdUrlID = URLEncoder.encode(periodId);
                String link = String.format(linkTemplate, shrAddress, examineGradeUipk, periodIdUrlID);
                context.setLink(link);
                //context.setType();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("LoginName", oaUserName);
                context.setTargets(jsonObject.toJSONString());
                context.setCreateTime(new Timestamp(new Date().getTime()));
                NotifyTodoFactory.getLocalInstance(ctx).sendTodo(context);
                success++;
            } catch (Exception e) {
                e.printStackTrace();
                fail++;
            }
        }
        resultMsg.append("待办任务总数").append(list.size()).append("条，");
        resultMsg.append("催办成功").append(success).append("条，");
        resultMsg.append("失败").append(fail).append("条。");
        return resultMsg.toString();
    }
}