菜谱项目

Menu.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 selectMenuList($tagId,$time,$status=1){
  28. // $menuListNum=config('view.menuListNum');
  29. // $menuIdList=DB::table('tag_menu')->select('menu_id')->where('id',$tagId)->get()->toArray();
  30. // if(empty($menuIdList)){
  31. // return false;
  32. // }
  33. // $menuIdList=explode(',',$menuIdList[0]->menu_id);
  34. // $menuIdList=array_slice($menuIdList,$time*$menuListNum,$menuListNum);
  35. // if(empty($menuIdList)){
  36. // return [];
  37. // }
  38. // DB::table('album')->select('id','menu_id','url')->whereIn('id',$menuIdList)->get()->toArray();
  39. // $menuDataList=DB::table('menu')->select('id','title','ingredients','url')->whereIn('id',$menuIdList)->get()->toArray();
  40. // foreach($menuDataList as $v){
  41. // $v->collections=10;
  42. // $v->views=10;
  43. // }
  44. // return $menuDataList;
  45. // }
  46. public static function selectCollectionMenu($menuIdList, $time, $status = 1)
  47. {
  48. $menuIdList = self::formatListId($menuIdList);
  49. $menuDataList = DB::table('menu')->leftJoin('record', 'menu.id', '=', 'record.menu_id')->select('id', 'title', 'ingredients', 'url', 'collections', 'views')->whereIn('id', $menuIdList)->get()->toArray();
  50. return $menuDataList;
  51. }
  52. public static function selectMenu($menuId, $status = 1)
  53. {
  54. $menu = DB::table('menu')->where(['id' => $menuId, 'status' => $status])->first();
  55. if (empty($menu)) {
  56. return false;
  57. }
  58. $menuStep = DB::table('menu_step')->select('step', 'url')->where(['menu_id' => $menuId, 'status' => $status])->get()->toArray();
  59. $menu->step = $menuStep;
  60. $menu->ingredients = explode(';', $menu->ingredients);
  61. $menu->burden = explode(';', $menu->burden);
  62. $menu->ingredients = self::formatStr($menu->ingredients);
  63. $menu->burden = self::formatStr($menu->burden);
  64. return $menu;
  65. }
  66. public static function selectRecommend($type = 0, $time = 0)
  67. {
  68. if ($type == 1) {
  69. $offset = 0;
  70. $menuListNum = 5;
  71. } else {
  72. $menuListNum = config('view.menuListNum');
  73. $offset = $time * $menuListNum;
  74. }
  75. $menuId = DB::select('select menu_id from recommend where type=' . $type . ' and status=1 limit ' . $offset . ',' . $menuListNum);
  76. foreach ($menuId as &$v) {
  77. $v = $v->menu_id;
  78. }
  79. unset($v);
  80. if (!empty($menuId)) {
  81. $menuDataList = DB::table('menu')->select('id', 'title', 'url')->whereIn('id', $menuId)->get()->toArray();
  82. }
  83. if (!isset($menuDataList)) {
  84. return [];
  85. }
  86. if ($type == 1) {return $menuDataList;}
  87. $record = Record::selectListRecord($menuId);
  88. foreach ($menuDataList as $v) {
  89. $init=abs(ceil(crc32($v->id)/1000000));
  90. if (isset($record['collection'][$v->id])) {
  91. $v->collections = $record['collection'][$v->id]+floor($init/50);
  92. } else {
  93. $v->collections = floor($init/50);
  94. }
  95. if (isset($record['view'][$v->id])) {
  96. $v->views = $record['view'][$v->id]+$init;
  97. } else {
  98. $v->views = $init;
  99. }
  100. }
  101. return $menuDataList;
  102. }
  103. }