兔姬桑 4 жил өмнө
parent
commit
ae3a94d873

+ 7 - 7
app/Components/Helpers.php

@@ -193,12 +193,12 @@ class Helpers
      *
      * @param  string  $description  备注
      * @param  int  $couponId  优惠券ID
-     * @param  int  $goodsId  商品ID
-     * @param  int  $orderId  订单ID
+     * @param  int|null  $goodsId  商品ID
+     * @param  int|null  $orderId  订单ID
      *
      * @return bool
      */
-    public static function addCouponLog(string $description, int $couponId, $goodsId = 0, $orderId = 0): bool
+    public static function addCouponLog($description, $couponId, $goodsId = null, $orderId = null): bool
     {
         $log = new CouponLog();
         $log->coupon_id = $couponId;
@@ -213,7 +213,7 @@ class Helpers
      * 记录余额操作日志.
      *
      * @param  int  $userId  用户ID
-     * @param  int  $orderId  订单ID
+     * @param  int|null  $orderId  订单ID
      * @param  int  $before  记录前余额
      * @param  int  $after  记录后余额
      * @param  int  $amount  发生金额
@@ -221,7 +221,7 @@ class Helpers
      *
      * @return bool
      */
-    public static function addUserCreditLog(int $userId, int $orderId, int $before, int $after, int $amount, $description = ''): bool
+    public static function addUserCreditLog($userId, $orderId, $before, $after, $amount, $description = ''): bool
     {
         $log = new UserCreditLog();
         $log->user_id = $userId;
@@ -239,14 +239,14 @@ class Helpers
      * 记录流量变动日志.
      *
      * @param  int  $userId  用户ID
-     * @param  int  $orderId  订单ID
+     * @param  int|null  $orderId  订单ID
      * @param  int  $before  记录前的值
      * @param  int  $after  记录后的值
      * @param  string  $description  描述
      *
      * @return bool
      */
-    public static function addUserTrafficModifyLog(int $userId, int $orderId, int $before, int $after, $description = ''): bool
+    public static function addUserTrafficModifyLog($userId, $orderId, $before, $after, $description = ''): bool
     {
         $log = new UserDataModifyLog();
         $log->user_id = $userId;

+ 1 - 1
app/Console/Commands/AutoClearLog.php

@@ -54,7 +54,7 @@ class AutoClearLog extends Command
             UserHourlyDataFlow::where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 days')))->delete();
 
             // 清除用户各节点 / 节点总计的每天流量数据日志
-            UserDailyDataFlow::where('node_id', '<>', 0)
+            UserDailyDataFlow::where('node_id', '<>', null)
                 ->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('-1 month')))
                 ->orWhere('created_at', '<=', date('Y-m-d H:i:s', strtotime('-3 month')))
                 ->delete();

+ 1 - 1
app/Console/Commands/AutoStatisticsUserDailyTraffic.php

@@ -34,7 +34,7 @@ class AutoStatisticsUserDailyTraffic extends Command
         Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
     }
 
-    private function statisticsByUser($user_id, $node_id = 0): void
+    private function statisticsByUser($user_id, $node_id = null): void
     {
         $query = UserDataFlowLog::whereUserId($user_id)->whereBetween('log_time', [strtotime(date('Y-m-d')), time()]);
 

+ 1 - 1
app/Console/Commands/AutoStatisticsUserHourlyTraffic.php

@@ -34,7 +34,7 @@ class AutoStatisticsUserHourlyTraffic extends Command
         Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
     }
 
-    private function statisticsByNode($user_id, $node_id = 0): void
+    private function statisticsByNode($user_id, $node_id = null): void
     {
         $query = UserDataFlowLog::whereUserId($user_id)->whereBetween('log_time', [strtotime('-1 hour'), time()]);
 

+ 2 - 2
app/Console/Commands/DailyJob.php

@@ -63,7 +63,7 @@ class DailyJob extends Command
                 Invite::whereInviterId($user->id)->whereStatus(0)->update(['status' => 2]);
 
                 // 写入用户流量变动记录
-                Helpers::addUserTrafficModifyLog($user->id, 0, $user->transfer_enable, 0, '[定时任务]账号已过期(禁止登录,清空账户)');
+                Helpers::addUserTrafficModifyLog($user->id, null, $user->transfer_enable, 0, '[定时任务]账号已过期(禁止登录,清空账户)');
             } else {
                 $user->update([
                     'u' => 0,
@@ -78,7 +78,7 @@ class DailyJob extends Command
                 $this->addUserBanLog($user->id, 0, '【封禁代理,清空账户】-账号已过期');
 
                 // 写入用户流量变动记录
-                Helpers::addUserTrafficModifyLog($user->id, 0, $user->transfer_enable, 0, '[定时任务]账号已过期(封禁代理,清空账户)');
+                Helpers::addUserTrafficModifyLog($user->id, null, $user->transfer_enable, 0, '[定时任务]账号已过期(封禁代理,清空账户)');
             }
         }
     }

+ 1 - 1
app/Console/Commands/UserTrafficAbnormalAutoWarning.php

@@ -29,7 +29,7 @@ class UserTrafficAbnormalAutoWarning extends Command
     private function userTrafficAbnormalWarning(): void
     {
         // 1小时内流量异常用户(多往前取5分钟,防止数据统计任务执行时间过长导致没有数据)
-        $userTotalTrafficLogs = UserHourlyDataFlow::whereNodeId(0)
+        $userTotalTrafficLogs = UserHourlyDataFlow::whereNodeId(null)
             ->where('total', '>', MB * 50)
             ->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))
             ->groupBy('user_id')

+ 1 - 1
app/Http/Controllers/Admin/TicketController.php

@@ -27,7 +27,7 @@ class TicketController extends Controller
     {
         $email = $request->input('email');
 
-        $query = Ticket::whereIn('admin_id', [0, Auth::id()]);
+        $query = Ticket::whereAdminId(Auth::id())->orwhere('admin_id', null);
 
         if (isset($email)) {
             $query->whereHas('user', static function ($q) use ($email) {

+ 4 - 4
app/Http/Controllers/Admin/UserController.php

@@ -137,7 +137,7 @@ class UserController extends Controller
 
             if ($user) {
                 // 写入用户流量变动记录
-                Helpers::addUserTrafficModifyLog($user->id, 0, 0, $data['transfer_enable'], '后台手动添加用户');
+                Helpers::addUserTrafficModifyLog($user->id, null, 0, $data['transfer_enable'], '后台手动添加用户');
 
                 return Response::json(['status' => 'success', 'message' => '添加成功']);
             }
@@ -196,7 +196,7 @@ class UserController extends Controller
 
             // 写入用户流量变动记录
             if ($user->transfer_enable !== $data['transfer_enable']) {
-                Helpers::addUserTrafficModifyLog($id, 0, $user->transfer_enable, $data['transfer_enable'], '后台手动编辑用户');
+                Helpers::addUserTrafficModifyLog($id, null, $user->transfer_enable, $data['transfer_enable'], '后台手动编辑用户');
             }
 
             if ($user->update($data)) {
@@ -245,7 +245,7 @@ class UserController extends Controller
 
                 if ($uid) {
                     // 写入用户流量变动记录
-                    Helpers::addUserTrafficModifyLog($uid, 0, 0, 1024 * GB, '后台批量生成用户');
+                    Helpers::addUserTrafficModifyLog($uid, null, 0, 1024 * GB, '后台批量生成用户');
                 }
             }
 
@@ -303,7 +303,7 @@ class UserController extends Controller
 
         // 加减余额
         if ($user->updateCredit($amount)) {
-            Helpers::addUserCreditLog($userId, 0, $user->credit, $user->credit + $amount, $amount, '后台手动充值');  // 写入余额变动日志
+            Helpers::addUserCreditLog($userId, null, $user->credit, $user->credit + $amount, $amount, '后台手动充值');  // 写入余额变动日志
 
             return Response::json(['status' => 'success', 'message' => '充值成功']);
         }

+ 3 - 3
app/Http/Controllers/UserController.php

@@ -86,7 +86,7 @@ class UserController extends Controller
         }
 
         // 写入用户流量变动记录
-        Helpers::addUserTrafficModifyLog($user->id, 0, $user->transfer_enable, $user->transfer_enable + $traffic, '[签到]');
+        Helpers::addUserTrafficModifyLog($user->id, null, $user->transfer_enable, $user->transfer_enable + $traffic, '[签到]');
 
         // 多久后可以再签到
         $ttl = sysConfig('traffic_limit_time') ? sysConfig('traffic_limit_time') * Minute : Day;
@@ -239,7 +239,7 @@ class UserController extends Controller
         $user->updateCredit(-$renewCost);
 
         // 记录余额操作日志
-        Helpers::addUserCreditLog($user->id, 0, $user->credit, $user->credit - $renewCost, -1 * $renewCost, '用户自行重置流量');
+        Helpers::addUserCreditLog($user->id, null, $user->credit, $user->credit - $renewCost, -1 * $renewCost, '用户自行重置流量');
 
         return Response::json(['status' => 'success', 'message' => '重置成功']);
     }
@@ -600,7 +600,7 @@ class UserController extends Controller
             DB::beginTransaction();
             // 写入日志
             $user = Auth::getUser();
-            Helpers::addUserCreditLog($user->id, 0, $user->credit, $user->credit + $coupon->value, $coupon->value, '用户手动充值 - [充值券:'.$request->input('coupon_sn').']');
+            Helpers::addUserCreditLog($user->id, null, $user->credit, $user->credit + $coupon->value, $coupon->value, '用户手动充值 - [充值券:'.$request->input('coupon_sn').']');
 
             // 余额充值
             $user->updateCredit($coupon->value);

+ 1 - 1
app/Models/UserDailyDataFlow.php

@@ -26,6 +26,6 @@ class UserDailyDataFlow extends Model
     // 用户每天使用总流量
     public function scopeUserDaily($query, $uid)
     {
-        return $query->whereUserId($uid)->whereNodeId(0);
+        return $query->whereUserId($uid)->whereNodeId(null);
     }
 }

+ 2 - 2
app/Models/UserHourlyDataFlow.php

@@ -26,7 +26,7 @@ class UserHourlyDataFlow extends Model
     // 用户每时使用总流量
     public function scopeUserHourly($query, $uid)
     {
-        return $query->whereUserId($uid)->whereNodeId(0);
+        return $query->whereUserId($uid)->whereNodeId(null);
     }
 
     public function scopeUserRecentUsed($query, $uid)
@@ -37,7 +37,7 @@ class UserHourlyDataFlow extends Model
     // 1小时内流量异常用户
     public function trafficAbnormal(): array
     {
-        $userTotalTrafficList = self::whereNodeId(0)
+        $userTotalTrafficList = self::whereNodeId(null)
             ->where('total', '>', MB * 50)
             ->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))
             ->groupBy('user_id')

+ 35 - 1
database/migrations/2020_11_10_075555_improve_table.php

@@ -1,5 +1,12 @@
 <?php
 
+use App\Models\CouponLog;
+use App\Models\Ticket;
+use App\Models\TicketReply;
+use App\Models\UserCreditLog;
+use App\Models\UserDailyDataFlow;
+use App\Models\UserDataModifyLog;
+use App\Models\UserHourlyDataFlow;
 use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\Schema;
@@ -20,7 +27,7 @@ class ImproveTable extends Migration
             $table->unsignedInteger('order_id')->default(null)->nullable()->change();
             $table->foreign('coupon_id')->references('id')->on('coupon')->nullOnDelete();
             $table->foreign('goods_id')->references('id')->on('goods')->nullOnDelete();
-            $table->foreign('order_id')->references('id')->on('order')->nullOnDelete();
+            $table->foreign('order_id')->references('id')->on('order')->cascadeOnDelete();
         });
 
         Schema::table('email_filter', function (Blueprint $table) {
@@ -122,6 +129,7 @@ class ImproveTable extends Migration
         });
 
         Schema::table('ticket_reply', function (Blueprint $table) {
+            $table->unsignedInteger('user_id')->default(null)->nullable()->comment('用户ID')->change();
             $table->unsignedInteger('admin_id')->default(null)->nullable()->comment('管理员ID')->change();
             $table->foreign('user_id')->references('id')->on('user')->cascadeOnDelete();
             $table->foreign('admin_id')->references('id')->on('user')->nullOnDelete();
@@ -143,6 +151,7 @@ class ImproveTable extends Migration
         });
 
         Schema::table('user_daily_data_flow', function (Blueprint $table) {
+            $table->unsignedInteger('node_id')->default(null)->nullable()->comment('节点ID,null表示统计全部节点')->change();
             $table->foreign('user_id')->references('id')->on('user')->cascadeOnDelete();
             $table->foreign('node_id')->references('id')->on('ss_node')->cascadeOnDelete();
         });
@@ -154,6 +163,7 @@ class ImproveTable extends Migration
         });
 
         Schema::table('user_hourly_data_flow', function (Blueprint $table) {
+            $table->unsignedInteger('node_id')->default(null)->nullable()->comment('节点ID,null表示统计全部节点')->change();
             $table->foreign('user_id')->references('id')->on('user')->cascadeOnDelete();
             $table->foreign('node_id')->references('id')->on('ss_node')->cascadeOnDelete();
         });
@@ -179,6 +189,16 @@ class ImproveTable extends Migration
         Schema::table('verify', function (Blueprint $table) {
             $table->foreign('user_id')->references('id')->on('user')->cascadeOnDelete();
         });
+        CouponLog::whereCouponId(0)->update(['coupon_id' => null]);
+        CouponLog::whereGoodsId(0)->update(['goods_id' => null]);
+        CouponLog::whereOrderId(0)->update(['order_id' => null]);
+        Ticket::whereAdminId(0)->update(['admin_id' => null]);
+        TicketReply::whereUserId(0)->update(['user_id' => null]);
+        TicketReply::whereAdminId(0)->update(['admin_id' => null]);
+        UserCreditLog::whereOrderId(0)->update(['order_id' => null]);
+        UserHourlyDataFlow::whereNodeId(0)->update(['node_id' => null]);
+        UserDailyDataFlow::whereNodeId(0)->update(['node_id' => null]);
+        UserDataModifyLog::whereOrderId(0)->update(['order_id' => null]);
 
         Schema::enableForeignKeyConstraints();
     }
@@ -191,6 +211,17 @@ class ImproveTable extends Migration
     public function down()
     {
         Schema::disableForeignKeyConstraints();
+        CouponLog::whereCouponId(null)->update(['coupon_id' => 0]);
+        CouponLog::whereGoodsId(null)->update(['goods_id' => 0]);
+        CouponLog::whereOrderId(null)->update(['order_id' => 0]);
+        Ticket::whereAdminId(null)->update(['admin_id' => 0]);
+        TicketReply::whereUserId(null)->update(['user_id' => 0]);
+        TicketReply::whereAdminId(null)->update(['admin_id' => 0]);
+        UserCreditLog::whereOrderId(null)->update(['order_id' => 0]);
+        UserHourlyDataFlow::whereNodeId(null)->update(['node_id' => 0]);
+        UserDailyDataFlow::whereNodeId(null)->update(['node_id' => 0]);
+        UserDataModifyLog::whereOrderId(null)->update(['order_id' => 0]);
+
         Schema::table('coupon_log', function (Blueprint $table) {
             $table->dropForeign(['coupon_id']);
             $table->dropForeign(['goods_id']);
@@ -328,6 +359,7 @@ class ImproveTable extends Migration
         });
 
         Schema::table('ticket_reply', function (Blueprint $table) {
+            $table->unsignedInteger('user_id')->default(0)->nullable(false)->change();
             $table->dropForeign(['user_id']);
             $table->dropForeign(['admin_id']);
             $table->dropForeign(['ticket_id']);
@@ -365,6 +397,7 @@ class ImproveTable extends Migration
             $table->dropForeign(['user_id']);
             $table->dropForeign(['node_id']);
             $table->dropIndex('user_daily_data_flow_node_id_foreign');
+            $table->unsignedInteger('node_id')->default(0)->nullable(false)->change();
         });
 
         Schema::table('user_data_modify_log', function (Blueprint $table) {
@@ -382,6 +415,7 @@ class ImproveTable extends Migration
             $table->dropForeign(['user_id']);
             $table->dropForeign(['node_id']);
             $table->dropIndex('user_hourly_data_flow_node_id_foreign');
+            $table->unsignedInteger('node_id')->default(0)->nullable(false)->change();
         });
 
         Schema::table('user_login_log', function (Blueprint $table) {