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

import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.utils.sg.ExcelUtil;
import com.jeeplus.modules.sg.overheadline.entity.JkxlAdjustFee;
import com.jeeplus.modules.sg.overheadline.entity.JkxlDetailFee;
import com.jeeplus.modules.sg.overheadline.entity.Settlement;
import com.jeeplus.modules.sg.overheadline.entity.WbsProject;
import com.jeeplus.modules.sg.overheadline.mapper.OverheadLineMapper;
import com.jeeplus.modules.sg.overheadline.util.BashInfo;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OverheadLineService {
    @Autowired
    private OverheadLineMapper overheadLineMapper;
    @Autowired
    private FeeAdjustService feeAdjustService;
    @Autowired
    private RuleRatioService ruleRatioService;
    @Autowired
    private SettlementService settlementService;




   public List<JkxlDetailFee> getList(String id){
       return overheadLineMapper.getList(id);
   }

   public List<JkxlDetailFee> findList(String id){
       List<JkxlDetailFee> list = overheadLineMapper.findList(id);
       double btTotal = 0.00;
       double qtTotal = 0.00;
       for(JkxlDetailFee jkxlDetailFee : list){
           //本体费用求和
           if(jkxlDetailFee.getOriginalTotalCost() != null){
               btTotal += jkxlDetailFee.getTotalFee();
           }
           //其他费用求和
           if(jkxlDetailFee.getOriginalTotalCost() == null && jkxlDetailFee.getTotalFee() != null){
               qtTotal += jkxlDetailFee.getTotalFee();
           }
       }
       for(JkxlDetailFee jkxlDetailFee : list){
           if(jkxlDetailFee.getWbsCode().equals("30000000")){
               jkxlDetailFee.setTotalFee(btTotal+qtTotal);
           }
           if(jkxlDetailFee.getWbsCode().equals("39000000")){
               jkxlDetailFee.setTotalFee(btTotal);
           }
           if(jkxlDetailFee.getWbsCode().equals("34000000")){
               jkxlDetailFee.setTotalFee(qtTotal);
           }
       }
       return list;

   }

   public JkxlDetailFee getDate(String id, String wbsCode){
       return overheadLineMapper.getData(id,wbsCode);
   }


    @Transactional(readOnly = false)
    public void updataDate(JkxlDetailFee jkxlDetailFee){
        overheadLineMapper.updateDate(jkxlDetailFee);
    }

    @Transactional(readOnly = false)
   public void saveProject(WbsProject wbsProject){
       overheadLineMapper.saveProject(wbsProject);
   }

    @Transactional(readOnly = false)
   public void save(MultipartFile file,double sl,String name,String id) throws IOException, InvalidFormatException {
//       WbsProject wbsProject = new WbsProject();
//       wbsProject.setId(id);
//       wbsProject.setName(name);
//       wbsProject.setCreateDate(new Date());
       //保存项目
//       overheadLineMapper.saveProject(wbsProject);
       //获取调差系数
       double[] tcRate = new double[3];
       tcRate[0] = ruleRatioService.getValueByName(BashInfo.RATIO_RGTC).getValue()/100;
       tcRate[1] = ruleRatioService.getValueByName(BashInfo.RATIO_CLTC).getValue()/100;
       tcRate[2] = ruleRatioService.getValueByName(BashInfo.RATIO_JXTC).getValue()/100;
       //读取表格
       ImportExcel importExcel = new ImportExcel(file,1,BashInfo.SHEET_FBFXJJ);  //分部分项工程量清单计价表
       ImportExcel csqd = new ImportExcel(file,1,BashInfo.SHEET_CSB);  // 措施项目清单计价表（一）
       ImportExcel gf = new ImportExcel(file,1,BashInfo.SHEET_GFQD);  //规费项目清单计价表
       ImportExcel qt = new ImportExcel(file,1,BashInfo.SHEET_QTFY);  //其他项目清单计价表
       ImportExcel total = new ImportExcel(file,1,BashInfo.SHEET_HZB );  //工程项目竣工结算汇总表
       ImportExcel gzhz = new ImportExcel(file,1,BashInfo.SHEET_GCHZ );   //架空线路工程费用汇总表
       //数据核验数据
       Map<String,Object[]> builtMap = BashInfo.getCompletionWorks(total);//获取工程项目竣工所有数据
       double builtTotal = ExcelUtil.getDouble(gzhz,BashInfo.QT_HJ,0,2);//获取架空线路工程费用汇总表 分部分项 合计
       double wordtotal =  ExcelUtil.getDouble(csqd,BashInfo.QT_HJ,0,4);//获取措施费清单计价表
       double otherTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
       int[] comlumn2 = {12,14,15};//定义所要数组
       double[] divisiontotal=ExcelUtil.getDoubleArray(importExcel,BashInfo.PROJECTNAME,2,comlumn2);//获取分部分项比对数据清单
       double feescount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
       //数据核验并保存数据库
       settlementService.save(builtMap,builtTotal,divisiontotal,wordtotal,otherTotal,feescount,id);
      //获取补增值税税率调整差额和扣减甲供材超供材料费
       double[] wsbFees = ExcelUtil.getDoubleArray(total, new String[]{BashInfo.BZZSSL,BashInfo.KJJGCC},1,2);
       double[] originalTotalFees = ExcelUtil.getDoubleArray(gzhz,BashInfo.HZ_ALL,1,2); //获取合计费
       double[] zjgcFees = ExcelUtil.getDoubleArray(gzhz,BashInfo.HZ_ALL,1,3);  //获取直接工程费
       double[][] originalFees = ExcelUtil.getDoubleArrays(importExcel,BashInfo.MX_ALL,2, new int[]{13, 14, 15, 17}); //获取原始费用
       double texRate = ExcelUtil.getTexRate(total); //获取税率
       double totalFee = ExcelUtil.getDouble(importExcel,BashInfo.PROJECTNAME,2,12);  //获取架空线路合计金额
       double[] csRate = BashInfo.getCsRate(csqd); //获取措施费率
       double[] gfRate = BashInfo.getGfRate(gf);  //获取规费率
       double[] qtFees = BashInfo.getQtFees(qt);  //获取其他费用
       double gcjsFee = BashInfo.getGcjsFee(total);  //获取工程结算价
       //将本体工程费用明细存入集合
       List<JkxlDetailFee> list = new ArrayList<>();
       for(int i=0;i<originalTotalFees.length;i++){
           JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
           double originalTotalCost = originalTotalFees[i]; //原合计
           double otherRate = originalTotalCost/totalFee;   //其他费用分摊比例
           double orRate = new BigDecimal(otherRate).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入
           double otherFee = qtFees[2]*otherRate;  //其他费用
           double zjgcFee = zjgcFees[i];//直接工程费
           double originalRgCost = originalFees[i][0];// 原人工费
           double rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
           double originalCbrCost = originalFees[i][1];  //原承包人采购
           double cbrCost = originalCbrCost/(1+tcRate[1]);  //后承包人采购
           double originalFbrCost = originalFees[i][2];  //原发包人采购
           double fbrCost = originalFbrCost/(1+sl); //后发包人采购
           double originalJxrCost = originalFees[i][3];   //原机械费
           double jxCost = originalJxrCost/(1+tcRate[2]); //后机械费
           double measuresFee1 = (zjgcFee-fbrCost*sl)*csRate[0]; //措施费一（2）
           double measuresFee2 = rgCost*csRate[1];   //措施费一（1）
           double measuresFee3 = 0.00; //措施费二
           double fees = rgCost*gfRate[1]*1.12+rgCost*gfRate[0];  //规费
           double tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee-originalFbrCost)*texRate; //税金
           double totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax-originalFbrCost; //合计
           jkxlDetailFee.setOriginalTotalCost(originalTotalCost);
           jkxlDetailFee.setOriginalRate(orRate);
           jkxlDetailFee.setOriginalRgCost(originalRgCost);
           jkxlDetailFee.setRgCost(rgCost);
           jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
           jkxlDetailFee.setCbrCost(cbrCost);
           jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
           jkxlDetailFee.setFbrCost(fbrCost);
           jkxlDetailFee.setOriginalJxCost(originalJxrCost);
           jkxlDetailFee.setJxCost(jxCost);
           jkxlDetailFee.setMeasuresFee1(measuresFee1);
           jkxlDetailFee.setMeasuresFee2(measuresFee2);
           jkxlDetailFee.setMeasuresFee3(measuresFee3);
           jkxlDetailFee.setFees(fees);
           jkxlDetailFee.setOtherCost(otherFee);
           jkxlDetailFee.setTex(tax);
           jkxlDetailFee.setTotalFee(totalfee);
           jkxlDetailFee.setZjgcFee(zjgcFee);
           jkxlDetailFee.setWbsCode(BashInfo.WBSIDS[i]);
           jkxlDetailFee.setId(id);
           list.add(jkxlDetailFee);
       }
       double tz = 0.00; //总合计金额
       for(JkxlDetailFee jkxlDetailFee : list){
           tz += jkxlDetailFee.getTotalFee();
       }
       double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1]-wsbFees[0]-wsbFees[1]);
//       if(gcjsFee>tz){
//           double gxFee = gcjsFee-tz-qtFees[0]-qtFees[1]-wsbFees[0]-wsbFees[1];
//           double rate1 = 0.00;
//           double gxFee1 = 0.00;
//           double totalF = 0.00;
//           for(JkxlDetailFee jkxlDetailFee : list){
//               rate1 = jkxlDetailFee.getTotalFee()/tz;
//               gxFee1 = gxFee*rate1;
//               totalF = gxFee1 + jkxlDetailFee.getTotalFee();
//               jkxlDetailFee.setGxFee(Math.abs(gxFee1));
//               jkxlDetailFee.setTotalFee(totalF);
//           }
//       }else {
//           double gxFee = tz+qtFees[0]+qtFees[1]+wsbFees[0]+wsbFees[1]-gcjsFee;
//           double rate1 = 0.00;
//           double gxFee1 = 0.00;
//           double totalF = 0.00;
//           for(JkxlDetailFee jkxlDetailFee : list){
//               rate1 = jkxlDetailFee.getTotalFee()/tz;
//               gxFee1 = gxFee*rate1;
//               totalF = jkxlDetailFee.getTotalFee()-gxFee1;
//               jkxlDetailFee.setGxFee(Math.abs(gxFee1));
//               jkxlDetailFee.setTotalFee(totalF);
//           }
//
//       }
       //将其他费用存入集合
       JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
       JkxlDetailFee jkxlDetailFee2 = new JkxlDetailFee();
       jkxlDetailFee1.setId(id);
       jkxlDetailFee1.setWbsCode("34120000");
       jkxlDetailFee1.setTotalFee(qtFees[0]);
       jkxlDetailFee2.setWbsCode("34220000");
       jkxlDetailFee2.setTotalFee(qtFees[1]);
       jkxlDetailFee2.setId(id);
       list.add(jkxlDetailFee1);
       list.add(jkxlDetailFee2);
       //保存费用明细
       for(JkxlDetailFee jkxlDetailFee:list){
           overheadLineMapper.save(jkxlDetailFee);
       }
       //保存未识别费用
       JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
       jkxlAdjustFee.setId(id);
       jkxlAdjustFee.setType(BashInfo.BZZSSL);
       jkxlAdjustFee.setFee(wsbFees[0]);
       feeAdjustService.save(jkxlAdjustFee);
       JkxlAdjustFee jkxlAdjustFee1 = new JkxlAdjustFee();
       jkxlAdjustFee1.setId(id);
       jkxlAdjustFee1.setType(BashInfo.KJJGCC);
       jkxlAdjustFee1.setFee(wsbFees[1]);
       feeAdjustService.save(jkxlAdjustFee1);
       JkxlAdjustFee jkxlAdjustFee2 = new JkxlAdjustFee();
       jkxlAdjustFee2.setId(id);
       jkxlAdjustFee2.setType(BashInfo.CE);
       jkxlAdjustFee2.setFee(gxFee);
       feeAdjustService.save(jkxlAdjustFee2);
   }




}
