package com.jeeplus.modules.sg.overheadline.web;

import com.alibaba.fastjson.JSONArray;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.utils.sg.ExcelUtil;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sg.overheadline.entity.*;
import com.jeeplus.modules.sg.overheadline.service.FeeAdjustService;
import com.jeeplus.modules.sg.overheadline.service.OverheadLineService;
import com.jeeplus.modules.sg.overheadline.service.SettlementService;
import com.jeeplus.modules.sg.overheadline.util.BashInfo;
import com.jeeplus.modules.sg.project.entity.WbsItem;
import com.jeeplus.modules.sg.project.entity.WbsProject;
import com.jeeplus.modules.sg.project.service.ItemService;
import com.jeeplus.modules.sg.project.service.ProjectService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = "${adminPath}/jkxl")
public class OverheadLineController extends BaseController {
  @Autowired
  private OverheadLineService overheadLineService;
  @Autowired
  private FeeAdjustService feeAdjustService;
  @Autowired
  private SettlementService settlementService;
  @Autowired
  private ItemService itemService;
  @Autowired
  private ProjectService projectService;


  /**
   *数据展示
   */
  @RequestMapping("/list")
  public String list(HttpServletRequest request,Model model){
    String id = request.getParameter("id");
    String type = request.getParameter("type");
    String projectId = request.getParameter("projectId");
    WbsItem wbsItem = itemService.get(id);
    WbsProject wbsProject = projectService.get(wbsItem.getProjectId());
    if(projectId == null) {
      wbsItem.setProjectId(null);
    }
    wbsItem.setProjectName(wbsProject.getProjectName());
    wbsItem.setProjectNo(wbsProject.getProjectNo());
    List<WbsItem> list1 = new ArrayList<>();
    list1.add(wbsItem);
    //查询费用明细
    List<JkxlDetailFee> list = overheadLineService.getList(id,type);
    //求和
    list.addAll(getTotal(list,type));
    //查询未识别数据
    JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
    jkxlAdjustFee.setId(id);
    List<JkxlAdjustFee> list2 = feeAdjustService.getTzFee(jkxlAdjustFee);
    //数据校验展示
    List<Settlement> list3 = settlementService.selectSettlementService(id,type);
    //差额计算
    List<Settlement> list4 = disposeData(list,list3,list2);
    HashMap map = new HashMap();
    map.put("list1",list1);
    map.put("list",list);
    map.put("list2",list2);
    map.put("list3",list4);
    model.addAttribute("map",map);
    return "modules/sg/free/wbslist";
  }




  /**
   *金额分摊调整
   */
  @RequestMapping("/fttz")
  public String reList(FtData ftData, Model model,RedirectAttributes redirectAttributes){
      String id = ftData.getId();
      String code = ftData.getCode();   //分摊费用类型
      String state = ftData.getState();  //1：增加 2：减少
      String type = ftData.getType();   //分摊费用名
      String itemType = ftData.getItemType();   //项目类型
      double fee = ftData.getFee();
      double total = 0.00;
      double rate = 0.00;
      try {
        List<JkxlDetailFee> jkxl = overheadLineService.getList(id,itemType);
        //获取本体部分费用明细
        List<JkxlDetailFee> jkxlDetailFees = new ArrayList<>();
        for(JkxlDetailFee jkxlDetailFee : jkxl){
          if(jkxlDetailFee.getOriginalTotalCost() != null){
            jkxlDetailFees.add(jkxlDetailFee);
          }
        }
        double size = jkxlDetailFees.size();
        //直接工程费
        if(code.equals(BashInfo.TZ_ZJGC)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getZjgcFee();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getZjgcFee();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getZjgcFee();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getZjgcFee();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //人工费
        if(code.equals(BashInfo.TZ_RGF)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getOriginalRgCost();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getOriginalRgCost();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getOriginalRgCost();
            }
          }
            for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
              double rgFee = jkxlDetailFee1.getOriginalRgCost();
              double totalFee = jkxlDetailFee1.getTotalFee();
              if(total==0.00){
                rate = 1/size;
              } else {
                rate = rgFee/total;
              }
              double ftFee = rate*fee;
              jkxlDetailFee1.setRgFtFee(ftFee);
              if(state.equals("1")){
                double zFee = totalFee+ftFee;
                jkxlDetailFee1.setTotalFee(zFee);
              }else {
                double zFee = totalFee-ftFee;
                jkxlDetailFee1.setTotalFee(zFee);
              }
            }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //承包人采购
        if(code.equals(BashInfo.TZ_CBRCG)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getOriginalCbrCost();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getOriginalCbrCost();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getOriginalCbrCost();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getOriginalCbrCost();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //发包人采购
        if(code.equals(BashInfo.TZ_FBRCG)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getOriginalFbrCost();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getOriginalFbrCost();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getOriginalFbrCost();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getOriginalFbrCost();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //机械费
        if(code.equals(BashInfo.TZ_JX)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getOriginalJxCost();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getOriginalJxCost();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getOriginalJxCost();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getOriginalJxCost();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //措施费一（1）
        if(code.equals(BashInfo.TZ_CSF1)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getMeasuresFee1();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getMeasuresFee1();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getMeasuresFee1();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getMeasuresFee1();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //措施费一（2）
        if(code.equals(BashInfo.TZ_CSF2)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getMeasuresFee2();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getMeasuresFee2();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getMeasuresFee2();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getMeasuresFee2();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //措施费二
        if(code.equals(BashInfo.TZ_CSF3)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getMeasuresFee3();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getMeasuresFee3();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getMeasuresFee3();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getMeasuresFee3();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //其他费
        if(code.equals(BashInfo.TZ_QTF)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getOtherCost();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getOtherCost();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getOtherCost();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getOtherCost();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //规费
        if(code.equals(BashInfo.TZ_GF)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getFees();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getFees();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getFees();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getFees();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //税费
        if(code.equals(BashInfo.TZ_SF)){
          if(itemType.equals(BashInfo.PROJECT_BDZ)){
            double totalJz = 0.00;
            double totalAz = 0.00;
            for(JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              if(jkxlDetailFee.getWbsCode().equals("21000000")){
                totalJz = jkxlDetailFee.getTex();
              }
              if(jkxlDetailFee.getWbsCode().equals("23000000")){
                totalAz = jkxlDetailFee.getTex();
              }
              total = totalAz + totalJz;
            }
          }else {
            for (JkxlDetailFee jkxlDetailFee : jkxlDetailFees) {
              total += jkxlDetailFee.getTex();
            }
          }
          for(JkxlDetailFee jkxlDetailFee1 : jkxlDetailFees){
            double rgFee = jkxlDetailFee1.getTex();
            double totalFee = jkxlDetailFee1.getTotalFee();
            if(total==0.00){
              rate = 1/size;
            } else {
              rate = rgFee/total;
            }
            double ftFee = rate*fee;
            jkxlDetailFee1.setRgFtFee(ftFee);
            if(state.equals("1")){
              double zFee = totalFee+ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }else {
              double zFee = totalFee-ftFee;
              jkxlDetailFee1.setTotalFee(zFee);
            }
          }
          overheadLineService.updateList(jkxlDetailFees);
        }
        //更新未识别费用表
        JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
        jkxlAdjustFee.setType(type);
        jkxlAdjustFee.setId(id);
        feeAdjustService.updateFee(jkxlAdjustFee);
      } catch (Exception e){
        addMessage(redirectAttributes, "调整失败");
        return "redirect:/a/jkxl/list?id="+id+"&type="+itemType;
      }
    addMessage(redirectAttributes, "调整成功");
    return "redirect:/a/jkxl/list?id="+id+"&type="+itemType;
  }


  /**
   * 分摊表
   */
  @RequestMapping("/getft")
  public String getft(HttpServletRequest request,Model model){
    String id = request.getParameter("id");
    WbsItem wbsItem = itemService.get(id);
    String type = wbsItem.getType();
    List<JkxlDetailFee> list = overheadLineService.getAdjust(id);
    if(type.equals(BashInfo.PROJECT_BDZ)){
      JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
      jkxlDetailFee.setParentNode("-1");
      jkxlDetailFee.setDescribe("变电站工程");
      jkxlDetailFee.setWbsCode("20000000");
      list.add(jkxlDetailFee);
      model.addAttribute("list",list);
      return "modules/sg/free/individualForm";
    }
    model.addAttribute("list",list);
    return "modules/sg/free/alongst";
  }


  /**
   *金额单独调整
   */
  @RequestMapping("/ddtz")
  public String ddtz(FtData ftData, Model model, RedirectAttributes redirectAttributes){
    String id = ftData.getId();
    String itemType = itemService.get(id).getType();
    String state  = ftData.getState();  // 1:增加 2:减少
    String data1 = ftData.getTableData();   //表格数据JOSN字符串
    String row = data1.replaceAll("&quot;","\\\"");
    //将JSON字符串转为list
    List<DdtzData> ts = (List<DdtzData>) JSONArray.parseArray(row, DdtzData.class);
    try {
      //更新细节费用表
      for(DdtzData ddtzData: ts){
        double tzFee = Double.valueOf(ddtzData.getFee());
        String wbsCode = ddtzData.getWbsCode();
        double totalFee = overheadLineService.getDate(id,wbsCode).getTotalFee();
        //增加
        if(state.equals("1")){
          totalFee = totalFee+tzFee;
        }
        //减少
        if(state.equals("2")){
          totalFee = totalFee-tzFee;
        }
        JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
        jkxlDetailFee.setId(id);
        jkxlDetailFee.setWbsCode(wbsCode);
        jkxlDetailFee.setTotalFee(totalFee);
        //更新明细表费用
        overheadLineService.updataDate(jkxlDetailFee);
      }
      //更新未识别费用表
      JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
      jkxlAdjustFee.setType(ftData.getType());
      jkxlAdjustFee.setId(id);
      feeAdjustService.updateFee(jkxlAdjustFee);

    }catch (Exception e){
      addMessage(redirectAttributes, "调整失败");
    }
    addMessage(redirectAttributes, "调整成功");
    return "redirect:/a/jkxl/list?id="+id+"&type="+itemType;
  }

  /**
   * 变电站单独调整
   */
  @RequestMapping("/bddtz")
  public String bddtz(FtData ftData, Model model, RedirectAttributes redirectAttributes){
    String id = ftData.getId();
    String itemType = itemService.get(id).getType();
    String state  = ftData.getState();  // 1:增加 2:减少
    String data1 = ftData.getTableData();   //表格数据JOSN字符串
    String row = data1.replaceAll("&quot;","\\\"");
    //将JSON字符串转为list
    List<DdtzData> ts = (List<DdtzData>) JSONArray.parseArray(row, DdtzData.class);
    List<JkxlDetailFee> list = overheadLineService.getList(id,itemType);
    try {
      //更新细节费用表
      for(DdtzData ddtzData: ts){
        double tzFee = Double.valueOf(ddtzData.getFee());
        String wbsCode = ddtzData.getWbsCode();
        JkxlDetailFee jkxlDetailFee1 = overheadLineService.getDate(id,wbsCode);
        List<JkxlDetailFee> list1 = new ArrayList<>();
        //递归获取需要更新的数据存入集合
        JkxlDetailFee jkxlDetailFee2 = getParent(jkxlDetailFee1,list,list1);
        list1.add(jkxlDetailFee1);
        for(JkxlDetailFee jkxlDetailFee3 : list1) {
          if(jkxlDetailFee3.getWbsCode().equals("20000000")){
            continue;
          }
          double totalFee = jkxlDetailFee3.getTotalFee();
          if (state.equals("1")) {
            totalFee = totalFee + tzFee;
          }
          if (state.equals("2")) {
            totalFee = totalFee - tzFee;
          }
//          JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
//          jkxlDetailFee.setId(id);
//          jkxlDetailFee.setWbsCode(wbsCode);
          jkxlDetailFee3.setTotalFee(totalFee);
          //更新明细表费用
          overheadLineService.updataDate(jkxlDetailFee3);
        }

      }
      //更新未识别费用表
      JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
      jkxlAdjustFee.setType(ftData.getType());
      jkxlAdjustFee.setId(id);
      feeAdjustService.updateFee(jkxlAdjustFee);

    }catch (Exception e){
      addMessage(redirectAttributes, "调整失败");
    }
    addMessage(redirectAttributes, "调整成功");
    return "redirect:/a/jkxl/list?id="+id+"&type="+itemType;
  }

  //递归获取节点
  private JkxlDetailFee getParent(JkxlDetailFee jkxlDetailFee,List<JkxlDetailFee> list,List<JkxlDetailFee> list1){
    if(jkxlDetailFee.getParentNode().equals("-1")){
      return jkxlDetailFee;
    }
    JkxlDetailFee jkxlDetailFee1 = null;
    for(JkxlDetailFee jkxlDetailFee2:list){
      if(jkxlDetailFee2.getWbsCode().equals(jkxlDetailFee.getParentNode())){
        jkxlDetailFee1 = jkxlDetailFee2;
        list1.add(jkxlDetailFee1);
        break;
      }
    }
    return getParent(jkxlDetailFee1,list,list1);
  }





  /**
   *导出
   */
  @RequestMapping("/export")
  public String export(HttpServletRequest request, HttpServletResponse response,Model model,RedirectAttributes redirectAttributes) {
    String id = request.getParameter("id");
    String type = request.getParameter("type");
    try{
      WbsItem wbsItem = itemService.get(id);
      String file = wbsItem.getItemName();
      String fileName = file+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
      List<JkxlDetailFee> list = overheadLineService.findList(id,type);
      for (int i=0;i<list.size();i++){
        list.get(i).setIndex(i+1);
      }
      //导出
      new ExportExcel(null, JkxlDetailFee.class).setDataList(list).write(response, fileName).dispose();
      return null;
    }catch (Exception e){
      addMessage(redirectAttributes, "导出失败");
    }
     addMessage(redirectAttributes, "导出成功");
    return "redirect:/a/jkxl/list?id="+id+"&type="+type;
  }





  /**
   *求和
   */
  private List<JkxlDetailFee> getTotal(List<JkxlDetailFee> list,String type) {
    List<JkxlDetailFee> list1 = new ArrayList<>();
    List<JkxlDetailFee> list2  = new ArrayList<>();
    //获取主体部分list集合
    for(JkxlDetailFee jkxlDetailFee : list){
      if(jkxlDetailFee.getTotalFee() != null && jkxlDetailFee.getFeeType().equals("1")){
        list1.add(jkxlDetailFee);
      }
    }
    //获取其他费用list集合
    for(JkxlDetailFee jkxlDetailFee : list){
      if(jkxlDetailFee.getOriginalTotalCost() == null && jkxlDetailFee.getTotalFee() != null && jkxlDetailFee.getFeeType().equals("0")){
        list2.add(jkxlDetailFee);
      }
    }
    //变电站项目求和
    if(type.equals(BashInfo.PROJECT_BDZ)){
      double[][] zFees = new double[14][2];
       for(JkxlDetailFee jkxlDetailFee : list){
         //建筑部分求和
          if(jkxlDetailFee.getWbsCode().equals("21000000")){
            zFees[0][0] = jkxlDetailFee.getOriginalTotalCost()!=null? jkxlDetailFee.getOriginalTotalCost():0.00;
            zFees[1][0] = jkxlDetailFee.getZjgcFee()!=null? jkxlDetailFee.getZjgcFee():0.00;
            zFees[2][0] = jkxlDetailFee.getOriginalRgCost()!=null? jkxlDetailFee.getOriginalRgCost():0.00;
            zFees[3][0] = jkxlDetailFee.getOriginalCbrCost()!=null? jkxlDetailFee.getOriginalCbrCost():0.00;
            zFees[4][0] = jkxlDetailFee.getOriginalFbrCost()!=null? jkxlDetailFee.getOriginalFbrCost():0.00;
            zFees[5][0] = jkxlDetailFee.getOriginalJxCost()!=null? jkxlDetailFee.getOriginalJxCost():0.00;
//            zFees[6][0] = jkxlDetailFee.getRgCost()!=null? jkxlDetailFee.getRgCost():0.00;
            zFees[6][0] = jkxlDetailFee.getMeasuresFee1()!=null? jkxlDetailFee.getMeasuresFee1():0.00;
            zFees[7][0] = jkxlDetailFee.getMeasuresFee2()!=null? jkxlDetailFee.getMeasuresFee2():0.00;
            zFees[8][0] = jkxlDetailFee.getMeasuresFee3()!=null? jkxlDetailFee.getMeasuresFee3():0.00;
            zFees[9][0] = jkxlDetailFee.getOtherCost()!=null? jkxlDetailFee.getOtherCost():0.00;
            zFees[10][0] = jkxlDetailFee.getFees()!=null? jkxlDetailFee.getFees():0.00;
            zFees[11][0] = jkxlDetailFee.getTex()!=null? jkxlDetailFee.getTex():0.00;
            zFees[12][0] = jkxlDetailFee.getRgFtFee()!=null? jkxlDetailFee.getRgFtFee():0.00;
            zFees[13][0] = jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
          }
          //安装部分求和
         if(jkxlDetailFee.getWbsCode().equals("23000000")){
           zFees[0][1] = jkxlDetailFee.getOriginalTotalCost()!=null? jkxlDetailFee.getOriginalTotalCost():0.00;
           zFees[1][1] = jkxlDetailFee.getZjgcFee()!=null? jkxlDetailFee.getZjgcFee():0.00;
           zFees[2][1] = jkxlDetailFee.getOriginalRgCost()!=null? jkxlDetailFee.getOriginalRgCost():0.00;
           zFees[3][1] = jkxlDetailFee.getOriginalCbrCost()!=null? jkxlDetailFee.getOriginalCbrCost():0.00;
           zFees[4][1] = jkxlDetailFee.getOriginalFbrCost()!=null? jkxlDetailFee.getOriginalFbrCost():0.00;
           zFees[5][1] = jkxlDetailFee.getOriginalJxCost()!=null? jkxlDetailFee.getOriginalJxCost():0.00;
//           zFees[6][1] = jkxlDetailFee.getRgCost()!=null? jkxlDetailFee.getRgCost():0.00;
           zFees[6][1] = jkxlDetailFee.getMeasuresFee1()!=null? jkxlDetailFee.getMeasuresFee1():0.00;
           zFees[7][1] = jkxlDetailFee.getMeasuresFee2()!=null? jkxlDetailFee.getMeasuresFee2():0.00;
           zFees[8][1] = jkxlDetailFee.getMeasuresFee3()!=null? jkxlDetailFee.getMeasuresFee3():0.00;
           zFees[9][1] = jkxlDetailFee.getOtherCost()!=null? jkxlDetailFee.getOtherCost():0.00;
           zFees[10][1] = jkxlDetailFee.getFees()!=null? jkxlDetailFee.getFees():0.00;
           zFees[11][1] = jkxlDetailFee.getTex()!=null? jkxlDetailFee.getTex():0.00;
           zFees[12][1] = jkxlDetailFee.getRgFtFee()!=null? jkxlDetailFee.getRgFtFee():0.00;
           zFees[13][1] = jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
         }
       }
       //合并建筑和安装部分
      JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
      jkxlDetailFee.setOriginalTotalCost(new BigDecimal(zFees[0][0]+zFees[0][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setZjgcFee(new BigDecimal(zFees[1][0]+zFees[1][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setOriginalRgCost(new BigDecimal(zFees[2][0]+zFees[2][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setOriginalCbrCost(new BigDecimal(zFees[3][0]+zFees[3][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setOriginalFbrCost(new BigDecimal(zFees[4][0]+zFees[4][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setOriginalJxCost(new BigDecimal(zFees[5][0]+zFees[5][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//      jkxlDetailFee.setRgCost(new BigDecimal(zFees[6][0]+zFees[6][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setMeasuresFee1(new BigDecimal(zFees[6][0]+zFees[6][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setMeasuresFee2(new BigDecimal(zFees[7][0]+zFees[7][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setMeasuresFee3(new BigDecimal(zFees[8][0]+zFees[8][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setOtherCost(new BigDecimal(zFees[9][0]+zFees[9][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setFees(new BigDecimal(zFees[10][0]+zFees[10][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setTex(new BigDecimal(zFees[11][0]+zFees[11][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setRgFtFee(new BigDecimal(zFees[12][0]+zFees[12][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setTotalFee(new BigDecimal(zFees[13][0]+zFees[13][1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      jkxlDetailFee.setWbsCode("合计");
      jkxlDetailFee.setParentNode("20000000");
      jkxlDetailFee.setFeeType("1");
      JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
      jkxlDetailFee1.setWbsCode("合计");
      jkxlDetailFee1.setParentNode("24000000");
      double total1 = 0.00;
      for (JkxlDetailFee jkxlDetailFee2 :list2){
        total1 += jkxlDetailFee2.getTotalFee();
      }
      jkxlDetailFee1.setFeeType("0");
      jkxlDetailFee1.setTotalFee(total1);
      List<JkxlDetailFee> list3 = new ArrayList<>();
      list3.add(jkxlDetailFee);
      list3.add(jkxlDetailFee1);
      return list3;

    }
    //架空线路和电缆求和
    double total1 = 0.00;
    for (JkxlDetailFee jkxlDetailFee :list2){
      total1 += jkxlDetailFee.getTotalFee();
    }
    double [] zFee = new double[14];
    //各项费用求和
    for(JkxlDetailFee jkxlDetailFee :list1){
      zFee[0] += jkxlDetailFee.getOriginalTotalCost()!=null? jkxlDetailFee.getOriginalTotalCost():0.00;
      zFee[1] += jkxlDetailFee.getZjgcFee()!=null? jkxlDetailFee.getZjgcFee():0.00;
      zFee[2] += jkxlDetailFee.getOriginalRgCost()!=null? jkxlDetailFee.getOriginalRgCost():0.00;
      zFee[3] += jkxlDetailFee.getOriginalCbrCost()!=null? jkxlDetailFee.getOriginalCbrCost():0.00;
      zFee[4] += jkxlDetailFee.getOriginalFbrCost()!=null? jkxlDetailFee.getOriginalFbrCost():0.00;
      zFee[5] += jkxlDetailFee.getOriginalJxCost()!=null? jkxlDetailFee.getOriginalJxCost():0.00;
      zFee[6] += jkxlDetailFee.getMeasuresFee1()!=null? jkxlDetailFee.getMeasuresFee1():0.00;
      zFee[7] += jkxlDetailFee.getMeasuresFee2()!=null? jkxlDetailFee.getMeasuresFee2():0.00;
      zFee[8] += jkxlDetailFee.getMeasuresFee3()!=null? jkxlDetailFee.getMeasuresFee3():0.00;
      zFee[9] += jkxlDetailFee.getOtherCost()!=null? jkxlDetailFee.getOtherCost():0.00;
      zFee[10] += jkxlDetailFee.getFees()!=null? jkxlDetailFee.getFees():0.00;
      zFee[11] += jkxlDetailFee.getTex()!=null? jkxlDetailFee.getTex():0.00;
      zFee[12] += jkxlDetailFee.getRgFtFee()!=null? jkxlDetailFee.getRgFtFee():0.00;
      zFee[13] += jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
    }
    JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
    JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
    jkxlDetailFee.setOriginalTotalCost(new BigDecimal(zFee[0]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setZjgcFee(new BigDecimal(zFee[1]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setOriginalRgCost(new BigDecimal(zFee[2]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setOriginalCbrCost(new BigDecimal(zFee[3]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setOriginalFbrCost(new BigDecimal(zFee[4]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setOriginalJxCost(new BigDecimal(zFee[5]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//    jkxlDetailFee.setRgCost(new BigDecimal(zFee[6]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setMeasuresFee1(new BigDecimal(zFee[6]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setMeasuresFee2(new BigDecimal(zFee[7]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setMeasuresFee3(new BigDecimal(zFee[8]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setOtherCost(new BigDecimal(zFee[9]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setFees(new BigDecimal(zFee[10]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setTex(new BigDecimal(zFee[11]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setRgFtFee(new BigDecimal(zFee[12]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setTotalFee(new BigDecimal(zFee[13]).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
    jkxlDetailFee.setWbsCode("合计");
    if(type.equals(BashInfo.PROJECT_JKXL)) {
      jkxlDetailFee.setParentNode("30000000");
    }
    if(type.equals(BashInfo.PROJECT_DLXL)) {
      jkxlDetailFee.setParentNode("40000000");
    }
    jkxlDetailFee.setFeeType("1"); //1:本体部分  0,其他部分
    jkxlDetailFee1.setWbsCode(BashInfo.QT_HJ);
    if(type.equals(BashInfo.PROJECT_JKXL)) {
      jkxlDetailFee1.setParentNode("34000000");
    }
    if(type.equals(BashInfo.PROJECT_DLXL)) {
      jkxlDetailFee1.setParentNode("44000000");
    }
    jkxlDetailFee1.setFeeType("0");//1:本体部分  0,其他部分
    jkxlDetailFee1.setTotalFee(total1);
    List<JkxlDetailFee> list3 = new ArrayList<>();
    list3.add(jkxlDetailFee);
    list3.add(jkxlDetailFee1);
     return list3;


  }

  //数据核验，各项差额计算
  private List<Settlement> disposeData(List<JkxlDetailFee> list, List<Settlement> list3,List<JkxlAdjustFee> list2) {
    double originalTotal  = 0.00;
    double otherCost =0.00;
    double measuresFee =0.00;
    double fees = 0.00;
    double tex = 0.00;
    double totalFee = 0.00;
    double originalFbrCost = 0.00;
    double originalCbrCost = 0.00;
    double ce1 = 0.00;
    double qt = 0.00;
    //总计算差额
    for(JkxlAdjustFee jkxlAdjustFee : list2){
      if(jkxlAdjustFee.getType().equals(BashInfo.CE)){
        ce1 = jkxlAdjustFee.getFee();
      }
    }
    for (JkxlDetailFee jkxlDetailFee : list){
      if(jkxlDetailFee.getWbsCode().equals(BashInfo.QT_HJ) && jkxlDetailFee.getOriginalTotalCost() != null){
           originalTotal  =   jkxlDetailFee.getOriginalTotalCost();  //分部分项合计
           otherCost = jkxlDetailFee.getOtherCost();     //其他费用
           measuresFee = jkxlDetailFee.getMeasuresFee1()+jkxlDetailFee.getMeasuresFee2()+jkxlDetailFee.getMeasuresFee3();   //措施费
           fees = jkxlDetailFee.getFees();    //规费
           tex = jkxlDetailFee.getTex();   //税金
           totalFee = jkxlDetailFee.getTotalFee();   //合计
           originalFbrCost = jkxlDetailFee.getOriginalFbrCost();  //发包人合计
           originalCbrCost = jkxlDetailFee.getOriginalCbrCost();   //承包人合计
      }
      if(jkxlDetailFee.getWbsCode().equals(BashInfo.QT_HJ) && jkxlDetailFee.getOriginalTotalCost() == null){
         qt = jkxlDetailFee.getTotalFee();
      }
    }
    for(Settlement settlement : list3){
      //分部分项工程费差额
      if(settlement.getEntryName().contains(BashInfo.JS_FBFX)){
        double ce = Math.abs(settlement.getMoney() - originalTotal);
        settlement.setTotalFee(originalTotal);
        settlement.setDifference(new BigDecimal(ce).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      }
      //发包人材料采购费
      if(settlement.getEntryName().contains("发包人") || settlement.getEntryName().contains("招标人")){
        double ce = Math.abs(settlement.getMoney() - originalFbrCost);
        settlement.setTotalFee(originalFbrCost);
        settlement.setDifference(new BigDecimal(ce).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      }
      //措施费差额
      if(settlement.getEntryName().contains(BashInfo.JS_CSXM)){
        double ce = Math.abs(settlement.getMoney() - measuresFee);
        settlement.setTotalFee(measuresFee);
        settlement.setDifference(new BigDecimal(ce).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      }
      //规费差额
      if(settlement.getEntryName().contains(BashInfo.JS_GF)){
        double ce = Math.abs(settlement.getMoney() - fees);
        settlement.setTotalFee(fees);
        settlement.setDifference(new BigDecimal(ce).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      }
      //税金差额
      if(settlement.getEntryName().contains(BashInfo.JS_SJ)){
        double ce = Math.abs(settlement.getMoney() - tex);
        settlement.setTotalFee(tex);
        settlement.setDifference(new BigDecimal(ce).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      }
      //其他费用差额
      if(settlement.getEntryName().contains(BashInfo.JS_QTXM)){
        double ce = Math.abs(settlement.getMoney() - otherCost-qt);
        settlement.setTotalFee(otherCost+qt);
        settlement.setDifference(new BigDecimal(ce).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      }
      //结算差额
      if(settlement.getEntryName().contains("结算") || settlement.getEntryName().contains("报价") || settlement.getEntryName().contains("下浮后") || settlement.getEntryName().contains("最高")){
        settlement.setTotalFee(totalFee);
        settlement.setDifference(new BigDecimal(ce1).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
      }


    }
    return list3;
  }




}
