php生成优惠券 带排重方法,欢迎大家拍砖

2016-11-15 10:51:00
hainuo
原创 1592
``` /** * 生成优惠券数组 * @param $num * @return array */ public function generatorGoodsQuan($num) { $array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); for ($i = 0; $i < $num; $i++) { $randChar = array_rand($array, 1); $quan[] = uniqid($array[$randChar]); } return $quan; } /** * 优惠券数组排重 * * @param $quan * @param $num * @return mixed */ private function checkUniQuan($quan, $num) { $quan = array_unique($quan); if (count($quan) < $num) { $quan = array_merge($quan, $this->generatorGoodsQuan($num - count($quan))); return $this->checkUniQuan($quan, $num); } else return $quan; } /** * 数据库排重 * @param $shopId * @param $goodsId * @param $quan * @param $num * @return array|mixed */ private function checkDatabaseQuan($shopId, $goodsId, $quan, $num) { $data=M("UserQuans")->where(array( 'shopId'=>$shopId, 'goodsid'=>$goodsId, 'quan'=>array('in',$quan) ))->getField('quan',true); if(empty($data)) return $quan; else{ $quan=array_diff($quan,$data); $quan = array_merge($quan, $this->generatorGoodsQuan($num - count($quan))); $quan=$this->checkUniQuan($quan,$num); return $this->checkDatabaseQuan($shopId, $goodsId, $quan, $num); } } /** * 集合方法 生成针对某个店铺,某个商品的一定数量的优惠券 * @param $shopId * @param $goodsId * @param $num * @return array|mixed */ public function getQuanSn($shopId, $goodsId, $num) { $quan = $this->generatorGoodsQuan($num); $quan = $this->checkUniQuan($quan, $num); return $this->checkDatabaseQuan($shopId, $goodsId, $quan, $num); } ``` 如上是最新写的一个优惠券生成方法,欢迎大家拍砖 交流意见