package com.huimv.environ.eco.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.common.utils.StringUtilsWork;
import com.huimv.environ.eco.entity.BaseTelecomRoom;
import com.huimv.environ.eco.entity.SysTelecom;
import com.huimv.environ.eco.entity.SysTelecomEvent;
import com.huimv.environ.eco.entity.dto.TelecomVo;
import com.huimv.environ.eco.mapper.BaseTelecomRoomMapper;
import com.huimv.environ.eco.mapper.SysTelecomMapper;
import com.huimv.environ.eco.service.ISysTelecomEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/telecom")
public class SysTelecomEventController {

    @Autowired
    private BaseTelecomRoomMapper baseTelecomRoomMapper;

    @Autowired
    private SysTelecomMapper sysTelecomMapper;

    @Autowired
    private ISysTelecomEventService sysTelecomEventService;
    //传入机器编码  返回这个机器下的所有事件 带分页
    @GetMapping("/listevent")
    public Result list(@RequestParam(name = "deviceId", required = false) String deviceId    ,
                       @RequestParam(name = "startTimeString", required = false) String startTimeString ,
                       @RequestParam(name = "endTimeString", required = false) String endTimeString,
                       @RequestParam(name = "current", required = false) Integer current,//当前页
                       @RequestParam(name = "size", required = false) Long size,
                       @RequestParam(name = "farmId") Integer farmId  //分页大小

    ) throws ParseException {
        //如果传入的开始时间是默认的今天的 就返回最近三天的 --前端说改起来比较麻烦

        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Date startTime = fmt.parse(startTimeString);
        Date endTime = fmt.parse(endTimeString);

         long timeGap = date.getTime()-startTime.getTime();
        QueryWrapper<SysTelecomEvent> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id", farmId);
        if(StringUtilsWork.isNotBlank(deviceId)){
            wrapper.eq("device_id", deviceId);
        }
        if (timeGap<1000*60*60*72){
            startTime = new Date(endTime.getTime()-1000*60*60*100);
        }
        wrapper.between("event_time", startTime, endTime);
        wrapper.orderByDesc( "event_time" );
        Page<SysTelecomEvent> page = sysTelecomEventService.page(new Page< >(current, size), wrapper);
        //本要写SQL 现在没时间  这样处理一下问题不大
        List<SysTelecomEvent> records = page.getRecords();
        List<TelecomVo> TelecomVoRecords = new ArrayList<>();
        Page<TelecomVo> page1 = new Page< >(current, size);
        page1.setTotal(page.getTotal());
        for (SysTelecomEvent record : records) {
            TelecomVo telecomVo = new TelecomVo();
            telecomVo.setDeviceId(record.getDeviceId());
            telecomVo.setEventTime(record.getEventTime());
            //效率很低  暂且这样
            LambdaQueryWrapper<BaseTelecomRoom> wrapper3 = Wrappers.lambdaQuery();
            wrapper3.eq(BaseTelecomRoom::getDeviceId, record.getDeviceId());
            BaseTelecomRoom baseTelecomRoom = baseTelecomRoomMapper.selectOne(wrapper3);
            telecomVo.setLocation(baseTelecomRoom.getDeviceRoom());
            TelecomVoRecords.add(telecomVo);
        }
        return new Result(ResultCode.SUCCESS,page1.setRecords(TelecomVoRecords));
    }
        //判断时间   有传时间返回时间范围  没有时间

    @GetMapping("/listeventdetail")
    public Result listeventdetail(@RequestParam(name = "deviceId", required = false) String deviceId ,
                                  @RequestParam(name = "eventTimeString", required = false) String eventTimeString
    ) throws ParseException {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date eventTime = fmt.parse(eventTimeString);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(eventTime);
        c1.add(Calendar.MINUTE, -60);
        Date began = c1.getTime();
        String startTime = format.format(began);
        c1.add(Calendar.MINUTE, +150);
        Date endTime11 = c1.getTime();
        String endTime = format.format(endTime11);
        //这儿就直接拿温度了----还是不行的  温度和时间都是需要的
        Map map1 = new HashMap();
        List<String> timeList = new ArrayList<>();
        List<String> tempList = new ArrayList<>();
        LambdaQueryWrapper<SysTelecom> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysTelecom::getDeviceId ,deviceId).between(SysTelecom::getTimestamp ,startTime,endTime);
        List<SysTelecom> sysTelecoms = sysTelecomMapper.selectList(lambdaQueryWrapper);
        for (SysTelecom sysTelecom : sysTelecoms) {
            timeList.add(fmt.format(sysTelecom.getTimestamp()));
            tempList.add(sysTelecom.getTemp());
        }
        map1.put("timeList",timeList);
        map1.put("tempList",tempList);
        return new Result(ResultCode.SUCCESS,map1);
    }

    //过去几个小时的车辆烘干次数的----后面可以做成定时任务存表  防止断网就没数据
    @GetMapping("/listeventByHoure")
    public Result listeventByHoure( ) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        Date date = new Date();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.add(Calendar.DATE, +1);
        Date A = c.getTime();
        String formatA = format.format(A);

        c.add(Calendar.DATE, -1);
        Date B = c.getTime();
        String formatB = format.format(B);

        c.add(Calendar.DATE, -1);
        Date C = c.getTime();
        String formatC = format.format(C);

        c.add(Calendar.DATE, -1);
        Date D = c.getTime();
        String formatD = format.format(D);

        c.add(Calendar.DATE, -1);
        Date E = c.getTime();
        String formatE = format.format(E);

        c.add(Calendar.DATE, -1);
        Date F = c.getTime();
        String formatF = format.format(F);

        c.add(Calendar.DATE, -1);
        Date G = c.getTime();
        String formatG = format.format(G);

        c.add(Calendar.DATE, -1);
        Date H = c.getTime();
        String formatH = format.format(H);

        List result = new ArrayList();


        Map map1  = new HashMap();
        map1.put("time",splitTime(B));
        map1.put("value",sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                .between(SysTelecomEvent::getEventTime, formatB, formatA)));

        Map map2  = new HashMap();
        map2.put("time",splitTime(C));
        map2.put("value",sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                .between(SysTelecomEvent::getEventTime, formatC, formatB)));

        Map map3  = new HashMap();
        map3.put("time",splitTime(D));
        map3.put("value",sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                .between(SysTelecomEvent::getEventTime, formatD, formatC)));

        Map map4  = new HashMap();
        map4.put("time",splitTime(E));
        map4.put("value",sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                .between(SysTelecomEvent::getEventTime, formatE, formatD)));


        Map map5  = new HashMap();
        map5.put("time",splitTime(F));
        map5.put("value",sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                .between(SysTelecomEvent::getEventTime, formatF, formatE)));


        Map map6  = new HashMap();
        map6.put("time",splitTime(G));
        map6.put("value",sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                .between(SysTelecomEvent::getEventTime, formatG, formatF)));

        Map map7  = new HashMap();
        map7.put("time",splitTime(H));
        map7.put("value",sysTelecomEventService.count(Wrappers.<SysTelecomEvent>lambdaQuery()
                .between(SysTelecomEvent::getEventTime, formatH, formatG)));
        result.add(map1);
        result.add(map2);
        result.add(map3);
        result.add(map4);
        result.add(map5);
        result.add(map6);
        result.add(map7);
        //翻转
        Collections.reverse(result);
        return  new Result(ResultCode.SUCCESS,result);
    }

    public String splitTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String date1 = calendar.get(Calendar.MONTH)+1+"/"+calendar.get(Calendar.DATE);
        return date1;
    }

}
