SnPartServiceImpl.java
3 KB
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
package com.huaheng.pc.config.sn.service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.sn.domain.SnPart;
import com.huaheng.pc.config.sn.mapper.SnPartMapper;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeaderHistory.domain.ReceiptHeaderHistory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.exception.BusinessException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import org.springframework.transaction.annotation.Transactional;
@Service
public class SnPartServiceImpl extends ServiceImpl<SnPartMapper, SnPart> implements SnPartService {
/**
* 随机生成部件号 9位数
* @param type
* @return
*/
public String createdPartCode(String type){
int length=8;
String characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder sb = new StringBuilder(length);
Random random=new Random();
for (int i = 0; i < length; i++) {
sb.append(characters.charAt(random.nextInt(characters.length())));
}
return type+sb.toString();
}
/**
* 查找最大的code
* @param type
* @return
*/
public String getMaxCode(String type){
LambdaQueryWrapper<SnPart> lambda = new LambdaQueryWrapper<>();
lambda.select(SnPart::getCode).eq(SnPart::getType, type);
lambda.orderByDesc(SnPart::getId).last("Limit 1");
//如果指定类型的最后的code存在,并且日期一致。那么 code = 入库单类型 + 年月日 + (排序号 + 1)
String maxCode = null;
SnPart snPart = this.getOne(lambda);
if (snPart != null) {
maxCode = this.getOne(lambda).getCode();
}
return maxCode;
}
/**
* 随机生成电路板或电源n个
* @param type
* @param num
* @return
*/
@Transactional
public AjaxResult savePart(String type,Integer num){
String maxCode=getMaxCode(type);
Integer Count =0;
if(maxCode!=null){
Count = Integer.valueOf(maxCode.substring(maxCode.length() - 1, maxCode.length()));
}
List<SnPart> list=new ArrayList<>();
for (int i=0;i<num;i++){
String code=type + String.format("%010d", Count ++);
SnPart snPart=new SnPart();
snPart.setCode(code);
snPart.setType(type);
snPart.setCreatedBy(ShiroUtils.getUser().getLoginName());
snPart.setLastUpdatedBy(ShiroUtils.getLoginName());
list.add(snPart);
}
this.saveBatch(list);
return AjaxResult.success();
}
}