package link.at17.mid.tushare.data;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import link.at17.mid.tushare.dao.StockDailyDao;
import link.at17.mid.tushare.data.crawler.QueryWay;
import link.at17.mid.tushare.data.crawler.tushare.TushareCrawler;
import link.at17.mid.tushare.data.models.StockInfo;
import link.at17.mid.tushare.data.service.StockInfoService;
import link.at17.mid.tushare.data.service.StockMinuteService;
import link.at17.mid.tushare.enums.ListStatus;
import link.at17.mid.tushare.enums.StockSpan;
import lombok.extern.slf4j.Slf4j;

@SpringBootTest
@Slf4j
class UpdateMinuteTest {

    @Autowired
    TushareCrawler tushareCrawler;
    
    @Autowired
    StockDailyDao stockDailyDao;
    
    @Autowired
    StockMinuteService stockMinuteService;
    
    @Autowired
    StockInfoService stockInfoService;
    
    //@Test
    void testStockMinuteUpdate() {
        stockMinuteService.updateData(QueryWay.ByStock, StockSpan.Minute60);
    }
    
    //@Test
    void testGetQfqStockMinute() {
        try {
            stockMinuteService.getQfqMinuteBefore("002323.SZ", StockSpan.Minute60, null, 500l).stream().forEach(System.out::println);
        }
        catch (Exception e) {
            log.error("{}", e);
        }
    }
    @Test
    void testGetAllMissingDates() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<StockInfo> stockInfos = stockInfoService.listByListStatus(ListStatus.LIST);
        
        stockInfos.forEach(stockInfo -> {
            if (stockInfo.isMainBoard()) {
                List<LocalDateTime> allMissingDates = stockMinuteService.getAllMissingDates(StockSpan.Minute60, stockInfo);
                allMissingDates.forEach(date -> {
                    log.info("{} {}\t{}", stockInfo.getTsCode(), stockInfo.getName(), 
                            formatter.format(date));
                });
            }
        });
    }

}
