gongchunyi
2026-04-29 56b73ce57147653cfbcfe87a7c28e9792dbb2f72
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
package com.ruoyi.production.service.impl;
 
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.Pictures;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.MatrixToImageWriter;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.production.dto.ProductWorkOrderDto;
import com.ruoyi.production.mapper.ProductionProductMainMapper;
import com.ruoyi.production.mapper.ProductionProductReportDailyMapper;
import com.ruoyi.production.mapper.ProductWorkOrderFileMapper;
import com.ruoyi.production.mapper.ProductWorkOrderMapper;
import com.ruoyi.production.mapper.ProductWorkOrderRapporteurMapper;
import com.ruoyi.production.pojo.ProductionProductMain;
import com.ruoyi.production.pojo.ProductionProductReportDaily;
import com.ruoyi.production.pojo.ProductWorkOrder;
import com.ruoyi.production.pojo.ProductWorkOrderFile;
import com.ruoyi.production.pojo.ProductWorkOrderRapporteur;
import com.ruoyi.production.service.ProductWorkOrderService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
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 javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 
@Service
@Transactional(rollbackFor = Exception.class)
public class ProductWorkOrderServiceImpl extends ServiceImpl<ProductWorkOrderMapper, ProductWorkOrder> implements ProductWorkOrderService {
 
    @Autowired
    private ProductWorkOrderMapper productWorkOrdermapper;
    @Autowired
    private ProductWorkOrderFileMapper productWorkOrderFileMapper;
    @Autowired
    private ProductWorkOrderRapporteurMapper productWorkOrderRapporteurMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ProductionProductMainMapper productionProductMainMapper;
    @Autowired
    private ProductionProductReportDailyMapper productionProductReportDailyMapper;
 
    @Value("${file.temp-dir}")
    private String tempDir;
 
    @Override
    public IPage<ProductWorkOrderDto> listPage(Page<ProductWorkOrderDto> page, ProductWorkOrderDto productWorkOrder) {
        boolean reportView = productWorkOrder != null && Integer.valueOf(2).equals(productWorkOrder.getType());
        Long currentUserId = null;
        if (reportView) {
            currentUserId = SecurityUtils.getUserId();
            productWorkOrder.setCurrentUserId(currentUserId);
        }
        IPage<ProductWorkOrderDto> pageData = productWorkOrdermapper.pageProductWorkOrder(page, productWorkOrder);
        List<ProductWorkOrderDto> records = pageData.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return pageData;
        }
 
        List<Long> workOrderIds = records.stream()
                .map(ProductWorkOrderDto::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(workOrderIds)) {
            return pageData;
        }
 
        List<ProductWorkOrderRapporteur> rapporteurs = productWorkOrderRapporteurMapper.selectList(Wrappers.<ProductWorkOrderRapporteur>lambdaQuery()
                .in(ProductWorkOrderRapporteur::getWorkOrderId, workOrderIds));
 
        final Map<Long, List<Long>> rapporteurMap = CollectionUtils.isNotEmpty(rapporteurs)
                ? rapporteurs.stream()
                .filter(item -> item.getWorkOrderId() != null && item.getUserId() != null)
                .collect(Collectors.groupingBy(
                        ProductWorkOrderRapporteur::getWorkOrderId,
                        LinkedHashMap::new,
                        Collectors.mapping(ProductWorkOrderRapporteur::getUserId, Collectors.toList())
                ))
                : new LinkedHashMap<>();
 
        records.forEach(item -> {
            List<Long> userIds = rapporteurMap.get(item.getId());
            item.setReportWorkersId(userIds == null ? new Long[0] : userIds.toArray(new Long[0]));
        });
 
        // type=2 时:回填“当前报工人今日状态”
        if (reportView && currentUserId != null) {
            // 1) 进行中(status=0)的报工
            List<ProductionProductMain> runningList = productionProductMainMapper.selectList(
                    Wrappers.<ProductionProductMain>lambdaQuery()
                            .in(ProductionProductMain::getWorkOrderId, workOrderIds)
                            .eq(ProductionProductMain::getUserId, currentUserId)
                            .eq(ProductionProductMain::getStatus, 0)
            );
            final java.util.Set<Long> runningWorkOrders = CollectionUtils.isNotEmpty(runningList)
                    ? runningList.stream().map(ProductionProductMain::getWorkOrderId).filter(Objects::nonNull).collect(Collectors.toSet())
                    : java.util.Collections.emptySet();
 
            // 2) 今日已结束(今日有 daily 明细)
            LocalDate today = LocalDate.now();
            List<ProductionProductReportDaily> todayDailyList = productionProductReportDailyMapper.selectList(
                    Wrappers.<ProductionProductReportDaily>lambdaQuery()
                            .in(ProductionProductReportDaily::getWorkOrderId, workOrderIds)
                            .eq(ProductionProductReportDaily::getUserId, currentUserId)
                            .eq(ProductionProductReportDaily::getReportDate, today)
            );
            final java.util.Set<Long> endedTodayWorkOrders = CollectionUtils.isNotEmpty(todayDailyList)
                    ? todayDailyList.stream().map(ProductionProductReportDaily::getWorkOrderId).filter(Objects::nonNull).collect(Collectors.toSet())
                    : java.util.Collections.emptySet();
 
            records.forEach(item -> {
                Long woId = item.getId();
                if (woId == null) {
                    item.setTodayReportState(1);
                    return;
                }
                if (runningWorkOrders.contains(woId)) {
                    item.setTodayReportState(2);
                    return;
                }
                if (endedTodayWorkOrders.contains(woId)) {
                    item.setTodayReportState(3);
                    return;
                }
                item.setTodayReportState(1);
            });
        }
 
        // 回填报工时间总和(分钟): type=2 按当前登录人汇总,其他按工单全员汇总
        QueryWrapper<ProductionProductReportDaily> totalDurationQw = new QueryWrapper<>();
        totalDurationQw.select("work_order_id as workOrderId", "sum(duration_minutes) as totalMinutes")
                .in("work_order_id", workOrderIds)
                .groupBy("work_order_id");
        if (reportView && currentUserId != null) {
            totalDurationQw.eq("user_id", currentUserId);
        }
        List<Map<String, Object>> durationRows = productionProductReportDailyMapper.selectMaps(totalDurationQw);
        Map<Long, BigDecimal> durationMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(durationRows)) {
            for (Map<String, Object> row : durationRows) {
                Object workOrderObj = row.get("workOrderId");
                if (workOrderObj == null) {
                    workOrderObj = row.get("work_order_id");
                }
                Object totalObj = row.get("totalMinutes");
                if (totalObj == null) {
                    totalObj = row.get("total_minutes");
                }
                if (workOrderObj == null || totalObj == null) {
                    continue;
                }
                Long woId = workOrderObj instanceof Number ? ((Number) workOrderObj).longValue() : Long.valueOf(workOrderObj.toString());
                BigDecimal totalMinutes = totalObj instanceof BigDecimal
                        ? (BigDecimal) totalObj
                        : new BigDecimal(totalObj.toString());
                durationMap.put(woId, totalMinutes);
            }
        }
        records.forEach(item -> item.setTotalReportDurationMinutes(durationMap.getOrDefault(item.getId(), BigDecimal.ZERO)));
        return pageData;
    }
 
    @Override
    public int updateProductWorkOrder(ProductWorkOrderDto productWorkOrderDto) {
        int rows = productWorkOrdermapper.updateById(productWorkOrderDto);
        if (rows <= 0 || productWorkOrderDto.getId() == null) {
            return rows;
        }
 
        productWorkOrderRapporteurMapper.delete(Wrappers.<ProductWorkOrderRapporteur>lambdaQuery()
                .eq(ProductWorkOrderRapporteur::getWorkOrderId, productWorkOrderDto.getId()));
 
        Long[] rapporteurIds = productWorkOrderDto.getReportWorkersId();
        if (rapporteurIds == null || rapporteurIds.length == 0) {
            return rows;
        }
 
        List<Long> candidateUserIds = Arrays.stream(rapporteurIds)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(candidateUserIds)) {
            return rows;
        }
 
        List<Long> existUserIds = sysUserMapper.selectUsersByIds(candidateUserIds).stream()
                .map(SysUser::getUserId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        if (existUserIds.size() != candidateUserIds.size()) {
            List<Long> invalidUserIds = candidateUserIds.stream()
                    .filter(id -> !existUserIds.contains(id))
                    .collect(Collectors.toList());
            throw new ServiceException("报工人不存在: " + invalidUserIds);
        }
 
        List<ProductWorkOrderRapporteur> rapporteurList = candidateUserIds.stream()
                .map(userId -> {
                    ProductWorkOrderRapporteur rapporteur = new ProductWorkOrderRapporteur();
                    rapporteur.setWorkOrderId(productWorkOrderDto.getId());
                    rapporteur.setUserId(userId);
                    return rapporteur;
                })
                .collect(Collectors.toList());
 
        if (!rapporteurList.isEmpty()) {
            rapporteurList.forEach(productWorkOrderRapporteurMapper::insert);
        }
        return rows;
    }
 
    @Override
    public void down(HttpServletResponse response, ProductWorkOrder productWorkOrder) {
        ProductWorkOrderDto productWorkOrderDto = productWorkOrdermapper.getProductWorkOrderFlowCard(productWorkOrder.getId());
 
        Long workOrderId = productWorkOrder.getId();
        Long productProcessRouteItemId = productWorkOrderDto.getProductProcessRouteItemId();
 
        String qrContent = String.format(
                "{\"workOrderId\":%d,\"productProcessRouteItemId\":%d}",
                workOrderId,
                productProcessRouteItemId == null ? 0 : productProcessRouteItemId
        );
 
        String codePath;
        try {
            codePath = new MatrixToImageWriter().code(qrContent, tempDir);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        /*获取附件图片类型*/
        List<Map<String, Object>> images = new ArrayList<>();
        List<ProductWorkOrderFile> productWorkOrderFiles = productWorkOrderFileMapper.selectList(Wrappers.<ProductWorkOrderFile>lambdaQuery().eq(ProductWorkOrderFile::getWorkOrderId, productWorkOrder.getId()));
        if (CollectionUtils.isNotEmpty(productWorkOrderFiles)) {
            productWorkOrderFiles.forEach(productWorkOrderFile -> {
                Map<String, Object> image = new HashMap<>();
                PictureRenderData pictureRenderData = Pictures.ofLocal(productWorkOrderFile.getUrl()).sizeInCm(17, 20).create();
                image.put("url", pictureRenderData);
                images.add(image);
            });
        }
        InputStream inputStream = this.getClass().getResourceAsStream("/static/work-order-template.docx");
        XWPFTemplate template = XWPFTemplate.compile(inputStream).render(
                new HashMap<String, Object>() {{
                    put("process", productWorkOrderDto.getProcessName());
                    put("workOrderNo", productWorkOrderDto.getWorkOrderNo());
                    put("productOrderNpsNo", productWorkOrderDto.getProductOrderNpsNo());
                    put("productName", productWorkOrderDto.getProductName());
                    put("planQuantity", productWorkOrderDto.getPlanQuantity());
                    put("model", productWorkOrderDto.getModel());
                    put("completeQuantity", productWorkOrderDto.getCompleteQuantity());
                    put("scrapQty", productWorkOrderDto.getScrapQty());
                    put("planStartTime", productWorkOrderDto.getPlanStartTime());
                    put("planEndTime", productWorkOrderDto.getPlanEndTime());
                    put("actualStartTime", productWorkOrderDto.getActualStartTime());
                    put("actualEndTime", productWorkOrderDto.getActualEndTime());
                    put("twoCode", Pictures.ofLocal(codePath).create());
                    put("images", images.isEmpty() ? null : images);
                }});
 
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    "流转卡", "UTF-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
 
}