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

import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.sg.overheadline.entity.Settlement;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class BashInfo {
    public static final String PROJECTNAME = "架空线路";
    public static final String SHEET_FBFXJJ = "分部分项工程量清单计价表";
    public static final String SHEET_HZB = "工程项目竣工结算汇总表";
    public static final String SHEET_CSB = "措施项目清单计价表（一）";
    public static final String SHEET_QTFY = "其他项目清单计价表";
    public static final String SHEET_GFQD = "规费项目清单计价表";
    public static final String SHEET_GCHZ = "架空线路工程费用汇总表";

    public static final String JCGC = "1 基础工程";
    public static final String GTGC = "2 杆塔工程";
    public static final String JDGC = "3 接地工程";
    public static final String JXGC = "4 架线工程";
    public static final String FJAZGC = "5 附件安装工程";
    public static final String FZGC = "6 辅助工程";

    public static final String RATIO_RGTC = "人工调差系数";
    public static final String RATIO_CLTC = "材料调差系数";
    public static final String RATIO_JXTC = "机械调差系数";
    public static final String RATIO_CCRGTC = "拆除人工调差系数";
    public static final String RATIO_CCCLTC = "拆除材料调差系数";
    public static final String RATIO_CCJXTC = "拆除机械调差系数";

    public static final String CS_LS = "临时设施费";
    public static final String CS_AQWM = "安全文明施工费";
    public static final String GF_WXZY = "危险作业意外伤害保险费";
    public static final String QT_SGCDZY = "施工场地租用费";
    public static final String QT_ZBDL = "招标代理";
    public static final String QT_HJ = "合计";
    public static final String BZZSSL = "补增值税税率调整差额";
    public static final String KJJGCC = "扣减甲供材超供材料费";
    public static final String GCJS = "工程结算价";
    public static final String TBJG = "竣工结算价合计";
    public static final String TBJG1 = "投标报价";
    public static final String CE = "差额";

    public static final String HZ_JCGC = "基础工程";
    public static final String HZ_GTGC = "杆塔工程";
    public static final String HZ_JDGC = "接地工程";
    public static final String HZ_JXGC = "架线工程";
    public static final String HZ_FJAZGC = "附件安装工程";
    public static final String HZ_FZ = "辅助工程";

    public static final String TZ_RGF = "人工费";
    public static final String TZ_CBRCG = "承包分采购";
    public static final String TZ_FBRCG = "发包人采购";
    public static final String TZ_JX = "机械费";
    public static final String TZ_CSF1 = "措施费-1";
    public static final String TZ_CSF2 = "措施费-2";
    public static final String TZ_CSF3 = "措施费二";
    public static final String TZ_QTF = "其他费";
    public static final String TZ_GF = "规费";
    public static final String TZ_SF = "税费";

    public static final String JS_FBFX="分部分项工程费";
    public static final String JS_CBRF="承包人采购设备费";
    public static final String JS_CSXM="措施项目费";
    public static final String JS_QTXM="其他项目费";
    public static final String JS_GF="规费";
    public static final String JS_SJ="税金";
    public static final String JS_FBRF="发包人采购材料费";
    public static final String JS_JGJS="竣工结算价合计";
    public static final String JS_BZZS="补增值税税率调整差额";
    public static final String JS_KJJG="扣减甲供材超供材料费";
    public static final String JS_GCJS="工程结算价";
    public static final String JS_REASON="数据核验正确";

    public static final String RESON_FBFX="《工程项目竣工结算汇总表》中分部分项金额与《架空线路工程费用汇总表》中分部分项单项合计金额/《分布分项工程量清单计价表》中分布分项单项金额不一致";
    public static final String RESON_CBRF="《工程项目竣工结算汇总表》中承包人采购设备费与《分布分项工程量清单计价表》中'架空线路'合价承包人采购不一致";
    public static final String RESON_CSXM="《工程项目竣工结算汇总表》中措施项目费与《措施项目清单计价表一》的合计+《措施项目清单计价表二》的合计不一致";
    public static final String RESON_QTXM="《工程项目竣工结算汇总表》中其他项目费与《其他项目清单计价表》的合计费用不一致";
    public static final String RESON_GF="《工程项目竣工结算汇总表》中规费与《规费项目清单计价表》合计不一致";
    public static final String RESON_SJ="《工程项目竣工结算汇总表》中税金 (税率: 9%)";
    public static final String RESON_FBRF="《工程项目竣工结算汇总表》中发包人采购材料费与《分布分项工程量清单计价表》中'架空线路'合价发包人采购不一致";
    public static final String RESON_JGJS="《工程项目竣工结算汇总表》中竣工结算价合计金额不一致";
    public static final String RESON_BZZS="补增值税税率调整差额";
    public static final String RESON_KJJG="扣减甲供材超供材料费";
    public static final String RESON_GCJS="《工程项目竣工结算汇总表》中工程结算价不一致";




    //主体模块WBSID
    public static final String[] WBSIDS = {"39100000","39200000","39300000","39400000","39500000","39600000"};

    public static final String[] HZ_ALL = {HZ_JCGC,HZ_GTGC,HZ_JDGC,HZ_JXGC,HZ_FJAZGC,HZ_FZ};
    public static final String[] MX_ALL ={JCGC,GTGC,JDGC,JXGC,FJAZGC,FZGC};


    /**
     *获取措施费率
     */
    public static double[] getCsRate(ImportExcel importExcel){
        int lastRow = importExcel.getLastDataRowNum();
        double lsRate = 0.00;
        double aqRate = 0.00;
        double totalRate = 0.00;
        for(int i=2;i<lastRow;i++){
            String type = importExcel.getValue(i+1,1);
            if(type.equals(CS_LS)){
                lsRate = importExcel.getDouble(i+1,3);
            }
            if(type.equals(CS_AQWM)){
                aqRate = importExcel.getDouble(i+1,3);
            }
            totalRate += importExcel.getDouble(i+1,3);
        }
        double[] rate = new double[2];
        rate[0] = (lsRate+aqRate)/100;
        rate[1] = (totalRate-lsRate-aqRate)/100;
        return  rate;
    }

    /**
     *获取规费率
     */
    public static double[] getGfRate(ImportExcel importExcel) {
        int lastRow = importExcel.getLastDataRowNum();
        double totalRate = 0.00;
        double wxzyRate = 0.00;
        for (int i = 2; i < lastRow; i++) {
            String type = importExcel.getValue(i + 1, 1);
            if (type.equals(GF_WXZY)) {
                wxzyRate = importExcel.getDouble(i + 1, 3);
            }
            if (!importExcel.getValue(i + 1, 2).equals("")) {
                totalRate += importExcel.getDouble(i + 1, 3);
            }
        }
        double[] rate = new double[2];
        rate[0] = wxzyRate / 100;
        rate[1] = (totalRate - wxzyRate) / 100;
        return rate;
    }

    /**
     * 获取其他费用
     */
    public static double[] getQtFees(ImportExcel importExcel) {
        int lastRow = importExcel.getLastDataRowNum();
        double cdFree = 0.00;
        double zbFree = 0.00;
        double totalFree = 0.00;
        for(int i=2;i<lastRow;i++){
            String type = importExcel.getValue(i+1,1);
            String type1 = importExcel.getValue(i+1,0);
            if(type.equals(QT_SGCDZY)){
                cdFree = importExcel.getDouble(i+1,2);
                continue;
            }
            if(type.contains(QT_ZBDL)){
                zbFree = importExcel.getDouble(i+1,2);
                continue;
            }
            if(type1.equals(QT_HJ)){
                totalFree = importExcel.getDouble(i+1,2);
                continue;
            }
        }
        double[] rate = new double[3];
        rate[0] = cdFree;
        rate[1] = zbFree;
        rate[2] = totalFree-cdFree-zbFree;
        return  rate;
    }

    /**
     * 获取工程结算价
     */
    public static double getGcjsFee(ImportExcel importExcel){
        int lastRow = importExcel.getLastDataRowNum();
        double gcjs = 0.00;
        for(int i=0;i<lastRow;i++){
            String type = importExcel.getValue(i+1,1);
            if(type.equals(BashInfo.TBJG) || type.equals(BashInfo.GCJS)){
                gcjs = importExcel.getDouble(i+1,2);
            }
        }
        return  gcjs;

    }
    /*
     获取《工程项目竣工结算汇总表》匹配数据
    */
    public static Map<String,Object[]> getCompletionWorks(ImportExcel importExcel){
        Map<String,Object[]> map  = new LinkedHashMap<String, Object[]>();
        int lastDataRowNum =  importExcel.getLastDataRowNum();
        String key="";
        double val=0.0;
        String text = "";
        Object[] objects;
        for (int i = 2; i < lastDataRowNum; i++) {
            objects = new Object[2];
            key =  importExcel.getValue(i+1,1);
            if(key.contains(BashInfo.JS_SJ)){
                key=BashInfo.JS_SJ;
            }
            switch (key){
                case BashInfo.JS_FBFX:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_CBRF:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_CSXM:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_QTXM:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_GF:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_SJ:

                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_FBRF:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_JGJS:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_BZZS:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_KJJG:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
                case BashInfo.JS_GCJS:
                    val = importExcel.getDouble(i+1,2);
                    text=importExcel.getValue(i+1,3);
                    objects[0] = val;
                    objects[1] = text;
                    map.put(key, objects);
                    break;
            }
        }
        return  map;
    }

    /**
     * 结算汇总数据校验
     * @param builtMap 工程竣工结算Map集合
     * @param builtTotal
     * @param divisiontotal
     * @param wordtotal
     * @param otherTotal
     * @param feescount
     * @param id
     * @return
     */
    public static List<Settlement> getTotality(Map<String,Object[]> builtMap, double builtTotal, double[] divisiontotal, double wordtotal, double otherTotal, double feescount, String id){
        List<Settlement> list = new ArrayList<Settlement>();
        String key="";
        double fbr = 0.0;//发包人金额
        double count=0.0;//1+2+3+4+5+6
        double countProtect=0.0;//竣工结算+不增值税
        double creatProtect=0.0;//工程结算数据
        String status="1";//状态 默认值 0
        Settlement  settlement;
        String reason = BashInfo.JS_REASON;
        for (Map.Entry<String,Object[]> map:builtMap.entrySet()){
            key = map.getKey();
            Object[] val = map.getValue();
            double money = Double.parseDouble(val[0].toString());
            String remarks=val[1].toString();
            switch (key){
                case BashInfo.JS_FBFX:
                    if(money==builtTotal&&money==divisiontotal[0]){
                        status="0";
                    }else {
                        reason = BashInfo.RESON_FBFX;
                    }
                    count+=money;
                    break;
                case  BashInfo.JS_CBRF:
                    if(money==divisiontotal[1]){
                        status="0";
                    }else {
                        reason = BashInfo.RESON_CBRF;
                    }
                    count+=money;
                    break;
                case BashInfo.JS_CSXM:
                    if(money==wordtotal){
                        status="0";
                    }else {
                        reason = BashInfo.RESON_CSXM;
                    }
                    count+=money;
                    break;
                case BashInfo.JS_QTXM:
                    if(money==otherTotal){
                        status="0";
                    }else {
                        reason = BashInfo.RESON_QTXM;
                    }
                    count+=money;
                    break;
                case BashInfo.JS_GF:
                    if(money==feescount){
                        status="0";
                    }else {
                        reason = BashInfo.RESON_GF;
                    }
                    count+=money;
                    break;
                case BashInfo.JS_SJ:
                    status="0";
                    count+=money;
                    break;
                case BashInfo.JS_FBRF:
                    if(money==divisiontotal[2]){
                        status="0";
                    }else {
                        reason = BashInfo.RESON_FBRF;
                    }
                    fbr=money;
                    break;
                case BashInfo.JS_JGJS:
                    double totalcount = count-fbr;
                    if(money==totalcount){
                        status="0";
                    }else {
                        reason = BashInfo.RESON_JGJS;
                    }
                    countProtect+=money;
                    break;
                case BashInfo.JS_BZZS:
                    status="0";
                    countProtect+=money;
                    break;
                case BashInfo.JS_KJJG:
                    status="0";
                    creatProtect=money;
                    break;
                case BashInfo.GCJS:
                    double reduce = countProtect-creatProtect;
                    if (money==reduce){
                        status="0";
                    }else {
                        reason = BashInfo.RESON_GCJS;
                    }
                    break;
            }
            settlement = new Settlement();
            settlement.setId(id);
            settlement.setEntryName(key);
            settlement.setMoney(money);
            settlement.setStatus(status);
            settlement.setRemarks(remarks);
            settlement.setReason(reason);
            list.add(settlement);
            status="1";
            reason = BashInfo.JS_REASON;
        }
        return list;
    }

}
