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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.region.entity.RegionTreeNodeEntity;
import org.springblade.modules.baseinfo.region.service.IRegionInfoService;
import org.springblade.modules.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.business.warning.entity.RtuWarnSettingEntity;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarnSettingService;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.business.warning.vo.RtuWarningInfoVO;
import org.springblade.modules.business.warning.wrapper.RtuWarningWrapper;
import org.springblade.modules.yjxt.business.warn.entity.OriginalWarningInfoEntity;
import org.springblade.modules.yjxt.business.warn.service.IOriginalWarningService;
import org.springblade.modules.yjxt.business.warn.vo.OriginalWarningInfoVO;
import org.springblade.modules.yjxt.business.warn.wrapper.OriginalWarningWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


/***
 * Date:2022/8/24
 * Title: 预警信息管理模块
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/rtu/warning")
@RequiredArgsConstructor
@Api(value = "测站预警管理模块", tags = "测站预警管理模块")
public class RtuWarningController extends BladeController {
	private final IRtuWarningService rtuWarningService;
	private final IOriginalWarningService originalWarningService;
	private final IDeptRegionService deptRegionService;
	private final IRegionInfoService regionService;
	private final IRtuWarnSettingService warnSettingService;

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

	@GetMapping("/list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "列表", notes = "传入RtuWarningInfoDTO")
	public R<List<RtuWarningInfoVO>> list(RtuWarningInfoDTO rtuWarningInfoDTO) {
		LambdaQueryWrapper<RtuWarningInfoEntity> wrapper = Wrappers.<RtuWarningInfoEntity>query().lambda();
		if (null != rtuWarningInfoDTO && null != rtuWarningInfoDTO.getRtuCode()) {
			wrapper.eq(RtuWarningInfoEntity::getRtuCode, rtuWarningInfoDTO.getRtuCode());
		}
		wrapper.orderByAsc(RtuWarningInfoEntity::getWarningRecoveryTime);
		wrapper.orderByDesc(RtuWarningInfoEntity::getCreateTime);
		List<RtuWarningInfoEntity> pages = rtuWarningService.list(wrapper);
		return R.data(RtuWarningWrapper.build().listVO(pages));
	}

	/**
	 * 分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "warningStatus", value = "测站名称", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> page(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		if (rtuWarningInfoDTO.getAdCode() != null) {
			RegionTreeNodeEntity node = this.regionService.getRegionNode(user.getRoleName(), user.getDeptId(), rtuWarningInfoDTO.getAdCode());
			if (null != node) {
				rtuWarningInfoDTO.setExcludeCode(node.getExclude());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	@GetMapping("/clock/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> clockPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectClockPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	/**
	 * 分页
	 */
	@GetMapping("/offline/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> offlinePage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectOfflinePage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}


	/**
	 * 其他异常列表
	 * @param rtuWarningInfoDTO
	 * @param query
	 * @return
	 */
	@GetMapping("/otherswarn/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> othersWarnPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectOthersWarnPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}


	/**
	 * 分页
	 */
	@GetMapping("/hourmissout/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> hourMissOutPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectHourMissOutPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	@GetMapping("/rain/hourmissout/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> rainHourMissOutPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectRainHourMissOutPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	@GetMapping("/river/hourmissout/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> riverHourMissOutPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectRiverHourMissOutPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	/**
	 * 分页
	 */
	@GetMapping("/minupdelay/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> minUpDelayPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectMinUpDelayPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	/**
	 * 分页
	 */
	@GetMapping("/hourupdelay/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> hourUpDelayPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectHourUpDelayPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	@GetMapping("/rain/hourupdelay/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> rainHourUpDelayPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectRainHourUpDelayPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}


	@GetMapping("/river/hourupdelay/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> riverHourUpDelayPage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectRiverHourUpDelayPage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}


	@GetMapping("/outltervalue/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "deptId", value = "部门ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> outlterValuePage(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (rtuWarningInfoDTO.getDeptId() != null && rtuWarningInfoDTO.getDeptId() > 0) {
			DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(rtuWarningInfoDTO.getDeptId());
			if (null == deptRegionEntity) {
				return R.status(false);
			}
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		} else {
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				rtuWarningInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
				DeptRegionEntity deptRegionEntity = deptRegionService.getRegionByDeptId(Func.toLong(user.getDeptId()));
				if (null == deptRegionEntity) {
					return R.status(false);
				}
				rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuWarningInfoVO> pages = rtuWarningService.selectOutlierValuePage(Condition.getPage(query), rtuWarningInfoDTO);
		List<RtuWarningInfoVO> list = pages.getRecords();
		for (RtuWarningInfoVO vo : list) {
			RtuWarningWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入RtuWarningInfoEntity")
	public R<RtuWarningInfoVO> detail(RtuWarningInfoEntity rtuWarningInfoEntity) {
		RtuWarningInfoEntity detail = rtuWarningService.getOne(Condition.getQueryWrapper(rtuWarningInfoEntity));
		return R.data(RtuWarningWrapper.build().entityVO(detail));
	}

	@GetMapping("/original/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入RtuWarningInfoEntity")
	public R<OriginalWarningInfoVO> originalWarnDetail(RtuWarningInfoEntity rtuWarningInfoEntity) {
		RtuWarningInfoEntity detail = rtuWarningService.getById(rtuWarningInfoEntity.getId());
		OriginalWarningInfoEntity originalDetail = originalWarningService.getById(detail.getDataFromId());
		return R.data(OriginalWarningWrapper.build().entityVO(originalDetail));
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入RtuWarningInfoEntity")
	public R save(@RequestBody RtuWarningInfoEntity rtuWarningInfoEntity) {
		return R.status(rtuWarningService.save(rtuWarningInfoEntity));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入RtuWarningInfoEntity")
	public R update(@RequestBody RtuWarningInfoEntity rtuWarningInfoEntity) {
		return R.status(rtuWarningService.updateById(rtuWarningInfoEntity));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入RtuWarningInfoEntity")
	public R submit(@RequestBody RtuWarningInfoEntity rtuWarningInfoEntity) {
		return R.status(rtuWarningService.saveOrUpdate(rtuWarningInfoEntity));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入id")
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		boolean temp = rtuWarningService.deleteLogic(Func.toLongList(ids));
		return R.status(temp);
	}


	/**
	 * 查询预警配置
	 */
	@GetMapping("/setting/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "设置详情")
	public R<RtuWarnSettingEntity> getSettingDetail() {
		LambdaQueryWrapper<RtuWarnSettingEntity> wrapper = Wrappers.<RtuWarnSettingEntity>query().lambda();
		wrapper.eq(RtuWarnSettingEntity::getIsDeleted, 0);
		List<RtuWarnSettingEntity> list = warnSettingService.list(wrapper);
		if (null != list && list.size() > 0) {
			return R.data(list.get(0));
		} else {
			RtuWarnSettingEntity entity = new RtuWarnSettingEntity();
			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 = "传入实体类RtuWarnSettingEntity")
	public R settingSubmit(@RequestBody RtuWarnSettingEntity warnSettingEntity) {
		if (warnSettingEntity.getId() <= 0L) {
			warnSettingEntity.setId(null);
		}
		//warnSettingEntity.setWarnHourUpDelayActive(warnSettingEntity.getWarnMinUpDelayActive());
		//warnSettingEntity.setWarnHourUpDelayAutoTask(warnSettingEntity.getWarnDelayAutoTask());
		warnSettingEntity.setWarnRsvrOfflineDuration(warnSettingEntity.getWarnRiverOfflineDuration());
		warnSettingEntity.setWarnRsvrDelayHourDuration(warnSettingEntity.getWarnRiverDelayHourDuration());
		warnSettingEntity.setWarnRsvrDelayMinDuration(warnSettingEntity.getWarnRiverDelayMinDuration());
		return R.status(warnSettingService.saveOrUpdate(warnSettingEntity));
	}

}
