package com.yc.ship.module.trade.service.report.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.module.trade.controller.admin.report.vo.PrepaidRechargeLedgerPageReqVO;
import com.yc.ship.module.trade.controller.admin.report.vo.PrepaidRechargeLedgerRespVO;
import com.yc.ship.module.trade.dal.mysql.report.PrepaidRechargeLedgerMapper;
import com.yc.ship.module.trade.service.report.PrepaidRechargeLedgerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * 预存款充值流水 Service 实现类
 *
 * @author 管理员
 */
@Service
@Slf4j
public class PrepaidRechargeLedgerServiceImpl implements PrepaidRechargeLedgerService {

    @Resource
    private PrepaidRechargeLedgerMapper prepaidRechargeLedgerMapper;

    /**
     * 查询预存款充值流水分页
     *
     * @param pageReqVO 分页查询条件
     * @return 分页结果
     */
    @Override
    public PageResult<PrepaidRechargeLedgerRespVO> getPrepaidRechargeLedgerPage(PrepaidRechargeLedgerPageReqVO pageReqVO) {
        IPage<PrepaidRechargeLedgerRespVO> page = prepaidRechargeLedgerMapper.selectPrepaidRechargeLedgerPage(
                new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    /**
     * 导出预存款充值流水 Excel
     *
     * @param pageReqVO 查询条件
     * @param response  HTTP响应
     * @throws IOException IO异常
     */
    @Override
    public void exportPrepaidRechargeLedgerExcel(PrepaidRechargeLedgerPageReqVO pageReqVO,
                                                 HttpServletResponse response) throws IOException {
        // 1. 查询所有数据（不分页）
        List<PrepaidRechargeLedgerRespVO> list = prepaidRechargeLedgerMapper.selectPrepaidRechargeLedgerExportList(pageReqVO);
        if (CollUtil.isEmpty(list)) {
            log.warn("[exportPrepaidRechargeLedgerExcel] 无数据可导出");
        } else {
            // 2. 计算合计行并追加到列表末尾
            PrepaidRechargeLedgerRespVO summary = calculateSummary(list);
            list.add(summary);
        }
        // 3. 导出 Excel（数字字段使用 @NumberFormat 注解确保导出为数字格式）
        ExcelUtils.write(response, "预存款充值流水表.xls", "预存款充值流水", PrepaidRechargeLedgerRespVO.class, list);
    }

    /**
     * 计算合计行数据
     */
    private PrepaidRechargeLedgerRespVO calculateSummary(List<PrepaidRechargeLedgerRespVO> list) {
        PrepaidRechargeLedgerRespVO summary = new PrepaidRechargeLedgerRespVO();
        summary.setRechargeDate("合计");
        summary.setTravelAgency(null);
        summary.setPaymentTypeName(null);
        summary.setRemark(null);

        BigDecimal totalAmount = BigDecimal.ZERO;
        for (PrepaidRechargeLedgerRespVO item : list) {
            totalAmount = totalAmount.add(
                    item.getAmount() != null ? item.getAmount() : BigDecimal.ZERO);
        }
        summary.setAmount(totalAmount);
        return summary;
    }

}
