package com.zcxk.service.impl;

import com.zcxk.dao.AlarmDetailMapper;
import com.zcxk.dao.MonitorDataMapper;
import com.zcxk.dao.TestDataMapper;
import com.zcxk.dto.DeviceCheckAlarmDto;
import com.zcxk.entity.AlarmDetailsEntity;
import com.zcxk.entity.DayReportEntity;
import com.zcxk.service.TestDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @description
 * @auto wangli
 * @data 2020/12/14 14:36
 */
@Service
public class TestDataServiceImpl implements TestDataService {

    @Resource
    private AlarmDetailMapper alarmDetailMapper;
    @Resource
    private TestDataMapper testDataMapper;

    @Resource
    private MonitorDataMapper monitorDataMapper;

    public List<DayReportEntity> selectDeviceInfo(Long id){
        return testDataMapper.selectDeviceInfo(id);
    }

    @Override
    public void createDataByYear(Long id ,LocalDate beginDate, LocalDate endDate) {
        while(beginDate.isBefore(endDate)){
            testDataMapper.batchInsertYearReport(beginDate.getYear(),id);
            beginDate=beginDate.plusYears(1);
        }
    }

    @Override
    public void createDataByMonth(Long id ,LocalDate beginDate, LocalDate endDate) {
        while(beginDate.isBefore(endDate)){
            testDataMapper.batchInsertMonthReport(beginDate.getYear(),beginDate.getMonthValue(),id);
            beginDate=beginDate.plusMonths(1);
        }
    }
    public void createDataByDay(List<DayReportEntity> dayReportEntityList,LocalDate beginDate, LocalDate endDate) {
        LocalDateTime beginDateTime=beginDate.atStartOfDay();
        LocalDateTime endDateTime=endDate.atStartOfDay();

        Random r = new Random();
        Double j =0d;
        List<DayReportEntity> dayReportEntities =new ArrayList<>();
        for(int i=0; beginDateTime.isBefore(endDateTime);i++,j++){
            Double value = r.nextDouble() * 12000;

            if(i==24){
                i=0;
            }
            beginDateTime = beginDateTime.plusHours(1);
            for(DayReportEntity dayReportEntity : dayReportEntityList){

                DayReportEntity dayReport = new DayReportEntity();
                dayReport.setTenantId(dayReportEntity.getTenantId());
                dayReport.setParentSceneId(dayReportEntity.getParentSceneId());
                dayReport.setParentSceneName(dayReportEntity.getParentSceneName());
                dayReport.setSceneId(dayReportEntity.getSceneId());
                dayReport.setSceneName(dayReportEntity.getSceneName());
                dayReport.setDeviceId(dayReportEntity.getDeviceId());
                dayReport.setDeviceName(dayReportEntity.getDeviceName());
                dayReport.setDeviceCode(dayReportEntity.getDeviceCode());
                dayReport.setAttributeId(dayReportEntity.getAttributeId());
                dayReport.setAttributeName(dayReportEntity.getAttributeName());


                dayReport.setYear(beginDateTime.getYear());
                dayReport.setMonth(beginDateTime.getMonthValue());
                dayReport.setDay(beginDateTime.getDayOfMonth());
                dayReport.setHour(i);
                dayReport.setMinValue(j);
                dayReport.setAvgValue(j+(value/2));
                dayReport.setSumValue(value);

                dayReport.setMaxValue(j+value);
                dayReport.setLatestValue(j+value);
                dayReport.setCollectDate(Date.from(beginDateTime.atZone(ZoneId.systemDefault()).toInstant()));
                dayReportEntities.add(dayReport);
            }
            j+=value;
        }
        if(dayReportEntities.size()>0){
            for(int i=0;500*i<dayReportEntities.size();i++){
                if(500*i+500<dayReportEntities.size()){
                    monitorDataMapper.batchInsertDayReport(dayReportEntities.subList(500*i,500*i+500));
                }else{
                    monitorDataMapper.batchInsertDayReport(dayReportEntities.subList(500*i,dayReportEntities.size()));
                }
            }
        }
    }

    public void createAlarmByDay(Integer deviceId,LocalDate beginDate, LocalDate endDate,Integer times){

        List<DeviceCheckAlarmDto> deviceCheckAlarmDtos = alarmDetailMapper.selectDeviceForCheckAlarm(deviceId,"状态报警");

        ZoneOffset zoneOffset = ZoneOffset.ofHours(8);
        Long beginTimestamp = beginDate.atStartOfDay().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        Long endTimestamp = endDate.atStartOfDay().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        int size =deviceCheckAlarmDtos.size();
        if(size>0){
            List<AlarmDetailsEntity>  deviceStateAlarmDtoNews = new ArrayList<>();
            for(int i=0;i<times;i++){
                //随机时间报警
                Long timestamp = new Double(Math.random()*(endTimestamp-beginTimestamp+1)+beginTimestamp).longValue();
                Integer m = (int)(Math.random()*size);
                //随机报警参数
//                DeviceCheckAlarmDto d=deviceCheckAlarmDtos.get(m);
                DeviceCheckAlarmDto d=deviceCheckAlarmDtos.get(0);

                AlarmDetailsEntity alarmDetailsEntity = new AlarmDetailsEntity();
                alarmDetailsEntity.setDateUpdate(new Date(timestamp));
                alarmDetailsEntity.setDateCreate(new Date(timestamp));
                alarmDetailsEntity.setStatus(1);
                alarmDetailsEntity.setOpState(1);
                alarmDetailsEntity.setState(1);
                alarmDetailsEntity.setAlarmStartTime(new Date(timestamp));
                alarmDetailsEntity.setAlarmContent(d.getDeviceName()+"离线时间"+d.getAlarmCondition() +d.getAlarmValue()+"分钟");
                alarmDetailsEntity.setAlarmValue(d.getDuration());
                alarmDetailsEntity.setAttributeId(d.getAttributeId());
                alarmDetailsEntity.setAlarmType("状态报警");
                alarmDetailsEntity.setDeptOrgId(d.getDeptOrgId());
                alarmDetailsEntity.setCompanyOrgId(d.getCompanyOrgId());
                alarmDetailsEntity.setDeviceId(d.getDeviceId());
                alarmDetailsEntity.setTenantId(d.getTenantId());
                alarmDetailsEntity.setAlarmSettingId(d.getAlarmSettingId());
                deviceStateAlarmDtoNews.add(alarmDetailsEntity);
            }
            if(deviceStateAlarmDtoNews.size()>0){
                for(int i=0;500*i<deviceStateAlarmDtoNews.size();i++){
                    if(500*i+500<deviceStateAlarmDtoNews.size()){
                        alarmDetailMapper.batchInsert(deviceStateAlarmDtoNews.subList(500*i,500*i+500));
                    }else{
                        alarmDetailMapper.batchInsert(deviceStateAlarmDtoNews.subList(500*i,deviceStateAlarmDtoNews.size()));
                    }
                }
            }
        }
    }
    
}
