package com.coolservlets.beans.method;

import java.text.DecimalFormat;
import java.util.HashMap;

public class DBDataTools {
    /**
     * 按照某几个数据列进行合计，并将合计添加到二维数组中
     * @param data 需要合计的二维数组
     * @param typecolum  需要合计类型的二维数组的下标列编号，其中用"," 分开
     * @param collectcolum 需要合计的二维数组的下标列编号，其中用"," 分开
     * @return String[][]
     * @throws Exception
     */
    static public  String[][] formatCollect(String[][] data,String typecolum,String collectcolum)throws Exception
    {
        String[] aTypecolum= genMethod.split(typecolum, ",");
        String[] aCollectcolum= genMethod.split(collectcolum, ",");
        HashMap hmTotal = new HashMap();
        for(int j=0;j<aCollectcolum.length;j++){
            hmTotal.put(aCollectcolum[j],new Double(0));
        }
        String[][] data1 = null;
        //增加的列数
        int loop = data.length;
        //实际添加的行
        int iAddColums = 0;
        int iAddRows = 0;
        String curstr = "";
        for(int i=0;i<loop;i++){
            iAddColums = data[i].length;
            String tcompstr = "";
            int tloop = aTypecolum.length;
            for(int t=0;t<tloop;t++){
                String ts = data[i][Integer.parseInt(aTypecolum[t])];
                tcompstr = tcompstr + ts +"@@";
            }
            if(!curstr.equalsIgnoreCase(tcompstr)){
                iAddRows++;
                curstr = tcompstr;
            }
        }
        if(iAddRows == 0) return data;
        data1 = new String[loop+iAddRows][iAddColums];
        int it = loop+iAddRows;
        for(int f=0;f<it;f++){
            int yloop = data1[f].length;
            for(int y=0;y<yloop;y++){
                data1[f][y] = "";
            }
        }
        //System.out.println("loop+iAddRows="+it);
        curstr = "";
        int row1 = 0;
        //boolean addIs = false;
        for(int i=0;i<loop;i++){
            String tcompstr = "";
            int tloop = aTypecolum.length;
            for(int t=0;t<tloop;t++){
                String ts = data[i][Integer.parseInt(aTypecolum[t])];
                tcompstr = tcompstr + ts +"@@";
            }
            if(i == 0) curstr = tcompstr;
            if(!curstr.equalsIgnoreCase(tcompstr) && i > 0){
                curstr = tcompstr;
                //小计描述
                data1[row1][Integer.parseInt(aTypecolum[0])] = data[i-1][Integer.parseInt(aTypecolum[0])]+"小计";
                //小计数值赋值
                int floop = aCollectcolum.length;
                for(int f=0;f<floop;f++){
                    String putkey = aCollectcolum[f];
                    Double cost = (Double)hmTotal.get(putkey);
                    //System.out.println("cost.toString()="+cost.toString());
                    data1[row1][Integer.parseInt(putkey)] = cost.toString();
                    //清零
                    hmTotal.put(putkey,new Double(0));
                }
                row1++;
            }
            int kloop = data[i].length;
            for(int k=0;k<kloop;k++){
                data1[row1][k] = data[i][k];
                //合计相加
                int floop = aCollectcolum.length;
                //System.out.println("floop="+floop);
                for(int f=0;f<floop;f++){
                    String putkey = aCollectcolum[f];
                    if(k == Integer.parseInt(putkey)){
                        double totalcost = ((Double)hmTotal.get(putkey)).doubleValue();
                        //System.out.println("totalcost="+totalcost);
                        double curcost = Double.parseDouble(data[i][Integer.parseInt(putkey)]);
                        totalcost = totalcost + curcost;
                        //赋值
                        hmTotal.put(putkey,new Double(totalcost));
                    }
                }
            }
            row1++;
        }
        //System.out.println("row1="+row1);
        data1[row1][Integer.parseInt(aTypecolum[0])] = data[loop-1][Integer.parseInt(aTypecolum[0])]+"小计";
        //小计数值赋值
        int floop = aCollectcolum.length;
        for(int f=0;f<floop;f++){
            String putkey = aCollectcolum[f];
            Double cost = (Double)hmTotal.get(putkey);
            //System.out.println("cost.toString()="+cost.toString());
            data1[row1][Integer.parseInt(putkey)] = cost.toString();
            //清零
            hmTotal.put(putkey,new Double(0));
        }
        return data1;
    }
    /**
     * 将浮点数float 或 double 格式化为带小数点的位数
     * @param str
     * @return
     * @throws Exception
     */
    static public String formatFloat(String str)throws Exception
    {
        double cost = 0.00d;
        DecimalFormat dfb = null;
        try{
            cost = Double.parseDouble(str);
            dfb = new DecimalFormat("#############.##");
        }
        catch (Exception ex) {
            //System.out.println("str="+str+",vendor="+vendor);
            ex.printStackTrace();
        }
        return dfb.format(cost);
    }
}
