package com.saye.hospitalgd.service.FinancialReconciliation.impl;

import com.saye.hospitalgd.commons.excel.RefundStatisticsExportXLSX;
import com.saye.hospitalgd.commons.log.ExceptionDUtil;
import com.saye.hospitalgd.mapper.FinancialReconciliation.RefundStatisticsMapper;
import com.saye.hospitalgd.service.FinancialReconciliation.RefundStatisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * @author thuang
 * @version 1.0
 * @description: 退款数据统计报表服务实现类
 * @date 2024/12/19 10:00
 */
@Service
@Transactional
public class RefundStatisticsServiceImpl implements RefundStatisticsService {

    private static final Logger log = LoggerFactory.getLogger(RefundStatisticsServiceImpl.class);

    @Autowired
    private RefundStatisticsMapper refundStatisticsMapper;

    @Override
    public List<HashMap<Object, Object>> findRefundStatistics(HashMap<Object, Object> map) throws Exception {
        try {
            return refundStatisticsMapper.findRefundStatistics(map);
        } catch (Exception e) {
            log.info("查询退款统计报表数据失败", e);
            throw e;
        }
    }

    @Override
    public HashMap<Object, Object> getRefundSummary(HashMap<Object, Object> map) throws Exception {
        try {
            return refundStatisticsMapper.getRefundSummary(map);
        } catch (Exception e) {
            log.error("获取退款统计汇总数据失败", e);
            throw e;
        }
    }

    @Override
    public List<HashMap<Object, Object>> getRefundTrend(HashMap<Object, Object> map) throws Exception {
        try {
            return refundStatisticsMapper.getRefundTrend(map);
        } catch (Exception e) {
            log.error("获取退款趋势数据失败", e);
            throw e;
        }
    }

    @Override
    public String exportRefundStatistics(HashMap<Object, Object> map) throws Exception {
        try {
            // 获取统计数据
            List<HashMap<Object, Object>> dataList = refundStatisticsMapper.findRefundStatistics(map);
            
            // 获取汇总数据
            HashMap<Object, Object> summaryData = refundStatisticsMapper.getRefundSummary(map);
            
            // 创建导出文件
            String fileName = "退款统计报表_" + System.currentTimeMillis() + ".xlsx";
            String filePath = System.getProperty("user.dir") + File.separator + "downloadFile" + File.separator + fileName;
            
            // 确保目录存在
            File file = new File(filePath);
            File parentDir = file.getParentFile();
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
            
            // 导出Excel
            RefundStatisticsExportXLSX exportXLSX = new RefundStatisticsExportXLSX();
            exportXLSX.exportRefundStatistics(dataList, summaryData, filePath);
            
            return fileName;
        } catch (Exception e) {
            log.error("导出退款统计报表失败", e);
            throw e;
        }
    }
}
