package com.jeeplus.common.utils.sg;

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

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {

    /**
     * 根据字段读取数据
     * 方法逻辑：遍历column1列值，当值等于st时，获取对应column2列值
     * @param importExcel
     * @param st
     * @param column1
     * @param column2
     * @return
     */
    public static double getDouble(ImportExcel importExcel,String st,int column1,int column2){
        int lastRow = importExcel.getLastDataRowNum();
        double d = 0.00;
        for(int i=0;i<lastRow;i++){
            String type = importExcel.getValue(i+1,column1);
            if(type.equals("")){
                continue;
            }
            if(type.equals(st)){
                d = importExcel.getDouble(i+1,column2);
                break;
            }
        }
        return d;
    }

    public static double getDouble(ImportExcel importExcel,String[] st,int column1,int column2){
        int lastRow = importExcel.getLastDataRowNum();
        double d = 0.00;
        for(int i=0;i<lastRow;i++){
            String type = importExcel.getValue(i+1,column1);
            if(type.equals("")){
                continue;
            }
            for(int j=0;j<st.length;j++) {
                if (type.equals(st[j])) {
                    d = importExcel.getDouble(i + 1, column2);
                    if(d != 0.00){
                        break;
                    }
                }
            }
        }
        return d;
    }



    /**
     * 根据字段读取数据，遍历column1的文字，与sts中文字进行匹配，匹配上了，就获取coulumn2数据增加到新数组对应位置
     * @param importExcel 文件对象
     * @param sts   字段
     * @param column1 字段列号
     * @param column2 读取数据列号
     * @return
     */
    public static double[] getDoubleArray(ImportExcel importExcel,String[] sts,int column1,int column2){
        int lastRow = importExcel.getLastDataRowNum();
        int len = sts.length;
        double[] d = new double[len];
        for(int i=0;i<lastRow;i++){
            String type = importExcel.getValue(i+1,column1);
            if(type.equals("")){
                continue;
            }
            for(int j=0;j<len;j++){
                if(type.equals(sts[j])){
                    d[j] = importExcel.getDouble(i+1,column2);
                }
            }
        }
        return d;
    }


    /**
     * 遍历excel页签，column1列，查找值为st的行，返回column2中数字表示多个列的值
     * @param importExcel
     * @param st
     * @param column1
     * @param column2
     * @return
     */
    public static double[] getDoubleArray(ImportExcel importExcel,String st,int column1,int[] column2){
        int lastRow = importExcel.getLastDataRowNum();
        int len = column2.length;
        double[] d = new double[len];
        for(int i=0;i<lastRow;i++){
            String type = importExcel.getValue(i+1,column1);
            if(type.equals("")){
                continue;
            }
            if(type.equals(st)){
                for(int j=0;j<len;j++) {
                    d[j] = importExcel.getDouble(i + 1, column2[j]);
                }
                break;
            }
        }
        return d;
    }

    public static double[] getBashArray(ImportExcel importExcel,String st,int column1,int[] column2){
        int lastRow = importExcel.getLastDataRowNum();
        int len = column2.length;
        double[] d = new double[len];
        for(int i=0;i<lastRow;i++){
            String type = importExcel.getValue(i+1,column1);
            if(type.equals("")){
                continue;
            }
            if(type.contains(st)){
                for(int j=0;j<len;j++) {
                    d[j] = importExcel.getDouble(i + 1, column2[j]);
                }
                break;
            }
        }
        return d;
    }



    /**
     * 根据字段读取数据
     * @param importExcel 文件对象
     * @param sts  字段
     * @param column1 字段列号
     * @param column2 读取数据列号
     * @return
     */

    public static double[][] getDoubleArrays(ImportExcel importExcel,String[] sts,int column1,int[] column2){
        int lastRow = importExcel.getLastDataRowNum();
        int len = sts.length;
        int len1 = column2.length;
        double[][] d = new double[len][len1];
        for(int i=0;i<lastRow;i++){
            String type = importExcel.getValue(i+1,column1);
            if(type.equals("")){
                continue;
            }
            for(int j=0;j<len;j++){
                if(type.equals(sts[j])){
                    for(int k=0;k<column2.length;k++) {
                        d[j][k] = importExcel.getDouble(i + 1, column2[k]);
                    }
                }
            }
        }
        return d;
    }

//    public static double[][] getBashArray(ImportExcel importExcel,String[] sts,int column1,int[] column2){
//        int lastRow = importExcel.getLastDataRowNum();
//        int len = sts.length;
//        int len1 = column2.length;
//        double[][] d = new double[len][len1];
//        for(int j=0;j<len;j++){
//          for(int i=0;i<lastRow;i++){
//            String type = importExcel.getValue(i+1,column1);
//            if(type.equals("")){
//                continue;
//            }
//
//                if(type.contains(sts[j])){
//                    for(int k=0;k<column2.length;k++) {
//                        d[j][k] = importExcel.getDouble(i + 1, column2[k]);
//                    }
//                    break;
//                }
//            }
//        }
//        return d;
//    }

    public static double[][] getBashArray(ImportExcel importExcel,String[] sts,int column1,int[] column2){
        int lastRow = importExcel.getLastDataRowNum();
        int len = sts.length;
        int len1 = column2.length;
        double[][] d = new double[len][len1];
        for(int j=0;j<len;j++){
            for(int i=0;i<lastRow;i++){
                String type = importExcel.getValue(i+1,column1);
                if(type.equals("")){
                    continue;
                }
                if(!type.contains(" ")){
                    continue;
                }
                int in = type.indexOf(" ");
                String newstr = type.substring(in,type.length()).trim();
                if(newstr.equals(sts[j])){
                    for(int k=0;k<column2.length;k++) {
                        d[j][k] = importExcel.getDouble(i + 1, column2[k]);
                    }
                    break;
                }
            }
        }
        return d;
    }

    /**
     * 获取角标
     * @param importExcel
     * @param st
     * @param column1
     * @return
     */
    public static String getIndex(ImportExcel importExcel,String st,int column1){
        int lastRow = importExcel.getLastDataRowNum();
        String str = "";
        for(int i=0;i<lastRow;i++){
            String type = importExcel.getValue(i+1,column1);
            if(type.equals("")){
                continue;
            }
            if(type.contains(st)){
                str = type.substring(0,1)+".";
                break;
            }
        }
        return str;
    }



    /**
     * 读取税率
     * 代码逻辑：读取“：”与“%”之间的值 除以100
     */
    public static double getTexRate(ImportExcel importExcel){
        double rate = 0.00;
        int lastRow = importExcel.getLastDataRowNum();
        for(int i=2;i<lastRow;i++){
            String st = importExcel.getValue(i+1,1);
            if(st.contains("税金")){
                int a = st.indexOf(":");
                int a1 = st.indexOf("%");
                String st1 = st.substring(a+2,a1);
                rate = Double.valueOf(st1)/100;
                break;
            }
        }
        return rate;
    }

    /**
     * 读取税金
     * 代码逻辑：读取第一个包含税金的行的值
     */
    public static double getTex(ImportExcel importExcel){
        double rate = 0.00;
        int lastRow = importExcel.getLastDataRowNum();
        for(int i=2;i<lastRow;i++){
            String st = importExcel.getValue(i+1,1);
            if(st.contains("税金")){
                rate = importExcel.getDouble(i+1,2);
                break;
            }
        }
        return rate;
    }

    /**
     * 读取发包人采购设备费
     */
    public static double getFbr(ImportExcel importExcel){
        double rate = 0.00;
        int lastRow = importExcel.getLastDataRowNum();
        for(int i=2;i<lastRow;i++){
            String st = importExcel.getValue(i+1,1);
            if(st.contains("发包人采购") || st.contains("招标人采购")){
                rate = importExcel.getDouble(i+1,2);
                break;
            }
        }
        return rate;
    }


    /**
     * 读取人工调差系数
     */
    public static double getRgRate(ImportExcel importExcel){
        double rate = 0.00;
        int lastRow = importExcel.getLastDataRowNum();
        for(int i=2;i<lastRow;i++){
            String st = importExcel.getValue(i+1,1);
            if(st.contains("%")){
                int a = st.indexOf("(");
                int a1 = st.indexOf("%");
                String st1 = st.substring(a+1,a1);
                rate = Double.valueOf(st1)/100;
                break;
            }
        }
        return rate;
    }


}
