Преглед на файлове

review refund bonus code

stanley-king преди 9 години
родител
ревизия
22544a3be2
променени са 2 файла, в които са добавени 34 реда и са изтрити 49 реда
  1. 2 4
      crontab/control/hour.php
  2. 32 45
      data/model/bonus_type.model.php

+ 2 - 4
crontab/control/hour.php

@@ -244,11 +244,9 @@ class hourControl extends BaseCronControl {
     /**
      * 定时释放红包
      */
-    public function releaseBonusOp() {
-
+    public function releaseBonusOp()
+    {
         $bonus_type = Model('bonus_type');
         $bonus_type->bonus_refund();
     }
-
-
 }

+ 32 - 45
data/model/bonus_type.model.php

@@ -45,76 +45,63 @@ class bonus_typeModel extends Model
     }
 
     /**
-     * 获取已过期的且有没抢的红包金额的红包列表
+     * 获取已过期的余额列表
      */
-    public function get_endbonus_list(){
-
-        $condition = array(
-            'remain_amount' => array('gt',0),
-            'is_refund' => 0,
-            'send_end_date' =>array('lt',time()-60*60)
-        );
-
-       return $this->where($condition)->field('type_id,type_sn,sender_id,sender_name,remain_amount')->select();
+    private function get_endbonus_list()
+    {
+        $one_hour = 60 * 60;
+        $condition = array('remain_amount' => array('gt',0),'is_refund' => 0,'send_end_date' =>array('lt',time() - $one_hour));
+        return $this->where($condition)->field('type_id,type_sn,sender_id,sender_name,remain_amount')->limit(false)->select();
     }
 
     /**
      * 红包退款
      * @return bool
      */
-    public function bonus_refund(){
-        //获取红包数据
+    public function bonus_refund()
+    {
         $results = $this->get_endbonus_list();
-
         if(empty($results)) {
             return false;
         }
 
         $model_predeposit = Model('predeposit');
-
-        foreach($results as $result) {
+        foreach($results as $result)
+        {
             Model::beginTransaction();
-            try {
-                //退款
+            try
+            {
+                //将没抢完的钱退还到用户余额
+                $log_data = array();
                 $log_data['member_id']   = $result['sender_id'];
                 $log_data['member_name'] = $result['sender_name'];
                 $log_data['amount'] = $result['remain_amount'];
                 $log_data['type_sn'] = $result['type_sn'];
-                $model_predeposit->changePd('bonus_refund',$log_data);
-
-                //更新状态
-                $condition = array(
-                    'type_id' =>$result['type_id'],
-                );
 
-                $update = array(
-                    'is_refund' =>1,
-                    'refund_time' => time(),
-                );
-
-                $this->where($condition)->update($update);
+                $model_predeposit->changePd('bonus_refund',$log_data);
 
+                //更新红包状态
+                $this->where(array('type_id' =>$result['type_id']))->update(array('is_refund' => 1,'refund_time' => time()));
                 if ($this->affected_rows() <= 0) {
                     Model::rollback();
-                    Log::record('bonus refund 更新状态失败,result:'.json_encode($result));
-                    continue;
-                }
-
-                //删除数据
-                $condition = array(
-                    'type_id' =>$result['type_id'],
-                    'bonus_status' => array('in','0,1'),
-                );
-
-                Model('user_bonus')->where($condition)->delete();
 
-                Model::commit();
-
-            } catch (Exception $e) {
+                    $sresult = implode($result);
+                    Log::record("bonus refund 更新状态失败,result:{$sresult}.");
+                } else {
+                    $condition = array('type_id' =>$result['type_id'],'bonus_status' => array('in','0,1'));
+                    Model('user_bonus')->where($condition)->delete();
+                    Model::commit();
+                }
+            }
+            catch (Exception $e)
+            {
                 Model::rollback();
-                Log::record('bonus refund : error:'.json_encode($e->getMessage()).'  result:'.json_encode($result));
+
+                $sresult = implode($result);
+                Log::record('bonus refund : error:' . $e->getMessage() . " result:{$sresult}.");
             }
         }
-    }
 
+        return true;
+    }
 }