package com.zcxk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.common.EasyExcelUtil;
import com.zcxk.dto.alarm.DataAlarmDto;
import com.zcxk.dto.alarm.DataAlarmExportDto;
import com.zcxk.dto.alarm.DataAlarmManageDto;
import com.zcxk.entity.alarm.DataAlarmManageEntity;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.DataAlarmAnalysisService;
import com.zcxk.service.DataAlarmDetailService;
import com.zcxk.service.DataAlarmManageService;
import com.zcxk.vo.DataAlarmAnalysisVo;
import com.zcxk.vo.DataAlarmDetailsVo;
import com.zcxk.vo.DataAlarmManageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/***
 * 数据报警处理
 * @author lihui
 * @date 14:17 2021/5/31
 **/
@RestController
@RequestMapping("/data/alarm")
@Api(tags = "数据报警")
@Slf4j
public class DataAlarmController {

    @Resource
    private DataAlarmManageService dataAlarmManageService;

    @Resource
    private DataAlarmDetailService dataAlarmDetailService;

    @Resource
    private DataAlarmAnalysisService dataAlarmAnalysisService;

    @Value("${SMS.sys_excel_path}")
    private String baseDir;

    @RequestMapping(value="list/manage" , method = RequestMethod.POST)
    @ApiOperation(value = "查询报警管理")
    public AjaxMessage<Pagination<DataAlarmManageVo>> listManage(@RequestBody @Valid DataAlarmDto dataAlarmDto) {
        return new AjaxMessage<>(ResultStatus.OK, new Pagination<>(dataAlarmManageService.findPage(dataAlarmDto)));
    }

    @RequestMapping(value="save/manage" , method = RequestMethod.POST)
    @ApiOperation(value = "保存报警管理设置")
    public AjaxMessage<String> saveManage(@RequestBody @Valid DataAlarmManageDto dataAlarmManageDto) {
        dataAlarmManageService.saveDataAlarmManage(dataAlarmManageDto);
        return new AjaxMessage<>(ResultStatus.OK );
    }

    @RequestMapping(value="find/manage" , method = RequestMethod.POST)
    @ApiOperation(value = "查询报警管理")
    public AjaxMessage<DataAlarmManageEntity> findManage(@ApiParam(value = "报警设置ID", required = true) @RequestParam("alarmId") Long alarmId) {
        return new AjaxMessage<>(ResultStatus.OK, dataAlarmManageService.findAlarmManage(alarmId));
    }

    @RequestMapping(value="update/manage" , method = RequestMethod.POST)
    @ApiOperation(value = "更新报警管理设置")
    public AjaxMessage<String> updateManage(@RequestBody @Valid DataAlarmManageDto dataAlarmManageDto) {
        dataAlarmManageService.updateDataAlarmManage(dataAlarmManageDto);
        return new AjaxMessage<>(ResultStatus.OK );
    }

    @RequestMapping(value="del/manage" , method = RequestMethod.POST)
    @ApiOperation(value = "删除报警管理设置")
    public AjaxMessage<String> delManage(@RequestBody List<Long> alarmId) {
        dataAlarmManageService.delDataAlarmManage(alarmId);
        return new AjaxMessage<>(ResultStatus.OK );
    }

    @RequestMapping(value="list/realtime" , method = RequestMethod.POST)
    @ApiOperation(value = "查询实时报警数据")
    public AjaxMessage<Pagination<DataAlarmDetailsVo>> listRealTime(@RequestBody @Valid DataAlarmDto dataAlarmDto) {
        return new AjaxMessage<>(ResultStatus.OK, new Pagination<>(dataAlarmDetailService.findPage(dataAlarmDto, 0)));
    }

    @RequestMapping(value="update/realtime/status" , method = RequestMethod.GET)
    @ApiOperation(value = "挂起/恢复实时报警数据")
    public AjaxMessage<String> updateRealTimeStatus(@ApiParam(value = "报警设置ID", required = true) @RequestParam("alarmId") Long alarmId,
                                              @ApiParam(value = "挂起状态：0恢复，1挂起", required = true) @RequestParam("status") int status) {
        dataAlarmDetailService.hangUpAlarmDetails(alarmId, status);
        return new AjaxMessage<>(ResultStatus.OK);
    }

    @RequestMapping(value="list/history" , method = RequestMethod.POST)
    @ApiOperation(value = "查询历史报警数据")
    public AjaxMessage<Pagination<DataAlarmDetailsVo>> listHistory(@RequestBody @Valid DataAlarmDto dataAlarmDto) {
        return new AjaxMessage<>(ResultStatus.OK, new Pagination<>(dataAlarmDetailService.findPage(dataAlarmDto, 1)));
    }

    @RequestMapping(value="list/analysis" , method = RequestMethod.POST)
    @ApiOperation(value = "查询数据分析")
    public AjaxMessage<Pagination<DataAlarmAnalysisVo>> listAnalysis(@RequestBody @Valid DataAlarmDto dataAlarmDto) {
        return new AjaxMessage<>(ResultStatus.OK, new Pagination<>(dataAlarmAnalysisService.findPage(dataAlarmDto)));
    }

    @RequestMapping(value="export/excel" , method = RequestMethod.POST)
    @ApiOperation(value = "excel导出")
    public AjaxMessage<String> exportExcel(@RequestBody @Valid DataAlarmExportDto exportDto) {
        DataAlarmDto alarmDto = new DataAlarmDto();
        BeanUtils.copyProperties(exportDto, alarmDto);
        alarmDto.setPageNum(1);
        alarmDto.setPageSize(100000);
        String filePath = null;
        if (exportDto.getExportType() == 0 || exportDto.getExportType() == 1) {
            String title = exportDto.getExportType() == 0 ? "实时数据" :"历史数据";
            Page<DataAlarmDetailsVo> page =  dataAlarmDetailService.findPage(alarmDto, exportDto.getExportType());
            filePath = EasyExcelUtil.excelWrite(baseDir, DataAlarmDetailsVo.class, "数据报警" + title, page.getRecords());
            return new AjaxMessage<>(ResultStatus.OK, filePath);
        }
        if (exportDto.getExportType() == 2) {
            Page<DataAlarmAnalysisVo> page = dataAlarmAnalysisService.findPage(alarmDto);
            filePath = EasyExcelUtil.excelWrite(baseDir, DataAlarmAnalysisVo.class, "数据报警分析", page.getRecords());
            return new AjaxMessage<>(ResultStatus.OK, filePath);
        }
        return new AjaxMessage<>(ResultStatus.OK);
    }


}
