package com.ruoyi.cai.mq.handle;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.ruoyi.cai.domain.Gift;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.mq.dto.CommonConsumerEnum;
import com.ruoyi.cai.mq.handle.dto.WindowGiftDTO;
import com.ruoyi.cai.notice.YunxinHttpService;
import com.ruoyi.cai.notice.data.child.SendGiftWindowsAmountNoticeData;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.cai.util.CaiDateUtil;
import com.ruoyi.framework.OnlineTodayCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
public class WindowGiftHandle implements IHandle{

    @Autowired
    private YunxinHttpService yunxinHttpService;
    @Autowired
    private OnlineTodayCache onlineTodayCache;
    @Autowired
    private UserService userService;

    @Override
    public void run(String message) {
        WindowGiftDTO windowGift = JSON.parseObject(message, WindowGiftDTO.class);
        User fromUser = userService.getById(windowGift.getFromId());
        User toUser = userService.getById(windowGift.getToId());
        Gift gift = windowGift.getGift();
        Set<Long> userIds = onlineTodayCache.getAllOnlineToday();
        List<Long> userIdList = new ArrayList<>(userIds);
        List<List<Long>> lists = Lists.partition(userIdList, 300);
        for (List<Long> list : lists) {
            SendGiftWindowsAmountNoticeData data = new SendGiftWindowsAmountNoticeData();
            data.setId(fromUser.getId());
            data.setNickname(fromUser.getNickname());
            data.setAvatar(fromUser.getAvatar());
            data.setToid(toUser.getId());
            data.setTonickname(toUser.getNickname());
            data.setToavatar(toUser.getAvatar());
            data.setAmount(gift.getPrice());
            data.setGiftname(gift.getName());
            data.setGiftimg(gift.getImg());
            data.setGifttotal(windowGift.getGiftNum());
            data.setTime(System.currentTimeMillis());
            yunxinHttpService.sendGiftWindowsAmount(list,data);
        }

    }

    @Override
    public CommonConsumerEnum getType() {
        return CommonConsumerEnum.WINDOW_GIFT;
    }
}
