package com.yuchen.saas.park.service.controller.alarm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.entity.park.PmgBatHandlerWarning;
import com.yuchen.saas.park.api.entity.park.PmgBatWarningInfo;
import com.yuchen.saas.park.service.service.IHandlerWarningService;
import com.yuchen.saas.park.service.service.IWarningInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.redis.cache.NestRedis;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author 吕海泉
 * @ClassName HandlerWarningController
 * @Description
 * @Date 2021/4/7 15:55
 * @Version 1.0
 **/

@RestController
@RequestMapping("/handler")
@Api(value = "告警事件处理", tags = "告警事件处理表")
public class HandlerWarningController implements InstantiationAwareBeanPostProcessor {
    @Resource
    private IHandlerWarningService iHandlerWarningService;
    //private IHandlerWarningServiceTest iHandlerWarningService;

    @Resource
    private IWarningInfoService iWarningInfoService;

    @Resource
    private NestRedis nestRedis;

    /**
     * 查询 告警事件
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询 告警事件", notes = "查询 告警事件")
    public R<PmgBatHandlerWarning> detail(PmgBatHandlerWarning PmgBatHandlerWarning) {
        PmgBatHandlerWarning detail = iHandlerWarningService.getOne(Condition.getQueryWrapper(PmgBatHandlerWarning));
        return R.data(detail);
    }

    /**
     * 分页 告警事件处理表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页 告警事件处理表", notes = "分页 告警事件处理表")
    public R<IPage<PmgBatHandlerWarning>> list(PmgBatHandlerWarning PmgBatHandlerWarning, Query query) {
        System.out.println("*********:" + PmgBatHandlerWarning);
        IPage<PmgBatHandlerWarning> pages = iHandlerWarningService.page(Condition.getPage(query), Condition.getQueryWrapper(PmgBatHandlerWarning));
        return R.data(pages);
    }

    /**
     * 根据pk、pn 查找处理记录
     */
    @GetMapping("/byPkAndPn")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "根据pk、pn 查找处理记录", notes = "根据pk、pn 查找处理记录")
    public R<PmgBatHandlerWarning> getHandlerResult(@RequestParam String productKey, @RequestParam String productName) {
        LambdaQueryWrapper<PmgBatHandlerWarning> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmgBatHandlerWarning::getProductKey, productKey)
                .eq(PmgBatHandlerWarning::getProductName, productName)
                .orderByDesc(PmgBatHandlerWarning::getCreateTime)
                .last("LIMIT 1");
        PmgBatHandlerWarning ipmgBatHandlerWarning = iHandlerWarningService.getOne(queryWrapper);
        return R.data(ipmgBatHandlerWarning);
    }

    /**
     * 新增 告警事件处理表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增 告警事件处理", notes = "新增 告警事件处理")
    public R save(@Valid @RequestBody PmgBatHandlerWarning pmgBatHandlerWarning) {
        long id = System.currentTimeMillis() / 1000L;
        // 根据pk、dn、eventTime查询该时间正负两分钟的数据
        if (Integer.valueOf(pmgBatHandlerWarning.getHandlerModel()) == 1) {
            LambdaQueryWrapper<PmgBatWarningInfo> pmgBatWarningInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            pmgBatWarningInfoLambdaQueryWrapper.eq(PmgBatWarningInfo::getProductKey, pmgBatHandlerWarning.getProductKey())
                    .eq(PmgBatWarningInfo::getDeviceName, pmgBatHandlerWarning.getProductName())
                    .between(PmgBatWarningInfo::getEventTime, pmgBatHandlerWarning.getEventTime() - 120, pmgBatHandlerWarning.getEventTime() + 120);
            List<PmgBatWarningInfo> pmgBatWarningInfoList = iWarningInfoService.list(pmgBatWarningInfoLambdaQueryWrapper);

            for (PmgBatWarningInfo pmgBatWarningInfo : pmgBatWarningInfoList) {
                pmgBatWarningInfo.setIsHandling("1");
                iWarningInfoService.updateById(pmgBatWarningInfo);
            }
            String key = "event" + ":" + id;
            long time = pmgBatHandlerWarning.getEndTime() - pmgBatHandlerWarning.getStartTime();
            if (time > 0 && (pmgBatHandlerWarning.getEndTime() > System.currentTimeMillis() / 1000L)) {
                nestRedis.setEx(key, key, time);
            }
        }
        pmgBatHandlerWarning.setId(id);
        return R.status(iHandlerWarningService.save(pmgBatHandlerWarning));
    }

    /**
     * 批量新增 告警事件处理表
     */
    @PostMapping("/saveList")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量新增 告警事件处理表", notes = "批量新增 告警事件处理表")
    public R saveList(@Valid @RequestBody List<PmgBatHandlerWarning> PmgBatHandlerWarning) {
        return R.status(iHandlerWarningService.saveOrUpdateBatch(PmgBatHandlerWarning));
    }

    /**
     * 修改 告警事件处理表
     */
    @PutMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改 告警事件处理表", notes = "修改 告警事件处理表")
    public R update(@Valid @RequestBody PmgBatHandlerWarning PmgBatHandlerWarning) {
        return R.status(iHandlerWarningService.updateById(PmgBatHandlerWarning));
    }

    /**
     * 删除 告警事件处理表
     */
    @DeleteMapping("/remove")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除 告警事件处理表", notes = "删除 告警事件处理表")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(iHandlerWarningService.deleteLogic(Func.toLongList(ids)));
    }
}


