package com.ozs.web.controller.accountmanagment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.utils.StringUtils;
import com.ozs.service.entity.MsgAlarm;
import com.ozs.service.entity.MsgAlarmVice;
import com.ozs.service.entity.vo.HistoricalAlarmVo;
import com.ozs.service.entity.vo.MsgAlarmVo;
import com.ozs.service.service.MsgAlarmService;
import com.ozs.service.service.MsgAlarmViceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
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;

/**
 * <p>
 * 预报警信息管理表 前端控制器
 * </p>
 *
 * @author ozs
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/service/msgAlarm")
public class MsgAlarmController extends BaseController {
    @Resource
    MsgAlarmService msgAlarmService;
    @Resource
    MsgAlarmViceService msgAlarmViceService;


    /**
     * 预报警信息分页
     *
     * @param msgAlarmVo
     * @return
     */
    @ApiOperation(value = "预报警信息分页")
    @PostMapping("/list")
    public AjaxResult selectMsgAlarm(@RequestBody MsgAlarmVo msgAlarmVo) {
        LambdaQueryWrapper<MsgAlarm> lw = new LambdaQueryWrapper<MsgAlarm>();
        if (!StringUtils.isBlank(msgAlarmVo.getAlarmLine())) {
            lw.eq(MsgAlarm::getAlarmLine, msgAlarmVo.getAlarmLine());
        }
        if (!ObjectUtils.isEmpty(msgAlarmVo.getLineDir())) {
            lw.eq(MsgAlarm::getLineDir, msgAlarmVo.getLineDir());
        }
        if (!ObjectUtils.isEmpty(msgAlarmVo.getAlarmType())) {
            lw.eq(MsgAlarm::getAlarmType, msgAlarmVo.getAlarmType());
        }
        if (!StringUtils.isBlank(msgAlarmVo.getAlarmMile())) {
            lw.eq(MsgAlarm::getAlarmMile, msgAlarmVo.getAlarmMile());
        }
        if (!ObjectUtils.isEmpty(msgAlarmVo.getBeginAlarmArrr())) {
            lw.ge(MsgAlarm::getAlarmArrr, msgAlarmVo.getBeginAlarmArrr());
        }
        if (!ObjectUtils.isEmpty(msgAlarmVo.getEndAlarmArrr())) {
            lw.le(MsgAlarm::getAlarmArrr, msgAlarmVo.getEndAlarmArrr());
        }
        if (!ObjectUtils.isEmpty(msgAlarmVo.getBeginAlarmTime())) {
            lw.ge(MsgAlarm::getAlarmTime, msgAlarmVo.getBeginAlarmTime());
        }
        if (!ObjectUtils.isEmpty(msgAlarmVo.getEndAlarmTime())) {
            lw.le(MsgAlarm::getAlarmTime, msgAlarmVo.getEndAlarmTime());
        }
        if (!ObjectUtils.isEmpty(msgAlarmVo.getIsLock())) {
            lw.eq(MsgAlarm::getIsLock, msgAlarmVo.getIsLock());
        }
        IPage<MsgAlarm> page = msgAlarmService.page(new Page<>(msgAlarmVo.getPageNum(), msgAlarmVo.getPageSize()), lw);
        return AjaxResult.success(page);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation("根据预报警信息ID获取详细信息")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(msgAlarmService.getById(id));
    }


    @PutMapping(value = "/{id}")
    @ApiOperation("根据预报警信息ID解除报警")
    public AjaxResult updateIsLock(@PathVariable Long id) {
        MsgAlarm msgAlarm = msgAlarmService.getById(id);
        msgAlarm.setIsLock(1);
        msgAlarm.setUpdateBy(getUsername());
        msgAlarm.setReleasedBy(getUsername());
        msgAlarm.setReleasedTime(System.currentTimeMillis());
        return AjaxResult.success(msgAlarmService.updateById(msgAlarm));
    }

    @GetMapping(value = "/historicalAlarm/{id}")
    @ApiOperation("历史报警次数信息")
    public AjaxResult historicalAlarm(@PathVariable Long id) {
        MsgAlarm msgAlarm = msgAlarmService.getById(id);
        List<HistoricalAlarmVo> historicalAlarmVoList=new ArrayList<>();
        HistoricalAlarmVo historicalAlarmVo = new HistoricalAlarmVo();
        historicalAlarmVo.setAlarmTime(msgAlarm.getAlarmTime());
        historicalAlarmVo.setAlarmType(msgAlarm.getAlarmType());
        historicalAlarmVo.setIsLock(msgAlarm.getIsLock());
        historicalAlarmVoList.add(historicalAlarmVo);
        QueryWrapper<MsgAlarmVice> wrapper = new QueryWrapper<>();
        wrapper.eq("alarm_id",msgAlarm.getAlarmId());
        wrapper.orderByDesc("alarm_time");
        List<MsgAlarmVice> list = msgAlarmViceService.list(wrapper);
        for (MsgAlarmVice msgAlarmVice : list) {
            historicalAlarmVo = new HistoricalAlarmVo();
            historicalAlarmVo.setAlarmTime(msgAlarmVice.getAlarmTime());
            historicalAlarmVo.setAlarmType(msgAlarmVice.getAlarmType());
            historicalAlarmVo.setIsLock(2);
            historicalAlarmVoList.add(historicalAlarmVo);
        }
        return AjaxResult.success(historicalAlarmVoList);
    }
}

