package com.sy.coinage.core.util;

import com.alibaba.fastjson2.annotation.JSONField;
import com.sy.coinage.core.annotation.Conver;
import com.sy.coinage.workshop.model.WorkshopAccount;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.ReflectionUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * excel导出通用工具类
 *
 * @author lug
 */
public class ExcelExportUtil<T> {

    public String exportData(String[] codes, String[] names, List<T> list, String fileName,
                             String defPath, String defUrl) {
        String resp = null;
        File file = FileUtil.createFile(defPath, defUrl, fileName);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFCellStyle cellStyleHead = FileUtil.cellStyleHead(wb);
            XSSFCellStyle style = FileUtil.cellStyle(wb);
            XSSFSheet sheet = wb.createSheet("sheet1");
            // 写出表头
            XSSFRow row0 = sheet.createRow(0);
            for (int i = 0; i < codes.length + 1; i++) {
                XSSFCell cell_0 = row0.createCell(i);
                cell_0.setCellStyle(cellStyleHead);
                if (i == 0) {
                    cell_0.setCellValue("序号");
                } else {
                    cell_0.setCellValue(codes[i - 1]);
                }
            }
            // 写出内容
            if (list != null && list.size() > 0) {
                Map<String, Map<String, String>> m = new ConcurrentHashMap<>();
                for (int i = 0; i < list.size(); i++) {
                    XSSFRow row = sheet.createRow(i + 1);
                    for (int j = 0; j < names.length + 1; j++) {
                        XSSFCell cell_0 = row.createCell(j);
                        cell_0.setCellStyle(style);
                        if (j == 0) {
                            cell_0.setCellValue(i + 1);
                        } else {
                            Field f = list.get(i).getClass().getDeclaredField(names[j - 1]);
                            if (f != null) {
                                Conver ant = f.getAnnotation(Conver.class);
                                if (ant != null && ant.keys() != null && ant.values() != null) {
                                    if (!m.containsKey(names[j - 1])) {
                                        Map<String, String> mm = new ConcurrentHashMap<>();
                                        for (int k = 0; k < ant.keys().length; k++) {
                                            mm.put(ant.keys()[k], ant.values()[k]);
                                        }
                                        m.put(names[j - 1], mm);
                                    }
                                }
                            }
                            String val = this.getValue(list.get(i), names[j - 1], m);
                            cell_0.setCellValue(val);
                        }
                    }
                }
            }
            wb.write(fos);
            wb.close();
            resp = defUrl + "/" + fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    /**
     * -根据反射获取值
     *
     * @param t
     * @param name
     * @param m
     * @return
     * @throws Exception
     */
    private String getValue(T t, String name, Map<String, Map<String, String>> m) throws Exception {
        String getMethodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
        Method getMethod = t.getClass().getDeclaredMethod(getMethodName, new Class[]{});
        Object value = getMethod.invoke(t, new Object[]{});
        String result = "";
        if (value != null) {
            if (value instanceof Boolean) {
                boolean bValue = (Boolean) value;
                if (bValue) {
                    result = "是";
                } else {
                    result = "否";
                }
            } else if (value instanceof Date) {
                SimpleDateFormat sdf = null;
                Field f = t.getClass().getDeclaredField(name);
                if (f != null) {
                    JSONField ant = f.getAnnotation(JSONField.class);
                    if (ant != null && ant.format() != null) {
                        sdf = new SimpleDateFormat(ant.format());
                    } else {
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                    }
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                }
                Date date = (Date) value;
                result = sdf.format(date);
            } else {
                if (m.containsKey(name)) {
                    result = m.get(name).get(String.valueOf(value));
                } else {
                    result = String.valueOf(value);
                }
            }
        }
        return result;
    }

    public String exportTempLateData(String[] codes, String[] names, List<T> list, String fileName,
                                     String defPath, String defUrl, int index, XSSFWorkbook wb, XSSFSheet sheet) {
        String resp = null;
        File file = FileUtil.createFile(defPath, defUrl, fileName);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            XSSFCellStyle style = FileUtil.cellStyle(wb);
            // 写出内容
            if (list != null && list.size() > 0) {
                Map<String, Map<String, String>> m = new ConcurrentHashMap<>();
                for (int i = 0; i < list.size(); i++) {
                    XSSFRow row = sheet.createRow(i + index);
                    for (int j = 0; j < names.length + 1; j++) {
                        XSSFCell cell_0 = row.createCell(j);
                        cell_0.setCellStyle(style);
                        if (j == 0) {
                            cell_0.setCellValue(i + 1);
                        } else {
                            Field f = list.get(i).getClass().getDeclaredField(names[j - 1]);
                            if (f != null) {
                                Conver ant = f.getAnnotation(Conver.class);
                                if (ant != null && ant.keys() != null && ant.values() != null) {
                                    if (!m.containsKey(names[j - 1])) {
                                        Map<String, String> mm = new ConcurrentHashMap<>();
                                        for (int k = 0; k < ant.keys().length; k++) {
                                            mm.put(ant.keys()[k], ant.values()[k]);
                                        }
                                        m.put(names[j - 1], mm);
                                    }
                                }
                            }
                            String val = this.getValue(list.get(i), names[j - 1], m);
                            cell_0.setCellValue(val);
                        }
                    }
                }
            }
            wb.write(fos);
            wb.close();
            resp = defUrl + "/" + fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    /**
     * 实物收发卡导出
     *
     * @param title
     * @param securities
     * @param address
     * @param names
     * @param list
     * @param fileName
     * @param defPath
     * @param defUrl
     * @return
     */
    public String exportWare(String title, String securities, String address, String[] names, List<T> list, String fileName, String defPath, String defUrl) {
        String resp = null;
        File file = FileUtil.createFile(defPath, defUrl, fileName);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFCellStyle cellStyleHead = FileUtil.cellStyleHead(wb);
            XSSFCellStyle style = FileUtil.cellStyle(wb);
            XSSFCellStyle styleRed = FileUtil.cellStyleRed(wb);
            XSSFCellStyle styleTitle = FileUtil.cellStyleTitle(wb, 16);
            XSSFSheet sheet = wb.createSheet("sheet1");
            // 写出标题
            XSSFRow row0 = sheet.createRow(0);
            XSSFCell cell_00 = row0.createCell(0);
            cell_00.setCellStyle(styleTitle);
            cell_00.setCellValue(title);
            // 写出品名
            XSSFRow row2 = sheet.createRow(2);
            XSSFCell cell_20 = row2.createCell(0);
            cell_20.setCellValue("品名： " + securities);
//            XSSFCell cell_22 = row2.createCell(2);
//            cell_22.setCellValue(securities);
            // 写出位置
            XSSFRow row3 = sheet.createRow(3);
            XSSFCell cell_30 = row3.createCell(0);
            cell_30.setCellValue("位置： " + address);
//            XSSFCell cell_32 = row3.createCell(2);
//            cell_32.setCellValue(address);
            // 写出计量单位
            XSSFRow row4 = sheet.createRow(4);
            XSSFCell cell_38 = row4.createCell(8);
            cell_38.setCellValue("计量单位：枚");
            // 写出列头
            XSSFRow row5 = sheet.createRow(5);
            for (int i = 0; i < names.length; i++) {
                XSSFCell cell_0 = row5.createCell(i);
                cell_0.setCellStyle(cellStyleHead);
                cell_0.setCellValue(names[i]);
            }
            Integer income = 0;
            Integer pay = 0;
            Integer inventory = 0;
            // 写出内容
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    WorkshopAccount t = (WorkshopAccount) list.get(i);
                    if (i == list.size() - 1) {
                        inventory = t.getInventory();
                    }
                    XSSFRow row = sheet.createRow(i + 6);
                    XSSFCell cell_0 = row.createCell(0);
                    cell_0.setCellStyle(style);
                    cell_0.setCellValue(i + 1);
                    XSSFCell cell_1 = row.createCell(1);
                    cell_1.setCellStyle(style);
                    cell_1.setCellValue(DateUtils.toString(t.getCreateTime(), DateUtils.YY_MM_DD_HH_mm_ss));
                    XSSFCell cell_2 = row.createCell(2);
                    if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                        cell_2.setCellStyle(style);
                    } else {
                        cell_2.setCellStyle(styleRed);
                    }
                    cell_2.setCellValue(t.getDigest());

                    XSSFCell cell_3 = row.createCell(3);
                    if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                        cell_3.setCellStyle(style);
                        income = income + (t.getIncomeQty() == null ? 0 : t.getIncomeQty());
                        if (t.getIncomeQty() != null && t.getIncomeQty() > 0) {
                            cell_3.setCellValue(t.getIncomeQty());
                        }

                    } else {
                        cell_3.setCellStyle(styleRed);
                        income = income - (t.getPayQty() == null ? 0 : t.getPayQty());
                        if (t.getPayQty() != null && t.getPayQty() > 0) {
                            cell_3.setCellValue(t.getPayQty());
                        }
                    }
                    XSSFCell cell_4 = row.createCell(4);
                    cell_4.setCellStyle(style);
                    if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                        pay = pay + (t.getPayQty() == null ? 0 : t.getPayQty());
                        if (t.getPayQty() != null && t.getPayQty() > 0) {
                            cell_4.setCellValue(t.getPayQty());
                        }
                    }
                    XSSFCell cell_5 = row.createCell(5);
                    cell_5.setCellStyle(style);
                    XSSFCell cell_6 = row.createCell(6);
                    cell_6.setCellStyle(style);
                    XSSFCell cell_7 = row.createCell(7);
                    cell_7.setCellStyle(style);
                    if (t.getInventory() != null) {
                        cell_7.setCellValue(t.getInventory());
                    }
                    XSSFCell cell_8 = row.createCell(8);
                    cell_8.setCellStyle(style);
                    String name = t.getOperator();
                    if (StringUtils.isNotBlank(t.getChecker())) {
                        name = name + "," + t.getChecker();
                    }
                    cell_8.setCellValue(name);
                }
            }

            // 写出合计
            XSSFRow rowCount = sheet.createRow(list.size() + 6);
            XSSFCell cell_count00 = rowCount.createCell(0);
            cell_count00.setCellStyle(style);
            cell_count00.setCellValue("合计");
            XSSFCell cell_count01 = rowCount.createCell(1);
            cell_count01.setCellStyle(style);
            XSSFCell cell_count02 = rowCount.createCell(2);
            cell_count02.setCellStyle(style);
            XSSFCell cell_count03 = rowCount.createCell(3);
            cell_count03.setCellStyle(style);
            if (income.intValue() != 0) {
                cell_count03.setCellValue(income);
            } else {
                cell_count03.setCellValue("-");
            }
            XSSFCell cell_count04 = rowCount.createCell(4);
            cell_count04.setCellStyle(style);
            if (pay.intValue() != 0) {
                cell_count04.setCellValue(pay);
            } else {
                cell_count04.setCellValue("-");
            }
            XSSFCell cell_count05 = rowCount.createCell(5);
            cell_count05.setCellStyle(style);
            XSSFCell cell_count06 = rowCount.createCell(6);
            cell_count06.setCellStyle(style);
            XSSFCell cell_count07 = rowCount.createCell(7);
            cell_count07.setCellStyle(style);
            cell_count07.setCellValue(inventory);
            XSSFCell cell_count08 = rowCount.createCell(8);
            cell_count08.setCellStyle(style);
            wb.write(fos);
            wb.close();
            resp = defUrl + "/" + fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    public String exportShop(Integer type, String title, String securities, String address, String[] names, List<T> list, String fileName, String defPath, String defUrl) {
        String resp = null;
        File file = FileUtil.createFile(defPath, defUrl, fileName);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFCellStyle cellStyleHead = FileUtil.cellStyleHead(wb);
            XSSFCellStyle style = FileUtil.cellStyle(wb);
            XSSFCellStyle styleRed = FileUtil.cellStyleRed(wb);
            XSSFCellStyle styleTitle = FileUtil.cellStyleTitle(wb, 16);
            XSSFSheet sheet = wb.createSheet("sheet1");
            // 写出标题
            XSSFRow row0 = sheet.createRow(0);
            XSSFCell cell_00 = row0.createCell(0);
            cell_00.setCellStyle(styleTitle);
            cell_00.setCellValue(title);
            // 写出品名
            XSSFRow row2 = sheet.createRow(2);
            XSSFCell cell_20 = row2.createCell(0);
            cell_20.setCellValue("品名： " + securities);
//            XSSFCell cell_22 = row2.createCell(2);
//            cell_22.setCellValue(securities);
            // 写出位置
            XSSFRow row3 = sheet.createRow(3);
            XSSFCell cell_30 = row3.createCell(0);
            cell_30.setCellValue("位置： " + address);
//            XSSFCell cell_32 = row3.createCell(2);
//            cell_32.setCellValue(address);
            // 写出计量单位
            XSSFRow row4 = sheet.createRow(4);
            XSSFCell cell_38 = row4.createCell(8);
            cell_38.setCellValue("计量单位：枚");
            // 写出列头
            XSSFRow row5 = sheet.createRow(5);
            for (int i = 0; i < names.length; i++) {
                XSSFCell cell_0 = row5.createCell(i);
                cell_0.setCellStyle(cellStyleHead);
                cell_0.setCellValue(names[i]);
            }
            switch (type) {
                case 2:
                    //"封箱成品实物收发卡"
                    this.shop2(list, style, styleRed, sheet);
                    break;
                case 3:
                    //"剔除品实物收发卡";
                    this.shop3(list, style, styleRed, sheet);
                    break;
            }
            wb.write(fos);
            wb.close();
            resp = defUrl + "/" + fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    public String exportAccount(Integer type, String title, String securities, String address, String[] names, List<T> list, String fileName, String defPath, String defUrl) {
        String resp = null;
        File file = FileUtil.createFile(defPath, defUrl, fileName);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFCellStyle cellStyleHead = FileUtil.cellStyleHead(wb);
            XSSFCellStyle style = FileUtil.cellStyle(wb);
            XSSFCellStyle styleRed = FileUtil.cellStyleRed(wb);
            XSSFCellStyle styleTitle = FileUtil.cellStyleTitle(wb, 16);
            XSSFSheet sheet = wb.createSheet("sheet1");
            // 写出标题
            XSSFRow row0 = sheet.createRow(0);
            XSSFCell cell_00 = row0.createCell(0);
            cell_00.setCellStyle(styleTitle);
            cell_00.setCellValue(title);
            // 写出品名
            XSSFRow row2 = sheet.createRow(2);
            XSSFCell cell_20 = row2.createCell(0);
            cell_20.setCellValue("品名： " + securities);
//            XSSFCell cell_22 = row2.createCell(2);
//            cell_22.setCellValue(securities);
            // 写出位置
            XSSFRow row3 = sheet.createRow(3);
            XSSFCell cell_30 = row3.createCell(0);
            cell_30.setCellValue("位置： " + address);
//            XSSFCell cell_32 = row3.createCell(2);
//            cell_32.setCellValue(address);
            // 写出计量单位
            XSSFRow row4 = sheet.createRow(4);
            XSSFCell cell_38 = row4.createCell(7);
            cell_38.setCellValue("计量单位：枚");
            // 写出列头
            XSSFRow row5 = sheet.createRow(5);
            for (int i = 0; i < names.length; i++) {
                XSSFCell cell_0 = row5.createCell(i);
                cell_0.setCellStyle(cellStyleHead);
                cell_0.setCellValue(names[i]);
            }
            switch (type) {
                case 4:
                    //"印花成品出入库台账";
                    this.shop4(list, style, styleRed, sheet);
                    break;
                case 5:
                    //"封箱成品出入库台账";
                    this.shop5(list, style, styleRed, sheet);
                    break;
            }
            wb.write(fos);
            wb.close();
            resp = defUrl + "/" + fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    private void shop2(List<T> list, XSSFCellStyle style, XSSFCellStyle styleRed, XSSFSheet sheet) {
        Integer innerIncome = 0;
        Integer pay = 0;
        Integer inventory = 0;
        // 写出内容
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                WorkshopAccount t = (WorkshopAccount) list.get(i);
                if (i == list.size() - 1) {
                    inventory = t.getInventory();
                }
                XSSFRow row = sheet.createRow(i + 6);
                XSSFCell cell_0 = row.createCell(0);
                cell_0.setCellStyle(style);
                cell_0.setCellValue(i + 1);
                XSSFCell cell_1 = row.createCell(1);
                cell_1.setCellStyle(style);
                cell_1.setCellValue(DateUtils.toString(t.getCreateTime(), DateUtils.YY_MM_DD_HH_mm_ss));
                // 摘要
                XSSFCell cell_2 = row.createCell(2);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_2.setCellStyle(style);
                } else {
                    cell_2.setCellStyle(styleRed);
                }
                cell_2.setCellValue(t.getDigest());
                // 收入
                XSSFCell cell_3 = row.createCell(3);
                cell_3.setCellStyle(style);
                // 付出
                XSSFCell cell_4 = row.createCell(4);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_4.setCellStyle(style);
                    pay = pay + (t.getPayQty() == null ? 0 : t.getPayQty());
                    if (t.getPayQty() != null && t.getPayQty() > 0) {
                        cell_4.setCellValue(t.getPayQty());
                    }
                } else {
                    cell_4.setCellStyle(styleRed);
                    // 红冲是总库
                    if (t.getLocFrom().intValue() == 1) {
                        pay = pay - (t.getIncomeQty() == null ? 0 : t.getIncomeQty());
                        if (t.getIncomeQty() != null && t.getIncomeQty() > 0) {
                            cell_4.setCellValue(t.getIncomeQty());
                        }
                    }
                }
                // 内部收入
                XSSFCell cell_5 = row.createCell(5);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_5.setCellStyle(style);
                    innerIncome = innerIncome + (t.getInnerIncomeQty() == null ? 0 : t.getInnerIncomeQty());
                    if (t.getInnerIncomeQty() != null && t.getInnerIncomeQty() > 0) {
                        cell_5.setCellValue(t.getInnerIncomeQty());
                    }
                } else {
                    cell_5.setCellStyle(styleRed);
                    // 红冲非总库
                    if (t.getLocFrom().intValue() != 1) {
                        innerIncome = innerIncome - (t.getInnerPayQty() == null ? 0 : t.getInnerPayQty());
                        if (t.getInnerPayQty() != null && t.getInnerPayQty() > 0) {
                            cell_5.setCellValue(t.getInnerPayQty());
                        }
                    }
                }
                // 内部付出
                XSSFCell cell_6 = row.createCell(6);
                cell_6.setCellStyle(style);
                XSSFCell cell_7 = row.createCell(7);
                cell_7.setCellStyle(style);
                if (t.getInventory() != null) {
                    cell_7.setCellValue(t.getInventory());
                }
                XSSFCell cell_8 = row.createCell(8);
                cell_8.setCellStyle(style);
                String name = t.getOperator();
                if (StringUtils.isNotBlank(t.getChecker())) {
                    name = name + "," + t.getChecker();
                }
                cell_8.setCellValue(name);
            }
        }

        // 写出合计
        XSSFRow rowCount = sheet.createRow(list.size() + 6);
        XSSFCell cell_count00 = rowCount.createCell(0);
        cell_count00.setCellStyle(style);
        cell_count00.setCellValue("合计");
        XSSFCell cell_count01 = rowCount.createCell(1);
        cell_count01.setCellStyle(style);
        XSSFCell cell_count02 = rowCount.createCell(2);
        cell_count02.setCellStyle(style);
        XSSFCell cell_count03 = rowCount.createCell(3);
        cell_count03.setCellStyle(style);
        XSSFCell cell_count04 = rowCount.createCell(4);
        cell_count04.setCellStyle(style);
        if (pay.intValue() != 0) {
            cell_count04.setCellValue(pay);
        } else {
            cell_count04.setCellValue("-");
        }
        XSSFCell cell_count05 = rowCount.createCell(5);
        cell_count05.setCellStyle(style);
        if (innerIncome.intValue() != 0) {
            cell_count05.setCellValue(innerIncome);
        } else {
            cell_count05.setCellValue("-");
        }
        XSSFCell cell_count06 = rowCount.createCell(6);
        cell_count06.setCellStyle(style);
        XSSFCell cell_count07 = rowCount.createCell(7);
        cell_count07.setCellStyle(style);
        cell_count07.setCellValue(inventory);
        XSSFCell cell_count08 = rowCount.createCell(8);
        cell_count08.setCellStyle(style);
    }

    private void shop3(List<T> list, XSSFCellStyle style, XSSFCellStyle styleRed, XSSFSheet sheet) {
        Integer innerIncome = 0;
        Integer innerPay = 0;
        Integer inventory = 0;
        // 写出内容
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                WorkshopAccount t = (WorkshopAccount) list.get(i);
                if (i == list.size() - 1) {
                    inventory = t.getInventory();
                }
                XSSFRow row = sheet.createRow(i + 6);
                XSSFCell cell_0 = row.createCell(0);
                cell_0.setCellStyle(style);
                cell_0.setCellValue(i + 1);
                XSSFCell cell_1 = row.createCell(1);
                cell_1.setCellStyle(style);
                cell_1.setCellValue(DateUtils.toString(t.getCreateTime(), DateUtils.YY_MM_DD_HH_mm_ss));
                // 摘要
                XSSFCell cell_2 = row.createCell(2);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_2.setCellStyle(style);
                } else {
                    cell_2.setCellStyle(styleRed);
                }
                cell_2.setCellValue(t.getDigest());
                // 收入
                XSSFCell cell_3 = row.createCell(3);
                cell_3.setCellStyle(style);
                // 付出
                XSSFCell cell_4 = row.createCell(4);
                cell_4.setCellStyle(style);
                // 内部收入
                XSSFCell cell_5 = row.createCell(5);
                cell_5.setCellStyle(style);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    innerIncome = innerIncome + (t.getInnerIncomeQty() == null ? 0 : t.getInnerIncomeQty());
                    if (t.getInnerIncomeQty() != null && t.getInnerIncomeQty() > 0) {
                        cell_5.setCellValue(t.getInnerIncomeQty());
                    }
                }
                // 内部付出
                XSSFCell cell_6 = row.createCell(6);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_6.setCellStyle(style);
                    innerPay = innerPay + (t.getInnerPayQty() == null ? 0 : t.getInnerPayQty());
                    if (t.getInnerPayQty() != null && t.getInnerPayQty() > 0) {
                        cell_6.setCellValue(t.getInnerPayQty());
                    }
                } else {
                    cell_6.setCellStyle(styleRed);
                    innerPay = innerPay - (t.getInnerIncomeQty() == null ? 0 : t.getInnerIncomeQty());
                    if (t.getInnerIncomeQty() != null && t.getInnerIncomeQty() > 0) {
                        cell_6.setCellValue(t.getInnerIncomeQty());
                    }
                }
                XSSFCell cell_7 = row.createCell(7);
                cell_7.setCellStyle(style);
                if (t.getInventory() != null) {
                    cell_7.setCellValue(t.getInventory());
                }
                XSSFCell cell_8 = row.createCell(8);
                cell_8.setCellStyle(style);
                String name = t.getOperator();
                if (StringUtils.isNotBlank(t.getChecker())) {
                    name = name + "," + t.getChecker();
                }
                cell_8.setCellValue(name);
            }
        }
        // 写出合计
        XSSFRow rowCount = sheet.createRow(list.size() + 6);
        XSSFCell cell_count00 = rowCount.createCell(0);
        cell_count00.setCellStyle(style);
        cell_count00.setCellValue("合计");
        XSSFCell cell_count01 = rowCount.createCell(1);
        cell_count01.setCellStyle(style);
        XSSFCell cell_count02 = rowCount.createCell(2);
        cell_count02.setCellStyle(style);
        XSSFCell cell_count03 = rowCount.createCell(3);
        cell_count03.setCellStyle(style);
        XSSFCell cell_count04 = rowCount.createCell(4);
        cell_count04.setCellStyle(style);
        XSSFCell cell_count05 = rowCount.createCell(5);
        cell_count05.setCellStyle(style);
        if (innerIncome.intValue() != 0) {
            cell_count05.setCellValue(innerIncome);
        } else {
            cell_count05.setCellValue("-");
        }
        XSSFCell cell_count06 = rowCount.createCell(6);
        cell_count06.setCellStyle(style);
        if (innerPay.intValue() != 0) {
            cell_count06.setCellValue(innerPay);
        } else {
            cell_count06.setCellValue("-");
        }
        XSSFCell cell_count07 = rowCount.createCell(7);
        cell_count07.setCellStyle(style);
        cell_count07.setCellValue(inventory);
        XSSFCell cell_count08 = rowCount.createCell(8);
        cell_count08.setCellStyle(style);
    }

    private void shop4(List<T> list, XSSFCellStyle style, XSSFCellStyle styleRed, XSSFSheet sheet) {
        Integer income = 0;
        Integer pay = 0;
        Integer inventory = 0;
        // 写出内容
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                WorkshopAccount t = (WorkshopAccount) list.get(i);
                if (i == list.size() - 1) {
                    inventory = t.getInventory();
                }
                XSSFRow row = sheet.createRow(i + 6);
                XSSFCell cell_0 = row.createCell(0);
                cell_0.setCellStyle(style);
                cell_0.setCellValue(i + 1);
                XSSFCell cell_1 = row.createCell(1);
                cell_1.setCellStyle(style);
                cell_1.setCellValue(DateUtils.toString(t.getCreateTime()));
                // 凭证编号
                XSSFCell cell_2 = row.createCell(2);
                cell_2.setCellStyle(style);
                cell_2.setCellValue(t.getOrderNo());
                // 摘要
                XSSFCell cell_3 = row.createCell(3);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_3.setCellStyle(style);
                } else {
                    cell_3.setCellStyle(styleRed);
                }
                cell_3.setCellValue(t.getDigest());
                // 收入
                XSSFCell cell_4 = row.createCell(4);
                cell_4.setCellStyle(style);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    income = income + (t.getIncomeQty() == null ? 0 : t.getIncomeQty());
                    if (t.getIncomeQty() != null && t.getIncomeQty() > 0) {
                        cell_4.setCellValue(t.getIncomeQty());
                    }
                }
                // 付出
                XSSFCell cell_5 = row.createCell(5);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_5.setCellStyle(style);
                    pay = pay + (t.getPayQty() == null ? 0 : t.getPayQty());
                    if (t.getPayQty() != null && t.getPayQty() > 0) {
                        cell_5.setCellValue(t.getPayQty());
                    }
                } else {
                    cell_5.setCellStyle(styleRed);
                    pay = pay - (t.getIncomeQty() == null ? 0 : t.getIncomeQty());
                    if (t.getIncomeQty() != null && t.getIncomeQty() > 0) {
                        cell_5.setCellValue(t.getIncomeQty());
                    }
                }
                XSSFCell cell_6 = row.createCell(6);
                cell_6.setCellStyle(style);
                if (t.getInventory() != null) {
                    cell_6.setCellValue(t.getInventory());
                }
                XSSFCell cell_7 = row.createCell(7);
                cell_7.setCellStyle(style);
                String name = t.getOperator();
                if (StringUtils.isNotBlank(t.getChecker())) {
                    name = name + "," + t.getChecker();
                }
                cell_7.setCellValue(name);
            }
        }
        // 写出合计
        XSSFRow rowCount = sheet.createRow(list.size() + 6);
        XSSFCell cell_count00 = rowCount.createCell(0);
        cell_count00.setCellStyle(style);
        cell_count00.setCellValue("合计");
        XSSFCell cell_count01 = rowCount.createCell(1);
        cell_count01.setCellStyle(style);
        XSSFCell cell_count02 = rowCount.createCell(2);
        cell_count02.setCellStyle(style);
        XSSFCell cell_count03 = rowCount.createCell(3);
        cell_count03.setCellStyle(style);
        XSSFCell cell_count04 = rowCount.createCell(4);
        cell_count04.setCellStyle(style);
        cell_count04.setCellValue(income);
        XSSFCell cell_count05 = rowCount.createCell(5);
        cell_count05.setCellStyle(style);
        cell_count05.setCellValue(pay);
        XSSFCell cell_count06 = rowCount.createCell(6);
        cell_count06.setCellStyle(style);
        cell_count06.setCellValue(inventory);
        XSSFCell cell_count07 = rowCount.createCell(7);
        cell_count07.setCellStyle(style);
    }

    private void shop5(List<T> list, XSSFCellStyle style, XSSFCellStyle styleRed, XSSFSheet sheet) {
        Integer income = 0;
        Integer pay = 0;
        Integer inventory = 0;
        // 写出内容
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                WorkshopAccount t = (WorkshopAccount) list.get(i);
                XSSFRow row = sheet.createRow(i + 6);
                XSSFCell cell_0 = row.createCell(0);
                cell_0.setCellStyle(style);
                cell_0.setCellValue(i + 1);
                XSSFCell cell_1 = row.createCell(1);
                cell_1.setCellStyle(style);
                cell_1.setCellValue(DateUtils.toString(t.getCreateTime()));
                // 凭证编号
                XSSFCell cell_2 = row.createCell(2);
                cell_2.setCellStyle(style);
                cell_2.setCellValue(t.getOrderNo());
                // 摘要
                XSSFCell cell_3 = row.createCell(3);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_3.setCellStyle(style);
                } else {
                    cell_3.setCellStyle(styleRed);
                }
                cell_3.setCellValue(t.getDigest());
                // 收入
                XSSFCell cell_4 = row.createCell(4);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_4.setCellStyle(style);
                    income = income + (t.getIncomeQty() == null ? 0 : t.getIncomeQty());
                    if (t.getIncomeQty() != null && t.getIncomeQty() > 0) {
                        cell_4.setCellValue(t.getIncomeQty());
                    }
                } else {
                    cell_4.setCellStyle(styleRed);
                    // 红冲非总库
                    if (t.getLocFrom().intValue() != 1) {
                        income = income - (t.getPayQty() == null ? 0 : t.getPayQty());
                        if (t.getPayQty() != null && t.getPayQty() > 0) {
                            cell_4.setCellValue(t.getPayQty());
                        }
                    }
                }
                // 付出
                XSSFCell cell_5 = row.createCell(5);
                if (t.getRedFlashFlag() != null && t.getRedFlashFlag().intValue() == 1) {
                    cell_5.setCellStyle(style);
                    pay = pay + (t.getPayQty() == null ? 0 : t.getPayQty());
                    if (t.getPayQty() != null && t.getPayQty() > 0) {
                        cell_5.setCellValue(t.getPayQty());
                    }
                } else {
                    cell_5.setCellStyle(styleRed);
                    // 红冲是总库
                    if (t.getLocFrom().intValue() == 1) {
                        pay = pay - (t.getIncomeQty() == null ? 0 : t.getIncomeQty());
                        if (t.getIncomeQty() != null && t.getIncomeQty() > 0) {
                            cell_5.setCellValue(t.getIncomeQty());
                        }
                    }
                }
                XSSFCell cell_6 = row.createCell(6);
                cell_6.setCellStyle(style);
                if (t.getInventory() != null) {
                    cell_6.setCellValue(t.getInventory());
                }
                XSSFCell cell_7 = row.createCell(7);
                cell_7.setCellStyle(style);
                String name = t.getOperator();
                if (StringUtils.isNotBlank(t.getChecker())) {
                    name = name + "," + t.getChecker();
                }
                cell_7.setCellValue(name);
            }
        }
        // 写出合计
        XSSFRow rowCount = sheet.createRow(list.size() + 6);
        XSSFCell cell_count00 = rowCount.createCell(0);
        cell_count00.setCellStyle(style);
        cell_count00.setCellValue("合计");
        XSSFCell cell_count01 = rowCount.createCell(1);
        cell_count01.setCellStyle(style);
        XSSFCell cell_count02 = rowCount.createCell(2);
        cell_count02.setCellStyle(style);
        XSSFCell cell_count03 = rowCount.createCell(3);
        cell_count03.setCellStyle(style);
        XSSFCell cell_count04 = rowCount.createCell(4);
        cell_count04.setCellStyle(style);
        cell_count04.setCellValue(income);
        XSSFCell cell_count05 = rowCount.createCell(5);
        cell_count05.setCellStyle(style);
        cell_count05.setCellValue(pay);
        XSSFCell cell_count06 = rowCount.createCell(6);
        cell_count06.setCellStyle(style);
        cell_count06.setCellValue(inventory);
        XSSFCell cell_count07 = rowCount.createCell(7);
        cell_count07.setCellStyle(style);
    }


}
