/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/24
 */
package org.springblade.modules.baseinfo.warn.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.baseinfo.warn.entity.BaseWarnSettingEntity;
import org.springblade.modules.baseinfo.warn.service.IBaseWarnSettingService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/***
 * Date:2022/8/24
 * Title: 预警信息管理模块
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/base/warn")
@RequiredArgsConstructor
@Api(value = "测站预警管理模块", tags = "测站预警管理模块")
public class BaseWarnController extends BladeController {

    private final IBaseWarnSettingService warnSettingService;

    @Value("${spring.warn-config.missout}")
    private Integer missout;


    /**
     * 查询预警配置
     */
    @GetMapping("/setting/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "设置详情")
    public R<BaseWarnSettingEntity> getSettingDetail() {
        LambdaQueryWrapper<BaseWarnSettingEntity> wrapper = Wrappers.<BaseWarnSettingEntity>query().lambda();
        wrapper.eq(BaseWarnSettingEntity::getIsDeleted, 0);
        List<BaseWarnSettingEntity> list = warnSettingService.list(wrapper);
        if (null != list && list.size() > 0) {
            return R.data(list.get(0));
        } else {
            BaseWarnSettingEntity entity = new BaseWarnSettingEntity();
            entity.setWarnActive(0);
            entity.setWarnClockActive(0);
            entity.setWarnHourUpDelayActive(0);
            entity.setWarnMinUpDelayActive(0);
            entity.setWarnOfflineActive(0);
            entity.setWarnMissOutActive(0);
            entity.setWarnOutlierValueActive(0);
            entity.setWarnClockAutoTask(0);
            entity.setWarnHourUpDelayAutoTask(0);
            entity.setWarnDelayAutoTask(0);
            entity.setWarnOfflineAutoTask(0);
            entity.setWarnMissOutAutoTask(0);
            entity.setWarnOutlierValueAutoTask(0);
            entity.setWarnRainDelayHourDuration(10);
            entity.setWarnRiverDelayMinDuration(10);
            entity.setWarnRsvrDelayMinDuration(10);
            entity.setWarnGroundDelayMinDuration(10);
            entity.setWarnRainDelayHourDuration(120);
            entity.setWarnRiverDelayHourDuration(120);
            entity.setWarnRsvrDelayHourDuration(120);
            entity.setWarnGroundDelayHourDuration(120);
            entity.setWarnRainOfflineDuration(24);
            entity.setWarnRiverOfflineDuration(24);
            entity.setWarnRsvrOfflineDuration(24);
            entity.setWarnGroundOfflineDuration(24);
            entity.setWarnMissNum(missout);
            return R.data(entity);
        }
    }

    /**
     * 提交预警配置
     */
    @PostMapping("/setting/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "")
    public R settingSubmit(@RequestBody BaseWarnSettingEntity warnSettingEntity) {
        if (warnSettingEntity.getId() <= 0L) {
            warnSettingEntity.setId(null);
        }
        warnSettingEntity.setWarnRsvrOfflineDuration(warnSettingEntity.getWarnRiverOfflineDuration());
        warnSettingEntity.setWarnRsvrDelayHourDuration(warnSettingEntity.getWarnRiverDelayHourDuration());
        warnSettingEntity.setWarnRsvrDelayMinDuration(warnSettingEntity.getWarnRiverDelayMinDuration());
        return R.status(warnSettingService.saveOrUpdate(warnSettingEntity));
    }

}
