$data['couponId'],'amount'=>$data['amount'],'give_type'=>$data['giveType'],'give_channel'=>$data['giveChannel']]; Db::startTrans(); try{ $res = $m->create($new); $id = $res->id??0; $codeM = new CouponRedeemCodes(); if ($data['giveType'] == 1){ $codes = [['coupon_id'=>$data['couponId'],'coupon_gen_redeem_codes_id'=>$id,'redeem_code'=>$data['code']]]; $codeM->addBatch($codes); }elseif ($data['giveType'] == 2){ $remainAmount = $data['amount']; $times = ceil($remainAmount/$bathLength); for($i=0;$i<$times;$i++){ $codes = []; if ($remainAmount >= $bathLength){ //$redeemCodes = gen_rand8($bathLength); $redeemCodes = gen_rand_number8($bathLength); for($j=0;$j<$bathLength;$j++){ $codes[] = ['coupon_id'=>$data['couponId'],'coupon_gen_redeem_codes_id'=>$id,'redeem_code'=>$redeemCodes[$j]]; } $codeM->addBatch($codes); } if ($remainAmount > 0 && $remainAmount<$bathLength){ //$redeemCodes = gen_rand8($remainAmount); $redeemCodes = gen_rand_number8($bathLength); for($j=0;$j<$remainAmount;$j++){ $codes[] = ['coupon_id'=>$data['couponId'],'coupon_gen_redeem_codes_id'=>$id,'redeem_code'=>$redeemCodes[$j]]; } $codeM->addBatch($codes); } $remainAmount -= $bathLength; } }else{ return true; } Db::commit(); }catch (\Exception $e) { // 回滚事务 Db::rollback(); log_record(__METHOD__.$e->getMessage(),'error'); return false; } return true; } /** * 兑换详细列表 * @param array $params * @return mixed */ public function getAllList(array $params = []) { // 检索查询条件 $filter = $this->getQueryFilter($params); $query = $this->alias('cc'); //券状态 if (isset($params['open_status']) && $params['open_status'] >= 0){ if($params['open_status'] == 1){ $query = $query->where('cp.status',1)->whereRaw('cp.expire_type=10 or (cp.expire_type =20 and unix_timestamp(cp.expire_time) > unix_timestamp(NOW()))'); }else{ $query = $query->whereRaw('cp.status=0 or (cp.expire_type =20 and unix_timestamp(cp.expire_time) < unix_timestamp(NOW()))'); } } // 获取数据列表 $query = $query->where($filter) ->leftJoin('coupon cp', 'cc.coupon_id = cp.coupon_id') ->field('cc.id,cc.coupon_id,cc.amount,cc.give_type,cc.give_channel,cp.name,cp.coupon_type,cp.reduce_price,cp.min_price, cp.describe,cp.expire_type,cp.expire_day,cp.expire_time,cp.status,cp.audit_status, cp.discount_type,cp.max_discount_price,cp.overlay_discount,cp.discount'); return $query->order(['cc.id' => 'desc'])->paginate(15)->each(function($item){ $item->open_status = 0;//券状态 if ($item->status == 1){ if ($item->expire_type == 10){//到期类型(10领取后生效 20固定时间) $item->open_status = 1; }elseif ($item->expire_type == 20 && strtotime($item->expire_time) > time()){// $item->open_status = 1; }else{ $item->open_status = 0; } } $item->overlay_discount = $item->overlay_discount ? explode(',', $item->overlay_discount) : []; $item->overlay_discount_text = '无'; $od_res = []; if (!empty($item->overlay_discount)) { foreach ($item->overlay_discount as $od) { if (isset(Coupon::OVERLAY_DISCOUNT[$od])) { $od_res[] = Coupon::OVERLAY_DISCOUNT[$od]; } } $item->overlay_discount_text = implode(',', $od_res); } $item->discount = $item->discount / 10; }); } //查询 public function getQueryFilter($param){ $filter = []; if(isset($param['name']) && $param['name']){ $filter[] = ['cp.name','like','%'.$param['name'].'%']; } if(isset($param['coupon_id']) && $param['coupon_id']){ $filter[] = ['cc.coupon_id','=',$param['coupon_id']]; } if(isset($param['coupon_type']) && $param['coupon_type']>=0){ $filter[] = ['cp.coupon_type','=',$param['coupon_type']]; } if(isset($param['status']) && $param['status']>=0){ $filter[] = ['cp.status','=',$param['status']]; } if(isset($param['audit_status']) && $param['audit_status']>=0){ $filter[] = ['cp.audit_status','=',$param['audit_status']]; } return $filter; } /** * 检验优惠券数量是否将要超发 * @param $couponId * @param int $num * @return float * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function sunAmount($couponId,$num=0){ $couponM = new Coupon(); $coupon = $couponM->where('coupon_id',$couponId)->find(); if (!$coupon){ return true; } if ($coupon->limit_total_type == 20){//不限数量 return false; } $totalNum = $coupon->total_num??0; $m = new self(); $amount = intval($m->where('coupon_id',$couponId)->sum('amount')); return ($num + $amount ) > $totalNum; } }