package cn.com.countrygarden.bi.main.controller.resident;

import cn.com.countrygarden.bi.main.model.resident.ResidentContractParty;
import cn.com.countrygarden.bi.main.service.dup.impl.ExcelHelper;
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.ss.util.CellRangeAddress;

import java.util.List;

public class PartyBExporter {

    public void createHeader(Workbook workbook, Sheet sheet) {
        ExcelHelper helper = new ExcelHelper();
        CellStyle headerStyle = helper.createHeaderStyle(workbook);
        Row row = sheet.createRow(0);
        Row rowTwo = sheet.createRow(1);
        row.setHeight((short) (250 * 2));
        helper.createCell("序列", headerStyle, row, 0);
        helper.createCell(null, headerStyle, rowTwo, 0);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));


        helper.createCell("供应商", headerStyle, row, 1);
        helper.createCell(null, headerStyle, rowTwo, 1);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));

        helper.createCell("纳税人识别号", headerStyle, row, 2);
        helper.createCell(null, headerStyle, rowTwo, 2);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));

        helper.createCell("地址", headerStyle, row, 3);
        helper.createCell(null, headerStyle, rowTwo, 3);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));

        helper.createCell("电话", headerStyle, row, 4);
        helper.createCell(null, headerStyle, rowTwo, 4);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 4, 4));

        helper.createCell("开户行", headerStyle, row, 5);
        helper.createCell(null, headerStyle, rowTwo, 5);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5));

        helper.createCell("银行账号", headerStyle, row, 6);
        helper.createCell(null, headerStyle, rowTwo, 6);
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 6, 6));

        sheet.createFreezePane( 0, 2, 0, 2 );
    }

    public void createBody(List<ResidentContractParty> dataList, Workbook workbook, Sheet sheet) {
        ExcelHelper helper = new ExcelHelper();
        CellStyle cellStyle = helper.createBodyStyle(workbook);
        if (dataList.size() == 0) {
            return;
        }
        for (int i = 0; i < dataList.size(); i++) {
            int index = i + 1;
            int rowNum = i + 2;
            createRow(rowNum, index, dataList.get(i), sheet, cellStyle, helper);
        }
        for (int i = 1; i < 7; i++) {
            sheet.autoSizeColumn(i);
        }
    }

    public void createRow(int rowIndex, int index, ResidentContractParty data, Sheet sheet, CellStyle cellStyle, ExcelHelper helper) {
        Row row = sheet.createRow(rowIndex);
        row.setHeight((short) (250 * 2));
        helper.createCell(Integer.toString(index), cellStyle, row, 0);
        helper.createCell(data.getPartyB(), cellStyle, row, 1);
        helper.createCell(data.getTaxpayerNumber(), cellStyle, row, 2);
        helper.createCell(data.getAddress(), cellStyle, row, 3);
        helper.createCell(data.getPhone(), cellStyle, row, 4);
        helper.createCell(data.getBank(), cellStyle, row, 5);
        helper.createCell(data.getAccount(), cellStyle, row, 6);
    }
}
