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

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.RepeatSubmit;
import com.seer.teach.mall.order.controller.req.OrderChildDeliveryReq;
import com.seer.teach.mall.order.service.IAdminOrderChildService;
import com.seer.teach.mall.request.OrderPageListReq;
import com.seer.teach.mall.request.SenderConfigReq;
import com.seer.teach.mall.response.OrderChildResp;
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.RestController;

/**
 * <p>
 * 后端子订单管理接口
 * </p>
 *
 * @author Captain
 * @since 2025-06-10
 */
@Tag(name = "管理端 - 子订单管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/order/child")
public class AdminOrderChildController {

    private final IAdminOrderChildService adminOrderChildService;

    @Operation(summary = "子订单详情")
    @GetMapping("/{OrderChildId}")
    @SaCheckPermission("admin:order:detail")
    public ResultBean<OrderChildResp> orderDetail(
            @Parameter(description = "订单id") @PathVariable("OrderChildId") Integer OrderChildId) {
        return ResultBean.success(adminOrderChildService.getOrderChildDetail(OrderChildId));
    }

    @Operation(summary = "子订单分页列表")
    @PostMapping("/child/paged")
    @SaCheckPermission("admin:order:child:paged")
    public ResultBean<PageListBean<OrderChildResp>> getChildOrderPage(@RequestBody @Validated OrderPageListReq params) {
        return ResultBean.success(adminOrderChildService.getChildOrderPage(params));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "子订单发货中，请稍后"
    )
    @Operation(summary = "子发货订单")
    @PutMapping("/{orderChildId}/{logisticsCode}/delivery")
    @SaCheckPermission("admin:order:delivery")
    public ResultBean<Boolean> deliveryOrder(
            @Parameter(description = "子订单Id") @PathVariable("orderChildId") Integer orderChildId,
            @RequestBody OrderChildDeliveryReq orderChildDeliveryReq) {
        return ResultBean.success(adminOrderChildService.deliveryOrder(orderChildId, orderChildDeliveryReq));
    }
}