ShopGoodsSpecService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\ShopGoodsSpec;
  4. use utils\RedisCache;
  5. /**
  6. * 商品规格服务 by wes
  7. * Class ShopGoodsSpecService
  8. * @package app\common\service
  9. */
  10. class ShopGoodsSpecService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new ShopGoodsSpec();
  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 $goodsId 商品ID
  32. * @param string $field 返回字段
  33. * @param string $cache 是否缓存
  34. * @return array|mixed
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function getListByGoods($goodsId, $field='', $cache=false)
  40. {
  41. if($goodsId<=0){
  42. return false;
  43. }
  44. $cacheKey = "caches:goodsSpec:list_{$goodsId}".($field? '_'.md5($field):'');
  45. $data = RedisCache::get($cacheKey);
  46. if($data && $cache){
  47. return $data;
  48. }
  49. $where = ['goods_id'=> $goodsId];
  50. $field = $field? $field : 'spec_ids,spec_text,original_price,price,picture,stock';
  51. $data = $this->model->where($where)->field($field)->select();
  52. $data = $data? $data->toArray():[];
  53. if($data && $cache){
  54. RedisCache::set($cacheKey, $data, rand(3, 5));
  55. }
  56. return $data;
  57. }
  58. /**
  59. * 获取商品规格库存
  60. * @param $goodsSn 商品编号
  61. * @param $specIds
  62. * @return array|mixed
  63. */
  64. public function getStock($goodsSn, $specIds)
  65. {
  66. $cacheKey = "caches:goodsSpec:stock:{$goodsSn}_".md5($specIds);
  67. $data = RedisCache::get($cacheKey);
  68. if($data){
  69. return $data;
  70. }
  71. $data = $this->model->where(['goods_sn'=> $goodsSn,'spec_ids'=> $specIds])->value('stock');
  72. if($data){
  73. RedisCache::set($cacheKey, $data, rand(3,5));
  74. }
  75. return $data;
  76. }
  77. /**
  78. * 获取商品规格数据
  79. * @param $goodsId
  80. * @param $specIds
  81. * @param string $field
  82. * @param bool $cache
  83. * @return array|false|mixed
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function getDataByGoods($goodsId, $specIds, $field='', $cache=true)
  89. {
  90. if($goodsId<=0){
  91. return false;
  92. }
  93. $cacheKey = "caches:goodsSpec:list_{$goodsId}".($specIds? '_'.md5(json_encode($specIds, 256)):'');
  94. $data = RedisCache::get($cacheKey);
  95. if($data && $cache){
  96. return $data;
  97. }
  98. $where = ['goods_id'=> $goodsId];
  99. $field = $field? $field : 'goods_spec_id,spec_ids,spec_text,original_price,price,rebate_score,weight,picture,stock';
  100. $data = $this->model->where($where)->field($field)->find();
  101. $data = $data? $data->toArray():[];
  102. if($data && $cache){
  103. RedisCache::set($cacheKey, $data, rand(3, 5));
  104. }
  105. return $data;
  106. }
  107. }