zhangherong
2025-03-31 eafb61d4b75fa30c7fd8b3d4917fad4ed1bfa088
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
package org.jeecg.modules.eam.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.DataBaseConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.eam.aspect.annotation.EquipmentHistoryLog;
import org.jeecg.modules.eam.constant.AssetStatusEnum;
import org.jeecg.modules.eam.constant.EquipmentMaintenanceStatus;
import org.jeecg.modules.eam.constant.EquipmentOperationTagEnum;
import org.jeecg.modules.eam.constant.EquipmentRepairStatus;
import org.jeecg.modules.eam.entity.EamEquipment;
import org.jeecg.modules.eam.entity.EamEquipmentExtend;
import org.jeecg.modules.eam.mapper.EamEquipmentMapper;
import org.jeecg.modules.eam.request.EamEquipmentQuery;
import org.jeecg.modules.eam.service.IEamEquipmentExtendService;
import org.jeecg.modules.eam.service.IEamEquipmentService;
import org.jeecg.modules.eam.tree.FindsEquipmentProductionUtil;
import org.jeecg.modules.eam.vo.EamEquipmentTree;
import org.jeecg.modules.eam.vo.EquipmentSearchResult;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Description: 设备台账
 * @Author: jeecg-boot
 * @Date: 2025-03-19
 * @Version: V1.0
 */
@Service
public class EamEquipmentServiceImpl extends ServiceImpl<EamEquipmentMapper, EamEquipment> implements IEamEquipmentService {
 
    @Resource
    private EamEquipmentMapper eamEquipmentMapper;
    @Autowired
    private IEamEquipmentExtendService equipmentExtendService;
    @Autowired
    private IMdcProductionService mdcProductionService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @EquipmentHistoryLog(operationTag = EquipmentOperationTagEnum.ACCEPTANCE, businessTable = "eam_equipment")
    public EamEquipment saveEquipment(EamEquipment eamEquipment) {
        if (eamEquipment == null) {
            return null;
        }
        //资产状态默认 正常
        eamEquipment.setAssetStatus(AssetStatusEnum.NORMAL.name());
        eamEquipment.setDelFlag(CommonConstant.DEL_FLAG_0);
        eamEquipmentMapper.insert(eamEquipment);
 
        //扩展表数据同步添加
        EamEquipmentExtend eamEquipmentExtend = new EamEquipmentExtend();
        eamEquipmentExtend.setId(eamEquipment.getId());
        eamEquipmentExtend.setMaintenanceStatus(EquipmentMaintenanceStatus.NORMAL.name());
        eamEquipmentExtend.setRepairStatus(EquipmentRepairStatus.NORMAL.name());
 
        equipmentExtendService.save(eamEquipmentExtend);
 
        //插入设备履历   @EquipmentHistoryLog
        return eamEquipment;
    }
 
    @Override
    public List<EamEquipmentTree> loadTreeListByProductionIds(String ids) {
        List<String> productionIds = Arrays.asList(ids.split(","));
        //获取所有产线数据
        List<MdcProduction> productionList = mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0.toString()).orderByAsc(MdcProduction::getProductionOrder));
        List<String> allProductionIds = new ArrayList<>();
        //找到所有产线id的上级id
        if (!productionIds.isEmpty()) {
            for (String productionId : productionIds) {
                this.getAllProductionIds(productionList, productionId, allProductionIds);
            }
        }
        //过滤产线数据
        List<MdcProduction> list = productionList.stream().filter((MdcProduction mdcProduction) -> allProductionIds.contains(mdcProduction.getId())).collect(Collectors.toList());
        //组装产线设备树
        List<EamEquipmentTree> treeList = FindsEquipmentProductionUtil.wrapEquipmentProductionTreeList(list);
        //填充设备数据
        fillEquipmentByProduction(treeList);
        return treeList;
    }
 
    @Override
    public IPage<EamEquipment> queryPageList(IPage<EamEquipment> page, EamEquipmentQuery eamEquipment) {
        QueryWrapper<EamEquipment> queryWrapper = new QueryWrapper<>();
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            return page;
        }
        if (StringUtils.isNotBlank(sysUser.getEquipmentIds())) {
            //选择了设备,根据设备id过滤设备
            List<String> equipArr = Arrays.asList(sysUser.getEquipmentIds().split(","));
            queryWrapper.in("e.equipment_code", equipArr);
        } else {
            //没有选择设备,根据车间过滤设备
            queryWrapper.exists("select 1 from mdc_user_production t where t.user_id={0} and t.pro_id=e.org_id", sysUser.getId());
        }
        //查询条件过滤
        if (eamEquipment != null) {
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentCode())) {
                queryWrapper.like("e.equipment_code", eamEquipment.getEquipmentCode());
            }
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentName())) {
                queryWrapper.like("e.equipment_name", eamEquipment.getEquipmentName());
            }
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentImportance())) {
                queryWrapper.eq("e.equipment_importance", eamEquipment.getEquipmentImportance());
            }
            if (StringUtils.isNotBlank(eamEquipment.getAssetStatus())) {
                queryWrapper.like("e.asset_status", eamEquipment.getAssetStatus());
            }
            if (StringUtils.isNotBlank(eamEquipment.getTechnologyStatus())) {
                queryWrapper.like("e.technology_status", eamEquipment.getTechnologyStatus());
            }
            if (StringUtils.isNotBlank(eamEquipment.getOperationSystem())) {
                queryWrapper.like("e.operation_system", eamEquipment.getOperationSystem());
            }
            if (StringUtils.isNotBlank(eamEquipment.getOrgId())) {
                queryWrapper.like("e.org_id", eamEquipment.getOrgId());
            }
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentCategory())) {
                queryWrapper.like("e.equipment_category", eamEquipment.getEquipmentCategory());
            }
            //排序
            if (StringUtils.isNotBlank(eamEquipment.getColumn()) && StringUtils.isNotBlank(eamEquipment.getOrder())) {
                //queryWrapper.like("column", eamEquipment.getColumn());
                String column = eamEquipment.getColumn();
                if (column.endsWith(CommonConstant.DICT_TEXT_SUFFIX)) {
                    column = column.substring(0, column.lastIndexOf(CommonConstant.DICT_TEXT_SUFFIX));
                }
                if (DataBaseConstant.SQL_ASC.equalsIgnoreCase(eamEquipment.getOrder())) {
                    queryWrapper.orderByAsc("e." + oConvertUtils.camelToUnderline(column));
                } else {
                    queryWrapper.orderByDesc("e." + oConvertUtils.camelToUnderline(column));
                }
            } else {
                queryWrapper.orderByDesc("e.create_time");
            }
        } else {
            queryWrapper.orderByDesc("e.create_time");
        }
 
        IPage<EamEquipment> ipage = eamEquipmentMapper.queryPageList(page, queryWrapper);
        return ipage;
    }
 
    @Override
    public List<EquipmentSearchResult> asyncLoadEquipment(String keyword, Integer pageSize) {
        IPage<EamEquipment> page = new Page<>(1, pageSize);
        QueryWrapper<EamEquipment> queryWrapper = new QueryWrapper<>();
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            return Collections.emptyList();
        }
        if (StringUtils.isNotBlank(sysUser.getEquipmentIds())) {
            //选择了设备,根据设备id过滤设备
            List<String> equipArr = Arrays.asList(sysUser.getEquipmentIds().split(","));
            queryWrapper.in("equipment_code", equipArr);
        } else {
            //没有选择设备,根据车间过滤设备
            queryWrapper.exists("select 1 from mdc_user_production t where t.user_id={0} and t.pro_id=org_id", sysUser.getId());
        }
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("equipment_code", keyword);
            queryWrapper.or().like("equipment_name", keyword);
        }
        IPage<EamEquipment> pageResult = eamEquipmentMapper.queryPageList(page, queryWrapper);
        if (pageResult != null && CollectionUtil.isNotEmpty(pageResult.getRecords())) {
            List<EquipmentSearchResult> resultList = new ArrayList<>();
            pageResult.getRecords().forEach((record) -> {
                resultList.add(new EquipmentSearchResult(record));
            });
            return resultList;
        }
        return Collections.emptyList();
    }
 
    /**
     * 获取所有的产线id(包含所有上级)
     */
    private void getAllProductionIds(List<MdcProduction> productionList, String productionId, List<String> allProductionIds) {
        if (!allProductionIds.contains(productionId)) {
            allProductionIds.add(productionId);
        }
        for (MdcProduction mdcProduction : productionList) {
            if (StringUtils.isEmpty(mdcProduction.getParentId())) {
                continue;
            }
            if (productionId.equals(mdcProduction.getId())) {
                if (!allProductionIds.contains(mdcProduction.getParentId())) {
                    allProductionIds.add(mdcProduction.getParentId());
                    getAllProductionIds(productionList, mdcProduction.getParentId(), allProductionIds);
                }
            }
        }
    }
 
    /**
     * 产线设备树填充设备数据
     */
    private void fillEquipmentByProduction(List<EamEquipmentTree> treeList) {
        for (EamEquipmentTree mdcEquipmentTree : treeList) {
            List<EamEquipment> equipmentList = eamEquipmentMapper.queryByProductionId(mdcEquipmentTree.getKey());
            if (CollectionUtil.isNotEmpty(equipmentList)) {
                for (EamEquipment mdcEquipment : equipmentList) {
                    EamEquipmentTree tree = new EamEquipmentTree().convert(mdcEquipment);
                    tree.setParentId(mdcEquipmentTree.getKey());
                    tree.setType(2);
                    mdcEquipmentTree.getChildren().add(tree);
                }
                mdcEquipmentTree.setLeaf(false);
            }
            if (CollectionUtil.isNotEmpty(mdcEquipmentTree.getChildren())) {
                fillEquipmentByProduction(mdcEquipmentTree.getChildren());
            }
        }
    }
 
}