package com.zhongshu.card.server.core.controller.payment;

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.helper.AuthHelper;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.types.OrderModeType;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.payment.*;
import com.zhongshu.card.client.model.payment.statistic.BusinessMainStatisticModel;
import com.zhongshu.card.client.model.payment.statistic.StatisticSearch;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.openApi.params.PaymentNoParam;
import com.zhongshu.card.client.service.payment.ExpenseFlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 学校 通讯录管理
 *
 * @author TRX
 * @date 2024/6/5
 */
@RestController
@RequestMapping("/order/expenseFlow")
@Tag(name = "订单--消费流水")
public class ExpenseFlowController {

    @Autowired
    ExpenseFlowService expenseFlowService;

    @Autowired
    AuthHelper authHelper;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "消费列表-分页查询（当前用户）", description = "消费列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<ExpenseFlowModel>> page(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) ExpenseFlowSearch param) {
        param.setIsPaySuccess(Boolean.TRUE);
        param.setOrderModeType(OrderModeType.Consume);
        param.setUserId(authHelper.getCurrentUser().getUserId());
        return expenseFlowService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "消费列表-分页查询（商户的 小程序端用）", description = "消费列表-分页查询")
    @RequestMapping(value = {"busPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<ExpenseFlowModel>> busPage(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) ExpenseFlowSearch param) {
        return expenseFlowService.busPage(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "消费详情", description = "消费详情")
    @RequestMapping(value = "getDetail", method = {RequestMethod.POST})
    public ResultContent<ExpenseFlowAreaModel> getDetail(@RequestBody IDParam param) {
        return this.expenseFlowService.getDetail(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "消费详情--支付订单号查询", description = "消费详情")
    @RequestMapping(value = "getDetailByPaymentNo", method = {RequestMethod.POST})
    public ResultContent<ExpenseFlowAreaModel> getDetailByPaymentNo(@RequestBody PaymentNoParam param) {
        return this.expenseFlowService.getDetailByPaymentNo(param.getPaymentNo());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "统计消费总数（当前用户）", description = "统计消费总数")
    @RequestMapping(value = {"countPayment"}, method = {RequestMethod.POST})
    public ResultContent<ExpenseFlowCount> countPayment(@RequestBody ExpenseFlowSearch param) {
        param.setIsPaySuccess(Boolean.TRUE);
        param.setOrderModeType(OrderModeType.Consume);
        param.setUserId(authHelper.getCurrentUser().getUserId());
        return expenseFlowService.countUserPayment(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "统计消费总数（商户的）", description = "统计消费总数")
    @RequestMapping(value = {"countBusPayment"}, method = {RequestMethod.POST})
    public ResultContent<ExpenseFlowCount> countBusPayment(@RequestBody ExpenseFlowSearch param) {
        param.setIsPaySuccess(Boolean.TRUE);
        param.setOrderModeType(OrderModeType.Consume);
        param.setUserId("");
        return expenseFlowService.countBusPayment(param);
    }


    //------------------------商户订单相关功能 start--------------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "商户订单查询", description = "商户订单查询询")
    @RequestMapping(value = {"pageBusMainOrder"}, method = {RequestMethod.POST})
    public ResultContent<Page<ExpenseFlowModel>> pageBusMainOrder(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) ExpenseFlowSearch param) {
        return expenseFlowService.pageBusMainOrder(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "商家直接消费订单退款", description = "商家直接消费订单退款")
    @RequestMapping(value = {"expenseShopRefund"}, method = {RequestMethod.POST})
    public ResultContent expenseShopRefund(@RequestBody ExpenseRefundParam param) {
        return expenseFlowService.expenseShopRefund(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "统计商户收入总数", description = "统计商户收入总数")
    @RequestMapping(value = {"countBusinessMainPayment"}, method = {RequestMethod.POST})
    public ResultContent<ExpenseFlowCount> countBusinessMainPayment(@RequestBody ExpenseFlowSearch param) {
        return expenseFlowService.countBuinessMainPayment(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "商户小程序首页统计", description = "商户小程序首页统计")
    @RequestMapping(value = {"businessMainPageStatistic"}, method = {RequestMethod.POST})
    public ResultContent<BusinessMainStatisticModel> businessMainPageStatistic(
            @RequestBody StatisticSearch param) {
        return expenseFlowService.businessMainPageStatistic(param);
    }


    //------------------------商户订单相关功能 end----------------------------

}
