package weaver.formmode.recruit.modeexpand.internal;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;

/**
 * @author:dxfeng
 * @createTime: 2024/12/17
 * @version: 1.0
 */
public class InterviewRatingModeExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        RecordSet rs = new RecordSet();
        Map<String, String> result = new HashMap<>();
        try {
            int billId;
            int modeId;
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                User user = (User) param.get("user");
                billId = Util.getIntValue(requestInfo.getRequestid());
                modeId = Util.getIntValue(requestInfo.getWorkflowid());
                if (billId > 0 && modeId > 0) {
                    Map<String, Object> mainDataMap = new HashMap<>();
                    MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
                    Property[] properties = mainTableInfo.getProperty();
                    for (Property property : properties) {
                        mainDataMap.put(property.getName(), property.getValue());
                    }

                    String nbjp = Util.null2String(mainDataMap.get("nbjp"));

                    rs.executeQuery("select * from uf_jcl_nbjpmspf where nbjp = ?", nbjp);
                    List<Integer> scopeList = new ArrayList<>();
                    while (rs.next()) {
                        int zhpf = rs.getInt("zhpf");
                        if (zhpf > 0) {
                            scopeList.add(zhpf);
                        }
                    }
                    if (scopeList.size() <= 2) {
                        return result;
                    }
                    // scopeList去掉一个最高分、一个最低分 求总分
                    int sum = calculateSum(scopeList);
                    if (sum > 0) {
                        // 更新内部应聘者台账的分数
                        rs.executeUpdate("update uf_jcl_nbzwsq set mspfhz = ? where id = ? ", sum, nbjp);
                    }


                }
            }
        } catch (Exception e) {
            new BaseBean().writeLog(e);
            result.put("errmsg", e.getMessage());
            result.put("flag", "false");
        }
        return result;
    }

    public static int calculateSum(List<Integer> scores) {
        // 找到最高分和最低分
        int maxScore = Collections.max(scores);
        int minScore = Collections.min(scores);

        // 去掉一个最高分和一个最低分
        scores.remove(Integer.valueOf(maxScore));
        scores.remove(Integer.valueOf(minScore));

        // 计算剩余分数的总和
        int sum = 0;
        for (int score : scores) {
            sum += score;
        }
        return sum;
    }
}
