package com.fjhx.task;

import com.alibaba.fastjson.JSONObject;
import com.fjhx.constants.RedisConstant;
import com.fjhx.uitl.kd100.KD100Util;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.launch.BladeApplication;
import org.springblade.core.redis.cache.BladeRedis;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Set;

@Configuration
@EnableScheduling
@Slf4j
public class ScheduleTaskController {

    @Resource
    private BladeRedis bladeRedis;

    /**
     * 定时查询快递
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    private void stockAcceptActionTask() {
        if (BladeApplication.isLocalDev()) {
            return;
        }

        log.info("定时查询快递");
        Set<String> keys = bladeRedis.keys(RedisConstant.KD_100_SEND_SUBSCRIBE + "*");

        for (String key : keys) {
            JSONObject jsonObject = bladeRedis.get(key);
            if (jsonObject != null) {
                bladeRedis.del(key);
                String com = jsonObject.getString("com");
                String num = jsonObject.getString("num");
                Date date = jsonObject.getDate("date");
                KD100Util.subscribe(com, num, date);
            }
        }

    }

}
