package com.chaozhanggui.admin.system.util;

import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;


public class ExcelUtil {

    static List<Demo> list = new ArrayList();

    public static String FormatCode(String code) {
        String[] codeArr = code.split(",");
        List<String> newCode = new ArrayList();
        DecimalFormat format = new DecimalFormat("           ");
        String[] var5 = codeArr;
        int var6 = codeArr.length;

        for (int var7 = 0; var7 < var6; ++var7) {
            String c = var5[var7];
            newCode.add(format.format(Integer.parseInt(c)));
        }

        return StringUtils.join(newCode, ",");
    }

    public static void wirter(String content) throws IOException {
        File fileOrFilename = new File("e://text1.txt");

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new

                FileOutputStream(fileOrFilename, true)));
        out.write(content+ "\n");

        out.close();
    }

    public static void main(String[] args) throws Exception {


//        System.out.println(FormatCode("88"));
        new ExcelUtil().readExcel();
        Map<String, List<Demo>> s = list.stream().sorted(Comparator.comparing(t -> t.getAmount(), Comparator.reverseOrder())).collect(Collectors.groupingBy(t -> t.getPhone(), TreeMap::new, Collectors.toList()));

        for (String key : s.keySet()) {
            List<Demo> demos = s.get(key);
            Double t = 0d;
            Double m=0d;
            Double f=0d;
            for (int i = 0; i < demos.size(); i++) {
                Demo d = demos.get(i);
                System.out.println(

                        d.getUserName().trim() + "\t"
                                + d.getPhone() + "\t"
                                + d.getAmount() + "\t"
                                + d.getRealAmount() + "\t"
                                + d.getFee() + "\t\t\t"
                                + d.getFeel() + "\t"
                                + d.getWay() + "\t"
                                + d.getInfo().replaceAll("\n","") + "\t"
                                + d.getStatus() + "\t"


                );

            wirter(
                    d.getUserName().trim() + "\t"
                                + d.getPhone() + "\t"
                                + String.format("%.2f",d.getAmount() )+ "\t"
                                + String.format("%.2f",d.getRealAmount()) + "\t"
                                + d.getFee() + "\t\t\t"
                                + d.getFeel() + "\t"
                                + d.getWay() + "\t"
                                + d.getInfo().replaceAll("\n","") + "\t"
                                + d.getStatus() + "\t"
            );


                t = t + d.getAmount();
                m=m+d.getRealAmount();
                f=f+d.getRealFee();

            }

                System.out.println("\t\t\t\t\t\t\t\t" + t);

            wirter("\t\t"+String.format("%.2f",t)+"\t"+String.format("%.2f",m)+"\t"+String.format("%.2f",f)+"元");
        }
    }

    public void readExcel() throws Exception {
        //根据文件位置找到需要读取的excel文件
        File Inputfile = new File("C:\\Users\\admin\\Desktop\\新建文件夹\\副本1_提现列表-2023-07-01 224241(1).xls");

        //根据路径生成 FileInputStream字节流
        FileInputStream fileInputStream = new FileInputStream(Inputfile);

        //将FileInputStream转换为Workbook
        Workbook workbook = Workbook.getWorkbook(fileInputStream);

        // 默认获取第一张工作表，可以自定义
        Sheet sheet = workbook.getSheet(0);

        // 循环获取每一行数据 因为默认第一行为标题行，我们可以从 1 开始循环，如果没有标题行，i从 0 开始
        // sheet.getRows() 获取总行数
        for (int i = 1; i < sheet.getRows(); i++) {
            if (i + 1 == sheet.getRows()) {
                return;
            }

            Demo d = new Demo();
            // 获取id
            String id = sheet.getCell(0, i).getContents();
            d.setId(id);
            // 获取订单号
            String orderNo = sheet.getCell(1, i).getContents();
            d.setOrderNo(orderNo);
            //获取名称
            String userName = sheet.getCell(2, i).getContents();
            d.setUserName(userName);

            //获取手机号
            String phone = sheet.getCell(3, i).getContents();
            d.setPhone(phone);

            String amount = sheet.getCell(4, i).getContents();
            d.setAmount(Double.valueOf(amount.substring(0, amount.length() - 1)));

            String realAmount = sheet.getCell(5, i).getContents();
            d.setRealAmount(Double.valueOf(realAmount.substring(0, realAmount.length() - 1)));

            String fee = sheet.getCell(6, i).getContents();
            d.setFee(fee);
            d.setRealFee(Double.valueOf(fee.substring(0, fee.length() - 1)));


            String feel = sheet.getCell(7, i).getContents();
            d.setFeel(feel);

            String way = sheet.getCell(8, i).getContents();
            d.setWay(way);

            String info = sheet.getCell(9, i).getContents();
            d.setInfo(info);

            String status = sheet.getCell(10, i).getContents();
            d.setStatus(status);

            String stime = sheet.getCell(11, i).getContents();
            d.setStime(stime);

            String dealtime = sheet.getCell(12, i).getContents();
            d.setDealtime(dealtime);

            list.add(d);
        }
    }

    class Demo {
        private String id;

        private String orderNo;

        private String userName;

        private String phone;

        private Double amount;

        private Double realAmount;

        private String fee;

        private Double realFee;

        private String feel;

        private String way;

        private String info;

        private String status;

        private String stime;

        private String dealtime;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getOrderNo() {
            return orderNo;
        }

        public void setOrderNo(String orderNo) {
            this.orderNo = orderNo;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public Double getAmount() {
            return amount;
        }

        public void setAmount(Double amount) {
            this.amount = amount;
        }

        public Double getRealAmount() {
            return realAmount;
        }

        public void setRealAmount(Double realAmount) {
            this.realAmount = realAmount;
        }

        public String getFee() {
            return fee;
        }

        public void setFee(String fee) {
            this.fee = fee;
        }

        public Double getRealFee() {
            return realFee;
        }

        public void setRealFee(Double realFee) {
            this.realFee = realFee;
        }

        public String getFeel() {
            return feel;
        }

        public void setFeel(String feel) {
            this.feel = feel;
        }

        public String getWay() {
            return way;
        }

        public void setWay(String way) {
            this.way = way;
        }

        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getStime() {
            return stime;
        }

        public void setStime(String stime) {
            this.stime = stime;
        }

        public String getDealtime() {
            return dealtime;
        }

        public void setDealtime(String dealtime) {
            this.dealtime = dealtime;
        }
    }

}
