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

import com.gyee.wisdom.alarm.sharding.entity.AlarmCount;
import com.gyee.wisdom.alarm.sharding.entity.AlarmHistory;
import com.gyee.wisdom.alarm.sharding.entity.AlarmSnap;
import com.gyee.wisdom.alarm.sharding.mapper.AlarmCountMapper;
import com.gyee.wisdom.alarm.sharding.service.AlarmHistoryService;
import com.gyee.wisdom.alarm.sharding.service.AlarmSnapService;
import com.gyee.wisdom.alarm.sharding.service.TableNameService;
import com.gyee.wisdom.alarm.sharding.util.SnowflakeGenerator;
import com.gyee.wisdom.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 风机报警统计
 * @auther: Wanghs
 * @date: 2022-03-31
 */
@Component
@Slf4j
@EnableScheduling
public class AlarmCountStatScheduled {

    @Value("${count.custom}")
    public boolean custom;

    @Autowired
    private AlarmSnapService alarmSnapService;

    @Autowired
    private TableNameService tableNameService;

    @Autowired
    private AlarmCountMapper alarmCountMapper;


    @Autowired
    private AlarmHistoryService alarmHistoryService;
    @Scheduled(cron = "0 30 0 ? * *")
    public boolean doTask() {

        if (!custom){
            return false;
        }

        try {
            List<AlarmSnap> snapList = alarmSnapService.queryAll(null, null, "custom", null, null, null, null, null, null, null);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String strDate = sdf.format(DateUtil.addDays(new Date(),-1));
            Date dtStart = sdf.parse(strDate);
            Date dtEnd = DateUtil.addDays(dtStart, 1);
            Date dtNow = new Date();

            while (dtEnd.getTime() < dtNow.getTime()) {
                List<AlarmCount> acList = new ArrayList<>();
                for (AlarmSnap snap : snapList) {
                    String tbName = tableNameService.getAlarmHistoryTableName(snap.getStationId(), dtStart);
                    int count = alarmHistoryService.selectSnapCount(tbName, snap.getId(), dtStart, dtEnd);
                    if (count > 0) {
                        AlarmCount ac = new AlarmCount();
                        ac.setId(SnowflakeGenerator.generateId());
                        ac.setAlarmDate(dtStart);
                        ac.setSnapId(snap.getId());
                        ac.setCount(count);

                        //此代码块为累加时间代码
                        List<AlarmHistory> alarmHistories = alarmCountMapper.selectAlarmHistoryBySnapid(tbName, snap.getId(), dtStart, dtEnd);
                        boolean bool = true;
                        Date alertTimebegin = null;
                        Date alertTimeend = null;
                        Double minall = 0.0;
                        for(AlarmHistory alarmHistory:alarmHistories){
                            int messageType = alarmHistory.getMessageType();
                            if(!(messageType==1^bool)){
                                if(bool) {
                                    alertTimebegin = alarmHistory.getAlertTime();
                                }
                                if(!bool){
                                    alertTimeend = alarmHistory.getAlertTime();
                                    Double min = Double.parseDouble((alertTimeend.getTime()-alertTimebegin.getTime())+"")/1000/60;
                                    minall = minall + min;
                                    bool = true;
                                }else {
                                    bool = false;
                                }
                            }
                        }
                        DecimalFormat df = new DecimalFormat("0.000");
                        ac.setTime(Double.parseDouble(df.format(minall+1)));
                        acList.add(ac);
                    }
                }

                if (acList.size() > 0)
                    alarmCountMapper.batchInsert(acList);
                dtStart = dtEnd;
                dtEnd = DateUtil.addDays(dtStart, 1);
            }

        } catch(Exception ex){
            log.error(ex.getMessage());
            log.error(ex.getStackTrace().toString());
            return false;
        }

        return true;
    }
}
