package com.community.huiju.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.model.BillInvoice;
import com.community.huiju.model.BillStatement;
import com.community.huiju.dao.BillStatementMapper;
import com.community.huiju.service.IBillStatementService;
import com.google.common.collect.Maps;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 账单流水表 服务实现类
 * </p>
 *
 * @author weiximei
 * @since 2019-02-13
 */
@Service
public class BillStatementServiceImpl extends ServiceImpl<BillStatementMapper, BillStatement> implements IBillStatementService {

    @Autowired
    private BillStatementMapper billStatementMapper;

    @Override
    public ResponseBean getBillStatement(UserElement userElement, String parameter) {
        ResponseBean responseBean = new ResponseBean();
        JSONObject jsonObject = JSONObject.parseObject(parameter);

        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        // 流水 id
        Integer id = jsonObject.getInteger("id");
        // 缴费项 id
        Integer billId = jsonObject.getInteger("billId");
        // 缴费项 名称
        String billName = jsonObject.getString("billName");
        // 缴费单 编号
        Integer billInvoiceId = jsonObject.getInteger("billInvoiceId");
        // 缴费人
        String payName = jsonObject.getString("payName");

        // 参数
        Map<String, Object> map = Maps.newHashMap();
        map.put("id", id);
        map.put("billId", billId);
        map.put("billName", billName);
        map.put("billInvoiceId", billInvoiceId);
        map.put("payName", payName);
        map.put("communityId", userElement.getCommunityId());

        Page<BillStatement> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<BillStatement> billStatementIPage = billStatementMapper.getBillStatementAll(page, map);

        Map<String, Object> respMap = Maps.newHashMap();
        respMap.put("list", billStatementIPage.getRecords());
        respMap.put("pageNum", billStatementIPage.getCurrent());
        respMap.put("pageSize", billStatementIPage.getSize());
        respMap.put("total", billStatementIPage.getTotal());

        responseBean.addSuccess(respMap);
        return responseBean;
    }

    @Override
    public ResponseBean exportExcel(UserElement userElement) {
        ResponseBean responseBean = new ResponseBean();
        String [] title = new String[] { "流水号","收费组编号","收费组名称","收费单号","费用金额","缴费人","缴费备注","缴费方式","流水生成时间"};

        HSSFWorkbook excel = excel(title, userElement,null);
        responseBean.addSuccess(excel);
        return responseBean;
    }

    /**
     * 表格
     * @param title
     * @param workbook
     * @return
     */
    private HSSFWorkbook excel(String[] title, UserElement userElement, HSSFWorkbook workbook) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        if (null == workbook) {
            workbook = new HSSFWorkbook();
        }
        HSSFSheet sheet = workbook.createSheet("缴费单");
        // 行数标记
        int rowNum = 0;
        // 创建一行,用作标题
        HSSFRow row = sheet.createRow(rowNum);
        // 创建样式
        HSSFCellStyle style = workbook.createCellStyle();
        // 居中
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 创建列对象
        HSSFCell cell = null;

        // 写入表头
        for (int i = 0; i<title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("communityId", userElement.getCommunityId());

        Page<BillStatement> page = new Page<>();
        page.setCurrent(1);
        page.setSize(500);
        IPage<BillStatement> billInvoiceIPage = billStatementMapper.getBillStatementAll(page, map);
        List<BillStatement> records = billInvoiceIPage.getRecords();


        // 写入内容
        for (int i = 1; i<= billInvoiceIPage.getPages(); i++) {
            for (int t = 0; t<records.size(); t++) {
                row = sheet.createRow(++rowNum);
                // 流水号
                cell = row.createCell(0);
                cell.setCellValue(records.get(t).getId());
                cell.setCellStyle(style);
                // 收费组编号
                cell = row.createCell(1);
                cell.setCellValue(records.get(t).getBillId());
                cell.setCellStyle(style);
                // 收费组名称
                cell = row.createCell(2);
                cell.setCellValue(records.get(t).getBillName());
                cell.setCellStyle(style);
                // 收费单号
                cell = row.createCell(3);
                cell.setCellValue(records.get(t).getBillInvoiceId());
                cell.setCellStyle(style);
                // 费用金额
                cell = row.createCell(4);
                double price = Double.valueOf(records.get(t).getPayPrice()) / 100.00;
                cell.setCellValue(price);
                cell.setCellStyle(style);
                // 缴费人
                cell = row.createCell(5);
                cell.setCellValue(records.get(t).getPayName());
                cell.setCellStyle(style);
                // 缴费备注
                cell = row.createCell(6);
                cell.setCellValue(records.get(t).getPayRemark());
                cell.setCellStyle(style);
                // 缴费方式
                cell = row.createCell(7);
                cell.setCellValue("0".equals(records.get(t).getPayType()) ? "微信支付" : "1".equals(records.get(t).getPayType()) ? "线下缴费" : "未知");
                cell.setCellStyle(style);
                if (null != records.get(t).getCreateTime()) {
                    // 生成流水时间
                    cell = row.createCell(8);
                    cell.setCellValue(dateFormat.format(records.get(t).getCreateTime()));
                    cell.setCellStyle(style);
                }
            }
            page.setCurrent(i);
            page.setSize(500);
            billInvoiceIPage = billStatementMapper.getBillStatementAll(page, map);
            records = billInvoiceIPage.getRecords();

        }

        return workbook;
    }

}
