2 天以前 f350465db70d6f9db9494483ee0cec7793a9007b
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
package com.ruoyi.production.service.impl;
 
 
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
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.utils.MatrixToImageWriter;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.dto.ProductWorkOrderDto;
import com.ruoyi.production.mapper.ProductWorkOrderFileMapper;
import com.ruoyi.production.mapper.ProductWorkOrderMapper;
import com.ruoyi.production.mapper.ProductionMachineRecordMapper;
import com.ruoyi.production.pojo.ProductWorkOrder;
import com.ruoyi.production.pojo.ProductWorkOrderFile;
import com.ruoyi.production.pojo.ProductionMachineRecord;
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.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
@Service
@Transactional(rollbackFor = Exception.class)
public class ProductWorkOrderServiceImpl extends ServiceImpl<ProductWorkOrderMapper, ProductWorkOrder> implements ProductWorkOrderService {
 
    @Autowired
    private ProductWorkOrderMapper productWorkOrderMapper;
    @Autowired
    private ProductWorkOrderFileMapper productWorkOrderFileMapper;
    @Autowired
    private ProductionMachineRecordMapper productionMachineRecordMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
 
    @Value("${file.temp-dir}")
    private String tempDir;
 
    @Override
    public IPage<ProductWorkOrderDto> listPage(Page<ProductWorkOrderDto> page, ProductWorkOrderDto productWorkOrder) {
        return productWorkOrderMapper.pageProductWorkOrder(page, productWorkOrder);
    }
 
    @Override
    public int updateProductWorkOrder(ProductWorkOrderDto productWorkOrderDto) {
        return productWorkOrderMapper.updateById(productWorkOrderDto);
    }
 
    @Override
    public void down(HttpServletResponse response, ProductWorkOrder productWorkOrder) {
        ProductWorkOrderDto productWorkOrderDto = productWorkOrderMapper.getProductWorkOrderFlowCard(productWorkOrder.getId());
        String codePath;
        try {
            codePath = new MatrixToImageWriter().code(productWorkOrderDto.getId().toString(), 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");
 
        // 日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
 
        // 格式化日期时间字段
        String planStartTimeStr = formatDateTime(productWorkOrderDto.getPlanStartTime(), formatter);
        String planEndTimeStr = formatDateTime(productWorkOrderDto.getPlanEndTime(), formatter);
        String actualStartTimeStr = formatDateTime(productWorkOrderDto.getActualStartTime(), formatter);
        String actualEndTimeStr = formatDateTime(productWorkOrderDto.getActualEndTime(), formatter);
 
        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", planStartTimeStr);
                    put("planEndTime", planEndTimeStr);
                    put("actualStartTime", actualStartTimeStr);
                    put("actualEndTime", actualEndTimeStr);
                    put("twoCode", Pictures.ofLocal(codePath).create());
                    put("deviceName", productWorkOrderDto.getDeviceName());
                    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("导出失败");
        }
    }
 
    @Override
    public ProductWorkOrderDto getProductWorkOrderById(Long id) {
        ProductWorkOrderDto productWorkOrderDto = productWorkOrderMapper.getProductWorkOrderFlowCard(id);
        // 合并工单排产人员与机台上机操作员,去重后作为报工弹窗默认班组成员
        Set<Long> userIdSet = new LinkedHashSet<>();
        if (productWorkOrderDto != null && StrUtil.isNotBlank(productWorkOrderDto.getUserIds())) {
            for (String uid : productWorkOrderDto.getUserIds().split(",")) {
                if (StrUtil.isNotBlank(uid)) {
                    userIdSet.add(Long.parseLong(uid.trim()));
                }
            }
        }
        List<ProductionMachineRecord> machineRecords = productionMachineRecordMapper.selectList(
                Wrappers.<ProductionMachineRecord>lambdaQuery()
                        .eq(ProductionMachineRecord::getWorkOrderId, id)
                        .isNotNull(ProductionMachineRecord::getOperatorId));
        for (ProductionMachineRecord machineRecord : machineRecords) {
            if (StrUtil.isNotBlank(machineRecord.getOperatorId())) {
                for (String uid : machineRecord.getOperatorId().split(",")) {
                    if (StrUtil.isNotBlank(uid)) {
                        userIdSet.add(Long.parseLong(uid.trim()));
                    }
                }
            }
        }
        List<ProductWorkOrderDto.ReportPerson> reportPersons = new ArrayList<>();
        for (Long userId : userIdSet) {
            ProductWorkOrderDto.ReportPerson reportPerson = new ProductWorkOrderDto.ReportPerson();
            reportPerson.setUserId(userId);
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            if (sysUser != null) {
                reportPerson.setUserName(sysUser.getNickName());
            }
            reportPersons.add(reportPerson);
        }
        if (productWorkOrderDto != null) {
            productWorkOrderDto.setReportPersons(reportPersons);
        }
        return productWorkOrderDto;
    }
 
    @Override
    public String generateProductWorkOrder(String processName, String npsNo) {
        processName = StrUtil.isBlank(processName) ? "未知" : processName;
        Assert.notNull(npsNo, "生产订单号编号不能为空");
        Integer maxNo = productWorkOrderMapper.selectMax(npsNo);
        int sequenceNumber = maxNo + 1; // 默认序号
        String processPinyin = StringUtils.getProcessNo(processName);
        return StrUtil.format("{}{}", processPinyin, npsNo, String.format("%03d", sequenceNumber));
    }
 
    /**
     * 格式化 LocalDateTime 为字符串
     */
    private String formatDateTime(LocalDateTime dateTime, DateTimeFormatter formatter) {
        if (dateTime == null) {
            return "";
        }
        return dateTime.format(formatter);
    }
 
}