package com.gyee.wisdom.alarm.sharding.schedule;

import com.gyee.wisdom.alarm.sharding.mapper.AlarmHisotryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;

@Component
public class CreateTablesScheduled {
    @Autowired
    AlarmHisotryMapper alarmHisotryMapper;
    @Value("${stationname}")
    String stationname;
    @Value("${tablespace}")
    String tablespace;
    //秒 分 时 日 月 周
    @Scheduled(cron="0 45 11 * * 1")
    public void doTask(){
        Calendar instance = Calendar.getInstance();
        //查看当前时间的下月是否有表,没有就创建
        //instance.add(Calendar.MONTH,1);
        String yearmonth=null;
        int year=instance.get(Calendar.YEAR);
        int month=instance.get(Calendar.MONTH)+1;
        if(month<10){
            yearmonth=year+"0"+month;
        }else {
            yearmonth=""+year+month;
        }
        String[] stationnamelist=stationname.split(",");
        //tablespace表空间
        for (String s:stationnamelist) {
            if(alarmHisotryMapper.isHasTable(s,yearmonth)==0){//表是否存在
                alarmHisotryMapper.timedCreatTable(s,yearmonth,tablespace);
                alarmHisotryMapper.timedAlterTable(s,yearmonth,tablespace);
                alarmHisotryMapper.timedCreatAlerttimeIndex(s,yearmonth,tablespace);
                alarmHisotryMapper.timedCreatSnapidIndex(s,yearmonth,tablespace);
                System.out.println(s);
            }
        }
    }
}
