package link.at17.mid.tushare.data;

import java.text.ParseException;
import java.time.LocalDateTime;
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.service.StockAdjustService;
import link.at17.mid.tushare.data.service.StockCalendarService;
import link.at17.mid.tushare.data.service.StockDailyBasicService;
import link.at17.mid.tushare.data.service.StockDailyService;
import link.at17.mid.tushare.data.service.StockInfoService;
import link.at17.mid.tushare.data.service.StockLimitService;
import link.at17.mid.tushare.data.service.ThsDailyService;
import link.at17.mid.tushare.data.service.ThsMemberService;
import link.at17.mid.tushare.enums.StockMarket;
import lombok.extern.slf4j.Slf4j;

@SpringBootTest
@Slf4j
class UpdateData {

	@Autowired
	TushareCrawler tushareCrawler;
	
	@Autowired
	StockDailyDao stockDailyDao;

	@Autowired
	StockInfoService stockInfoService;
	
	@Autowired
	StockCalendarService stockCalendarService;
	
	@Autowired
	StockLimitService stockLimitService;
    
    @Autowired
    StockDailyService stockDailyService;
    
    @Autowired
    StockAdjustService stockAdjustService;
    
    @Autowired
    StockDailyBasicService stockDailyBasicService;
    
    @Autowired
    ThsMemberService thsMemberService;
    
    @Autowired
    ThsDailyService thsDailyService;

	@Test
	void contextLoads() {
	}

	// @Test
	void testDateTimeFormat() throws ParseException {
	    
		LocalDateTime startDate3 = LocalDateTime.of(2021, 8, 13, 0, 0);
		LocalDateTime startDate = LocalDateTime.of(2021, 8, 13, 1, 0); //LocalDateTimeUtils.parseDate("20210813010000", "yyyyMMddHHmmss");
		LocalDateTime startDate2 = LocalDateTime.of(2021, 8, 14, 0, 0);//LocalDateTimeUtils.parseDate("20210814000000", "yyyyMMddHHmmss");
		// Date endDate = DateUtils.parseDate("20210820192030", "yyyyMMddHHmmss");
		System.out.println(startDate.toLocalDate());
		System.out.println(stockCalendarService.getOpenDateOffset(StockMarket.SZ, startDate3, 1));
		System.out.println(stockCalendarService.getOpenDateOffset(StockMarket.SZ, startDate, 1));
		System.out.println(stockCalendarService.getOpenDateOffset(StockMarket.SZ, startDate2, 1));

	}

	// 手动更新运行这个 2024-01-10
	@Test
	void testStockListUpdate() {

		
		// tushareCrawler.updateStockList();
		// tushareCrawler.updateStockDaily(QueryWay.ByDateUpdate);
		// tushareCrawler.updateDailyBasic(QueryWay.ByDateUpdate);
		// tushareCrawler.updateStockAdjustTushare(QueryWay.ByDateUpdate);

		// tushareCrawler.updateThsList();
		// tushareCrawler.updateThsDaily(QueryWay.ByDateUpdate);
		// tushareCrawler.updateStockMinValue(QueryWay.ByStock, StockSpan.SixtyMinute);
		// tushareCrawler.updateStockMinValue(QueryWay.ByStock, StockSpan.Minute);

		// 更新交易日历(if needed)
        stockCalendarService.updateData();

		stockInfoService.updateData();
        stockDailyService.updateData(QueryWay.ByDateUpdate);
        stockDailyBasicService.updateData(QueryWay.ByDateUpdate);
        stockAdjustService.updateData(QueryWay.ByDateUpdate);
		stockLimitService.updateData(QueryWay.ByDateUpdate);

        thsDailyService.updateData(QueryWay.ByDateUpdate);
		thsMemberService.updateData();
		// tushareCrawler.updateStockHolder(StockHolderType.TOP10Float);
		// tushareCrawler.updateStockMinValue(QueryWay.ByStock, StockSpan.SixtyMinute);
		// tushareCrawler.updateStockMinValue(QueryWay.ByStock, StockSpan.Minute);

		// 2022-02-11 高级 token 失效了，daily_basic 接口和 ths 相关接口用不了了
		// 2022-02-12 添加自己的高级 token
		// List<StockInfo> stockList =
		// stockInfoDao.getStockListByListStatus(ListStatus.LIST);
		// kaipanlaCrawler.rollingQueryByStock(KaipanlaIndexType.BID_VOL,
		// StockSpan.Daily, stockList, 630L, QueryWay.ByStock);
	}

	@Test
	void testUpdateKaipanlaInfo() {
	}

}
