优化数据

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,16 +169,17 @@ 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, function ($q) { ->when($date == 'today', function ($q) {
$q->whereDate('created_at', now()->format('Y-m-d')); $q->whereDate('created_at', '2020-11-11');
}) })
->select('thirdPartyGoodsId', DB::raw('count(*) as total')) ->select('thirdPartyGoodsId', DB::raw('count(*) as total'))
->groupBy('thirdPartyGoodsId') ->groupBy('thirdPartyGoodsId')