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

import cn.com.countrygarden.bi.main.dto.mediaResource.MediaOrderDto;
import cn.com.countrygarden.bi.main.handler.wcx.Export;
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.math.BigDecimal;
import java.util.List;

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

    /**
     * 创建表头
     *
     * @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));
        // 0订单号 1客户类型 2客户名称 3联系人 4电话 5订单开始时间
        // 6订单结束时间 7订单金额 8订单状态 9操作人
        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);
    }

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

        for (MediaOrderDto tem : data) {
            Row row = sheet.createRow(rowIndex);
            // 0订单号 1客户类型 2客户名称 3联系人 4电话 5订单开始时间
            createCell(tem.getOrderCode(), bodyStyle, row, 0, false);
            createCell(tem.getCustomerTypeName(), bodyStyle, row, 1, false);
            createCell(tem.getCustomerName(), bodyStyle, row, 2, false);
            createCell(tem.getContactName(), bodyStyle, row, 3, false);
            createCell(tem.getContactPhone(), bodyStyle, row, 4, false);
            String orderStartDate = DateUtil.formatDateToStr(tem.getOrderStartDate(), "yyyy-MM-dd");
            createCell(orderStartDate, bodyStyle, row, 5, false);

            // 6订单结束时间 7订单金额 8订单状态 9操作人
            String orderEndDate = DateUtil.formatDateToStr(tem.getOrderEndDate(), "yyyy-MM-dd");
            createCell(orderEndDate , bodyStyle, row, 6, false);
            BigDecimal orderAmount = tem.getOrderAmount();
            orderAmount = orderAmount == null ? new BigDecimal("0") : orderAmount;
            createCell(orderAmount.toString(), bodyStyle, row, 7, false);
            createCell(tem.getOrderStatusName(), bodyStyle, row, 8, false);
            createCell(tem.getCreateByName(), bodyStyle, row, 9, false);

            rowIndex++;
        }
    }

    public int exportItem(List<MediaOrderDto> 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;
    }
    
    public int exportItemForPre(List<MediaOrderDto> data, String prizeTitle, OutputStream outputStream) {
        // 表头
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        createHeaderForPre(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;
    }
    
    public void createHeaderForPre(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预订人
        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);
    }

}
