Просмотр исходного кода

客户管理-操作费计费时对于历史账单的处理加锁与缓存

Zhouzhendong 5 лет назад
Родитель
Сommit
d41fb28790
2 измененных файлов с 25 добавлено и 19 удалено
  1. 0 10
      app/Http/Controllers/TestController.php
  2. 25 9
      app/Services/OwnerPriceOperationService.php

+ 0 - 10
app/Http/Controllers/TestController.php

@@ -156,16 +156,6 @@ class TestController extends Controller
         dd(Region::query()->where("id",">=",404)->where("id","<=",432)->delete());
     }
     public function tt1(){
-        $a = 88;
-        $id = md5(date("m-d"));
-        dump($id);
-        $id = substr_replace($id,$a,5,0);
-        dump($id);
-        $len = strlen($id);
-        dump(substr($id,5,$len-32));
-        $id = substr($id,0,5).substr($id,5+$len-32);
-        dump($id);
-        dd($id == md5(date("m-d")));
     }
     public function zzd(){
         ini_set('max_execution_time',2500);

+ 25 - 9
app/Services/OwnerPriceOperationService.php

@@ -142,11 +142,31 @@ class OwnerPriceOperationService
             }
             //满减存在
             if ($isDiscount){
-                //未被标记过处理时间或处理时间不为本月,或上次处理值过期,处理历史即时账单
-                $pivot = DB::selectOne(DB::raw("SELECT * FROM owner_price_operation_owner WHERE owner_price_operation_id = ? AND owner_id = ?"),[$rule->id,$owner_id]);
-                if ($pivot && (!$pivot->discount_date || substr($pivot->discount_date,0,7)!=date("Y-m") || $pivot->target_value < $targetValue)){
-                    $this->handlePastBill($targetValue,$rule,$owner_id,$units,$discountIndex,$pivot);
-                }
+                $sign = false;
+                $key = "pivot_".$rule->id."_".$owner_id;
+                $pivot = app(CacheService::class)->getOrExecute($key,function ()use($key,$targetValue,&$sign,$rule,$owner_id){
+                    try{
+                        DB::beginTransaction();
+                        $pivot = DB::selectOne(DB::raw("SELECT * FROM owner_price_operation_owner WHERE owner_price_operation_id = ? AND owner_id = ? for update"),[$rule->id,$owner_id]);
+                        if ($pivot && (!$pivot->discount_date || substr($pivot->discount_date,0,7)!=date("Y-m") || $pivot->target_value < $targetValue)){
+                            //未被标记过处理时间或处理时间不为本月,或上次处理值过期,处理历史即时账单
+                            $sign = true;
+                        }
+                        if ($sign){
+                            //先标记成功 进入处理
+                            DB::update(DB::raw("UPDATE owner_price_operation_owner SET discount_date = ?,target_value = ? WHERE  owner_price_operation_id = ? AND owner_id = ?"),
+                                [date("Y-m-d"),$targetValue,$rule->id,$owner_id]);
+                            $pivot->discount_date = date("Y-m-d");
+                            $pivot->target_value = $targetValue;
+                            Cache::put($key,$pivot,1000);
+                        }
+                        DB::commit();
+                    }catch (\Exception $exception){
+                        DB::rollBack();
+                    }
+                    return $pivot ?? null;
+                },1000);
+                if ($pivot && $sign)$this->handlePastBill($targetValue,$rule,$owner_id,$units,$discountIndex,$pivot);
             }
             if ($rule->strategy == '特征'){//特征策略匹配
                 $bool = app("FeatureService")->matchFeature($rule->feature,$columnMapping,$matchObject);
@@ -328,10 +348,6 @@ class OwnerPriceOperationService
      */
     public function handlePastBill($targetValue, $rule, $owner_id, $units, $discountIndex, $pivot)
     {
-        //先标记成功 进入处理
-        DB::update(DB::raw("UPDATE owner_price_operation_owner SET discount_date = ?,target_value = ? WHERE  owner_price_operation_id = ? AND owner_id = ?"),
-            [date("Y-m-d"),$targetValue,$rule->id,$owner_id]);
-
         try{
             DB::beginTransaction();
             $month = date("Y-m");