/**
 * 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.AllArgsConstructor;
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.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.business.warning.entity.OriginalWarningInfoEntity;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IOriginalWarningService;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.business.warning.vo.OriginalWarningInfoVO;
import org.springblade.modules.business.warning.vo.RtuWarningInfoVO;
import org.springblade.modules.business.warning.wrapper.OriginalWarningWrapper;
import org.springblade.modules.business.warning.wrapper.RtuWarningWrapper;
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")
@AllArgsConstructor
@Api(value = "测站预警管理模块", tags = "测站预警管理模块")
public class RtuWarningController extends BladeController {
	private final IRtuWarningService rtuWarningService;
	private final IOriginalWarningService originalWarningService;
    private final IDeptRegionService deptRegionService;

	@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"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuWarningInfoVO>> page(@ApiIgnore RtuWarningInfoDTO rtuWarningInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		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())) {
			LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
			deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getIsDeleted, 0);
			deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
			DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptRegionEntityLambdaQueryWrapper);
			rtuWarningInfoDTO.setAdCode(deptRegionEntity.getAdcd());
		}
		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("/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);
	}

}
