编号的生成(日期+序列号)

Posted Rain_in_Summer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编号的生成(日期+序列号)相关的知识,希望对你有一定的参考价值。

经常会用到编号的生成,这边整理一下:

先看controller方法:

package com.simpleframework.bjno.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/serial")
public class SerialController {

    @Autowired
    private SerialService serialService;

    
    
    /**
     * 获取编号
     * 
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getSipSdNo")
    public InvokeResult getSipSdNo(
            
            ) {
        try {
            Map returnResult = new HashMap();
            String dateString=serialService.getDateString();
            int sdNo = serialService.getInfSipSdNo();
            String serialNum = dateString + String.format("%04d", sdNo);
            returnResult.put("serialNo", serialNum);
            serialService.save(String.valueOf(sdNo),"");
            
            return InvokeResult.success(returnResult);
        } catch (Exception e) {
            return InvokeResult.failure(e.getMessage());

        }

    }
}

再看service,sql语句写在service中:

package com.bjno.service.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.simpleframework.bjno.model.InfApplyNo;
import com.simpleframework.bjno.service.SerialService;
import com.simpleframework.core.repo.RepositoryFactory;
import com.simpleframework.core.repo.ServiceSupport;
import com.simpleframework.core.repo.jpa.spi.SingleEntityManager;


@Service
@Transactional
public class SerialServiceImpl extends ServiceSupport implements SerialService {

    private SingleEntityManager<InfApplyNo> internal = RepositoryFactory.get().build(InfApplyNo.class);

    @Override
    public void save(String applyNo,String sdNo) {
        InfApplyNo infApplyNo = new InfApplyNo();
        infApplyNo.setSdNo(StringUtils.isEmpty(sdNo)?"":sdNo);
        infApplyNo.setCreateDate(new Date());
        infApplyNo.setAppSdNo(StringUtils.isEmpty(applyNo)?"":applyNo);
        // infApplyNo.setId(null);
        internal.save(infApplyNo);
    }

    

    @Override
    public int getInfSipSdNo() {
        String sql = "select  max(to_number(APP_SD_NO))  " + "from  INF_APPLY_NO t "
                + "where  to_char(CREATE_DATE,‘YYYY-MM-DD‘)= to_char(sysdate,‘YYYY-MM-DD‘) " + "and APP_SD_NO is not null ";

        
        Map<String, Object> param = new HashMap<>();
        List<Object[]> list = nativeQuery().setSql(sql).list();
        int sdNo;
        if (list != null && list.size() > 0 && list.get(0) != null) {
            sdNo = Integer.parseInt(String.valueOf(list.get(0))) + 1;

        } else {
            sdNo = 1;
        }

        return sdNo;
    }
    
    

    @Override
    public String getYwNo(String deptQlId, String deptYwNum) {
        String sql = "select  max(DEPT_YW_REG_NO)  " + "from  DEPT_YW_INF t " + "where DEPT_QL_ID=:deptQlId and DEPT_YW_NUM=:deptYwNum  ";
        String serialNo = "";
        Map<String, Object> param = new HashMap<>();
        param.put("deptQlId", deptQlId);
        param.put("deptYwNum", deptYwNum);
        List<Object[]> list = nativeQuery().setSql(sql).setParam(param).list();
        if (list != null && list.size() > 0 && list.get(0) != null) {
            serialNo = String.valueOf(list.get(0));
        }
        return serialNo;
    }
    
    
    @Override
    public String getSipYwNo(String deptQlId, String deptYwNum) {
        String sql = "select  max(YW_NAME_DESC)  " + "from  DEPT_YW_INF t " + "where DEPT_QL_ID=:deptQlId and DEPT_YW_NUM=:deptYwNum  ";
        String serialNo = "";
        Map<String, Object> param = new HashMap<>();
        param.put("deptQlId", deptQlId);
        param.put("deptYwNum", deptYwNum);
        List<Object[]> list = nativeQuery().setSql(sql).setParam(param).list();
        if (list != null && list.size() > 0 && list.get(0) != null) {
            serialNo = String.valueOf(list.get(0));
        }
        return serialNo;
    }
    
    
    @Override
    public String getDateString() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
    
    
}

 

以上是关于编号的生成(日期+序列号)的主要内容,如果未能解决你的问题,请参考以下文章

如何使用redis生成唯一编号及原理

在一天内为实体生成唯一的序列号

vscode 用户代码片段 vue初始化模板 Snippet #新加入开头注释 自动生成文件名 开发日期时间等内容

怎么生成流水号

根据当前日期生成16062801格式编号

R 知识片段