package cn.com.countrygarden.bi.main.util;

import cn.com.countrygarden.bi.main.dao.financial.CommIdMatchCodoMapper;
import cn.com.countrygarden.bi.main.model.financial.CommIdMatchCodo;
import cn.com.countrygarden.bi.main.model.financial.ProjectProfit;
import cn.com.countrygarden.bi.main.model.financial.Subbalance;
import cn.com.countrygarden.bi.main.model.financial.dto.OutCome;
import cn.com.countrygarden.bi.main.model.financial.dto.ProjectProfitDetail;
import cn.com.countrygarden.bi.main.model.twerp.ErpNcData;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.*;

/**
 * 1
 *
 * @program: DPshow_depart
 * @description: NC财务工具类
 * @author: SignV
 * @create: 2018-08-02 11:01
 **/
public class FinancialUtil {
    /**
     * @Description: P&LByMonth 成本数据处理方法（主营业务，销售费用，管理费用，财务费用）
     * @Param: [subbalanceList, year, month, idOrCode, judgeIdOrCode]
     * @return: cn.com.countrygarden.bi.main.model.financial.dto.OutCome
     * @Author: SignV
     * @Date: 2018/8/16
     */
    public static OutCome dataHandleForPLByMonth(List<Subbalance> subbalanceList, String year, String month, String idOrCode, int judgeIdOrCode) {
        String commId = null;
        String organCode = null;
        //判断返回idOrCode
        if (judgeIdOrCode == 2) commId = idOrCode;               //返回项目commId
        else if (judgeIdOrCode == 1) organCode = idOrCode;      //返回区域organCode
        //初始化数据
        Map<String, BigDecimal> saleMap = new HashMap<>();
        Map<String, BigDecimal> managementMap = new HashMap<>();
        Map<String, BigDecimal> mainCostMap = new HashMap<>();

        saleMap.put("人工成本(含社保、公积金）", new BigDecimal(0));
        saleMap.put("其中：年终奖", new BigDecimal(0));
        saleMap.put("福利费", new BigDecimal(0));
        saleMap.put("职工教育经费", new BigDecimal(0));
        saleMap.put("办公费", new BigDecimal(0));
        saleMap.put("折旧摊销费", new BigDecimal(0));
        saleMap.put("上市费用", new BigDecimal(0));
        saleMap.put("研发费用", new BigDecimal(0));
        saleMap.put("水电气暖费", new BigDecimal(0));
        saleMap.put("食堂收支", new BigDecimal(0));
        saleMap.put("宿舍收支", new BigDecimal(0));
        saleMap.put("诉讼费", new BigDecimal(0));
        saleMap.put("服务费", new BigDecimal(0));
        saleMap.put("车辆费", new BigDecimal(0));
        saleMap.put("差旅费", new BigDecimal(0));
        saleMap.put("员工股权支付费", new BigDecimal(0));
        saleMap.put("其他费用", new BigDecimal(0));
        saleMap.put("销售费用小计", new BigDecimal(0));

        managementMap.put("人工成本(含社保、公积金）", new BigDecimal(0));
        managementMap.put("其中：年终奖", new BigDecimal(0));
        managementMap.put("福利费", new BigDecimal(0));
        managementMap.put("职工教育经费", new BigDecimal(0));
        managementMap.put("办公费", new BigDecimal(0));
        managementMap.put("折旧摊销费", new BigDecimal(0));
        managementMap.put("上市费用", new BigDecimal(0));
        managementMap.put("研发费用", new BigDecimal(0));
        managementMap.put("水电气暖费", new BigDecimal(0));
        managementMap.put("食堂收支", new BigDecimal(0));
        managementMap.put("宿舍收支", new BigDecimal(0));
        managementMap.put("诉讼费", new BigDecimal(0));
        managementMap.put("服务费", new BigDecimal(0));
        managementMap.put("车辆费", new BigDecimal(0));
        managementMap.put("差旅费", new BigDecimal(0));
        managementMap.put("员工股权支付费", new BigDecimal(0));
        managementMap.put("其他费用", new BigDecimal(0));
        managementMap.put("管理费用小计", new BigDecimal(0));

        mainCostMap.put("人工成本(含社保、公积金）", new BigDecimal(0));
        mainCostMap.put("其中：年终奖", new BigDecimal(0));
        mainCostMap.put("福利费", new BigDecimal(0));
        mainCostMap.put("职工教育经费", new BigDecimal(0));
        mainCostMap.put("设施维护费", new BigDecimal(0));
        mainCostMap.put("设施维护费（委外）", new BigDecimal(0));
        mainCostMap.put("清洁费", new BigDecimal(0));
        mainCostMap.put("清洁费（委外）", new BigDecimal(0));
        mainCostMap.put("绿化费", new BigDecimal(0));
        mainCostMap.put("绿化费（委外）", new BigDecimal(0));
        mainCostMap.put("办公费", new BigDecimal(0));
        mainCostMap.put("水费", new BigDecimal(0));
        mainCostMap.put("电费", new BigDecimal(0));
        mainCostMap.put("服装费", new BigDecimal(0));
        mainCostMap.put("责任险", new BigDecimal(0));
        mainCostMap.put("工本费", new BigDecimal(0));
        mainCostMap.put("文化活动费用", new BigDecimal(0));
        mainCostMap.put("网络通信费", new BigDecimal(0));
        mainCostMap.put("汽车费用", new BigDecimal(0));
        mainCostMap.put("差旅费", new BigDecimal(0));
        mainCostMap.put("保安费", new BigDecimal(0));
        mainCostMap.put("折旧摊销费", new BigDecimal(0));
        mainCostMap.put("区内车队成本", new BigDecimal(0));
        mainCostMap.put("食堂收支", new BigDecimal(0));
        mainCostMap.put("宿舍收支", new BigDecimal(0));
        mainCostMap.put("有偿维修物料", new BigDecimal(0));
        mainCostMap.put("有偿清洁物料", new BigDecimal(0));
        mainCostMap.put("有偿绿化物料", new BigDecimal(0));
        mainCostMap.put("节日装饰", new BigDecimal(0));
        mainCostMap.put("维修费", new BigDecimal(0));
        mainCostMap.put("宣传推广费", new BigDecimal(0));
        mainCostMap.put("办公室租赁费", new BigDecimal(0));
        mainCostMap.put("办公室装修费", new BigDecimal(0));
        mainCostMap.put("外包成本", new BigDecimal(0));
        mainCostMap.put("业务费", new BigDecimal(0));
        mainCostMap.put("维修物料费", new BigDecimal(0));
        mainCostMap.put("清洁物料费", new BigDecimal(0));
        mainCostMap.put("绿化物料费", new BigDecimal(0));
        mainCostMap.put("水果食品费", new BigDecimal(0));
        mainCostMap.put("办公室水电费", new BigDecimal(0));
        mainCostMap.put("其他", new BigDecimal(0));
        mainCostMap.put("成本明细合计", new BigDecimal(0));
        //判断数据是否为空
        if (subbalanceList == null) {
            Map<String, Object> saleResultMap = new HashMap<>();
            saleResultMap.put("saleCostDetail", saleMap);

            Map<String, Object> managementResultMap = new HashMap<>();
            managementMap.put("管理费用小计", new BigDecimal(0));
            managementResultMap.put("managementCostDetail", managementMap);

            Map<String, Object> financeResultMap = new HashMap<>();
            financeResultMap.put("financialCostTotal", 0);

            Map<String, Object> mainCostResultMap = new HashMap<>();
            mainCostResultMap.put("estateManagement", 0);
            mainCostResultMap.put("marketingService", 0);
            mainCostResultMap.put("communityService&siteService", 0);
            mainCostResultMap.put("mainCostTotal", 0);
            mainCostResultMap.put("mainCostDetail", mainCostMap);

            Map<String, Object> indirectCostMap = new HashMap<>();
            indirectCostMap.put("indirectCostTotal", 0);
            return new OutCome(mainCostResultMap, managementResultMap, saleResultMap, financeResultMap,
                    indirectCostMap, year, month, commId, organCode);
        }
        String accasoaNum;
        //将成本进行分类
        List<Subbalance> mainCostList = new ArrayList<>();
        List<Subbalance> saleList = new ArrayList<>();
        List<Subbalance> managementList = new ArrayList<>();
        List<Subbalance> financeList = new ArrayList<>();
        for (Subbalance subbalance : subbalanceList) {
            String typeCode = subbalance.getAccasoa_nam().substring(0, 4);
            switch (typeCode) {
                case "6401":
                    mainCostList.add(subbalance);
                    break;   //主营业务
                case "6601":
                    saleList.add(subbalance);
                    break;       //销售费用
                case "6602":
                    managementList.add(subbalance);
                    break; //管理费用
                case "6603":
                    financeList.add(subbalance);
                    break;    //财务费用
                default:
                    break;
            }
        }

        int index;
        String content;
        BigDecimal cost;
        BigDecimal total;
        BigDecimal indirectCost = new BigDecimal(0);    //间接费用
        //销售费用成本    6601
        total = new BigDecimal(0);
        for (Subbalance subbalance : saleList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            total = total.add(cost);
            if (content.equals("工资")) {
                if (subbalance.getAssValue().contains("工资")) {
                    saleMap.put("人工成本(含社保、公积金）", cost.add(saleMap.get("人工成本(含社保、公积金）")));
                } else {
                    saleMap.put("其中：年终奖", cost.add(saleMap.get("其中：年终奖")));
                }
            } else if (content.equals("社会保险费") || content.equals("住房公积金")) {
                saleMap.put("人工成本(含社保、公积金）", cost.add(saleMap.get("人工成本(含社保、公积金）")));
            } else if (!saleMap.containsKey(content)) {
                saleMap.put(content, cost);
            } else {
                cost = cost.add(saleMap.get(content));
                saleMap.put(content, cost);
            }
        }
        Map<String, Object> saleResultMap = new HashMap<>();
        total = total.subtract(saleMap.get("其中：年终奖"));
        saleMap.put("销售费用小计", total);
        saleResultMap.put("saleCostDetail", saleMap);
        indirectCost = indirectCost.add(total);

        //管理费用成本    6602
        total = new BigDecimal(0);
        for (Subbalance subbalance : managementList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            total = total.add(cost);
            if (content.equals("工资")) {
                if (subbalance.getAssValue().contains("工资")) {
                    managementMap.put("人工成本(含社保、公积金）", cost.add(managementMap.get("人工成本(含社保、公积金）")));
                } else {
                    managementMap.put("其中：年终奖", cost.add(managementMap.get("其中：年终奖")));
                }
            } else if (content.equals("社会保险费") || content.equals("住房公积金")) {
                managementMap.put("人工成本(含社保、公积金）", cost.add(managementMap.get("人工成本(含社保、公积金）")));
            } else if (!managementMap.containsKey(content)) {
                managementMap.put(content, cost);
            } else {
                cost = cost.add(managementMap.get(content));
                managementMap.put(content, cost);
            }
        }
        Map<String, Object> managementResultMap = new HashMap<>();
        total = total.subtract(managementMap.get("其中：年终奖"));
        managementMap.put("管理费用小计", total);
        managementResultMap.put("managementCostDetail", managementMap);
        indirectCost = indirectCost.add(total);

        //财务费用成本    6603
        total = new BigDecimal(0);
        for (Subbalance subbalance : financeList) {
            cost = new BigDecimal(subbalance.getCreditamount());
            total = total.add(cost);
        }
        Map<String, Object> financeResultMap = new HashMap<>();
        financeResultMap.put("financialCostTotal", total);
        indirectCost = indirectCost.add(total);

        //主营业务成本    6401
        total = new BigDecimal(0);
        mainCostMap.put("物业管理", new BigDecimal(0));
        mainCostMap.put("协销服务", new BigDecimal(0));
        mainCostMap.put("社区增值服务&场地管理服务", new BigDecimal(0));
        for (Subbalance subbalance : mainCostList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            if (accasoaNum.contains("基础物业服务成本")) {
                mainCostMap.put("物业管理", cost.add(mainCostMap.get("物业管理")));
            } else if (accasoaNum.contains("协销服务成本")) {
                mainCostMap.put("协销服务", cost.add(mainCostMap.get("协销服务")));
            } else {
                mainCostMap.put("社区增值服务&场地管理服务", cost.add(mainCostMap.get("社区增值服务&场地管理服务")));
            }
            total = total.add(cost);
            if (content.equals("工资")) {
                if (subbalance.getAssValue().contains("工资")) {
                    mainCostMap.put("人工成本(含社保、公积金）", cost.add(mainCostMap.get("人工成本(含社保、公积金）")));
                } else {
                    mainCostMap.put("其中：年终奖", cost.add(mainCostMap.get("其中：年终奖")));
                }
            } else if (content.equals("社会保险费") || content.equals("住房公积金")) {
                mainCostMap.put("人工成本(含社保、公积金）", cost.add(mainCostMap.get("人工成本(含社保、公积金）")));
            } else if (content.equals("设施维护费") || content.equals("清洁费") || content.equals("绿化费")) {
                if (subbalance.getAssValue().contains("委外")) {
                    content = content + "（委外）";
                    mainCostMap.put(content, cost.add(mainCostMap.get(content)));
                } else {
                    mainCostMap.put(content, cost.add(mainCostMap.get(content)));
                }
            } else if (!mainCostMap.containsKey(content)) {
                mainCostMap.put(content, cost);
            } else {
                cost = cost.add(mainCostMap.get(content));
                mainCostMap.put(content, cost);
            }
        }
        Map<String, Object> mainCostResultMap = new HashMap<>();
        mainCostResultMap.put("estateManagement", mainCostMap.get("物业管理"));
        mainCostResultMap.put("marketingService", mainCostMap.get("协销服务"));
        mainCostResultMap.put("communityService&siteService", mainCostMap.get("社区增值服务&场地管理服务"));
        mainCostResultMap.put("mainCostTotal", total);
        total = total.subtract(mainCostMap.get("其中：年终奖"));
        mainCostMap.put("成本明细合计", total);
        mainCostResultMap.put("mainCostDetail", mainCostMap);
        mainCostMap.remove("物业管理");
        mainCostMap.remove("协销服务");
        mainCostMap.remove("社区增值服务&场地管理服务");

        Map<String, Object> indirectCostMap = new HashMap<>();
        indirectCostMap.put("indirectCostTotal", indirectCost);

        return new OutCome(mainCostResultMap, managementResultMap, saleResultMap, financeResultMap,
                indirectCostMap, year, month, commId, organCode);
    }

    /**
     * @Description: 账簿一对多项目时，进行项目分类
     * @Param: [subbalanceList, oneToManyCodos, assValueList, year, month, codo, erpNcData, returnList, idOrCode, judgeIdOrCode]
     * @return: void
     * @Author: SignV
     * @Date: 2018/8/16
     */
    public static void financialDataHandle(
            List<Subbalance> subbalanceList, String year, String month,
            String codo, List<OutCome> returnList, int judgeIdOrCode, List<CommIdMatchCodo> commIdMatchCodoList, String organCode) {
        String assValue;
        //System.out.println(year + "年  账簿一对多项目：" + codo + "  数据长度：" + subbalanceList.size());
        //对同项目的数据进行处理
        //获取对应commid 模型
        Map<String, ArrayList<Subbalance>> tempMap = new HashMap<>();    //键 commid
        for (Subbalance subbalance : subbalanceList) {
            assValue = subbalance.getAssValue();
            for (CommIdMatchCodo commIdMatchCodo : commIdMatchCodoList) {
                String commId = commIdMatchCodo.getCommId();
                if (assValue.contains(commIdMatchCodo.getAssvalueCutOut())) {
                    if (!tempMap.containsKey(commId)) {
                        ArrayList<Subbalance> tempList = new ArrayList<>();
                        tempList.add(subbalance);
                        tempMap.put(commId, tempList);
                    } else
                        tempMap.get(commId).add(subbalance);
                }
            }
        }
        for (CommIdMatchCodo commIdMatchCodo : commIdMatchCodoList) {
            if (organCode == null) {   //若为true则使用PLByMonth的数据处理方法
                returnList.add(dataHandleForPLByMonth(tempMap.get(commIdMatchCodo.getCommId()), year, month, commIdMatchCodo.getCommId(), judgeIdOrCode));
            } else       //若为false则使用PLByProjectRow的数据处理方法
                returnList.add(projectDataForProjectRow(tempMap.get(commIdMatchCodo.getCommId()), year, month, commIdMatchCodo.getCommId(), organCode));
        }
    }

    /**
     * @Description: 账簿一对多项目时，进行项目分类（税金及附加，营业外收入，营业外支出，主营业务成本）
     * @Param: [subbalanceList, oneToManyCodos, assValueList, year, month, codo, erpNcData, returnList, idOrCode, judgeIdOrCode]
     * @return: void
     * @Author: SignV
     * @Date: 2018/8/16
     */
    public static List<Map<String, ArrayList<Subbalance>>> financialDataHandleForTaxOutInCome(
            List<Subbalance> subbalanceList, List<Subbalance> subbalanceLastYearList, List<CommIdMatchCodo> commIdMatchCodoList) {
        String assValue;
        String commId;
        Map<String, ArrayList<Subbalance>> tempMap = new HashMap<>();    //键 commid
        Map<String, ArrayList<Subbalance>> tempLastYearMap = new HashMap<>();    //键 commid
        if (subbalanceList != null) {
            for (Subbalance subbalance : subbalanceList) {
                assValue = subbalance.getAssValue();
                for (CommIdMatchCodo commIdMatchCodo : commIdMatchCodoList) {
                    commId = commIdMatchCodo.getCommId();
                    if (assValue.contains(commIdMatchCodo.getAssvalueCutOut())) {
                        if (!tempMap.containsKey(commId)) {
                            ArrayList<Subbalance> tempList = new ArrayList<>();
                            tempList.add(subbalance);
                            tempMap.put(commId, tempList);
                        } else
                            tempMap.get(commId).add(subbalance);
                    }
                }
            }
        }
        if (subbalanceLastYearList != null) {
            for (Subbalance subbalance : subbalanceLastYearList) {
                assValue = subbalance.getAssValue();
                for (CommIdMatchCodo commIdMatchCodo : commIdMatchCodoList) {
                    commId = commIdMatchCodo.getCommId();
                    if (assValue.contains(commIdMatchCodo.getAssvalueCutOut())) {
                        if (!tempLastYearMap.containsKey(commId)) {
                            ArrayList<Subbalance> tempList = new ArrayList<>();
                            tempList.add(subbalance);
                            tempLastYearMap.put(commId, tempList);
                        } else
                            tempLastYearMap.get(commId).add(subbalance);
                    }
                }
            }
        }
        List<Map<String, ArrayList<Subbalance>>> returnList = new ArrayList<>();
        returnList.add(tempMap);
        returnList.add(tempLastYearMap);
        return returnList;
    }

    /**
     * @Description: 通过organCode或commId处理数据（税金及附加，营业外收入，营业外支出）
     * @Param: [subbalanceList, organCode, month]
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: SignV
     * @Date: 2018/8/16
     */
    public static JSONObject financialDataHandleByOrganCode(List<Subbalance> subbalanceList, List<Subbalance> subbalanceLastYearList, String year, int startMonth, int monthSize, String idOrCode, int judgeIdOrCode) {
        String commId = null;
        String organCode = null;
        String lastYear = String.valueOf(Integer.valueOf(year) - 1);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //判断返回idOrCode
        if (judgeIdOrCode == 2) commId = idOrCode;               //返回项目commId
        else if (judgeIdOrCode == 1) organCode = idOrCode;      //返回区域organCode
        int monthIndex;
        //将成本进行分类
        List<Subbalance> taxList = new ArrayList<>();
        List<Subbalance> businessIncomeList = new ArrayList<>();
        List<Subbalance> businessOutcomeList = new ArrayList<>();
        //去年数据分类
        List<Subbalance> taxLastYearList = new ArrayList<>();
        List<Subbalance> businessLastYearIncomeList = new ArrayList<>();
        List<Subbalance> businessLastYearOutcomeList = new ArrayList<>();
        if (subbalanceList == null) {
            //放入年度数据
            JSONObject yearJson = new JSONObject();
            yearJson.put("year", year);
            yearJson.put("month", "13");
            yearJson.put("tax", new BigDecimal(0));
            yearJson.put("outIncome", new BigDecimal(0));
            jsonArray.add(yearJson);
            //放入各个月份数据
            for (int i = 0; i <= monthSize; i++) {
                JSONObject monthJson = new JSONObject();
                monthJson.put("year", year);
                monthJson.put("month", startMonth + i);
                monthJson.put("tax", new BigDecimal(0));
                monthJson.put("outIncome", new BigDecimal(0));
                jsonArray.add(monthJson);
            }
        } else {
            for (Subbalance subbalance : subbalanceList) {
                String typeCode = subbalance.getAccasoa_nam().substring(0, 4);
                switch (typeCode) {
                    case "6403":
                        taxList.add(subbalance);
                        break;   //税金及附加
                    case "6301":
                        businessIncomeList.add(subbalance);
                        break;       //营业外收入
                    case "6711":
                        businessOutcomeList.add(subbalance);
                        break; //营业外支出
                    default:
                        break;
                }
            }
            BigDecimal taxYear = new BigDecimal(0);         //年度税金及附加
            BigDecimal outIncomeYear = new BigDecimal(0);   //年度营业外收入
            //区间月份税金及附加 营业外收入数据列表保存
            List<BigDecimal> taxMonthList = new ArrayList<>();
            List<BigDecimal> outIncomeMonthList = new ArrayList<>();
            for (int i = 0; i <= monthSize; i++) {
                taxMonthList.add(new BigDecimal(0));         //月份税金及附加
                outIncomeMonthList.add(new BigDecimal(0));   //月份营业外收入
            }
            //计算年度税金及附加数据
            for (Subbalance subbalance : taxList) {
                taxYear = taxYear.add(new BigDecimal(subbalance.getCreditamount()));
                monthIndex = Integer.valueOf(subbalance.getPeriod()) - startMonth;
                if (monthIndex >= 0 && monthIndex <= monthSize) {
                    taxMonthList.set(monthIndex, taxMonthList.get(monthIndex).add(new BigDecimal(subbalance.getCreditamount())));
                }
            }
            //计算年度营业外收入（营业外收入6301 - 营业外支出6711）
            for (Subbalance subbalance : businessIncomeList) {
                outIncomeYear = outIncomeYear.add(new BigDecimal(subbalance.getCreditamount()));
                monthIndex = Integer.valueOf(subbalance.getPeriod()) - startMonth;
                if (monthIndex >= 0 && monthIndex <= monthSize) {
                    outIncomeMonthList.set(monthIndex, outIncomeMonthList.get(monthIndex).add(new BigDecimal(subbalance.getCreditamount())));
                }
            }
            for (Subbalance subbalance : businessOutcomeList) {
                outIncomeYear = outIncomeYear.subtract(new BigDecimal(subbalance.getCreditamount()));
                monthIndex = Integer.valueOf(subbalance.getPeriod()) - startMonth;
                if (monthIndex >= 0 && monthIndex <= monthSize) {
                    outIncomeMonthList.set(monthIndex, outIncomeMonthList.get(monthIndex).subtract(new BigDecimal(subbalance.getCreditamount())));
                }
            }
            //放入年度数据
            JSONObject yearJson = new JSONObject();
            yearJson.put("year", year);
            yearJson.put("month", "13");
            yearJson.put("tax", taxYear);
            yearJson.put("outIncome", outIncomeYear);
            jsonArray.add(yearJson);
            //放入各个月份数据
            for (int i = 0; i <= monthSize; i++) {
                JSONObject monthJson = new JSONObject();
                monthJson.put("year", year);
                monthJson.put("month", startMonth + i);
                monthJson.put("tax", taxMonthList.get(i));
                monthJson.put("outIncome", outIncomeMonthList.get(i));
                jsonArray.add(monthJson);
            }
        }
        if (subbalanceLastYearList == null) {
            //放入去年年度数据
            JSONObject lastYearJson = new JSONObject();
            lastYearJson.put("year", lastYear);
            lastYearJson.put("month", "0");
            lastYearJson.put("tax", new BigDecimal(0));
            lastYearJson.put("outIncome", new BigDecimal(0));
            jsonArray.add(lastYearJson);
        } else {
            for (Subbalance subbalance : subbalanceLastYearList) {
                String typeCode = subbalance.getAccasoa_nam().substring(0, 4);
                switch (typeCode) {
                    case "6403":
                        taxLastYearList.add(subbalance);
                        break;   //税金及附加
                    case "6301":
                        businessLastYearIncomeList.add(subbalance);
                        break;       //营业外收入
                    case "6711":
                        businessLastYearOutcomeList.add(subbalance);
                        break; //营业外支出
                    default:
                        break;
                }
            }
            BigDecimal taxLastYear = new BigDecimal(0);         //年度税金及附加
            BigDecimal outIncomeLastYear = new BigDecimal(0);   //年度营业外收入
            //汇总去年数据 不需要月份数据
            //计算年度税金及附加数据
            for (Subbalance subbalance : taxLastYearList) {
                taxLastYear = taxLastYear.add(new BigDecimal(subbalance.getCreditamount()));
            }
            //计算年度营业外收入（营业外收入6301 - 营业外支出6711）
            for (Subbalance subbalance : businessLastYearIncomeList) {
                outIncomeLastYear = outIncomeLastYear.add(new BigDecimal(subbalance.getCreditamount()));
            }
            for (Subbalance subbalance : businessLastYearOutcomeList) {
                outIncomeLastYear = outIncomeLastYear.subtract(new BigDecimal(subbalance.getCreditamount()));
            }
            //放入去年年度数据
            JSONObject lastYearJson = new JSONObject();
            lastYearJson.put("year", lastYear);
            lastYearJson.put("month", "0");
            lastYearJson.put("tax", taxLastYear);
            lastYearJson.put("outIncome", outIncomeLastYear);
            jsonArray.add(lastYearJson);
        }
        //数据封装
        jsonObject.put("commId", commId);
        jsonObject.put("organCode", organCode);
        jsonObject.put("data", jsonArray);
        return jsonObject;
    }

    /**
     * @Description: 按月份分类
     * @Param: [subbalanceList]
     * @return: java.util.List<java.util.List   <   cn.com.countrygarden.bi.main.model.financial.Subbalance>>
     * @Author: SignV
     * @Date: 2018/8/16
     */
    public static List<List<Subbalance>> monthClassify(List<Subbalance> subbalanceList) {
        //12个月月份成本数据
        List<Subbalance> oneMonthList = new ArrayList<>();
        List<Subbalance> twoMonthList = new ArrayList<>();
        List<Subbalance> threeMonthList = new ArrayList<>();
        List<Subbalance> fourMonthList = new ArrayList<>();
        List<Subbalance> fiveMonthList = new ArrayList<>();
        List<Subbalance> sixMonthList = new ArrayList<>();
        List<Subbalance> sevenMonthList = new ArrayList<>();
        List<Subbalance> eightMonthList = new ArrayList<>();
        List<Subbalance> nineMonthList = new ArrayList<>();
        List<Subbalance> tenMonthList = new ArrayList<>();
        List<Subbalance> elevenMonthList = new ArrayList<>();
        List<Subbalance> twelveMonthList = new ArrayList<>();
        //进行月份分类List
        for (Subbalance subbalance : subbalanceList) {
            switch (subbalance.getPeriod()) {
                case "01":
                    oneMonthList.add(subbalance);
                    break;
                case "02":
                    twoMonthList.add(subbalance);
                    break;
                case "03":
                    threeMonthList.add(subbalance);
                    break;
                case "04":
                    fourMonthList.add(subbalance);
                    break;
                case "05":
                    fiveMonthList.add(subbalance);
                    break;
                case "06":
                    sixMonthList.add(subbalance);
                    break;
                case "07":
                    sevenMonthList.add(subbalance);
                    break;
                case "08":
                    eightMonthList.add(subbalance);
                    break;
                case "09":
                    nineMonthList.add(subbalance);
                    break;
                case "10":
                    tenMonthList.add(subbalance);
                    break;
                case "11":
                    elevenMonthList.add(subbalance);
                    break;
                case "12":
                    twelveMonthList.add(subbalance);
                    break;
                default:
                    break;
            }
        }
        List<List<Subbalance>> monthClassifyList = new ArrayList<>();
        monthClassifyList.add(oneMonthList);
        monthClassifyList.add(twoMonthList);
        monthClassifyList.add(threeMonthList);
        monthClassifyList.add(fourMonthList);
        monthClassifyList.add(fiveMonthList);
        monthClassifyList.add(sixMonthList);
        monthClassifyList.add(sevenMonthList);
        monthClassifyList.add(eightMonthList);
        monthClassifyList.add(nineMonthList);
        monthClassifyList.add(tenMonthList);
        monthClassifyList.add(elevenMonthList);
        monthClassifyList.add(twelveMonthList);
        return monthClassifyList;
    }

    /**
     * @Description: 通过月份长度进行月份分类
     * @Param: [subbalanceList, startMonth, monthSize]
     * @return: java.util.List<java.util.List   <   cn.com.countrygarden.bi.main.model.financial.Subbalance>>
     * @Author: SignV
     * @Date: 2018/8/19
     */
    public static List<List<Subbalance>> monthClassifyByMonthSize(List<Subbalance> subbalanceList, int startMonth, int monthSize) {
        List<List<Subbalance>> monthClassifyList = new ArrayList<>();
        //区间月份成本数据 例：开始为3月份 结束为6月份 则monthSize为3 从0开始，区间月份为3月 4月 5月 6月
        for (int i = 0; i <= monthSize; i++) {
            monthClassifyList.add(new ArrayList<>());
        }
        //进行月份分类List
        int monthIndex;
        for (Subbalance subbalance : subbalanceList) {
            //月份索引 = 实际月份 - 开始月份
            monthIndex = Integer.valueOf(subbalance.getPeriod()) - startMonth;
            if (monthIndex >= 0 && monthIndex <= monthSize)
                monthClassifyList.get(monthIndex).add(subbalance);
        }
        return monthClassifyList;
    }

    /**
     * @Description: excel处理方法 sheet传入模板页，dataMap数据，start开始位置，end结束行数，monthIndex月份对应位置索引数
     * @Param: [sheet, dataMap, start, end, monthIndex]
     * @return: void
     * @Author: SignV
     * @Date: 2018/8/16
     */
    public static void excelHandle(Sheet sheet, JSONObject dataMap, int start, int end, int monthIndex) {
        for (int i = start; i <= end; i++) {
            Row tempRow = sheet.getRow(i);
            if (tempRow == null) continue;     //获取空白列则略过该行
            String comm = tempRow.getCell(1).getStringCellValue();      //获取对应科目
            BigDecimal data = dataMap.getBigDecimal(comm);
            //map的key和B列科目名称对应 无数据则设置为0
            if (data != null) tempRow.getCell(monthIndex).setCellValue(data.doubleValue());
            else tempRow.getCell(monthIndex).setCellValue(0d);
        }
    }

    /**
     * @Description: Excel单行处理方法 sheet模板页，dataStr数据字符串，rowIndex单行位置，monthIndex月份对应位置索引数
     * @Param: [sheet, dataStr, rowIndex, monthIndex]
     * @return: void
     * @Author: SignV
     * @Date: 2018/8/16
     */
    public static void excelOneRawHandle(Sheet sheet, String dataStr, int rowIndex, int monthIndex) {
        Row tempRow = sheet.getRow(rowIndex);
        BigDecimal data = new BigDecimal(dataStr);
        if (data != null) tempRow.getCell(monthIndex).setCellValue(data.doubleValue());
        else tempRow.getCell(monthIndex).setCellValue(0d);
    }

    /**
     * @Description: P&L按项目横向 成本数据处理方法（主营业务，销售费用，管理费用，财务费用）
     * @Param: [subbalanceList, year, month, idOrCode, judgeIdOrCode]
     * @return: cn.com.countrygarden.bi.main.model.financial.dto.OutCome
     * @Author: SignV
     * @Date: 2018/8/21
     */
    public static OutCome projectDataForProjectRow(List<Subbalance> subbalanceList, String year, String month, String commId, String organCode) {
        //初始化数据
        Map<String, Object> saleResultMap = new HashMap<>();
        saleResultMap.put("saleCostTotal", 0);

        Map<String, Object> managementResultMap = new HashMap<>();
        managementResultMap.put("managementCostTotal", 0);

        Map<String, Object> financeResultMap = new HashMap<>();
        financeResultMap.put("financialCostTotal", 0);

        Map<String, Object> mainCostResultMap = new HashMap<>();
        mainCostResultMap.put("estateManagement", 0);
        mainCostResultMap.put("marketingService", 0);
        mainCostResultMap.put("communityService&siteService", 0);
        mainCostResultMap.put("mainCostTotal", 0);
        Map<String, BigDecimal> mainCostMap = new HashMap<>();
        mainCostMap.put("设施维护费", new BigDecimal(0));
        mainCostMap.put("清洁费", new BigDecimal(0));
        mainCostMap.put("绿化费", new BigDecimal(0));
        mainCostMap.put("水费", new BigDecimal(0));
        mainCostMap.put("电费", new BigDecimal(0));
        mainCostMap.put("食堂收支", new BigDecimal(0));
        mainCostMap.put("宿舍收支", new BigDecimal(0));
        mainCostMap.put("人工成本(含社保、公积金）", new BigDecimal(0));
        mainCostResultMap.put("mainCostDetail", mainCostMap);
        //判断数据是否为空
        if (subbalanceList == null)
            return new OutCome(mainCostResultMap, managementResultMap, saleResultMap, financeResultMap,
                    null, year, month, commId, organCode);
        String accasoaNum;
        //将成本进行分类
        List<Subbalance> mainCostList = new ArrayList<>();
        List<Subbalance> saleList = new ArrayList<>();
        List<Subbalance> managementList = new ArrayList<>();
        List<Subbalance> financeList = new ArrayList<>();
        for (Subbalance subbalance : subbalanceList) {
            String typeCode = subbalance.getAccasoa_nam().substring(0, 4);
            switch (typeCode) {
                case "6401":
                    mainCostList.add(subbalance);
                    break;   //主营业务
                case "6601":
                    saleList.add(subbalance);
                    break;       //销售费用
                case "6602":
                    managementList.add(subbalance);
                    break; //管理费用
                case "6603":
                    financeList.add(subbalance);
                    break;    //财务费用
                default:
                    break;
            }
        }
        int index;
        String content;
        BigDecimal cost;
        BigDecimal total;
//        BigDecimal indirectCost = new BigDecimal(0);    //间接费用
        //销售费用成本    6601
        total = new BigDecimal(0);
        Map<String, BigDecimal> saleMap = new HashMap<>();
        saleMap.put("其中：年终奖", new BigDecimal(0));
        for (Subbalance subbalance : saleList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            total = total.add(cost);
            if (content.equals("工资")) {
                if (!subbalance.getAssValue().contains("工资")) {
                    saleMap.put("其中：年终奖", cost.add(saleMap.get("其中：年终奖")));
                }
            }
        }
        total = total.subtract(saleMap.get("其中：年终奖"));
//        saleMap.put("saleCostTotal",total);
//        saleMap.remove("其中：年终奖");
        saleResultMap.put("saleCostTotal", total);

        //管理费用成本    6602
        total = new BigDecimal(0);
        Map<String, BigDecimal> managementMap = new HashMap<>();
        managementMap.put("其中：年终奖", new BigDecimal(0));
        for (Subbalance subbalance : managementList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            total = total.add(cost);
            if (content.equals("工资")) {
                if (!subbalance.getAssValue().contains("工资")) {
                    managementMap.put("其中：年终奖", cost.add(managementMap.get("其中：年终奖")));
                }
            }
        }
        total = total.subtract(managementMap.get("其中：年终奖"));
//        managementMap.put("managementCostTotal",total);
//        managementMap.remove("其中：年终奖");
        managementResultMap.put("managementCostTotal", total);

        //财务费用成本    6603
        total = new BigDecimal(0);
        for (Subbalance subbalance : financeList) {
            cost = new BigDecimal(subbalance.getCreditamount());
            total = total.add(cost);
        }
        financeResultMap.put("financialCostTotal", total);

        //主营业务成本    6401
        total = new BigDecimal(0);
        mainCostMap.put("物业管理", new BigDecimal(0));
        mainCostMap.put("协销服务", new BigDecimal(0));
        mainCostMap.put("社区增值服务&场地管理服务", new BigDecimal(0));
        for (Subbalance subbalance : mainCostList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            if (accasoaNum.contains("基础物业服务成本")) {
                mainCostMap.put("物业管理", cost.add(mainCostMap.get("物业管理")));
            } else if (accasoaNum.contains("协销服务成本")) {
                mainCostMap.put("协销服务", cost.add(mainCostMap.get("协销服务")));
            } else {
                mainCostMap.put("社区增值服务&场地管理服务", cost.add(mainCostMap.get("社区增值服务&场地管理服务")));
            }
            total = total.add(cost);
            if (content.equals("工资")) {
                if (subbalance.getAssValue().contains("工资")) {
                    mainCostMap.put("人工成本(含社保、公积金）", cost.add(mainCostMap.get("人工成本(含社保、公积金）")));
                }
            } else if (content.equals("社会保险费") || content.equals("住房公积金")) {
                mainCostMap.put("人工成本(含社保、公积金）", cost.add(mainCostMap.get("人工成本(含社保、公积金）")));
            } else if (content.equals("设施维护费") || content.equals("清洁费") || content.equals("绿化费") || content.contains("水费")
                    || content.contains("电费") || content.equals("宿舍收支") || content.equals("食堂收支")) {
                cost = cost.add(Optional.ofNullable(mainCostMap.get(content)).orElse(BigDecimal.valueOf(0)));
                mainCostMap.put(content, cost);
            }
        }
        mainCostResultMap.put("estateManagement", mainCostMap.get("物业管理"));
        mainCostResultMap.put("marketingService", mainCostMap.get("协销服务"));
        mainCostResultMap.put("communityService&siteService", mainCostMap.get("社区增值服务&场地管理服务"));
        mainCostResultMap.put("mainCostTotal", total);
//        total = total.subtract(mainCostMap.get("其中：年终奖"));
//        mainCostMap.put("成本明细合计",total);
        mainCostMap.remove("物业管理");
        mainCostMap.remove("协销服务");
        mainCostMap.remove("社区增值服务&场地管理服务");
        mainCostResultMap.put("mainCostDetail", mainCostMap);

        return new OutCome(mainCostResultMap, managementResultMap, saleResultMap, financeResultMap,
                null, year, month, commId, organCode);
    }

    /**
     * @Description: P&L按项目横向 区域成本数据（assValue包含 区域办公室）处理方法（主营业务，销售费用，管理费用，财务费用）
     * @Param: [subbalanceList, year, month, idOrCode, judgeIdOrCode]
     * @return: cn.com.countrygarden.bi.main.model.financial.dto.OutCome
     * @Author: SignV
     * @Date: 2018/8/22
     */
    public static OutCome RegionDataForPLByProjectRow(List<Subbalance> subbalanceList, String year, String month, String idOrCode, int judgeIdOrCode) {
        String commId = null;
        String organCode = null;
        //判断返回idOrCode
        if (judgeIdOrCode == 2) commId = idOrCode;               //返回项目commId
        else if (judgeIdOrCode == 1) organCode = idOrCode;      //返回区域organCode
        //判断数据是否为空
        if (subbalanceList == null) {
            Map<String, Object> saleResultMap = new HashMap<>();
            saleResultMap.put("saleCostTotal", 0);

            Map<String, Object> managementResultMap = new HashMap<>();
            managementResultMap.put("managementCostTotal", 0);

            Map<String, Object> mainCostResultMap = new HashMap<>();
            mainCostResultMap.put("estateManagement", 0);
            mainCostResultMap.put("marketingService", 0);
            mainCostResultMap.put("communityService&siteService", 0);
            mainCostResultMap.put("mainCostTotal", 0);

            return new OutCome(mainCostResultMap, managementResultMap, saleResultMap, null,
                    null, year, month, commId, organCode);
        }
        String accasoaNum;
        //将成本进行分类
        List<Subbalance> mainCostList = new ArrayList<>();
        List<Subbalance> saleList = new ArrayList<>();
        List<Subbalance> managementList = new ArrayList<>();
        for (Subbalance subbalance : subbalanceList) {
            String typeCode = subbalance.getAccasoa_nam().substring(0, 4);
            switch (typeCode) {
                case "6401":
                    mainCostList.add(subbalance);
                    break;   //主营业务
                case "6601":
                    saleList.add(subbalance);
                    break;       //销售费用
                case "6602":
                    managementList.add(subbalance);
                    break; //管理费用
                default:
                    break;
            }
        }
        int index;
        String content;
        BigDecimal cost;
        BigDecimal total;

        //销售费用成本    6601
        total = new BigDecimal(0);
        Map<String, BigDecimal> saleMap = new HashMap<>();
        saleMap.put("其中：年终奖", new BigDecimal(0));
        for (Subbalance subbalance : saleList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            total = total.add(cost);
            if (content.equals("工资")) {
                if (!subbalance.getAssValue().contains("工资")) {
                    saleMap.put("其中：年终奖", cost.add(saleMap.get("其中：年终奖")));
                }
            }
        }
        Map<String, Object> saleResultMap = new HashMap<>();
        total = total.subtract(saleMap.get("其中：年终奖"));
//        saleMap.put("saleCostTotal",total);
//        saleMap.remove("其中：年终奖");
        saleResultMap.put("saleCostTotal", total);

        //管理费用成本    6602
        total = new BigDecimal(0);
        Map<String, BigDecimal> managementMap = new HashMap<>();
        managementMap.put("其中：年终奖", new BigDecimal(0));
        for (Subbalance subbalance : managementList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            total = total.add(cost);
            if (content.equals("工资")) {
                if (!subbalance.getAssValue().contains("工资")) {
                    managementMap.put("其中：年终奖", cost.add(managementMap.get("其中：年终奖")));
                }
            }
        }
        Map<String, Object> managementResultMap = new HashMap<>();
        total = total.subtract(managementMap.get("其中：年终奖"));
//        managementMap.put("managementCostTotal",total);
//        managementMap.remove("其中：年终奖");
        managementResultMap.put("managementCostTotal", total);

        //主营业务成本    6401
        total = new BigDecimal(0);
        Map<String, BigDecimal> mainCostMap = new HashMap<>();
        mainCostMap.put("其中：年终奖", new BigDecimal(0));
        mainCostMap.put("物业管理", new BigDecimal(0));
        mainCostMap.put("协销服务", new BigDecimal(0));
        mainCostMap.put("社区增值服务&场地管理服务", new BigDecimal(0));
        for (Subbalance subbalance : mainCostList) {
            accasoaNum = subbalance.getAccasoa_nam();
            index = accasoaNum.lastIndexOf("\\");
            content = accasoaNum.substring(index + 1);
            cost = new BigDecimal(subbalance.getCreditamount());
            if (accasoaNum.contains("基础物业服务成本")) {
                mainCostMap.put("物业管理", cost.add(mainCostMap.get("物业管理")));
            } else if (accasoaNum.contains("协销服务成本")) {
                mainCostMap.put("协销服务", cost.add(mainCostMap.get("协销服务")));
            } else {
                mainCostMap.put("社区增值服务&场地管理服务", cost.add(mainCostMap.get("社区增值服务&场地管理服务")));
            }
            total = total.add(cost);
            if (content.equals("工资")) {
                if (!subbalance.getAssValue().contains("工资")) {
                    mainCostMap.put("其中：年终奖", cost.add(mainCostMap.get("其中：年终奖")));
                }
            }
        }
        Map<String, Object> mainCostResultMap = new HashMap<>();
        mainCostResultMap.put("estateManagement", mainCostMap.get("物业管理"));
        mainCostResultMap.put("marketingService", mainCostMap.get("协销服务"));
        mainCostResultMap.put("communityService&siteService", mainCostMap.get("社区增值服务&场地管理服务"));
        mainCostResultMap.put("mainCostTotal", total);
//        total = total.subtract(mainCostMap.get("其中：年终奖"));
//        mainCostMap.put("成本明细合计",total);


        return new OutCome(mainCostResultMap, managementResultMap, saleResultMap, null,
                null, year, month, commId, organCode);
    }

    //一个账套编号对应多项目时 主营业务收入数据处理方法
    public static void mainInComeHandle(JSONObject returnJsonObject, List<Subbalance> subbalanceList, List<Subbalance> subbalanceLastYearList, int startMonth, int monthSize, String idOrCode) {
        int monthIndex;
        int index;
        //将主营业务收入进行分类
        List<Subbalance> managementService = new ArrayList<>();     //物业费
        List<Subbalance> marketingService = new ArrayList<>();      //协销服务费
        List<Subbalance> siteService = new ArrayList<>();           //场地管理服务费
        List<Subbalance> communityService = new ArrayList<>();      //社区增值服务费

        //去年主营业务收入数据分类
        List<Subbalance> managementLastYear = new ArrayList<>();
        List<Subbalance> siteLastYear = new ArrayList<>();
        List<Subbalance> marketingLastYear = new ArrayList<>();
        List<Subbalance> communityLastYear = new ArrayList<>();

        Map<Integer, BigDecimal> managementDataMap = new HashMap<>();
        Map<Integer, BigDecimal> siteDataMap = new HashMap<>();
        Map<Integer, BigDecimal> marketingDataMap = new HashMap<>();
        Map<Integer, BigDecimal> communitytDataMap = new HashMap<>();

        //放入12个月的数据 以及年度数据 和 去年年度数据 初始化数据Map
        for (int i = 0; i < 14; i++) {
            managementDataMap.put(i, new BigDecimal(0));
            siteDataMap.put(i, new BigDecimal(0));
            marketingDataMap.put(i, new BigDecimal(0));
            communitytDataMap.put(i, new BigDecimal(0));
        }

        if (subbalanceList == null) {
            //无操作 数据为0
        } else {
            for (Subbalance subbalance : subbalanceList) {
                String typeCode = subbalance.getAccasoa_nam().substring(0, 7);
                switch (typeCode) {
                    case "6001007":
                        managementService.add(subbalance);
                        break;    //物业费
                    case "6001008":
                        communityService.add(subbalance);
                        break;     //社区增值服务费
                    case "6001009":
                        marketingService.add(subbalance);
                        break;     //协销服务费
                    case "6001010":
                        siteService.add(subbalance);
                        break;          //场地管理服务费
                    default:
                        break;
                }
            }
            //计算物业费
            for (Subbalance subbalance : managementService) {
                managementDataMap.put(13, managementDataMap.get(13).add(new BigDecimal(subbalance.getCreditamount())));
                index = Integer.valueOf(subbalance.getPeriod());    //月份索引
                monthIndex = index - startMonth;
                if (monthIndex >= 0 && monthIndex <= monthSize) {
                    managementDataMap.put(index, managementDataMap.get(index).add(new BigDecimal(subbalance.getCreditamount())));
                }
            }
            //计算社区增值服务费
            for (Subbalance subbalance : communityService) {
                communitytDataMap.put(13, communitytDataMap.get(13).add(new BigDecimal(subbalance.getCreditamount())));
                index = Integer.valueOf(subbalance.getPeriod());    //月份索引
                monthIndex = index - startMonth;
                if (monthIndex >= 0 && monthIndex <= monthSize) {
                    communitytDataMap.put(index, communitytDataMap.get(index).add(new BigDecimal(subbalance.getCreditamount())));
                }
            }
            //计算协销服务费
            for (Subbalance subbalance : marketingService) {
                marketingDataMap.put(13, marketingDataMap.get(13).add(new BigDecimal(subbalance.getCreditamount())));
                index = Integer.valueOf(subbalance.getPeriod());    //月份索引
                monthIndex = index - startMonth;
                if (monthIndex >= 0 && monthIndex <= monthSize) {
                    marketingDataMap.put(index, marketingDataMap.get(index).add(new BigDecimal(subbalance.getCreditamount())));
                }
            }
            //计算场地管理服务费
            for (Subbalance subbalance : siteService) {
                siteDataMap.put(13, siteDataMap.get(13).add(new BigDecimal(subbalance.getCreditamount())));
                index = Integer.valueOf(subbalance.getPeriod());    //月份索引
                monthIndex = index - startMonth;
                if (monthIndex >= 0 && monthIndex <= monthSize) {
                    siteDataMap.put(index, siteDataMap.get(index).add(new BigDecimal(subbalance.getCreditamount())));
                }
            }
        }
        if (subbalanceLastYearList == null) {
            //无操作 数据为0
        } else {
            for (Subbalance subbalance : subbalanceLastYearList) {
                String typeCode = subbalance.getAccasoa_nam().substring(0, 7);
                switch (typeCode) {
                    case "6001007":
                        managementLastYear.add(subbalance);
                        break;    //物业费
                    case "6001008":
                        communityLastYear.add(subbalance);
                        break;     //社区增值服务费
                    case "6001009":
                        marketingLastYear.add(subbalance);
                        break;     //协销服务费
                    case "6001010":
                        siteLastYear.add(subbalance);
                        break;          //场地管理服务费
                    default:
                        break;
                }
            }

            //去年年度 主营业务收入 数据
            BigDecimal managemen = new BigDecimal(0);
            BigDecimal community = new BigDecimal(0);
            BigDecimal marketing = new BigDecimal(0);
            BigDecimal site = new BigDecimal(0);

            //计算物业费
            for (Subbalance subbalance : managementLastYear) {
                managemen = managemen.add(new BigDecimal(subbalance.getCreditamount()));
            }
            //计算社区增值服务费
            for (Subbalance subbalance : communityLastYear) {
                community = community.add(new BigDecimal(subbalance.getCreditamount()));
            }
            //计算协销服务费
            for (Subbalance subbalance : marketingLastYear) {
                marketing = marketing.add(new BigDecimal(subbalance.getCreditamount()));
            }
            //计算场地管理服务费
            for (Subbalance subbalance : siteLastYear) {
                site = site.add(new BigDecimal(subbalance.getCreditamount()));
            }
            //放入去年年度数据
            managementDataMap.put(0, managemen);
            siteDataMap.put(0, site);
            marketingDataMap.put(0, marketing);
            communitytDataMap.put(0, community);
        }
        //数据封装
        returnJsonObject.getJSONObject("managementService").put(idOrCode, managementDataMap);         //物业费
        returnJsonObject.getJSONObject("siteService").put(idOrCode, siteDataMap);               //场地管理服务费
        returnJsonObject.getJSONObject("marketingService").put(idOrCode, marketingDataMap);          //协销服务费
        returnJsonObject.getJSONObject("communityService").put(idOrCode, communitytDataMap);         //社区增值服务费
    }

    public static void profitDetialDataHandle(ProjectProfitDetail projectProfitDetail, ProjectProfit projectProfit, List<Integer> integerList) {
        //计数 项目统计
        projectProfitDetail.setProjectCount(projectProfitDetail.getProjectCount() + 1);
        //计算 不含税收入
        projectProfitDetail.setIncomeWithOutTax(projectProfitDetail.getIncomeWithOutTax().add(new BigDecimal(projectProfit.getIncomeWithOutTax())));
        //计算 利润
        projectProfitDetail.setProfit(projectProfitDetail.getProfit().add(new BigDecimal(projectProfit.getProfit())));
        switch (projectProfit.getProjectStage()) {
            case "纯常规": {      //计数 纯常规
                projectProfitDetail.setRoutineCount(projectProfitDetail.getRoutineCount() + 1);
                integerList.set(0, integerList.get(0) + 1);
                break;
            }
            case "纯案场": {      //计数 纯案场
                projectProfitDetail.setCaseCount(projectProfitDetail.getCaseCount() + 1);
                integerList.set(1, integerList.get(1) + 1);
                break;
            }
            case "案场常规混合": {      //计数 混合（常规与案场）
                projectProfitDetail.setRoutineCaseCount(projectProfitDetail.getRoutineCaseCount() + 1);
                integerList.set(2, integerList.get(2) + 1);
                break;
            }
        }
    }
}
