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.WcxClassificationEnum;
import cn.com.countrygarden.bi.main.constant.wcx.enumeration.WcxInnoTypeEnum;
import cn.com.countrygarden.bi.main.dto.wcx.WcxInnovationDto;
import cn.com.countrygarden.bi.main.util.wcx.DateUtil;
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;

/**
 * 创意导出处理
 */
@Component
public class WcxInnoExportHandle extends Export<WcxInnovationDto>{

    /**
     * 创建表头
     *
     * @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));
        // 0标题 1用户名 2区域 3项目 4基线 5类型 6点赞数 7金锤数 8评论数 9创新类型 10团队成员介绍 11创建时间
        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, 15, false);
        createCell("处理时间", headerStyle, row, 16, false);
    }

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

        for (WcxInnovationDto tem : data) {
            Row row = sheet.createRow(rowIndex);
            // 0标题 1用户名 2区域 3项目 4基线 5类型 6点赞数 7金锤数 8评论数 9创新类型 10团队成员介绍 11创建时间
            createCell(tem.getInnovationTitle(), bodyStyle, row, 0, false);
            createCell(tem.getInnovationSimpleContent(), bodyStyle, row, 1, false);
            createCell(tem.getEmployeeName(), bodyStyle, row, 2, false);
            createCell(tem.getRegionName(), bodyStyle, row, 3, false);
            createCell(tem.getProjectName(), bodyStyle, row, 4, false);
            createCell(WcxBaselineEnum.getBaselineName(tem.getBaseline()), bodyStyle, row, 5, false);

            if(tem.getBaseline()!="" && "7".equals(tem.getBaseline())){
                String names="";
                if (tem.getClassification()=="1" || "1".equals(tem.getClassification()) ){
                    names="优秀人物/措施";
                }else if (tem.getClassification()=="2" || "2".equals(tem.getClassification())){
                    names="行业荣誉";
                }else if (tem.getClassification()=="3" || "3".equals(tem.getClassification())){
                    names="专业成果";
                }
//                else if (tem.getClassification()=="4" || "4".equals(tem.getClassification())){
//                    names="社会责任";
//                }else if (tem.getClassification()=="5" || "5".equals(tem.getClassification())){
//                    names="创新举措";
//                }
                createCell(names, bodyStyle, row, 6, false);
            }else {
                createCell(WcxClassificationEnum.getClassificationName(tem.getClassification()), bodyStyle, row, 6, false);
            }
            long thumbNumber = tem.getThumbNumber();
            String status="";
            if(tem.getExamineStatus()!=""){
                if(tem.getExamineStatus()=="1"||"1".equals(tem.getExamineStatus())){
                    status="审核通过";
                }
                if(tem.getExamineStatus()=="0"||"0".equals(tem.getExamineStatus())){
                    status="待审核";
                }
                if(tem.getExamineStatus()=="2"||"2".equals(tem.getExamineStatus())){
                    status="审核不通过";
                }
            }
            createCell(status + "" , bodyStyle, row, 7, false);
            createCell(tem.getOperator() + "", bodyStyle, row, 8, false);
            createCell(tem.getOpinion() + "", bodyStyle, row, 9, false);
            createCell(WcxInnoTypeEnum.getInnoTypeName(tem.getInnovationType()), bodyStyle, row, 10, false);

            createCell(tem.getTeamContent(), bodyStyle, row, 11, false);
            String createDateStr = DateUtil.formatDateToStr(tem.getCreateDate(), "yyyy-MM-dd HH:mm:ss");
            String handleDate=DateUtil.formatDateToStr(tem.getHandleDate(),"yyyy-MM-dd HH:mm:ss");
            createCell(tem.getIsTop().equals(1)?"是":"否", bodyStyle, row, 12, false);
            createCell(tem.getIsRecomm().equals(1)?"是":"否", bodyStyle, row, 13, false);
            createCell(tem.getIsExcellent().equals(1)?"是":"否", bodyStyle, row, 14, false);
            createCell(createDateStr, bodyStyle, row, 15, false);
            createCell(handleDate, bodyStyle, row, 16, false);

            rowIndex++;
        }
    }

    public int exportItem(List<WcxInnovationDto> data, String prizeTitle, OutputStream outputStream) {
        // 表头
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        createHeader(workbook, sheet);
        // 表身
        if (data != null && data.size() > 0) {
            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;
    }
}
