cuilei
7 天以前 1563fa33e203959c80ee713ffff3cad2f825b098
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
package org.jeecg.modules.eam.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
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.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.FileUploadResult;
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.*;
import org.jeecg.modules.eam.entity.*;
import org.jeecg.modules.eam.mapper.EamReportRepairMapper;
import org.jeecg.modules.eam.request.EamReportRepairQuery;
import org.jeecg.modules.eam.service.IEamEquipmentExtendService;
import org.jeecg.modules.eam.service.IEamEquipmentFaultReasonService;
import org.jeecg.modules.eam.service.IEamEquipmentService;
import org.jeecg.modules.eam.service.IEamReportRepairService;
import org.jeecg.modules.eam.vo.EquipmentRepairListVO;
import org.jeecg.modules.eam.vo.EquipmentRepairStatistics;
import org.jeecg.modules.eam.vo.EquipmentRepairStatusStatistics;
import org.jeecg.modules.eam.vo.FaultTypeStatisticsVO;
import org.jeecg.modules.qywx.message.vo.TemplateCard;
import org.jeecg.modules.qywx.message.vo.TemplateCardEntity;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.impl.ThirdAppWechatEnterpriseServiceImpl;
import org.jeecg.modules.system.vo.UserSelector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @Description: 故障报修
 * @Author: Lius
 * @Date: 2025-04-01
 */
@Service
public class EamReportRepairServiceImpl extends ServiceImpl<EamReportRepairMapper, EamReportRepair> implements IEamReportRepairService {
 
    @Resource
    private IEamEquipmentExtendService eamEquipmentExtendService;
    @Autowired
    private IMdcProductionService mdcProductionService;
    @Autowired
    private IEamEquipmentFaultReasonService eamEquipmentFaultReasonService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IEamEquipmentService eamEquipmentService;
    @Autowired
    private ThirdAppWechatEnterpriseServiceImpl wechatEnterpriseService;
 
    @Value("${wechatEnterprise.cardActionUrl}")
    private String cardActionUrl;
 
    /**
     * 分页列表
     *
     * @param page
     * @param query
     * @return
     */
    @Override
    public IPage<EamReportRepair> pageList(Page<EamReportRepair> page, EamReportRepairQuery query) {
        QueryWrapper<EamReportRepair> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wmo.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 (query != null) {
            if (StringUtils.isNotBlank(query.getEquipmentId())) {
                queryWrapper.eq("wmo.equipment_id", query.getEquipmentId());
            }
            if (StringUtils.isNotBlank(query.getEquipmentCode())) {
                queryWrapper.like("e.equipment_code", query.getEquipmentCode());
            }
            if (StringUtils.isNotBlank(query.getEquipmentName())) {
                queryWrapper.like("e.equipment_name", query.getEquipmentName());
            }
            if (StringUtils.isNotBlank(query.getBreakdownFlag())) {
                queryWrapper.eq("wmo.breakdown_flag", query.getBreakdownFlag());
            }
            if (StringUtils.isNotBlank(query.getReportStatus())) {
                queryWrapper.eq("wmo.report_status", query.getReportStatus());
            }
            if (StringUtils.isNotBlank(query.getFaultDescription())) {
                queryWrapper.like("wmo.fault_description", query.getFaultDescription());
            }
            if(query.getStartTime() != null && query.getEndTime() != null) {
                queryWrapper.between("wmo.fault_start_time", query.getStartTime(), query.getEndTime());
            }
            //排序
            if (StringUtils.isNotBlank(query.getColumn()) && StringUtils.isNotBlank(query.getOrder())) {
                String column = query.getColumn();
                if (column.endsWith(CommonConstant.DICT_TEXT_SUFFIX)) {
                    column = column.substring(0, column.lastIndexOf(CommonConstant.DICT_TEXT_SUFFIX));
                }
                if (DataBaseConstant.SQL_ASC.equalsIgnoreCase(query.getOrder())) {
                    queryWrapper.orderByAsc("wmo." + oConvertUtils.camelToUnderline(column));
                } else {
                    queryWrapper.orderByDesc("wmo." + oConvertUtils.camelToUnderline(column));
                }
            } else {
                queryWrapper.orderByDesc("wmo.create_time");
            }
        } else {
            queryWrapper.orderByDesc("wmo.create_time");
        }
 
        return baseMapper.queryPageList(page, queryWrapper);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @EquipmentHistoryLog(operationTag = EquipmentOperationTagEnum.REPORT_REPAIR, businessTable = "eam_report_repair")
    public EamReportRepair reportRepairFromMaintenance(String equipmentId, String reportUser, List<EamWeekMaintenanceOrderDetail> detailList) {
        StringBuilder sb = new StringBuilder();
        detailList.forEach(detail -> {
            sb.append(detail.getItemCode()).append("、");
            sb.append(detail.getExceptionDescription()).append(";");
        });
        EamEquipmentFaultReason reason = eamEquipmentFaultReasonService.selectByFaultCode(BusinessCodeConst.EFR20250009);
        EamReportRepair entity = new EamReportRepair();
        entity.setEquipmentId(equipmentId);
        entity.setCreateBy(reportUser);
        entity.setFaultStartTime(new Date());
        entity.setBreakdownFlag(CommonConstant.DEFAULT_0);
        entity.setDelFlag(CommonConstant.DEL_FLAG_0);
        if(reason == null){
            entity.setFaultName("周保执行报修处理");
            entity.setFaultDescription(sb.toString());
        }else {
            entity.setFaultName(reason.getFaultName());
            entity.setFaultType(reason.getFaultCategory());
        }
        entity.setReportStatus(ReportRepairEnum.WAIT_REPAIR.name());
        this.baseMapper.insert(entity);
        //更新设备维修状态
        eamEquipmentExtendService.updateEquipmentRepairStatus(entity.getEquipmentId(), EquipmentRepairStatus.WAIT_REPAIR.name());
        return entity;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @EquipmentHistoryLog(operationTag = EquipmentOperationTagEnum.REPORT_REPAIR, businessTable = "eam_report_repair")
    public EamReportRepair reportRepairFromInspection(String equipmentId, String reportUser, List<EamInspectionOrderDetail> detailList) {
        StringBuilder sb = new StringBuilder();
        detailList.forEach(detail -> {
            sb.append(detail.getItemCode()).append("、");
            sb.append(detail.getExceptionDescription()).append(";");
        });
        EamEquipmentFaultReason reason = eamEquipmentFaultReasonService.selectByFaultCode(BusinessCodeConst.EFR20250010);
        EamReportRepair entity = new EamReportRepair();
        entity.setEquipmentId(equipmentId);
        entity.setCreateBy(reportUser);
        entity.setFaultStartTime(new Date());
        entity.setBreakdownFlag(CommonConstant.DEFAULT_0);
        entity.setDelFlag(CommonConstant.DEL_FLAG_0);
        if(reason == null){
            entity.setFaultName("点检执行报修处理");
            entity.setFaultDescription(sb.toString());
        }else {
            entity.setFaultName(reason.getFaultName());
            entity.setFaultType(reason.getFaultCategory());
        }
        entity.setReportStatus(ReportRepairEnum.WAIT_REPAIR.name());
        this.baseMapper.insert(entity);
        //更新设备维修状态
        eamEquipmentExtendService.updateEquipmentRepairStatus(entity.getEquipmentId(), EquipmentRepairStatus.WAIT_REPAIR.name());
        return entity;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    @EquipmentHistoryLog(operationTag = EquipmentOperationTagEnum.REPORT_REPAIR, businessTable = "eam_report_repair")
    public EamReportRepair add(EamReportRepair eamReportRepair) {
        eamReportRepair.setReportStatus(ReportRepairEnum.WAIT_REPAIR.name());
        eamReportRepair.setDelFlag(CommonConstant.DEL_FLAG_0);
        // 附件处理
        if (eamReportRepair.getImageFilesResult() != null) {
            List<FileUploadResult> imageFilesResult = eamReportRepair.getImageFilesResult();
            ObjectMapper mapper = new ObjectMapper();
            try {
                String referenceFile = mapper.writeValueAsString(imageFilesResult);
                eamReportRepair.setImageFiles(referenceFile);
            } catch (JsonProcessingException e) {
                return null;
            }
        }
        this.baseMapper.insert(eamReportRepair);
        //更新设备维修状态
        eamEquipmentExtendService.updateEquipmentRepairStatus(eamReportRepair.getEquipmentId(), EquipmentRepairStatus.WAIT_REPAIR.name());
        //推送企业微信消息
        sendQywxTemplateCardMessage(eamReportRepair);
        return eamReportRepair;
    }
 
    private void sendQywxTemplateCardMessage(EamReportRepair eamReportRepair) {
        TemplateCard templateCard = new TemplateCard();
        String equipmentId = eamReportRepair.getEquipmentId();
        EamEquipment equipment = eamEquipmentService.getById(equipmentId);
        if (equipment != null) {
            //查询有该设备权限的所有维修工
            String productionId = equipment.getOrgId();
            List<UserSelector> userSelectorList = sysUserService.selectOperatorList(equipment.getEquipmentCode(), productionId, BusinessCodeConst.PCR0002);
            String toUsers = userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.joining(","));
            String touser = wechatEnterpriseService.getTouser(toUsers, false);
            templateCard.setTouser(touser);
 
            TemplateCardEntity templateCardEntity = new TemplateCardEntity();
            templateCard.setTemplate_card(templateCardEntity);
            templateCardEntity.setTask_id(eamReportRepair.getId());
            TemplateCardEntity.MainTitle mainTitle = new TemplateCardEntity.MainTitle();
            mainTitle.setTitle("设备维修");
            templateCardEntity.setMain_title(mainTitle);
            templateCardEntity.setSub_title_text("您有待领取维修工单,请进入系统领取");
            TemplateCardEntity.CardAction cardAction = new TemplateCardEntity.CardAction();
            cardAction.setType(1);
            cardAction.setUrl(cardActionUrl);
            templateCardEntity.setCard_action(cardAction);
            wechatEnterpriseService.sendTemplateCardMsg(templateCard, true);
        } else {
            log.error("报修工单: " + eamReportRepair.getId() + " 设备不存在,无法推送工单待领取提醒消息!");
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(EamReportRepair eamReportRepair) {
        // 附件处理
        if (eamReportRepair.getImageFilesResult() != null) {
            List<FileUploadResult> imageFilesResult = eamReportRepair.getImageFilesResult();
            ObjectMapper mapper = new ObjectMapper();
            try {
                String referenceFile = mapper.writeValueAsString(imageFilesResult);
                eamReportRepair.setImageFiles(referenceFile);
            } catch (JsonProcessingException e) {
                return false;
            }
        } else {
            eamReportRepair.setImageFiles(null);
        }
        this.baseMapper.updateById(eamReportRepair);
        return true;
    }
 
    @Override
    public List<EquipmentRepairStatistics> equipmentRepairStatistics(String productionId, LocalDate firstOfMonth, LocalDate today) {
        if (StringUtils.isNotBlank(productionId)) {
            //车间编码不为空
            List<String> productIds = mdcProductionService.recursionChildren(productionId);
            if (CollectionUtil.isEmpty(productIds)) {
                return Collections.emptyList();
            }
            List<EquipmentRepairStatistics> list = this.baseMapper.equipmentRepairStatistics(productIds, firstOfMonth.toString(), today.plusDays(1).toString());
            if (CollectionUtil.isEmpty(list)) {
                return Collections.emptyList();
            }
            return list;
        }
        List<EquipmentRepairStatistics> list = this.baseMapper.equipmentRepairStatistics(null, firstOfMonth.toString(), today.plusDays(1).toString());
        if (CollectionUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list;
    }
 
    @Override
    public EquipmentRepairStatusStatistics repairStatusStatistics() {
        return this.baseMapper.repairStatusStatistics();
    }
 
    @Override
    public List<EquipmentRepairListVO> repairList() {
        return this.baseMapper.repairList();
    }
 
    @Override
    public List<FaultTypeStatisticsVO> faultTypeStatistics(String start, String end) {
        return this.baseMapper.faultTypeStatistics(start, end);
    }
}