package com.saye.hospitalgd.controller.FinancialReconciliation;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.log.ExceptionDUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.model.Dicinfo;
import com.saye.hospitalgd.service.FinancialReconciliation.RefundStatisticsService;
import com.saye.hospitalgd.service.system.DicinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author thuang
 * @version 1.0
 * @description: 退款数据统计报表
 * @date 2024/12/19 10:00
 */
@Api(tags = "退款数据统计报表")
@Controller
@RequestMapping("/refundStatistics")
public class RefundStatisticsController {

    @Autowired
    private RefundStatisticsService refundStatisticsService;

    @Autowired
    private DicinfoService dicinfoService;

    /**
     * @description: 到退款统计报表页面
     * @author thuang
     * @date 2024/12/19 10:00
     * @version 1.0
     */
    @RequestMapping("/toRefundStatistics")
    public String toRefundStatistics(ModelMap modelMap) {

        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());

            // 默认查询最近1个月的数据
            calendar.add(Calendar.MONTH, -1);
            Date startDate = calendar.getTime();

            String startTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, startDate);
            String endTime = DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd);

            modelMap.addAttribute("startTime", startTime);
            modelMap.addAttribute("endTime", endTime);

        //支付方式
        List<Dicinfo> payType = dicinfoService.findDicinfoTreeNodeList("PAY_TYPE");
        modelMap.addAttribute("payTypeList", payType);

        //退款类型
        List<Dicinfo> refundType = dicinfoService.findDicinfoTreeNodeList("REFUND_TYPE");
        modelMap.addAttribute("refundTypeList", refundType);

        //业务类型
        List<Dicinfo> bizType = dicinfoService.findDicinfoTreeNodeList("BIZ_TYPE");
        modelMap.addAttribute("bizTypeList", bizType);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "financialReconciliation/refundStatistics";
    }

    /**
     * @description: 查询退款统计报表数据
     * @author thuang
     * @date 2024/12/19 10:00
     * @version 1.0
     */
    @RequestMapping("/findRefundStatistics")
    @ResponseBody
    @ApiOperation("查询退款统计报表数据")
    public HashMap<Object, Object> findRefundStatistics(
            @ApiParam("开始时间") String startTime,
            @ApiParam("结束时间") String endTime,
            @ApiParam("支付方式") String payType,
            @ApiParam("业务类型") String bizType,
            @ApiParam("退款类型") String refundType,
            @ApiParam("统计维度") String dimension,
            @ApiParam("页码") int page,
            @ApiParam("每页数量") int limit) {

        HashMap<Object, Object> responseMap = new HashMap<Object, Object>();

        try {
            HashMap<Object, Object> map = new HashMap<Object, Object>();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("payType", payType);
            map.put("bizType", bizType);
            map.put("refundType", refundType);
            map.put("dimension", dimension);

            PageHelper.startPage(page, limit);
            PageInfo<HashMap<Object, Object>> pageInfo = new PageInfo<HashMap<Object, Object>>(refundStatisticsService.findRefundStatistics(map));

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            responseMap.put("count", pageInfo.getTotal());
            responseMap.put("data", pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 获取退款统计汇总数据
     * @author thuang
     * @date 2024/12/19 10:00
     * @version 1.0
     */
    @RequestMapping("/getRefundSummary")
    @ResponseBody
    @ApiOperation("获取退款统计汇总数据")
    public HashMap<Object, Object> getRefundSummary(
            @ApiParam("开始时间") String startTime,
            @ApiParam("结束时间") String endTime,
            @ApiParam("支付方式") String payType,
            @ApiParam("业务类型") String bizType,
            @ApiParam("退款类型") String refundType) {

        HashMap<Object, Object> responseMap = new HashMap<Object, Object>();

        try {
            HashMap<Object, Object> map = new HashMap<Object, Object>();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("payType", payType);
            map.put("bizType", bizType);
            map.put("refundType", refundType);

            HashMap<Object, Object> summaryData = refundStatisticsService.getRefundSummary(map);

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            responseMap.put("data", summaryData);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 获取退款趋势数据
     * @author thuang
     * @date 2024/12/19 10:00
     * @version 1.0
     */
    @RequestMapping("/getRefundTrend")
    @ResponseBody
    @ApiOperation("获取退款趋势数据")
    public HashMap<Object, Object> getRefundTrend(
            @ApiParam("开始时间") String startTime,
            @ApiParam("结束时间") String endTime,
            @ApiParam("支付方式") String payType,
            @ApiParam("业务类型") String bizType,
            @ApiParam("退款类型") String refundType) {

        HashMap<Object, Object> responseMap = new HashMap<Object, Object>();

        try {
            HashMap<Object, Object> map = new HashMap<Object, Object>();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("payType", payType);
            map.put("bizType", bizType);
            map.put("refundType", refundType);

            List<HashMap<Object, Object>> trendData = refundStatisticsService.getRefundTrend(map);

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            responseMap.put("data", trendData);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因：" + msg);
        }
        return responseMap;
    }

    /**
     * @description: 导出退款统计报表
     * @author thuang
     * @date 2024/12/19 10:00
     * @version 1.0
     */
    @RequestMapping("/exportRefundStatistics")
    @ResponseBody
    @ApiOperation("导出退款统计报表")
    public HashMap<Object, Object> exportRefundStatistics(
            @ApiParam("开始时间") String startTime,
            @ApiParam("结束时间") String endTime,
            @ApiParam("支付方式") String payType,
            @ApiParam("业务类型") String bizType,
            @ApiParam("退款类型") String refundType,
            @ApiParam("统计维度") String dimension) {

        HashMap<Object, Object> responseMap = new HashMap<Object, Object>();

        try {
            HashMap<Object, Object> map = new HashMap<Object, Object>();
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("payType", payType);
            map.put("bizType", bizType);
            map.put("refundType", refundType);
            map.put("dimension", dimension);

            String fileName = refundStatisticsService.exportRefundStatistics(map);

            responseMap.put("code", 0);
            responseMap.put("msg", "导出成功");
            responseMap.put("fileName", fileName);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "导出失败,原因：" + msg);
        }
        return responseMap;
    }
}
