Bladeren bron

增加红包信息到session 中

stanley-king 8 jaren geleden
bovenliggende
commit
0d0c16f345

+ 0 - 1
data/model/member.model.php

@@ -615,7 +615,6 @@ class memberModel extends Model
             'freeze_predeposit' => $freeze_predeposit,
             'available_relay_balance' => $available_relay_balance,
             'freeze_relay_balance' => $freeze_relay_balance,
-            'available_bonus' => $member_info['available_bonus'],
             'rate_version' => $rate_version);
     }
 }

+ 2 - 2
data/model/user_bonus.model.php

@@ -51,9 +51,9 @@ class user_bonusModel extends Model
         return $this->table('user_bonus')->where($cond)->sum('remain_amount');
     }
 
-    public function getSum($cond)
+    public function getSum($cond,$field = 'remain_amount')
     {
-        return $this->table('user_bonus')->where($cond)->sum('remain_amount');
+        return $this->table('user_bonus')->where($cond)->sum($field);
     }
 
     public function lasted_binded_time($condition) {

+ 1 - 0
helper/model/goods_summary.php

@@ -76,6 +76,7 @@ class goods_summary
         $summary['gc_id'] = intval($this->goods_info['gc_id']);
 
         $bonus_cents = intval($this->goods_info['goods_price'] * (1 - predeposit_helper::scale()) * 100 + 0.5);
+
         $summary['bonus_price'] = $bonus_cents / 100;
         $summary['goods_price'] = $this->goods_info['goods_price'];
         $summary['goods_marketprice'] = $this->goods_info['goods_marketprice'];

+ 112 - 51
helper/predeposit_helper.php

@@ -15,6 +15,7 @@ class RateMoney
 {
     const ASC  = 1;
     const DESC = 2;
+    const PRED_RATE = 30;
     private $mRates;
     private $mDirty;
 
@@ -33,6 +34,45 @@ class RateMoney
         ksort($this->mRates);
     }
 
+    public function add_bonuses($items)
+    {
+        foreach ($items as $item)
+        {
+            $bonus = \bonus\user_bonus::create_by_param($item);
+            $rate = $bonus->bonus_rate();
+            $amount = intval($bonus->remain_amount() * 100 + 0.5);
+
+            if($amount <= 0) continue;
+
+            $this->mDirty = true;
+            if(array_key_exists($rate,$this->mRates) == false) {
+                $this->mRates[$rate] = 0.00;
+            }
+            $this->mRates[$rate] += $bonus->remain_amount();
+        }
+        ksort($this->mRates);
+    }
+
+    public function balance($money)
+    {
+        $money = intval($money * 100 + 0.5);
+        $total = intval($this->total() * 100 + 0.5);
+
+        if($money > $total)
+        {
+            if(array_key_exists(self::PRED_RATE,$this->mRates) == false) {
+                $this->mRates[self::PRED_RATE] = 0.00;
+            }
+            $this->mRates[self::PRED_RATE] += ($money - $total) / 100;
+        }
+    }
+
+
+    public function resort()
+    {
+        ksort($this->mRates);
+    }
+
     public function is_enough(&$rate,$amount)
     {
         $amount = intval($amount * 100 + 0.5);
@@ -83,10 +123,21 @@ class RateMoney
         }
         return $result;
     }
+    public function total()
+    {
+        $total = 0.00;
+        foreach ($this->mRates as $key => $amount) {
+            $total += $amount;
+        }
+        return $total;
+    }
 
     public function dirty() {
         return $this->mDirty;
     }
+    public function clean() {
+        $this->mDirty = false;
+    }
 }
 
 class predeposit_helper
@@ -96,6 +147,7 @@ class predeposit_helper
     private $mTotal_value;
     private $mFreeze_value;
     private $mRates;
+    private $mBonusState;
     private $mRateVersion; //用来记录,红包过期带来的红包变化
 
     public function __construct($member_id)
@@ -106,49 +158,67 @@ class predeposit_helper
         $pd_array = Model('member')->getMemberPdInfo($this->member_id);
         $this->mTotal_value = $pd_array['available_predeposit'];   // 当前预存款
         $this->mFreeze_value = $pd_array['freeze_predeposit'];     // 当前预存款冻结
+        $this->mRateVersion = $pd_array['rate_version'];
+        $this->init_rate();
+    }
 
-        $available_bonus = $pd_array['available_bonus'];
-        if (empty($available_bonus))
+    public function init_rate()
+    {
+        $fUpdate = false;
+        if(isset($_SESSION['bonus_rate_version']) == false) {
+            $fUpdate = true;
+        }
+        else
         {
-            $total = intval($this->mTotal_value * 100 + 0.5);
-            if($total > 0) {
-                $this->mRates = new RateMoney(array(30 => $this->mTotal_value));
-            } else {
-                $this->mRates = null;
+            $version = $_SESSION['bonus_rate_version'];
+            if($version != $this->mRateVersion) {
+                $fUpdate = true;
+            }
+            else
+            {
+                if(isset($_SESSION['bonus_rate']) && isset($_SESSION['bonus_state'])) {
+                    $this->mRates = new RateMoney($_SESSION['bonus_rate']);
+                    $this->mBonusState = $_SESSION['bonus_state'];
+                } else {
+                    $fUpdate = true;
+                }
             }
         }
-        else {
-            $this->mRates = new RateMoney(unserialize($available_bonus));
+
+        if($fUpdate)
+        {
+            $mod_bonus = Model('user_bonus');
+            $this->mRates = new RateMoney(array());
+            $items = $mod_bonus->getUsableBonus($this->member_id);
+            $this->mRates->add_bonuses($items);
+            $this->mRates->balance($this->mTotal_value);
+            $bonus_rate = $this->mRates->format();
+
+            $this->mBonusState = [];
+            $querys = array('usable','expiring','used','expired');
+            foreach ($querys as $state) {
+                $cond = $this->query_cond($state);
+                if($state == 'used') {
+                    $sum = $mod_bonus->getSum($cond,'bonus_value');
+                } else {
+                    $sum = $mod_bonus->getSum($cond);
+                }
+                $this->mBonusState[$state] = doubleval($sum);
+            }
+            $this->write_rates($this->mBonusState,$bonus_rate);
+            $this->mRates->clean();
         }
-        $this->mRateVersion = $pd_array['rate_version'];
     }
 
     public function __destruct()
     {
         if($this->mRates != null && $this->mRates->dirty()) {
-            $data = $this->mRates->format();
-            $member = Model('member');
-            $member->editMember(array('member_id' => $this->member_id),array('available_bonus' => serialize($data)));
+            $this->del_rates();
         }
     }
 
-    public function bonus_state()
-    {
-        $ret = $this->read_rates();
-        if($ret != false) {
-            return $ret;
-        }
-
-        $result = [];
-        $mod_bonus = Model('user_bonus');
-        $querys = array('usable','expiring','used','expired');
-        foreach ($querys as $state) {
-            $cond = $this->query_cond($state);
-            $sum = $mod_bonus->getSum($cond);
-            $result[$state] = doubleval($sum);
-        }
-        $this->write_rates($result);
-        return $result;
+    public function bonus_state() {
+        return $this->mBonusState;
     }
 
     private function query_cond($query_state)
@@ -162,10 +232,14 @@ class predeposit_helper
         {
             if($query_state == 'usable') {
                 $cond['remain_amount'] = array('gt','0.00');
+                $cond['expired'] = 0;
             } elseif ($query_state == 'expiring') {
                 $cond['usable_time'] = array('elt',time() + 5 * $day_secs);
+                $cond['remain_amount'] = array('gt','0.00');
+                $cond['expired'] = 0;
             } elseif ($query_state == 'used') {
                 $cond['remain_amount'] = '0.00';
+                $cond['expired'] = 0;
             } elseif ($query_state == 'expired') {
                 $cond['expired'] = 1;
             } else {
@@ -175,47 +249,34 @@ class predeposit_helper
         return $cond;
     }
 
-    private function read_rates()
-    {
-        if(isset($_SESSION['bonus_rate_version']) == false) {
-            return false;
-        }
-
-        $version = $_SESSION['bonus_rate_version'];
-        if($version != $this->mRateVersion) {
-            return false;
-        }
-
-        if(isset($_SESSION['bonus_rate'])) {
-            return $_SESSION['bonus_rate'];
-        } else {
-            return false;
-        }
-    }
-
-    private function write_rates($bonus_rate)
+    private function write_rates($bonus_state,$bonus_rate)
     {
+        $_SESSION['bonus_state'] = $bonus_state;
         $_SESSION['bonus_rate'] = $bonus_rate;
         $_SESSION['bonus_rate_version'] = $this->mRateVersion;
     }
 
     private function del_rates()
     {
+        if(array_key_exists('bonus_state',$_SESSION)) {
+            unset($_SESSION['bonus_state']);
+        }
         if(array_key_exists('bonus_rate',$_SESSION)) {
             unset($_SESSION['bonus_rate']);
         }
+
         if(array_key_exists('bonus_rate_version',$_SESSION)) {
             unset($_SESSION['bonus_rate_version']);
         }
     }
+
     public function topup_bonus($mobile)
     {
         $bonuses = bonus_helper::topup_bonus($mobile);
         if($bonuses != false) {
             $this->del_rates();
-        } else {
-            //todo 此处增加红包
         }
+
         return $bonuses;
     }
 

+ 1 - 1
helper/special_helper.php

@@ -203,7 +203,7 @@ class special_helper
         $result = [];
 
         $result['item_title'] = $title;
-        $result['item_type'] = 'homebonus';
+        $result['item_type'] = 'home_bonus';
 
         foreach ($bonuses as $bonus) {
             $item['image'] = "";

+ 5 - 1
mobile/control/member_bonus.php

@@ -173,10 +173,14 @@ class member_bonusControl extends mbMemberControl
         {
             if($query_state == 'usable') {
                 $cond['remain_amount'] = array('gt','0.00');
+                $cond['expired'] = 0;
             } elseif ($query_state == 'expiring') {
                 $cond['usable_time'] = array('elt',time() + 5 * $day_secs);
+                $cond['remain_amount'] = array('gt','0.00');
+                $cond['expired'] = 0;
             } elseif ($query_state == 'used') {
                 $cond['remain_amount'] = '0.00';
+                $cond['expired'] = 0;
             } elseif ($query_state == 'expired') {
                 $cond['expired'] = 1;
             } else {
@@ -203,7 +207,7 @@ class member_bonusControl extends mbMemberControl
             $blocks = [];
             $block = special_helper::format_bonus($bonus);
             $blocks[] = $block;
-            
+
             return self::outsuccess(array('special_list' => $blocks,
                 'summary'  => null,
                 'groupbuy' => null,