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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
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.sql.ParserException;
import com.kingdee.eas.custom.performancenew.*;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.batchAffair.web.handler.EmpBatchEnrollListHandler;
import com.kingdee.shr.compensation.intelligent.dto.json.AdminOrg;
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.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author qingwu
 * @date 2024/4/10
 * @apiNote 绩效管理  组织绩效得分
 * EmpBatchEnrollListHandler
 */
public class PerformEditHandler extends EditHandler {
    private Context ctx = SHRContext.getInstance().getContext();

    /**
     * 获取值  表单 编辑表格 过滤显示当前登录人数据
     *
     * @param request
     * @param modelID 单据id
     * @return
     * @throws SHRWebException
     */
    @Override
    protected CoreBaseInfo getValue(HttpServletRequest request, String modelID) throws SHRWebException {
        ExamineGradePersonInfo examineGradePersonInfo = (ExamineGradePersonInfo) super.getValue(request, modelID);
        ExamineGradePersonEntryCollection entrys = examineGradePersonInfo.getEntrys();
        Set set = new HashSet<>();
        //去重操作
        for (int i = entrys.size() - 1; i >= 0; i--) {
            ExamineGradePersonEntryInfo examineGradePersonEntryInfo = entrys.get(i);
            String adminId = examineGradePersonEntryInfo.getAdminOrg().getId().toString();
            if (set.contains(adminId)) {
                entrys.remove(examineGradePersonEntryInfo);
            } else {
                set.add(adminId);
            }
        }
        return examineGradePersonInfo;
    }


    @Override
    public String saveAction(HttpServletRequest request,
                             HttpServletResponse response,
                             ModelMap modelMap) throws SHRWebException {
        ExamineGradePersonInfo model = (ExamineGradePersonInfo) request.getAttribute("dynamic_model");
        try {
            ExamineGradePersonEntryCollection entrys = model.getEntrys();
            IExamineGradePersonEntry iExamineGradePersonEntry = ExamineGradePersonEntryFactory.getLocalInstance(ctx);
            SelectorItemCollection sic = new SelectorItemCollection();
            sic.add("performanceScoreOrg");
            for (int i = 0; i < entrys.size(); i++) {
                ExamineGradePersonEntryInfo entryInfo = entrys.get(i);
                if (entryInfo.getPerformanceScoreOrg() != null) {
                    //修改分录该年度所有该组织的组织绩效得分
                    ExamineGradePersonEntryCollection examineGradePersonEntryCollection =
                            iExamineGradePersonEntry.getExamineGradePersonEntryCollection("where parent = '" + model.getId() + "' and adminOrg = '" + entryInfo.getAdminOrg().getId() + "'");
                    for (int j = 0; j < examineGradePersonEntryCollection.size(); j++) {
                        ExamineGradePersonEntryInfo entryInfo2 = examineGradePersonEntryCollection.get(j);
                        entryInfo2.setPerformanceScoreOrg(entryInfo.getPerformanceScoreOrg());
                        iExamineGradePersonEntry.updatePartial(entryInfo2, sic);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }
}
