lyh
2025-02-20 3302ff7054d02f6b21452f2f2cd3658c8cad6b53
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package org.jeecg.modules.dnc.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.ProcessStreamMapper;
import org.jeecg.modules.dnc.response.*;
 
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.dnc.utils.ValidateUtil;
 
 
import org.jeecg.modules.dnc.request.ProcessStreamRequest;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
@Service
public class ProcessStreamServiceImpl extends ServiceImpl<ProcessStreamMapper, ProcessStream> implements IProcessStreamService {
    @Autowired
    @Lazy
    private IComponentInfoService componentInfoService;
    @Autowired
    private IProcessInfoService processInfoService;
    @Autowired
    private IPartsInfoService partsInfoService;
    @Autowired
    private IDocInfoService docInfoService;
    @Autowired
    @Lazy
    private IDeviceInfoService deviceInfoService;
    @Autowired
    private INcLogInfoService iNcLogInfoService;
    @Autowired
    private IPermissionStreamService permissionStreamService;
    @Autowired
    private IProcessionDepartmentService processionDepartmentService;
    @Autowired
    private IWorkStepService workStepService;
    @Autowired
    private IDocRelativeService iDocRelativeService;
    @Autowired
    private IProcessStreamPermissionService processStreamPermissionService;
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addProcessStream(ProcessStream stream) {
        if(stream == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(!ValidateUtil.validateString(stream.getProductId()))
            ExceptionCast.cast(ProcessInfoCode.PROCESS_PRODUCT_NONE);
        if(!ValidateUtil.validateString(stream.getComponentId()))
            ExceptionCast.cast(ProcessInfoCode.PROCESS_COMPONENT_NONE);
        if(!ValidateUtil.validateString(stream.getProcessName()))
            ExceptionCast.cast(ProcessInfoCode.PROCESS_NAME_NONE);
        if(!ValidateUtil.validateString(stream.getProcessCode()))
            ExceptionCast.cast(ProcessInfoCode.PROCESS_CODE_NONE);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        if(!ValidateUtil.validateString(userId))
            ExceptionCast.cast(UcenterCode.UCENTER_ACCOUNT_NOT_EXIST);
        if(ValidateUtil.validateString(stream.getPartsId())) {
            PartsInfo partsInfo = partsInfoService.getById(stream.getPartsId());
            if(partsInfo == null)
                ExceptionCast.cast(ProcessInfoCode.PROCESS_PARTS_NONE);
            if(!stream.getProductId().equals(partsInfo.getProductId()))
                ExceptionCast.cast(ProcessInfoCode.PROCESS_PRODUCT_NONE);
            if(!stream.getComponentId().equals(partsInfo.getComponentId()))
                ExceptionCast.cast(ProcessInfoCode.PROCESS_COMPONENT_NONE);
            ProcessStream en = findByProcessNoAndPartsId(stream.getProcessCode(), partsInfo.getPartsId());
            if(en != null) {
                ExceptionCast.cast(ProcessInfoCode.PROCESS_IS_EXIST);
            }
        }else {
            ComponentInfo componentInfo = componentInfoService.getById(stream.getComponentId());
            if(componentInfo == null)
                ExceptionCast.cast(ProcessInfoCode.PROCESS_COMPONENT_NONE);
            if(!stream.getProductId().equals(componentInfo.getProductId()))
                ExceptionCast.cast(ProcessInfoCode.PROCESS_PRODUCT_NONE);
            stream.setPartsId(null);
            ProcessStream en = findByProcessNoAndComponentId(stream.getProcessCode(), componentInfo.getComponentId());
            if(en != null) {
                ExceptionCast.cast(ProcessInfoCode.PROCESS_IS_EXIST);
            }
        }
        boolean b = super.save(stream);
        if(!b)
            ExceptionCast.cast(CommonCode.FAIL);
        //添加用户权限
        ProcessionPermission permission = new ProcessionPermission();
        permission.setProcessId(stream.getProcessId());
        permission.setUserId(userId);
        processStreamPermissionService.save(permission);
        //添加权限验证
        PermissionStream permissionStream = new PermissionStream();
        permissionStream.setUserId(userId);
        permissionStream.setProductId(stream.getProductId());
        permissionStream.setComponentId(stream.getComponentId());
        if (StrUtil.isNotEmpty(stream.getPartsId())){
            permissionStream.setPartsId(stream.getPartsId());
        }
        permissionStream.setProcessId(stream.getProcessId());
        return permissionStreamService.save(permissionStream);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean editProcessStream(String id, ProcessStream stream) {
        if(!ValidateUtil.validateString(id) || stream == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(!ValidateUtil.validateString(stream.getProcessName()))
            ExceptionCast.cast(ProcessInfoCode.PROCESS_NAME_NONE);
        ProcessStream en = super.getById(id);
        if(en == null)
            ExceptionCast.cast(ProcessInfoCode.PROCESS_NOT_EXIST);
        stream.setProcessId(id);
        stream.setProductId(null);
        stream.setComponentId(null);
        stream.setPartsId(null);
        stream.setProcessName(stream.getProcessName().toUpperCase());
        stream.setProcessCode(null);
        //添加日志
        NcLogInfo ncLogInfo = new NcLogInfo();
        //模块
        ncLogInfo.setModuleInfo("产品结构树");
        //类型
        ncLogInfo.setOperateType(3);
        //日志内容
        ncLogInfo.setLogContent("工序号:"+en.getProcessCode());
        ncLogInfo.setRemark(JSONObject.toJSONString(en));
        iNcLogInfoService.saveLogNcInfos(ncLogInfo);
        boolean b = super.updateById(stream);
        if(!b)
            ExceptionCast.cast(CommonCode.FAIL);
        ProcessInfo processInfo = new ProcessInfo();
        processInfo.setProcessName(stream.getProcessName());
        processInfo.setDescription(stream.getDescription());
        return processInfoService.addOrEdit(processInfo);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteProcessStream(String id) {
        if(!ValidateUtil.validateString(id))
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        ProcessStream en = super.getById(id);
        if(en == null)
            ExceptionCast.cast(ProcessInfoCode.PROCESS_NOT_EXIST);
        //验证工序下是否有工步
        List<WorkStep> workStepList=workStepService.list(new QueryWrapper<WorkStep>().eq("process_id",id));
        if(workStepList != null && !workStepList.isEmpty()) {
            ExceptionCast.cast(ProcessInfoCode.WORKSTEP_HAS_DATA);
        }
        //验证是否存在文档
        List<DocRelative> docRelativeList=iDocRelativeService.list(new QueryWrapper<DocRelative>().eq("attribution_type","5").eq("attribution_id",id));
        if (!docRelativeList.isEmpty()){
            ExceptionCast.cast(ProcessInfoCode.PROCESS_HAS_DOC);
        }
        return super.removeById(id);
    }
 
    @Override
    public List<ProcessStream> findByNodeParams(ProcessStreamRequest request) {
        if(request == null || !ValidateUtil.validateString(request.getProductId()) || !ValidateUtil.validateString(request.getComponentId()))
            return Collections.emptyList();
        LambdaQueryChainWrapper<ProcessStream> lambdaQuery = super.lambdaQuery().eq(ProcessStream::getProductId, request.getProductId()).eq(ProcessStream::getComponentId, request.getComponentId());
        if(ValidateUtil.validateString(request.getPartsId())) {
            lambdaQuery.eq(ProcessStream::getPartsId, request.getPartsId());
        }else {
            lambdaQuery.isNull(ProcessStream::getPartsId);
        }
        return lambdaQuery.list();
    }
 
    @Override
    public List<ProcessStream> findByProductId(String productId) {
        return super.lambdaQuery().eq(ProcessStream::getProductId, productId).list();
    }
 
    @Override
    public List<ProcessStream> findByComponentId(String componentId) {
        return super.lambdaQuery().eq(ProcessStream::getComponentId, componentId).list();
    }
 
    @Override
    public List<ProcessStream> findByPartsId(String partsId) {
        return super.lambdaQuery().eq(ProcessStream::getPartsId, partsId).list();
    }
 
    @Override
    public ProcessStream findByProcessNoAndPartsId(String processNo, String partsId) {
        List<ProcessStream> list = super.lambdaQuery().eq(ProcessStream::getPartsId, partsId).eq(ProcessStream::getProcessCode, processNo).list();
        if(list == null || list.isEmpty())
            return null;
        return list.get(0);
    }
 
    @Override
    public ProcessStream findByProcessNoAndComponentId(String processNo, String componentId) {
        List<ProcessStream> list = super.lambdaQuery().eq(ProcessStream::getComponentId, componentId).eq(ProcessStream::getProcessCode, processNo).list();
        if(list == null || list.isEmpty())
            return null;
        return list.get(0);
    }
 
    @Override
    public List<ProcessStream> validateDeviceProcessInfo(String pnCode, String deviceNo) {
        if(!ValidateUtil.validateString(pnCode) || !ValidateUtil.validateString(deviceNo))
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        DeviceInfo deviceInfo = deviceInfoService.getByDeviceNo(deviceNo);
        if(deviceInfo == null)
            ExceptionCast.cast(DeviceCode.DEVICE_NOT_EXIST);
        PermissionStream stream = componentInfoService.validateComponentOrPartsPnCode(pnCode);
        if(stream == null)
            ExceptionCast.cast(ComponentInfoCode.COMPONENT_PN_NOT_EXIST);
        LambdaQueryWrapper<ProcessStream> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ProcessStream::getProductId, stream.getProductId());
        lambdaQueryWrapper.eq(ProcessStream::getComponentId, stream.getComponentId());
        if(ValidateUtil.validateString(stream.getPartsId())) {
            lambdaQueryWrapper.eq(ProcessStream::getPartsId, stream.getPartsId());
        }
        lambdaQueryWrapper.like(ProcessStream::getProcessingEquipmentCode, deviceNo);
        List<ProcessStream> list = super.list(lambdaQueryWrapper);
        if(list == null || list.isEmpty())
            ExceptionCast.cast(ProcessInfoCode.PROCESS_NOT_EXIST);
        return list;
    }
 
    @Override
    public List<ProcessStream> getByuserPerms(String userId){
        if(!ValidateUtil.validateString(userId))
            return Collections.emptyList();
        return super.getBaseMapper().getByUserPerms(userId);
    }
 
    @Override
    public List<ProcessStream> getByuserPerms(String userId,String queryParam){
        if(!ValidateUtil.validateString(userId))
            return Collections.emptyList();
        if(!ValidateUtil.validateString(queryParam))
            return Collections.emptyList();
        LambdaQueryWrapper<ProcessStream> queryWrapper = Wrappers.lambdaQuery();
        if(ValidateUtil.validateString(queryParam)) {
            queryWrapper.and(wrapper->wrapper.like(ProcessStream::getProcessName, queryParam)
                    .or()
                    .like(ProcessStream::getProcessName, queryParam));
        }
        queryWrapper.orderByAsc(ProcessStream::getCreateTime);
        return super.list(queryWrapper);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignAddDepart(ProcessStream processStream, Collection<MdcProduction> departmentList) {
        if(processStream == null || departmentList == null || departmentList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<ProcessionDepartment> processionDepartmentList = new ArrayList<>();
        List<PermissionStream> permissionStreamList = new ArrayList<>();
        departmentList.forEach(item -> {
            ProcessionDepartment en = processionDepartmentService.getByProcessIdAndDepartId(processStream.getProcessId(), item.getId());
            if(en == null) {
                en = new ProcessionDepartment();
                en.setDepartId(item.getId());
                en.setProcessId(processStream.getProcessId());
                processionDepartmentList.add(en);
            }
            PermissionStream stream = permissionStreamService.getByProcessIdAndDepartId(processStream.getProductId(), processStream.getComponentId(), processStream.getPartsId(),processStream.getProcessId(),item.getId());
            if(stream == null) {
                stream = new PermissionStream();
                stream.setDepartId(item.getId());
                stream.setProductId(processStream.getProductId());
                stream.setComponentId(processStream.getComponentId());
                stream.setPartsId(processStream.getPartsId());
                stream.setProcessId(processStream.getProcessId());
                permissionStreamList.add(stream);
            }
        });
        if(!processionDepartmentList.isEmpty()) {
            boolean b = processionDepartmentService.saveBatch(processionDepartmentList);
            if(!b) {
                ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR);
            }
        }
        if(!permissionStreamList.isEmpty()) {
            boolean b = permissionStreamService.saveBatch(permissionStreamList);
            if(!b) {
                ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR);
            }
        }
        return true;
    }
 
    @Override
    public List<ProcessStream> getByComponentIdList(String productId, List<String> componentIds, List<String> partsIds) {
        return super.baseMapper.findByPartsAndComponents(productId,componentIds,partsIds);
    }
 
 
    /**
     * 分配部门权限
     * @param processStream
     * @param departmentList
     * @return
     */
    @Override
    public boolean assignRemoveDepart(ProcessStream processStream, Collection<MdcProduction> departmentList){
        if(processStream == null || departmentList == null || departmentList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<ProcessionDepartment> processionDepartmentList = new ArrayList<>();
        List<PermissionStream> permissionStreamList = new ArrayList<>();
        departmentList.forEach(item -> {
            ProcessionDepartment en = processionDepartmentService.getByProcessIdAndDepartId(processStream.getProcessId(), item.getId());
            if(en != null) {
                processionDepartmentList.add(en);
            }
            PermissionStream stream = permissionStreamService.getByProcessIdAndDepartId(processStream.getProductId(), processStream.getComponentId(), processStream.getPartsId()
                    ,processStream.getProcessId(), item.getId());
            if(stream != null) {
                permissionStreamList.add(stream);
            }
        });
        if(!processionDepartmentList.isEmpty()) {
            boolean b = processionDepartmentService.removeByCollection(processionDepartmentList);
            if(!b) {
                ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR);
            }
        }
        if(!permissionStreamList.isEmpty()) {
            boolean b = permissionStreamService.removeByCollection(permissionStreamList);
            if(!b) {
                ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR);
            }
        }
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignAddUser(ProcessStream processStream, Collection<SysUser> userList) {
        if(processStream == null || userList == null || userList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<ProcessionPermission> permissionList = new ArrayList<>();
        List<PermissionStream> permissionStreamList = new ArrayList<>();
        userList.forEach(item -> {
            ProcessionPermission en = processStreamPermissionService.getByProcessIdAndUserId(processStream.getProcessId(), item.getId());
            if(en == null) {
                en = new ProcessionPermission();
                en.setUserId(item.getId());
                en.setProcessId(processStream.getProcessId());
                permissionList.add(en);
            }
            PermissionStream stream = permissionStreamService.getByProcessIdAndUserId(processStream.getProductId(), processStream.getComponentId(), processStream.getPartsId(),processStream.getProcessId(), item.getId());
            if(stream == null) {
                stream = new PermissionStream();
                stream.setUserId(item.getId());
                stream.setProductId(processStream.getProductId());
                stream.setComponentId(processStream.getComponentId());
                stream.setPartsId(processStream.getPartsId());
                stream.setProcessId(processStream.getProcessId());
                permissionStreamList.add(stream);
            }
        });
        if(!permissionList.isEmpty()) {
            boolean b = processStreamPermissionService.saveBatch(permissionList);
            if(!b) {
                ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR);
            }
        }
        if(!permissionStreamList.isEmpty()) {
            boolean b = permissionStreamService.saveBatch(permissionStreamList);
            if(!b) {
                ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR);
            }
        }
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean assignRemoveUser(ProcessStream processStream, Collection<SysUser> userList) {
        if(processStream == null || userList == null || userList.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        List<ProcessionPermission> permissionList = new ArrayList<>();
        List<PermissionStream> permissionStreamList = new ArrayList<>();
        userList.forEach(item -> {
            ProcessionPermission en = processStreamPermissionService.getByProcessIdAndUserId(processStream.getProcessId(), item.getId());
            if(en != null) {
                permissionList.add(en);
            }
            PermissionStream stream = permissionStreamService.getByProcessIdAndUserId(processStream.getProductId(), processStream.getComponentId(), processStream.getPartsId(),processStream.getProcessId(), item.getId());
            if(stream != null) {
                permissionStreamList.add(stream);
            }
        });
        //清空用户权限校验
        if(!permissionList.isEmpty()) {
            boolean b = processStreamPermissionService.removeByCollection(permissionList);
            if(!b) {
                ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR);
            }
        }
        if(!permissionStreamList.isEmpty()) {
            boolean b = permissionStreamService.removeByCollection(permissionStreamList);
            if(!b) {
                ExceptionCast.cast(ProductInfoCode.PRODUCT_USER_PERM_ERROR);
            }
        }
        return true;
    }
 
}