package com.ruoyi.cai.mq.handle;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.ruoyi.cai.mq.dto.CommonConsumerEnum;
import com.ruoyi.cai.mq.handle.dto.WindowRechargeDTO;
import com.ruoyi.cai.notice.YunxinHttpService;
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 WindowRechargeHandle implements IHandle{

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

    @Override
    public void run(String message) {
        WindowRechargeDTO windowRecharge = JSON.parseObject(message, WindowRechargeDTO.class);
        Set<Long> userIds = onlineTodayCache.getAllOnlineToday();
        List<Long> userIdList = new ArrayList<>(userIds);
        List<List<Long>> lists = Lists.partition(userIdList, 300);
        for (List<Long> list : lists) {
            yunxinHttpService.sendRechargeWindowsAmount(list,windowRecharge);
        }
    }

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