优化商品的条码的自增逻辑,解决长条码的问题

This commit is contained in:
jishenghua
2025-12-25 22:30:03 +08:00
parent 1d5f0aefa8
commit 0a1a599a9d
3 changed files with 48 additions and 13 deletions

View File

@@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*; import java.util.*;
@Service @Service
@@ -1329,13 +1330,14 @@ public class MaterialService {
public String getMaxBarCode() { public String getMaxBarCode() {
List<String> barCodeOldList = materialMapperEx.getBarCodeList(); List<String> barCodeOldList = materialMapperEx.getBarCodeList();
// 使用 Stream API 处理条码列表 // 使用 Stream API 处理条码列表
OptionalLong maxBarcode = barCodeOldList.stream() // 使用 BigInteger 处理可能的大数字
Optional<BigInteger> maxBarcode = barCodeOldList.stream()
.filter(StringUtil::isNumeric) // 过滤掉非数字条码 .filter(StringUtil::isNumeric) // 过滤掉非数字条码
.mapToLong(Long::parseLong) // 将字符串转换为 Long 类型 .map(BigInteger::new) // 使用 BigInteger 构造函数
.max(); // 获取最大值 .max(Comparator.naturalOrder()); // 获取最大值
// 如果存在最大值,返回它;否则返回 1000L // 如果存在最大值,返回它;否则返回 1000L
Long maxBarCodeOld = maxBarcode.orElse(1000L); BigInteger maxBarCodeOld = maxBarcode.orElse(new BigInteger("1000"));
return maxBarCodeOld + ""; return maxBarCodeOld.toString();
} }
public List<String> getMaterialNameList() { public List<String> getMaterialNameList() {

View File

@@ -564,6 +564,36 @@ export function getPrevMonthFormatDate(monthNum) {
return prevMonth.getFullYear() + seperator1 + month + seperator1 + strDate return prevMonth.getFullYear() + seperator1 + month + seperator1 + strDate
} }
/**
* 自定义大数加法函数,两个数字相加
* @param a
* @param b
* @returns {string}
*/
export function addBigNumbers(a, b) {
a = a.toString();
b = b.toString();
// 反转字符串,从个位开始计算
let aRev = a.split('').reverse();
let bRev = b.split('').reverse();
const maxLength = Math.max(aRev.length, bRev.length);
let result = [];
let carry = 0;
for (let i = 0; i < maxLength; i++) {
const digitA = i < aRev.length ? parseInt(aRev[i]) : 0;
const digitB = i < bRev.length ? parseInt(bRev[i]) : 0;
const sum = digitA + digitB + carry;
result.push(sum % 10);
carry = Math.floor(sum / 10);
}
// 处理最后的进位
if (carry > 0) {
result.push(carry);
}
// 反转回来得到正确的结果
return result.reverse().join('');
}
/** /**
* JS中根据指定值删除数组中的元素 * JS中根据指定值删除数组中的元素
* @param arrylist * @param arrylist

View File

@@ -294,7 +294,7 @@
import JEditableTable from '@/components/jeecg/JEditableTable' import JEditableTable from '@/components/jeecg/JEditableTable'
import { FormTypes, getRefPromise, VALIDATE_NO_PASSED, validateFormAndTables } from '@/utils/JEditableTableUtil' import { FormTypes, getRefPromise, VALIDATE_NO_PASSED, validateFormAndTables } from '@/utils/JEditableTableUtil'
import { changeNameToPinYin, checkMaterial, checkMaterialBarCode, getMaterialAttributeNameList, getMaterialAttributeValueListById, getMaxBarCode, queryMaterialCategoryTreeList } from '@/api/api' import { changeNameToPinYin, checkMaterial, checkMaterialBarCode, getMaterialAttributeNameList, getMaterialAttributeValueListById, getMaxBarCode, queryMaterialCategoryTreeList } from '@/api/api'
import { autoJumpNextInput, handleIntroJs, removeByVal } from '@/utils/util' import { autoJumpNextInput, handleIntroJs, removeByVal, addBigNumbers } from '@/utils/util'
import { getAction, httpAction } from '@/api/manage' import { getAction, httpAction } from '@/api/manage'
import JImageUpload from '@/components/jeecg/JImageUpload' import JImageUpload from '@/components/jeecg/JImageUpload'
import JDate from '@/components/jeecg/JDate' import JDate from '@/components/jeecg/JDate'
@@ -578,11 +578,14 @@
if(this.action === 'copyAdd') { if(this.action === 'copyAdd') {
getMaxBarCode({}).then((res)=> { getMaxBarCode({}).then((res)=> {
if (res && res.code === 200) { if (res && res.code === 200) {
let maxBarCode = res.data.barCode - 0 let maxBarCode = res.data.barCode
let meTableData = [] let meTableData = []
for (let i = 0; i < tab.dataSource.length; i++) { for (let i = 0; i < tab.dataSource.length; i++) {
let meInfo = tab.dataSource[i] let meInfo = tab.dataSource[i]
meInfo.barCode = maxBarCode + i + 1 console.log(maxBarCode)
console.log(addBigNumbers(maxBarCode, i+1))
meInfo.barCode = addBigNumbers(maxBarCode, i+1)
console.log(meInfo.barCode)
meTableData.push(meInfo) meTableData.push(meInfo)
} }
tab.dataSource = meTableData tab.dataSource = meTableData
@@ -1025,7 +1028,7 @@
getMaxBarCode({}).then((res)=>{ getMaxBarCode({}).then((res)=>{
if(res && res.code===200) { if(res && res.code===200) {
let k = 0 let k = 0
let maxBarCode = res.data.barCode-0 let maxBarCode = res.data.barCode
for (let i = 0; i < barCodeSku.length; i++) { for (let i = 0; i < barCodeSku.length; i++) {
let currentBarCode = '' let currentBarCode = ''
let currentId = '' let currentId = ''
@@ -1050,7 +1053,7 @@
wholesaleDecimal: wholesaleDecimal, lowDecimal: lowDecimal}) wholesaleDecimal: wholesaleDecimal, lowDecimal: lowDecimal})
} else { } else {
k = k+1 k = k+1
currentBarCode = maxBarCode + k currentBarCode = addBigNumbers(maxBarCode, k)
meTableData.push({barCode: currentBarCode, commodityUnit: unit, sku: barCodeSku[i]}) meTableData.push({barCode: currentBarCode, commodityUnit: unit, sku: barCodeSku[i]})
} }
} }
@@ -1080,13 +1083,13 @@
if(this.maxBarCodeInfo === '') { if(this.maxBarCodeInfo === '') {
getMaxBarCode({}).then((res)=> { getMaxBarCode({}).then((res)=> {
if (res && res.code === 200) { if (res && res.code === 200) {
this.maxBarCodeInfo = res.data.barCode - 0 this.maxBarCodeInfo = res.data.barCode
this.maxBarCodeInfo = this.maxBarCodeInfo + 1 this.maxBarCodeInfo = addBigNumbers(this.maxBarCodeInfo, 1)
target.setValues([{rowKey: row.id, values: {barCode: this.maxBarCodeInfo, commodityUnit: unit?unit:''}}]) target.setValues([{rowKey: row.id, values: {barCode: this.maxBarCodeInfo, commodityUnit: unit?unit:''}}])
} }
}) })
} else { } else {
this.maxBarCodeInfo = this.maxBarCodeInfo + 1 this.maxBarCodeInfo = addBigNumbers(this.maxBarCodeInfo, 1)
target.setValues([{rowKey: row.id, values: {barCode: this.maxBarCodeInfo, commodityUnit: unit?unit:''}}]) target.setValues([{rowKey: row.id, values: {barCode: this.maxBarCodeInfo, commodityUnit: unit?unit:''}}])
} }
}, },