cuijian
2023-08-19 bdd0875d4b13a3f1ef472f64d4b6a95e0ef64b22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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;
    }
 
}