ExpressShippingMethodService.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\ExpressShippingMethodModel;
  4. use utils\RedisCache;
  5. /**
  6. * 配送费模板列表服务 by wes
  7. * Class ExpressShippingMethodService
  8. * @package app\common\service
  9. */
  10. class ExpressShippingMethodService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new ExpressShippingMethodModel();
  17. }
  18. /**
  19. * 静态化入口
  20. * @return static|null
  21. */
  22. public static function make()
  23. {
  24. if(!self::$instance){
  25. self::$instance = new static();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 获取模板列表
  31. * @param $templateId 模板ID
  32. * @param bool $cache 是否缓存
  33. * @return array|mixed
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function getListByTemplate($templateId, $cache=true)
  39. {
  40. if($templateId<=0){
  41. return false;
  42. }
  43. $cacheKey = "caches:shippingList:template_{$templateId}";
  44. $data = RedisCache::get($cacheKey);
  45. if($data && $cache){
  46. return $data;
  47. }
  48. $where = ['template_id'=> $templateId];
  49. $data = $this->model->where($where)->select();
  50. $data = $data? $data->toArray():[];
  51. if($data && $cache){
  52. RedisCache::set($cacheKey, $data, rand(10,20));
  53. }
  54. return $data;
  55. }
  56. }