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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.wisdom.alarm.sharding.entity.AlarmHistory;
import com.gyee.wisdom.alarm.sharding.entity.AlarmSnap;
import com.gyee.wisdom.alarm.sharding.entity.WindPowerStation;
import com.gyee.wisdom.alarm.sharding.mapper.AlarmHisotryMapper;
import com.gyee.wisdom.alarm.sharding.mapper.AlarmSnapMapper;
import com.gyee.wisdom.alarm.sharding.util.SnowflakeGenerator;
import com.gyee.wisdom.common.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class TableNameService {

    @Autowired
    private WindpowerstationService windpowerstationService;
    public static String FORMAT_ALARMHISTORY = "alarmhistory_%s_%s";
    public static SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("yyyyMM");
    public static Date DT_BEGIN = new Date(119, 06, 01);

    private String[] stationIds;

    public String getAlarmHistoryTableName(String stationId) {
        return getAlarmHistoryTableName(stationId, new Date());
    }

    public String getAlarmHistoryTableName(String stationId, Date dt) {
        try {
            if (dt.before(DT_BEGIN))
                return "alarmhistory_before201907";

            String[] arr = stationId.split("_");
            return String.format(FORMAT_ALARMHISTORY, arr[0].toLowerCase(), FORMAT_DATE.format(dt));
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        return "";
    }

    //分表序列由近到远
    public List<String> getAlarmHistoryTableNames(String[] stationIds, Date dtStart, Date dtEnd) {
        List<String> result = new ArrayList<>();
        if (dtStart.before(DT_BEGIN)) {
            result.add("alarmhistory_before201907");
            dtStart = DT_BEGIN;
            if (dtEnd.before(DT_BEGIN))
                return result;
        }

        for (String stId : stationIds) {
            Date dtIndex = dtEnd;
            int diff = 0;
            do {
                diff = DateUtil.monthsDiff2(dtIndex, dtStart);
                result.add(getAlarmHistoryTableName(stId, dtIndex));
                dtIndex = DateUtil.addMonths(dtIndex, -1);
            } while (diff > 0);
        }
        return result;
    }

    //年月拼接，由近到远
    public List<String> getYearAndMonthList(Date dtStart, Date dtEnd) {
        List<String> result = new ArrayList<>();
        Date dtIndex = dtEnd;
        int diff = 0;
        do {
            diff = DateUtil.monthsDiff2(dtIndex, dtStart);
            result.add(FORMAT_DATE.format(dtIndex));
            dtIndex = DateUtil.addMonths(dtIndex, -1);
        } while (diff > 0);

        return result;
    }

    public List<String> getAlarmHistoryTableNames(String stationId, Date dtStart, Date dtEnd) {
        if (stationId == null || stationId.trim() == "") {
            return getAlarmHistoryTableNames(getStationIds(), dtStart, dtEnd);
        } else {
            String[] arr = new String[]{stationId};
            return getAlarmHistoryTableNames(arr, dtStart, dtEnd);
        }
    }


    public String[] getStationIds() {

        if (stationIds==null||stationIds.length<=0) {
            List<WindPowerStation> lst = windpowerstationService.getAllStation("");
            Object[] objects = lst.stream().map(s -> s.getId()).collect(Collectors.toList()).toArray();
            stationIds=Arrays.copyOf(objects,objects.length,String[].class);

        }
        return stationIds;
    }


}
