package com.zswl.cloud.springBtach.server.core.timer;

import com.github.microservice.auth.client.content.ResultContent;
import com.zhongshu.payment.client.model.order.v2.GoodsResultModel;
import com.zhongshu.payment.client.service.v2.OrderService2;
import com.zhongshu.payment.client.service.v2.PaymentService2;
import com.zhongshu.payment.client.type.RedisEventType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class DelayTimer {

    @Autowired
    PaymentService2 paymentService2;

    @Autowired
    OrderService2 orderService2;


    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Scheduled(fixedRate = 1000) // 每秒执行一次
    public void processDelayedQueue() {
        long currentTimestamp = System.currentTimeMillis();
        Set<String> orderTmp = redisTemplate.opsForZSet().rangeByScore(RedisEventType.ORDER_TMPE.name(), 0, currentTimestamp);

        for (String orderNo : orderTmp) {
            ResultContent<GoodsResultModel> goodsResultModelResultContent = orderService2.orderInfo(orderNo);
            GoodsResultModel content = goodsResultModelResultContent.getContent();
            paymentService2.close(orderNo,content.getUserId());
            redisTemplate.opsForZSet().remove(RedisEventType.ORDER_TMPE.name(), orderNo);
        }
    }




}
