package com.seer.teach.mall.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.LogPrint;
import com.seer.teach.common.annotation.RepeatSubmit;
import com.seer.teach.mall.order.service.IAdminOrderService;
import com.seer.teach.mall.request.OrderConsigneeUpdateReq;
import com.seer.teach.mall.request.OrderPageListReq;
import com.seer.teach.mall.request.OrderReq;
import com.seer.teach.mall.request.SenderConfigReq;
import com.seer.teach.mall.response.OrderResp;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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;

import jakarta.validation.Valid;

/**
 * <p>
 * 商城订单 前端控制器
 * </p>
 *
 * @author Captain
 * @since 2025-06-10
 */
@Slf4j
@RestController
@RequestMapping("/mall/order")
@Tag(name = "管理端 - 商城订单")
@Validated
@RequiredArgsConstructor
@SaCheckLogin(type = StpUtil.TYPE)
public class AdminOrderController {

    private final IAdminOrderService adminOrderService;

    private final IOrderQueryService orderQueryService;

    @Operation(summary = "订单详情")
    @GetMapping("/{orderId}")
    @SaCheckPermission("admin:order:detail")
    public ResultBean<OrderResp> orderDetail(
            @Parameter(description = "订单id") @PathVariable("orderId") Integer orderId) {
        return ResultBean.success(orderQueryService.getOrderDetail(orderId));
    }

    @Operation(summary = "订单分页列表")
    @PostMapping("/paged")
    @SaCheckPermission("admin:order:list")
    public ResultBean<PageListBean<OrderResp>> pageList(@RequestBody @Validated OrderPageListReq params) {
        return ResultBean.success(orderQueryService.getOrderPage(params,null,null));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单修改中，请稍后"
    )
    @Operation(summary = "修改订单", description = "仅能修改用户的收获信息及备注")
    @PutMapping("/updateOrder")
    @SaCheckPermission("admin:order:update")
    public ResultBean<Boolean> updateOrder(@RequestBody OrderConsigneeUpdateReq orderReq) {
        return ResultBean.success(adminOrderService.updateOrder(orderReq));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单关闭中，请稍后"
    )
    @Operation(summary = "关闭订单")
    @PutMapping("/closeOrder")
    @SaCheckPermission("admin:order:close")
    public ResultBean<Boolean> closeOrder(@RequestBody OrderReq orderReq) {
        return ResultBean.success(adminOrderService.closeOrder(orderReq));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单删除中，请稍后"
    )
    @Operation(summary = "删除订单")
    @DeleteMapping("/delete")
    @SaCheckPermission("admin:order:delete")
    public ResultBean<Boolean> deleteOrder(@RequestBody OrderReq orderReq) {
        return ResultBean.success(adminOrderService.deleteOrder(orderReq));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单发货中，请稍后"
    )
    @Operation(summary = "发货订单", description = "需要选择寄件人信息")
    @PutMapping("/{orderId}/{logisticsCode}/delivery")
    @SaCheckPermission("admin:order:delivery")
    public ResultBean<Boolean> deliveryOrder(
            @Parameter(description = "订单Id") @PathVariable("orderId") Integer orderId,
            @Parameter(description = "物流公司编码,现仅支持极兔，编码为jt") @PathVariable("logisticsCode") String logisticsCode,
            @RequestBody SenderConfigReq sender) {
        return ResultBean.success(adminOrderService.deliveryOrder(orderId, logisticsCode, sender));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单确认中，请稍后"
    )
    @Operation(summary = "确认订单(经销商订单)", description = "经销商提货的订单，需要管理员确认审核")
    @PutMapping("/{orderId}/confirm")
    @SaCheckPermission("admin:order:delivery")
    public ResultBean<Boolean> confirm(
            @Parameter(description = "订单Id") @PathVariable("orderId") Integer orderId,
            @Parameter(description = "订单状态") @RequestParam("orderStatus") Integer orderStatus,
            @Parameter(description = "发货人ID") @RequestParam("deliveryUserId") Integer deliveryUserId) {
        return ResultBean.success(adminOrderService.confirm(orderId, orderStatus, deliveryUserId));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "订单确认-通知，请稍后"
    )
    @Operation(summary = "确认订单(经销商订单)", description = "经销商提货的订单，需要管理员确认审核")
    @PutMapping("/{orderId}/confirm-notify")
    @SaCheckPermission("admin:order:delivery")
    public ResultBean<Boolean> confirmAndNotify(
            @Parameter(description = "订单Id") @PathVariable("orderId") Integer orderId,
            @Parameter(description = "订单状态") @RequestParam("orderStatus") Integer orderStatus,
            @Parameter(description = "发货人ID") @RequestParam("deliveryUserId") Integer deliveryUserId,
            @Parameter(description = "业务场景编码") @RequestParam("businessSceneCode") String businessSceneCode) {
        return ResultBean.success(adminOrderService.confirmAndNotify(orderId, orderStatus, deliveryUserId, businessSceneCode));
    }
}