Lius
2025-07-03 1b1942e6b2755ae3fc3b3a5989615c9f8544cf34
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package org.jeecg.modules.eam.base.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.util.FillRuleUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.eam.base.entity.EamBaseRepairDepart;
import org.jeecg.modules.eam.base.service.IEamBaseRepairDepartService;
import org.jeecg.modules.eam.base.mapper.EamBaseRepairDepartMapper;
import org.jeecg.modules.eam.base.model.EamBaseRepairDepartTreeModel;
import org.jeecg.modules.eam.base.model.RepairDepartIdModel;
import org.jeecg.modules.eam.base.util.FindsRepairDepartsChildrenUtil;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * @Author: Lius
 * @CreateTime: 2025-07-01
 * @Description:
 */
@Service
public class EamBaseRepairDepartImpl extends ServiceImpl<EamBaseRepairDepartMapper, EamBaseRepairDepart> implements IEamBaseRepairDepartService {
 
    @Resource
    private SysUserMapper sysUserMapper;
 
    /**
     * queryTreeList 对应 queryTreeList 查询所有的维修部门班组数据,以树结构形式响应给前端
     */
    @Override
    @Cacheable(value = "eam:cache:repairDepart:alldata")
    public List<EamBaseRepairDepartTreeModel> queryTreeList() {
        LambdaQueryWrapper<EamBaseRepairDepart> query = new LambdaQueryWrapper<EamBaseRepairDepart>();
        query.eq(EamBaseRepairDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(EamBaseRepairDepart::getDepartOrder);
        List<EamBaseRepairDepart> list = this.list(query);
        //设置用户id,让前台显示
//        this.setUserIdsByProList(list);
        //调用wrapTreeDataToTreeList方法生成树状数据
        return FindsRepairDepartsChildrenUtil.wrapTreeDataToTreeList(list);
    }
 
    /**
     * queryTreeList 根据维修部门班组id查询,前端回显调用
     */
    @Override
    public List<EamBaseRepairDepartTreeModel> queryTreeList(String ids) {
        List<EamBaseRepairDepartTreeModel> listResult = new ArrayList<>();
        LambdaQueryWrapper<EamBaseRepairDepart> query = new LambdaQueryWrapper<EamBaseRepairDepart>();
        query.eq(EamBaseRepairDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        if (oConvertUtils.isNotEmpty(ids)) {
            query.in(true, EamBaseRepairDepart::getId, ids.split(","));
        }
        query.orderByAsc(EamBaseRepairDepart::getDepartOrder);
        List<EamBaseRepairDepart> list = this.list(query);
        for (EamBaseRepairDepart repairDepart : list) {
            listResult.add(new EamBaseRepairDepartTreeModel(repairDepart));
        }
        return listResult;
    }
 
    @Override
    @Cacheable(value = "eam:cache:repairDepart:allids")
    public List<RepairDepartIdModel> queryMaintainDepartIdTreeList() {
        LambdaQueryWrapper<EamBaseRepairDepart> query = new LambdaQueryWrapper<>();
        query.eq(EamBaseRepairDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(EamBaseRepairDepart::getDepartOrder);
        List<EamBaseRepairDepart> list = this.list(query);
        //调用wrapTreeDataToTreeList方法生成树状数据
        return FindsRepairDepartsChildrenUtil.wrapTreeDataToMaintainDepartIdTreeList(list);
    }
 
    /**
     * 根据关键字搜索相关的部门数据
     */
    @Override
    public List<EamBaseRepairDepartTreeModel> searchByKeyWord(String keyWord) {
        LambdaQueryWrapper<EamBaseRepairDepart> query = new LambdaQueryWrapper<>();
        List<EamBaseRepairDepartTreeModel> newList = new ArrayList<>();
        query.like(EamBaseRepairDepart::getDepartName, keyWord);
        EamBaseRepairDepartTreeModel model = new EamBaseRepairDepartTreeModel();
        List<EamBaseRepairDepart> repairDepartList = this.list(query);
        if (!repairDepartList.isEmpty()) {
            for (EamBaseRepairDepart eamBaseRepairDepart : repairDepartList) {
                model = new EamBaseRepairDepartTreeModel(eamBaseRepairDepart);
                model.setChildren(null);
                newList.add(model);
            }
            return newList;
        }
        return Collections.emptyList();
    }
 
    /**
     * saveMaintainDepartData 对应 add 保存用户在页面添加的新的维修部门班组对象数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMaintainDepartData(EamBaseRepairDepart eamBaseRepairDepart) {
        if (eamBaseRepairDepart != null) {
            if (eamBaseRepairDepart.getParentId() == null) {
                eamBaseRepairDepart.setParentId("");
            }
            eamBaseRepairDepart.setId(IdWorker.getIdStr(eamBaseRepairDepart));
            // 先判断该对象有无父级ID,有则意味着不是最高级,否则意味着是最高级
            // 获取父级ID
            String parentId = eamBaseRepairDepart.getParentId();
            JSONObject formData = new JSONObject();
            formData.put("parentId",parentId);
            String[] codeArray = (String[]) FillRuleUtil.executeRule(FillRuleConstant.REPAIRDEPART,formData);
            eamBaseRepairDepart.setOrgCode(codeArray[0]);
            String orgType = codeArray[1];
            eamBaseRepairDepart.setOrgType(String.valueOf(orgType));
            eamBaseRepairDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            this.save(eamBaseRepairDepart);
        }
    }
 
    /**
     * updateMaintainDepartDataById 对应 edit 根据维修部门班组主键来更新对应的维修部门班组数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMaintainDepartDataById(EamBaseRepairDepart eamBaseRepairDepart) {
        if (eamBaseRepairDepart != null) {
            this.updateById(eamBaseRepairDepart);
            return true;
        }
        return false;
    }
 
    /**
     * 根据维修部门班组id删除并删除其可能存在的子级维修部门班组
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(String id) {
        List<String> idList = new ArrayList<>();
        idList.add(id);
        this.checkChildrenExists(id, idList);
        boolean result = this.removeByIds(idList);
        //根据维修部门班组id删除用户与维修部门班组关系
//        repairDepartUserMapper.delete(new LambdaQueryWrapper<EamBaseRepairDepartUser>().in(EamBaseRepairDepartUser::getDepartId, idList));
        //根据维修部门班组id删除维修部门班组与设备关系
//        repairDepartEquipmentMapper.delete(new LambdaQueryWrapper<SysMaintainDepartEquipment>().in(SysMaintainDepartEquipment::getMaintainDepartId, idList));
        return result;
    }
 
    /**
     * 根据维修部门班组id批量删除并删除其可能存在的子级维修部门班组
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatchWithChildren(List<String> ids) {
        List<String> idList = new ArrayList<>();
        for (String id : ids) {
            idList.add(id);
            this.checkChildrenExists(id, idList);
        }
        this.removeByIds(idList);
        //根据维修部门班组id删除用户与维修部门班组关系
//        repairDepartUserMapper.delete(new LambdaQueryWrapper<EamBaseRepairDepartUser>().in(EamBaseRepairDepartUser::getDepartId, idList));
        //根据维修部门班组id删除维修部门班组与设备关系
//        repairDepartEquipmentMapper.delete(new LambdaQueryWrapper<SysMaintainDepartEquipment>().in(SysMaintainDepartEquipment::getMaintainDepartId, idList));
    }
 
//    /**
//     * 根据id查询下级维修部门班组
//     */
//    @Override
//    public List<EamBaseRepairDepart> queryRepDepByPid(String pid) {
//        return this.baseMapper.queryRepDepByPid(pid);
//    }
//
//    /**
//     * 根据用户id获取维修部门班组下拉树选项
//     */
//    @Override
//    public List<RepairDepartIdModel> loadMaintainDepartTreeOptions(String userId) {
//        //获取所有维修部门班组数据
//        List<EamBaseRepairDepart> repairDepartList = this.baseMapper.selectList(new LambdaQueryWrapper<EamBaseRepairDepart>().eq(EamBaseRepairDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(EamBaseRepairDepart::getDepartOrder));
//        //根据用户id获取拥有的维修部门班组信息集合
//        List<String> repairDepartIds = repairDepartUserMapper.queryRepairDepartIdsByUserId(userId);
//        List<String> allMaintainDepartIds = new ArrayList<>();
//        //找到所有维修部门班组id的上级id
//        if (repairDepartIds != null && !repairDepartIds.isEmpty()) {
//            for (String repairDepartId : repairDepartIds) {
//                this.getAllMaintainDepartIds(repairDepartList, repairDepartId, allMaintainDepartIds);
//            }
//        }
//        //过滤维修部门班组数据
//        List<EamBaseRepairDepart> list = repairDepartList.stream().filter((EamBaseRepairDepart eamBaseRepairDepart) -> allMaintainDepartIds.contains(eamBaseRepairDepart.getId())).collect(Collectors.toList());
//        return FindsRepairDepartsChildrenUtil.wrapTreeDataToMaintainDepartIdTreeList(list);
//    }
//
//    /**
//     * 递归查询所有子节点id
//     */
//    @Override
//    public List<String> recursionChildren(String repairDepartId) {
//        return this.baseMapper.recursionChildren(repairDepartId);
//    }
//
//    /**
//     * 根据用户id和车间id获取用户拥有的车间id
//     * @param userId
//     * @param repairDepartId
//     * @return
//     */
//    @Override
//    public String findFirstMaintainDepart(String userId, String repairDepartId) {
//        return this.baseMapper.findFirstMaintain(userId, repairDepartId);
//    }
//
//    /**
//     * 根据用户id查询用户维修部门班组权限
//     */
//    @Override
//    public String findThreeMaintainDepartId(String userId) {
//        return this.baseMapper.findThreeMaintainId(userId);
//    }
//
//    @Override
//    public EamBaseRepairDepart findMaintainDepartList(String equipmentId) {
//        return this.baseMapper.findMaintainList(equipmentId);
//    }
 
    /**
     * delete 方法调用 递归查找子集id
     */
    private void checkChildrenExists(String id, List<String> idList) {
        LambdaQueryWrapper<EamBaseRepairDepart> query = new LambdaQueryWrapper<>();
        query.eq(EamBaseRepairDepart::getParentId, id);
        List<EamBaseRepairDepart> repairDepartList = this.list(query);
        if (repairDepartList != null && !repairDepartList.isEmpty()) {
            for (EamBaseRepairDepart repairDepart : repairDepartList) {
                idList.add(repairDepart.getId());
                this.checkChildrenExists(repairDepart.getId(), idList);
            }
        }
    }
 
//    /**
//     * 获取所有的维修部门班组id(包含所有上级)
//     */
//    private void getAllMaintainDepartIds(List<EamBaseRepairDepart> repairDepartList, String repairDepartId, List<String> allMaintainDepartIds) {
//        if (!allMaintainDepartIds.contains(repairDepartId)) {
//            allMaintainDepartIds.add(repairDepartId);
//        }
//        for (EamBaseRepairDepart eamBaseRepairDepart : repairDepartList) {
//            if (StringUtils.isEmpty(eamBaseRepairDepart.getParentId())) {
//                continue;
//            }
//            if (repairDepartId.equals(eamBaseRepairDepart.getId())) {
//                if (!allMaintainDepartIds.contains(eamBaseRepairDepart.getParentId())) {
//                    allMaintainDepartIds.add(eamBaseRepairDepart.getParentId());
//                    getAllMaintainDepartIds(repairDepartList, eamBaseRepairDepart.getParentId(), allMaintainDepartIds);
//                }
//            }
//        }
//    }
//
//    /**
//     * 通过维修部门班组集合为维修部门班组设置用户id,用于前台展示
//     */
//    private void setUserIdsByProList(List<EamBaseRepairDepart> repairDepartList) {
//        //查询负责部门不为空的情况
//        LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<>();
//        query.isNotNull(SysUser::getDepartIds);
//        List<SysUser> users = sysUserMapper.selectList(query);
//        Map<String, Object> map = new HashMap(5);
//        //先循环一遍找到不同的负责维修部门班组id
//        for (SysUser user : users) {
//            String repairDepartIds = user.getRepairDepartIds();
//            if (StringUtils.isNotBlank(repairDepartIds)) {
//                String[] repairDepartIdArray = repairDepartIds.split(",");
//                for (String repairDepartId : repairDepartIdArray) {
//                    if (map.containsKey(repairDepartId)) {
//                        String userIds = map.get(repairDepartId) + "," + user.getId();
//                        map.put(repairDepartId, userIds);
//                    } else {
//                        map.put(repairDepartId, user.getId());
//                    }
//                }
//            }
//        }
//        //循环维修部门班组集合找到维修部门班组id对应的负责用户
//        for (EamBaseRepairDepart eamBaseRepairDepart : repairDepartList) {
//            if (map.containsKey(eamBaseRepairDepart.getId())) {
//                eamBaseRepairDepart.setDirectorUserIds(map.get(eamBaseRepairDepart.getId()).toString());
//            }
//        }
//    }
 
}