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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
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.bos.sql.ParserException;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.performancenew.*;
import com.kingdee.eas.custom.performancenew.app.periodStatusEnum;
import com.kingdee.eas.hr.ats.AtsUtil;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.DynamicListHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.batchAffair.web.handler.EmpBatchEnrollListHandler;
import com.kingdee.util.StringUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * @author qingwu
 * @date 2024/4/10
 * @apiNote 绩效管理  专员维护考核人和评分人页面
 * EmpBatchEnrollListHandler
 */
public class ExamineDynamicListHandler extends EmpBatchEnrollListHandler {

    private Context ctx = SHRContext.getInstance().getContext();

    @Override
    protected FilterInfo assembleFilterInfo(HttpServletRequest request, Map<String, String> params, Map filterCondition, Date effectDate) throws SHRWebException {
        FilterInfo quickFilter = null;
        FilterInfo settingFilter = null;
        FilterInfo customFilter = null;
        if (filterCondition != null) {
            quickFilter = this.convertQuickFilterInfo((String) filterCondition.get("quickFilter"));
            settingFilter = this.convertFilterInfo((String) filterCondition.get("settingFilter"));
            customFilter = this.convertCustomFilterInfo((String) filterCondition.get("customFilter"));
        }
        String custom_params = request.getParameter("custom_params");
        if (!StringUtils.isEmpty(custom_params)) {
            customFilter = new FilterInfo();
            Map customParams = JSONUtils.convertJsonToObject(custom_params);
            String examineGradePersonId = (String) customParams.get("examineGradePersonId");
            customFilter.getFilterItems().add(new FilterItemInfo("periodicQuarter.id", examineGradePersonId));
        }
        FilterInfo advanceFilter = this.getAdvanceFilter(request);
        FilterInfo filterInfo = new FilterInfo();

        try {
            //FilterInfo defaultFilterInfo = this.getDefaultFilter(request, (HttpServletResponse) null);
            FilterInfo domainFilterInfo = this.getDomainFilter(request);
            FilterInfo treeDomainFilter = this.getTreeDomainFilter(request, (Map) filterCondition.get("tree_params"));
            //FilterInfo effectDateFilter = this.getEffectDateFilter(effectDate);
            //FilterInfo fastFilter = this.getFastFilter(request);
            String filter = (String) params.get("filter");
            if (!StringUtils.isEmpty(filter)) {
                filterInfo.mergeFilter(new FilterInfo(filter), "AND");
            }

            //filterInfo.mergeFilter(defaultFilterInfo, "AND");
            filterInfo.mergeFilter(domainFilterInfo, "AND");
            filterInfo.mergeFilter(treeDomainFilter, "AND");
            filterInfo.mergeFilter(quickFilter, "AND");
            filterInfo.mergeFilter(settingFilter, "AND");
            //filterInfo.mergeFilter(effectDateFilter, "AND");
            filterInfo.mergeFilter(customFilter, "AND");
            //filterInfo.mergeFilter(fastFilter, "AND");
            filterInfo.mergeFilter(advanceFilter, "AND");
            return filterInfo;
        } catch (BOSException var16) {
            throw new SHRWebException(var16);
        } catch (ParserException var17) {
            throw new SHRWebException(var17.getMessage(), var17);
        }
    }

    /**
     * 锁定周期
     *
     * @param request
     * @param response
     * @param modelMap
     */
    public void periodLockAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws BOSException, EASBizException {
        String periodicQuarterId = request.getParameter("periodicQuarterId");
        IExamineGradePerson iExamineGradePerson = ExamineGradePersonFactory.getLocalInstance(ctx);
        ExamineGradePersonCollection examineGradePersonCollection = iExamineGradePerson.getExamineGradePersonCollection("where id = '" + periodicQuarterId + "'");
        if (examineGradePersonCollection.size() > 0) {
            SelectorItemCollection sic = new SelectorItemCollection();
            ExamineGradePersonInfo examineGradePersonInfo = examineGradePersonCollection.get(0);
            examineGradePersonInfo.setPeriodStatus(periodStatusEnum.periodLock);
            sic.add("periodStatus");
            iExamineGradePerson.updatePartial(examineGradePersonInfo, sic);
        }
    }

    /**
     * 解除周期
     *
     * @param request
     * @param response
     * @param modelMap
     */
    public void periodUnlockAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws BOSException, EASBizException {
        String periodicQuarterId = request.getParameter("periodicQuarterId");
        IExamineGradePerson iExamineGradePerson = ExamineGradePersonFactory.getLocalInstance(ctx);
        ExamineGradePersonCollection examineGradePersonCollection = iExamineGradePerson.getExamineGradePersonCollection("where id = '" + periodicQuarterId + "'");
        if (examineGradePersonCollection.size() > 0) {
            SelectorItemCollection sic = new SelectorItemCollection();
            ExamineGradePersonInfo examineGradePersonInfo = examineGradePersonCollection.get(0);
            examineGradePersonInfo.setPeriodStatus(periodStatusEnum.periodRelieve);
            sic.add("periodStatus");
            iExamineGradePerson.updatePartial(examineGradePersonInfo, sic);
        }
    }

    /**
     * 启动排序
     */
    public void startOrderAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {

        try {
            //组织绩效与个人绩效分布对应关系
            IPersonRelationOrg iPersonRelationOrg = PersonRelationOrgFactory.getLocalInstance(ctx);
            PersonRelationOrgCollection personRelationOrgCollection = iPersonRelationOrg.getPersonRelationOrgCollection();
            if (personRelationOrgCollection.size() > 0) {
                for (int i = 0; i < personRelationOrgCollection.size(); i++) {
                    PersonRelationOrgInfo personRelationOrgInfo = personRelationOrgCollection.get(i);
                    //判断数据是否完整性，累积比例是否等于100
                    int sum = getCumulativeSum(personRelationOrgInfo);
                    if(sum>100){

                    }else{

                    }

                }
            }
        } catch (BOSException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 累加比例累积和
     */
    public int getCumulativeSum(PersonRelationOrgInfo personRelationOrgInfo) {
        int sum = 0;
        sum += personRelationOrgInfo.getMeritA();
        sum += personRelationOrgInfo.getMeritBplus();
        sum += personRelationOrgInfo.getMeritB();
        sum += personRelationOrgInfo.getMeritBminus();
        sum += personRelationOrgInfo.getMeritC();
        sum += personRelationOrgInfo.getMeritD();
        return sum;
    }


}
