php优惠券生成-去重

记录一次优惠券生成-去重

方法一

/**
* 生成批量礼品消费券
*/
public function giftCardAddOp() {
  //接收get值
  $num = $_GET['gift_num'];
  $branch_prefix = $_GET['city_name'];
  if ($_GET) {
  $this->add_gift_card($branch_prefix, $num);
  }

showMessage('礼品码生成成功!','index.php?act=order&op=giftCardCode');

}
/**
* 处理插入
*$branch_prefix
*$num
*$sum
*
*/
private function add_gift_card($branch_prefix, $num ,$sum='0'){

  // 计数
  $n = 0;
  $promotion_codes = array();//这个数组用来接收生成的优惠码
  for ($i=0; $i < (($sum >0) ? $sum : $num); $i++) {
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    $uuid = substr($charid, 8, 8);

    $company = '爱之依';
    $promotion_codes[$i] = $uuid;

  
    $data.= "('".$branch_prefix."','".$uuid."','".time()."','".$company."'),";

    //插入一次 +1
    $n++;

    //防止死循环
    $num_con = $num+1000;
    if ($n == $num_con) {
      break;
    }

  }
  //去除右侧逗号
  $data = rtrim($data,',');
  //批量插入
  $model_gift_card = Model('goods_gift_card');
  $gift_code_info = $model_gift_card->addGiftAll($data);

  //如果插入成功true
  if ($gift_code_info) {
  //查询插入数量
  $condition = array('branch_prefix' => $branch_prefix);
  $gift_count = $model_gift_card->getGiftCodeCount($condition);

  //插入失败的再次循环插入
  if ($gift_count < $num) {
    $this->add_gift_card($branch_prefix, $num, $num - $gift_count);
  }
  }

}

 

 

方法2:

$array = [];
$num = 10;
$lenght = 8;
$start = 99999999;
$step = 60000;


$arrDate = explode('-', date('Y-m-d'));
$m = base_convert($arrDate[1],10,32); //进制转换
$d = base_convert($arrDate[2],10,32);
for ($i=$start;$i<=($start+$step*$num);$i+=$step){
$array[] = $uuid = strtoupper($m.$d.str_pad(base_convert($i,10,32),$lenght-2,'0',STR_PAD_LEFT)); //取进制数,不足补0


}
var_dump($array);die;


$update['gift_card_code'] = $uuid;
$update['add_time'] = time();
$update['use_time'] = 0;
$update['buyer_id'] = 0;
$update['is_delete'] = 0;
$update['order_id'] = 0;

$model_gift_card = M('goods_gift_card');
$gift_code_info = $model_gift_card->add($update);
if(strlen(array_pop($array))>9) return false;
return $array;

posted @ 2018-08-25 13:34  进击的巨人X  阅读(2014)  评论(0编辑  收藏  举报