/**
* Copyright (c) 2016-2019 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package io.renren.modules.sys.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysPlantEntity;
import io.renren.modules.sys.entity.SysUserPlantEntity;
import io.renren.modules.sys.service.SysPlantService;
import io.renren.modules.sys.service.SysUserPlantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 装置管理
*
* @author Mark 735032128@qq.com
*/
@RestController
@RequestMapping("/sys/plant")
public class SysPlantController extends AbstractController {
@Autowired
private SysPlantService sysPlantService;
@Autowired
private SysUserPlantService sysUserPlantService;
/**
* 列表
*/
@RequestMapping("/list")
@RequiresPermissions("sys:plant:list")
public List list() {
List plantList = sysPlantService.queryList(new HashMap());
return plantList;
}
/**
* 用户列表
*/
@RequestMapping("/mylist")
public List mylist() {
List userPlantList = sysUserPlantService.list(new QueryWrapper()
.eq("user_id" ,getUserId()));
List plantList = new ArrayList<>();
for (SysUserPlantEntity s : userPlantList
) {
SysPlantEntity sysPlantEntity = sysPlantService.getById(s.getPlantId());
if (sysPlantEntity != null)
plantList.add(sysPlantEntity);
}
return plantList;
}
/**
* 选择装置(添加、修改菜单)
*/
@RequestMapping("/select")
@RequiresPermissions("sys:plant:select")
public R select() {
List plantList = sysPlantService.queryList(new HashMap());
//添加一级装置
if (getUserId() == Constant.SUPER_ADMIN) {
SysPlantEntity root = new SysPlantEntity();
root.setPlantId(0L);
root.setName("一级装置");
root.setParentId(-1L);
root.setOpen(true);
plantList.add(root);
}
return R.ok().put("plantList", plantList);
}
/**
* 上级装置Id(管理员则为0)
*/
@RequestMapping("/info")
@RequiresPermissions("sys:plant:list")
public R info() {
long plantId = 0;
if (getUserId() != Constant.SUPER_ADMIN) {
List plantList = sysPlantService.queryList(new HashMap());
Long parentId = null;
for (SysPlantEntity sysPlantEntity : plantList) {
if (parentId == null) {
parentId = sysPlantEntity.getParentId();
continue;
}
if (parentId > sysPlantEntity.getParentId().longValue()) {
parentId = sysPlantEntity.getParentId();
}
}
plantId = parentId;
}
return R.ok().put("plantId", plantId);
}
/**
* 信息
*/
@RequestMapping("/info/{plantId}")
@RequiresPermissions("sys:plant:info")
public R info(@PathVariable("plantId") Long plantId) {
SysPlantEntity plant = sysPlantService.getById(plantId);
return R.ok().put("plant", plant);
}
/**
* 保存
*/
@RequestMapping("/save")
@RequiresPermissions("sys:plant:save")
public R save(@RequestBody SysPlantEntity plant) {
sysPlantService.save(plant);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
@RequiresPermissions("sys:plant:update")
public R update(@RequestBody SysPlantEntity plant) {
sysPlantService.updateById(plant);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete/{plantId}")
@RequiresPermissions("sys:plant:delete")
public R delete(@PathVariable("plantId") long plantId) {
//判断是否有子装置
List plantList = sysPlantService.queryPlantIdList(plantId);
if (plantList.size() > 0) {
return R.error("请先删除子装置");
}
sysPlantService.removeById(plantId);
return R.ok();
}
}