소스 검색

add stat member self discount

stanley-king 7 년 전
부모
커밋
a30e395a24
7개의 변경된 파일236개의 추가작업 그리고 10개의 파일을 삭제
  1. 4 4
      core/framework/db/mysqli.php
  2. 5 1
      crontab/control/date.php
  3. 53 0
      helper/stat_helper.php
  4. 1 1
      helper/statistics/statorder.php
  5. 139 2
      helper/statistics/stbonus.php
  6. 2 2
      mobile/control/inoherb.php
  7. 32 0
      test/TestStat.php

+ 4 - 4
core/framework/db/mysqli.php

@@ -151,10 +151,10 @@ class Db
             }
             else
             {
-//                Log::record($sql . " [ RunTime:" . $staer->elapsed(6) . "s ]", Log::SQL);
-                if(strpos($sql,"SELECT") === false) {
-                    Log::record($sql . " [ RunTime:" . $staer->elapsed(6) . "s ]", Log::SQL);
-                }
+                Log::record($sql . " [ RunTime:" . $staer->elapsed(6) . "s ]", Log::SQL);
+//                if(strpos($sql,"SELECT") === false) {
+//                    Log::record($sql . " [ RunTime:" . $staer->elapsed(6) . "s ]", Log::SQL);
+//                }
                 return $query;
             }
         }

+ 5 - 1
crontab/control/date.php

@@ -15,7 +15,6 @@ require_once(BASE_ROOT_PATH . '/helper/stat_helper.php');
 
 class dateControl extends BaseCronControl
 {
-
     /**
      * 该文件中所有任务执行频率,默认1天,单位:秒
      * @var int
@@ -123,6 +122,11 @@ class dateControl extends BaseCronControl
         stat_helper::onDaiyOrder($cur_date->getTimestamp());
     }
 
+    public function init_member_bonusOp()
+    {
+        stat_helper::onInitSelfDiscount();
+    }
+
     /**
      * 未付款订单超期自动关闭
      */

+ 53 - 0
helper/stat_helper.php

@@ -57,6 +57,59 @@ class stat_helper
             Log::record($ex->getMessage(),Log::ERR);
         }
     }
+
+    public static function onInitSelfDiscount()
+    {
+        try
+        {
+            $stat = new statistics\stbonus(time());
+            $stat->init_discount_self();
+        }
+        catch(Exception $ex)
+        {
+            Log::record($ex->getMessage(),Log::ERR);
+        }
+    }
+
+    public static function onInitInviteesDiscount()
+    {
+        try
+        {
+            $stat = new statistics\stbonus(time());
+            $stat->init_discount_invitee();
+        }
+        catch(Exception $ex)
+        {
+            Log::record($ex->getMessage(),Log::ERR);
+        }
+    }
+
+    public static function onDailySelfDiscount($time)
+    {
+        try
+        {
+            $stat = new statistics\stbonus($time);
+            $stat->discount_self();
+        }
+        catch(Exception $ex)
+        {
+            Log::record($ex->getMessage(),Log::ERR);
+        }
+    }
+
+    public static function onDailyInviteeDiscount($time)
+    {
+        try
+        {
+            $stat = new statistics\stbonus($time);
+            $stat->discount_invitee();
+        }
+        catch(Exception $ex)
+        {
+            Log::record($ex->getMessage(),Log::ERR);
+        }
+    }
+
     public static function onInvite()
     {
         try

+ 1 - 1
helper/statistics/statorder.php

@@ -37,7 +37,7 @@ class statorder extends stat_base
 
         $where = array();
         $where['order_isvalid'] = 1;//计入统计的有效订单
-        $where['order_add_time'] = ['between',[$this->mStartm,$this->mEndtm]];
+        $where['order_add_time'] = ['between',"{$this->mStartm},{$this->mEndtm}"];
         $field = ' COUNT(*) as ordernum, SUM(order_amount) as orderamount, COUNT(DISTINCT buyer_id) as ordermembernum, AVG(order_amount) as orderavg ';
         $stat_order = $model->getoneByStatorder($where, $field);
 

+ 139 - 2
helper/statistics/stbonus.php

@@ -95,7 +95,7 @@ class stbonus extends stat_base
     {
         $this->calc_time(self::cur_date_type);
 
-        $cond['send_start_date'] = array('between',array($this->mStartm,$this->mEndtm));
+        $cond['send_start_date'] = ['between',"{$this->mStartm},{$this->mEndtm}"];
         $field = 'bonus_rate,
         sum(total_amount) as all_total_amount,
         sum(remain_amount) as all_remain_amount,
@@ -198,7 +198,7 @@ class stbonus extends stat_base
     {
         $this->calc_time(self::cur_date_type);
 
-        $cond['send_start_date'] = array('between',array($this->mStartm,$this->mEndtm));
+        $cond['send_start_date'] = ['between',"{$this->mStartm},{$this->mEndtm}"];
         $cond['sender_id'] = self::admin_member_id;
         $field = 'bonus_rate,
         sum(total_amount) as admin_total_amount,
@@ -253,4 +253,141 @@ class stbonus extends stat_base
             return $this->save($fEdit,$datas);
         }
     }
+
+    public function discount_self()
+    {
+        $this->calc_time(self::cur_date_type);
+
+        $mod_member = Model('member');
+        $mod_order = Model('order');
+        $orders = $mod_order->where(array('order_state' => ['in',[20,30,40]],
+            'payment_time' => ['between',"{$this->mStartm},{$this->mEndtm}"]))->field('bonus_rate,buyer_id')->limit(false)->select();
+
+        foreach ($orders as $order)
+        {
+            $member_id = $order['buyer_id'];
+            $discount = 0.00;
+            $rates = $order['bonus_rate'];
+            if(!empty($rates))
+            {
+                $rate_amount = unserialize($rates);
+                foreach ($rate_amount as $key => $val) {
+                    $discount += $val;
+                }
+            }
+            $mod_member->editMember(['member_id' => $member_id],['discount_self_amount' => ['exp', "discount_self_amount + {$discount}"]]);
+        }
+    }
+
+    public function discount_invitee()
+    {
+        $this->calc_time(self::cur_date_type);
+
+        $mod_member = Model('member');
+        $mod_order = Model('order');
+        $orders = $mod_order->where(array('order_state' => ['in',[20,30,40]],
+            'payment_time' => ['between',"{$this->mStartm},{$this->mEndtm}"]))->field('bonus_rate,buyer_id')->limit(false)->select();
+
+        foreach ($orders as $order)
+        {
+            $member_id = $order['buyer_id'];
+            $inviter_id = $this->inviter($mod_member,$member_id);
+            if($inviter_id == false) continue;
+
+            $discount = 0.00;
+            $rates = $order['bonus_rate'];
+            if(!empty($rates))
+            {
+                $rate_amount = unserialize($rates);
+                foreach ($rate_amount as $key => $val) {
+                    $discount += $val;
+                }
+            }
+            $mod_member->editMember(['member_id' => $inviter_id],['discount_invitee_amount' => ['exp', "discount_invitee_amount + {$discount}"]]);
+        }
+    }
+
+    private function inviter($mod_member,$member_id)
+    {
+        $members = $mod_member->field('inviter_id')->where(['member_id' => $member_id])->limit(false)->select();
+        if(empty($members)) {
+            return false;
+        }
+
+        return intval($members[0]['member_id']);
+    }
+
+    public function init_discount_self()
+    {
+        $mod_member = Model('member');
+        $members = $mod_member->field('*')->limit(false)->select();
+        foreach ($members as $member)
+        {
+            $member_id = intval($member['member_id']);
+            $mod_order = Model('order');
+            $orders = $mod_order->where(['order_state' => ORDER_STATE_SUCCESS,'buyer_id' => $member_id])->field('bonus_rate')->limit(false)->select();
+
+            if(empty($orders)) continue;
+
+            $discount = 0.00;
+            foreach ($orders as $order)
+            {
+                $rates = $order['bonus_rate'];
+                if(!empty($rates))
+                {
+                    $rate_amount = unserialize($rates);
+                    foreach ($rate_amount as $key => $val) {
+                        $discount += $val;
+                    }
+                }
+            }
+            $mod_member->editMember(['member_id' => $member_id],['discount_self_amount' => $discount]);
+        }
+    }
+
+    private function invitees($mod_member,$member_id)
+    {
+        $members = $mod_member->field('member_id')->where(['inviter_id' => $member_id])->limit(false)->select();
+        if(empty($members)) {
+            return false;
+        }
+
+        $result = [];
+        foreach ($members as $member) {
+            $result[] = intval($member['member_id']);
+        }
+
+        return $result;
+    }
+
+    public function init_discount_invitee()
+    {
+        $mod_member = Model('member');
+        $members = $mod_member->field('*')->limit(false)->select();
+        foreach ($members as $member)
+        {
+            $member_id = intval($member['member_id']);
+            $invitees = $this->invitees($mod_member,$member_id);
+            if($invitees == false) continue;
+
+            $mod_order = Model('order');
+            $orders = $mod_order->where(['order_state' => ORDER_STATE_SUCCESS,'buyer_id' => ['in',$invitees]])->field('bonus_rate')->limit(false)->select();
+
+            if(empty($orders)) continue;
+
+            $discount = 0.00;
+            foreach ($orders as $order)
+            {
+                $rates = $order['bonus_rate'];
+                if(!empty($rates))
+                {
+                    $rate_amount = unserialize($rates);
+                    foreach ($rate_amount as $key => $val) {
+                        $discount += $val;
+                    }
+                }
+            }
+            $mod_member->editMember(['member_id' => $member_id],['discount_invitee_amount' => $discount]);
+        }
+    }
 }

+ 2 - 2
mobile/control/inoherb.php

@@ -234,7 +234,7 @@ class inoherbControl extends mobileControl
 
     private function likes($inoherb_id)
     {
-        $items = $this->mModInoherb->field('likes')->where(['inoherb_id' => $inoherb_id])->select();
+        $items = $this->mModInoherb->field('likes')->where(['inoherb_id' => $inoherb_id])->lock(true)->select();
         if(empty($items)) {
             return 0;
         } else {
@@ -249,7 +249,7 @@ class inoherbControl extends mobileControl
 
     private function items($page = 0,$lock = false)
     {
-        return $this->mModInoherb->field('*')->order('likes desc')->page($page)->lock($lock)->select();
+        return $this->mModInoherb->field('*')->order('likes desc,inoherb_id desc')->page($page)->lock($lock)->select();
     }
 
     private function format($items)

+ 32 - 0
test/TestStat.php

@@ -10,6 +10,7 @@
 define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
 require_once(BASE_ROOT_PATH . '/fooder.php');
 require_once(BASE_ROOT_PATH . '/helper/stat_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/stat_helper.php');
 
 class TestStat extends PHPUnit_Framework_TestCase
 {
@@ -22,6 +23,7 @@ class TestStat extends PHPUnit_Framework_TestCase
         $param = ['time' => time(),'use_type' => 2,'rate' => 30,'amount' => 50,'member_id' => 0];
         stat_helper::onUseBonus($param);
     }
+
     public function testMember()
     {
         $cur_date = new DateTime();
@@ -32,4 +34,34 @@ class TestStat extends PHPUnit_Framework_TestCase
         stat_helper::onDaiyBonus($cur_date->getTimestamp());
         stat_helper::onDaiyOrder($cur_date->getTimestamp());
     }
+
+    public function testSelfDiscount()
+    {
+        stat_helper::onInitSelfDiscount();
+    }
+
+    public function testInviteeDiscount()
+    {
+        stat_helper::onInitInviteesDiscount();
+    }
+    public function testDailySelfDicount()
+    {
+        $cur_date = new DateTime();
+        $inter = new DateInterval('P1D');
+        $cur_date->sub($inter);
+        stat_helper::onDailySelfDiscount($cur_date->getTimestamp());
+    }
+    public function testDailyInviteeDiscount()
+    {
+
+    }
+    private function inviter($mod_member,$member_id)
+    {
+        $members = $mod_member->field('inviter_id')->where(['member_id' => $member_id])->limit(false)->select();
+        if(empty($members)) {
+            return false;
+        }
+
+        return intval($members[0]['member_id']);
+    }
 }