场景描述:
某客户需要100W条SN码用来做刮奖活动,
奖品有3类
A类1W个中奖名额
B类1K个中奖名额
C类10个中奖名额
系统采用THINKPHP 框架
现将和谐函数记录如下。
1、 生成SN码
function mknum($n,$len){
<span style="white-space:pre"> </span>$f ="%0".$len."d";
<span style="white-space:pre"> </span>//获得0串
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return sprintf($f,$n);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>//可能存在
<span style="white-space:pre"> </span>//生成$num 个sno,其中数字包含$lenno个, $lenchr 个字母,总长度是$pre+$len+$lenchr+1
<span style="white-space:pre"> </span>//14 整数 8 + 2 + 4
<span style="white-space:pre"> </span>//8 + 01-59 6
//$num 是多少条SN码
//$pre sn前奏
<span style="font-family: Arial, Helvetica, sans-serif;">function mksnno($pre,$num){</span>
/*
0.25139300 1138197510
*/
set_time_limit(0);
$M=M("sn");
$count = $M->count();
while($count<$num){
$count++;
list($ms,$s)=explode(' ',microtime());
$ms = intval(floatval($ms)*100000000);
$ms = $this->mknum($ms,8);
$s = intval($s);
$s = $this->mknum($s%200,2);
$rand = mt_rand(1000,9999);
$sno =<span style="font-family: Arial, Helvetica, sans-serif;">$pre</span><span style="font-family: Arial, Helvetica, sans-serif;">. $s . $rand . $ms;</span>
$data=array("sno"=>$sno);
$M->add($data);
}
}
//
<span style="white-space:pre"> </span>
//这里是生成奖品
//total:sn 总条数
//$n某类奖品的个数
//$jp 奖品标示
function makesnjp($total,$n,$jp){
<span style="white-space:pre"> </span>set_time_limit(0);
<span style="white-space:pre"> </span>$M=M("fbsn");
<span style="white-space:pre"> </span>$m = $M->where(array("jp"=>$jp))->count();<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>$d = $total/$n;;
<span style="white-space:pre"> </span>for($m;$m<$n;$m++){
<span style="white-space:pre"> </span>hander:
<span style="white-space:pre"> </span>$id = intval($m*$d + mt_rand(0,$d));
<span style="white-space:pre"> </span>$r = $M->where(array("id"=>$id,"jp"=>0))->save(array("jp"=>$jp));
<span style="white-space:pre"> </span>if(!$r){
<span style="white-space:pre"> </span>goto hander;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>$m = $M->where(array("jp"=>$jp))->count();<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
2、 数据库设计如下
id bigint //自增长id ,key
sno varchar 16 //sn序列号,做索引
mob varchar 14 //领奖者电话
stat tinyint 4//是否已经领奖
jp tinyint //奖品标示0 标示没有中奖1 标示中了1等奖
ctm //奖品操作时间