Browse Source

拆分订单脚本优化并发及未定义变量

wufeng 2 years ago
parent
commit
c77df69192
1 changed files with 12 additions and 1 deletions
  1. 12 1
      app/command/SplitSale.php

+ 12 - 1
app/command/SplitSale.php

@@ -6,6 +6,7 @@ use think\console\Command;
 use think\console\Input;
 use think\console\Output;
 use think\Exception;
+use think\facade\Cache;
 use think\facade\Db;
 
 class SplitSale extends Command
@@ -34,6 +35,14 @@ class SplitSale extends Command
 
         try {
 
+            $key = 'split_sale_';
+
+            $rs = Cache::store('redis')->get($key);
+
+            if ($rs) return true;
+
+            Cache::store('redis')->set($key, 1, 60 * 5);
+
             Db::startTrans();
 
             try {
@@ -184,6 +193,8 @@ class SplitSale extends Command
                 $output->writeln('事务回滚:' . $e->getMessage() . '||' . $e->getFile() . '||' . $e->getLine());
             }
 
+            Cache::store('redis')->set($key, 0);
+
         } catch (Exception $exception) {
             $output->writeln('脚本执行出错,' . $exception->getMessage() . '||' . $exception->getFile() . '||' . $exception->getLine());
         }
@@ -276,7 +287,7 @@ class SplitSale extends Command
     {
 
         $tmp_cgd = [
-            'good_price' => $cgd['good_num'] > 0 ? round(bcdiv($cgd_total, $cgd['good_num'], 5), 4) : 0,
+            'good_price' => isset($cgd['good_num']) ? ($cgd['good_num'] > 0 ? round(bcdiv($cgd_total, $cgd['good_num'], 5), 4) : 0) : 0,
             'total_fee' => $cgd_total,
             'pakage_fee' => $cgd['pakge_fee'] ?? 0,
             'open_fee' => $cgd['open_fee'] ?? 0,