package com.tmerclub.cloud.cuerecycle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.api.order.constant.OrderStatus;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.SendTypeEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.vo.SendNotifyBO;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.cuerecycle.manager.CreateOrderManager;
import com.tmerclub.cloud.cuerecycle.mapper.CueOrderMapper;
import com.tmerclub.cloud.cuerecycle.model.CueOrder;
import com.tmerclub.cloud.cuerecycle.model.dto.CueOrderDTO;
import com.tmerclub.cloud.cuerecycle.model.dto.CueSaveMailingDTO;
import com.tmerclub.cloud.cuerecycle.model.vo.AdminCueOrderVO;
import com.tmerclub.cloud.cuerecycle.model.vo.AppCueOrderVO;
import com.tmerclub.cloud.cuerecycle.model.vo.CueOrderVO;
import com.tmerclub.cloud.cuerecycle.service.CueOrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 回收订单Service实现类
 *
 * @author : frank
 * @create : 2025-04-15
 */
@Slf4j
@Service
public class CueOrderServiceImpl implements CueOrderService {

    @Resource
    CueOrderMapper cueOrderMapper;
    @Resource
    CreateOrderManager createOrderManager;
    @Resource
    private RocketMQTemplate sendNotifyToUserTemplate;

    @Override
    public PageVO<AdminCueOrderVO> page(PageDTO pageDTO, CueOrderDTO cueOrderDTO) {
        return PageUtil.doPage(pageDTO, () -> cueOrderMapper.list(cueOrderDTO));
    }

    @Override
    public CueOrderVO getById(Long orderId) {
        return cueOrderMapper.getById(orderId);
    }

    @Override
    public int save(CueOrderDTO cueOrderDTO) {
        CueOrder cueOrder = BeanUtil.toBean(cueOrderDTO, CueOrder.class);
        cueOrder.setCreateTime(DateUtil.date());
        return cueOrderMapper.save(cueOrder);
    }

    @Override
    public int updateAuditStatus(CueOrderDTO cueOrderDTO) {
        CueOrder cueOrder = new CueOrder();
        cueOrder.setOrderId(cueOrderDTO.getOrderId());
        cueOrder.setOrderStatus(cueOrderDTO.getOrderStatus());
        if (Objects.equals(cueOrderDTO.getOrderStatus(), OrderStatus.CUE_AUDIT_PASS.value())) {
            cueOrder.setEstimatedAmount(cueOrderDTO.getEstimatedAmount());
        }
        cueOrder.setUpdateTime(DateUtil.date());
        // 发送消息
        syncSend(cueOrderDTO);
        return cueOrderMapper.update(cueOrder);
    }

    @Override
    public int updateEvaluateStatus(CueOrderDTO cueOrderDTO) {
        CueOrder cueOrder = new CueOrder();
        cueOrder.setOrderId(cueOrderDTO.getOrderId());
        cueOrder.setOrderStatus(cueOrderDTO.getOrderStatus());
        if (Objects.equals(cueOrderDTO.getOrderStatus(), OrderStatus.CUE_EVALUATION_SUCCESS.value())) {
            cueOrder.setActualAmount(cueOrderDTO.getActualAmount());
            // 寄售需要填写服务费和佣金
            if (cueOrderDTO.getCueOrderType().equals(3)) {
                cueOrder.setServiceCharge(cueOrderDTO.getServiceCharge());
                cueOrder.setBrokerage(cueOrderDTO.getBrokerage());
            } else {
                cueOrder.setVoucherAmount(cueOrderDTO.getVoucherAmount());
            }
        }
        cueOrder.setUpdateTime(DateUtil.date());
        // 发送消息
        syncSend(cueOrderDTO);
        return cueOrderMapper.update(cueOrder);
    }

    private void syncSend(CueOrderDTO cueOrderDTO) {
        // 修改系统订单的状态
        createOrderManager.updateOrderMall(null, cueOrderDTO.getMallOrderId(), cueOrderDTO.getOrderStatus());
        // 发送消息
        List<SendNotifyBO> notifyList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        SendNotifyBO sendNotifyBO = new SendNotifyBO();
        sendNotifyBO.setSendType(SendTypeEnum.CUE_ORDER_AUDIT_RESULT.getValue());
        sendNotifyBO.setUserId(cueOrderDTO.getUserId());
        sendNotifyBO.setShopId(Constant.PLATFORM_SHOP_ID);
        notifyList.add(sendNotifyBO);
        SendStatus sendStockStatus = sendNotifyToUserTemplate.syncSend(RocketMqConstant.SEND_NOTIFY_TO_USER_TOPIC, new GenericMessage<>(notifyList)).getSendStatus();
        if (!Objects.equals(sendStockStatus, SendStatus.SEND_OK)) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
    }

    @Override
    public int delete(Long id) {
        CueOrderVO cueOrderVO = cueOrderMapper.getById(id);
        CueOrder cueOrder = BeanUtil.toBean(cueOrderVO, CueOrder.class);
        cueOrder.setUpdateTime(DateUtil.date());
        cueOrder.setDeleted(1);
        return cueOrderMapper.update(cueOrder);
    }

    @Override
    public AppCueOrderVO getByAppCueOrderId(Long orderId) {
        CueOrderVO cueOrderVO = cueOrderMapper.getById(orderId);
        return BeanUtil.toBean(cueOrderVO, AppCueOrderVO.class);
    }

    @Override
    public int saveMailing(CueSaveMailingDTO cueSaveMailingDTO) {
        // 修改系统订单的状态
        createOrderManager.updateOrderMall(null, cueSaveMailingDTO.getMallOrderId(), OrderStatus.CUE_FINISH_DELIVERY.value());
        // 修改回收单状态
        CueOrder cueOrder = BeanUtil.toBean(cueSaveMailingDTO, CueOrder.class);
        cueOrder.setOrderStatus(OrderStatus.CUE_FINISH_DELIVERY.value());
        cueOrder.setDeliveryTime(DateUtil.date());
        return cueOrderMapper.update(cueOrder);
    }
}