diff --git a/app/Endpoint/Api/Http/Resources/Dealer/DealerResource.php b/app/Endpoint/Api/Http/Resources/Dealer/DealerResource.php index ff422639..eab3f586 100644 --- a/app/Endpoint/Api/Http/Resources/Dealer/DealerResource.php +++ b/app/Endpoint/Api/Http/Resources/Dealer/DealerResource.php @@ -18,11 +18,11 @@ class DealerResource extends JsonResource 'lvl' => $this->lvl, 'lvl_name'=> $this->lvl_text, 'is_sale' => $this->is_sale, - 'sale_values'=> $this->sale_values, //团队销售业绩基数 'guanli_values'=> $this->calculate_total_amount, //预计管理津贴 - 'team_sales_value' => $this->team_sales_value, + 'team_sales_value' => $this->team_sales_value, // 团队业绩 'pay_info'=>$this->pay_info ?: null, 'can_withdraw'=> $this->canWithdraw(), + 'total_purchase_amount'=> $this->total_purchase_amount, // 总进货业绩 ]; } } diff --git a/app/Models/Dealer.php b/app/Models/Dealer.php index c1ddd06a..72c4e097 100644 --- a/app/Models/Dealer.php +++ b/app/Models/Dealer.php @@ -6,6 +6,7 @@ use App\Casts\JsonArray; use App\Enums\DealerLvl; use Dcat\Admin\Traits\HasDateTimeFormatter; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Cache; class Dealer extends Model { @@ -81,35 +82,29 @@ class Dealer extends Model return $this->hasMany(DealerUpgradeLog::class, 'user_id', 'user_id'); } + /** + * 属于此经销商的进货补贴 + */ + public function purchaseSubsidies() + { + return $this->hasMany(DealerPurchaseSubsidy::class, 'user_id', 'user_id'); + } + public function getLvlTextAttribute() { return $this->lvl->text(); } /** - * 统计经销商进货补贴实时的【基数业绩】 + * 获取已结算的进货业绩 * - * @return void + * @return string */ - public function getSaleValuesAttribute() + public function getTotalPurchaseAmountAttribute() { - // return $this->self_sales_value + $this->team_sales_value; - $tz = now(); - - if ($tz->day >= 20) { - // 结算当月20号开始的业绩 - $startAt = $tz->copy()->setDay(20)->startOfDay(); - } elseif ($tz->day >= 5) { - // 结算当月5号开始的的业绩 - $startAt = $tz->copy()->setDay(5)->startOfDay(); - } else { - // 结算上月20号开始的业绩 - $startAt = $tz->copy()->subMonthNoOverflow()->setDay(20)->startOfDay(); - } - - return DealerPurchaseLog::query()->where('order_completed_at', '>=', $startAt) - ->where('path', 'like', "{$this->userInfo->full_path}%") - ->sum('total_amount'); + return Cache::remember('dealer_total_purchase_amount:'.$this->user_id, 3600, function () { + return $this->purchaseSubsidies()->sum('total_purchase_amount'); + }); } /** @@ -273,7 +268,7 @@ class Dealer extends Model $remark = ''; - if ($lvl === DealerLvl::Gold && $this->getDirectGoldDealersCount() >=5) { + if ($lvl === DealerLvl::Gold && $this->getDirectGoldDealersCount() >= 5) { $lvl = DealerLvl::Special; $remark .= '有5个直推金牌';