package com.younike.store.config;

import com.younike.common.constant.Constants;
import lombok.extern.log4j.Log4j2;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 缓存过期监听
 *
 * @author tianh
 */
@Component
@Log4j2
public class CacheExpireListener implements ApplicationRunner, Ordered {

    @Resource
    private RedissonClient redissonClient;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("监听Redis过期");
        RMapCache<String, String> rMapCache = redissonClient.getMapCache(Constants.PREFIX_NO_PAY_ORDER);
//        rMapCache.addListener((EntryExpiredListener<String, String>) entryEvent -> {
//            try {
//                String splicingParameters = entryEvent.getKey();
//                log.info(splicingParameters);
//                if (StringUtils.isNotBlank(splicingParameters)) {
//                    // 查询订单信息
//                    TOrder order = orderService.getById(splicingParameters);
//                    if(order != null){
//                        // 修改订单状态
//                        if(OrderState.NO_PAY.getCode().equals(order.getOrderState())){
//                            order.setOrderState(OrderState.CANCEL.getCode());
//                        }
//                        if(!orderService.updateById(order)){
//                            log.info("订单未付款取消状态更新失败，订单号："+order.getId());
//                            throw new CustomException("");
//                        }
//                    }
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });

    }

    @Override
    public int getOrder() {
        return 0;
    }

}
