IdMaker

1
2
3
4
5
6
7
create table `id_maker`
(
`business` varchar(255) NOT NULL COMMENT '业务',
`next_id` decimal(64, 0) unsigned NOT NULL default 1 COMMENT '下一次id',
PRIMARY KEY (`business`) USING BTREE
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4 COMMENT ='idMaker';
1
2
3
4
5
6
insert into id_maker values ('default',1);
insert into id_maker values ('eat_food_00',1);
insert into id_maker values ('eat_food_01',101);
insert into id_maker values ('eat_food_02',201);
insert into id_maker values ('eat_food_03',301);
insert into id_maker values ('eat_food_04',401);
1
2
3
4
begin;
select next_id from id_maker where business = 'default';
update id_maker set next_id = next_id+100 where business = 'default';
commit;
1
2
3
4
begin;
select next_id from id_maker where business = 'default';
update id_maker set next_id = next_id+500 where eat_food_01 = 'default';
commit;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class IdMaker
{
public $idMaker = 'default';
public $maxId = '0';
public $nextId = '0';
public function __construct()
{
$this->idMaker = 'eat_food_'.rand_mt(0,4);
$this->updateIdMaker();
}

public function getNextId()
{
if($nextId>$maxId){
$this->updateIdMaker();
}
return $this->nextId++;
}

protected function updateIdMaker()
{
// 通过 idMaker 获取最新的
$this->nextId = $maxId;
$this->maxId = $nextId + 100;
}
}