package cn.com.countrygarden.bi.main.handler.wcx;

import cn.com.countrygarden.bi.main.dto.wcx.WcxReportDto;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import java.io.OutputStream;
import java.util.List;

/**
 * Created by ADMS on 2018/7/9.
 */
@Component
public class WcxReportProjectExportHandle extends Export<WcxReportDto>{

    /**
     * 创建表头
     *
     * @param workbook
     * @param sheet
     */
    public void createHeader(Workbook workbook, Sheet sheet) {
        int rowIndex = 0;
        CellStyle headerStyle = createHeaderStyle(workbook);
        Row row = sheet.createRow(rowIndex);
        row.setHeight((short) (250 * 2));
        createCell("区域", headerStyle, row, 0, false);
        createCell("项目", headerStyle, row, 1, false);
        createCell("实际注册人数", headerStyle, row, 2, false);
        createCell("提报创新数量", headerStyle, row, 3, false);
        createCell("获得点赞数量", headerStyle, row, 4, false);
    }

    /**
     * 创建表头
     *
     * @param data
     * @param workbook
     * @param sheet
     */
    public void createBody(List<WcxReportDto> data, Workbook workbook, Sheet sheet) {
        CellStyle bodyStyle = createBodyStyle(workbook);
        int rowIndex = 1;

        for (WcxReportDto tem : data) {
            Row row = sheet.createRow(rowIndex);
            createCell(tem.getRegionName() + "", bodyStyle, row, 0, false);
            createCell(tem.getProjectName() + "", bodyStyle, row, 1, false);

            Integer regNum = tem.getRegNum();
            createCell(regNum == null ? ""  : regNum + "", bodyStyle, row, 2, false);

            Integer innNum = tem.getInnNum();
            createCell(innNum  == null ? ""  : innNum + "", bodyStyle, row, 3, false);

            Integer thumbNum = tem.getThumbNum();
            createCell(thumbNum == null ? ""  : thumbNum + "", bodyStyle, row, 4, false);
            rowIndex++;
        }
    }

    public int exportItem(List<WcxReportDto> data, String prizeTitle, OutputStream outputStream) {
        // 表头
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        createHeader(workbook, sheet);
        // 表身
        if (data != null && data.size() > 0) {
           /* String prizeId = data.get(0).getPrizeId();
            // 获取最新批次号
            Integer batchCode = wcxPrizeService.findBatchCodeMax();
            if (batchCode == null) {
                batchCode = 1;
            } else {
                batchCode = batchCode + 1;
            }

            wcxPrizeService.updateForExport(batchCode, prizeTitle, prizeId);

            for (WcxPrizeDto ele : data) {
                ele.setPrizeTitle(prizeTitle);
                ele.setBatchCode(batchCode);
            }*/
            createBody(data, workbook, sheet);
        }

        try {
            workbook.write(outputStream);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                workbook.close();
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return data != null ? data.size() : 0;
    }
}
