dever 6 tahun lalu
induk
melakukan
9c0dd9dc14
1 mengubah file dengan 56 tambahan dan 22 penghapusan
  1. 56 22
      main/lib/Cron.php

+ 56 - 22
main/lib/Cron.php

@@ -52,37 +52,41 @@ class Cron
         }
 
         if ($score > 0) {
-            $where['uid'] = $data['uid'];
-            $user = Dever::db('score/user')->one($where);
-            if ($user) {
-                $user_score = $user['score'] - $score;
-                if ($user_score < 0) {
-                    $user_score = 0;
-                }
-                Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
-            }
 
-            $where['type'] = 4;
-            $where['data_id'] = 28;
-            $user = Dever::db('act/score')->one($where);
-            if ($user) {
-                $user_score = $user['score'] - $score;
-                if ($user_score < 0) {
-                    $user_score = 0;
+            if ($data['type'] == 1) {
+                $where['uid'] = $data['uid'];
+                $user = Dever::db('score/user')->one($where);
+                if ($user) {
+                    $user_score = $user['score'] - $score;
+                    if ($user_score < 0) {
+                        $user_score = 0;
+                    }
+                    Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
                 }
-                Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
-            }
 
-            # 判断兑换码是否使用
-            if ($data['type'] == 3) {
+                $where['type'] = 4;
+                $where['data_id'] = 28;
+                $user = Dever::db('act/score')->one($where);
+                if ($user) {
+                    $user_score = $user['score'] - $score;
+                    if ($user_score < 0) {
+                        $user_score = 0;
+                    }
+                    Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
+                }
+            } elseif ($data['type'] == 3) {
                 $code_where['order_id'] = $data['order_id'];
-                $code_where['type'] = 1;
+                //$code_where['type'] = 1;
                 $code_where['create_uid'] = $data['uid'];
                 $code = Dever::db('code/info')->state($code_where);
                 if ($code) {
                     print_r($code);
                     foreach ($code as $k => $v) {
-                        Dever::db('code/info')->update(array('where_id' => $v['id'], 'type' => 4));
+                        if ($v['type'] == 1) {
+                            Dever::db('code/info')->update(array('where_id' => $v['id'], 'type' => 4));
+                        } elseif ($v['type'] == 3) {
+                            $this->refundScoreCode($v);
+                        }
                     }
                 }
             }
@@ -100,6 +104,36 @@ class Cron
         return $score;
     }
 
+    private function refundScoreCode($data)
+    {
+        if (isset($data['score']) && $data['score'] > 0) {
+            $score = $data['score'];
+        } elseif (isset($data['product_num']) && $data['product_num'] > 0) {
+            $score = $data['product_num'] * 20;
+        }
+
+        $where['uid'] = $data['uid'];
+        $user = Dever::db('score/user')->one($where);
+        if ($user) {
+            $user_score = $user['score'] - $score;
+            if ($user_score < 0) {
+                $user_score = 0;
+            }
+            Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
+        }
+
+        $where['type'] = 4;
+        $where['data_id'] = 28;
+        $user = Dever::db('act/score')->one($where);
+        if ($user) {
+            $user_score = $user['score'] - $score;
+            if ($user_score < 0) {
+                $user_score = 0;
+            }
+            Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
+        }
+    }
+
     public function pays_api()
     {
         $where['product_id'] = 28;