/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.baseinfo.rtu.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.baseinfo.rtu.dto.RtuInfoDTO;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.baseinfo.rtu.vo.RtuInfoVO;
import org.springblade.modules.baseinfo.rtu.wrapper.RtuInfoWrapper;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


/***
 * Date:2022/8/22
 * Title: 测站信息管理接口
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/rtu/base")
@AllArgsConstructor
@Api(value = "测站设备基础管理", tags = "测站设备基础管理")
public class RtuBaseInfoController extends BladeController {
	private final IRtuBaseInfoService rtuBaseInfoService;
	private final IDeptService deptService;
	private final IDictBizService dictBizService;
    private final IDeptRegionService deptRegionService;

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页")
	public R<List<RtuInfoVO>> list(@ApiIgnore RtuInfoDTO rtuInfoDTO) {
		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
		wrapper.eq(RtuInfoEntity::getIsDeleted, 0);
		if (null != rtuInfoDTO.getRtuName()) {
			wrapper.like(RtuInfoEntity::getRtuName, rtuInfoDTO.getRtuName());
		}
		if (null != rtuInfoDTO.getRtuCode()) {
			wrapper.like(RtuInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());
		}
		wrapper.orderByAsc(RtuInfoEntity::getRtuCode);
		List<RtuInfoEntity> pages = rtuBaseInfoService.list(wrapper);
		return R.data(RtuInfoWrapper.build().listVO(pages));
	}

	/**
	 * 多表联合查询自定义分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页")
	public R<IPage<RtuInfoVO>> page(@ApiIgnore RtuInfoDTO rtuInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			if (rtuInfoDTO.getAdCode() == null) {
				rtuInfoDTO.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())) {
			if (rtuInfoDTO.getAdCode() == null) {
				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);
				rtuInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		IPage<RtuInfoVO> pages = rtuBaseInfoService.selectPage(Condition.getPage(query), rtuInfoDTO);
		List<RtuInfoVO> list = pages.getRecords();
		for (RtuInfoVO vo : list) {
			RtuInfoWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}


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

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

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

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

}
