Explorar o código

TestMemberCard update

stanley-king %!s(int64=4) %!d(string=hai) anos
pai
achega
277f596870
Modificáronse 3 ficheiros con 110 adicións e 72 borrados
  1. 0 0
      data/cards/userids.txt
  2. 0 5
      helper/vrorder_helper.php
  3. 110 67
      test/TestMemberCard.php

+ 0 - 0
data/cards/userids.txt


+ 0 - 5
helper/vrorder_helper.php

@@ -66,9 +66,4 @@ class vrorder_helper
             }
         }
     }
-
-    public static function get_order($condition){
-        $vr_order = Model('vr_order');
-        return $vr_order->getOrderList($condition);
-    }
 }

+ 110 - 67
test/TestMemberCard.php

@@ -179,10 +179,8 @@ class TestMemberCard extends TestCase
         $ret = mcard\getTopcardInfo('oil',36490);
     }
 
-
     public function testAddMemberYearCard()
     {
-        global $config;
         $finder = function () {
             $mid_amounts = [];
             $path = BASE_DATA_PATH . "/cards/memcard.txt";
@@ -194,10 +192,9 @@ class TestMemberCard extends TestCase
 
             while(!feof($file)) {
                 $line = fgets($file);
-                $line = str_replace("\r\n", '',$line);
                 $kv = preg_split('/,/',$line);
-                if(count($kv) != 3) continue;
-                $mid_amounts[] = ['mid' => intval($kv[0]),'start_time' => strtotime($kv[1]),'end_time' => strtotime($kv[2])];
+                if(count($kv) != 2) continue;
+                $mid_amounts[] = ['mid' => intval($kv[0]),'amount' => intval($kv[1])];
             }
             fclose($file);
 
@@ -205,68 +202,20 @@ class TestMemberCard extends TestCase
         };
 
         $mids = $finder();
-        $spec_card = $config['vgoods_spec_card'];
         foreach ($mids as $val)
         {
-            $condition['buyer_id'] = $val['mid'];
-            $condition['add_time'] = ['between',[$val['start_time'],$val['end_time']]];
-            $vrorder_helper = new \vrorder_helper();
-            $orders = $vrorder_helper->get_order($condition);
-            if(empty($orders)){
-                $result[$val['mid']] = [];
-            }
-            foreach ($orders as $item){
-                $goods_id  = intval($item['goods_id']);
-                $amount = $spec_card[$goods_id];
-                $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($card_type == 1 || $card_type == 2){
-                    $data[$card_type]['type'] = $card_type;
-                    $data[$card_type]['amount'] += $amount * $item['goods_num'];
-                }elseif($card_type == 3){
-                    $PhoneLocation = $this->GetPhoneLocation($card_no);
-                    $data[$card_type]['type'] = $card_type;
-                    $data[$card_type]['amount'] += $amount * $item['goods_num'];
-//                    $phoneData['catName'] = $PhoneLocation['catName'];
-//                    $phoneData['card_no'] = $card_no;
-//                    $phoneData['amount']  = $amount * $item['goods_num'];
-                    $phoneData[$card_no]['catName'] = $PhoneLocation['catName'];
-                    $phoneData[$card_no]['card_no'] = $card_no;
-                    $phoneData[$card_no]['amount']  += $amount * $item['goods_num'];
-                    $data[$card_type]['phoneData'] = $phoneData;
-                }
+            $usercards = new mcard\user_mcards($val['mid']);
+            $params = mcard\getConfig('year',$val['amount']);
+            $ret = $usercards->addCard($params);
+            if($ret) {
+                Log::record("{$val['mid']} : {$val['amount']} success.",Log::DEBUG);
+            } else {
+                Log::record("{$val['mid']} : {$val['amount']} fail.",Log::ERR);
             }
-            $result[$val['mid']] = $data;
-            return $result;
-//            if($ret) {
-//                Log::record("{$val['mid']} : {$val['amount']} success.",Log::DEBUG);
-//            } else {
-//                Log::record("{$val['mid']} : {$val['amount']} fail.",Log::ERR);
-//            }
         }
     }
 
-    /**
-     * 根据手机号获取收运营商信息
-     * @param $phone
-     * @return array|bool
-     */
-    private function GetPhoneLocation($phone)
-    {
-        if (empty($phone)) return false;  //手机号不能为空
-        $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
-        $response = file_get_contents($url);  //获取API返回的数据
-        $list = mb_convert_encoding($response, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
-        //解决返回数据格式问题
-        $res = trim(explode('=', $list)[1]);
-        preg_match_all("/(\w+):'([^']+)/", $res, $m);
-        $res = array_combine($m[1], $m[2]);
-        return $res;
-    }
-
-        public function testAddMemberCard4Hg()
+    public function testAddMemberCard4Hg()
     {
         $mids = [62704,52836];
         foreach ($mids as $mid) {
@@ -415,25 +364,119 @@ class TestMemberCard extends TestCase
 
     public function testAnalyseBuyer()
     {
-        $finder = function () {
-            $mid_amounts = [];
+        $inputer = function () {
             $path = BASE_DATA_PATH . "/cards/userids.txt";
             $file = fopen($path,'r');
             if(!is_resource($file)) {
                 Log::record(__METHOD__ . " {$path} file is not exist",Log::ERR);
-                return $mid_amounts;
+                return [];
             }
 
+            $mids = [];
             while(!feof($file)) {
                 $line = fgets($file);
+                $line = str_replace("\r\n", '',$line);
                 $kv = preg_split('/,/',$line);
-                if(count($kv) != 2) continue;
-                $mid_amounts[] = ['mid' => intval($kv[0]),'amount' => intval($kv[1])];
+
+                if(count($kv) != 3) continue;
+                $mids[] = ['mid' => intval($kv[0]),'start_time' => strtotime($kv[1]),'end_time' => strtotime($kv[2])];
             }
             fclose($file);
 
-            return $mid_amounts;
+            return $mids;
         };
+
+        $mids = $inputer();
+
+        $result = [];
+        foreach ($mids as $val) {
+            $uid = intval($val['mid']);
+            if (!array_key_exists($uid, $result)) {
+                $result[$uid] = [1 => 0.0, 2 => 0.0, 3 => 0.0, 4 => 0.0, 5 => 0.0];
+            }
+
+            $condition['buyer_id'] = $val['mid'];
+            $condition['add_time'] = ['between', [$val['start_time'], $val['end_time']]];
+            $condition['order_state'] = ORDER_STATE_SUCCESS;
+
+            $vr_order = Model('vr_order');
+            $orders = $vr_order->getOrderList($condition);
+
+            foreach ($orders as $order) {
+                [$card_type, $amount] = $this->calc_order($order);
+                if ($card_type > 0) {
+                    $result[$uid][$card_type] += $amount;
+                }
+            }
+        }
+    }
+
+    private function carrier_type($phone)
+    {
+        $checker = function ($phone) {
+            if (empty($phone)) return false;  //手机号不能为空
+            $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
+            $response = file_get_contents($url);  //获取API返回 的数据
+            $list = mb_convert_encoding($response, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
+            //解决返回数据格式问题
+            $res = trim(explode('=', $list)[1]);
+            preg_match_all("/(\w+):'([^']+)/", $res, $m);
+            $res = array_combine($m[1], $m[2]);
+            return $res;
+        };
+
+        $ret = $checker($phone);
+
+        if(empty($ret)) {
+            return 0;
+        }
+        elseif($ret['catName'] == '中国联通') {
+            return 1;
+        }
+        elseif($ret['catName'] == '中国电信') {
+            return 2;
+        }
+        elseif($ret['catName'] == '中国移动') {
+            return 3;
+        }
+    }
+
+    /**
+     * @param $order
+     * @param $spec_card
+     * @param $result
+     */
+    private function calc_order($order)
+    {
+        global $config;
+        $spec_card = $config['vgoods_spec_card'];
+
+
+        $goods_id = intval($order['goods_id']);
+        $spec_amount = $spec_card[$goods_id];
+        $extra_info = json_decode($order['extra_info'], true);
+
+        $card_no = $extra_info['input']['card_no'];
+        $card_type = mtopcard\topcard_type($extra_info['input']['card_type']);
+
+        if ($card_type == 1 || $card_type == 2) {
+            return [$card_type,$spec_card[$goods_id] * $order['goods_num']];
+        }
+        elseif ($card_type == 3)
+        {
+            $carrier_type = $this->carrier_type($card_no);
+            if ($carrier_type > 0) {
+                $card_type = $card_type + $carrier_type - 1;
+                return [$card_type,$spec_amount * $order['goods_num']];
+
+            } else {
+                Log::record("order {$order} 无法检查手机号码:{$card_no} 类型", Log::ERR);
+            }
+        } else {
+            Log::record("order {$order} 卡类型无效.", Log::ERR);
+        }
+
+        return [0,0];
     }
 }