ShopOrderGoodsService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\ShopOrderGoods;
  4. use utils\RedisCache;
  5. /**
  6. * 订单商品服务 by wes
  7. * Class ShopOrderGoodsService
  8. * @package app\common\service
  9. */
  10. class ShopOrderGoodsService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new ShopOrderGoods();
  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 $uid 用户ID
  32. * @param int $goodsId 商品ID,0表示用户购买的所有商品数量统计
  33. * @return array|mixed
  34. */
  35. public function getCountByUserAndGoods($uid, $goodsSn='0')
  36. {
  37. $cacheKey = "caches:orders:goodsCount:{$uid}_{$goodsSn}";
  38. $data = RedisCache::get($cacheKey);
  39. if($data){
  40. return $data;
  41. }
  42. $where = ['og.uid'=>$uid];
  43. if($goodsSn){
  44. $where['g.goods_sn'] = $goodsSn;
  45. }
  46. $data = $this->model->alias('og')
  47. ->leftJoin('shop_order o', 'o.order_id = og.order_id')
  48. ->leftJoin('shop_goods g', 'og.goods_id = g.goods_id')
  49. ->where('o.status', 'in', [1, 2, 4])
  50. ->where($where)
  51. ->count('og.og_id');
  52. if($data){
  53. RedisCache::set($cacheKey, $data, rand(3,5));
  54. }
  55. return $data;
  56. }
  57. }