user_bonus.model.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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();
  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. $mine = $this->where(array('type_id' => $type_id,'session_id' => $sess_id))->field($fields)->order('bonus_status,bonus_id')->limit(1)->find();
  106. if(empty($mine))
  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. return $mine;
  125. }
  126. }
  127. private function try_grab($bonus, $type_id, &$is_new_grab, $sess_id, &$is_new_bind)
  128. {
  129. if($bonus['bonus_status'] == 0) {
  130. $is_new_grab = true;
  131. }
  132. $cond = [];
  133. $cond['type_id'] = $type_id;
  134. $cond['bonus_id'] = $bonus['bonus_id'];
  135. $cond['bonus_status'] = $bonus['bonus_status'];
  136. $cond['grab_time'] = $bonus['grab_time'];
  137. $cond['get_time'] = $bonus['get_time'];
  138. $cond['session_id'] = $bonus['session_id'];
  139. if(session_helper::isLogin()) { // 如果是登录状态直接绑定
  140. $datas = array('bonus_status' => 2,
  141. 'grab_time' => time(),
  142. 'get_time' => time(),
  143. 'session_id' => $sess_id,
  144. 'user_id' => $_SESSION['member_id'],
  145. 'user_mobile' => session_helper::cur_mobile(),
  146. 'user_name' => session_helper::nickname());
  147. $is_new_bind = true;
  148. }
  149. else if(session_helper::isVerfiyMobile()) { //如果该会话手机号码曾经认证过,直接绑定
  150. $datas = array('bonus_status' => 2,
  151. 'grab_time' => time(),
  152. 'get_time' => time(),
  153. 'session_id' => $sess_id,
  154. 'user_mobile' => session_helper::cur_mobile());
  155. $is_new_bind = true;
  156. }
  157. else {
  158. $datas = array('bonus_status' => 1,
  159. 'grab_time' => time(),
  160. 'session_id' => $sess_id);
  161. }
  162. try
  163. {
  164. $this->beginTransaction();
  165. $ret = $this->where($cond)->update($datas);
  166. $affect_rows = $this->affected_rows();
  167. $this->commit();
  168. Log::record("user_bonusModel::grab ret={$ret} affected_rows={$affect_rows}",Log::DEBUG);
  169. if($ret != false && $affect_rows > 0) {
  170. $bonus = array_merge($bonus,$datas);
  171. return $bonus;
  172. } else {
  173. return false;
  174. }
  175. }
  176. catch (Exception $ex) {
  177. $this->rollback();
  178. }
  179. }
  180. public function bind($type_id,$bonus_id,$sess_id,$mobile,&$bonus)
  181. {
  182. try
  183. {
  184. $fields = '*';
  185. //如果已经绑定手机
  186. $items = $this->getOwned($mobile,$type_id,$fields);
  187. if(!empty($items)) {
  188. $bonus = $items;
  189. return false;
  190. }
  191. $condition = array('type_id' => $type_id,'bonus_id' => $bonus_id,'session_id' => $sess_id,'bonus_status' => 1);
  192. $datas = array('bonus_status' => 2,
  193. 'user_mobile' => $mobile,
  194. 'get_time' => time());
  195. if(session_helper::isLogin()) {
  196. $datas['user_name'] = session_helper::nickname();
  197. $datas['user_id'] = $_SESSION['member_id'];
  198. }
  199. $this->beginTransaction();
  200. $ret = $this->where($condition)->update($datas);
  201. $affect_rows = $this->affected_rows();
  202. $this->commit();
  203. return ($ret != false && $affect_rows > 0);
  204. } catch (Exception $ex) {
  205. $this->rollback();
  206. return false;
  207. }
  208. }
  209. public function replace($data)
  210. {
  211. $this->insert($data,true);
  212. }
  213. public function replaceAll($datas)
  214. {
  215. $this->insertAll($datas,array(),true);
  216. }
  217. public function getNeedWarn($left_days, $interval_days)
  218. {
  219. if(!isset($left_days) || !isset($interval_days)) {
  220. return false;
  221. }
  222. $day_secs = 24 * 3600;
  223. $left_warn_secs = intval($left_days) * $day_secs;
  224. $period_secs = intval($interval_days) * $day_secs;
  225. $cur_time = time();
  226. $cond = array();
  227. $cond['bonus_status'] = 3;
  228. $cond['usable_time'] = array(array('gt',$cur_time), array('elt',$cur_time + $left_warn_secs),'and');
  229. $cond['notify_time'] = array(array('eq',0), array('elt',$cur_time - $period_secs),'or');
  230. $cond['expired'] = 0;
  231. $cond['remain_amount'] = array('gt','0.00');
  232. $ret = $this->getBonusList($cond,'*','usable_time asc',0,0,1000);
  233. if(empty($ret)) {
  234. return false;
  235. } else {
  236. return $ret;
  237. }
  238. }
  239. public function getExpired()
  240. {
  241. $cond = array('expired' => 0, 'remain_amount' => array('gt','0.00'),'bonus_status' => 3,'usable_time' => array('elt',time()));
  242. $ret = $this->getBonusList($cond,'*','usable_time asc',0,0,1000);
  243. return $ret;
  244. }
  245. }