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

import cn.com.countrygarden.bi.main.constant.wcx.enumeration.WcxBaselineEnum;
import cn.com.countrygarden.bi.main.constant.wcx.enumeration.WcxInnoTypeEnum;
import cn.com.countrygarden.bi.main.dto.wcx.WcxPrizeDto;
import cn.com.countrygarden.bi.main.service.wcx.WcxPrizeService;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * Created by ADMS on 2018/7/9.
 */
@Component
public class WcxPrizeExportHandle extends Export<WcxPrizeDto>{
    @Autowired
    private WcxPrizeService wcxPrizeService;

    /**
     * 创建表头
     *
     * @param workbook
     * @param sheet
     */
    @Override
    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, 0, false);
        createCell("奖项标题", headerStyle, row, 1, false);
        // 创新
        createCell("获奖创新标题", headerStyle, row, 2, false);
        createCell("获奖创新基础业务线", headerStyle, row, 3, false);
        createCell("获奖创新点赞数", headerStyle, row, 4, false);
        createCell("获奖创新提出人员姓名", headerStyle, row, 5, false);

        createCell("获奖创新类型", headerStyle, row, 6, false);
        createCell("获奖创新团队成员", headerStyle, row, 7, false);
        createCell("获奖创新所属项目", headerStyle, row, 8, false);
        createCell("获奖创新所属区域", headerStyle, row, 9, false);
        // 人员
        createCell("获奖人员姓名", headerStyle, row, 10, false);
        createCell("获奖人员点赞数", headerStyle, row, 11, false);
        createCell("获奖人员创意数", headerStyle, row, 12, false);
        // 项目
        createCell("获奖项目", headerStyle, row, 13, false);
        // 区域
        createCell("获奖区域", headerStyle, row, 14, false);

        //createCell("状态", headerStyle, row, 8, false);
    }

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

        for (WcxPrizeDto tem : data) {
            Row row = sheet.createRow(rowIndex);
            createCell(tem.getBatchCode() + "", bodyStyle, row, 0, false); // 奖项批次号
            createCell(tem.getPrizeTitle(), bodyStyle, row, 1, false); // 奖项标题
            // 创新
            createCell(tem.getInnTitle(), bodyStyle, row, 2, false); // 创新标题
            String innBaseline = tem.getInnBaseline();
            createCell(WcxBaselineEnum.getBaselineName(innBaseline), bodyStyle, row, 3, false); // 创新基线
            Long innThumbNumber = tem.getInnThumbNumber();
            createCell(innThumbNumber == null ? ""  : innThumbNumber + "", bodyStyle, row, 4, false); // 创意点赞量
            createCell(tem.getInnEmployeeName(), bodyStyle, row, 5, false); // 创意提出的用户姓名

            createCell(WcxInnoTypeEnum.getInnoTypeName(tem.getInnType()), bodyStyle, row, 6, false);
            createCell(tem.getInnTeamContent(), bodyStyle, row, 7, false);
            createCell(tem.getInnProjectName(), bodyStyle, row, 8, false);
            createCell(tem.getInnRegionName(), bodyStyle, row, 9, false);

            // 人员
            createCell(tem.getPrizeEmployeeName(), bodyStyle, row, 10, false); // 获奖人员名称
            Integer prizeUserThumbNum = tem.getPrizeUserThumbNum();
            Integer prizeUserInnNum = tem.getPrizeUserInnNum();
            createCell(prizeUserThumbNum == null ? "" : prizeUserThumbNum + "", bodyStyle, row, 11, false); // 获奖人员名称
            createCell(prizeUserInnNum == null ? "" : prizeUserInnNum + "", bodyStyle, row, 12, false); // 获奖人员名称

            // 项目
            createCell(tem.getProjectName(), bodyStyle, row, 13, false); // 获奖项目
            // 区域
            createCell(tem.getRegionName(), bodyStyle, row, 14, false); // 获奖区域
            rowIndex++;
        }
    }

    public int exportItem(List<WcxPrizeDto> 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;
    }
}
