Batch generation serial number scheme

 Because a large amount of data is involved, and certain stability is required to not make mistakes, mysql is used.

The so-called simple understanding of things is that either all the SQL contained in it are executed, or none of them are executed.

Here I use the tp5 framework, and the idea without a framework is the same.

The core is mainly transaction + batch input card number + verification card password uniqueness.

public function setCard($data,$id){

        Db::startTrans();//准备事物
        try{
            $card = model('CardType')->getOneData(' * ',['id'=>$data['cardtype']]);//获取卡分类
            $table = 'card_'.$card->mark;
            $card_id = Db::table($table)->field('max(id) id')->find();//获取最大卡号
            //准备数据
            $sql = "INSERT INTO `{$table}` (`id`,`password`,`product_id`) VALUES ";
            $i = $data['num'];
            $card_id = empty($card_id['id']) ? 10000000 : $card_id['id'];
            $str = '';
            $old_psw = array();
            for ($i;$i>0;$i--){
                if ($i != $data['num'])$str .= ',';
                ++$card_id;
                $t = true;
                while ($t){//处理可能重复的卡号
                    $psw = rand(10000001,99999999).rand(10000001,99999999);
                    if (!in_array($psw,$old_psw)){
                        if (!Db::table($table)->field('id')->where('password = '.$psw)->find()){
                            $t = false;
                            $old_psw[] = $psw;
                        }
                    }
                }
                $str .= '('.$card_id.','.$psw.','.$id.')';
            }
            $sql = $sql.$str;
            unset($old_psw);
            Db::query($sql);
            unset($sql);
            unset($str);
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
        return true;
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324473552&siteId=291194637