package com.community.huiju.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.TaUserLicenseOrder;
import com.community.huiju.dao.TaUserLicenseOrderMapper;
import com.community.huiju.service.ITaUserLicenseOrderService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 月租车缴费订单表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-13
 */
@Service
public class TaUserLicenseOrderServiceImpl extends ServiceImpl<TaUserLicenseOrderMapper, TaUserLicenseOrder> implements ITaUserLicenseOrderService {

    @Autowired
    private TaUserLicenseOrderMapper taUserLicenseOrderMapper;

    @Override
    public ResponseBean getAll(Integer id, String licensePlate, String orderNumber, String paymentName, String paymentTel, Integer pageNum, Integer pageSize, UserElement userElement) {
        ResponseBean responseBean = new ResponseBean();
        IPage<TaUserLicenseOrder> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        QueryWrapper<TaUserLicenseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(id != null ,"id", id);
        queryWrapper.like(StringUtils.isNotBlank(licensePlate), "license_plate", licensePlate);
        queryWrapper.like(StringUtils.isNotBlank(orderNumber), "order_number", orderNumber);
        queryWrapper.like(StringUtils.isNotBlank(paymentName), "payment_name", paymentName);
        queryWrapper.like(StringUtils.isNotBlank(paymentTel), "payment_tel", paymentTel);
        queryWrapper.eq("community_id", userElement.getCommunityId());
        queryWrapper.orderByDesc("create_date");

        IPage<TaUserLicenseOrder> orderIPage = this.page(page, queryWrapper);
        responseBean.addSuccess(orderIPage);
        return responseBean;
    }

    @Override
    public ResponseBean exportDate(UserElement userElement) {
        ResponseBean responseBean = new ResponseBean();
        Integer pageNum = 0;
        Integer pageSize = 10;
        Integer count = 0;
        count = taUserLicenseOrderMapper.getCount(userElement.getCommunityId());
        List<TaUserLicenseOrder> licensePage = taUserLicenseOrderMapper.getLicensePage(userElement.getCommunityId(), pageNum * pageSize, pageSize);

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("编号");
        cell = row.createCell(0);
        cell.setCellValue("车牌号");
        cell = row.createCell(1);
        cell.setCellValue("续期时长（月）");
        cell = row.createCell(2);
        cell.setCellValue("续期费用（元）");
        cell = row.createCell(3);
        cell.setCellValue("缴费订单号");
        cell = row.createCell(4);
        cell.setCellValue("缴费人手机号");
        cell = row.createCell(5);
        cell.setCellValue("缴费人姓名");
        cell = row.createCell(6);
        cell.setCellValue("缴费方式");
        cell = row.createCell(7);
        cell.setCellValue("延期后到期日");
        cell = row.createCell(8);
        cell.setCellValue("缴费完成时间");

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");

        int listIndex = 0;
        int currentRow = 1;
        if (listIndex < licensePage.size()) {
            do{
                TaUserLicenseOrder taUserLicenseOrder = licensePage.get(listIndex);

                row = sheet.createRow(currentRow);
                cell = row.createCell(0);
                cell.setCellValue(taUserLicenseOrder.getId());
                cell = row.createCell(1);
                cell.setCellValue(taUserLicenseOrder.getLicensePlate());
                cell = row.createCell(2);
                cell.setCellValue(taUserLicenseOrder.getExtensionMonth());
                cell = row.createCell(3);
                cell.setCellValue(taUserLicenseOrder.getExtensionPrice());
                cell = row.createCell(4);
                cell.setCellValue(taUserLicenseOrder.getOrderNumber());
                cell = row.createCell(5);
                cell.setCellValue(taUserLicenseOrder.getPaymentTel());
                cell = row.createCell(6);
                cell.setCellValue(taUserLicenseOrder.getPaymentName());
                cell = row.createCell(7);
                cell.setCellValue("0".equals(taUserLicenseOrder.getPaymentType()) ? "微信支付" : "2".equals(taUserLicenseOrder.getPaymentType()) ? "支付宝" : "" );
                cell = row.createCell(8);
                cell.setCellValue(dateTimeFormatter.format(taUserLicenseOrder.getExpireDate()));
                cell = row.createCell(9);
                cell.setCellValue(taUserLicenseOrder.getUpdateDate() == null ? "" : dateTimeFormatter.format(taUserLicenseOrder.getUpdateDate()));

                listIndex ++;
                currentRow ++;

                if (listIndex == licensePage.size()) {
                    listIndex = 0;
                    pageNum ++;
                    licensePage = taUserLicenseOrderMapper.getLicensePage(userElement.getCommunityId(), pageNum * pageSize, pageSize);
                }
                if (licensePage.isEmpty()) {
                    break;
                }

            }while (pageNum <= count);
        }

        responseBean.addSuccess(workbook);
        return responseBean;
    }
}
