<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="java.sql.Array" %>
<%@ page import="com.wbi.util.Util" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="tebie.applib.api.O" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<%
    Map<String, Object> param = ParamUtil.request2Map(request);
    String[] yffieldArr = {"y4","y5","y6","y7","y8","y9","y10","y11","y12","y1","y2","y3"};

    List<String> yffieldList = new ArrayList<>(Arrays.asList(yffieldArr));

    // out.print(JSONObject.toJSONString(param));
    String sql = "select zx. *,kyb.ky ,jz.syry from  " +
            "uf_axyssjzx  zx  " +
            "left join (select a.budget,ISNULL(nky,0)-ISNULL(am,0) as ky from  " +
            "(select sum(budgetAmount) nky, depart,subject,budget,year ,departMemberId,subjectMemberId,budgetMemberId,yearMemberId  " +
            "from FnaMultiBudgetAX_3  " +
            "group by depart,subject,budget,year ,departMemberId,subjectMemberId,budgetMemberId,yearMemberId) a  " +
            "left join (select sum(amount) am,budgetMember_1, budgetMember_2  , budgetMember_3   ,budgetMember_4  from FnaExpenseInfo_3_2  " +
            "where expenseStaus= 1 or expenseStaus= 0  group by budgetMember_1, budgetMember_2  , budgetMember_3   ,budgetMember_4 ) b  " +
            "on a.budget = b.budgetMember_3 where a.budget is not null and a.budget != 'null') kyb  " +
            "on zx.id = kyb.budget  " +
            "left join matrixtable_33 jz  " +
            "on zx.YSBMYSBS = jz.ysbm  " +
            "WHERE zx.zt = 0";
    String data = (String) param.get("data");
    String sxyf = "y"+(String) param.get("sxyf");

    int index = yffieldList.indexOf(sxyf);
    // out.print("sxyf=====>"+sxyf);
    // out.print("<br/>");
    // out.print("index=====>"+index);
    // out.print("<br/>");
    List<String> subList = new ArrayList<>();
    if (index != -1) {
        subList = yffieldList.subList(index , yffieldList.size());
        System.out.println(subList); // 打印剩余元素
    }
    // out.print("subList=====>"+subList);
    // out.print("<br/>");
    RecordSet recordSet = new RecordSet();
    JSONArray objects = JSONObject.parseArray(data);
    ArrayList<String> ids = new ArrayList<>();
    for (int i = 0; i <objects.size(); i++) {
        // out.print((Map)objects.get(i));
        Map map = (Map)objects.get(i);
        String bh = (String)map.get("bh");
        ids.add(bh);
    }
    sql = sql + " AND zx.id in ("+String.join(",",ids)+" )";
    // out.print(sql);
    recordSet.executeQuery(sql);
    HashMap<String, String> ysMap = new HashMap<>();
    while (recordSet.next()){
        // out.print(recordSet.getString("YSBHYSFH"));
        // out.print("----------");
        // out.print(recordSet.getString("YSKMBH"));
        // out.print("<br/>");
        ysMap.put(Util.null2String(recordSet.getString("id")),Util.null2String(recordSet.getString("ky")));
    }
    ArrayList<String> errorList = new ArrayList<>();
    String errorTxt = "";
    for (int i = 0; i <objects.size(); i++) {
        // out.print((Map)objects.get(i));
        Map map = (Map)objects.get(i);
        String bh = (String)map.get("bh");
        BigDecimal sum = new BigDecimal("0");
        for (String yf : subList) {
            // out.print(yf+"------"+Util.null2String(map.get(yf)));
            BigDecimal yfy = new BigDecimal(Util.null2String(map.get(yf)));
            sum = sum.add(yfy);
        }
        // out.print(bh+">>>>>>"+sum.toString());

        BigDecimal kyys = new BigDecimal(ysMap.get(bh));
        // out.print(">>>>>>"+kyys.toString());
        // out.print("<br/>");
        if (sum.compareTo(kyys)>0){
            errorList.add(bh);
            String bhName = (String)map.get("bhName");
            errorTxt = errorTxt + " " + bhName;
        }
    }
    HashMap<String, Object> result = new HashMap<>();
    result.put("errList",errorList);
    result.put("errTxt",errorTxt);
    out.print(JSONObject.toJSONString(result));

%>
<%!

%>