bm_promoterModel.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. *
  4. */
  5. class bm_promoterModel extends NewModel
  6. {
  7. static $user_redis;
  8. /**
  9. * [addGameLog description]
  10. * @param [type] $game_log_id 游戏日志id
  11. * @param [type] $result 获胜方下注项
  12. * @param [type] $win_times 赢钱比例
  13. * @param [type] $promoter_times 推广抽成比例
  14. * @param [type] $platform_times 平台抽成比例
  15. */
  16. public function addGameLog($game_log_id, $result, $win_times, $promoter_times, $platform_times)
  17. {
  18. $pre = DB_PREFIX;
  19. $create_time = NOW_TIME;
  20. self::$sql = "INSERT INTO {$pre}bm_promoter_game_log (
  21. user_id,
  22. game_id,
  23. game_log_id,
  24. bm_pid,
  25. sum_bet,
  26. sum_gain,
  27. sum_win,
  28. promoter_gain,
  29. platform_gain,
  30. user_gain,
  31. gain,
  32. create_time
  33. ) SELECT
  34. user_id,
  35. game_id,
  36. {$game_log_id} AS game_log_id,
  37. bm_pid,
  38. sum_bet,
  39. sum_gain,
  40. sum_gain - sum_bet AS sum_win,
  41. FLOOR({$promoter_times} * ABS(sum_gain - sum_bet)) AS promoter_gain,
  42. FLOOR({$platform_times} * ABS(sum_gain - sum_bet)) AS platform_gain,
  43. FLOOR((sum_gain - sum_bet) - (
  44. (FLOOR({$promoter_times} * ABS(sum_gain - sum_bet))+FLOOR({$platform_times} * ABS(sum_gain - sum_bet))) * (sum_gain > sum_bet)
  45. )) AS user_gain,
  46. FLOOR((sum_bet - sum_gain) - (
  47. (FLOOR({$promoter_times} * ABS(sum_gain - sum_bet))+FLOOR({$platform_times} * ABS(sum_gain - sum_bet))) * (sum_bet > sum_gain)
  48. )) AS gain,
  49. {$create_time} AS create_time
  50. FROM
  51. (
  52. SELECT
  53. l.user_id,
  54. g.game_id,
  55. u.bm_pid,
  56. SUM(l.money*(l.type = 1)) AS sum_bet,
  57. SUM(l.money *(l.bet = {$result})) * {$win_times} AS sum_gain
  58. FROM
  59. {$pre}game_log AS g,
  60. {$pre}user_game_log AS l,
  61. {$pre}bm_promoter AS p,
  62. {$pre}user AS u
  63. WHERE
  64. u.id = l.user_id
  65. AND u.bm_pid = p.user_id
  66. AND l.game_log_id = g.id
  67. AND l.game_log_id = {$game_log_id}
  68. GROUP BY
  69. l.user_id
  70. ) AS a";
  71. return Connect::exec(self::$sql);
  72. }
  73. public static function checkFile()
  74. {
  75. if (!self::$user_redis) {
  76. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  77. self::$user_redis = new UserRedisService();
  78. }
  79. }
  80. public function payPromoter()
  81. {
  82. self::checkFile();
  83. $model = self::build('bm_promoter_game_log');
  84. $bmp_model = self::build('bm_promoter');
  85. $list = $model->select(['is_count' => 0]);
  86. foreach ($list as $value) {
  87. $game_log_id = $value['game_log_id'];
  88. $user_id = $value['gain'] > 0 ? $value['bm_pid'] : $value['user_id'];
  89. //lym 修正 当 推广商赢时候,重复扣流水当问题
  90. if($user_id==$value['bm_pid']){
  91. $value['gain'] = $value['gain'] + $value['promoter_gain'] + $value['platform_gain'];
  92. }
  93. self::payCoin($value['bm_pid'], $game_log_id, $value['gain'], '推广商收支');
  94. self::payCoin($user_id, $game_log_id, -$value['promoter_gain'], '推广中心抽成');
  95. self::payCoin($user_id, $game_log_id, -$value['platform_gain'], '平台抽成');
  96. $bmp = $bmp_model->field('pid')->selectOne(['user_id' => $value['bm_pid']]);
  97. if ($bmp) {
  98. self::payCoin($bmp['pid'], $game_log_id, $value['promoter_gain'], '推广中心抽成收入');
  99. }
  100. $model->update(['is_count' => 1], ['id' => $value['id']]);
  101. }
  102. }
  103. public static function payCoin($user_id, $game_log_id, $coin, $des)
  104. {
  105. self::checkFile();
  106. $user_model = self::build('user');
  107. $res = $user_model->update(array('coin' => array('coin + ' . $coin)), ['id' => $user_id]);
  108. if ($res) {
  109. self::$user_redis->inc_field($user_id, 'coin', $coin);
  110. $account_diamonds = $user_model->coin($user_id);
  111. $res = self::build('coin_log')->addLog($user_id, $game_log_id, $coin, $account_diamonds, $des);
  112. }
  113. }
  114. public function changePid($pid, $to_pid)
  115. {
  116. return self::build('user')->update(['bm_pid' => $to_pid], ['bm_pid' => $pid, 'is_robot' => 0]);
  117. }
  118. public function update_promoter_child($user_id, $is_pommoter = 0)
  119. {
  120. if ($is_pommoter == 1) {
  121. $GLOBALS['db']->query("update " . DB_PREFIX . "bm_promoter set child_count=(SELECT temp.id from (SELECT COUNT(id) as id from " . DB_PREFIX . "bm_promoter where pid=" . intval($user_id) . " and is_effect=1 and status=1)temp ) where user_id= " . intval($user_id) . "");
  122. } else {
  123. $GLOBALS['db']->query("update " . DB_PREFIX . "bm_promoter set child_count=(SELECT COUNT(id) as id from " . DB_PREFIX . "user where bm_pid=" . intval($user_id) . " and is_effect=1 ) where user_id= " . intval($user_id) . "");
  124. }
  125. }
  126. public function isPromoter($user_id)
  127. {
  128. return !!self::build('bm_promoter')->selectOne(['user_id'=>intval($user_id),'status'=>1]);
  129. }
  130. }