菜谱项目

Menu.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Record;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. class Menu extends Model
  7. {
  8. protected $table = 'menu';
  9. public $timestamps = false;
  10. public static function formatStr($value = [])
  11. {
  12. foreach ($value as &$v) {
  13. $v = explode(',', $v);
  14. $v = ['name' => $v[0], 'weightOrNum' => $v[1]];
  15. }
  16. unset($v);
  17. return $value;
  18. }
  19. public static function formatListId($value = [])
  20. {
  21. foreach ($value as &$v) {
  22. $v = $v->menu_id;
  23. }
  24. unset($v);
  25. return $value;
  26. }
  27. public static function getReadNum($initArr=[]){
  28. $keyArr=array();
  29. $keyArr=DB::table('menu_read_num')->whereIn('menu_id',$initArr)->orderByRaw("FIELD(menu_id, " . implode(", ", $initArr) . ")")->pluck('menu_id');
  30. $keyArr=json_decode(json_encode($keyArr),true);
  31. if(count($keyArr)==0){
  32. $keyArr=array();
  33. $readArr=array();
  34. }else{
  35. $valueArr=DB::table('menu_read_num')->whereIn('menu_id',$keyArr)->orderByRaw("FIELD(menu_id, " . implode(", ", $keyArr) . ")")->pluck('num');
  36. $valueArr=json_decode(json_encode($valueArr),true);
  37. $readArr=array_combine($keyArr,$valueArr);
  38. }
  39. return $readArr;
  40. }
  41. public static function getCollectNum($initArr=[]){
  42. $discountKeyArr=DB::table('collect_recipes')->whereIn('menu_id',$initArr)->orderByRaw("FIELD(menu_id, " . implode(", ", $initArr) . ")")->pluck('menu_id');
  43. $discountKeyArr=json_decode(json_encode($discountKeyArr),true);
  44. if(count($discountKeyArr)==0){
  45. $discountKeyArr=array();
  46. $discountArr=array();
  47. }else{
  48. $discountValueArr=DB::table('collect_recipes')->whereIn('menu_id',$initArr)->orderByRaw("FIELD(menu_id, " . implode(", ", $discountKeyArr) . ")")->pluck('menu_id');
  49. $discountArr=array_combine($discountKeyArr,$discountValueArr);
  50. }
  51. return $discountArr;
  52. }
  53. public static function isCollectTicket($user_id,$menu_id){
  54. $collect = DB::table('collect_recipes')->where('user_id',$user_id)->where('menu_id',$menu_id)->first();
  55. if ($collect){
  56. $is_collect = 1;
  57. }else{
  58. $is_collect = 0;
  59. }
  60. return $is_collect;
  61. }
  62. public static function getUserId($token) {
  63. $user_id = DB::table('users')->where('token',$token)->pluck('id');
  64. $user_id = json_decode($user_id,true);
  65. return $user_id[0];
  66. }
  67. // public static function selectMenuList($tagId,$time,$status=1){
  68. // $menuListNum=config('view.menuListNum');
  69. // $menuIdList=DB::table('tag_menu')->select('menu_id')->where('id',$tagId)->get()->toArray();
  70. // if(empty($menuIdList)){
  71. // return false;
  72. // }
  73. // $menuIdList=explode(',',$menuIdList[0]->menu_id);
  74. // $menuIdList=array_slice($menuIdList,$time*$menuListNum,$menuListNum);
  75. // if(empty($menuIdList)){
  76. // return [];
  77. // }
  78. // DB::table('album')->select('id','menu_id','url')->whereIn('id',$menuIdList)->get()->toArray();
  79. // $menuDataList=DB::table('menu')->select('id','title','ingredients','url')->whereIn('id',$menuIdList)->get()->toArray();
  80. // foreach($menuDataList as $v){
  81. // $v->collections=10;
  82. // $v->views=10;
  83. // }
  84. // return $menuDataList;
  85. // }
  86. public static function selectCollectionMenu($menuIdList, $time, $status = 1)
  87. {
  88. $menuIdList = self::formatListId($menuIdList);
  89. $menuDataList = DB::table('menu')->leftJoin('record', 'menu.id', '=', 'record.menu_id')->select('id', 'title', 'ingredients', 'url', 'collections', 'views')->whereIn('id', $menuIdList)->get()->toArray();
  90. return $menuDataList;
  91. }
  92. public static function selectMenu($menuId, $status = 1)
  93. {
  94. $menu = DB::table('menu')->where(['id' => $menuId, 'status' => $status])->first();
  95. if (empty($menu)) {
  96. return false;
  97. }
  98. $menuStep = DB::table('menu_step')->select('step', 'url')->where(['menu_id' => $menuId, 'status' => $status])->get()->toArray();
  99. $menu->step = $menuStep;
  100. $menu->ingredients = explode(';', $menu->ingredients);
  101. $menu->burden = explode(';', $menu->burden);
  102. $menu->ingredients = self::formatStr($menu->ingredients);
  103. $menu->burden = self::formatStr($menu->burden);
  104. return $menu;
  105. }
  106. public static function selectRecommend($type = 0, $time = 0)
  107. {
  108. if ($type == 1) {
  109. $offset = 0;
  110. $menuListNum = 5;
  111. } else {
  112. $menuListNum = config('view.menuListNum');
  113. $offset = $time * $menuListNum;
  114. }
  115. $menuId = DB::select('select menu_id from recommend where type=' . $type . ' and status=1 limit ' . $offset . ',' . $menuListNum);
  116. foreach ($menuId as &$v) {
  117. $v = $v->menu_id;
  118. }
  119. unset($v);
  120. if (!empty($menuId)) {
  121. $menuDataList = DB::table('menu')->select('id', 'title', 'url')->whereIn('id', $menuId)->get()->toArray();
  122. }
  123. if (!isset($menuDataList)) {
  124. return [];
  125. }
  126. if ($type == 1) {return $menuDataList;}
  127. $record = Record::selectListRecord($menuId);
  128. foreach ($menuDataList as $v) {
  129. $init=abs(ceil(crc32($v->id)/1000000));
  130. if (isset($record['collection'][$v->id])) {
  131. $v->collections = $record['collection'][$v->id]+floor($init/50);
  132. } else {
  133. $v->collections = floor($init/50);
  134. }
  135. if (isset($record['view'][$v->id])) {
  136. $v->views = $record['view'][$v->id]+$init;
  137. } else {
  138. $v->views = $init;
  139. }
  140. }
  141. return $menuDataList;
  142. }
  143. }