生成流水號 YYYY+MM+DD+NUM
阿新 • • 發佈:2018-12-06
持久層為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 = -9059889841842551568 L;
/**
* 流水號型別
*/
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);
}