package com.ruoyi.web.controller.info;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.info.order.domain.bo.OrderBo;
import com.ruoyi.info.order.domain.vo.OrderVo;
import com.ruoyi.info.order.enums.RefundStatus;
import com.ruoyi.info.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务Controller
 *
 * @author ruoyi
 * @date 2025-09-20
 */
@Validated
@Api(value = "财务控制器", tags = {"财务管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/order/statistics")
public class OrderStatisticsController extends BaseController {

    private final IOrderService iOrderService;

    /**
     * 分页查询财务列表
     */
    @ApiOperation("查询财务列表")
    @SaCheckPermission("order:statistics:list")
    @GetMapping("/page")
    public TableDataInfo<OrderVo> page(@Validated(QueryGroup.class) OrderBo bo, PageQuery pageQuery) {
        bo.setIsRefund(true);
        return iOrderService.queryAllPageList(bo, pageQuery);
    }

    @ApiOperation("统计金额")
    @GetMapping("/sumPrice")
    public R<Map<String,  Object>> sumPrice(OrderBo bo) {
        Map<String, Object> map = new HashMap<>();
        map.put("totalPrice", iOrderService.totalPriceSum(bo));
        bo.setRefundStatus(RefundStatus.complete.getCode());
        map.put("refundPrice", iOrderService.refundPriceSum(bo));
        return R.ok(map);
    }

    /**
     * 导出财务列表
     */
    @ApiOperation("导出财务列表")
    @SaCheckPermission("order:statistics:export")
    @Log(title = "财务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated OrderBo bo, HttpServletResponse response) {
        List<OrderVo> list = iOrderService.queryAllList(bo);
        ExcelUtil.exportExcel(list, "财务列表", OrderVo.class, response);
    }

}
