菜谱项目

Menu.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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) . ")")->where('status',1)->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. $discountValueArr=json_decode(json_encode($discountValueArr),true);
  50. $discountArr=array_combine($discountKeyArr,$discountValueArr);
  51. }
  52. return $discountArr;
  53. }
  54. public static function isCollectTicket($user_id,$menu_id){
  55. $collect = DB::table('collect_recipes')->where('user_id',$user_id)->where('menu_id',$menu_id)->first();
  56. if ($collect){
  57. $is_collect = 1;
  58. }else{
  59. $is_collect = 0;
  60. }
  61. return $is_collect;
  62. }
  63. public static function getUserId($token) {
  64. $user_id = DB::table('users')->where('token',$token)->pluck('id');
  65. $user_id = json_decode($user_id,true);
  66. return $user_id[0];
  67. }
  68. // public static function selectMenuList($tagId,$time,$status=1){
  69. // $menuListNum=config('view.menuListNum');
  70. // $menuIdList=DB::table('tag_menu')->select('menu_id')->where('id',$tagId)->get()->toArray();
  71. // if(empty($menuIdList)){
  72. // return false;
  73. // }
  74. // $menuIdList=explode(',',$menuIdList[0]->menu_id);
  75. // $menuIdList=array_slice($menuIdList,$time*$menuListNum,$menuListNum);
  76. // if(empty($menuIdList)){
  77. // return [];
  78. // }
  79. // DB::table('album')->select('id','menu_id','url')->whereIn('id',$menuIdList)->get()->toArray();
  80. // $menuDataList=DB::table('menu')->select('id','title','ingredients','url')->whereIn('id',$menuIdList)->get()->toArray();
  81. // foreach($menuDataList as $v){
  82. // $v->collections=10;
  83. // $v->views=10;
  84. // }
  85. // return $menuDataList;
  86. // }
  87. public static function selectCollectionMenu($menuIdList, $time, $status = 1)
  88. {
  89. $menuIdList = self::formatListId($menuIdList);
  90. $menuDataList = DB::table('menu')->leftJoin('record', 'menu.id', '=', 'record.menu_id')->select('id', 'title', 'ingredients', 'url', 'collections', 'views')->whereIn('id', $menuIdList)->get()->toArray();
  91. return $menuDataList;
  92. }
  93. public static function selectMenu($menuId, $status = 1)
  94. {
  95. $menu = DB::table('menu')->where(['id' => $menuId, 'status' => $status])->first();
  96. if (empty($menu)) {
  97. return false;
  98. }
  99. $menuStep = DB::table('menu_step')->select('step', 'url')->where(['menu_id' => $menuId, 'status' => $status])->get()->toArray();
  100. $menu->step = $menuStep;
  101. $menu->ingredients = explode(';', $menu->ingredients);
  102. $menu->burden = explode(';', $menu->burden);
  103. $menu->ingredients = self::formatStr($menu->ingredients);
  104. $menu->burden = self::formatStr($menu->burden);
  105. return $menu;
  106. }
  107. public static function selectRecommend($type = 0, $time = 0)
  108. {
  109. if ($type == 1) {
  110. $offset = 0;
  111. $menuListNum = 5;
  112. } else {
  113. $menuListNum = config('view.menuListNum');
  114. $offset = $time * $menuListNum;
  115. }
  116. $menuId = DB::select('select menu_id from recommend where type=' . $type . ' and status=1 limit ' . $offset . ',' . $menuListNum);
  117. foreach ($menuId as &$v) {
  118. $v = $v->menu_id;
  119. }
  120. unset($v);
  121. if (!empty($menuId)) {
  122. $menuDataList = DB::table('menu')->select('id', 'title', 'url')->whereIn('id', $menuId)->get()->toArray();
  123. }
  124. if (!isset($menuDataList)) {
  125. return [];
  126. }
  127. if ($type == 1) {return $menuDataList;}
  128. $record = Record::selectListRecord($menuId);
  129. foreach ($menuDataList as $v) {
  130. $init=abs(ceil(crc32($v->id)/1000000));
  131. if (isset($record['collection'][$v->id])) {
  132. $v->collections = $record['collection'][$v->id]+floor($init/50);
  133. } else {
  134. $v->collections = floor($init/50);
  135. }
  136. if (isset($record['view'][$v->id])) {
  137. $v->views = $record['view'][$v->id]+$init;
  138. } else {
  139. $v->views = $init;
  140. }
  141. }
  142. return $menuDataList;
  143. }
  144. }