package com.gyee.wisdom.alarm.sharding.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.gyee.wisdom.alarm.sharding.entity.AlarmSnap;
import com.gyee.wisdom.alarm.sharding.service.AlarmSnapService;
import com.gyee.wisdom.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@Slf4j
@RestController
@RequestMapping("/alarm")
@CrossOrigin
public class AlarmSnapController {

    Logger logger = LogManager.getLogger(AlarmSnapController.class);
    @Autowired
    private AlarmSnapService alarmSnapService;

    @GetMapping(value = "/snap/{snapId}")
    public AlarmSnap queryById( @PathVariable("snapId")  Long snapId ){
            return alarmSnapService.queryById(snapId);
    }

    @GetMapping(value = "/snap/single")
    public AlarmSnap querySingle(@RequestParam(value = "stationid", required = false) String stId,
                                 @RequestParam(value = "windturbineid",required = false) String wtId,
                                 @RequestParam(value = "alertvalue", required = false) Integer alertValue,
                                 @RequestParam(value = "testingpointkey", required = false) String testingpointkey){
        if (stId != null && stId.trim() !="" && alertValue != null )
            return alarmSnapService.findByStationIdAndAlertValue(stId, alertValue);
        else if (wtId != null && wtId.trim() != "" && alertValue != null)
            return alarmSnapService.findByWindturbineIdAndAlertValue(wtId, alertValue);
        else if (testingpointkey != null && testingpointkey.trim() !="")
            return alarmSnapService.findByTestingpointkey(testingpointkey);
        return  null;
    }

    @GetMapping(value = "/snap")
    public List<AlarmSnap> queryAll(
            @RequestParam(value = "stationid", required = false) String stId,
            @RequestParam(value = "windturbineid",required = false) String wtId,
            @RequestParam(value = "category1",required = false) String category1,
            @RequestParam(value = "category2",required = false) String category2,
            @RequestParam(value = "rank",required = false) String rank,
            @RequestParam(value = "modelid",required = false) String modelId,
            @RequestParam(value = "isopened",required = false) Integer isOpened,
            @RequestParam(value = "keyword",required = false) String keyWord,
            @RequestParam(value = "starttime", required = false) String startTime,
            @RequestParam(value = "endtime", required = false) String endTime
    ){
        try {
            return alarmSnapService.queryAll(stId,wtId,category1,category2,rank,modelId,isOpened,keyWord,startTime,endTime);
        } catch (Exception e) {
            logger.error("操作失败",e);
            return null;
        }
    }

    @GetMapping(value = "/snap/page")
    public IPage<AlarmSnap> queryByPage(
            @RequestParam(value = "pagenum")Integer pageNum,
            @RequestParam(value = "pagesize")Integer pageSize,
            @RequestParam(value = "stationid", required = false) String stId,
            @RequestParam(value = "windturbineid",required = false) String wtId,
            @RequestParam(value = "category1",required = false) String category1,
            @RequestParam(value = "category2",required = false) String category2,
            @RequestParam(value = "rank",required = false) String rank,
            @RequestParam(value = "modelid",required = false) String modelId,
            @RequestParam(value = "isopened",required = false) Integer isOpened,
            @RequestParam(value = "keyword",required = false) String keyWord,
            @RequestParam(value = "starttime", required = false) String startTime,
            @RequestParam(value = "endtime", required = false) String endTime
    ){
        Page<AlarmSnap> page =new Page(pageNum,pageSize);

        try {
            return  alarmSnapService.pageQueryAll(page,stId,wtId,category1,category2,rank,modelId,isOpened,keyWord,startTime,endTime);
        } catch (Exception e) {
            logger.error("操作失败",e);
            return null;
        }

    }

    @PostMapping("/snap/batch")
    public void writeLatestBatch(@RequestBody List<AlarmSnap> alarmSnaps) {
        try {
            alarmSnapService.saveAlarmSnaps(alarmSnaps);
        } catch (Exception e) {
            logger.error("操作失败",e);
        }
    }

    @PostMapping("/snap")
    public void saveAlarmSnap(@RequestBody AlarmSnap alarmSnap) {
        try {
            alarmSnapService.saveAlarmSnap(alarmSnap);
        } catch (Exception e) {
            logger.error("操作失败",e);
        }
    }

    @GetMapping(value = "/snap/top")
    public List<AlarmSnap> getTopNumAlarm(
            @RequestParam(value = "category1", required = true) String category1,
            @RequestParam(value = "topnum", required = true) String topNum
    ) {
        try {
            return alarmSnapService.getTopNumAlarm(category1,topNum);
            //return faultSnapService.getTopNumFault(topNum);
        } catch (Exception e) {
            log.error("操作失败",e);
            return null;
        }
    }

    @GetMapping(value = "/snap/recent")
    public List<AlarmSnap> getRecentAlarm(
            @RequestParam(value = "category1", required = true) String category1,
            @RequestParam(value = "lastupdatetime", required = true) String lastUpdateTime
    ) {
        lastUpdateTime = lastUpdateTime.trim();

        Date dtFault = null;
        if (lastUpdateTime != null && lastUpdateTime != "") {
            if (lastUpdateTime.length() < 19)
                dtFault = DateUtil.parseDate(lastUpdateTime);
            else
                dtFault = DateUtil.parseDate1(lastUpdateTime);
        }

        if (dtFault == null)
            return null;

        return alarmSnapService.getRecentAlarm(category1, dtFault);
    }
}
