package com.tmerclub.cloud.cuerecycle.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.api.order.feign.OrderFeignClient;
import com.tmerclub.cloud.common.local.dto.SaveOrderToMallDTO;
import com.tmerclub.cloud.cuerecycle.constant.CueOrderStatus;
import com.tmerclub.cloud.cuerecycle.mapper.CueOrderFlawMapper;
import com.tmerclub.cloud.cuerecycle.mapper.CueOrderMapper;
import com.tmerclub.cloud.cuerecycle.model.CueOrder;
import com.tmerclub.cloud.cuerecycle.model.CueOrderFlaw;
import com.tmerclub.cloud.cuerecycle.model.dto.CreateOrderDTO;
import com.tmerclub.cloud.cuerecycle.model.dto.CreateOrderFlawDTO;
import com.tmerclub.cloud.cuerecycle.service.CueFlawService;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 确认订单管理器
 *
 * @author: frank
 * @create: 2025-04-15
 **/
@Component
public class CreateOrderManager {
    private final Logger log = LoggerFactory.getLogger(CreateOrderManager.class);

    @Resource
    CueFlawService cueFlawService;
    @Resource
    CueOrderMapper cueOrderMapper;
    @Resource
    CueOrderFlawMapper cueOrderFlawMapper;
    @DubboReference
    private OrderFeignClient orderFeignClient;

    /**
     * 保存订单信息
     *
     * @param createOrderDTO 订单信息
     * @return 订单id
     */
    public Long saveOrder(CreateOrderDTO createOrderDTO) {
        // 保存订单信息
        CueOrder cueOrder = BeanUtil.toBean(createOrderDTO, CueOrder.class);
        cueOrder.setOrderStatus(CueOrderStatus.PENDING_AUDIT.value());
        cueOrder.setCreateTime(DateUtil.date());
        cueOrderMapper.save(cueOrder);
        return cueOrder.getOrderId();
    }

    /**
     * 添加瑕疵项
     *
     * @param createOrderDTO 订单信息
     */
    public Long addFlawItem(Long orderId, CreateOrderDTO createOrderDTO) {
        // 循环插入瑕疵信息并累计选择金额
        Long flawPrice = 0L;
        for (CreateOrderFlawDTO createOrderFlawDTO : createOrderDTO.getFlawList()) {
            if (createOrderFlawDTO.getIsFlaw() == 1) {
                // 累加瑕疵金额
                flawPrice += createOrderFlawDTO.getFlawPrice();
            }
            CueOrderFlaw cueOrderFlaw = BeanUtil.toBean(createOrderFlawDTO, CueOrderFlaw.class);
            cueOrderFlaw.setOrderId(orderId);
            cueOrderFlawMapper.save(cueOrderFlaw);
        }
        return flawPrice;
    }

    /**
     * 保存订单到mallOrder
     */
    public void saveOrderToMall(Long orderId, Long estimatedAmount, CreateOrderDTO createOrderDTO) {
        SaveOrderToMallDTO saveOrderToMallDTO = new SaveOrderToMallDTO();
        saveOrderToMallDTO.setUserId(createOrderDTO.getUserId());
        saveOrderToMallDTO.setEstimatedAmount(estimatedAmount);
        saveOrderToMallDTO.setProductName(createOrderDTO.getProductName());
        saveOrderToMallDTO.setProductImages(createOrderDTO.getProductImages());
        Long mallOrderId = orderFeignClient.saveOrderToMall(saveOrderToMallDTO);
        CueOrder cueOrder = new CueOrder();
        cueOrder.setOrderId(orderId);
        cueOrder.setMallOrderId(mallOrderId);
        cueOrder.setEstimatedAmount(estimatedAmount);
        cueOrderMapper.update(cueOrder);
    }
}
