package com.czg.controller.user;

import com.czg.annotation.Debounce;
import com.czg.exception.CzgException;
import com.czg.market.dto.MemberOrderDTO;
import com.czg.market.entity.MemberOrder;
import com.czg.market.service.MemberOrderService;
import com.czg.market.service.OrderInfoService;
import com.czg.order.dto.OrderCannelDTO;
import com.czg.order.dto.OrderInfoAddDTO;
import com.czg.order.dto.OrderInfoQueryDTO;
import com.czg.order.entity.OrderInfo;
import com.czg.order.service.OrderInfoCustomService;
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.enums.OrderStatusEnums;
import com.czg.utils.AssertUtil;
import com.czg.utils.ServletUtil;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 订单管理
 *
 * @author ww
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/user/order")
public class UserOrderController {

    @Resource
    private OrderInfoCustomService orderInfoCustomService;
    @Resource
    private OrderInfoService orderInfoService;
    @Resource
    private MemberOrderService memberOrderService;
    /**
     * 订单列表
     */
    @GetMapping
    public CzgResult<Page<OrderInfoVo>> getOrderPage(OrderInfoQueryDTO queryDTO) {
        queryDTO.setIsDel(0);
        queryDTO.setUserId(StpKit.USER.getLoginIdAsLong());
        return CzgResult.success(orderInfoCustomService.getOrderByPage(queryDTO));
    }

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

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

    /**
     * 生成订单
     */
    @PostMapping("/createOrder")
    @Debounce(value = "#addDto.tableCode")
    public CzgResult<OrderInfo> createOrder(@RequestBody OrderInfoAddDTO addDto) {
        addDto.setPlatformType(ServletUtil.getHeaderIgnoreCase(ServletUtil.getRequest(), "platformType"));
//        addDto.setUserId(StpKit.USER.getLoginIdAsLong());
        addDto.setShopId(StpKit.USER.getShopId());
        addDto.setOrderType("miniapp");
        return CzgResult.success(orderInfoCustomService.createOrder(addDto));
    }

    /**
     * 会员购买
     * @param orderDTO 充值信息
     * @return
     */
    @PostMapping("/joinMember")
    public CzgResult<MemberOrder> joinMember(@Validated @RequestBody MemberOrderDTO orderDTO) {
        orderDTO.setPlatformType(ServletUtil.getHeaderIgnoreCase(ServletUtil.getRequest(), "platformType"));
        orderDTO.setUserId(StpKit.USER.getLoginIdAsLong());
        orderDTO.setOrderType("miniapp");
        return CzgResult.success(memberOrderService.createMemberOrder(orderDTO));
    }


    @PutMapping("/{id}")
    public CzgResult<Void> upOrderIsDel(@PathVariable("id") Long id) {
        //效验数据
        AssertUtil.isNull(id, "{}不能为空", "订单Id");
        OrderInfo orderInfo = orderInfoService.getById(id);
        AssertUtil.isNull(orderInfo, "删除失败订单不存在");
        if (orderInfo.getStatus().equals(OrderStatusEnums.UNPAID.getCode())) {
            throw new CzgException("待支付订单不可删除");
        }
        orderInfoService.updateById(new OrderInfo().setIsDel(1).setId(id));
        return CzgResult.success();
    }

    @PostMapping("/cancelOrder")
    @Debounce(value = "#param.orderId")
    public CzgResult<Void> cancelOrder(@Validated @RequestBody OrderCannelDTO param) {
        orderInfoCustomService.cancelledOrder(param.getShopId(), param.getOrderId());
        return CzgResult.success();
    }

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