/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.consumptionrecord.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
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.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.biz.modular.consumptionrecord.param.*;
import vip.xiaonuo.biz.modular.user.result.BizMemberUserResult;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.annotation.CommonNoRepeat;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.biz.modular.consumptionrecord.entity.ConsumptionRecord;
import vip.xiaonuo.biz.modular.consumptionrecord.service.ConsumptionRecordService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 消费记录控制器
 *
 * @author fanzherong
 * @date  2025/02/06 08:56
 */
@Tag(name = "消费记录控制器")
@RestController
@Validated
public class ConsumptionRecordController {

    @Resource
    private ConsumptionRecordService consumptionRecordService;

    /**
     * 获取消费记录分页
     *
     * @author fanzherong
     * @date  2025/02/06 08:56
     */
    @Operation(summary = "获取消费记录分页")
    @SaCheckPermission("/biz/consumptionrecord/page")
    @GetMapping("/biz/consumptionrecord/page")
    public CommonResult<Page<ConsumptionRecord>> page(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.page(consumptionRecordPageParam));
    }

    /**
     * 小程序获取消费记录分页
     *
     * @author fanzherong
     * @date  2025/02/06 08:56
     */
    @Operation(summary = "小程序获取消费记录分页")
    @SaCheckPermission("/biz/consumptionrecord/userPage")
    @GetMapping("/biz/consumptionrecord/userPage")
    public CommonResult<Page<ConsumptionRecord>> userPage(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.userPage(consumptionRecordPageParam));
    }

    /**
     * 添加消费记录
     *
     * @author fanzherong
     * @date  2025/02/06 08:56
     */
    @Operation(summary = "消费结算&会员管理赠送消费")
    @CommonLog("消费结算&会员管理赠送消费")
    @SaCheckPermission("/biz/consumptionrecord/add")
    @PostMapping("/biz/consumptionrecord/add")
    @CommonNoRepeat
    public CommonResult<String> add(@RequestBody @Valid ConsumptionRecordAddParam consumptionRecordAddParam) {
        consumptionRecordService.add(consumptionRecordAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑消费记录
     *
     * @author fanzherong
     * @date  2025/02/06 08:56
     */
    @Operation(summary = "编辑消费记录")
    @CommonLog("编辑消费记录")
    @SaCheckPermission("/biz/consumptionrecord/edit")
    @PostMapping("/biz/consumptionrecord/edit")
    public CommonResult<String> edit(@RequestBody @Valid ConsumptionRecordEditParam consumptionRecordEditParam) {
        consumptionRecordService.edit(consumptionRecordEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除消费记录
     *
     * @author fanzherong
     * @date  2025/02/06 08:56
     */
    @Operation(summary = "删除消费记录")
    @CommonLog("删除消费记录")
    @SaCheckPermission("/biz/consumptionrecord/delete")
    @PostMapping("/biz/consumptionrecord/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<ConsumptionRecordIdParam> consumptionRecordIdParamList) {
        consumptionRecordService.delete(consumptionRecordIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取消费记录详情
     *
     * @author fanzherong
     * @date  2025/02/06 08:56
     */
    @Operation(summary = "获取消费记录详情")
    @SaCheckPermission("/biz/consumptionrecord/detail")
    @GetMapping("/biz/consumptionrecord/detail")
    public CommonResult<ConsumptionRecord> detail(@Valid ConsumptionRecordIdParam consumptionRecordIdParam) {
        return CommonResult.data(consumptionRecordService.detail(consumptionRecordIdParam));
    }

    @Operation(summary = "小程序门店每日销售额统计")
    @SaCheckPermission("/biz/consumptionrecord/getRecordTotal")
    @GetMapping("/biz/consumptionrecord/getRecordTotal")
    public CommonResult<ConsumptionResult> getRecordTotal() {
        return CommonResult.data(consumptionRecordService.getRecordTotal());
    }

    @Operation(summary = "小程序门店每日销售额统计及较昨日对比")
    @SaCheckPermission("/biz/consumptionrecord/queryTodayRevenue")
    @GetMapping("/biz/consumptionrecord/queryTodayRevenue")
    public CommonResult<Map<String,Object>> queryTodayRevenue(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.queryTodayRevenue(consumptionRecordPageParam));
    }

    /**
     * 后台销售额统计
     * @return
     */
    @Operation(summary = "后台销售额统计")
    @SaCheckPermission("/biz/consumptionrecord/queryRecordTotal")
    @GetMapping("/biz/consumptionrecord/queryRecordTotal")
    public CommonResult<ConsumptionResult> getRecordTotal(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.getRecordTotal(consumptionRecordPageParam));
    }

    /**
     * 后台账户余额&会员数统计
     * @return
     */
    @Operation(summary = "后台账户余额&会员数统计")
    @SaCheckPermission("/biz/consumptionrecord/queryBalanceTotal")
    @GetMapping("/biz/consumptionrecord/queryBalanceTotal")
    public CommonResult<UserBalanceResult> queryBalanceTotal(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.queryBalanceTotal(consumptionRecordPageParam));
    }

    /**
     * 后台消费金额订单数折线图
     * @return
     */
    @Operation(summary = "后台消费金额订单数折线图")
    @SaCheckPermission("/biz/consumptionrecord/queryConsumptionChart")
    @GetMapping("/biz/consumptionrecord/queryConsumptionChart")
    public CommonResult<Map<String,Object>> queryConsumptionChart(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.queryConsumptionChart(consumptionRecordPageParam));
    }

    /**
     * 后台充值金额订单数折线图
     * @return
     */
    @Operation(summary = "后台充值金额订单数折线图")
    @SaCheckPermission("/biz/consumptionrecord/queryRechargeChar")
    @GetMapping("/biz/consumptionrecord/queryRechargeChar")
    public CommonResult<Map<String,Object>> queryRechargeChar(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.queryRechargeChar(consumptionRecordPageParam));
    }

    @Operation(summary = "后台充值金额统计")
    @SaCheckPermission("/biz/consumptionrecord/queryRechargeTotal")
    @GetMapping("/biz/consumptionrecord/queryRechargeTotal")
    public CommonResult<Map<String,Object>> queryRechargeTotal(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.queryRechargeTotal(consumptionRecordPageParam));
    }

    /**
     * 各个门店消费记录
     * @return
     */
    @Operation(summary = "各个门店消费记录")
    @SaCheckPermission("/biz/consumptionrecord/queryEachStore")
    @GetMapping("/biz/consumptionrecord/queryEachStore")
    public CommonResult<Map<String,Object>> queryEachStore(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.queryEachStore(consumptionRecordPageParam));
    }

    /**
     * 导出汇总统计报表
     */
    @Operation(summary = "导出汇总统计报表")
    @SaCheckPermission("/biz/consumptionrecord/exportRecord")
    @GetMapping(value = "/biz/consumptionrecord/exportRecord", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportRecord(ConsumptionRecordPageParam consumptionRecordPageParam, HttpServletResponse response) throws IOException {
        consumptionRecordService.exportRecord(consumptionRecordPageParam,response);
    }

    /**
     * 预警记录
     * @param consumptionRecordPageParam
     * @return
     */
    @Operation(summary = "预警记录")
    @SaCheckPermission("/biz/consumptionrecord/warnPage")
    @GetMapping("/biz/consumptionrecord/warnPage")
    public CommonResult<Page<BizMemberUserResult>> warnPage(ConsumptionRecordPageParam consumptionRecordPageParam) {
        return CommonResult.data(consumptionRecordService.warnPage(consumptionRecordPageParam));
    }
}
