ソースを参照

fixed: 商品添加/编辑出错的问题

兔姬桑 4 年 前
コミット
9e124738f0

+ 18 - 18
app/Console/Commands/fixDailyTrafficLogError.php

@@ -2,9 +2,7 @@
 
 namespace App\Console\Commands;
 
-use App\Models\SsNode;
 use App\Models\SsNodeTrafficDaily;
-use App\Models\User;
 use App\Models\UserTrafficDaily;
 use App\Models\UserTrafficLog;
 use Illuminate\Console\Command;
@@ -14,8 +12,12 @@ class fixDailyTrafficLogError extends Command {
 	protected $signature = 'fixDailyTrafficLogError';
 	protected $description = '修复原版本的每日流量计算错误';
 
+	private $end;
+
 	public function handle(): void {
-		$end = date('Y-m-d 23:59:59', strtotime("-1 days"));
+		// set value
+		$this->end = date('Y-m-d 23:59:59', strtotime("-1 days"));
+		$nodeArray = UserTrafficLog::query()->distinct()->pluck('node_id')->toArray();
 
 		Log::info('----------------------------【修复原版本的每日流量计算错误】开始----------------------------');
 		Log::info('----------------------------【节点流量日志修正】开始----------------------------');
@@ -25,11 +27,12 @@ class fixDailyTrafficLogError extends Command {
 			]);
 		}
 
-		foreach(SsNode::all() as $node){
+		Log::info('----------------------------【添加节点流量日志】开始----------------------------');
+		foreach($nodeArray as $nodeId){
 			$query = UserTrafficLog::query()
-			                       ->whereNodeId($node->id)
+			                       ->whereNodeId($nodeId)
 			                       ->whereBetween('log_time',
-				                       [strtotime(date('Y-m-d', strtotime("-1 days"))), strtotime($end)]);
+				                       [strtotime(date('Y-m-d', strtotime("-1 days"))), strtotime($this->end)]);
 
 			$u = $query->sum('u');
 			$d = $query->sum('d');
@@ -37,17 +40,17 @@ class fixDailyTrafficLogError extends Command {
 
 			if($total){ // 有数据才记录
 				$obj = new SsNodeTrafficDaily();
-				$obj->node_id = $node->id;
+				$obj->node_id = $nodeId;
 				$obj->u = $u;
 				$obj->d = $d;
 				$obj->total = $total;
 				$obj->traffic = flowAutoShow($total);
-				$obj->created_at = $end;
+				$obj->created_at = $this->end;
 				$obj->save();
 			}
 		}
+		Log::info('----------------------------【添加节点流量日志】结束----------------------------');
 		Log::info('----------------------------【节点流量日志修正】结束----------------------------');
-
 		Log::info('----------------------------【用户流量日志修正】开始----------------------------');
 		foreach(UserTrafficDaily::all() as $log){
 			UserTrafficDaily::query()->whereId($log->id)->update([
@@ -55,13 +58,12 @@ class fixDailyTrafficLogError extends Command {
 			]);
 		}
 		Log::info('----------------------------【用户个人流量日志修正】开始----------------------------');
-		foreach(User::query()->whereIn('id',UserTrafficLog::query()->distinct()->pluck('user_id')->toArray())->get() as $user){
+		foreach(UserTrafficLog::query()->distinct()->pluck('user_id')->toArray() as $userId){
 			// 统计一次所有节点的总和
-			$this->statisticsByUser($user->id);
-
+			$this->statisticsByUser($userId);
 			// 统计每个节点产生的流量
-			foreach(SsNode::query()->whereStatus(1)->orderBy('id')->get() as $node){
-				$this->statisticsByUser($user->id, $node->id);
+			foreach($nodeArray as $nodeId){
+				$this->statisticsByUser($userId, $nodeId);
 			}
 		}
 		Log::info('----------------------------【用户个人流量日志修正】结束----------------------------');
@@ -70,12 +72,10 @@ class fixDailyTrafficLogError extends Command {
 	}
 
 	private function statisticsByUser($user_id, $node_id = 0): void {
-		$end = date('Y-m-d 23:59:59', strtotime("-1 days"));
-
 		$query = UserTrafficLog::query()
 		                       ->whereUserId($user_id)
 		                       ->whereBetween('log_time',
-			                       [strtotime(date('Y-m-d', strtotime("-1 days"))), strtotime($end)]);
+			                       [strtotime(date('Y-m-d', strtotime("-1 days"))), strtotime($this->end)]);
 
 		if($node_id){
 			$query->whereNodeId($node_id);
@@ -93,7 +93,7 @@ class fixDailyTrafficLogError extends Command {
 			$obj->d = $d;
 			$obj->total = $total;
 			$obj->traffic = flowAutoShow($total);
-			$obj->created_at = $end;
+			$obj->created_at = $this->end;
 			$obj->save();
 		}
 	}

+ 2 - 2
app/Http/Controllers/Admin/ShopController.php

@@ -51,7 +51,7 @@ class ShopController extends Controller {
 				'traffic' => 'required|integer|min:1024|max:10240000|nullable',
 				'price'   => 'required|numeric|min:0',
 				'type'    => 'required',
-				'renew'   => 'require_unless:type,2|min:0',
+				'renew'   => 'required_unless:type,2|min:0',
 				'days'    => 'required|integer',
 			], [
 				'traffic.min' => '内含流量不能低于1MB',
@@ -120,7 +120,7 @@ class ShopController extends Controller {
 				'traffic' => 'required|integer|min:1024|max:10240000|nullable',
 				'price'   => 'required|numeric|min:0',
 				'type'    => 'required',
-				'renew'   => 'require_unless:type,2|min:0',
+				'renew'   => 'required_unless:type,2|min:0',
 				'days'    => 'required|integer',
 			], [
 				'traffic.min' => '内含流量不能低于1MB',