Meals.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace app\weixin\model;
  3. use app\weixin\service\PRedis;
  4. use think\Db;
  5. use think\Model;
  6. class Meals extends Model
  7. {
  8. protected $table = 'sg_meals';
  9. /**
  10. * 获取套餐列表
  11. * @return array|bool|\PDOStatement|string|\think\Collection
  12. * @throws \think\db\exception\DataNotFoundException
  13. * @throws \think\db\exception\ModelNotFoundException
  14. * @throws \think\exception\DbException
  15. */
  16. public static function getList($type){
  17. $cacheKey = "cache:meals:type_{$type}";
  18. $dataList = PRedis::get($cacheKey);
  19. if($dataList){
  20. return $dataList;
  21. }
  22. $where = ['status'=> 1];
  23. if($type){
  24. $where['type'] = $type;
  25. }
  26. $dataList = Meals::where($where)
  27. ->field('id,name,price,type,time')
  28. ->select()
  29. ->each(function($item){
  30. $item['price'] = round($item['price'], 2);
  31. });
  32. $dataList = $dataList? $dataList->toArray() : [];
  33. if($dataList){
  34. PRedis::set($cacheKey, $dataList, 3 * 24 * 3600);
  35. }
  36. return $dataList;
  37. }
  38. }