with(['user']) ->where('shop_supplier_id','=',$shop_supplier_id) ->order(['create_time' => 'desc']) ->paginate($limit); } /** * 发送优惠券 * @param int $send_type 1给所有会员 2给指定等级的用户 3给指定用户发送 * @param int $coupon_id * @param int $user_level * @param string $user_ids */ public function SendCoupon($data,$shop_supplier_id) { $send_type = $data['send_type']; $coupon_id = $data['coupon_id']; $user_level = $data['user_level']; $user_ids = $data['user_ids']; $data['shop_supplier_id'] = $shop_supplier_id; $user = new User(); $coupon = Coupon::detail($coupon_id); if (empty($coupon)) { throw new BaseException(['msg' => '未找到优惠券信息']); return false; } if ($send_type == 1) { $user_arr = $user->getUsers(); if (count($user_arr) == 0) { throw new BaseException(['msg' => '没有符合条件的会员']); return false; } $data = $this->setData($coupon, $user_arr); } elseif ($send_type == 2) { $user_arr = $user->getUsers(['grade_id' => $user_level]); if (count($user_arr) == 0) { throw new BaseException(['msg' => '没有符合条件的会员']); return false; } $data = $this->setData($coupon, $user_arr); } elseif ($send_type == 3) { if ($user_ids == '') { throw new BaseException(['msg' => '请选择用户']); return false; } $user_ids = explode(',', $user_ids); $user_arr = []; foreach ($user_ids as $val) { $user_arr[]['user_id'] = $val; } $data = $this->setData($coupon, $user_arr); } return $this->saveAll($data); } /** * 数组重组 * @param $coupon * @param $user_arr */ public function setData($coupon, $user_arr) { $data = []; foreach ($user_arr as $k => $val) { if ($coupon['expire_type'] == 10) { $start_time = time(); $end_time = $start_time + ($coupon['expire_day'] * 86400); } else { $start_time = $coupon['start_time']['value']; $end_time = $coupon['end_time']['value']; } $data[$k]['coupon_id'] = $coupon['coupon_id']; $data[$k]['name'] = $coupon['name']; $data[$k]['color'] = $coupon['color']['value']; $data[$k]['coupon_type'] = $coupon['coupon_type']['value']; $data[$k]['reduce_price'] = $coupon['reduce_price']; $data[$k]['discount'] = $coupon['discount']; $data[$k]['min_price'] = $coupon['min_price']; $data[$k]['expire_type'] = $coupon['expire_type']; $data[$k]['expire_day'] = $coupon['expire_day']; $data[$k]['start_time'] = $start_time; $data[$k]['end_time'] = $end_time; $data[$k]['apply_range'] = $coupon['apply_range']; $data[$k]['app_id'] = self::$app_id; $data[$k]['user_id'] = $val['user_id']; $data[$k]['shop_supplier_id'] = $coupon['shop_supplier_id']; $data[$k]['back_money'] = $coupon['back_money']; // 返还金额 by lyzflash // 兑换券相关 by lyzflash $data[$k]['product_price'] = $coupon['product_price']; // 兑换券价值金额 by lyzflash $data[$k]['total_use_num'] = $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0; $data[$k]['limit_use_num'] = $coupon['coupon_type']['value'] == 30 ? $coupon['total_use_num'] : 0; // 兑换券优惠项目 $data[$k]['coupon_project'] = $coupon['coupon_project']; // 使用规则 $data[$k]['coupon_rule'] = $coupon['coupon_rule']; // 每天核销上限 $data[$k]['verify_num'] = $coupon['verify_num']; // 优惠项目可用数量 $data[$k]['project_limit_num'] = $coupon['project_limit_num']; // 适用范围 $data[$k]['use_scope'] = $coupon['use_scope']; } return $data; } /** * 撤销卡作废优惠券 by lyzflash * @param $coupon_ids * @param $user_id */ public function cancelUserCardCoupon($coupons, $user, $order_id) { foreach ($coupons as $item) { $data[] = [ 'data' => ['is_use' => 1], 'where' => [ 'coupon_id' => $item['coupon_id'], 'user_id' => $user['user_id'], 'order_id' => $order_id, ], ]; } return $this->updateAll($data); } /** * 扣除兑换券使用次数 by lyzflash * @param $user_coupon_id * @param $user_id */ public static function setUseNum($user_coupon_id, $user_id, $num = 1) { // return (new self)->where('user_coupon_id', '=', $user_coupon_id)->where('user_id', '=', $user_id)->dec('limit_use_num', $num)->update(); $model = static::detail($user_coupon_id); $data['limit_use_num'] = $model['limit_use_num'] - $num; if ($data['limit_use_num'] <= 0) { $data['limit_use_num'] = 0; $data['is_use'] = 1; } return $model->save($data); } /** * 返还兑换券使用次数 by lyzflash * @param $user_coupon_id * @param $user_id */ public static function backUseNum($user_coupon_id, $user_id, $num = 1) { // return (new self)->where('user_coupon_id', '=', $user_coupon_id)->where('user_id', '=', $user_id)->inc('limit_use_num', $num)->update(); $model = static::detail($user_coupon_id); $data['limit_use_num'] = $model['limit_use_num'] + $num; if ($data['limit_use_num'] >= $model['total_use_num']) { $data['total_use_num'] = $model['total_use_num']; $data['is_use'] = 0; } return $model->save($data); } }