package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.jarvis.domain.GiftCoupon;
import com.ruoyi.jarvis.domain.OrderRows;
import com.ruoyi.jarvis.service.IGiftCouponService;
import com.ruoyi.jarvis.service.IOrderRowsService;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 礼金管理Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/giftcoupon")
public class GiftCouponController extends BaseController {

    private final IGiftCouponService giftCouponService;
    private final IOrderRowsService orderRowsService;

    public GiftCouponController(IGiftCouponService giftCouponService, IOrderRowsService orderRowsService) {
        this.giftCouponService = giftCouponService;
        this.orderRowsService = orderRowsService;
    }

    /**
     * 查询礼金列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GiftCoupon query, HttpServletRequest request) {
        startPage();
        
        // 处理时间筛选参数
        String beginTimeStr = request.getParameter("beginTime");
        String endTimeStr = request.getParameter("endTime");
        
        if (beginTimeStr != null && !beginTimeStr.isEmpty()) {
            query.getParams().put("beginTime", beginTimeStr);
        }
        if (endTimeStr != null && !endTimeStr.isEmpty()) {
            query.getParams().put("endTime", endTimeStr);
        }
        
        List<GiftCoupon> list = giftCouponService.selectGiftCouponList(query);
        return getDataTable(list);
    }

    /**
     * 导出礼金列表
     */
    @Log(title = "礼金管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GiftCoupon giftCoupon) throws IOException {
        String fileName = "礼金数据";

        List<GiftCoupon> list = giftCouponService.selectGiftCouponList(giftCoupon);

        // 设置响应头，指定文件名
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String encodedFileName = java.net.URLEncoder.encode(fileName + ".xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName);
        // 添加download-filename响应头，以支持前端工具类
        response.setHeader("download-filename", encodedFileName);

        ExcelUtil<GiftCoupon> util = new ExcelUtil<GiftCoupon>(GiftCoupon.class);
        util.exportExcel(response, list, fileName);
    }

    /**
     * 获取礼金详细信息
     */
    @GetMapping(value = "/{giftCouponKey}")
    public AjaxResult getInfo(@PathVariable("giftCouponKey") String giftCouponKey) {
        GiftCoupon giftCoupon = giftCouponService.selectGiftCouponByKey(giftCouponKey);
        if (giftCoupon != null) {
            // 查询关联的订单
            OrderRows query = new OrderRows();
            query.setGiftCouponKey(giftCouponKey);
            List<OrderRows> orders = orderRowsService.selectOrderRowsList(query);
            // 将订单列表添加到返回数据中
            java.util.Map<String, Object> result = new java.util.HashMap<>();
            result.put("giftCoupon", giftCoupon);
            result.put("orders", orders);
            return success(result);
        }
        return success(giftCoupon);
    }

    /**
     * 查询礼金关联的订单列表
     */
    @GetMapping("/{giftCouponKey}/orders")
    public TableDataInfo getOrders(@PathVariable("giftCouponKey") String giftCouponKey) {
        startPage();
        OrderRows query = new OrderRows();
        query.setGiftCouponKey(giftCouponKey);
        List<OrderRows> list = orderRowsService.selectOrderRowsList(query);
        return getDataTable(list);
    }

    /**
     * 查询礼金统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult statistics(GiftCoupon giftCoupon, HttpServletRequest request) {
        // 处理时间筛选参数
        String beginTimeStr = request.getParameter("beginTime");
        String endTimeStr = request.getParameter("endTime");
        
        if (beginTimeStr != null && !beginTimeStr.isEmpty()) {
            giftCoupon.getParams().put("beginTime", beginTimeStr);
        }
        if (endTimeStr != null && !endTimeStr.isEmpty()) {
            giftCoupon.getParams().put("endTime", endTimeStr);
        }
        
        GiftCoupon statistics = giftCouponService.selectGiftCouponStatistics(giftCoupon);
        return success(statistics);
    }
}

