xxxrrrdddd il y a 3 ans
Parent
commit
31414c02a0
1 fichiers modifiés avec 39 ajouts et 26 suppressions
  1. 39 26
      application/common/model/Mobile.php

+ 39 - 26
application/common/model/Mobile.php

@@ -73,25 +73,38 @@ class Mobile extends Model
 
 
 
-
+    public function makeAmount(){
+        $mobile=$this;
+        if(empty($mobile['amount_base'])){
+            $mobile['amount_base']=0;
+        }
+        if(empty($mobile['amount_charge'])){
+            $mobile['amount_charge']=0;
+        }
+        if(!isset($mobile['is_activity'])){
+            $mobile['is_activity']=0;
+        }
+        if(!$mobile['is_activity']) {
+            $mobile['amount'] = $mobile['amount_base'] + $mobile['amount_charge'];
+        }else{
+            $mobile['amount'] = $mobile['amount_kill'] + $mobile['amount_charge'];
+        }
+    }
+    public function makeRules(){
+        $mobile=$this;
+        foreach (MobileComputer::setMobile($mobile['no'])->filter() as $key=>$value){
+            $mobile[$key]=$value;
+        }
+    }
+    public function amountChangeColumn(){
+        return [
+            'amount_base',
+            'amount_charge',
+        ];
+    }
     public static function init()
     {
         self::beforeWrite(function (self $mobile){
-            if(empty($mobile['amount_base'])){
-                $mobile['amount_base']=0;
-            }
-            if(empty($mobile['amount_charge'])){
-                $mobile['amount_charge']=0;
-            }
-            if(!isset($mobile['is_activity'])){
-                $mobile['is_activity']=0;
-            }
-            if(!$mobile['is_activity']) {
-                $mobile['amount'] = $mobile['amount_base'] + $mobile['amount_charge'];
-            }else{
-                $mobile['amount'] = $mobile['amount_kill'] + $mobile['amount_charge'];
-            }
-
             if(isset($mobile['top_time']) && $mobile['top_time']==1){
                 $mobile['top_time']=time();
             }else{
@@ -107,18 +120,16 @@ class Mobile extends Model
                 self::where('id','>',0)->update(['activity_time'=>null]);
                 $mobile['activity_time']=time();
             }
-
-            foreach (MobileComputer::setMobile($mobile['no'])->filter() as $key=>$value){
-                $mobile[$key]=$value;
-            }
         });
-
-        self::afterInsert(function (self $mobile){
-            /*if(!$mobile->info()->find()){
-                $mobile->info()->save([]);
-            }*/
+        self::beforeUpdate(function (self $mobile){
+            $data=$mobile->getChangedData();
+            if(isset($data['no'])){
+                $mobile->makeRules();
+            }
+            if(array_intersect($data,$mobile->amountChangeColumn())){
+                $mobile->makeAmount();
+            }
         });
-
         self::beforeInsert(function (self $mobile){
             $mobile['sort']=mt_rand(0,99999999);
             if(empty($mobile['province_id'])){
@@ -134,6 +145,8 @@ class Mobile extends Model
             if(!empty($mobile['city_id'])){
                 $mobile['city']=Area::where('id',$mobile['city_id'])->value('name');
             }
+            $mobile->makeAmount();
+            $mobile->makeRules();
         });
     }