stanley-king před 4 roky
rodič
revize
464875ac0b

+ 44 - 0
data/model/topcard.model.php

@@ -1 +1,45 @@
 <?php
+
+declare(strict_types=0);
+
+defined('InShopNC') or exit('Access Invalid!');
+
+use mtopcard;
+
+class topcardModel extends Model
+{
+    public function add($card_no,$card_type,$add_time)
+    {
+        return $this->table('topcard')->insert(['card_no' => $card_no,'card_type' => $card_type,'add_time' => $add_time]);
+    }
+    public function get_card($card_no)
+    {
+        return $this->find($card_no);
+    }
+    public function add_money($card_no,$amount,$time)
+    {
+        $this->table('topcard')->where(['card_no' => $card_no])
+             ->update(['total_amount' => ['exp',"total_amount+{$amount}"],
+                       'add_times' => ['exp',"add_times+1"],
+                       'last_time' => time()]);
+
+        $stamp = mtopcard\month_stamp($time);
+        if(empty($this->find_log($card_no,$stamp))) {
+            $datas = ['card_no' => $card_no,'month_stamp' => $stamp,'month_amount' => $amount,
+                      'add_times' => 1,'last_time' => time()];
+            $ret = $this->table('topcard_log')->insert($datas);
+        }
+        else {
+            $ret = $this->where(['card_no' => $card_no,'month_stamp' => $stamp])
+                        ->update(['month_amount' => ['exp',"month_amount+{$amount}"],
+                                  'add_times' => ['exp',"add_times+1"],
+                                  'last_time' => time()]);
+        }
+
+        return $ret;
+    }
+    private function find_log($card_no,$month_stamp)
+    {
+        return $this->table('topcard_log')->where(['card_no' => $card_no,'month_stamp' => $month_stamp])->select();
+    }
+}

+ 2 - 2
helper/mtopcard/mtopcard.php

@@ -27,8 +27,8 @@ const FreezedCard  = 3;
 const OilCardPaper   = 1;
 const PhoneCardPaper = 2;
 
-function month_stamp() : int {
-    $date = getdate();
+function month_stamp($time=null) : int {
+    $date = getdate($time);
     $stamp = $date['year'] * 100 + $date['mon'];
 
     return $stamp;

+ 47 - 1
test/TestMemberCard.php

@@ -19,7 +19,6 @@ require_once(BASE_HELPER_PATH . '/util_helper.php');
 require_once(BASE_HELPER_PATH . '/order_helper.php');
 require_once(BASE_HELPER_PATH . '/bonus_helper.php');
 require_once(BASE_HELPER_PATH . '/vrorder_helper.php');
-
 use mcard;
 
 class TestMemberCard extends TestCase
@@ -31,6 +30,53 @@ class TestMemberCard extends TestCase
         Base::run_util();
     }
 
+    public function testLoadTopcards()
+    {
+        global $config;
+        $spec_card = $config['vgoods_spec_card'];
+
+        $vr_order = Model();
+        $topcard = Model('topcard');
+        $member_topcard = Model('member_topcard');
+
+        $i = 0;
+        while (true)
+        {
+            $start = $i * 1000;
+            $items = $vr_order->table('vr_order')->field('*')->order('order_id asc')->limit("{$start},1000")->select();
+            if(empty($items)) {
+                return;
+            }
+            $i++;
+
+            foreach ($items as $item)
+            {
+                $member_id = intval($item['buyer_id']);
+                $add_time  = intval($item['add_time']);
+                $payment_time  = intval($item['payment_time']);
+                $goods_id  = intval($item['goods_id']);
+                $amount = $spec_card[$goods_id];
+                $order_state = intval($item['order_state']);
+
+                $extra_info = json_decode($item['extra_info'],true);
+                $card_no   = $extra_info['input']['card_no'];
+                $card_type = mtopcard\topcard_type($extra_info['input']['card_type']);
+
+                if(!$topcard->add($card_no,$card_type,$add_time)) {
+                    Log::record("{$card_no} 已经录入",Log::DEBUG);
+                }
+
+                if(in_array($order_state,[ORDER_STATE_PAY, ORDER_STATE_SEND, ORDER_STATE_SUCCESS]))
+                {
+                    $ret = $topcard->add_money($card_no,$amount,$payment_time);
+                    if(!$ret) {
+                        Log::record("{$card_no} add money fail.",Log::DEBUG);
+                    }
+                }
+            }
+        }
+    }
+
     public function testDate()
     {
         $now = time();