| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace app\common\service;
- use app\common\model\ShopOrderGoods;
- use utils\RedisCache;
- /**
- * 订单商品服务 by wes
- * Class ShopOrderGoodsService
- * @package app\common\service
- */
- class ShopOrderGoodsService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new ShopOrderGoods();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 用户购买过该商品的数量统计
- * @param $uid 用户ID
- * @param int $goodsId 商品ID,0表示用户购买的所有商品数量统计
- * @return array|mixed
- */
- public function getCountByUserAndGoods($uid, $goodsSn='0')
- {
- $cacheKey = "caches:orders:goodsCount:{$uid}_{$goodsSn}";
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $where = ['og.uid'=>$uid];
- if($goodsSn){
- $where['g.goods_sn'] = $goodsSn;
- }
- $data = $this->model->alias('og')
- ->leftJoin('shop_order o', 'o.order_id = og.order_id')
- ->leftJoin('shop_goods g', 'og.goods_id = g.goods_id')
- ->where('o.status', 'in', [1, 2, 4])
- ->where($where)
- ->count('og.og_id');
- if($data){
- RedisCache::set($cacheKey, $data, rand(3,5));
- }
- return $data;
- }
- }
|