package com.wzj.soopin.statistics.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.order.domain.entity.Aftersale;
import com.wzj.soopin.order.domain.vo.PageVO;
import com.wzj.soopin.order.utils.PageUtil;
import com.wzj.soopin.statistics.entity.dto.StatisticsQueryParam;
import com.wzj.soopin.statistics.mapper.AfterSaleStatisticsMapper;
import com.wzj.soopin.statistics.service.AfterSaleStatisticsService;
import com.wzj.soopin.statistics.util.StatisticsDateUtil;
import jakarta.servlet.http.HttpServletResponse;
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.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 售后统计业务层实现
 *
 * @author Bulbasaur
 * @since 2020/12/9 11:30
 */
@Service
public class AfterSaleStatisticsServiceImpl extends ServiceImpl<AfterSaleStatisticsMapper, Aftersale> implements AfterSaleStatisticsService {


    @Override
    public long applyNum(String serviceType) {
//        AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser());
        LambdaQueryWrapper<Aftersale> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(Aftersale::getServiceStatus, AfterSaleStatusEnum.APPLY.name());
//        queryWrapper.eq(CharSequenceUtil.isNotEmpty(serviceType), AfterSale::getServiceType, serviceType);
//        queryWrapper.eq(CharSequenceUtil.equals(authUser.getRole().name(), UserEnums.STORE.name()),
//                AfterSale::getStoreId, authUser.getStoreId());
        return this.count(queryWrapper);
    }


    @Override
    public IPage<Aftersale> getStatistics(StatisticsQueryParam statisticsQueryParam, PageVO pageVO) {

        LambdaQueryWrapper<Aftersale> queryWrapper = new LambdaQueryWrapper<>();
//        Date[] dates = StatisticsDateUtil.getDateArray(statisticsQueryParam);
//        queryWrapper.between(AfterSale::getCreateTime, dates[0], dates[1]);
//        queryWrapper.eq(CharSequenceUtil.isNotEmpty(statisticsQueryParam.getStoreId()), AfterSale::getStoreId, statisticsQueryParam.getStoreId());

        return this.page(PageUtil.initPage(pageVO), queryWrapper);
    }

    @Override
    public void exportRefund(StatisticsQueryParam statisticsQueryParam, HttpServletResponse response) throws IOException {
        // 构建查询条件
        LambdaQueryWrapper<Aftersale> queryWrapper = new LambdaQueryWrapper<>();
        Date[] dates = StatisticsDateUtil.getDateArray(statisticsQueryParam);
        queryWrapper.between(Aftersale::getCreateTime, dates[0], dates[1]);
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(statisticsQueryParam.getStoreId()),
            Aftersale::getTenantId, statisticsQueryParam.getStoreId());

        // 查询数据
        List<Aftersale> list = this.list(queryWrapper);

        // 创建Excel工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("退单统计");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        String[] headers = {"商家名称", "商品名称", "售后单类型", "售后单状态", "退款时间", "申请退款金额", "申请原因", "实际金额"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 创建日期格式化对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // 填充数据行
        int rowNum = 1;
        for (Aftersale afterSale : list) {
            Row row = sheet.createRow(rowNum++);
//            row.createCell(0).setCellValue(afterSale.getStoreName() != null ? afterSale.getStoreName() : "");
//            row.createCell(1).setCellValue(afterSale.getGoodsName() != null ? afterSale.getGoodsName() : "");
//            row.createCell(2).setCellValue(afterSale.getServiceType() != null ? afterSale.getServiceType() : "");
//            row.createCell(3).setCellValue(afterSale.getServiceStatus() != null ? afterSale.getServiceStatus() : "");
//            row.createCell(4).setCellValue(afterSale.getCreateTime() != null ? dateFormat.format(afterSale.getCreateTime()) : "");
//            row.createCell(5).setCellValue(afterSale.getApplyRefundPrice() != null ? afterSale.getApplyRefundPrice().toString() : "0");
//            row.createCell(6).setCellValue(afterSale.getReason() != null ? afterSale.getReason() : "");
//            row.createCell(7).setCellValue(afterSale.getActualRefundPrice() != null ? afterSale.getActualRefundPrice().toString() : "0");
        }

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=退单统计.xlsx");

        // 写入响应流
        workbook.write(response.getOutputStream());
        workbook.close();
    }

}
