Prechádzať zdrojové kódy

add merchant storage limit

stanley-king 4 rokov pred
rodič
commit
e07e50ca50

+ 62 - 17
crontab/control/minutes.php

@@ -9,6 +9,7 @@
 
 defined('InShopNC') or exit('Access Invalid!');
 
+require_once (BASE_ROOT_PATH . '/helper/message/publisher.php');
 
 class minutesControl extends BaseCronControl
 {
@@ -71,12 +72,12 @@ class minutesControl extends BaseCronControl
         $mch_cache = rcache("storge_limit" , 'merchant-');
         $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
 
-        $reader = function ($mchid,$time,$curtime)
+        $reader = function ($mchid,$time)
         {
             $cond['mchid'] = $mchid;
             $cond['inner_status'] = 0;
             $cond['order_state'] = ORDER_STATE_SUCCESS;
-            $cond['refill_order.order_time'] = [['egt', $time],['lt', $curtime],'and'];
+            $cond['refill_order.order_time'] = ['egt', $time];
 
             $items = Model('')->table('refill_order,vr_order')->join('inner')
                 ->on('refill_order.order_id=vr_order.order_id')
@@ -87,29 +88,73 @@ class minutesControl extends BaseCronControl
             return $items;
         };
 
-        $curtime = time();
-        $new_caches = [];
-        $card_type = [1 => 'petrochina' , 2 => 'sinopec'];
+
+        $merger = function ($limits,$reals,$mchid,$card_type)
+        {
+            $result = [];
+            foreach ($limits as $amount => $limit)
+            {
+                $limit = intval($limit);
+
+                if($limit === -1) {
+                    $allow = true;
+                }
+                elseif($limit === 0) {
+                    $allow = false;
+                }
+                else
+                {
+                    $finded = false;
+                    foreach ($reals as $item)
+                    {
+                        $tmp = intval($item['refill_amount'] + 0.005);
+                        if($item['card_type'] == $card_type && $tmp == $amount) {
+                            $num = $item['num'];
+                            $allow = $limit > $num;
+                            $finded = true;
+                            break;
+                        }
+                    }
+
+                    if(!$finded) {
+                        $allow = true;
+                    }
+                }
+
+                $result["{$mchid}-{$card_type}-{$amount}"] = $allow;
+            }
+
+            return $result;
+        };
+
+        $type_map = ['petrochina' => 1,'sinopec' => 2];
+        $abilitys = [];
         foreach ($caches as $cache)
         {
             $mchid = intval($cache['mchid']);
             $start = $cache['time'];
-            $end = $curtime;
 
-            $items = $reader($mchid,$start,$end);
-            foreach ($items as $item) {
-                //指定card_type,refill_amount的单数比缓存中设置单数大,设置对应缓存能否下单为false,key为商户ID-卡类型-额度
+            $petros = $cache['petrochina'];
+            $sinos  = $cache['sinopec'];
+            $items = $reader($mchid,$start);
 
-                $cache_num = $cache[$card_type[$item['card_type']]][$item['refill_amount']];
-                $can_add = true;
-                if ($cache_num < $item['num']) {
-                    $can_add = $cache_num == -1;
-                }
-                $new_caches["{$cache['mchid']}-{$item['card_type']}-{$item['refill_amount']}"] = $can_add;
+            $prets = $merger($petros,$items,$mchid,$type_map['petrochina']);
+            $srets = $merger($sinos,$items,$mchid,$type_map['sinopec']);
+            foreach ($prets as $key => $val) {
+                $abilitys[$key] = $val;
+            }
+            foreach ($srets as $key => $val) {
+                $abilitys[$key] = $val;
             }
         }
-        if(!empty($new_caches)) {
-            wcache("mch_can_order" , ['data' => serialize($new_caches)] , 'merchant-');
+
+        $old = rcache("refill_able",'merchant-');
+        $old = $old['data'];
+        $new = serialize($abilitys);
+        if($new != $old) {
+            wcache("refill_able" , ['data' => $new] , 'merchant-');
+            $publisher = new message\publisher();
+            $publisher->modify_refill_merchant();
         }
     }
 

+ 36 - 0
helper/refill/RefillFactory.php

@@ -103,6 +103,7 @@ class RefillFactory
     private $mOilProvider;
     private $mPhoneProvider;
     private $mProviderNames;
+    private $mLimits = [];
 
     private function __construct()
     {
@@ -114,6 +115,41 @@ class RefillFactory
         }
     }
 
+    public function allow($mchid,$card_type,$amount)
+    {
+        $reader = function () {
+            $cache = rcache("refill_able",'merchant-');
+            if(!empty($cache)) {
+                $result = unserialize($cache['data']);
+            }
+            else {
+                $result = [];
+            }
+            return $result;
+        };
+
+        if(defined('MOBILE_SERVER') && MOBILE_SERVER === true)
+        {
+            if(StatesHelper::fetch_state('merchant')) {
+                $this->mLimits = $reader();
+            }
+        }
+        else {
+            $this->mLimits = $reader();
+        }
+
+        $key = "{$mchid}-{$card_type}-{$amount}";
+        if(empty($this->mLimits)) {
+            return true;
+        }
+        elseif(array_key_exists($key,$this->mLimits)) {
+            return $this->mLimits[$key];
+        }
+        else {
+            return true;
+        }
+    }
+
     public function goods()
     {
         global $config;

+ 15 - 0
mobile/control/refill.php

@@ -79,6 +79,21 @@ class refillControl extends merchantControl
             return self::outerr(206,"平台不支持该卡充值.");
         }
 
+        $card_type = mtopcard\card_type($card_no);
+        if($card_type === mtopcard\SinopecCard || $card_type === mtopcard\PetroChinaCard)
+        {
+            $allow = refill\RefillFactory::instance()->allow($this->mchid(),$card_type,$amount);
+            if(!$allow) {
+                if ($card_type === mtopcard\SinopecCard) {
+                    $text = "中石化";
+                }
+                else {
+                    $text = "中石油";
+                }
+                return self::outerr(207,"今日贵司{$amount}{$text}充值库存已经用完.");
+            }
+        }
+
         [$state, $err] = refill\RefillFactory::instance()->add($this->mchid(), $this->adminid(), $amount, $card_no, $mch_order, $idcard, $card_name, $notify_url);
         if($state === true) {
             return self::outsuccess(['state' => true]);

+ 6 - 0
test/TestRefill.php

@@ -867,6 +867,12 @@ class TestRefill extends TestCase
     {
         refill\RefillFactory::instance()->read_channel();
     }
+
+    public function testAllow()
+    {
+        $allow = refill\RefillFactory::instance()->allow(10096,1,100);
+
+    }
 }