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
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.Unit;
import org.jeecg.modules.base.entity.UnitConversion;
import org.jeecg.modules.base.service.IUnitConversionService;
import org.jeecg.modules.base.service.IUnitService;
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-26
 * @Version: V1.0
 */
@RestController
@RequestMapping("/base/unit")
@Slf4j
public class UnitController {
 
    @Resource
    private IUnitService momUnitService;
    @Resource
    private IUnitConversionService momUnitConversionService;
    /**
     * 查询数据 根据单位分类id查询计量单位列表
     * @author cj
     * @param unitCategoryId
     * @return
     */
    @GetMapping(value="/getListByUnitCategoryId")
    public Result<List<Unit>> getListByUnitCategoryId(String unitCategoryId){
        Result<List<Unit>> resule = new Result<>();
        List<Unit> list = momUnitService.getListByUnitCategoryId(unitCategoryId);
        resule.setResult(list);
        return  resule;
    }
    /**
     * 增加数据 添加和编辑计量单位信息
     * @author cj
     * @param momUnit
     * @return
     */
    @PostMapping(value = "/addMomUnit")
    public Result<Unit> addMomUnit(@RequestBody Unit momUnit){
        Result<Unit> result = new Result<>();
        try{
            momUnitService.saveOrUpdate(momUnit);
            result.success("操作成功");
        }catch (Exception e){
            result.error500("操作失败");
        }
        return result;
    }
 
    /**
     * 删除数据 删除选中的计量单位信息
     * @author cj
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteMomUnit")
    public Result<Unit> deleteMomUnit(@RequestParam String ids){
        Result<Unit> result = new Result<>();
        if(oConvertUtils.isEmpty(ids)) {
            result.error500("未选中计量单位!");
        }else {
            String[] ls = ids.split(",");
            List<UnitConversion> list = new ArrayList<>();
            List<Long> idList = new ArrayList<>();
            List<String> nameList = new ArrayList<>();
            for (String id : ls) {
                //根据id查询是否有换算信息
                LambdaQueryWrapper<UnitConversion> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(UnitConversion::getUnitId,id);
                list = momUnitConversionService.list(queryWrapper);
                if(list.size()==0){
                    idList.add(Long.parseLong(id) );
                }else{
                    LambdaQueryWrapper<Unit> queryWrapper1 = new LambdaQueryWrapper<>();
                    queryWrapper1.eq(Unit::getId,id);
                    nameList.add(momUnitService.getOne(queryWrapper1).getName());
                }
            }
            if (idList.size() > 0) {
                momUnitService.removeByIds(idList);
                if (ls.length == idList.size()) {
                    result.success("删除成功!");
                } else {
                    result.error500("部分删除成功!("+nameList+"中含有计量单位换算信息无法删除!)");
                }
            }else  {
                result.error500("选择的计量单位都含有计量单位换算信息,无法删除!");
            }
        }
        return result;
    }
 
    /**
     * 查询目标计量单位树结构
     * @author cj
     * @return
     */
    @GetMapping(value = "/getTreeList")
    public Result<List<Unit>> getTreeList(){
        Result<List<Unit>> result = new Result<>();
        List<Unit> list = momUnitService.getTreeList();
        result.setResult(list);
        return result;
    }
 
 
}