package com.zksy.data.schedule;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zksy.api.client.TableDataClient;
import com.zksy.api.dto.tableData.EBaseinfo;
import com.zksy.common.domain.Result;
import com.zksy.data.domain.po.XcrEBaseinfo;
import com.zksy.data.utils.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@EnableScheduling
@Component
public class PrepareCreditKeySchedule {

    @Autowired
    private RedisService redisService;
    @Autowired
    private TableDataClient tableDataClient;
    @Autowired
    private RedisTemplate redisTemplate;
    //一天一次
    @Scheduled(cron = "0 0 0 * * ?")
    public void prepareCreditKey() {
        Result<List<EBaseinfo>> baseinfoAll = tableDataClient.getEBaseinfoAll();
        if(baseinfoAll.getCode() != 200){
            throw new RuntimeException(baseinfoAll.getMsg());
        }
        List<XcrEBaseinfo> xcrEBaseinfos = BeanUtil.copyToList(baseinfoAll.getData(), XcrEBaseinfo.class);
        List<String> list = xcrEBaseinfos.stream().map(XcrEBaseinfo::getUniscid).filter(StrUtil::isNotBlank).collect(Collectors.toList());
        redisTemplate.delete("creditCodes");
        redisService.saveList("creditCodes",list);
        List<String> list2 = xcrEBaseinfos.stream().map(XcrEBaseinfo::getEntname).filter(StrUtil::isNotBlank).collect(Collectors.toList());
        redisTemplate.delete("creditNames");
        redisService.saveList("creditNames",list2);
    }
}
