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.IncomeAuditPageReqVO;
import com.yc.ship.module.trade.controller.admin.report.vo.IncomeAuditRespVO;
import com.yc.ship.module.trade.dal.mysql.report.IncomeAuditMapper;
import com.yc.ship.module.trade.service.report.IncomeAuditService;
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 auto-generated
 */
@Service
@Slf4j
public class IncomeAuditServiceImpl implements IncomeAuditService {

    @Resource
    private IncomeAuditMapper incomeAuditMapper;

    /**
     * 查询收入稽核表分页
     *
     * @param pageReqVO 分页查询条件
     * @return 分页结果
     */
    @Override
    public PageResult<IncomeAuditRespVO> getIncomeAuditPage(IncomeAuditPageReqVO pageReqVO) {
        IPage<IncomeAuditRespVO> page = incomeAuditMapper.selectIncomeAuditPage(
                new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    /**
     * 导出收入稽核表 Excel
     * 数字字段导出为数字格式（通过 @NumberFormat 注解实现）
     *
     * @param pageReqVO 查询条件
     * @param response  HTTP响应
     * @throws IOException IO异常
     */
    @Override
    public void exportIncomeAuditExcel(IncomeAuditPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        // 1. 查询所有数据（不分页）
        List<IncomeAuditRespVO> list = incomeAuditMapper.selectIncomeAuditExportList(pageReqVO);
        if (CollUtil.isEmpty(list)) {
            log.warn("[exportIncomeAuditExcel] 无数据可导出");
        } else {
            // 2. 计算合计行并追加到列表末尾
            IncomeAuditRespVO summary = calculateSummary(list);
            list.add(summary);
        }
        // 3. 导出 Excel（数字字段使用 @NumberFormat 注解确保导出为数字格式）
        ExcelUtils.write(response, "收入稽核表.xls", "收入稽核表", IncomeAuditRespVO.class, list);
    }

    /**
     * 计算合计行数据
     *
     * @param list 数据列表
     * @return 合计行
     */
    private IncomeAuditRespVO calculateSummary(List<IncomeAuditRespVO> list) {
        IncomeAuditRespVO summary = new IncomeAuditRespVO();
        summary.setTravelAgency("合计");

        BigDecimal totalYichangToChongqing = BigDecimal.ZERO;
        BigDecimal totalChongqingToYichang = BigDecimal.ZERO;
        BigDecimal totalAmount = BigDecimal.ZERO;

        for (IncomeAuditRespVO item : list) {
            totalYichangToChongqing = totalYichangToChongqing.add(
                    item.getYichangToChongqing() != null ? item.getYichangToChongqing() : BigDecimal.ZERO);
            totalChongqingToYichang = totalChongqingToYichang.add(
                    item.getChongqingToYichang() != null ? item.getChongqingToYichang() : BigDecimal.ZERO);
            totalAmount = totalAmount.add(
                    item.getTotalAmount() != null ? item.getTotalAmount() : BigDecimal.ZERO);
        }

        summary.setYichangToChongqing(totalYichangToChongqing);
        summary.setChongqingToYichang(totalChongqingToYichang);
        summary.setTotalAmount(totalAmount);
        return summary;
    }

}
