Non-primary key increment sequence, need to start from zero saved to the database every day

The title does not know how to describe, so be it.

demand:

    

 solve:

        1, the new table

CREATE TABLE `man_busi_code` (
  `busi_type` int(11) NOT NULL COMMENT '类型id',
  `busi_desc` varchar(255) DEFAULT NULL COMMENT '描述',
  `prefix` varchar(255) DEFAULT NULL COMMENT '前缀',
  `suffix` varchar(255) DEFAULT NULL COMMENT '后缀',
  `granularity` int(11) NOT NULL DEFAULT '0' COMMENT '时间粒度 0 不按时间 1 日 2 月 3 季 4 年',
  `gen_time` date DEFAULT NULL COMMENT '时间',
  `gen_val` int(11) NOT NULL DEFAULT '1' COMMENT '序列',
  `extent` int(11) NOT NULL DEFAULT '1' COMMENT '填充位数'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

    NOTE: Type id: identifies the field. For example, now that the declaration serial number, can be unified as one. The next time you need to purchase a serial number that you can set to 2, so that multiple common demand.

           Prefix, suffix: automatically stitching before and after the serial number returned. I have here is empty.

           Time granularity: that is a daily or a monthly basis starting from 0, and so on. For example, when 1, the first operation the next day I returned to the number 000000 to start again.

           Filling digits: that is, you want to return the length of the number. For example, when I want six-digit serial number, then the value is 6.

2. Create a function in the database:

(Stored procedures?)

CREATE PROCEDURE `f_generator_busi_code`(IN i_busi_type int, OUT v_code varchar(50))
BEGIN

DECLARE v_cnt int DEFAULT 0;  
DECLARE v_granularity int;
DECLARE v_fmt varchar(50);
    set v_code='';
  START TRANSACTION; 
   select count(*)
      into v_cnt
      from man_busi_code
     where busi_type = i_busi_type;
     if v_cnt > 0 then
    select distinct granularity
      into v_granularity
      from man_busi_code
     where busi_type = i_busi_type;


   case v_granularity  
   when 0 then   
   
      select CONCAT( IFNULL(t.prefix,''), lpad(t.gen_val + 1, t.extent, '0'),IFNULL(t.suffix,'')) 
        into v_code
        from man_busi_code t
       where t.busi_type = i_busi_type ;

      update man_busi_code t
         set t.gen_val = t.gen_val + 1
       where t.busi_type = i_busi_type;
   when 1 then  
   
    SET v_fmt='%Y%m%d';
   when 2 then  
   
    SET v_fmt='%Y%m';
   when 3 then 
   
        select CONCAT( IFNULL(t.prefix,''), DATE_FORMAT(now(),'%Y'),quarter(now()),lpad(t.gen_val + 1, t.extent, '0'),IFNULL(t.suffix,'')) 
            into v_code
            from man_busi_code t
           where 
            CONCAT(DATE_FORMAT(t.gen_time,'%Y'),quarter(t.gen_time))=CONCAT(DATE_FORMAT(now(), '%Y'),quarter(now()))
            and t.busi_type = i_busi_type LIMIT 1;
        if LENGTH(v_code)>0 then 
          update man_busi_code t
            set t.gen_val = t.gen_val + 1
          where CONCAT(DATE_FORMAT(t.gen_time,'%Y'),quarter(t.gen_time))=CONCAT(DATE_FORMAT(now(), '%Y'),quarter(now()))
            and t.busi_type = i_busi_type;
        else
          select CONCAT( IFNULL(t.prefix,''), DATE_FORMAT(now(),'%Y'),quarter(now()),lpad(1, t.extent, '0'),IFNULL(t.suffix,'')) 
            into v_code
            from man_busi_code t
           where  t.busi_type = i_busi_type LIMIT 1;
           insert into man_busi_code(
            busi_type,   busi_desc,   prefix, suffix,   granularity,   gen_time, gen_val,  extent
            )  select t.busi_type ,t.busi_desc,t.prefix ,t.suffix,t.granularity,NOW(),1,t.extent from man_busi_code t 
            where t.busi_type=i_busi_type LIMIT 1;
        end if;
   when 4 then 
   
    SET v_fmt='%Y';
   end case;
       if v_granularity != 3 and v_granularity != 0 then
       
          select CONCAT( IFNULL(t.prefix,''), DATE_FORMAT(now(),v_fmt),lpad(t.gen_val + 1, t.extent, '0'),IFNULL(t.suffix,'')) 
            into v_code
            from man_busi_code t
           where 
            DATE_FORMAT(t.gen_time,v_fmt)=DATE_FORMAT(now(), v_fmt )
            and t.busi_type = i_busi_type LIMIT 1;
       if LENGTH(v_code)>0 then 
       
          update man_busi_code t
            set t.gen_val = t.gen_val + 1
            where DATE_FORMAT(t.gen_time,v_fmt)=DATE_FORMAT(now(), v_fmt ) and t.busi_type = i_busi_type;
       else
        
        select CONCAT( IFNULL(t.prefix,''), DATE_FORMAT(now(),v_fmt),lpad(1, t.extent, '0'),IFNULL(t.suffix,'')) 
            into v_code
            from man_busi_code t
           where t.busi_type = i_busi_type limit 1;

        insert into man_busi_code(
            busi_type,   busi_desc,   prefix, suffix,   granularity,   gen_time, gen_val,  extent
            )  select t.busi_type ,t.busi_desc,t.prefix ,t.suffix,t.granularity,NOW(),1,t.extent from man_busi_code t 
            where t.busi_type=i_busi_type LIMIT 1;
        end if;
   end if;
   end if;
   COMMIT;
    if LENGTH(v_code)=0 then 
        set v_code='没有找到代码';
   end if;
END

 In the following you can splice the value to be returned, for example, I added a DATE_FORMAT (now (), v_fmt) in the middle, in the middle of the returned data will fight on the current number of years (at this time v_fmt = '% Y')

 3, java program call:

        I use annotation-based method:

           mapper layer:     

@Select(value = "{call f_generator_busi_code( #{input, mode=IN, jdbcType=INTEGER}, #{output, mode=OUT, jdbcType=VARCHAR} )}")
    @Options(statementType = StatementType.CALLABLE)
    public void getShenpin(Map map);

               service layer:

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Map test(Map map){

        map.put("input", 1);
        map.put("output", "");
       tbRegistInfoMapper.getShenpin(map);

        return map;
    }

      Parameter input, is above type id, serial number representing the current time of approval. output to return to the number. It has been called tbRegistInfoMapper.getShenpin (map); after calling map.get ( "output") returns the value you want.

4, prior to use, you need to add a seed in the data table, which is coupled with a data, the future will not have control of.

We architect taught me a way to sell off now, ha ha!

Published 39 original articles · won praise 6 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_40155654/article/details/95738743