/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
* <p>
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
* <p>
* 1. This software is for development use only under a valid license
* from BladeX.
* <p>
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
* <p>
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
* <p>
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
* <p>
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
* <p>
* Author: Chill Zhuang (bladejava@qq.com)
*/
package com.pantech.materials.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pantech.base.service.BaseServiceImpl;
import com.pantech.materials.mapper.SysCodeConfMapper;
import com.pantech.materials.service.ISysCodeConfService;
import com.pantech.sys.pojo.entity.SysCodeConfEntity;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 单号配置 服务实现类
*
* @author rsz
* @since 2025-02-17
*/
@Service
public class SysCodeConfServiceImpl extends BaseServiceImpl<SysCodeConfMapper, SysCodeConfEntity> implements ISysCodeConfService {
@Transactional
@Override
public String getCodeByType(String bussType) {
SysCodeConfEntity sysCodeConfEntity = baseMapper.selectOne(Wrappers.lambdaQuery(SysCodeConfEntity.class).eq(SysCodeConfEntity::getBussType, bussType));
if (sysCodeConfEntity != null) {
String newCode = generateNewCode(sysCodeConfEntity);
baseMapper.updateById(sysCodeConfEntity);
return newCode;
}
return null;
}
@NotNull
private String generateNewCode(SysCodeConfEntity sysCodeConfEntity) {
String prefix = sysCodeConfEntity.getPrefix();
Long currentCount = sysCodeConfEntity.getCurrentCount();
// 生成日期部分,格式为 yyyyMMdd
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String currentDateStr = sdf.format(new Date());
// 获取数据库中记录的最新日期
String latestDateStr = extractDateFromCode(sysCodeConfEntity.getLatestCode());
// 如果日期不同,重置计数
if (!currentDateStr.equals(latestDateStr)) {
currentCount = 0L;
}
// 生成新的计数
long newCount = currentCount + 1;
// 格式化计数为 5 位,不足 5 位前面补 0
String countStr = String.format("%05d", newCount);
// 组合成完整的编码
String newCode = prefix + currentDateStr + countStr;
// 更新数据库中的当前计数和最新单号
sysCodeConfEntity.setCurrentCount(newCount);
sysCodeConfEntity.setLatestCode(newCode);
return newCode;
}
/**
* 从编码中提取日期部分
* @param code 编码
* @return 日期字符串(格式:yyyyMMdd),如果编码为空则返回空字符串
*/
private String extractDateFromCode(String code) {
if (code != null && code.length() >= 12) {
return code.substring(code.length() - 13, code.length() - 5);
}
return "";
}
}
java自动生成单号代码
最新推荐文章于 2025-06-13 15:06:56 发布