package com.tax.file.utils.income;

import com.tax.file.model.FIncomeInvoice;
import com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.ExcelReadUtil;
import com.yxcheng.token.utils.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class IncomeFileExcelUtil extends ExcelReadUtil {

    public static List<FIncomeInvoice> readInvoice(InputStream inputStream) throws Exception {
        Workbook workbook = getWorkBook(inputStream);
        List<FIncomeInvoice> list = new ArrayList<>();
        if(workbook != null){
            for(int sheetNum = 0;sheetNum < workbook.getNumberOfSheets();sheetNum++){
                //获得当前sheet工作表
                Sheet sheet = workbook.getSheetAt(sheetNum);
                if(sheet == null){
                    continue;
                }
                if(sheetNum != 1)continue;

                //获得当前sheet的开始行
                int firstRowNum  = sheet.getFirstRowNum();
                //获得当前sheet的结束行
                int lastRowNum = sheet.getLastRowNum();
                //循环除了第一行的所有行
                for(int rowNum = firstRowNum+3;rowNum <= lastRowNum;rowNum++){
                    //获得当前行
                    Row row = sheet.getRow(rowNum);
                    if(row == null){
                        break;
                    }
                    list.add(getInvoice(row));
                }
            }

        }
        return list;
    }

    private static FIncomeInvoice getInvoice(Row row) throws Exception {
        FIncomeInvoice entity = new FIncomeInvoice();
        //获得当前行的开始列
        int firstCellNum = row.getFirstCellNum();
        //获得当前行的列数
        int lastCellNum = row.getPhysicalNumberOfCells();
        //循环当前行
        for(int cellNum = firstCellNum ; cellNum < lastCellNum;cellNum++){
            Cell cell = row.getCell(cellNum);
            String value = getCellValue(cell);
            getData(entity,cellNum,value);
        }
        return entity;
    }

    private static void getData(FIncomeInvoice entity, int cellNum, String value) throws Exception {
        switch (cellNum){
            case 0:
                entity.setInvKind(value);
                break;
            case 1:
                entity.setInvNum(value);
                break;
            case 2:
                if(StringUtil.isNotEmpty(value)) {
                    entity.setInvDate(new SimpleDateFormat("yyyy-MM-dd").parse(value));
                }
                break;
            case 3:
                entity.setIncomeMonth(value);
                break;
            case 4:
                entity.setResult(value);
                break;
            case 5:
                if(StringUtil.isNotEmpty(value)) {
                    entity.setProcessDate(new SimpleDateFormat("yyyy-MM-dd").parse(value));
                }
                break;
        }
    }



    public static void generateExcel(List<FIncomeInvoice>list,String path) throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        setTitle(sheet);
        if(list!=null && list.size()>0){
            int num = 1;
            for(int i=0;i<list.size();i++){
                FIncomeInvoice entity = list.get(i);
                setData(sheet,entity,num);
                num++;
            }
        }

        FileOutputStream fileOut = new FileOutputStream(path);
        wb.write(fileOut);
    }

    private static void setData(HSSFSheet sheet, FIncomeInvoice entity, int num) {
        HSSFRow row0 = sheet.createRow(num);
        HSSFCell cell0 = row0.createCell(0);   //生成第一行的第一列
        HSSFCell cell1 = row0.createCell(1);   //生成第一行的第二列
        HSSFCell cell2 = row0.createCell(2);   //生成第一行的第三列
        HSSFCell cell3 = row0.createCell(3);   //生成第一行的第四列
        HSSFCell cell4 = row0.createCell(4);   //生成第一行的第四列

        cell0.setCellValue(entity.getInvKind());
        cell1.setCellValue(entity.getInvNum());
        if(entity.getInvDate()!=null){
            cell2.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(entity.getInvDate()));
        }
        cell3.setCellValue("1".equals(entity.getIsIncome()) ? "是":"否");
        cell4.setCellValue(entity.getIncomeMonth());
    }

    private static void setTitle(HSSFSheet sheet) {
        HSSFRow row0 = sheet.createRow(0);
        //4.生成列
        HSSFCell cell0 = row0.createCell(0);   //生成第一行的第一列
        HSSFCell cell1 = row0.createCell(1);   //生成第一行的第二列
        HSSFCell cell2 = row0.createCell(2);   //生成第一行的第三列
        HSSFCell cell3 = row0.createCell(3);   //生成第一行的第四列
        HSSFCell cell4 = row0.createCell(4);   //生成第一行的第四列

        cell0.setCellValue("发票代码");
        cell1.setCellValue("发票号码");
        cell2.setCellValue("开票日期（格式需为yyyy-MM-dd的文本格式）");
        cell3.setCellValue("是否入账（是/否）");
        cell4.setCellValue("入账所属期（格式需为yyyyMM的文本格式）");
    }

    public static void main(String[] args) throws Exception {
//        String filePath = "/Users/wenhao/tmp/123455/20200525/9999/14/4/72ece956-09e4-4a74-a0e4-01c9f6e8db7c.xls";
//        FileInputStream fis = new FileInputStream(new File(filePath));
//        List<FIncomeInvoice> fIncomeInvoices = IncomeFileExcelUtil.readInvoice(fis);
//        System.out.println(fIncomeInvoices);

        List<FIncomeInvoice>list = new ArrayList<>();
        FIncomeInvoice entity = new FIncomeInvoice();
        entity.setInvKind("041001800104");
        entity.setInvNum("86504132");
        entity.setInvDate(DateUtils.getSearchDate("2019-12-12"));
        entity.setIsIncome("1");
        entity.setIncomeMonth("202001");
        list.add(entity);

        IncomeFileExcelUtil.generateExcel(list,"/Users/wenhao/tmp/123455/20200525/9999/14/4/abc.xls");


    }
}
