package org.dromara.mall.statistics.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.statistics.domain.bo.StatisticsQueryBo;
import org.dromara.mall.statistics.domain.vo.MemberStatisticsVo;
import org.dromara.mall.statistics.domain.vo.TransactionStatisticsVo;
import org.dromara.mall.statistics.service.MallStatisticsService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 统计管理控制器
 *
 * @author pengles
 */
@Tag(name = "管理端-统计数据", description = "统计数据查询接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/statistics")
public class AdminMallStatisticsController extends BaseController {

    private final MallStatisticsService statisticsService;

    /**
     * 获取交易统计
     */
    @Operation(summary = "获取交易统计", description = "获取交易统计数据，包括订单统计、销售额统计、商品销售排行等")
    @SaCheckPermission("mall:statistics:query")
    @GetMapping("/transaction")
    public R<TransactionStatisticsVo> getTransactionStatistics(
            @Parameter(description = "开始日期") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @Parameter(description = "结束日期") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @Parameter(description = "商品销售排行限制数量") 
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        StatisticsQueryBo queryBo = new StatisticsQueryBo();
        queryBo.setStartDate(startDate);
        queryBo.setEndDate(endDate);
        queryBo.setLimit(limit);
        
        TransactionStatisticsVo result = statisticsService.getTransactionStatistics(queryBo);
        return R.ok(result);
    }

    /**
     * 获取会员统计
     */
    @Operation(summary = "获取会员统计", description = "获取会员统计数据，包括会员增长、消费统计等")
    @SaCheckPermission("mall:statistics:query")
    @GetMapping("/member")
    public R<MemberStatisticsVo> getMemberStatistics(
            @Parameter(description = "开始日期") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @Parameter(description = "结束日期") 
            @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        StatisticsQueryBo queryBo = new StatisticsQueryBo();
        queryBo.setStartDate(startDate);
        queryBo.setEndDate(endDate);
        
        MemberStatisticsVo result = statisticsService.getMemberStatistics(queryBo);
        return R.ok(result);
    }

}
