userModel.class.php 4.4 KB

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