java自动生成单号代码

/**
 * 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 "";
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值