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

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.model.dto.CueAgreeOrderDTO;
import com.tmerclub.cloud.cuerecycle.model.dto.CueFillAddrDTO;
import com.tmerclub.cloud.cuerecycle.model.dto.CueSaveMailingDTO;
import com.tmerclub.cloud.cuerecycle.model.vo.AppCueOrderVO;
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.*;

/**
 * 回收订单
 *
 * @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>
     */
    @PostMapping("/createOrder")
    @Operation(summary = "创建订单", description = "创建订单")
    public ServerResponseEntity<Long> 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(orderId);
    }

    /**
     * 根据订单id查询订单信息
     *
     * @param orderId 订单id
     * @return ServerResponseEntity<CueOrderVO>
     */
    @GetMapping("/getOrderById")
    @Operation(summary = "根据订单id查询订单信息", description = "根据订单id查询订单信息")
    public ServerResponseEntity<AppCueOrderVO> getOrderById(@RequestParam("orderId") Long orderId) {
        AppCueOrderVO appCueOrderVO = cueOrderService.getByAppCueOrderId(orderId);
        return ServerResponseEntity.success(appCueOrderVO);
    }

    /**
     * 邮寄填写订单
     *
     * @param cueSaveMailingDTO 提交参数
     * @return 结果
     */
    @PostMapping("/saveMailing")
    @Operation(summary = "邮寄填写订单", description = "邮寄填写订单")
    public ServerResponseEntity<Void> saveMailing(@Valid @RequestBody CueSaveMailingDTO cueSaveMailingDTO) {
        int update = cueOrderService.saveMailing(cueSaveMailingDTO);
        return update > 0 ? ServerResponseEntity.success() : ServerResponseEntity.showFailMsg("提交失败");
    }


    /**
     * 同意回收
     *
     * @param cueAgreeOrderDTO 同意回收参数
     * @return 结果
     */
    @PostMapping("/agreeOrder")
    @Operation(summary = "同意回收", description = "同意回收")
    public ServerResponseEntity<Void> agreeOrder(@Valid @RequestBody CueAgreeOrderDTO cueAgreeOrderDTO) {
        createOrderManager.agreeOrder(cueAgreeOrderDTO);
        return ServerResponseEntity.success();
    }

    /**
     * 填写不合格或不满意回邮地址
     *
     * @param cueFillAddrDTO 回邮地址入参
     * @return 结果
     */
    @PostMapping("/fillAddr")
    @Operation(summary = "填写回邮地址", description = "填写回邮地址")
    public ServerResponseEntity<Void> fillAddr(@Valid @RequestBody CueFillAddrDTO cueFillAddrDTO) {
        createOrderManager.fillAddr(cueFillAddrDTO);
        return ServerResponseEntity.success();
    }
}