package org.jeecg.modules.base.controller;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import lombok.extern.slf4j.Slf4j;
|
import org.jeecg.common.api.vo.Result;
|
import org.jeecg.common.util.oConvertUtils;
|
import org.jeecg.modules.base.entity.UnitCategory;
|
import org.jeecg.modules.base.service.IUnitCategoryService;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* @Title: Controller
|
* @Description: 计量单位分类表 前端控制器
|
* @Author: cuijian
|
* @Date: 2022-10-19
|
* @Version: V1.0
|
*/
|
@RestController
|
@RequestMapping("/base")
|
@Slf4j
|
public class UnitCategoryController {
|
|
@Resource
|
private IUnitCategoryService unitCategoryService;
|
/**
|
* 查询数据 查出计量单位分类,并以树结构数据格式响应给前端
|
* @author cj
|
* @return
|
*/
|
@GetMapping(value="/getTree")
|
public Result<List<UnitCategory>> getTreeList(){
|
Result<List<UnitCategory>> resule = new Result<>();
|
List<UnitCategory> treeList = unitCategoryService.getTreeList();
|
resule.setResult(treeList);
|
return resule;
|
}
|
|
/**
|
* 查询数据 查出计量单位分类和分类下的计量单位,并以树结构数据格式响应给前端
|
* @author cj
|
* @return
|
*/
|
@GetMapping(value="/getUnitTree")
|
public Result<List<UnitCategory>> getUnitTreeList(){
|
Result<List<UnitCategory>> resule = new Result<>();
|
List<UnitCategory> treeList = unitCategoryService.getUnitTreeList();
|
resule.setResult(treeList);
|
return resule;
|
}
|
|
/**
|
* 查询数据 根据计量单位分类,查询出该计量单位下的计量单位
|
* @author cj
|
* @param id
|
* @return
|
*/
|
@GetMapping(value="/getList")
|
public Result<List<UnitCategory>> getList(@RequestParam String id){
|
Result<List<UnitCategory>> resule = new Result<>();
|
//id前端传空字符串时,查询pid为-1的数据返回
|
if (id == "") {
|
LambdaQueryWrapper<UnitCategory> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.eq(UnitCategory::getPid,"-1");
|
List<UnitCategory> treeList = unitCategoryService.getList(unitCategoryService.getOne(queryWrapper).getId());
|
resule.setResult(treeList);
|
}else{
|
List<UnitCategory> treeList = unitCategoryService.getList(id);
|
resule.setResult(treeList);
|
}
|
return resule;
|
}
|
|
/**
|
* 增加数据 添加计量单位分类信息
|
* @author cj
|
* @param unitCategory
|
* @return
|
*/
|
@PostMapping(value = "/add")
|
public Result<UnitCategory> add(@RequestBody UnitCategory unitCategory){
|
Result<UnitCategory> result = new Result<>();
|
try{
|
unitCategoryService.saveOrUpdate(unitCategory);
|
result.success("操作成功");
|
}catch (Exception e){
|
result.error500("操作失败");
|
}
|
return result;
|
}
|
|
/**
|
* 删除数据 删除选中的计量单位
|
* @author cj
|
* @param ids
|
* @return
|
*/
|
@DeleteMapping(value = "/delete")
|
public Result<UnitCategory> deleteCategory(@RequestParam String ids){
|
Result<UnitCategory> result = new Result<>();
|
if(oConvertUtils.isEmpty(ids)) {
|
result.error500("未选中计量单位分类!");
|
}else {
|
String[] ls = ids.split(",");
|
List<UnitCategory> list = new ArrayList<>();
|
List<Long> idList = new ArrayList<>();
|
List<String> nameList = new ArrayList<>();
|
for (String id : ls) {
|
//根据id查询是否有子分类
|
LambdaQueryWrapper<UnitCategory> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.eq(UnitCategory::getPid,id);
|
list = unitCategoryService.list(queryWrapper);
|
if(list.size()==0){
|
idList.add(Long.parseLong(id) );
|
}else{
|
LambdaQueryWrapper<UnitCategory> queryWrapper1 = new LambdaQueryWrapper<>();
|
queryWrapper1.eq(UnitCategory::getId,id);
|
nameList.add(unitCategoryService.getOne(queryWrapper1).getCode()+unitCategoryService.getOne(queryWrapper1).getName());
|
}
|
}
|
if (idList.size() > 0) {
|
unitCategoryService.removeByIds(idList);
|
if (ls.length == idList.size()) {
|
result.success("删除成功!");
|
} else {
|
result.error500("部分删除成功!("+nameList+"中含有子分类无法删除!)");
|
}
|
}else {
|
result.error500("选择的计量单位分类都含有子分类,无法删除!");
|
}
|
}
|
return result;
|
}
|
|
}
|