/**
* 生成唯一FBA货箱号
* @return
*/
private String buildUniqueFbaBoxNo() {
SimpleDateFormat format_template = new SimpleDateFormat("yyMMdd");
// 生成13位FBA货箱号,并检测其唯一性,随机数范围:1000~9999
String uniqueFbaBoxNo = "FBA" + format_template.format(new Date()) + (int) (Math.random() * 9000 + 1000);
// 检测该货箱号是否存在,不存在直接返回生成的uniqueFbaBoxNo
AbroadShelfBox abroadShelfBox = abroadShelfBoxService.getAbroadShelfBoxByBoxNo(uniqueFbaBoxNo);
if (abroadShelfBox == null) {
return uniqueFbaBoxNo;
} else {
return buildUniqueFbaBoxNo();
}
}
/**
* 调用服务生成自动增长的货箱号序列
*
* @param generalStatusService
* @return
*/
private String buildUniqueFbaBoxNo(GeneralStatusService generalStatusService) {
SimpleDateFormat format_template = new SimpleDateFormat("yyMMdd");
// FBA货箱号前缀
String box_no_prefix = "FBA" + format_template.format(new Date());
// 完整货箱号
String box_no_full = "";
Map<String, Object> generalStatuMap = generalStatusService.fetchByKey(box_no_prefix);
// 将Map集合转换成对象
GeneralStatus generalStatus = generalStatusService.mapToGeneralStatus(generalStatuMap);
Map<String, Object> updateGeneralStatusMap = new HashMap<String, Object>();
if (generalStatus == null) {
updateGeneralStatusMap.put("statusKey", box_no_prefix);
updateGeneralStatusMap.put("value", "0001");
// 根据Key查找不存在,则创建
Dumper.dump(generalStatusService.create(updateGeneralStatusMap));// 执行创建操作
box_no_full = box_no_prefix + "0001";
} else {
String generalstatus_value = generalStatus.getValue() == null ? "6666" : generalStatus.getValue();
Integer index_value = Integer.valueOf(generalstatus_value);
Integer new_index_value = index_value + 1;
String new_value = "";
// ------------------------------------------------------------------------------------------设置货箱最后四位序列号
if (index_value >= 1 && index_value < 10) {
box_no_full = box_no_prefix + "000" + index_value;
} else if (index_value >= 10 && index_value < 100) {
box_no_full = box_no_prefix + "00" + index_value;
} else if (index_value >= 100 && index_value < 1000) {
box_no_full = box_no_prefix + "0" + index_value;
} else if (index_value >= 1000 && index_value < 10000) {
box_no_full = box_no_prefix + index_value;
} else {
box_no_full = box_no_prefix + (int) (Math.random() * 9000 + 1000);
}
// ------------------------------------------------------------------------------------------更新最后四位序列索引
if (new_index_value >= 1 && new_index_value < 10) {
new_value = "000" + new_index_value;
} else if (new_index_value >= 10 && new_index_value < 100) {
new_value = "00" + new_index_value;
} else if (new_index_value >= 100 && new_index_value < 1000) {
new_value = "0" + new_index_value;
} else if (new_index_value >= 1000 && new_index_value < 10000) {
new_value = String.valueOf(new_index_value);
} else {
new_value = String.valueOf((int) (Math.random() * 9000 + 1000));
}
updateGeneralStatusMap.put("id", generalStatus.getId());
updateGeneralStatusMap.put("statusKey", box_no_prefix);// statusKey存储类似FBA+161010
updateGeneralStatusMap.put("value", new_value);// value值,再之前基础上+1
Dumper.dump(generalStatusService.update(updateGeneralStatusMap));// 执行更新操作
}
// 检测该货箱号是否存在,不存在直接返回生成的uniqueFbaBoxNo
AbroadShelfBox abroadShelfBox = abroadShelfBoxService.getAbroadShelfBoxByBoxNo(box_no_full);
if (abroadShelfBox == null) {
return box_no_full;
} else {
return buildUniqueFbaBoxNo(generalStatusService);
}
}