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