diff --git a/app/Customer.php b/app/Customer.php index bf54bd1..c6ba5dd 100644 --- a/app/Customer.php +++ b/app/Customer.php @@ -3,6 +3,7 @@ namespace App; use App\Models\Balance; +use App\Models\Orders; use App\Models\Patient; use App\Models\Recharge; use Illuminate\Foundation\Auth\User as Authenticatable; @@ -53,7 +54,7 @@ class Customer extends Authenticatable implements JWTSubject { use Notifiable; - CONST GUARD_NAME = "customer"; + const GUARD_NAME = "customer"; public function guardName() { @@ -130,4 +131,9 @@ class Customer extends Authenticatable implements JWTSubject return $this->hasMany(Recharge::class, "customer_id")->whereNotNull("paid_at"); } + public function orders() + { + return $this->hasMany(Orders::class, "customer_id"); + } + } diff --git a/app/Http/Controllers/Admin/StatisticsController.php b/app/Http/Controllers/Admin/StatisticsController.php index 7bdaae5..e75703e 100755 --- a/app/Http/Controllers/Admin/StatisticsController.php +++ b/app/Http/Controllers/Admin/StatisticsController.php @@ -248,12 +248,11 @@ class StatisticsController extends CommonController return $this->error($this->noProjects); } $project_id = request()->project_id ?? $projects->first()->id; - $before_date = $request->before_date ?: date("Y-m-d"); $before_datetime = strtotime($before_date . " 23:59:59"); DB::enableQueryLog(); - //todo:子订单的数量增加,用更省查询时间的方式获取项目相关数据 + $customers = (new Customer()) ->whereNull("deleted_at") ->with([ @@ -264,16 +263,18 @@ class StatisticsController extends CommonController $query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->orderBy("id", "desc"); } ]) - ->whereHas("balances", function ($query) use ($before_datetime, $project_id) { + ->whereHas("oneBalance", function ($query) use ($before_datetime) { + $query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->where("balance", ">", 0)->orderBy("id", "desc"); + }) + ->whereHas("orders", function ($query) use ($before_datetime, $project_id) { $query ->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}") - ->whereHas("order", function ($query) use ($project_id) { - $query->where("project_id", $project_id); - }); + ->where("project_id", $project_id); }) ->get(); - return view($this->bladePath . ".customer-balance", compact("customers", "before_datetime")); + $laravel_duration = microtime(true) - LARAVEL_START; + return view($this->bladePath . ".customer-balance", compact("customers", "before_datetime", "project_id", "laravel_duration")); } public function fixMonthLastDayCheckout(Request $request) diff --git a/resources/views/admin/statistics/customer-balance.blade.php b/resources/views/admin/statistics/customer-balance.blade.php index 4d66d8d..86a2ec1 100755 --- a/resources/views/admin/statistics/customer-balance.blade.php +++ b/resources/views/admin/statistics/customer-balance.blade.php @@ -20,7 +20,16 @@
+
+ +