hyingbo
2 天以前 6da57b610d97be3f79084d43440e7823d707b249
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
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.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.compress.utils.Lists;
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.dto.EquipmentCategoryStatistics;
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.entity.SysCategory;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
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;
 
    @Value("${jeecg.path.upload}")
    private String upLoadPath;
 
    @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<>();
        queryWrapper.eq("e.del_flag", CommonConstant.DEL_FLAG_0);
        //用户数据权限
        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.getManufacturingEnterprise())) {
                queryWrapper.like("e.manufacturing_enterprise", eamEquipment.getManufacturingEnterprise());
            }
            if (StringUtils.isNotBlank(eamEquipment.getOriginalValue())) {
                queryWrapper.like("e.original_value", eamEquipment.getOriginalValue());
            }
            if (StringUtils.isNotBlank(eamEquipment.getPerformanceIndicators())) {
                queryWrapper.like("e.performance_indicators", eamEquipment.getPerformanceIndicators());
            }
            if(oConvertUtils.isNotEmpty(eamEquipment.getLeaveFactoryStartTime()) && oConvertUtils.isNotEmpty(eamEquipment.getLeaveFactoryEndTime())){
                queryWrapper.ge("e.leave_factory_date",eamEquipment.getLeaveFactoryStartTime());
                queryWrapper.le("e.leave_factory_date",eamEquipment.getLeaveFactoryEndTime());
            }
            if(oConvertUtils.isNotEmpty(eamEquipment.getAcceptanceCheckStartTime()) && oConvertUtils.isNotEmpty(eamEquipment.getAcceptanceCheckEndTime())){
                queryWrapper.ge("e.acceptance_check_date",eamEquipment.getAcceptanceCheckStartTime());
                queryWrapper.le("e.acceptance_check_date",eamEquipment.getAcceptanceCheckEndTime());
            }
            if (StringUtils.isNotBlank(eamEquipment.getOrgId())) {
                //车间编码不为空
                List<String> productIds = mdcProductionService.recursionChildren(eamEquipment.getOrgId());
                if (CollectionUtil.isNotEmpty(productIds)) {
                    //不做筛选
                    queryWrapper.in("e.org_id", productIds);
                }
            }
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentCategory())) {
                queryWrapper.like("e.equipment_category", eamEquipment.getEquipmentCategory());
            }
            if (StringUtils.isNotBlank(eamEquipment.getEquipmentUse())) {
                queryWrapper.like("e.equipment_use", eamEquipment.getEquipmentUse());
            }
            if (StringUtils.isNotBlank(eamEquipment.getId())) {
                queryWrapper.eq("e.id", eamEquipment.getId());
            }
            //排序
            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.orderByAsc("e." + oConvertUtils.camelToUnderline(column));
                }
            } else {
                queryWrapper.orderByAsc("e.equipment_code");
            }
        } else {
            queryWrapper.orderByAsc("e.equipment_code");
        }
 
        IPage<EamEquipment> ipage = eamEquipmentMapper.queryPageList(page, queryWrapper);
        return ipage;
    }
 
    @Override
    public List<EquipmentSearchResult> asyncLoadEquipment(String keyword, Integer pageSize, String id) {
        if (StringUtils.isNotBlank(id)) {
            EamEquipment eamEquipment = eamEquipmentMapper.selectById(id);
            if (eamEquipment != null) {
                List<EquipmentSearchResult> resultList = new ArrayList<>();
                resultList.add(new EquipmentSearchResult(eamEquipment));
                return resultList;
            }
        }
        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);
        }
        queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
        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();
    }
 
    @Override
    public EamEquipment selectByEquipmentCode(String equipmentCode) {
        if (StringUtils.isBlank(equipmentCode)) {
            return null;
        }
        QueryWrapper<EamEquipment> queryWrapper = new QueryWrapper<>();
        //用户数据权限
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (sysUser == null) {
            return null;
        }
        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());
        }
        queryWrapper.eq("equipment_code", equipmentCode);
        queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
 
        return eamEquipmentMapper.selectOne(queryWrapper);
    }
 
    @Override
    public List<EquipmentCategoryStatistics> queryLedgerStatistics() {
        List<EquipmentCategoryStatistics> list = Lists.newArrayList();
        //获取所有已设置设备类别的设备信息
        List<EamEquipment> equipmentList = this.list(new LambdaQueryWrapper<EamEquipment>().isNotNull(EamEquipment::getEquipmentType));
 
        //获取分类字典二级、三级以下包含当前分类的所有分类id
        List<SysCategory> categoryTypeMap = eamEquipmentMapper.getCategoryTypeMap();
        if (CollectionUtils.isEmpty(equipmentList) || CollectionUtils.isEmpty(categoryTypeMap)) {
            return list;
        }
        for (SysCategory sysCategory : categoryTypeMap) {
            List<EamEquipment> eamEquipmentList = equipmentList.stream().filter(eamEquipment -> Arrays.asList(sysCategory.getChildCodes().split(StringPool.COMMA)).contains(eamEquipment.getEquipmentType())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(eamEquipmentList)) {
                continue;
            }
            EquipmentCategoryStatistics statistics = new EquipmentCategoryStatistics();
            statistics.setEquipmentType(sysCategory.getName());
 
            //设置设备总数
            statistics.setEquipmentCount(eamEquipmentList.size());
            statistics.setOriginalValue(eamEquipmentList.stream().filter(eamEquipment -> eamEquipment.getOriginalValue() != null).map(EamEquipment::getOriginalValue).reduce(BigDecimal.ZERO, BigDecimal::add));
 
            //设置高精度信息
            List<EamEquipment> equipmentCategoryList = eamEquipmentList.stream().filter(eamEquipment -> "JMSB".equals(eamEquipment.getEquipmentCategory())).collect(Collectors.toList());
            statistics.setHighPrecisionCount(CollectionUtils.isNotEmpty(equipmentCategoryList) ? equipmentCategoryList.size() : 0);
            statistics.setHighPrecisionOriginalValue(CollectionUtils.isNotEmpty(equipmentCategoryList) ? equipmentCategoryList.stream().map(eamEquipment -> eamEquipment.getOriginalValue() != null ? eamEquipment.getOriginalValue() : BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add) : BigDecimal.ZERO);
 
            //设置国产信息
            List<EamEquipment> equipmentDomesticList = eamEquipmentList.stream().filter(eamEquipment -> "china".equals(eamEquipment.getOriginCountry())).collect(Collectors.toList());
            statistics.setDomesticCount(CollectionUtils.isNotEmpty(equipmentDomesticList) ? equipmentDomesticList.size() : 0);
            statistics.setDomesticOriginalValue(CollectionUtils.isNotEmpty(equipmentDomesticList) ? equipmentDomesticList.stream().map(eamEquipment -> eamEquipment.getOriginalValue() != null ? eamEquipment.getOriginalValue() : BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add) : BigDecimal.ZERO);
 
            //设置进口信息
            List<EamEquipment> equipmentImportList = eamEquipmentList.stream().filter(eamEquipment -> !"china".equals(eamEquipment.getOriginCountry())).collect(Collectors.toList());
            statistics.setImportCount(CollectionUtils.isNotEmpty(equipmentImportList) ? equipmentImportList.size() : 0);
            statistics.setImportOriginalValue(CollectionUtils.isNotEmpty(equipmentImportList) ? equipmentImportList.stream().map(eamEquipment -> eamEquipment.getOriginalValue() != null ? eamEquipment.getOriginalValue() : BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add) : BigDecimal.ZERO);
 
            //设置完好信息
            List<EamEquipment> equipmentIntactList = eamEquipmentList.stream().filter(eamEquipment -> StringPool.ONE.equals(eamEquipment.getTechnologyStatus())).collect(Collectors.toList());
            statistics.setIntactCount(CollectionUtils.isNotEmpty(equipmentIntactList) ? equipmentIntactList.size() : 0);
            statistics.setIntactOriginalValue(CollectionUtils.isNotEmpty(equipmentIntactList) ? equipmentIntactList.stream().map(eamEquipment -> eamEquipment.getOriginalValue() != null ? eamEquipment.getOriginalValue() : BigDecimal.ZERO).reduce(BigDecimal.ZERO, BigDecimal::add) : BigDecimal.ZERO);
            list.add(statistics);
        }
        return list;
    }
 
    @Override
    public ModelAndView exportLedgerStatisticsXls(HttpServletRequest request, Class<EquipmentCategoryStatistics> clazz, String title) {
        // 获取导出人
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
 
        // Step.2 获取导出数据
        List<EquipmentCategoryStatistics> exportList = queryLedgerStatistics();
 
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        //update-begin--Author:liusq  Date:20210126 for:图片导出报错,ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for:图片导出报错,ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }
 
    /**
     * 获取所有的产线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());
            }
        }
    }
 
}