package com.jeeplus.common.utils.sg;

import com.jeeplus.common.utils.excel.ImportExcel;

public class ExcelUtil {

    /**
     * 根据字段读取数据
     * @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;
    }


    /**
     * 根据字段读取数据
     * @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;
    }


    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]);
                }
                continue;
            }
        }
        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 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;
    }
}
