package com.seer.teach.mall.app.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.annotation.RepeatSubmit;
import com.seer.teach.common.enums.OrderStatusEnum;
import com.seer.teach.mall.app.order.service.IAppOrderService;
import com.seer.teach.mall.request.OrderPageListReq;
import com.seer.teach.mall.request.OrderUpdateReq;
import com.seer.teach.mall.request.OrderReq;
import com.seer.teach.mall.request.OrderSettlementReq;
import com.seer.teach.mall.response.OrderChildResp;
import com.seer.teach.mall.response.OrderResp;
import com.seer.teach.mall.response.OrderSettlementRespVO;
import com.seer.teach.mall.service.IOrderQueryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  微信小程序用户订单控制器
 * </p>
 *
 * @author Captain
 * @since 2025-06-10
 */
@LogPrint
@Tag(name = "APP - 用户订单")
@Validated
@RestController
@RequestMapping("/app/user/order")
@RequiredArgsConstructor
@DecryptionAnnotation
@EncryptionAnnotation
@SaCheckLogin(type = StpUtil.TYPE)
public class AppOrderController {

    private final IAppOrderService appOrderService;

    private final IOrderQueryService orderQueryService;

    @PostMapping("/settlement")
    @SaCheckPermission("app:order:settlement")
    @Operation(summary = "获得订单结算信息")
    public ResultBean<OrderSettlementRespVO> getSettlementOrderDetail(@RequestBody OrderSettlementReq orderSettlementReq){
        return ResultBean.success(appOrderService.getSettlementOrderDetail(orderSettlementReq));
    }

    @Operation(summary = "我的订单详情")
    @GetMapping("/{orderId}")
    @SaCheckPermission("app:order:detail")
    public ResultBean<OrderResp> myOrderDetail(
            @Parameter(description = "订单id") @PathVariable("orderId") Integer orderId,@RequestParam(value = "childOrderId",required = false) Integer childOrderId) {
        return ResultBean.success(orderQueryService.getOrderDetail(orderId));
    }

    @Operation(summary = "子订单详情")
    @GetMapping("/child/{childOrderId}")
    @SaCheckPermission("app:order:child:detail")
    public ResultBean<OrderResp> myChildOrderDetail(@Parameter(description = "子订单id") @PathVariable(value = "childOrderId") Integer childOrderId) {
        return ResultBean.success(orderQueryService.myOrderChildDetail(childOrderId));
    }

    @Operation(summary = "用户订单分页列表")
    @PostMapping("/list")
    @SaCheckPermission("app:order:list")
    public ResultBean<PageListBean<OrderResp>> getOrderPage(@RequestBody @Validated OrderPageListReq params) {
        int userId = StpUtil.getLoginIdAsInt();
        params.setOrderStatusList(OrderStatusEnum.getAppOrderStatusList());
        return ResultBean.success(orderQueryService.getOrderPage(params,userId,null));
    }

    @Operation(summary = "子订单分页列表")
    @PostMapping("/children/paged")
    @SaCheckPermission("app:order:child:paged")
    public ResultBean<PageListBean<OrderChildResp>> pageList(@RequestBody @Validated OrderPageListReq params) {
        params.setUserId(StpUtil.getLoginIdAsInt());
        return ResultBean.success(orderQueryService.getOrderChildrenPage(params));
    }

    @Operation(summary = "收货人订单分页列表")
    @PostMapping("/delivery/paged")
    @SaCheckPermission("app:order:delivery:paged")
    public ResultBean<PageListBean<OrderResp>> getDeliveryOrders(@RequestBody @Validated OrderPageListReq params) {
        int userId = StpUtil.getLoginIdAsInt();
        return ResultBean.success(orderQueryService.getOrderPage(params,null,userId));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单创建中，请勿重复点击"
    )
    @Operation(summary = "创建订单")
    @PostMapping("/createOrder")
    @SaCheckPermission("app:order:create")
    public ResultBean<String> createOrder(@RequestBody OrderReq order) {
        return ResultBean.success(appOrderService.createOrder(order));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单取消中，请稍后"
    )
    @Operation(summary = "取消订单")
    @PutMapping("/cancel")
    @SaCheckPermission("app:order:cancel")
    public ResultBean<Boolean> cancelOrder(@RequestParam("orderSn") String orderSn) {
        return ResultBean.success(appOrderService.cancelOrder(orderSn));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单取消中，请稍后"
    )
    @Operation(summary = "取消订单")
    @PutMapping("/child/cancel")
    @SaCheckPermission("app:order:child:cancel")
    public ResultBean<Boolean> cancelChildOrder(@RequestParam(value = "childOrderSn") String  childOrderSn) {
        return ResultBean.success(appOrderService.cancelChildOrder(childOrderSn));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单删除，请稍后"
    )
    @Operation(summary = "删除订单")
    @PostMapping("/delete")
    @SaCheckPermission("app:order:delete")
    public ResultBean<Boolean> deleteOrder(@RequestBody OrderUpdateReq orderUpdateReq) {
        return ResultBean.success(appOrderService.deleteOrder(orderUpdateReq));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单修改中，请稍后"
    )
    @Operation(summary = "修改订单")
    @PutMapping("/update")
    @SaCheckPermission("app:order:update")
    public ResultBean<Boolean> updateOrder(@RequestBody OrderReq orderReq) {
        return ResultBean.success(appOrderService.updateOrder(orderReq));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "确认收获中，请稍后"
    )
    @Operation(summary = "确认交易订单收货")
    @PutMapping("/receive")
    @SaCheckPermission("app:order:receive")
    public ResultBean<Boolean> receiveOrder(
            @RequestBody OrderUpdateReq orderUpdateReq) {
        return ResultBean.success(appOrderService.receiveOrder(orderUpdateReq));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单发货中，请稍后"
    )
    @Operation(summary = "仓库发送订单(经销商的仓管使用)", description = "只有经销商的仓管才使用此接口")
    @PutMapping("/delivery")
    @SaCheckPermission("app:order:receive")
    public ResultBean<Boolean> deliveryOrder(
            @RequestBody OrderUpdateReq orderUpdateReq) {
        return ResultBean.success(appOrderService.deliveryOrder(orderUpdateReq));
    }

}