其他分享
首页 > 其他分享> > 自定义前缀生成流水号

自定义前缀生成流水号

作者:互联网

   自定义前缀生成流水号 

   前缀、中间为yyyyMMdd日期、后面6位自增

   数据库截图

   

 

 逻辑代码service

@Service
public class NumerService {

    @Autowired
    private NumerDao numerDao;


         public String getNumber(String bef){
             String number=bef+DateUtils.getDateTime("yyyyMMdd");
             Map<String, Object> map = numerDao.getNumber();
             if(map==null){
                 number+="000001";
                 Map<String, Object> data = new HashMap<>();
                 data.put("ID",UUID.randomUUID().toString());
                 data.put("NUMBER",number);
                 data.put("VS","0");
                 Integer save = numerDao.save(data);
                 if(save>=1){
                     return number;
                 }
             }else{
                 String numberData=String.valueOf(map.get("NUMBER"));
                 String s1 ="";
                 String s2="";
                 String s3 ="";
                 if(numberData.indexOf(DateUtils.getDateTime("yyyyMMdd"))<0){
                     number+="000001";
                 }else{
                     //最后的6位自增数字
                     s1 = numberData.substring(numberData.length() - 6);
                     //日期
                     s2=DateUtils.getDateTime("yyyyMMdd");
                     
                     //自增+1
                     s3 = String.valueOf(Integer.parseInt(s1) + 1);
                     int length=s3.length();
                     for (int i=0;i<6-length;i++){
                           s3="0"+s3;
                        }
                        number=bef+s2+s3;
                 }
                 //循环10次,如果还失败,则停止重试!
                 for(int i=0;i<=10;i++){
                     map.put("NUMBER",number);
                     map.put("VS2",map.get("VS"));//用于版本控制
                     map.put("VS",Integer.parseInt(String.valueOf(map.get("VS")))+1);
                     Integer edit = numerDao.edit(map);
                     if(edit>=1){
                         return number;
                     }else{
                         s3 = String.valueOf(Integer.parseInt(s3) + 1);
                         int length=s3.length();
                         for (int j=0;j<6-length;j++){
                             s3="0"+s3;
                         }
                         number=bef+s2+s3;
                     }
                 }
             }

             return "";
         }


}

dao接口

@Mapper
public interface NumerDao {

    Map<String,Object> getNumber();

    Integer save(Map<String,Object> map);

    Integer edit(Map<String,Object> map);

}

xml文件sql

<select id="getNumber" resultType="java.util.Map">
      SELECT * FROM OA.EHR_NUMBER
   </select>
   <!--保存年假数据-->
   <insert id="save" parameterType="java.util.Map">
        INSERT INTO OA.EHR_NUMBER
           (
             "ID",
             "NUMBER",
             "VS"
           )
         VALUES(
            #{ID,jdbcType=VARCHAR},
            #{NUMBER,jdbcType=VARCHAR},
            #{VS,jdbcType=VARCHAR}
         )
</insert>

   <update id="edit" parameterType="java.util.Map">
       update OA.EHR_NUMBER
          set
            "NUMBER"=#{NUMBER},
            "VS"=#{VS}
        where "ID"=#{ID} and "VS"=#{VS2}
   </update>

标签:Map,String,自定义,NUMBER,number,流水号,VS,ID,前缀
来源: https://blog.51cto.com/u_15242378/2871509