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.FaultHistory;
import com.gyee.wisdom.alarm.sharding.entity.FaultInfo;
import com.gyee.wisdom.alarm.sharding.entity.FaultSnap;
import com.gyee.wisdom.alarm.sharding.service.FaultInfoService;
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.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @descrition:
 * @author:Wanghs
 * @date:2020-06-10
 */
@Slf4j
@RestController
@RequestMapping("/fault")
@CrossOrigin
public class FaultInfoController {

    Logger logger = LogManager.getLogger(FaultInfoController.class);
    @Autowired
    private FaultInfoService faultService;

    @GetMapping(value = "/history/list")
    public List<FaultInfo> queryByList(
            @RequestParam(value = "pagenum", required = false) Integer pageNum,
            @RequestParam(value = "pagesize", required = false) Integer pageSize,
            @RequestParam(value = "stationid", required = false) String stId,
            @RequestParam(value = "keyword", required = false) String keyWord,
            @RequestParam(value = "category1", required = false) String cateGory1,
            @RequestParam(value = "starttime", required = false) String startTime,
            @RequestParam(value = "endtime", required = false) String endTime,
            @RequestParam(value = "messagetype", required = false) Integer messageType,
            @RequestParam(value = "isopened", required = false) Integer isOpened
    ) {
        try {
            if(null != pageNum && null !=pageSize ){
                Page<FaultInfo> page = new Page(pageNum, pageSize);
                IPage<FaultInfo> result = faultService.queryFaultInfo(page, stId, keyWord,cateGory1, startTime, endTime, isOpened);
                for (FaultInfo fi : result.getRecords()) {
                    fi.setProjectId("" + result.getTotal());
                }
                return result.getRecords();
            }
            List<FaultInfo> result = faultService.queryFaultInfo(stId, keyWord, startTime, endTime,messageType);
            return result;
        } catch (Exception e) {
            logger.error("操作失败",e);
            return null;
        }
    }


    @GetMapping(value = "/history/listpage")
    public IPage<FaultInfo> queryByListpage(
            @RequestParam(value = "pagenum") Integer pageNum,
            @RequestParam(value = "pagesize") Integer pageSize,
            @RequestParam(value = "stationid", required = false) String stId,
            @RequestParam(value = "keyword", required = false) String keyWord,
            @RequestParam(value = "category1", required = false) String cateGory1,
            @RequestParam(value = "starttime", required = false) String startTime,
            @RequestParam(value = "endtime", required = false) String endTime,
            @RequestParam(value = "messagetype", required = false) Integer messageType,
            @RequestParam(value = "isopened", required = false) Integer isOpened
    ) {
        try {
            Page<FaultInfo> page = new Page(pageNum, pageSize);
            IPage<FaultInfo> result = faultService.queryFaultInfo(page, stId, keyWord, cateGory1, startTime, endTime, isOpened);
            for (FaultInfo fi : result.getRecords()) {
                fi.setProjectId("" + result.getTotal());
            }
            return result;

        } catch (Exception e) {
            logger.error("操作失败", e);
            return null;
        }
    }

    @GetMapping("/history")
    public List<FaultInfo> getHistoryById(@RequestParam(value = "ids", required = false) String ids){
        if(ids==null){
            return null;
        }
        String[] nids = ids.split(",");
        List<FaultInfo> fi = faultService.getFaultInfoById(nids);
        return fi;
    }


    @GetMapping(value = "/history/station")
    public List<FaultSnap> queryByTimeAndStation(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "stationId", required = false) String stationId){
        return faultService.queryByTimeAndStation(startTime,endTime,stationId);
    }


    @GetMapping(value = "/recent")
    public List<FaultInfo> getRecentFault(
            @RequestParam(value = "faulttime", required = true) String faultTime
    ) {
        return faultService.getRecentFault(faultTime);
    }

    @GetMapping(value = "/history/page")
    public IPage<FaultInfo> queryByPage(
            @RequestParam(value = "pagenum") Integer pageNum,
            @RequestParam(value = "pagesize") Integer pageSize,
            @RequestParam(value = "stationid", required = false) String stId,
            @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 {
            Page<FaultInfo> page = new Page(pageNum, pageSize);
            return faultService.queryFaultInfo(page, stId, keyWord, "", startTime, endTime, isOpened);
        } catch (Exception e) {
            logger.error("操作失败",e);
            return null;
        }

    }


    @GetMapping(value = "/top")
    public List<FaultInfo> getTopNumFault(
            @RequestParam(value = "topnum", required = true) String topNum
    ) {
        try {
            return faultService.getTopNumFault(topNum);
        } catch (Exception e) {
            logger.error("操作失败",e);
            return null;
        }
    }


    @PostMapping("/history")
    public void saveFaultHistory(@RequestBody FaultHistory faultHistory){
        logger.info("请求数据:id = {},snapId = {},confirmPerson = {}",faultHistory.getId(),faultHistory.getSnapId(),faultHistory.getConfirmPerson());
        try {
            faultService.saveFaultHistory(faultHistory);
        } catch (Exception e) {
            logger.error("操作失败",e);
        }
    }


    @GetMapping(value = "/history/{historyId}")
    public FaultHistory queryFaultHistoryById(@PathVariable("historyId") Long historyId) {
        try {
            return faultService.queryFaultHistoryById(historyId);
        } catch (Exception e) {
            logger.error("操作失败",e);
            return null;
        }
    }


    @GetMapping(value = "/snap/ids")
    public List<FaultSnap> queryMultiFaultSnap(@RequestParam(value = "ids") String snapIds) {
        try {
            return faultService.queryMultiFaultSnap(snapIds);
        } catch (Exception e) {
            logger.error("操作失败",e);
            return null;
        }
    }


    @GetMapping(value = "/snap/single")
    public FaultSnap queryByTagId(@RequestParam(value = "tagId", required = true) String tagId){
        return faultService.queryByTagId(tagId);
    }

    @PostMapping("/snap/batch")
    public void writeLatestBatch(@RequestBody List<FaultSnap> faultSnaps) throws Exception {
        faultService.saveFaultSnaps(faultSnaps);
    }

    //一次性批量确认
    @GetMapping("/clear")
    public void clearFaultBatch(@RequestParam(value = "startTime") long startTime,
                                @RequestParam(value = "endTime") long endTime) {
        try {
            Date st = new Date(startTime);
            Date et = new Date(endTime);
            faultService.faultClear(st, et);
        } catch (Exception e) {
            logger.error("操作失败",e);
        }
    }



    //故障确认操作
    @GetMapping("/confirm")
    public void confirmFault(
            @RequestParam(value = "faultHistoryId") long faultHistoryId,
            @RequestParam(value = "confirmType") int confirmType,
            @RequestParam(value = "userName") String userName,
            @RequestParam(value = "snpaId") long snpaId) {

        try {
            faultService.confirmBatch(faultHistoryId,confirmType,userName,snpaId);
        } catch (Exception e) {
            logger.error("操作失败",e);
        }
    }
}
