lyh
2025-04-17 990ad5f6db0bb3ad5a3795e77c5d6f3971c12ff3
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
package org.jeecg.modules.dnc.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.dnc.entity.DeviceManagement;
import org.jeecg.modules.dnc.entity.DeviceType;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.DeviceManagementMapper;
import org.jeecg.modules.dnc.response.CommonCode;
import org.jeecg.modules.dnc.service.IDeviceManagementService;
import org.jeecg.modules.dnc.service.IDeviceTypeService;
import org.jeecg.modules.dnc.utils.ValidateUtil;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
import org.jeecg.modules.mdc.service.IMdcProductionEquipmentService;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.entity.MdcProductionEquipment;
import org.jeecg.modules.system.entity.MdcUserProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecg.modules.system.service.IMdcUserProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class DeviceManagementServiceImpl  extends ServiceImpl<DeviceManagementMapper, DeviceManagement> implements IDeviceManagementService {
 
    @Autowired
    private IMdcProductionService productionService;
    @Autowired
    private IMdcUserProductionService userProductionService;
    @Autowired
    private IMdcProductionEquipmentService productionEquipmentService;
    @Autowired
    private IMdcEquipmentService equipmentService;
    @Autowired
    private IDeviceTypeService deviceTypeService;
    /**
     * 新增设备类信息
     * @param deviceManagement
     * @return
     */
    @Override
    public Result<?> add(DeviceManagement deviceManagement){
        if(deviceManagement == null)
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        if(!ValidateUtil.validateString(deviceManagement.getEquipmentIds()))
            Result.error("无效的设备类");
        List<DeviceManagement> deviceManagementList =this.getDeviceManagementList(deviceManagement.getProductionId(),deviceManagement.getDeviceManagementName(),deviceManagement.getDeviceManagementCode());
        if (deviceManagementList != null && !deviceManagementList.isEmpty()) {
            return Result.error("已存在相同的设备类");
        }
        boolean save = this.save(deviceManagement);
        if(save){
            return Result.OK("添加设备类成功");
        }
        return Result.error("新增设备类失败");
    }
    /**
     * 编辑设备类信息
     * @param deviceManagement
     * @return
     */
    @Override
    public Result<?> edit(DeviceManagement deviceManagement){
        // 检查传入的设备类对象是否为空
        if (deviceManagement == null) {
            return Result.OK("设备类对象不能为空");
        }
        // 检查设备类名称是否有效
        if (!ValidateUtil.validateString(deviceManagement.getDeviceManagementName())) {
            return Result.OK("设备类名称无效");
        }
        // 根据设备类 ID 获取设备类信息
        DeviceManagement existingCutter = super.getById(deviceManagement.getId());
        if (existingCutter == null) {
            return Result.OK("设备类不存在");
        }
        // 过滤掉当前要编辑的设备类,检查是否有其他设备类存在相同编号
        List<DeviceManagement> otherCuttersWithSameNo =this.getDeviceManagementList(deviceManagement.getProductionId(),deviceManagement.getDeviceManagementName(),deviceManagement.getDeviceManagementCode()).stream()
                .filter(cut -> !cut.getId().equals(deviceManagement.getId()))
                .collect(Collectors.toList());
        if (!otherCuttersWithSameNo.isEmpty()) {
            // 如果存在除当前设备类外的其他设备类编号重复
            return Result.error("已存在相同的设备类编号");
        }
        // 尝试更新设备类信息
        boolean updated = this.updateById(deviceManagement);
        if (updated) {
            return Result.OK("设备类信息编辑成功");
        } else {
            return Result.error("设备类信息编辑失败");
        }
    }
    /**
     * 根据id删除设备类信息
     * @param id
     * @return
     */
    @Override
    public Result<?> delete(String id){
        if(!ValidateUtil.validateString(id))
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        DeviceManagement en = super.getById(id);
        if(en == null)
            return Result.error("无效的设备类");
        List<DeviceType> deviceTypes=deviceTypeService.list(new QueryWrapper<DeviceType>().eq("device_management_id",id));
        if(deviceTypes != null && !deviceTypes.isEmpty()){
            return Result.error("该设备类已被使用,不能删除");
        }
        boolean b=super.removeById(id);
        if(!b) {
            return Result.error("删除设备类失败");
        }
        return Result.OK("删除设备类成功");
    }
 
    /**
     * 批量删除设备类
     * @param ids
     * @return
     */
    @Override
    public Result<?> deleteBatch(List<String> ids){
        if(ids == null || ids.isEmpty())
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        return super.removeByIds(ids) ? Result.OK("批量删除设备类成功") : Result.error("批量删除设备类失败");
    }
 
    /**
     * 设备类信息分页查询
     * @param deviceManagement
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Result<?> query(DeviceManagement deviceManagement, Integer pageNo, Integer pageSize){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> productIdList = Arrays.asList(user.getProductionIds().split(","));
        QueryWrapper<DeviceManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotEmpty(deviceManagement.getProductionId()),"production_id",deviceManagement.getProductionId());
        queryWrapper.like(StrUtil.isNotEmpty(deviceManagement.getDeviceManagementName()),"device_management_name",deviceManagement.getDeviceManagementName());
        queryWrapper.like(StrUtil.isNotEmpty(deviceManagement.getDeviceManagementCode()),"device_management_code",deviceManagement.getDeviceManagementCode());
        queryWrapper.in("production_id",productIdList);
        queryWrapper.orderByDesc("create_time");
        Page<DeviceManagement> page = new Page<>(pageNo, pageSize);
        IPage<DeviceManagement> pageList = super.page(page, queryWrapper);
        return Result.OK(pageList);
    }
 
    /**
     * 通过车间id和设备类名称查询设备类信息
     * @param productionId
     * @parame deviceManagementName
     * @return
     */
    public List<DeviceManagement> getDeviceManagementList(String productionId,String deviceManagementName,String deviceManagementCode){
        return super.list(new QueryWrapper<DeviceManagement>()
                .eq(StrUtil.isNotEmpty(productionId),"production_id", productionId)
                .eq(StrUtil.isNotEmpty(deviceManagementName),"device_management_name", deviceManagementName)
                .eq(StrUtil.isNotEmpty(deviceManagementCode),"device_management_code", deviceManagementCode));
    }
 
    /**
     * 获取用户权限拥有的车间信息
     * @param userId
     * @return
     */
    @Override
    public List<MdcProduction> getProductionIdsByUserId(String userId){
        //权限信息,改为结构树权限
        List<MdcUserProduction> productionList = userProductionService.list(new QueryWrapper<MdcUserProduction>().eq("user_id", userId));
        List<String> productionIds = productionList.stream().map(MdcUserProduction::getProId).collect(Collectors.toList());
        return productionService.list(new QueryWrapper<MdcProduction>().in("id", productionIds)
                .eq("org_type","2"));
    }
 
    /**
     * 通过车间id查询设备列表信息
     * @param productionId
     * @return
     */
    @Override
    public IPage<MdcEquipment> getEquipmentListByProductionId(String productionId,String equipmentId,String equipmentName,Integer pageNo,Integer pageSize){
        //查询子节点
        List<String> productionIds = productionService.recursionChildren(productionId);
        if (productionIds == null) {
            return null;
        }
        List<String> equipmentIds = productionEquipmentService.list(new QueryWrapper<MdcProductionEquipment>()
               .in("production_id", productionIds))
               .stream().map(MdcProductionEquipment::getEquipmentId).collect(Collectors.toList());
        if (!equipmentIds.isEmpty()) {
            QueryWrapper<MdcEquipment> queryWrapper = new QueryWrapper<MdcEquipment>()
                    .in("id", equipmentIds)
                    .like(StrUtil.isNotEmpty(equipmentName),"equipment_name", equipmentName)
                    .eq(StrUtil.isNotEmpty(equipmentId),"equipment_id", equipmentId);
            queryWrapper.orderByDesc("create_time");
            Page<MdcEquipment> page = new Page<>(pageNo, pageSize);
            return equipmentService.page(page, queryWrapper);
        }
        return null;
    }
 
    /**
     * 通过车间id查询设备类管理列表信息
     * @param productionId
     * @return
     */
    @Override
    public List<DeviceManagement> getDeviceManagementListByProductionId(String productionId){
        QueryWrapper<DeviceManagement> queryWrapper = new QueryWrapper<DeviceManagement>()
                .eq(StrUtil.isNotEmpty(productionId),"production_id", productionId);
        queryWrapper.orderByDesc("create_time");
        return super.list(queryWrapper);
    }
 
    /**
     * 通过id查询设备列表信息
     * @param id
     * @return
     */
    @Override
    public List<MdcEquipment> getEquipmentListById(String id){
        DeviceManagement deviceManagement = super.getById(id);
        if (deviceManagement == null) {
            return null;
        }else {
            List<String> equipmentIds = Arrays.asList(deviceManagement.getEquipmentIds().split(","));
            return equipmentService.list(new QueryWrapper<MdcEquipment>()
                   .in("equipment_id", equipmentIds));
        }
    }
}