lyh
2025-06-30 0843d9fa608a6d319d9d1c37860a0f16ce263a19
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
package org.jeecg.modules.eam.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
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.entity.WorkShopDepart;
import org.jeecg.modules.eam.entity.WorkShopDepartUser;
import org.jeecg.modules.eam.mapper.WorkShopDepartMapper;
import org.jeecg.modules.eam.mapper.WorkShopDepartUserMapper;
import org.jeecg.modules.eam.model.EamWorkShopDepartTreeModel;
import org.jeecg.modules.eam.model.WorkShopIdModel;
import org.jeecg.modules.eam.service.IWorkShopDepartService;
import org.jeecg.modules.eam.util.FindsWorkShopDepartsChildrenUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
/**
 * @Description: 设备车间管理
 * @Author: jeecg-boot
 * @Date:   2025-06-30
 * @Version: V1.0
 */
@Service
public class WorkShopDepartServiceImpl extends ServiceImpl<WorkShopDepartMapper, WorkShopDepart> implements IWorkShopDepartService {
 
    @Autowired
    private SysUserMapper sysUserMapper;
 
    @Autowired
    private WorkShopDepartUserMapper workShopDepartUserMapper;
 
    /**
     * queryTreeList 对应 queryTreeList 查询所有的设备车间管理数据,以树结构形式响应给前端
     */
    @Override
    @Cacheable(value = "eam:cache:workShopDepart:alldata")
    public List<EamWorkShopDepartTreeModel> queryTreeList() {
        LambdaQueryWrapper<WorkShopDepart> query = new LambdaQueryWrapper<WorkShopDepart>();
        query.eq(WorkShopDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(WorkShopDepart::getWorkShopOrder);
        List<WorkShopDepart> list = this.list(query);
        //设置用户id,让前台显示
        this.setUserIdsByProList(list);
        //调用wrapTreeDataToTreeList方法生成树状数据
        return FindsWorkShopDepartsChildrenUtil.wrapTreeDataToTreeList(list);
    }
 
    /**
     * queryTreeList 根据设备车间管理id查询,前端回显调用
     */
    @Override
    public List<EamWorkShopDepartTreeModel> queryTreeList(String ids) {
        List<EamWorkShopDepartTreeModel> listResult = new ArrayList<>();
        LambdaQueryWrapper<WorkShopDepart> query = new LambdaQueryWrapper<WorkShopDepart>();
        query.eq(WorkShopDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        if (oConvertUtils.isNotEmpty(ids)) {
            query.in(true, WorkShopDepart::getId, ids.split(","));
        }
        query.orderByAsc(WorkShopDepart::getWorkShopOrder);
        List<WorkShopDepart> list = this.list(query);
        for (WorkShopDepart production : list) {
            if (production.getDescription().isEmpty()){
                production.setDescription("");
            }
            listResult.add(new EamWorkShopDepartTreeModel(production));
        }
        return listResult;
    }
 
    /**
     * 根据关键字搜索相关的部门数据
     */
    @Override
    public List<EamWorkShopDepartTreeModel> searchByKeyWord(String keyWord) {
        LambdaQueryWrapper<WorkShopDepart> query = new LambdaQueryWrapper<>();
        List<EamWorkShopDepartTreeModel> newList = new ArrayList<>();
        query.like(WorkShopDepart::getWorkShopName, keyWord);
        EamWorkShopDepartTreeModel model = new EamWorkShopDepartTreeModel();
        List<WorkShopDepart> productionList = this.list(query);
        if (!productionList.isEmpty()) {
            for (WorkShopDepart WorkShopDepart : productionList) {
                model = new EamWorkShopDepartTreeModel(WorkShopDepart);
                model.setChildren(null);
                newList.add(model);
            }
            return newList;
        }
        return Collections.emptyList();
    }
 
    /**
     * saveProductionData 对应 add 保存用户在页面添加的新的设备车间管理对象数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWorkShopDepartData(WorkShopDepart WorkShopDepart) {
        if (WorkShopDepart != null) {
            if (WorkShopDepart.getParentId() == null) {
                WorkShopDepart.setParentId("");
            }
            WorkShopDepart.setId(IdWorker.getIdStr(WorkShopDepart));
            // 先判断该对象有无父级ID,有则意味着不是最高级,否则意味着是最高级
            // 获取父级ID
            String parentId = WorkShopDepart.getParentId();
            JSONObject formData = new JSONObject();
            formData.put("parentId",parentId);
            String[] codeArray = (String[]) FillRuleUtil.executeRule(FillRuleConstant.PRODUCTION,formData);
            WorkShopDepart.setOrgCode(codeArray[0]);
            String orgType = codeArray[1];
            WorkShopDepart.setOrgType(String.valueOf(orgType));
            WorkShopDepart.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            this.save(WorkShopDepart);
        }
    }
 
    @Override
    @Cacheable(value = "eam:cache:workshop:allids")
    public List<WorkShopIdModel> queryProductionIdTreeList() {
        LambdaQueryWrapper<WorkShopDepart> query = new LambdaQueryWrapper<>();
        query.eq(WorkShopDepart::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        query.orderByAsc(WorkShopDepart::getWorkShopOrder);
        List<WorkShopDepart> list = this.list(query);
        //调用wrapTreeDataToTreeList方法生成树状数据
        return FindsWorkShopDepartsChildrenUtil.wrapTreeDataToProductionIdTreeList(list);
    }
 
 
    /**
     * updateProductionDataById 对应 edit 根据设备车间管理主键来更新对应的设备车间管理数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWorkShopDepartDataById(WorkShopDepart WorkShopDepart) {
        if (WorkShopDepart != null) {
            this.updateById(WorkShopDepart);
            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删除用户与设备车间管理关系
        workShopDepartUserMapper.delete(new LambdaQueryWrapper<WorkShopDepartUser>().in(WorkShopDepartUser::getWorkShopDepartId, 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删除用户与设备车间管理关系
        workShopDepartUserMapper.delete(new LambdaQueryWrapper<WorkShopDepartUser>().in(WorkShopDepartUser::getWorkShopDepartId, idList));
    }
 
    /**
     * 通过设备车间管理集合为设备车间管理设置用户id,用于前台展示
     */
    private void setUserIdsByProList(List<WorkShopDepart> productionList) {
        //查询负责部门不为空的情况
        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 productionIds = user.getProductionIds();
            if (StringUtils.isNotBlank(productionIds)) {
                String[] productionIdArray = productionIds.split(",");
                for (String productionId : productionIdArray) {
                    if (map.containsKey(productionId)) {
                        String userIds = map.get(productionId) + "," + user.getId();
                        map.put(productionId, userIds);
                    } else {
                        map.put(productionId, user.getId());
                    }
                }
            }
        }
        //循环设备车间管理集合找到设备车间管理id对应的负责用户
        for (WorkShopDepart WorkShopDepart : productionList) {
            if (map.containsKey(WorkShopDepart.getId())) {
                WorkShopDepart.setDirectorUserIds(map.get(WorkShopDepart.getId()).toString());
            }
        }
    }
 
    /**
     * delete 方法调用 递归查找子集id
     */
    private void checkChildrenExists(String id, List<String> idList) {
        LambdaQueryWrapper<WorkShopDepart> query = new LambdaQueryWrapper<>();
        query.eq(WorkShopDepart::getParentId, id);
        List<WorkShopDepart> workShopDepartList = this.list(query);
        if (workShopDepartList != null && !workShopDepartList.isEmpty()) {
            for (WorkShopDepart workShopDepart : workShopDepartList) {
                idList.add(workShopDepart.getId());
                this.checkChildrenExists(workShopDepart.getId(), idList);
            }
        }
    }
}