优化数据

This commit is contained in:
2021-07-05 17:11:02 +08:00
parent 74732188c9
commit 06ca4a9ec3
2 changed files with 61 additions and 14 deletions

View File

@@ -7,8 +7,10 @@ use App\Models\Traits\HasArea;
use DateTimeInterface;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use RuLong\Identity\Traits\UserHasIdentity;
use Illuminate\Support\Facades\Cache;
class User extends Authenticatable
{
@@ -128,22 +130,67 @@ class User extends Authenticatable
/**
* 关联平安卡券核销
* @author 玄尘 2020-04-03
* @return [type] [description]
* @return \Illuminate\Database\Eloquent\Relations\HasMany [type] [description]
*/
public function coupons()
{
return $this->hasMany(Coupon::class);
}
/**
* Notes: description
* @Author: 玄尘
* @Date : 2021/7/5 17:00
* @param $type
* @param string $date
* @return int
*/
public function getCouponCount($type, $date = '')
{
return $this->coupons()
->whereIn('status', [2])
->where('thirdPartyGoodsId', $type)
->when($date, function ($q) {
$q->whereDate('created_at', now()->format('Y-m-d'));
})
->count();
$data = $this->checkCouponCount($date);
return $data[$type] ?? 0;
// return $this->coupons()
// ->whereIn('status', [2])
// ->where('thirdPartyGoodsId', $type)
// ->when($date, function ($q) {
// $q->whereDate('created_at', now()->format('Y-m-d'));
// })
// ->count();
}
/**
* Notes: 一次性设置用户数据
* @Author: 玄尘
* @Date : 2021/7/5 17:01
* @param $date
* @return array|mixed
*/
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();
Cache::put($name, $res, 10);
return $res;
}
}
}

12
composer.lock generated
View File

@@ -5747,16 +5747,16 @@
},
{
"name": "xuanchen/coupon",
"version": "1.0.7",
"version": "1.0.9",
"source": {
"type": "git",
"url": "https://github.com/xuanchen120/coupon.git",
"reference": "c32352c12548ca94d824141e65d6bb6932005168"
"reference": "f67679162f55af80d8343fe1cacdc2eed3ad96e8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/xuanchen120/coupon/zipball/c32352c12548ca94d824141e65d6bb6932005168",
"reference": "c32352c12548ca94d824141e65d6bb6932005168",
"url": "https://api.github.com/repos/xuanchen120/coupon/zipball/f67679162f55af80d8343fe1cacdc2eed3ad96e8",
"reference": "f67679162f55af80d8343fe1cacdc2eed3ad96e8",
"shasum": "",
"mirrors": [
{
@@ -5796,9 +5796,9 @@
"homepage": "https://github.com/xuanchen120/coupon.git",
"support": {
"issues": "https://github.com/xuanchen120/coupon/issues",
"source": "https://github.com/xuanchen120/coupon/tree/1.0.7"
"source": "https://github.com/xuanchen120/coupon/tree/1.0.9"
},
"time": "2021-03-23T05:06:56+00:00"
"time": "2021-07-01T02:02:51+00:00"
},
{
"name": "xuanchen/unionpay",