diff --git a/app/Models/User.php b/app/Models/User.php index 517d5b9..6e0711e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -145,7 +145,7 @@ class User extends Authenticatable * @param string $date * @return int */ - public function getCouponCount($type, $date = ''): int + public function getCouponCount($type, $date = false) { $data = $this->checkCouponCount($date); @@ -169,22 +169,23 @@ class User extends Authenticatable */ public function checkCouponCount($date) { + $date = $date ? 'today' : 'all'; + $name = "user_{$this->id}_coupon_count_date_{$date}"; if (Cache::has($name)) { return Cache::get($name, []); } else { - $res = DB::table('coupons') - ->where('user_id', $this->id) - ->whereIn('status', [2]) - ->when($date, function ($q) { - $q->whereDate('created_at', now()->format('Y-m-d')); - }) - ->select('thirdPartyGoodsId', DB::raw('count(*) as total')) - ->groupBy('thirdPartyGoodsId') - ->get() - ->pluck('total', 'thirdPartyGoodsId') - ->toArray(); + $res = $this->coupons() + ->whereIn('status', [2]) + ->when($date == 'today', function ($q) { + $q->whereDate('created_at', '2020-11-11'); + }) + ->select('thirdPartyGoodsId', DB::raw('count(*) as total')) + ->groupBy('thirdPartyGoodsId') + ->get() + ->pluck('total', 'thirdPartyGoodsId') + ->toArray(); Cache::put($name, $res, 10);