user_bonus.model.php 9.1 KB

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