优化数据

This commit is contained in:
2021-07-06 12:03:50 +08:00
parent 15be3f7b3f
commit c9d760283e

View File

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