生成流水号 YYYY+MM+DD+NUM

持久层为hibernate实现JPA规范

实体

import java.util.Date;
import javax.persistence.Entity;
import net.xqx.activemq.model.BaseEntity;

/**
 * 
* 流水号记录
* @author Wtao
* @date 2018年7月17日
*
 */
@Entity
public class TResultCode extends BaseEntity{
	private static final long serialVersionUID = -9059889841842551568L;
	/**
	* 流水号类型 
	*/
	private String type;
	/**
	* 年
	*/
	private String year;
	/**
	* 月
	*/
	private String month;
	/**
	* 日
	*/
	private String day;
	/**
	* 流水号
	*/
	private String sequence;
	/**
	* 操作时间
	*/
	private Date createDate;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getSequence() {
		return sequence;
	}
	public void setSequence(String sequence) {
		this.sequence = sequence;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public String getDay() {
		return day;
	}
	public void setDay(String day) {
		this.day = day;
	}
	
}

service

/**
* 流水号生成接口
*/
public interface ResultCodeService {
	/**
	 *  获取流水号
	* @param type 类型
	* @param digit NUM位数
	* @return 流水编号
	* @throws Exception
	 */
	String getNewSequence(String type, Integer digit) throws Exception;
}

ServiceImpl

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.xqx.service.ResultCodeService;
import net.xqx.dao.TResultCodeDao;
import net.xqx.models.TResultCode;

/**
 * 
 *  
 * <p>描述: 获取流水号操作</p>  
 *
 * @author Wtao  
 * @date 2018年11月13日
 */
@Service
public class ResultCodeServiceImpl implements ResultCodeService {
	@Autowired
	private TResultCodeDao resultCodeDao;
	
	/*
	 * (非 Javadoc)
	*  生成编码
	* @param type 类型
	* @param digit 位数
	* @return 流水号
	* @throws Exception
	 */
	@Override
	public String getNewSequence(String type, Integer digit) throws Exception {
		if (type == null || type.trim().equals("")) {
			return null;
		}
		return getNewSequence(type, null, digit);
	}
	
	/**
	 * 
	 * <p>生成流水号的具体操作</p>
	 *
	 * @param type 类型
	 * @param incremental 递增数(默认为1)
	 * @param digit NUM位数
	 * @return 流水号
	 * @throws Exception
	 */
	public String getNewSequence(String type, Integer incremental, Integer digit) throws Exception {
		try {
			if (type == null || type.trim().equals("")) {
				return null;
			}
			SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
			SimpleDateFormat format_m = new SimpleDateFormat("MM");
			SimpleDateFormat format_d = new SimpleDateFormat("dd");
			Date date = new Date();
			String year = format_y.format(date);
			String month = format_m.format(date);
			String day = format_d.format(date);
			TResultCode sequence = this.findByYearAndMonthAndDayAndType(year,month,day,type);
			if (sequence == null) {
				sequence = new TResultCode();
				sequence.setType(type);
				sequence.setYear(year);
				sequence.setMonth(month);
				sequence.setDay(day);
				sequence.setSequence("0");
				sequence.setCreateDate(new Date());
				sequence = resultCodeDao.save(sequence);
			}
			BigDecimal big = new BigDecimal(Long.valueOf(sequence.getSequence().trim()));
			BigDecimal result = big.add(new BigDecimal(incremental == null ? 1 : incremental));
			sequence.setSequence(Long.valueOf(result.longValue()).toString());
			resultCodeDao.saveAndFlush(sequence);
			return year + month + day + digit(Long.valueOf(result.longValue()), digit);
		} catch (Exception e) {
			throw e;
		}
	}
	
	private static String digit(Long value, Integer digit) {
		if (value == null) {
			return null;
		}
		if (digit == null) {
			return value.toString();
		}
		String pattern = "";
		for (int i = 0; i < digit; i++) {
			pattern += "0";
		}
		DecimalFormat format = new DecimalFormat(pattern);
		return format.format(value);
	}
	
	public TResultCode findByYearAndMonthAndDayAndType(String year,String month,String day, String type) {
		List<TResultCode> list = resultCodeDao.findByYearAndMonthAndDayAndType(year, month, day,  type);
		
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}

DAO

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import net.xqx.models.TResultCode;

@Repository
public interface TResultCodeDao extends JpaRepository<TResultCode, Long> {

	@Query("from TResultCode r where r.year = :year and r.month = :month and r.day = :day and r.type = :type ")
	List<TResultCode> findByYearAndMonthAndDayAndType(@Param("year")String year, @Param("month")String month, @Param("day")String day, @Param("type")String type);

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值