package com.czg.controller.admin;

import com.czg.annotation.Debounce;
import com.czg.order.dto.*;
import com.czg.order.entity.OrderInfo;
import com.czg.order.service.OrderInfoCustomService;
import com.czg.order.vo.HistoryOrderPrintVo;
import com.czg.order.vo.HistoryOrderVo;
import com.czg.order.vo.OrderInfoVo;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.order.service.OrderPayService;
import com.czg.utils.AssertUtil;
import com.czg.utils.ServletUtil;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 订单管理
 *
 * @author ww
 * @description
 */
@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Resource
    private OrderInfoCustomService orderInfoService;
    @Resource
    private OrderPayService orderPayService;

    /**
     * 订单列表
     */
    @GetMapping
    public CzgResult<Page<OrderInfoVo>> getOrderPage(OrderInfoQueryDTO queryDTO) {
        queryDTO.setShopId(StpKit.USER.getShopId());
        return CzgResult.success(orderInfoService.getOrderByPage(queryDTO));
    }

    @GetMapping("/getOrderById")
    public CzgResult<HistoryOrderVo> getOrderById(Long orderId) {
        return orderInfoService.getOrderDetails(orderId);
    }

    @GetMapping("/getOrderByIdPrint")
    public CzgResult<HistoryOrderPrintVo> getOrderByIdPrint(Long orderId) {
        return orderInfoService.getOrderByIdPrint(orderId);
    }

    @GetMapping("/historyOrder")
    public CzgResult<HistoryOrderVo> historyOrder(
            @RequestParam(required = false) Long orderId,
            @RequestParam(required = false) String tableCode) {
        return CzgResult.success(orderInfoService.historyOrder(orderId, tableCode));
    }

    //@SaStaffCheckPermission("yun_xu_xia_dan")
    @PostMapping("/createOrder")
    @Debounce(value = "#addDto.tableCode")
    public CzgResult<OrderInfo> createOrder(@Validated @RequestBody OrderInfoAddDTO addDto) {
        addDto.setPlatformType(ServletUtil.getHeaderIgnoreCase(ServletUtil.getRequest(), "platformType"));
        addDto.setStaffId(StpKit.USER.getLoginIdAsLong());
        addDto.setShopId(StpKit.USER.getShopId());
        addDto.setOrderType("cash");
        return CzgResult.success(orderInfoService.createOrder(addDto));
    }

    /**
     * 起菜, 上菜
     */
    @PutMapping("/upOrderDetail")
    public CzgResult<Boolean> upOrderDetail(@RequestBody OrderDetailStatusDTO detailStatusDTO) {
        return CzgResult.success(orderInfoService.upOrderDetail(StpKit.USER.getShopId(), detailStatusDTO));
    }

    /**
     * 转桌
     */
    @PostMapping("/mergeOrder")
    public CzgResult<Object> mergeOrder(@Validated @RequestBody MergeOrderDTO param) {
        return orderInfoService.mergeOrder(param);
    }

    /**
     * 订单全额退款 只传订单id
     * 部分退款 传参refundDetail
     */
    //@SaStaffCheckPermission("yun_xu_tui_kuan")
    @PostMapping("/refundOrder")
    @Debounce(value = "#refundDTO.orderId")
    public CzgResult<Object> refundOrder(@Validated @RequestBody OrderInfoRefundDTO refundDTO) {
        return orderPayService.refundOrderBefore(refundDTO);
    }

    /**
     * 订单打印
     */
    @PostMapping("/print")
    @Debounce(value = "#orderInfoPrintDTO.id")
    public CzgResult<Boolean> printOrder(@Validated @RequestBody OrderInfoPrintDTO orderInfoPrintDTO) {
        return CzgResult.success(orderInfoService.printOrder(StpKit.USER.getShopId(), orderInfoPrintDTO));
    }

    @PostMapping("/cancelOrder")
    public CzgResult<Void> cancelOrder(@Validated @RequestBody OrderCannelDTO param) {
        orderInfoService.cancelledOrder(param.getShopId(), param.getOrderId());
        return CzgResult.success();
    }

    @PostMapping("/rmPlaceOrder")
    public CzgResult<Void> cancelledPlaceOrder(@Validated @RequestBody OrderCannelDTO param) {
        AssertUtil.isNull(param.getPlaceNum(), "{}不能为空", "取消单次");
        orderInfoService.cancelledPlaceOrder(param.getShopId(), param.getOrderId(), param.getPlaceNum());
        return CzgResult.success();
    }
}
