package com.tmerclub.cloud.cuerecycle.controller.app;

import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.cache.constant.OrderCacheNames;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.cuerecycle.manager.CreateOrderManager;
import com.tmerclub.cloud.cuerecycle.model.dto.CreateOrderDTO;
import com.tmerclub.cloud.cuerecycle.service.CueOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 回收订单
 *
 * @author : frank
 * @create : 2025-04-15
 */
@Tag(name = "App-回收订单")
@RestController("appCueOrderController")
@RequestMapping("/app/cueOrder")
public class CueOrderController {
    @Resource
    CueOrderService cueOrderService;
    @Resource
    CreateOrderManager createOrderManager;

    /**
     * 创建订单
     *
     * @param createOrderDTO 创建订单参数
     * @return ServerResponseEntity<CueOrderVO>
     */
    @Operation(summary = "创建订单", description = "创建订单")
    @PostMapping("/createOrder")
    public ServerResponseEntity<Void> createOrder(@Valid @RequestBody CreateOrderDTO createOrderDTO) {
        // 防止重复提交 TODO：不清楚为啥不好使
//        boolean cad = RedisUtil.cad(OrderCacheNames.ORDER_CONFIRM_UUID_KEY + CacheNames.UNION + createOrderDTO.getUserId(), String.valueOf(createOrderDTO.getUserId()));
//        if (!cad) {
//            return ServerResponseEntity.fail(ResponseEnum.REPEAT_ORDER);
//        }
        // 保存订单信息
        Long orderId = createOrderManager.saveOrder(createOrderDTO);
        // 插入瑕疵信息
        Long flawPrice = createOrderManager.addFlawItem(orderId, createOrderDTO);
        // 向商城order插入信息
        createOrderManager.saveOrderToMall(orderId, createOrderDTO.getProductPrice() - flawPrice, createOrderDTO);
        return ServerResponseEntity.success();
    }
}