userModel.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. *
  4. */
  5. class userModel extends NewModel
  6. {
  7. public function getOneById($id, $field = '')
  8. {
  9. return $this->field($field)->selectOne(array('id' => intval($id)));
  10. }
  11. public function coin($user_id, $coin = false, $coin_field = false)
  12. {
  13. if ($coin_field == false) {
  14. $open_game = defined('OPEN_GAME_MODULE') && (OPEN_GAME_MODULE == 1);
  15. $diamond = defined('OPEN_DIAMOND_GAME_MODULE') && (OPEN_DIAMOND_GAME_MODULE == 1);
  16. $coin_field = ($open_game && !$diamond) ? 'coin' : 'diamonds';
  17. }
  18. $user_id = intval($user_id);
  19. if (!$user_id) {
  20. return false;
  21. }
  22. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  23. $user_redis = new UserRedisService();
  24. if ($coin === false) {
  25. return intval($user_redis->getOne_db($user_id, $coin_field));
  26. } else {
  27. $coin = intval($coin);
  28. $where = array('id' => $user_id);
  29. if ($coin < 0) {
  30. $where[$coin_field] = array('>=', -$coin);
  31. }
  32. $res = $this->update(array($coin_field => array($coin_field . ' + ' . $coin)), $where);
  33. if ($res) {
  34. $user_redis->inc_field($user_id, $coin_field, $coin);
  35. }
  36. return $res;
  37. }
  38. }
  39. public function multiAddCoin($game_log_id, $result, $times)
  40. {
  41. $coin_field = 'coin';
  42. if (defined('OPEN_DIAMOND_GAME_MODULE') && OPEN_DIAMOND_GAME_MODULE == 1) {
  43. $coin_field = 'diamonds';
  44. }
  45. $table = DB_PREFIX . 'user_game_log';
  46. $table_user = DB_PREFIX . 'user';
  47. self::$sql =
  48. "UPDATE $table_user AS a
  49. INNER JOIN (
  50. SELECT
  51. SUM(`money`) AS `win`,
  52. `user_id`
  53. FROM
  54. $table
  55. WHERE
  56. `game_log_id` = $game_log_id
  57. AND `bet` = $result
  58. GROUP BY
  59. `user_id`
  60. ) AS b ON a.id = b.user_id
  61. SET a.{$coin_field} = a.{$coin_field} + b.win * $times";
  62. return Connect::exec(self::$sql);
  63. }
  64. public function returnCoin($game_log_id)
  65. {
  66. $coin_field = 'coin';
  67. if (defined('OPEN_DIAMOND_GAME_MODULE') && OPEN_DIAMOND_GAME_MODULE == 1) {
  68. $coin_field = 'diamonds';
  69. }
  70. $table = DB_PREFIX . 'user_game_log';
  71. $table_user = DB_PREFIX . 'user';
  72. self::$sql =
  73. "UPDATE $table_user a
  74. INNER JOIN (
  75. SELECT
  76. SUM(`money`) AS `win`,
  77. `user_id`
  78. FROM
  79. $table
  80. WHERE
  81. `game_log_id` = $game_log_id
  82. GROUP BY
  83. `user_id`
  84. ) AS b ON a.id = b.user_id
  85. SET a.{$coin_field} = a.{$coin_field} + b.win";
  86. return Connect::exec(self::$sql);
  87. }
  88. /**
  89. * 获取邀请码
  90. * @param [type] $user_id [description]
  91. * @return [type] [description]
  92. */
  93. public function getInvitationCode($user_id)
  94. {
  95. $user_id = intval($user_id);
  96. $user = $this->field('create_time,invitation_code')->selectOne(['id' => $user_id]);
  97. if (!$user) {
  98. return false;
  99. }
  100. if ($user['invitation_code']) {
  101. return $user['invitation_code'];
  102. }
  103. $users = $this->field('create_time,id')->select(['invitation_code' => '']);
  104. foreach ($users as $v) {
  105. $this->update(['invitation_code' => substr(md5($v['id'] . ':' . $v['create_time']), -16)], ['id' => $v['id']]);
  106. }
  107. return substr(md5($user_id . ':' . $user['create_time']), -16);
  108. }
  109. public function getInvitationId($user_id)
  110. {
  111. $user_id = intval($user_id);
  112. $user = $this->field('bm_pid')->selectOne(['id' => $user_id]);
  113. if ($user['bm_pid']) {
  114. return intval($user['bm_pid']);
  115. }
  116. return false;
  117. }
  118. /**
  119. * 通过邀请码设置邀请人
  120. * @param [type] $user_id [description]
  121. * @param [type] $qrcode_sn [description]
  122. */
  123. public function getInvitationBycode($qrcode_sn)
  124. {
  125. $user = $this->table('bm_qrcode')->field('id bm_qrcode_id,user_id bm_pid,promoter_id bm_promoter_id')->selectOne(['qrcode_sn' => $qrcode_sn, 'is_effect' => 1]);
  126. if (!$user) {
  127. return false;
  128. }
  129. return $user;
  130. }
  131. public function getChild($pid, $field = false)
  132. {
  133. if (!$pid) {
  134. return [];
  135. }
  136. if (is_array($pid)) {
  137. $where = ['game_distribution_id' => ['in', $pid]];
  138. } else {
  139. $where = ['game_distribution_id' => intval($pid)];
  140. }
  141. if ($field === false) {
  142. $child = $this->field('id')->select($where);
  143. $array = [];
  144. foreach ($child as $value) {
  145. $array[] = intval($value['id']);
  146. }
  147. return $array;
  148. } else {
  149. return $this->field($field)->select($where);
  150. }
  151. }
  152. }