package com.ruoyi.cai.manager;

import com.ruoyi.cai.domain.ConsumeLog;
import com.ruoyi.cai.dto.ConsumeResp;
import com.ruoyi.cai.dto.app.query.GiveGiftRes;
import com.ruoyi.cai.dto.app.query.GiveGuardReq;
import com.ruoyi.cai.dto.video.VideoSettleResp;
import com.ruoyi.cai.mq.AmqpProducer;
import com.ruoyi.cai.rank.RankManager;
import com.ruoyi.cai.service.AccountService;
import com.ruoyi.cai.service.GuardTotalService;
import com.ruoyi.cai.service.RechargeOrderService;
import com.ruoyi.cai.service.UserGiftService;
import com.ruoyi.cai.ws.bean.Room;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ConsumerManager {
    @Autowired
    private GuardTotalService guardTotalService;
    @Autowired
    private UserGiftService userGiftService;
    @Autowired
    private AmqpProducer amqpProducer;
    @Autowired
    private RechargeOrderService rechargeOrderService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private RankManager rankManager;

    public ConsumeResp sendGuard(GiveGuardReq query){
        ConsumeResp resp = guardTotalService.giveGuard(query);
        if(resp.isSuccess()){
            try {
                amqpProducer.sendCalculateSales(resp.getConsumeLogId()+"");
            }catch (Exception e){
                log.error("RabbitMq 发送失败, 守护分销流程流转失败！",e);
            }
        }
        return resp;
    }

    public ConsumeResp sendGift(GiveGiftRes query){
        ConsumeResp resp = userGiftService.giveGift(query);
        if(resp.isSuccess()){
            try {
                amqpProducer.sendCalculateSales(resp.getConsumeLogId()+"");
            }catch (Exception e){
                log.error("RabbitMq 发送失败, 礼物分销流程流转失败！",e);
            }
        }
        return resp;
    }

    public VideoSettleResp videoSettle(Room room){
        VideoSettleResp resp = accountService.videoSettle(room);
        ConsumeLog consumeLog = resp.getConsumeLog();
        try {
            amqpProducer.sendCalculateSales(consumeLog.getId()+"");
        }catch (Exception e){
            log.error("RabbitMq 发送失败, 视频分销流程流转失败！",e);
        }
        rankManager.sendLoveRankMq(consumeLog.getSourceUserId(),consumeLog.getAnchorAmount(),consumeLog.getTraceId());
        return resp;
    }

    public ConsumeResp rechargeOrderSuccess(String tradeNo){
        ConsumeResp resp = rechargeOrderService.orderSuccess(tradeNo);
        if(resp.isSuccess()){
            try {
                amqpProducer.sendCalculateSales(resp.getConsumeLogId()+"");
            }catch (Exception e){
                log.error("RabbitMq 发送失败, 充值分销流程流转失败！",e);
            }
        }
        return resp;
    }
}
