huang
2025-10-30 a99650cb00bf5b0650c33f39a4221b765201d228
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
package com.mes.base.report.service.impl;
 
import cn.smallbun.screw.core.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mes.base.damage.service.DamageService;
import com.mes.base.glassinfo.service.GlassInfoService;
import com.mes.base.report.mapper.ReportInfoMapper;
import com.mes.base.report.service.ReportInfoService;
import com.mes.base.work.service.WorkAssignmentService;
import com.mes.damage.Damage;
import com.mes.erp.ReportingWork;
import com.mes.erp.ReportingWorkDetail;
import com.mes.feign.ErpUrlOpenFeignService;
import com.mes.feign.OpenFeignService;
import com.mes.glassinfo.GlassInfo;
import com.mes.reportinfo.ReportInfo;
import com.mes.reportinfo.dto.ReportInfoDTO;
import com.mes.reportinfo.request.ReportQueryRequest;
import com.mes.sysconst.Const;
import com.mes.work.WorkAssignment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wu
 * @since 2024-06-13
 */
@Slf4j
@Service
public class ReportInfoServiceImpl extends ServiceImpl<ReportInfoMapper, ReportInfo> implements ReportInfoService {
 
    @Resource
    ErpUrlOpenFeignService erpUrlOpenFeignService;
    @Resource
    WorkAssignmentService workAssignmentService;
    @Resource
    GlassInfoService glassInfoService;
    @Resource
    DamageService damageService;
    @Resource
    OpenFeignService openFeignService;
 
    @Override
    public void saveReport(ReportInfoDTO reportInfoDTO) {
        //判断完工数据是否已经存在,避免重复添加
        if (getReportExist(reportInfoDTO)) {
            //补充破损记录缺少的小片数据
            ReportInfo reportInfo = new ReportInfo();
            BeanUtils.copyProperties(reportInfoDTO, reportInfo);
            LambdaQueryWrapper<GlassInfo> glassInfoSelectWrapper = new LambdaQueryWrapper<>();
            glassInfoSelectWrapper.eq(GlassInfo::getGlassId, reportInfo.getGlassId());
            GlassInfo glassInfo = glassInfoService.getOne(glassInfoSelectWrapper);
            BeanUtils.copyProperties(glassInfo, reportInfo);
            reportInfo.setProductSortNumber(glassInfo.getProductSortNumber());
            reportInfo.setCreateTime(LocalDateTime.now().toString());
            //补充值班信息
            WorkAssignment workAssignment = workAssignmentService.getOne(
                    new LambdaQueryWrapper<WorkAssignment>()
                            .eq(WorkAssignment::getLine, reportInfo.getLine())
                            .eq(WorkAssignment::getWorkProcess, reportInfo.getWorkingProcedure())
            );
            if (workAssignment != null) {
                reportInfo.setTeamsGroupsName(workAssignment.getTeamsGroupsName());
                reportInfo.setDeviceName(workAssignment.getDeviceName());
            }
            //进行报工,如果报工成功,则将报工状态设置为已报工,否则设置未报工
            if (submitReport(Arrays.asList(reportInfo))) {
                reportInfo.setReportState(Const.REPORT_STATE_REPORTED);
            } else {
                reportInfo.setReportState(Const.REPORT_STATE_COMPLETE);
            }
 
            //删除未现补未报工的破损拿走记录
            damageService.remove(
                    new LambdaQueryWrapper<Damage>()
                            .eq(Damage::getGlassId, reportInfo.getGlassId())
                            .lt(Damage::getReportState, Const.REPORT_STATE_REPORTED)
            );
            this.save(reportInfo);
        }
    }
 
    @Override
    public Boolean submitReport(List<ReportInfo> reportInfos) {
        for (ReportInfo reportInfo : reportInfos
        ) {
            ReportingWork reportingWork = new ReportingWork();
            reportingWork.setOrderId(reportInfo.getFlowCardId().substring(0, 10));
            reportingWork.setProductionId(reportInfo.getFlowCardId().substring(0, 11));
            reportingWork.setProcessId(reportInfo.getFlowCardId() + "/" + reportInfo.getLayer());
            if (StringUtils.isBlank(reportInfo.getDeviceName())) {
                reportingWork.setDeviceName("auto");
            } else {
                reportingWork.setDeviceName(reportInfo.getDeviceName());
            }
            reportingWork.setThisProcess(reportInfo.getWorkingProcedure());
            reportingWork.setThisCompletedQuantity(1);
            reportingWork.setThisWornQuantity(0);
            reportingWork.setClasses("早班");
            reportingWork.setReportingWorkTime(LocalDateTime.now());
            reportingWork.setTeamsGroupsName(reportInfo.getTeamsGroupsName());
            reportingWork.setCreator("auto");
 
            List<ReportingWorkDetail> reportingWorkDetails = new ArrayList<>();
            ReportingWorkDetail reportingWorkDetail = new ReportingWorkDetail();
            reportingWorkDetail.setTechnologyNumber(reportInfo.getLayer());
            reportingWorkDetail.setOrderNumber(reportInfo.getProductSortNumber());
            reportingWorkDetail.setCompletedQuantity(1);
            reportingWorkDetail.setBreakageQuantity(0);
            reportingWorkDetails.add(reportingWorkDetail);
            // 创建 JSON 对象,报工数据添加到对象中
 
            Map<String, Object> result = new HashMap<>(16);
            try {
 
                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, Object> detailsMap = objectMapper.convertValue(reportingWorkDetails.get(0), new TypeReference<Map<String, Object>>() {
                });
                result.put("detail", detailsMap);
                reportingWork.setProcessId(reportingWork.getProcessId().substring(0, 14));
                Map<String, Object> reportingWorkMap = objectMapper.convertValue(reportingWork, new TypeReference<Map<String, Object>>() {
                });
                reportingWorkMap.put("reportingWorkTime", reportingWork.getReportingWorkTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
                result.put("title", reportingWorkMap);
                result.put("type", 0);
                if (reportingWorkDetails.get(0).getDamageDetails() != null) {
                    int patchStatus = reportingWorkDetails.get(0).getDamageDetails().get(0).getPatchStatus();
                    result.put("isPatch", patchStatus == 1 ? 1 : 0);
                }
                result.put("userId", "admin");
                result.put("userName", "admin");
                result.put("qualityInsStatus", 0);
            } catch (Exception e) {
                log.error("JSON转换失败", e);
            }
            if (erpUrlOpenFeignService.mesReportingWork(result).getData()) {
                reportInfo.setReportState(Const.REPORT_STATE_REPORTED);
            } else {
                log.info("报完工失败,玻璃信息:{}", reportInfo);
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }
 
    @Override
    public Page<ReportInfo> queryReport(ReportQueryRequest reportQueryRequest) {
        return this.page(
                new Page<>(reportQueryRequest.getPageNo(), reportQueryRequest.getPageSize()),
                new LambdaQueryWrapper<ReportInfo>()
                        .eq(ReportInfo::getWorkingProcedure, reportQueryRequest.getWorkingProcedure())
                        .eq(ReportInfo::getReportState, reportQueryRequest.getState())
                        .eq(!reportQueryRequest.getGlassId().isEmpty(), ReportInfo::getGlassId, reportQueryRequest.getGlassId())
                        .between(null != reportQueryRequest.getStartTime(), ReportInfo::getCreateTime, reportQueryRequest.getStartTime(), reportQueryRequest.getEndTime())
        );
    }
 
    private Boolean getReportExist(ReportInfoDTO reportInfoDTO) {
        //判断完工数据是否已存在
        List<ReportInfo> reportInfos = this.list(
                new LambdaQueryWrapper<ReportInfo>()
                        .eq(ReportInfo::getGlassId, reportInfoDTO.getGlassId())
                        .eq(ReportInfo::getLine, reportInfoDTO.getLine())
                        .eq(ReportInfo::getWorkingProcedure, reportInfoDTO.getWorkingProcedure())
        );
        if (CollectionUtils.isEmpty(reportInfos)) {
            return Boolean.TRUE;
        } else {
            log.info("完工数据已存在,玻璃号:{}", reportInfoDTO.getGlassId());
            return Boolean.FALSE;
        }
    }
}