', $search['min_price']]; } if (isset($search['max_price']) && $search['max_price'] !== '') { $where[] = ['price', '<', $search['max_price']]; } if (isset($search['name']) && $search['name'] !== '') { $where[] = ['name', 'like', '%' . $search['name'] . '%']; } return $where; } /** * @description: 查询单条数据 * @param array $search * @return */ public static function findOne(array $search) { return static::model()::where(static::getWhere($search))->first(); } /** * @description: 查询所有数据 * @param array $search * @return \Illuminate\Database\Eloquent\Collection */ public static function findAll(array $search = []) { return static::model()::where(static::getWhere($search))->get(); } /** * @description: 分页查询 * @param array $search * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public static function paginate(array $search = []) { $limit = isset($search['limit']) ? $search['limit'] : 15; $paginator = static::model()::where(static::getWhere($search)) ->orderBy('id', 'desc') ->paginate($limit); return ['total' => $paginator->total(), 'data' => $paginator->items()]; } /** * @description: * @param {*} $params * @return {*} */ public static function submit($params = []) { $result = false; $msg['code'] = self::NOT; $msg['msg'] = ''; // 2. 判断是否是更新 if (!empty($params['id'])) { // 更新 $info = self::findOne(['id'=>$params['id']] ); if (!$info) { $msg['msg'] = '数据不存在!'; }else{ $result = $info->update($params); $id = $params['id']; } } else { // 创建 $result = $info = self::model()::create($params); $id = $result->id; } if($result){ $msg['code'] = self::YES; $msg['msg'] = '设置成功'; }else{ $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg']; } return $msg; } /** * @description: 获取随机的商品 * @param {*} $notInIds 排除的商品 * @param {*} $maxPrice 最高金额 * @param {*} $minPrice 最低金额 * @return {*} */ public static function getRandom($notInIds = [] ,$maxPrice = 0 ,$minPrice = 0) { // // 有最低消费 优先找大于最低消费的 // if($minPrice > 0){ // $info = static::model()::where(self::getWhere(['min_price' => $minPrice ,'status' => self::STATUS_YES]))->whereNotIn('id',$notInIds)->orderBy('price','asc')->first(); // return $info; // } // 先找适合自己余额的商品 $info = static::model()::where(self::getWhere(['max_price' => $maxPrice ,'status' => self::STATUS_YES]))->whereNotIn('id',$notInIds)->inRandomOrder()->first(); if(!$info){ // 随机商品 $info = static::model()::where(self::getWhere(['min_price' => $minPrice ,'status' => self::STATUS_YES]))->whereNotIn('id',$notInIds)->orderBy('price','asc')->first(); return $info; } return $info; } }