package com.sys.bus.jx.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Enhancer;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.sys.bus.excel.tool.ExcelRender;
import com.sys.bus.hr.model.Person;
import com.sys.bus.hr.service.HrService;
import com.sys.bus.jx.service.*;
import com.sys.bus.model.*;
import com.sys.bus.sys.model.DtBean;
import com.sys.core.model.PageBean;
import com.sys.core.utils.ConstUtil;
import com.sys.core.utils.R;
import com.sys.core.utils.SessionUtil;
import com.sys.core.utils.Tool;
import javafx.print.Collation;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class PerformanceController extends Controller {
    Log log = LogFactory.getLog(this.getClass());
    static PerformanceService performanceService = Enhancer.enhance(PerformanceService.class);//需要事务处理的service，必须使用增强方法
    static JxCommonService jxCommonService = Enhancer.enhance(JxCommonService.class);
    static JxCycleService jxCycleService = Enhancer.enhance(JxCycleService.class);
    static OrgService orgService =  Enhancer.enhance(OrgService.class);
    static HrService hrService =  Enhancer.enhance(HrService.class);
   // static RatingService ratingService =  Enhancer.enhance(RatingService.class);
    static WflogService wflogService =  Enhancer.enhance(WflogService.class);
    static PersonManagerService personManagerService = Enhancer.enhance(PersonManagerService.class);

    /**
     * 跳转到待处理页面
     */
    @ActionKey("/jxp/toMyToDoListPage")
    public void toMyToDoListPage(){
        //获取该数据是为了提供批量处理时选择的考核组织与考核周期数据
//        List<DtBean> cycleOrgDataList= performanceService.myGradingInToDoList(SessionUtil.getCurrentPerson());
//        List<DtBean> orgData= new ArrayList<DtBean>();
//        List<DtBean> cycleData= new ArrayList<DtBean>();
//        List<String> orgIdList = new ArrayList<String>();
//        List<String> cycleIdList = new ArrayList<String>();
//        for (DtBean cycleOrgData : cycleOrgDataList) {
//            String cycleId = cycleOrgData.get("entry_cycleid");
//            String orgId = cycleOrgData.get("entry_orgid");
//            if(!orgIdList.contains(orgId)){
//                orgIdList.add(orgId);
//                orgData.add(cycleOrgData);
//            }
//            if(!cycleIdList.contains(cycleId)){
//                cycleIdList.add(cycleId);
//                cycleData.add(cycleOrgData);
//            }
//        }
//        this.setAttr("orgData",orgData);
//        this.setAttr("cycleData",cycleData);
        this.setAttr("isQuickManager",performanceService.isQuickManager());
        this.getSession().removeAttribute("jxManager_query");
        this.getSession().removeAttribute("jxAllEntry_query");
        this.render("myToDoList.html");
    }
    /**
     * 获取待处理数据
     */
    @ActionKey("/jxp/getMyToDoList")
    public void getMyToDoList(){
        List<DtBean> dtBeans = performanceService.getMyToDoList(SessionUtil.getCurrentPerson());
        PageBean pageBean = new PageBean();
        pageBean.setData(dtBeans);
        pageBean.setCount(dtBeans.size());
        this.renderJson(pageBean);
    }

    /**
     * 跳转到自评页面
     */
    @ActionKey("/jxp/toSelfEvaluationPage")
    public void toSelfEvaluationPage(){
        String entryid = this.getPara("entryid");
        Person person = SessionUtil.getCurrentPerson();
        Entry jxEntry = jxCommonService.getJxentryById(entryid);
        List<Kpi> kpiList =  performanceService.getSelfEvaluationKpiList(entryid,person);
        if(kpiList.size() == 0){
            throw new RuntimeException("链接错误！请退出重试！");
        }

        //获取评分人打回日志
        wflogService.setBackWfLog(jxEntry,"4");
        //获取专员整体打回日志
        wflogService.setBackWfLog(jxEntry,"6");
        this.setAttr("kpiList",kpiList);
        this.setAttr("jxEntry",jxEntry);
        this.setAttr("apptype","1");//表示员工自评
        this.setAttr("wfLog",wflogService.getWfLog(entryid));
        this.render("jxKpiList.html");
    }
    /**
     * 跳转到改进计划页面
     */
    @ActionKey("/jxp/toMakeBetterPage")
    public void toMakeBetterPage(){
        String entryId = this.getPara("entryId");
        Person person = SessionUtil.getCurrentPerson();
        Entry jxEntry = jxCommonService.getJxentryById(entryId);
        List<Kpi> kpiList =  performanceService.getSelfEvaluationKpiList(entryId,person);
        if(kpiList.size() == 0){
            throw new RuntimeException("链接错误！请退出重试！");
        }
        BigDecimal entryScorelv1 = jxEntry.getEntryScorelv1();
        BigDecimal entryScorelv2 = jxEntry.getEntryScorelv2();
        BigDecimal entryScorelv3 = jxEntry.getEntryScorelv3();
        this.setAttr("lv1",entryScorelv1!=null);
        this.setAttr("lv2",entryScorelv2!=null);
        this.setAttr("lv3",entryScorelv3!=null);

        this.setAttr("kpiList",kpiList);
        this.setAttr("jxEntry",jxEntry);
        this.setAttr("apptype","11");//表示员工改进计划
        this.setAttr("wfLog",wflogService.getWfLog(entryId));
        this.render("jxKpiList.html");
    }
    /**
     * 员工提交改进计划
     */
    @ActionKey("/jxp/submitMakeBetter")
    public void submitMakeBetter() throws Exception {
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());

        performanceService.submitMakeBetter(jsonObj);
        this.renderJson(R.isOK());
    }
    /**
     * 员工自评保存
     */
    @ActionKey("/jxp/saveSelfEvaluation")
    public void saveSelfEvaluation() throws Exception {
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        performanceService.saveSelfEvaluation(jsonObj);
        this.renderJson(R.isOK());
    }
    /**
     * 员工自评提交
     */
    @ActionKey("/jxp/submitSelfEvaluation")
    public void  submitSelfEvaluation() throws Exception {
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        performanceService.submitSelfEvaluation(jsonObj, SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }

    /**
     * 跳转到打分人审核页面
     */
    @ActionKey("/jxp/toGradingPage")
    public void toGradingPage(){
        boolean isBack = false;
        String entryid = this.getPara("entryid");
        Entry jxEntry = jxCommonService.getJxentryById(entryid);
        List<Kpi> kpiList = performanceService.getGradingKpiList(entryid,SessionUtil.getCurrentPerson());
        performanceService.setManagerLvInfo(jxEntry,kpiList);
        //获取打回日志
        wflogService.setBackWfLog(jxEntry,"6");
        BigDecimal kpiWeightSum = new BigDecimal(0);
        this.setAttr("wfLog",wflogService.getWfLog(entryid));
        this.setAttr("isAllKpi",true);
        this.setAttr("isBack",isBack);
        this.setAttr("kpiList",kpiList);
        this.setAttr("jxEntry",jxEntry);
        this.setAttr("apptype","2");//表示评分人评分
        BigDecimal entryScorelv1 = jxEntry.getEntryScorelv1();
        BigDecimal entryScorelv2 = jxEntry.getEntryScorelv2();
        BigDecimal entryScorelv3 = jxEntry.getEntryScorelv3();
        this.setAttr("lv1",entryScorelv1!=null);
        this.setAttr("lv2",entryScorelv2!=null);
        this.setAttr("lv3",entryScorelv3!=null);

        Integer entryAppManagerLv = jxEntry.getEntryAppManagerLv();
        this.setAttr("lv"+entryAppManagerLv,true);
       // this.setAttr("currLv",entryAppManagerLv);
        this.render("jxKpiList.html");
    }

    /**
     * 跳转到快速整体评分界面
     */
    @ActionKey("/jxp/toQuickGradingPage")
    public void toQuickGradingPage(){
        String entryno = this.getPara("entryno");
        List<Entry> jxEntryList = performanceService.getMyToGrading(SessionUtil.getCurrentPerson(), entryno);
        for(Entry entry : jxEntryList){
            performanceService.setManagerLvInfo(entry,new ArrayList<>());
        }
        this.setAttr("jxEntryList", jxEntryList);

        this.render("jxQuickGrading.html");
    }

    /**
     * 打分人批量打分页面
     */
    @ActionKey("/jxp/toGradingInBathPage")
    public void toGradingInBathPage(){
        boolean isBack = false;
        String khOrgid = this.getPara("khOrgid");
        String khcycle = this.getPara("khcycle");
        List<Kpi> kpiList = performanceService.getGradingInBathList(khOrgid,khcycle,SessionUtil.getCurrentPerson());
        this.setAttr("kpiList",kpiList);
        this.setAttr("isBack",isBack);
        this.setAttr("khOrgid",khOrgid);
        this.setAttr("khcycle",khcycle);
        this.render("gradingInBathList.html");
    }

    /**
     * 打分人保存打分结果
     */
    @ActionKey("/jxp/saveGradingResults")
    public void saveGradingResults() throws Exception {
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        performanceService.saveGradingResults(jsonObj,SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }

    /**
     * 打分人提交打分结果
     */
    @ActionKey("/jxp/submitGradingResults")
    public void submitGradingResults() throws Exception {
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        performanceService.submitGradingResults(jsonObj,SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }
    /**
     * 打分人提交打分结果  快速评分
     */
    @ActionKey("/jxp/submitQuickGrading")
    public void submitQuickGrading() throws Exception {
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        performanceService.submitQuickGrading(jsonObj,SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }
    /**
     * 打分人保存打分结果  快速评分
     */
    @ActionKey("/jxp/saveQuickGrading")
    public void saveQuickGrading() throws Exception {
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        performanceService.saveQuickGrading(jsonObj,SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }


    /**
     * 打分人退回考核人自评
     */
    @ActionKey("/jxp/backSelfEvaluation")
    public void  backSelfEvaluation() throws Exception {
        String entryId = this.getPara("entryId");
        String backReason = this.getPara("reason");
        performanceService.backSelfEvaluation(entryId,backReason, SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }

    /**
     * 跳转到考核查询页面
     */
    @ActionKey("/jxp/toJxEntryQueryPage")
    public void toJxEntryQueryPage(){
        //获取所有状态为生效的考核周期
        List<Cycle> jxCycleList = jxCycleService.getJxCycleList();
        this.setAttr("jxCycleList", jxCycleList);
        List<Org> enableOrgList = orgService.getOrgList();
        this.setAttr("enableOrgList",enableOrgList);
        this.getSession().removeAttribute("jxManager_query");
        this.getSession().removeAttribute("jxAllEntry_query");
        this.render("jxEntryQueryList.html");
    }

    /**
     * 获取考核查询页面数据
     */
    @ActionKey("/jxp/getJxEntryQueryList")
    public void getJxEntryQueryList(){
        String cycleId = this.getPara("cycle");
        String khOrg = this.getPara("khOrg");
        String personName = this.getPara("personName");

        int pageNumber = this.getParaToInt("page", 1);
        int pageSize = this.getParaToInt("limit", ConstUtil.PAGE_SIZE);
        PageBean pageBean = performanceService.getJxEntryQueryList(SessionUtil.getCurrentPerson(), pageNumber, pageSize,cycleId,khOrg,personName);
        this.renderJson(pageBean);
    }

    /**
     * 在查询页面中  查看kpi详情
     */
    @ActionKey("/jxp/toJxKpiQueryPage")
    public void toJxKpiQueryPage(){
        Person person = SessionUtil.getCurrentPerson();
        String type = this.getPara("type");
        String entryid = this.getPara("entryid");
        List<Kpi> kpiList = null;
        Entry entry = jxCommonService.getJxentryByIdAndAttachment(entryid);
        if("1".equals(type)){ //查询自己评过分的人员kpi  只能查看自评评分部分的kpi
            kpiList = performanceService.getKpiQueryList(entryid,person);
        }else if("2".equals(type)){//查看下级kpi  可以查看考核人所有kpi
           //是否为直属上下级关系
            String pid = person.getId();
            boolean isSuper = entry.getEntrySuperId().equals(pid);
            if(!isSuper){
                PersonManager personManager = PersonManager.dao.findById(entry.getEntryKhpersonid());
                boolean flag = true;
                if(personManager==null ||(!pid.equals(personManager.getPersonManagerPid1()) && !pid.equals(personManager.getPersonManagerPid2()) && !pid.equals(personManager.getPersonManagerPid3()))){
                    flag =false;
                }
                if(!flag && !hrService.isDfSuper(entry.getEntryKhpersonid(),pid)){
                    throw new RuntimeException("链接错误！请退出重试！");
                }

            }
            kpiList = performanceService.getKpiQueryList(entryid,null);
            if("1".equals(entry.getEntryMbstatus())){
                this.setAttr("makeBetter",true);
            }
            if(!"9".equals(entry.getEntryApptype())){//上级查看 隐藏上级数据
                for (Kpi kpi : kpiList) {
                    jxCommonService.hideManagerData(kpi);
                }
                jxCommonService.hideManagerData(entry);
            }
        }else if("3".equals(type)){//查询自己的kpi
            if(!entry.getEntryKhpersonid().equals(person.getId())){
                throw new RuntimeException("链接错误！请退出重试！");
            }
            kpiList = performanceService.getKpiQueryList(entryid,null);
            if(!"9".equals(entry.getEntryApptype())){//个人查看 隐藏上级数据
                for (Kpi kpi : kpiList) {
                    jxCommonService.hideManagerData(kpi);
                }
                jxCommonService.hideManagerData(entry);
            }
        }else{
            throw new RuntimeException("链接错误！请退出重试！");
        }
        this.setAttr("wfLog",wflogService.getWfLog(entryid));
        this.setAttr("type",type);
        this.setAttr("kpiList",kpiList);
        this.setAttr("entry",entry);
        this.render("jxKpiQueryList.html");
    }
    /**
     * 导出指标明细信息
     */
    @ActionKey("/jxp/exportKpiInfo")
    public void exportKpiInfo(){
        String entryId = this.getPara("entryid");
        List<Kpi> kpiList = jxCommonService.getKpiList(entryId);
        List<Object[]> list = new ArrayList<Object[]>();
        Person person = SessionUtil.getCurrentPerson();
        Entry entry = jxCommonService.getJxentryById(entryId);
        boolean finish = "9".equals(entry.getEntryApptype());
        boolean isJxjl = (boolean) this.getSession().getAttribute("jxjl");
        boolean isSuper = entry.getEntrySuperId().equals(person.getId());
        boolean firstSuper = hrService.isSuper(entry.getEntryKhpersonid(), person.getId());
        boolean isSelf = entry.getEntryKhpersonid().equals(person.getId());
        if(!isJxjl && !isSuper &&!isSelf &&!firstSuper){
            throw new RuntimeException("链接错误！请退出重试！");
        }
        boolean hideManager = isSelf&&!finish;// 隐藏上级数据
        if(hideManager){
            jxCommonService.hideManagerData(entry);
        }
        for(int i=0;i<kpiList.size();i++){
            Kpi kpi = kpiList.get(i);
            if(hideManager){
                jxCommonService.hideManagerData(kpi);
            }
            String khPersonId = kpi.get("entry_khpersonid");
            list.add(new Object[]{
                    i+1,
                    kpi.get("entry_khpersonname"),
                    kpi.getKpiName(),
                    kpi.getKpiDefine(),
                    kpi.getKpiCaption(),
                    kpi.getKpiTarget(),
                    kpi.getKpiWeight()!=null?Tool.getStripZeros(kpi.getKpiWeight()).toString()+"%":null,
                    kpi.getKpiZpscore(),
                    kpi.getKpiZp(),
                    kpi.getKpiInfo(),
                    kpi.getKpiScore(),
                    i==0?entry.getEntryZpscore():null,
                    i==0 ?entry.getEntryScore():null,
                    i==0 ?entry.getEntryRating():null
            });
            if(khPersonId == null){
                throw new RuntimeException("khPersonId为空？？");
            }

        }

        String[] columns = {"序号","被考核人","指标类别","目标/举措/定义","评分标准","目标值","权重%",
                "自评分","自评说明","评分说明","上级评分","自评总分","上级评分总分","评级"};//,
        int[] columnType = {2,1,1,1,1,1,1,2,1,1,2,2,2,1};
        ExcelRender render = ExcelRender.me(list,columns,columnType);
        render.fileName( kpiList.get(0).get("entry_khpersonname") +"-" +kpiList.get(0).get("entry_cyclename")+"指标明细");
        render.setContext(this.getRequest(),this.getResponse());
        this.render(render);
    }

    /**
     * 打印指标明细信息
     */
    @ActionKey("/jxp/printKpiInfo")
    public void printKpiInfo(){
        String entryId = this.getPara("entryId");
        Entry entry = jxCommonService.getJxentryById(entryId);
        List<Kpi> kpiList = jxCommonService.getKpiList(entryId);
        jxCommonService.setEntryDisplayName(entry);

        this.setAttr("kpi0List",kpiList.stream().filter(kpi -> "0".equals(kpi.getKpiType())).collect(Collectors.toList()));
        this.setAttr("kpi1List",kpiList.stream().filter(kpi -> "1".equals(kpi.getKpiType())).collect(Collectors.toList()));
        this.setAttr("kpi2List",kpiList.stream().filter(kpi -> "2".equals(kpi.getKpiType())).collect(Collectors.toList()));
        this.setAttr("entry",entry);
        this.render("jxEntryPrint.html");
    }
    private String getKpiTypeStr(String KpiType){
        if("1".equals(KpiType)){
            return  "主观分";
        }else if("3".equals(KpiType)){
            return "附加项";
        }else{
            return  "客观分";
        }
    }
    /**
     * 跳转到我的考核页面
     */
    @ActionKey("/jxp/toMyJxEntryPage")
    public void toMyJxEntryPage(){

        this.getSession().removeAttribute("jxManager_query");
        this.getSession().removeAttribute("jxAllEntry_query");
        this.render("myJxEntryList.html");
    }
    /**
     * 获取我的考核页面数据
     */
    @ActionKey("/jxp/getMyJxEntryList")
    public void getMyJxEntryList(){
        int pageNumber = this.getParaToInt("page", 1);
        int pageSize = this.getParaToInt("limit", ConstUtil.PAGE_SIZE);
        PageBean pageBean = performanceService.getMyJxEntryList(SessionUtil.getCurrentPerson().getId(),pageNumber,pageSize);
        this.renderJson(pageBean);
    }

    /**
     * 我的考核页面查看我的kpi明细
     */
    @ActionKey("/jxp/toMyJxKpiPage")
    public void toMyJxKpiPage(){
        String entryid = this.getPara("entryid");
        Entry entry = jxCommonService.getJxentryById(entryid);
        if(!SessionUtil.getCurrentPerson().getId().equals(entry.getEntryKhpersonid())){
            throw new RuntimeException("链接错误！请退出重试！");
        }
        List<Kpi> kpiList = performanceService.getMyJxKpiList(entryid,SessionUtil.getCurrentPerson());
        this.setAttr("kpiList",kpiList);
        this.setAttr("entry",entry);
        this.setAttr("wfLog",wflogService.getWfLog(entryid));
        this.render("myJxKpiList.html");
    }

    /**
     * 绩效面谈新增界面
     */
    @ActionKey("/jxp/toJxInterviewAddPage")
    public void toJxInterviewAddPage(){
        String entryId = this.getPara("entryId");
        Entry entry = jxCommonService.getJxentryById(entryId);

        if(!SessionUtil.getCurrentPerson().getId().equals(entry.getEntrySuperId())){
            throw new RuntimeException("链接错误！请退出重试！");
        }

        this.setAttr("entry",entry);
        this.render("jxInterview.html");
    }
    /**
     * 绩效面谈查看界面
     */
    @ActionKey("/jxp/toJxInterviewPage")
    public void toJxInterviewPage(){
        String entryId = this.getPara("entryId");
        Entry entry = jxCommonService.getJxentryById(entryId);


        this.setAttr("entry",entry);
        this.setAttr("view",true);
        this.render("jxInterview.html");
    }
    @ActionKey("/jxp/saveInterview")
    public void saveInterview()throws Exception {
        Person person = (Person) this.getSession().getAttribute("person");
        JSONObject jsonObj = Tool.getRequestObject(this.getRequest());
        Entry entry = JSON.toJavaObject(jsonObj, Entry.class);

        JSONObject result = new JSONObject();
        result.put("code",0);
        try{
            performanceService.saveInterview(entry,person);
        }catch (Exception e){
            result.put("code",-1);
            result.put("msg",e.getMessage());
        }

        this.renderJson(result);
    }

    /**
     * 个人确定绩效结果
     */
    @ActionKey("/jxp/confirmEntry")
    public void confirmEntry() {
        String entryId = this.getPara("entryId");
        JSONObject result = new JSONObject();
        result.put("code",0);
        try{
            performanceService.confirmEntry(entryId, SessionUtil.getCurrentPerson());
        }catch (Exception e){
            result.put("code",-1);
            result.put("msg",e.getMessage());
        }
        this.renderJson(result);
    }
    /**
     * 个人申诉绩效结果
     */
    @ActionKey("/jxp/appealEntry")
    public void appealEntry() {
        String entryId = this.getPara("entryId");
        String reason = this.getPara("reason");
        JSONObject result = new JSONObject();
        result.put("code",0);
        try{
            performanceService.appealEntry(entryId, reason,SessionUtil.getCurrentPerson());
        }catch (Exception e){
            result.put("code",-1);
            result.put("msg",e.getMessage());
        }
        this.renderJson(result);
    }

    /**
     * 保存/提交考核人员自填kpi
     */
    @ActionKey("/jxp/saveOrSubmitSelfKpi")
    public void saveOrSubmitSelfKpi() throws Exception {
        JSONObject  jsonObj = Tool.getRequestObject(this.getRequest());
        String entryid = performanceService.saveOrSubmitSelfKpi(jsonObj);
        this.renderJson(R.isOK("entryid",entryid));
    }

    /**
     * 上级领导审批通过自填kpi
     */
    @ActionKey("/jxp/approvedSelfKpi")
    public void approvedSelfKpi(){
        String entryId = this.getPara("entryId");
        performanceService.approvedSelfKpi(entryId,SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }

    /**
     * 上级领导/绩效经理 打回自填kpi
     * type = 1 表示上级领导打回
     * type = 2 表示绩效经理打回
     */
    @ActionKey("/jxp/backselfKpi")
    public void backselfKpi(){
        String entryId = this.getPara("entryId");
        String backReason = this.getPara("reason");
        String type = this.getPara("type");
        performanceService.backselfKpi(entryId,backReason,type,SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }


//    /**
//     * 申请修改指标
//     */
//    @ActionKey("/jxp/applyModifyKpi")
//    public void applyModifyKpi(){
//        String entryId = this.getPara("entryId");
//        String reason = this.getPara("reason");
//        //String type = this.getPara("type");
//        performanceService.applyModifyKpi(entryId,reason,SessionUtil.getCurrentPerson());
//        this.renderJson(R.isOK());
//    }

    /**
     * 上级领导同意  修改指标
     */
    @ActionKey("/jxp/approvedModifyKpi")
    public void approvedModifyKpi(){
        String entryId = this.getPara("entryId");
        performanceService.approvedModifyKpi(entryId,SessionUtil.getCurrentPerson());
        this.renderJson(R.isOK());
    }
    /**
     * 上级领导不同意  修改指标
     */
    @ActionKey("/jxp/backModifyKpi")
    public void backModifyKpi(){
        String entryId = this.getPara("entryId");
        String backReason = this.getPara("reason");
        performanceService.backModifyKpi(entryId,backReason,SessionUtil.getCurrentPerson(),true);
        this.renderJson(R.isOK());
    }
    /**
     * 专员打回  修改指标
     */
    @ActionKey("/jxp/backModifyKpiJx")
    public void backModifyKpiJx(){
        String entryId = this.getPara("entryId");
        String backReason = this.getPara("reason");
        performanceService.backModifyKpi(entryId,backReason,SessionUtil.getCurrentPerson(),false);
        this.renderJson(R.isOK());
    }

    /**
     * 跳转到自填kpi页面
     */
    @ActionKey("/jxp/toSelfKpiPage")
    public void toSelfKpiPage(){
        String entryid = this.getPara("entryid");
        String type = this.getPara("type");
        Entry jxEntry = jxCommonService.getJxentryById(entryid);
        List<Kpi> kpiList = performanceService.getSelfkpiList(entryid,type,SessionUtil.getCurrentPerson());
        List<Kpi> noSelfKpiList = performanceService.getNoselfkpiList(entryid);
        if(kpiList.size() == 0 && "6".equals(jxEntry.getEntryApptype())){
            throw new RuntimeException("链接错误！请退出重试！");
        }
        if("61".equals(type)){// 申请人去申请指标修改
            for(Kpi kpi: kpiList){
                kpi.setKpiId(null);//清空id
            }
        }

        List<Kpi> kpiList0 = kpiList.stream().filter((Kpi kpi) -> "0".equals(kpi.getKpiType())).collect(Collectors.toList());
        List<Kpi> kpiList1 = kpiList.stream().filter((Kpi kpi) -> "1".equals(kpi.getKpiType())).collect(Collectors.toList());
        List<Kpi> kpiList2 = kpiList.stream().filter((Kpi kpi) -> "2".equals(kpi.getKpiType())).collect(Collectors.toList());
        if("6".equals(type))wflogService.setBackWfLog(jxEntry);
        if("41".equals(type))wflogService.setApplyWfLog(jxEntry);
        this.setAttr("histFlag",performanceService.existHistoryKpi(entryid));
        this.setAttr("wfLog",wflogService.getWfLog(entryid));
        this.setAttr("type",type);
        this.setAttr("entryid",entryid);
        this.setAttr("kpiList0",kpiList0);
        this.setAttr("kpiList1",kpiList1);
        this.setAttr("kpiList2",kpiList2);
        this.setAttr("noSelfKpiList",noSelfKpiList);
        this.setAttr("jxEntry",jxEntry);
        this.render("selfKpiList.html");
    }

    /**
     * 获取上一次考核 个人指标
     */
    @ActionKey("/jxp/getBeforeKpiList")
    public void getBeforeKpiList(){
        String khpersonid = this.getPara("khpersonid");
        String cycleId = this.getPara("cycleId");
        List<Kpi> beforeKpiList = performanceService.getBeforeKpiList(khpersonid,cycleId);
        this.renderJson(R.isOK("data",beforeKpiList));
    }

    @ActionKey("/jxp/task")
    public void task(){
        boolean have = performanceService.isHaveToDoList(SessionUtil.getCurrentPerson().getId());
        JSONObject result = new JSONObject();
        result.put("have", have);
        this.renderJson(result);
    }

    /**
     * 跳转到历史版本页面
     */
    @ActionKey("/jxp/toKpiHistPage")
    public void toKpiHistPage(){
        String entryId = this.getPara("entryId");
        String histNoIndex = this.getPara("histNoIndex");
        Entry entry = jxCommonService.getJxentryById(entryId);
        if(!SessionUtil.getCurrentPerson().getId().equals(entry.getEntryKhpersonid()) && !SessionUtil.isAdmin() && !SessionUtil.isJxjl() && !personManagerService.isManager(entry.getEntryKhpersonid(),SessionUtil.getCurrentPerson().getId())){
            throw new RuntimeException("链接错误！请退出重试！");
        }
        int histIndex =0;
        int maxIndex = performanceService.getMaxHistNo(entryId);
        if(StringUtils.isNotBlank(histNoIndex)){
            histIndex = Integer.parseInt(histNoIndex);
        }else{
            histIndex = maxIndex;
        }
        List<KpiHist> histKpiList = performanceService.getHistoryKpi(entryId, histIndex);
        this.setAttr("histIndex",histIndex);
        this.setAttr("maxIndex",maxIndex);
        this.setAttr("entryId",entryId);
        this.setAttr("histKpiList",histKpiList);

        this.render("jxKpiHist.html");
    }

    /**
     * 跳转到部门评级分布页面
     */
    @ActionKey("/jxp/toDeptRatingPercentPage")
    public void toDeptRatingPercentPage(){
        String entryId = this.getPara("entryId");
        Entry entry = jxCommonService.getJxentryById(entryId);
        if(!SessionUtil.getCurrentPerson().getId().equals(entry.getEntryKhpersonid()) && !SessionUtil.isAdmin() && !SessionUtil.isJxjl() && !personManagerService.isManager(entry.getEntryKhpersonid(),SessionUtil.getCurrentPerson().getId())){
            throw new RuntimeException("链接错误！请退出重试！");
        }
        DtBean ratingPercent = performanceService.getRatingPercent(entryId);
        this.setAttr("ratingPercent",ratingPercent);

        this.render("jxDeptRatingPercent.html");
    }

}
