首页 > 代码库 > 编号的生成(日期+序列号)

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

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

先看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;
    }
    
    
}

 

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