package com.gyee.alarm.controller;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.alarm.model.auto.ProEconAlarmConfiguration;
import com.gyee.alarm.model.auto.ProEconAlarmInfo;
import com.gyee.alarm.model.vo.AjaxResult;
import com.gyee.alarm.model.vo.AjaxStatus;
import com.gyee.alarm.model.vo.AlarmConfigurationVo;
import com.gyee.alarm.model.vo.ProEconAlarmInfoVo;
import com.gyee.alarm.service.AlarmConfigurationService;
import com.gyee.alarm.service.auto.IProEconAlarmInfoService;
import com.gyee.alarm.util.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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 javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报警缓存表 前端控制器
 * </p>
 *
 * @author shilin
 * @since 2023-10-12
 */
@RestController
@RequestMapping("//alarminfo")
public class ProEconAlarmInfoController {
    @Resource
    private IProEconAlarmInfoService proEconAlarmInfoService;

    @GetMapping(value = "/findAlarmlist")
    @ApiOperation(value = "报警信息查询", notes = "报警信息查询")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "begin", value = "开始时间", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "end", value = "结束时间", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "alarmType", value = "报警类型 booststation:升压站报警,windturbine:设备报警,custom:自定义报警", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "stationid", value = "场站编号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "deviceid", value = "设备编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "描述", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "modelId", value = "型号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "deviceType", value = "报警类型 booststation:升压站报警,windturbine:设备报警", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "components", value = "部件编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "alarmId", value = "报警编号", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isclose", value = "是否完成 不传全查,true 完成的，false 没完成的", required = false, dataType = "Boolean", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示行数", required = true, dataType = "string", paramType = "query")})
    public AjaxResult findAlarmlist(
            @RequestParam(value = "begin", required = false) String begin,
            @RequestParam(value = "end", required = false) String end,
            @RequestParam(value = "alarmType", required = false) String alarmType,
            @RequestParam(value = "stationid", required = false) String stationid,
            @RequestParam(value = "deviceid", required = false) String deviceid,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "modelId", required = false) String modelId,
            @RequestParam(value = "deviceType", required = false) String deviceType,
            @RequestParam(value = "components", required = false) String components,
            @RequestParam(value = "alarmId", required = false) String alarmId,
            @RequestParam(value = "isclose", required = false) Boolean isclose,
            @RequestParam(value = "pageNum", required = true) Integer pageNum,
            @RequestParam(value = "pageSize", required = true) Integer pageSize

    ) throws Exception {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date beginDate = null;
        Date endDate = null;
        if(StringUtils.notEmp(begin) && StringUtils.notEmp(end))
        {         beginDate = df.parse(begin);
             endDate = df.parse(end);
        }

        Page<ProEconAlarmConfiguration> page = new Page(pageNum, pageSize);
        IPage<ProEconAlarmInfo> map = proEconAlarmInfoService.findAlarmlist(page,modelId,beginDate, endDate, alarmType, stationid,deviceid, description,deviceType,components,alarmId,isclose, pageNum, pageSize);

        IPage<ProEconAlarmInfoVo> vomap=new Page<>();
        if(StringUtils.notEmp(map) && StringUtils.notEmp(map.getRecords()))
        {
            vomap.setTotal(map.getTotal());
            List<ProEconAlarmInfoVo> vos=new ArrayList<>();
            for(ProEconAlarmInfo info:map.getRecords())
            {
                ProEconAlarmInfoVo vo=new ProEconAlarmInfoVo();
                vo.setCharacteristic(info.getCharacteristic());
                vo.setTbname(info.getTbname());

                if(StringUtils.notEmp(info.getLastUpdateTime()))
                {
                    vo.setLastUpdateTime(info.getLastUpdateTime().getTime());
                }else
                {
                    vo.setLastUpdateTime(0l);
                }
                vo.setVal(info.getVal());
                vo.setTagId(info.getTagId());
                vo.setTriggerType(info.getTriggerType());
                vo.setAlarmId(info.getAlarmId());
                vo.setDeviceId(info.getDeviceId());
                vo.setSuffix(info.getSuffix());
                vo.setRank(info.getRank());
                vo.setConfirmed(info.getConfirmed());
                vo.setUniformCode(info.getUniformCode());
                vo.setAlarmType(info.getAlarmType());
                vo.setDeviceType(info.getDeviceType());
                vo.setComponents(info.getComponents());
                vo.setSubcomponents(info.getSubcomponents());
                vo.setDescription(info.getDescription());
                vo.setModelId(info.getModelId());
                vo.setResetTable(info.getResetTable());
                vo.setEnable(info.getEnable());
                vo.setCharacteristic(info.getCharacteristic());
                vo.setStationId(info.getStationId());
                vo.setProjectId(info.getProjectId());
                vo.setLineId(info.getLineId());
                vo.setStationName(info.getStationName());
                vo.setProjectiName(info.getProjectiName());
                vo.setLineidName(info.getLineidName());
                vo.setDeviceName(info.getDeviceName());
                vo.setOval(info.getOval());
                vo.setIsOpen(info.getIsOpen());

                if(StringUtils.notEmp(info.getConfirmedTime()))
                {
                    vo.setConfirmedTime(info.getConfirmedTime().getTime());
                }else
                {
                    vo.setConfirmedTime(0l);
                }

                if(StringUtils.notEmp(info.getCloseTime()))
                {
                    vo.setCloseTime(info.getCloseTime().getTime());
                }else
                {
                    vo.setCloseTime(0l);
                }

                if(StringUtils.notEmp(info.getUpdateTime()))
                {
                    vo.setUpdateTime(info.getUpdateTime().getTime());
                }else
                {
                    vo.setUpdateTime(0l);
                }

                vos.add(vo);
            }
            vomap.setRecords(vos);

        }
        return AjaxResult.successData(AjaxStatus.success.code, vomap);

    }
}
