package com.shulead.datasync.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.shulead.datasync.bo.ClassBO;
import com.shulead.datasync.entity.UfHzqy;
import com.shulead.datasync.entity.ViewPortalClassConsociation;
import com.shulead.datasync.manager.ClassManager;
import com.shulead.datasync.manager.DataSyncManager;
import com.shulead.datasync.manager.UfHzqyManager;
import com.shulead.datasync.manager.ViewPortalClassConsociationManager;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Classname InitDataTask
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/5/27 16:43
 * @Created by liuxiangshun
 */
@Slf4j
@Component
public class InitDataTask {
    @Resource
    private ClassManager classManager;

    @Resource
    private UfHzqyManager ufHzqyManager;

    @Resource
    private ViewPortalClassConsociationManager consociationManager;

    @Resource
    private DataSyncManager dataSyncManager;

    @Scheduled(cron="0 0 1 * * ? ")
    public void execute(){
        // 同步class 信息
        // 获取当前时间
        Date now = new Date();
        // 获取当天的开始时间
        Date beginDate = DateUtil.beginOfDay(now).toJdkDate();
        Date endDate = DateUtil.endOfDay(now).toJdkDate();

        log.info("task start, beginDate:{}, endDate:{}", beginDate, endDate);

        List<ClassBO> classBOList = classManager.getClassByUpdateDate(beginDate, endDate);
        if (CollectionUtils.isNotEmpty(classBOList)) {
            dataSyncManager.syncClass(classBOList);
        }

        log.info("class task end....");
        // 同步合作区域
        List<UfHzqy> ufHzqies = ufHzqyManager.findAll();
        dataSyncManager.syncHzqy(ufHzqies);
        log.info("hzqy task end....");

        // 同步合作价格
        List<ViewPortalClassConsociation> consociationList = consociationManager.findAll();
        dataSyncManager.syncConsociation(consociationList);
        log.info("consociation task end....");
    }
}
