user_bonus.model.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/2/16
  6. * Time: 下午4:42
  7. */
  8. defined('InShopNC') or exit('Access Invalid!');
  9. class user_bonusModel extends Model
  10. {
  11. const send_bonus = 1;
  12. const grab_bonus = 2;
  13. const TopupState = 3;
  14. public function __construct()
  15. {
  16. parent::__construct('user_bonus');
  17. }
  18. public function get_by_sql($sql) {
  19. return $this->query($sql);
  20. }
  21. public function get($condition,$fields='*')
  22. {
  23. return $this->field($fields)->where($condition)->limit(false)->select();
  24. }
  25. public function getBonusCount($condition = array()) {
  26. return $this->table('user_bonus')->where($condition)->count();
  27. }
  28. public function getBonusList($condition,$field='*', $order = '',$page = 0,$count = 0, $limit = 0, $group = '', $lock = false)
  29. {
  30. return $this->table('user_bonus')->field($field)->where($condition)->group($group)->order($order)->limit($limit)->page($page, $count)->lock($lock)->select();
  31. }
  32. public function getUsableBonus($member_id)
  33. {
  34. $cond = array('user_id' => $member_id,'bonus_status' => 3,'usable_time' => array('gt',time()),'remain_amount' => array('gt','0.00'));
  35. return $this->getBonusList($cond,'*','usable_time asc');
  36. }
  37. public function getUsableSum($member_id)
  38. {
  39. $cond = array('user_id' => $member_id,'bonus_status' => 3,'usable_time' => array('gt',time()));
  40. return $this->table('user_bonus')->where($cond)->sum('remain_amount');
  41. }
  42. public function getSum($cond,$field = 'remain_amount')
  43. {
  44. return $this->table('user_bonus')->where($cond)->sum($field);
  45. }
  46. public function lasted_binded_time($condition) {
  47. $items = $this->where($condition)->field('get_time')->order('get_time desc')->limit(1)->select();
  48. if(count($items) == 1) {
  49. return $items[0]['get_time'];
  50. } else {
  51. return false;
  52. }
  53. }
  54. public function lasted_grabed_time($condition) {
  55. $items = $this->where($condition)->field('grab_time')->order('grab_time desc')->limit(1)->select();
  56. if(count($items) == 1) {
  57. return $items[0]['grab_time'];
  58. } else {
  59. return false;
  60. }
  61. }
  62. public function add($datas) {
  63. return $this->insert($datas);
  64. }
  65. public function getTypeBinded($condition,$fields = '*')
  66. {
  67. $condition['bonus_status'] = array('in',array(2,3));
  68. return $this->where($condition)->field($fields)->order('get_time DESC')->limit(false)->select(array('master' => true));
  69. }
  70. public function getBinded($mobile,$fields = '*') {
  71. return $this->where(array('user_mobile' => $mobile, 'bonus_status' => 2))->field($fields)->order('type_id')->select();
  72. }
  73. public function getStatus($bonus_sn) {
  74. $ret = $this->where(array('bonus_sn' => $bonus_sn))->field('bonus_status')->find();
  75. if(empty($ret)) {
  76. return 0;
  77. } else {
  78. return intval($ret['bonus_status']);
  79. }
  80. }
  81. private function getOwned($mobile,$type_id,$fields = '*') {
  82. return $this->where(array('user_mobile' => $mobile, 'type_id' => $type_id,'bonus_status' => array('in',array(2,3))))->field($fields)->find();
  83. }
  84. public function edit($condition,$datas)
  85. {
  86. return $this->where($condition)->update($datas);
  87. }
  88. public function comment($bonus_id,$comment) {
  89. return $this->where(array('bonus_id' => $bonus_id))->update(array('user_comment' => $comment));
  90. }
  91. //返回单个红包
  92. public function grab($type_id,$time_out,$mobile,$sess_id,&$is_new_grab,&$is_new_bind)
  93. {
  94. $is_new_grab = false;
  95. $is_new_bind = false;
  96. $fields = '*';
  97. //如果已经绑定手机
  98. if(!empty($mobile))
  99. {
  100. $mine = $this->getOwned($mobile,$type_id,$fields);
  101. if(!empty($mine)) {
  102. return $mine;
  103. }
  104. }
  105. $items = $this->where(array('type_id' => $type_id,'session_id' => $sess_id))->field($fields)->order('bonus_status,bonus_id')->limit(1)->select(array('master' => true));
  106. if(empty($items))
  107. {
  108. while(true)
  109. {
  110. $is_new_grab = false;
  111. $is_new_bind = false;
  112. $cond = array( 'type_id' => $type_id,
  113. 'bonus_status' => array('in', array(0,1)),
  114. 'grab_time' => array('lt',time() - $time_out));
  115. $bonuses = $this->where($cond)->field($fields)->order('bonus_status asc,bonus_id asc')->limit(1)->select(array('master' => true));
  116. if(empty($bonuses) || empty($bonuses[0])) return false;
  117. $bonus = $this->try_grab($bonuses[0],$type_id,$is_new_grab,$sess_id,$is_new_bind);
  118. if($bonus != false) {
  119. return $bonus;
  120. }
  121. }
  122. }
  123. else {
  124. $mine = $items[0];
  125. return $mine;
  126. }
  127. }
  128. private function try_grab($bonus, $type_id, &$is_new_grab, $sess_id, &$is_new_bind)
  129. {
  130. if($bonus['bonus_status'] == 0) {
  131. $is_new_grab = true;
  132. }
  133. $cond = [];
  134. $cond['type_id'] = $type_id;
  135. $cond['bonus_id'] = $bonus['bonus_id'];
  136. $cond['bonus_status'] = $bonus['bonus_status'];
  137. $cond['grab_time'] = $bonus['grab_time'];
  138. $cond['get_time'] = $bonus['get_time'];
  139. $cond['session_id'] = $bonus['session_id'];
  140. if(session_helper::isLogin()) { // 如果是登录状态直接绑定
  141. $datas = array('bonus_status' => 2,
  142. 'grab_time' => time(),
  143. 'get_time' => time(),
  144. 'session_id' => $sess_id,
  145. 'user_id' => $_SESSION['member_id'],
  146. 'user_mobile' => session_helper::cur_mobile(),
  147. 'user_name' => session_helper::nickname());
  148. $is_new_bind = true;
  149. }
  150. else if(session_helper::isVerfiyMobile()) { //如果该会话手机号码曾经认证过,直接绑定
  151. $datas = array('bonus_status' => 2,
  152. 'grab_time' => time(),
  153. 'get_time' => time(),
  154. 'session_id' => $sess_id,
  155. 'user_mobile' => session_helper::cur_mobile());
  156. $is_new_bind = true;
  157. }
  158. else {
  159. $datas = array('bonus_status' => 1,
  160. 'grab_time' => time(),
  161. 'session_id' => $sess_id);
  162. }
  163. $ret = $this->where($cond)->update($datas);
  164. $affect_rows = $this->affected_rows();
  165. Log::record("user_bonusModel::grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  166. if($ret != false && $affect_rows > 0) {
  167. $bonus = array_merge($bonus,$datas);
  168. return $bonus;
  169. } else {
  170. return false;
  171. }
  172. }
  173. public function bind($type_id,$bonus_id,$sess_id,$mobile,&$bonus)
  174. {
  175. $fields = '*';
  176. $items = $this->getOwned($mobile, $type_id, $fields);
  177. if (!empty($items)) {
  178. $bonus = $items;
  179. return false;
  180. }
  181. $condition = array('type_id' => $type_id, 'bonus_id' => $bonus_id, 'session_id' => $sess_id, 'bonus_status' => 1);
  182. $datas = array('bonus_status' => 2,
  183. 'user_mobile' => $mobile,
  184. 'get_time' => time());
  185. if (session_helper::isLogin()) {
  186. $datas['user_name'] = session_helper::nickname();
  187. $datas['user_id'] = $_SESSION['member_id'];
  188. }
  189. $ret = $this->where($condition)->update($datas);
  190. $affect_rows = $this->affected_rows();
  191. return ($ret != false && $affect_rows > 0);
  192. }
  193. public function replace($data)
  194. {
  195. $this->insert($data,true);
  196. }
  197. public function replaceAll($datas)
  198. {
  199. $this->insertAll($datas,array(),true);
  200. }
  201. public function getNeedWarn($left_days, $interval_days)
  202. {
  203. if(!isset($left_days) || !isset($interval_days)) {
  204. return false;
  205. }
  206. $day_secs = 24 * 3600;
  207. $left_warn_secs = intval($left_days) * $day_secs;
  208. $period_secs = intval($interval_days) * $day_secs;
  209. $cur_time = time();
  210. $cond = array();
  211. $cond['bonus_status'] = 3;
  212. $cond['usable_time'] = array(array('gt',$cur_time), array('elt',$cur_time + $left_warn_secs),'and');
  213. $cond['notify_time'] = array(array('eq',0), array('elt',$cur_time - $period_secs),'or');
  214. $cond['expired'] = 0;
  215. $cond['remain_amount'] = array('gt','0.00');
  216. $ret = $this->getBonusList($cond,'*','usable_time asc',0,0,1000);
  217. if(empty($ret)) {
  218. return false;
  219. } else {
  220. return $ret;
  221. }
  222. }
  223. public function getExpired()
  224. {
  225. $cond = array('expired' => 0, 'remain_amount' => array('gt','0.00'),'bonus_status' => 3,'usable_time' => array('elt',time()));
  226. $ret = $this->getBonusList($cond,'*','usable_time asc',0,0,1000);
  227. return $ret;
  228. }
  229. }