郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.gk.hotwork.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.hotwork.Domain.ElementManagement;
import com.gk.hotwork.Domain.ElementTree;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.UserInfo;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Mapper.ElementManagementMapper;
import com.gk.hotwork.Service.ElementManagementService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Service("elementManagementService")
@Transactional
public class ElementManagementImpl extends ServiceImpl<ElementManagementMapper,ElementManagement> implements ElementManagementService {
 
    @Autowired
    private ElementManagementMapper elementManagementMapper;
 
    /**
    * @Description: 分页
    */
    @Override
    public IPage<ElementManagement> selectPage(Page<ElementManagement> page, Map<String, Object> filter, UserInfo user) {
        return elementManagementMapper.selectPages(page,filter);
    }
 
 
    /**
    * @Description: 新增
    */
    @Override
    public void addOne(ElementManagement param, UserInfo user) {
        requiredVerification(param);
 
        param.setValidFlag(Boolean.TRUE);
        param.setUpdateBy(user.getRealname());
        param.setCreateBy(user.getRealname());
        param.setUpdateTime(new Date());
        param.setCreateTime(new Date());
        this.save(param);
    }
 
 
    /**
    * @Description: 修改
    */
    @Override
    public void modOne(ElementManagement param, UserInfo user) {
        selectVerification(param.getId());
        requiredVerification(param);
 
        param.setUpdateTime(new Date());
        param.setUpdateBy(user.getRealname());
        this.updateById(param);
    }
 
    /**
    * @Description: 删除
    */
    @Override
    public void delOne(Long id, UserInfo user) {
        ElementManagement elementManagement = selectVerification(id);
        deleteVerification(elementManagement);
 
        ElementManagement delOne = new ElementManagement();
        delOne.setId(id);
        delOne.setUpdateTime(new Date());
        delOne.setUpdateBy(user.getRealname());
        delOne.setValidFlag(Boolean.FALSE);
        this.updateById(delOne);
    }
 
    @Override
    public List<ElementManagement> getParentElement() {
        return elementManagementMapper.getElementByType(0);
    }
 
    @Override
    public List<ElementTree> getElementTree() {
        List<ElementTree> elementTreeList = new ArrayList<>();
 
        List<ElementManagement> list1 = elementManagementMapper.getElementByType(0);
        List<ElementManagement> list2 = elementManagementMapper.getElementByType(1);
 
        if (CollectionUtils.isNotEmpty(list1)){
            for (ElementManagement elementManagement : list1){
                ElementTree elementTree=new ElementTree();
                elementTree.setType(0);
                elementTree.setLabel(elementManagement.getName());
                elementTree.setValue(elementManagement.getId());
                elementTreeList.add(elementTree);
            }
        }
        if (CollectionUtils.isNotEmpty(list2)) {
            for (int i = 0; i < list2.size(); i++) {
                for (int j = 0; j < elementTreeList.size(); j++) {
                    if (list2.get(i).getParentId().equals(elementTreeList.get(j).getValue())) {
                        if (CollectionUtils.isEmpty(elementTreeList.get(j).getChildren())) {
                            //如果为空 则new一个出来
                            elementTreeList.get(j).setChildren(new ArrayList<>());
                        }
                        ElementTree elementTree = new ElementTree();
                        elementTree.setType(1);
                        elementTree.setLabel(list2.get(i).getName());
                        elementTree.setValue(list2.get(i).getId());
                        elementTreeList.get(j).getChildren().add(elementTree);
                    }
                }
            }
        }
        return elementTreeList;
    }
 
 
    /**
     * 查询验证
     * 验证对象存在
     */
    public ElementManagement selectVerification(Long id){
        if (id == null) throw new BusinessException("id传参不能为空");
        ElementManagement elementManagement = this.getById(id);
        if (elementManagement ==  null) throw new BusinessException("找不到对应实体");
        return elementManagement;
    }
 
    /**
     * 操作验证
     * 验证必填项
     *
     */
    public void requiredVerification(ElementManagement param){
        if(StringUtils.isBlank(param.getName())) throw new BusinessException("请填写要素名称");
        if (param.getType() == null) throw new BusinessException("请选择要素类型");
        if (param.getType() == 1 && param.getParentId()==null){
            throw new BusinessException("请选择父要素");
        }
    }
 
    /**
     * 操作验证
     * 删除已被B级要素绑定的A级要素
     *
     */
    public void deleteVerification(ElementManagement param){
        if (param.getType()==0){
            int count = this.count(new LambdaQueryWrapper<ElementManagement>()
                    .eq(ElementManagement::getParentId, param.getId())
                    .eq(ElementManagement::getType , 1)
                    .eq(ElementManagement::getValidFlag, Boolean.TRUE));
            if (count > 0) throw new BusinessException("当前A级要素仍然有已绑定的B级要素,无法删除");
        }
    }
}