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.RepeatSubmit;
import com.seer.teach.mall.order.service.IAdminOrderService;
import com.seer.teach.mall.request.ChildOrderSenderReq;
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.response.OrderChildResp;
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.RestController;

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

    private final IAdminOrderService adminOrderService;

    private final IOrderQueryService orderQueryService;

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

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

    @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.updateChildOrder(orderReq,StpUtil.getLoginIdAsLong()));
    }

    @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.closeChildOrder(orderReq,StpUtil.getLoginIdAsLong()));
    }

    @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.deleteChildOrder(orderReq,StpUtil.getLoginIdAsLong()));
    }

    @RepeatSubmit(
            interval = 3000,
            keyType = RepeatSubmit.KeyType.TOKEN,
            message = "子订单发货中，请稍后"
    )
    @Operation(summary = "发货子订单", description = "需要选择寄件人信息")
    @PutMapping("/{childOrderId}/delivery")
    @SaCheckPermission("admin:order:delivery")
    public ResultBean<Boolean> deliveryOrder(
            @Parameter(description = "子订单Id") @PathVariable("childOrderId") Integer childOrderId,
            @RequestBody ChildOrderSenderReq childOrderSenderReq) {
        return ResultBean.success(adminOrderService.deliveryChildOrder(childOrderId, childOrderSenderReq,StpUtil.getLoginIdAsLong()));
    }

}