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

import cn.com.countrygarden.bi.main.dto.wcx.WcxQuerySuggestDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxReplyDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxSuggestReplyDto;
import cn.com.countrygarden.bi.main.util.wcx.DateUtil;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
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 WcxSuggestExportHandle extends Export<WcxQuerySuggestDto> {

	public static  List<WcxSuggestReplyDto> replyDtos;
    /**
     * 创建表头
     *
     * @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关闭状态
        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);

    }

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

        for (WcxQuerySuggestDto tem : data) {
            Row row = sheet.createRow(rowIndex);
            // 0员工姓名 1提议标题 2提议描述 3创建时间 4回复数量 5关闭状态
            createCell(tem.getEmployeeName(), bodyStyle, row, 0, false);
            createCell(tem.getSuggestTitle(), bodyStyle, row, 1, false);
            createCell(tem.getSuggestContent(), style2, row, 2, false);
            String createDateStr = DateUtil.formatDateToStr(tem.getCreateDate(), "yyyy-MM-dd HH:mm:ss");
            createCell(createDateStr, bodyStyle, row, 3, false);
            createCell(tem.getReplyNumber().toString(), bodyStyle, row, 4, false);
            //设置回复内容
            StringBuffer buffer = new StringBuffer();
            if(null != replyDtos) {
                for (WcxSuggestReplyDto replyDto : replyDtos) {
                	if(replyDto.getSuggestId().equals(tem.getSuggestId())) {
                		buffer.append(replyDto.getReplyEmployeeName()== null ?"未知用户":replyDto.getReplyEmployeeName())
                		.append("(回复时间:")
                		.append(DateUtil.formatDateToStr(replyDto.getCreateDate(), "yyyy-MM-dd HH:mm:ss")).append(")").append(":")
                		.append(replyDto.getReplyContent())
                		.append("(").append(replyDto.getAdoptStatus().equals("1")?"已采纳":"未采纳").append(")").append("\r\n");
                	}
    			}
            }
            createCell(buffer.toString(), style2, row, 5, false);
            String statusStr = tem.getCloseStatus() ==1 ? "开启":"关闭";
            createCell(statusStr, bodyStyle, row, 6, false);
            rowIndex++;
        } 
        replyDtos = null;
    }

    public int exportItem(List<WcxQuerySuggestDto> data,List<WcxSuggestReplyDto> replys, String prizeTitle, OutputStream outputStream) {
        // 表头
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        createHeader(workbook, sheet);
        sheet.setColumnWidth(5,9000);
        sheet.setColumnWidth(2,9000);
        replyDtos = replys;
        // 表身
        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;
    }
    
    /**
     * 创建体风格
     * @param workbook
     * @return
     */
    private CellStyle createBodyStyle2(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.LEFT);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setWrapText(true);
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderRight(BorderStyle.THIN);//右边框
        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        return style;
    }
    
}
