No primaria secuencia de teclas de incremento, es necesario empezar de cero guardado en la base de datos todos los días

El título no sabe cómo describir, que así sea.

requisitos:

    

 solución:

        1, la nueva tabla

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;

    NOTA: El identificador del tipo: identifica el campo. Por ejemplo, ahora que el número de serie de la declaración, puede ser unificados como uno. La próxima vez que necesite comprar un número de serie que se puede establecer a 2, por lo que la demanda común múltiplo.

           Prefijo, sufijo: costura automáticamente antes y después de que el número de serie devuelto. Tengo aquí está vacía.

           granularidad tiempo: que es un diario o una vez al mes a partir de 0, y así sucesivamente. Por ejemplo, cuando 1, la primera operación del día siguiente volví al número 000000 para empezar de nuevo.

           Llenar dígitos: es decir, que desea devolver la longitud de la serie. Por ejemplo, cuando quiero número de serie de seis dígitos, a continuación, el valor es 6.

2. Crear una función en la base de datos:

(Procedimientos almacenados?)

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

 En el siguiente se empalmar el valor a ser devuelto, por ejemplo, he añadido un DATE_FORMAT (ahora (), v_fmt) en el medio, en el medio de los datos devueltos a luchar en el número actual de años (en este momento v_fmt = '% Y')

 3, java llamada del programa:

        Yo uso el método basado en la anotación:

           capa mapper:     

@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);

               capa de servicio:

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

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

        return map;
    }

      Entrada de parámetros, está por encima de tipo de identificación, número de serie que representa la hora actual de la aprobación. salida para volver a la serie. Se le ha llamado tbRegistInfoMapper.getShenpin (mapa); después de llamar map.get ( "output") devuelve el valor que desee.

4, antes de su uso, es necesario agregar una semilla en la tabla de datos, que está acoplado con un conjunto de datos, el futuro no tendrá el control de.

Nos arquitecto me enseñó una manera de vender ahora, ja, ja!

Publicado 39 artículos originales · ganado elogios 6 · Vistas a 30000 +

Supongo que te gusta

Origin blog.csdn.net/qq_40155654/article/details/95738743
Recomendado
Clasificación