菜谱项目

Record.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. class Record extends Model
  6. {
  7. protected $table = '';
  8. public $timestamps = false;
  9. public static function insertViews($menuId)
  10. {
  11. $re = DB::table('view_record')->insert([
  12. 'menu_id' => $menuId,
  13. ]);
  14. return $re;
  15. }
  16. public static function insertCollection($menuId)
  17. {
  18. $re = DB::table('collection_record')->insert([
  19. 'menu_id' => $menuId,
  20. ]);
  21. return $re;
  22. }
  23. public static function selectListRecord($menuIdList)
  24. {
  25. $menuIdList = implode(',', $menuIdList);
  26. $viewNum = DB::select('select count(*) as num,menu_id from view_record where menu_id in (' . $menuIdList . ') group by menu_id');
  27. $collectionNum = DB::select('select count(*) as num,menu_id from collection_record where menu_id in (' . $menuIdList . ') group by menu_id');
  28. $count = count($viewNum);
  29. $record = [];
  30. for ($i = 0; $i < $count; $i++) {
  31. if (isset($viewNum[$i])) {
  32. $record['view'][$viewNum[$i]->menu_id] = $viewNum[$i]->num;
  33. }
  34. if (isset($collectionNum[$i])) {
  35. $record['collection'][$collectionNum[$i]->menu_id] = $collectionNum[$i]->num;
  36. }
  37. }
  38. return $record;
  39. }
  40. public static function selectRecord($menuId)
  41. {
  42. $viewNum = DB::select('select count(*) as num from view_record where menu_id=' . $menuId);
  43. $collectionNum = DB::select('select count(*) as num from collection_record where menu_id=' . $menuId);
  44. $record['view'] = $viewNum[0]->num;
  45. $record['collection'] = $collectionNum[0]->num;
  46. return $record;
  47. }
  48. }