package com.inspect.metadata.task.service.impl;

import com.inspect.metadata.eqpbook.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.mapper.BasedataEqpBookMapper;
import com.inspect.metadata.montdata.domain.BasedataMontPatDevOnline;
import com.inspect.metadata.montdata.mapper.BasedataMontPatDevOnlineMapper;
import com.inspect.metadata.statis.domain.BasedataStatisRobotUavOnlineDay;
import com.inspect.metadata.statis.mapper.BasedataStatisRobotUavOnlineDayMapper;
import com.inspect.metadata.task.service.IBasedataTaskService;
import com.inspect.base.core.utils.DateUtils;
import com.inspect.base.redis.service.RedisService;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class BasedataTaskServiceImpl implements IBasedataTaskService {
    @Resource
    BasedataEqpBookMapper basedataEqpbookMapper;
    @Resource
    BasedataStatisRobotUavOnlineDayMapper basedataStatisRobotuavOnlineDayMapper;
    @Resource
    BasedataMontPatDevOnlineMapper basedataMontPatdevOnlineMapper;
    @Resource
    RedisService redisService;

    public void statisBasedataStatisRobotUavOnlineDay() {
        Calendar now1 = Calendar.getInstance();
        int hour = now1.get(11);
        double interval = hour / 4;
        int intervalCount = (int) interval;
        Date theDayStartTime = DateUtils.theDayStartTime(new Date());
        int startFlag = intervalCount * 4;
        int endFlag = intervalCount * 4 + 4;
        Date startTime = DateUtils.theDayTimeByAddHour(theDayStartTime, startFlag);
        Date endTime = DateUtils.theDayTimeByAddHour(theDayStartTime, endFlag);
        String startTimeFormat = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime);
        String endTimeFormat = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime);
        Date dayStartTime = DateUtils.theDayStartTime(startTime);
        List<BasedataEqpBook> eqpBooks = this.basedataEqpbookMapper.getRobotUavList();
        System.out.println("任务statisBasedataStatisRobotuavOnlineDay，查询获得无人机机器人，数量：" + eqpBooks.size());
        if (!eqpBooks.isEmpty()) {
            for (BasedataEqpBook eqpBook : eqpBooks) {
                String patrolDeviceCode = eqpBook.getPatrolDeviceCode();
                BasedataStatisRobotUavOnlineDay basedataStatisRobotuavOnlineDay = new BasedataStatisRobotUavOnlineDay();
                basedataStatisRobotuavOnlineDay.setPatrolDeviceCode(patrolDeviceCode);
                basedataStatisRobotuavOnlineDay.setDay(theDayStartTime);
                BasedataStatisRobotUavOnlineDay robotuavOnlineDayStatis = this.basedataStatisRobotuavOnlineDayMapper.selectBasedataStatisRobotUavOnlineDayByDayStartTime(basedataStatisRobotuavOnlineDay);
                if (null == robotuavOnlineDayStatis) {
                    robotuavOnlineDayStatis = new BasedataStatisRobotUavOnlineDay();
                }

                if (null == robotuavOnlineDayStatis.getDay() || null == robotuavOnlineDayStatis.getStatId()) {
                    robotuavOnlineDayStatis.setDay(dayStartTime);
                    robotuavOnlineDayStatis.setEqpBookId(eqpBook.getEqpBookId());
                    robotuavOnlineDayStatis.setPatrolDeviceCode(eqpBook.getPatrolDeviceCode());
                    robotuavOnlineDayStatis.setTotalOnlineTime(0L);
                    robotuavOnlineDayStatis.setOfflineCount(0L);
                    robotuavOnlineDayStatis.setCreateBy("sys");
                    robotuavOnlineDayStatis.setCreateTime(DateUtils.getNowDate());
                }

                BasedataMontPatDevOnline basedataMontPatdevOnline = new BasedataMontPatDevOnline();
                Map<String, Object> params = new HashMap<>();
                params.put("actionBeginTime", startTimeFormat);
                params.put("actionEndTime", endTimeFormat);
                basedataMontPatdevOnline.setEqpBookId(eqpBook.getEqpBookId());
                basedataMontPatdevOnline.setParams(params);
                List<BasedataMontPatDevOnline> onLines = this.basedataMontPatdevOnlineMapper.selectBasedataMontPatDevOnlineList(basedataMontPatdevOnline);
                if (null != onLines && !onLines.isEmpty()) {
                    Map<String, String> map = new HashMap<>();

                    for (BasedataMontPatDevOnline online : onLines) {
                        String status = online.getStatus();
                        if ("1".equals(status)) {
                            if (map.containsKey("online")) {
                                break;
                            }

                            map.put("online", "1");
                        } else {
                            map.put("offline", "1");
                        }
                    }

                    if (map.containsKey("online")) {
                        robotuavOnlineDayStatis.setTotalOnlineTime(robotuavOnlineDayStatis.getTotalOnlineTime() + 240L);
                    } else {
                        robotuavOnlineDayStatis.setOfflineCount(robotuavOnlineDayStatis.getOfflineCount() + 1L);
                    }
                } else if (this.redisService.hasKey("mont_patrol_device_online_status:" + patrolDeviceCode)) {
                    robotuavOnlineDayStatis.setTotalOnlineTime(robotuavOnlineDayStatis.getTotalOnlineTime() + 240L);
                } else {
                    robotuavOnlineDayStatis.setOfflineCount(robotuavOnlineDayStatis.getOfflineCount() + 1L);
                }

                if (null != robotuavOnlineDayStatis.getStatId()) {
                    robotuavOnlineDayStatis.setUpdateBy("sys");
                    robotuavOnlineDayStatis.setUpdateTime(DateUtils.getNowDate());
                    this.basedataStatisRobotuavOnlineDayMapper.updateBasedataStatisRobotUavOnlineDay(robotuavOnlineDayStatis);
                } else {
                    this.basedataStatisRobotuavOnlineDayMapper.insertBasedataStatisRobotUavOnlineDay(robotuavOnlineDayStatis);
                }
            }

        }
    }
}
