刘小明
2024-07-09 f37449146d61a49388f557210cad944f032efefe
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
package com.dy.pmsProduct.taskPlan;
 
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.util.StringUtils;
import com.dy.common.webFilter.UserTokenContext;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pmsGlobal.daoPlt.PltProductMapper;
import com.dy.pmsGlobal.daoPr.PrAssemblyPlanMapper;
import com.dy.pmsGlobal.daoPr.PrBatchNumberMapper;
import com.dy.pmsGlobal.daoPr.PrDeviceMapper;
import com.dy.pmsGlobal.daoPr.PrProductionProcessMapper;
import com.dy.pmsGlobal.pojoBa.BaUser;
import com.dy.pmsGlobal.pojoPlt.PltProduct;
import com.dy.pmsGlobal.pojoPr.PrAssemblyPlan;
import com.dy.pmsGlobal.pojoPr.PrBatchNumber;
import com.dy.pmsGlobal.pojoPr.PrDevice;
import com.dy.pmsGlobal.pojoPr.PrProductionProcess;
import com.dy.pmsGlobal.util.QrCodeConstant;
import com.dy.pmsGlobal.util.QrCodeUtil;
import com.dy.pmsGlobal.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
public class AssemblySv {
    private static final int BATCH_SIZE = 100; // 批量处理大小
    private static final String CODE_FORMAT = "%03d";
    private static final String CODE_FORMAT_7 = "%07d";
    private PrAssemblyPlanMapper assemblyDao;
    private UserUtil userUtil;
    private PrBatchNumberMapper batchDao;
    private PrDeviceMapper deviceDao;
    private PltProductMapper productDao;
    private PrProductionProcessMapper processDao;
    @Autowired
    public void setAssemblyDao(PrAssemblyPlanMapper assemblyDao) {
        this.assemblyDao = assemblyDao;
    }
    @Autowired
    public void setBatchDao(PrBatchNumberMapper batchDao) {
        this.batchDao = batchDao;
    }
    @Autowired
    public void setUserUtil(UserUtil userUtil){
        this.userUtil = userUtil;
    }
    @Autowired
    public void setDeviceDao(PrDeviceMapper deviceDao) {
        this.deviceDao = deviceDao;
    }
    @Autowired
    public void setProductDao(PltProductMapper productDao) {
        this.productDao = productDao;
    }
 
    @Autowired
    public void setProcessDao(PrProductionProcessMapper processDao) {
        this.processDao = processDao;
    }
 
    @Transactional
    public int save(PrAssemblyPlan plan){
        //计划名称不能重复
        if(assemblyDao.exists(plan.name,plan.id)){
            throw new RuntimeException("计划名称不能重复");
        }
        PrProductionProcess process = processDao.selectByPrimaryKey(plan.processId);
        if(process == null || !process.proId.equals(plan.proId)){
            throw new RuntimeException("产品与生产流程不匹配");
        }
        PrBatchNumber batch = new PrBatchNumber();
        batch.batchNumber = getNextCode();
        batch.proId = plan.proId;
        batch.remark = plan.content;
        BaUser loginUser = userUtil.getUser(UserTokenContext.get());
        if(loginUser!=null){
            batch.creator = loginUser.id;
            plan.creator = loginUser.id;
        }
        batchDao.insertSelective(batch);
        insertDevice(plan.proId,batch.id,batch.batchNumber,plan.number,1);
 
        plan.batchId = batch.id;
        plan.status =PlanStatusEnum.NORMAL.getCode();
        plan.deleted = false;
        return assemblyDao.insertSelective(plan);
    }
 
    @Transactional
    public int update(PrAssemblyPlan plan){
        //计划名称不能重复
        if(assemblyDao.exists(plan.name,plan.id)){
            throw new RuntimeException("计划名称不能重复");
        }
        PrProductionProcess process = processDao.selectByPrimaryKey(plan.processId);
        if(process == null || !process.proId.equals(plan.proId)){
            throw new RuntimeException("产品与生产流程不匹配");
        }
        //Fancy add 2024/07/09   如果状态为执行 ,则结束日期必须大于等于当前日期
        if(plan.status == PlanStatusEnum.NORMAL.getCode()){
            String endDateStr = plan.getEndDate();
            LocalDate endDate = LocalDate.parse(endDateStr, DateTimeFormatter.ISO_LOCAL_DATE);
            LocalDate nextDay = endDate.plusDays(1);
            LocalDate today = LocalDate.now(); // 获取当前日期
            if (nextDay.isBefore(today)) {
                throw new RuntimeException("执行状态结束日期必须大于等于当前日期");
            }
        }
        PrAssemblyPlan origPlan = assemblyDao.selectByPrimaryKey(plan.id);
        PrBatchNumber batch = batchDao.selectByPrimaryKey(origPlan.batchId);
        //产品改变则更新全部已生成设备号
        if(origPlan.proId != plan.proId){
            batch.proId = plan.proId;
            batchDao.updateByPrimaryKeySelective(batch);
 
            deviceDao.deleteByBatchId(origPlan.batchId);
            insertDevice(plan.proId,origPlan.batchId,batch.batchNumber,plan.number,1);
        }else if(origPlan.number < plan.number){
            //数量增加则新增设备号
            insertDevice(plan.proId,origPlan.batchId,batch.batchNumber,plan.number,origPlan.number+1);
        }
        return assemblyDao.updateByPrimaryKeySelective(plan);
    }
 
 
    @Transactional
    public int addDevice(Long planId,int num){
        PrAssemblyPlan plan = assemblyDao.selectByPrimaryKey(planId);
        PrBatchNumber batch = batchDao.selectByPrimaryKey(plan.batchId);
        int count =insertDevice(plan.proId,plan.batchId,batch.batchNumber,num + plan.number,plan.number+1);
//        plan.number = plan.number + num;
//        assemblyDao.updateByPrimaryKeySelective (plan);
        return count;
    }
 
 
    /**
     * 批量插入  设备数据
     * @param proId 产品id
     * @param batchId 批次id
     * @param batchCode 批次号
     * @param number 数量
     * @return 插入数量
     */
    private int insertDevice(Long proId,Long batchId,String batchCode,Integer number,int startIndex){
        log.info("批量插入设备");
        PltProduct product = productDao.selectByPrimaryKey(proId);
        List<PrDevice> list = new java.util.ArrayList<>();
        for(int i= startIndex ;i<= number;i++){
            PrDevice device = new PrDevice();
            device.proId = proId;
            device.batchId = batchId;
            device.deviceNo = getDeviceNo(product.code,batchCode,i);
            device.status = 0;
            list.add(device);
        }
        return insertBatchDevice(list);
    }
 
    private int insertBatchDevice(List<PrDevice> list) {
        int count = 0;
        for (int i = 0; i < list.size(); i += BATCH_SIZE) {
            List<PrDevice> subList = list.subList(i, Math.min(i + BATCH_SIZE, list.size()));
            // 调用MyBatis插入这批数据
            count += deviceDao.insertBatch(subList);
        }
        return count;
    }
 
    public String getNextCode(){
        log.info("获取批次编号");
        String currentDay = getCurrentDay();
        String maxCode = batchDao.selectMaxCode(currentDay);
        if (StringUtils.isBlank(maxCode)) {
            return currentDay + String.format(CODE_FORMAT, 1);
        } else {
            int nextCode = Integer.parseInt(maxCode.replace(currentDay, "")) + 1;
            // 检查溢出
            if (nextCode > 999) {
                throw new RuntimeException("批次编号溢出");
            }
            return currentDay + String.format(CODE_FORMAT, nextCode);
        }
    }
    private static String getDeviceNo(String proCode,String batchCode,int index){
        String deviceNo = QrCodeConstant.TypeProduct + proCode + batchCode +
                String.format(CODE_FORMAT_7, index);
        return deviceNo;
    }
 
    public static void main(String[] args) {
        List<PrDevice> list = new java.util.ArrayList<>();
        for(int i= 1 ;i<= 10000;i++){
 
            PrDevice device = new PrDevice();
            device.proId = 2024052310512000003l;
            device.batchId = 2024053017154400004l;
            device.deviceNo = getDeviceNo("002","2024001",i);
            device.status = 0;
 
            list.add(device);
        }
        list.forEach(device->{
            System.out.println(device.deviceNo);
        });
    }
 
    /**
     * 获取当前日期
     * @return yyMMdd
     */
    private String getCurrentDay(){
        return DateUtil.format(new Date(), "yyMMdd");
    }
 
    public PrAssemblyPlan selectById(Long id){
        return assemblyDao.selectByPrimaryKey(id);
    }
 
    public QueryResultVo<List<PrAssemblyPlan>> selectSome(QueryVo queryVo){
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        //查询符合条件的记录总数
        Long itemTotal = assemblyDao.selectSomeCount(params);
        QueryResultVo<List<PrAssemblyPlan>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ;
        //计算分页等信息
        rsVo.calculateAndSet(itemTotal, params);
        //查询符合条件的记录
        rsVo.obj = assemblyDao.selectSome(params);
        return rsVo ;
    }
 
    public QueryResultVo<List<PrDevice>> selectSomeDevice(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        //查询符合条件的记录总数
        Long itemTotal = deviceDao.selectSomeCount(params);
        QueryResultVo<List<PrDevice>> rsVo = new QueryResultVo<>(queryVo.pageSize, queryVo.pageCurr) ;
        //计算分页等信息
        rsVo.calculateAndSet(itemTotal, params);
        //查询符合条件的记录
        rsVo.obj = deviceDao.selectSome(params);
        rsVo.obj.forEach(device->{
            device.qrCode = QrCodeUtil.genQrCodeString(device.deviceNo);
        });
        return rsVo ;
    }
 
    public List<PrDevice> selectDeviceByBatchId(Long batchId) {
        return deviceDao.selectByBatchId(batchId);
    }
 
    /**
     * 只更新状态,不更新其他字段
     * @param plan
     * @return
     */
    @Transactional
    public int updateStatus(PrAssemblyPlan plan) {
        PrAssemblyPlan param = new PrAssemblyPlan();
        param.id =plan.id;
        param.status = plan.status;
        return assemblyDao.updateByPrimaryKeySelective(param);
    }
}