123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace App\Models;
- use App\Models\Record;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- class Menu extends Model
- {
- protected $table = 'menu';
- public $timestamps = false;
- public static function formatStr($value = [])
- {
- foreach ($value as &$v) {
- $v = explode(',', $v);
- $v = ['name' => $v[0], 'weightOrNum' => $v[1]];
- }
- unset($v);
- return $value;
- }
- public static function formatListId($value = [])
- {
- foreach ($value as &$v) {
- $v = $v->menu_id;
- }
- unset($v);
- return $value;
- }
- public static function getReadNum($initArr=[]){
- $keyArr=array();
- $keyArr=DB::table('menu_read_num')->whereIn('menu_id',$initArr)->orderByRaw("FIELD(menu_id, " . implode(", ", $initArr) . ")")->pluck('menu_id');
- $keyArr=json_decode(json_encode($keyArr),true);
- if(count($keyArr)==0){
- $keyArr=array();
- $readArr=array();
- }else{
- $valueArr=DB::table('menu_read_num')->whereIn('menu_id',$keyArr)->orderByRaw("FIELD(menu_id, " . implode(", ", $keyArr) . ")")->pluck('num');
- $valueArr=json_decode(json_encode($valueArr),true);
- $readArr=array_combine($keyArr,$valueArr);
- }
- return $readArr;
- }
- public static function getCollectNum($initArr=[]){
- $discountKeyArr=DB::table('collect_recipes')->whereIn('menu_id',$initArr)->orderByRaw("FIELD(menu_id, " . implode(", ", $initArr) . ")")->where('status',1)->pluck('menu_id');
- $discountKeyArr=json_decode(json_encode($discountKeyArr),true);
- if(count($discountKeyArr)==0){
- $discountKeyArr=array();
- $discountArr=array();
- }else{
- $discountValueArr=DB::table('collect_recipes')->whereIn('menu_id',$initArr)->orderByRaw("FIELD(menu_id, " . implode(", ", $discountKeyArr) . ")")->pluck('menu_id');
- $discountValueArr=json_decode(json_encode($discountValueArr),true);
- $discountArr=array_combine($discountKeyArr,$discountValueArr);
- }
- return $discountArr;
- }
- public static function isCollectTicket($user_id,$menu_id){
- $collect = DB::table('collect_recipes')->where('user_id',$user_id)->where('menu_id',$menu_id)->first();
- if ($collect){
- $is_collect = 1;
- }else{
- $is_collect = 0;
- }
- return $is_collect;
- }
- public static function getUserId($token) {
- $user_id = DB::table('users')->where('token',$token)->pluck('id');
- $user_id = json_decode($user_id,true);
- return $user_id[0];
- }
- // public static function selectMenuList($tagId,$time,$status=1){
- // $menuListNum=config('view.menuListNum');
- // $menuIdList=DB::table('tag_menu')->select('menu_id')->where('id',$tagId)->get()->toArray();
- // if(empty($menuIdList)){
- // return false;
- // }
- // $menuIdList=explode(',',$menuIdList[0]->menu_id);
- // $menuIdList=array_slice($menuIdList,$time*$menuListNum,$menuListNum);
- // if(empty($menuIdList)){
- // return [];
- // }
- // DB::table('album')->select('id','menu_id','url')->whereIn('id',$menuIdList)->get()->toArray();
- // $menuDataList=DB::table('menu')->select('id','title','ingredients','url')->whereIn('id',$menuIdList)->get()->toArray();
- // foreach($menuDataList as $v){
- // $v->collections=10;
- // $v->views=10;
- // }
- // return $menuDataList;
- // }
- public static function selectCollectionMenu($menuIdList, $time, $status = 1)
- {
- $menuIdList = self::formatListId($menuIdList);
- $menuDataList = DB::table('menu')->leftJoin('record', 'menu.id', '=', 'record.menu_id')->select('id', 'title', 'ingredients', 'url', 'collections', 'views')->whereIn('id', $menuIdList)->get()->toArray();
- return $menuDataList;
- }
- public static function selectMenu($menuId, $status = 1)
- {
- $menu = DB::table('menu')->where(['id' => $menuId, 'status' => $status])->first();
- if (empty($menu)) {
- return false;
- }
- $menuStep = DB::table('menu_step')->select('step', 'url')->where(['menu_id' => $menuId, 'status' => $status])->get()->toArray();
- $menu->step = $menuStep;
- $menu->ingredients = explode(';', $menu->ingredients);
- $menu->burden = explode(';', $menu->burden);
- $menu->ingredients = self::formatStr($menu->ingredients);
- $menu->burden = self::formatStr($menu->burden);
- return $menu;
- }
- public static function selectRecommend($type = 0, $time = 0)
- {
- if ($type == 1) {
- $offset = 0;
- $menuListNum = 5;
- } else {
- $menuListNum = config('view.menuListNum');
- $offset = $time * $menuListNum;
- }
- $menuId = DB::select('select menu_id from recommend where type=' . $type . ' and status=1 limit ' . $offset . ',' . $menuListNum);
- foreach ($menuId as &$v) {
- $v = $v->menu_id;
- }
- unset($v);
- if (!empty($menuId)) {
- $menuDataList = DB::table('menu')->select('id', 'title', 'url')->whereIn('id', $menuId)->get()->toArray();
- }
- if (!isset($menuDataList)) {
- return [];
- }
- if ($type == 1) {return $menuDataList;}
- $record = Record::selectListRecord($menuId);
- foreach ($menuDataList as $v) {
- $init=abs(ceil(crc32($v->id)/1000000));
- if (isset($record['collection'][$v->id])) {
- $v->collections = $record['collection'][$v->id]+floor($init/50);
- } else {
- $v->collections = floor($init/50);
- }
- if (isset($record['view'][$v->id])) {
- $v->views = $record['view'][$v->id]+$init;
- } else {
- $v->views = $init;
- }
- }
- return $menuDataList;
- }
- }
|