total(array('product_id' => $product_id, 'type' => 1)); $total = 0; if ($code > 0 && $code > $total) { $num = $code - $total; for ($i = 0; $i < $num; $i++) { $this->createCode($product_id); } } } private function createCode($product_id) { $code = Dever::rand(8, 0); $data['product_id'] = $product_id; $data['code'] = $code; $total = Dever::db('code/info')->total($data); if ($total > 0) { return $this->createCode($product_id, $id); } $data['type'] = 1; Dever::db('code/info')->insert($data); return $code; } }