game_distributionModel.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. *
  4. */
  5. class game_distributionModel extends NewModel
  6. {
  7. /**
  8. * 获取上级用户信息
  9. * @param integer $user_id 用户id
  10. * @return array ['id', 'nick_name', 'head_image']
  11. */
  12. public function getParent($user_id)
  13. {
  14. $parent = null;
  15. $model = self::build('user');
  16. $user = $model->field('game_distribution_id')->selectOne(['id' => $user_id]);
  17. if ($user['game_distribution_id']) {
  18. $parent = $model->field('id', 'nick_name', 'head_image')->selectOne(['id' => $user['game_distribution_id']]);
  19. if ($parent) {
  20. $parent['head_image'] = get_spec_image($parent['head_image']);
  21. }
  22. }
  23. return $parent;
  24. }
  25. /**
  26. * 获取分销列表
  27. * @param integer $user_id 用户id
  28. * @param integer $page 分页页数
  29. * @param integer $page_size 分页大小
  30. * @return array 分销列表
  31. */
  32. public function getDistributionList($user_id, $page, $page_size = 20)
  33. {
  34. $page = $page > 0 ? $page : 1;
  35. $user_id = intval($user_id);
  36. $field = [
  37. 'u.id',
  38. 'u.nick_name',
  39. 'u.head_image',
  40. 'gd.is_ticket',
  41. ["sum(gd.first_distreibution_money * (gd.first_distreibution_id = {$user_id}) + gd.second_distreibution_money * (gd.second_distreibution_id = {$user_id})) as `sum`"],
  42. ];
  43. $table = ['user u', 'game_distribution gd'];
  44. $where = [
  45. [
  46. 'gd.first_distreibution_id' => ['=', $user_id],
  47. 'gd.second_distreibution_id' => ['=', $user_id, 'or'],
  48. ],
  49. 'u.id' => ['gd.user_id'],
  50. ];
  51. $list = $this->table($table)->field($field)->group('u.id,gd.is_ticket')->limit(($page - 1) * $page_size, $page_size)->select($where);
  52. $count = $this->table($table)->group('u.id')->count($where);
  53. foreach ($list as $key => $value) {
  54. $list[$key]['head_image'] = get_spec_image($value['head_image']);
  55. unset($list[$key]['is_ticket']);
  56. $list[$key]['sum'] = $value['sum'] . ($value['is_ticket'] ? '印票' : '游戏币');
  57. }
  58. $data = [
  59. 'parent' => $this->getParent($user_id),
  60. 'list' => $list,
  61. 'page' => [
  62. 'page' => $page,
  63. 'has_next' => intval($count > ($page * $page_size)),
  64. ],
  65. ];
  66. if (!$data['parent']) {
  67. unset($data['parent']);
  68. }
  69. return $data;
  70. }
  71. public function addLog($user_id, $room_id, $game_log_id, $money, $dec, $is_ticket = false)
  72. {
  73. $first_distreibution_id = 0;
  74. $first_distreibution_money = 0;
  75. $second_distreibution_id = 0;
  76. $second_distreibution_money = 0;
  77. $m_config = load_auto_cache("m_config");
  78. $model = self::build('user');
  79. $user = $model->field('game_distribution_id')->selectOne(['id' => $user_id]);
  80. if ($user['game_distribution_id']) {
  81. $first_distreibution_id = intval($user['game_distribution_id']);
  82. $first_distreibution = $model->field('game_distribution_id,game_distribution1')->selectOne(['id' => $first_distreibution_id]);
  83. $first_distreibution_money = intval($money / 100 * ($first_distreibution['game_distribution1'] ? $first_distreibution['game_distribution1'] : $m_config['game_distribution1']));
  84. if ($first_distreibution['game_distribution_id']) {
  85. $second_distreibution_id = intval($first_distreibution['game_distribution_id']);
  86. $second_distreibution = $model->field('game_distribution_id,game_distribution2')->selectOne(['id' => $second_distreibution_id]);
  87. $second_distreibution_money = intval($money / 100 * ($second_distreibution['game_distribution2'] ? $second_distreibution['game_distribution2'] : $m_config['game_distribution2']));
  88. }
  89. }
  90. $distreibution_money = $money - $first_distreibution_money - $second_distreibution_money;
  91. $create_time = NOW_TIME;
  92. $is_ticket = intval(defined('OPEN_DIAMOND_GAME_MODULE') && OPEN_DIAMOND_GAME_MODULE == 1 || $is_ticket);
  93. $res = $this->insert(compact('room_id', 'game_log_id', 'money', 'user_id', 'distreibution_money', 'first_distreibution_id', 'first_distreibution_money', 'second_distreibution_id', 'second_distreibution_money', 'dec', 'create_time', 'is_ticket'));
  94. if (!$res) {
  95. return false;
  96. }
  97. if ($first_distreibution_money + $second_distreibution_money) {
  98. $coin = -$first_distreibution_money - $second_distreibution_money;
  99. if ($is_ticket) {
  100. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/BaseRedisService.php');
  101. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  102. $user_redis = new UserRedisService();
  103. $log_model = self::build('user_log');
  104. $res = $model->update(['ticket' => ['ticket + ' . $coin]], ['id' => $user_id]);
  105. if ($res) {
  106. $user_redis->inc_field($user_id, 'ticket', $coin);
  107. $log_data = [
  108. 'log_info' => $dec . '抽成(上交)',
  109. 'log_time' => NOW_TIME,
  110. 'log_admin_id' => 0,
  111. 'money' => 0,
  112. 'user_id' => $user_id,
  113. 'type' => 7,
  114. 'prop_id' => 0,
  115. 'score' => 0,
  116. 'point' => 0,
  117. 'podcast_id' => $user_id,
  118. 'diamonds' => 0,
  119. 'ticket' => $coin,
  120. 'video_id' => $room_id,
  121. ];
  122. $log_model->insert($log_data);
  123. }
  124. if ($first_distreibution_money) {
  125. $res = $model->update(['ticket' => ['ticket + ' . $first_distreibution_money]], ['id' => $first_distreibution_id]);
  126. if ($res) {
  127. $user_redis->inc_field($first_distreibution_id, 'ticket', $first_distreibution_money);
  128. $log_data = [
  129. 'log_info' => $dec . '一级抽成(抽取)',
  130. 'log_time' => NOW_TIME,
  131. 'log_admin_id' => 0,
  132. 'money' => 0,
  133. 'user_id' => $first_distreibution_id,
  134. 'type' => 7,
  135. 'prop_id' => 0,
  136. 'score' => 0,
  137. 'point' => 0,
  138. 'podcast_id' => $user_id,
  139. 'diamonds' => 0,
  140. 'ticket' => $first_distreibution_money,
  141. 'video_id' => $room_id,
  142. ];
  143. $log_model->insert($log_data);
  144. }
  145. }
  146. if ($second_distreibution_money) {
  147. $res = $model->update(['ticket' => ['ticket + ' . $second_distreibution_money]], ['id' => $second_distreibution_id]);
  148. if ($res) {
  149. $user_redis->inc_field($second_distreibution_id, 'ticket', $second_distreibution_money);
  150. $log_data = [
  151. 'log_info' => $dec . '二级抽成(抽取)',
  152. 'log_time' => NOW_TIME,
  153. 'log_admin_id' => 0,
  154. 'money' => 0,
  155. 'user_id' => $second_distreibution_id,
  156. 'type' => 7,
  157. 'prop_id' => 0,
  158. 'score' => 0,
  159. 'point' => 0,
  160. 'podcast_id' => $user_id,
  161. 'diamonds' => 0,
  162. 'ticket' => $second_distreibution_money,
  163. 'video_id' => $room_id,
  164. ];
  165. $log_model->insert($log_data);
  166. }
  167. }
  168. } else {
  169. $coin_log_model = self::build('coin_log');
  170. $user_model = self::build('user');
  171. $res = $user_model->coin($user_id, $coin);
  172. $account_diamonds = $user_model->coin($user_id);
  173. if ($res) {
  174. $coin_log_model->addLog($user_id, $game_log_id, $coin, $account_diamonds, $dec . '抽成(上交)');
  175. }
  176. if ($first_distreibution_money) {
  177. $res = $user_model->coin($first_distreibution_id, $first_distreibution_money);
  178. $account_diamonds = $user_model->coin($first_distreibution_id);
  179. if ($res) {
  180. $coin_log_model->addLog($first_distreibution_id, $game_log_id, $first_distreibution_money, $account_diamonds, $dec . '一级抽成(抽取)');
  181. }
  182. }
  183. if ($second_distreibution_money) {
  184. $res = $user_model->coin($second_distreibution_id, $second_distreibution_money);
  185. $account_diamonds = $user_model->coin($second_distreibution_id);
  186. if ($res) {
  187. $coin_log_model->addLog($second_distreibution_id, $game_log_id, $second_distreibution_money, $account_diamonds, $dec . '二级抽成(抽取)');
  188. }
  189. }
  190. }
  191. }
  192. return $distreibution_money;
  193. }
  194. /**
  195. * 下级礼物明细及汇总
  196. * @param [type] $user_id [description]
  197. * @param string $where [description]
  198. * @param int $date [description]
  199. * @return array
  200. * [
  201. * 'sum_first' => 一级分销人数,
  202. * 'sum_second' => 二级分销人数,
  203. * 'sum_child' => 三级以上分销人数,
  204. * 'total_diamonds' => 总礼物金额,
  205. * 'list' => [
  206. * 'user_id' => 用户id,
  207. * 'nick_name' => 昵称,
  208. * 'game_log_id' => 游戏id,
  209. * 'total_diamonds'=> 分销金额,
  210. * 'create_time' => 创建时间,
  211. * ],
  212. * ]
  213. */
  214. public function childProp($user_id, $where = '', $date = false, $is_group = true)
  215. {
  216. if ($date === false) {
  217. $date = NOW_TIME;
  218. }
  219. $table = 'video_prop_' . to_date($date, 'Ym') . ' l';
  220. $user_id = intval($user_id);
  221. $model = self::build('user');
  222. $first = $model->getChild($user_id);
  223. $second = $model->getChild($first);
  224. $sum_first = sizeof($first);
  225. $sum_second = sizeof($second);
  226. $user_d = $model->field('id', 'game_distribution_top_id')->selectOne(['id' => $user_id]);
  227. $where['d.game_distribution_top_id'] = $user_d['game_distribution_top_id'];
  228. $where['l.from_user_id'] = ['d.id'];
  229. if ($user_d['id'] == $user_d['game_distribution_top_id']) {
  230. $sum_child = $model->count(['game_distribution_top_id' => $user_d['game_distribution_top_id']]);
  231. } else {
  232. if (!isset($where['d.id'])) {
  233. $ids = array_merge($first, $second);
  234. if ($ids) {
  235. $where['d.id'] = ['in', $ids];
  236. } else {
  237. return [
  238. 'sum_first' => 0,
  239. 'sum_second' => 0,
  240. 'sum_child' => 0,
  241. 'total_diamonds' => 0,
  242. 'list' => [
  243. ],
  244. ];
  245. }
  246. }
  247. }
  248. $sum = $this->table('user d', $table)->field([['SUM(total_diamonds) total_diamonds']])->selectOne($where);
  249. $total_diamonds = intval($sum['total_diamonds']);
  250. if ($is_group) {
  251. $field = [
  252. 'l.from_user_id user_id',
  253. 'l.to_user_id',
  254. 'd.nick_name',
  255. ['SUM(l.total_diamonds) total_diamonds'],
  256. 'l.create_time',
  257. ];
  258. $this->group('l.from_user_id');
  259. } else {
  260. $field = [
  261. 'l.from_user_id user_id',
  262. 'l.to_user_id',
  263. 'd.nick_name',
  264. 'l.total_diamonds',
  265. 'l.create_time',
  266. ];
  267. }
  268. $list = $this->table('user d', $table)->field($field)->select($where);
  269. return compact('sum_first', 'sum_second', 'sum_child', 'total_diamonds', 'list');
  270. }
  271. /**
  272. * 下级游戏明细及汇总
  273. * @param [type] $user_id [description]
  274. * @return array
  275. * [
  276. * 'sum_first' => 一级分销人数,
  277. * 'sum_second' => 二级分销人数,
  278. * 'sum_child' => 三级以上分销人数,
  279. * 'sum_bet' => 总下注金额,
  280. * 'sum_gain' => 总收益金额,
  281. * 'list' => [
  282. * 'user_id' => 用户id,
  283. * 'nick_name' => 昵称,
  284. * 'game_log_id' => 游戏id,
  285. * 'bet' => 下注金额,
  286. * 'gain' => 收益,
  287. * 'create_time' => 创建时间,
  288. * ],
  289. * ]
  290. */
  291. public function childGame($user_id, $where = '', $is_group = true)
  292. {
  293. $user_id = intval($user_id);
  294. $model = self::build('user');
  295. $first = $model->getChild($user_id);
  296. $second = $model->getChild($first);
  297. $sum_first = sizeof($first);
  298. $sum_second = sizeof($second);
  299. $user_d = $model->field('id', 'game_distribution_top_id')->selectOne(['id' => $user_id]);
  300. $where['d.game_distribution_top_id'] = $user_d['game_distribution_top_id'];
  301. $where['l.user_id'] = ['d.id'];
  302. if ($user_d['id'] == $user_d['game_distribution_top_id']) {
  303. $sum_child = $model->count(['game_distribution_top_id' => $user_d['game_distribution_top_id']]);
  304. } else {
  305. if (!isset($where['d.id'])) {
  306. $ids = array_merge($first, $second);
  307. if ($ids) {
  308. $where['d.id'] = ['in', $ids];
  309. } else {
  310. return [
  311. 'sum_first' => 0,
  312. 'sum_second' => 0,
  313. 'sum_child' => 0,
  314. 'sum_bet' => 0,
  315. 'sum_gain' => 0,
  316. 'list' => [
  317. ],
  318. ];
  319. }
  320. }
  321. }
  322. $sum = $this->table('user d', 'user_game_log_history l')->field(['SUM(l.money *(l.game_log_id>0)) sum_bet'], ['SUM(l.money *(l.game_log_id>0)) sum_gain'])->selectOne($where);
  323. $sum_bet = intval($sum['sum_bet']);
  324. $sum_gain = intval($sum['sum_gain']);
  325. $field = [
  326. 'l.user_id',
  327. 'd.nick_name',
  328. 'l.game_log_id',
  329. ['SUM(l.money *(l.game_log_id=0)) bet'],
  330. ['SUM(l.money *(l.game_log_id>0)) gain'],
  331. 'l.create_time',
  332. ];
  333. if ($is_group) {
  334. $this->group('l.user_id');
  335. } else {
  336. $this->group('l.game_log_id');
  337. }
  338. $list = $this->table('user d', 'user_game_log_history l')->field($field)->select($where);
  339. return compact('sum_first', 'sum_second', 'sum_child', 'sum_bet', 'sum_gain', 'list');
  340. }
  341. /**
  342. * 下级分销明细及汇总
  343. * @param [type] $user_id [description]
  344. * @return array
  345. * [
  346. * 'sum_first' => 一级分销人数,
  347. * 'sum_second' => 二级分销人数,
  348. * 'sum_child' => 三级以上分销人数,
  349. * 'sum_money' => 总金额,
  350. * 'sum_distribution' => 总分销金额,
  351. * 'list' => [
  352. * 'user_id' => 用户id,
  353. * 'nick_name' => 昵称,
  354. * 'game_log_id' => 游戏id,
  355. * 'money' => 总金额,
  356. * 'distreibution_money' => 分销金额,
  357. * 'first_distreibution_money' => 一级分销金额,
  358. * 'second_distreibution_money' => 二级分销金额,
  359. * 'create_time' => 创建时间,
  360. * ],
  361. * ]
  362. */
  363. public function childDistribution($user_id, $where = '', $is_group = true)
  364. {
  365. $user_id = intval($user_id);
  366. $model = self::build('user');
  367. $first = $model->getChild($user_id);
  368. $second = $model->getChild($first);
  369. $sum_first = sizeof($first);
  370. $sum_second = sizeof($second);
  371. $user_d = $model->field('id', 'game_distribution_top_id')->selectOne(['id' => $user_id]);
  372. $where['d.game_distribution_top_id'] = $user_d['game_distribution_top_id'];
  373. $where['l.user_id'] = ['d.id'];
  374. if ($user_d['id'] == $user_d['game_distribution_top_id']) {
  375. $sum_child = $model->count(['game_distribution_top_id' => $user_d['game_distribution_top_id']]);
  376. $first_field = 'l.first_distreibution_money';
  377. $second_field = 'l.second_distreibution_money';
  378. } else {
  379. $where[] = [
  380. 'l.first_distreibution_id' => $user_id,
  381. 'l.second_distreibution_id' => ['=', $user_id, 'or'],
  382. ];
  383. $first_field = "l.first_distreibution_money*(l.first_distreibution_id={$user_id})";
  384. $second_field = "l.second_distreibution_money*(l.second_distreibution_id={$user_id})";
  385. }
  386. $sum = $this->table('user d', 'game_distribution l')->field(
  387. ['sum(l.money) sum_money'],
  388. ["sum({$first_field}+{$second_field}) sum_distribution"]
  389. )->selectOne($where);
  390. $sum_money = intval($sum['sum_money']);
  391. $sum_distribution = intval($sum['sum_distribution']);
  392. if ($is_group) {
  393. $field = [
  394. 'l.user_id',
  395. 'd.nick_name',
  396. ['0 as game_log_id'],
  397. ['sum(l.money) as money'],
  398. ['sum(l.distreibution_money) as distreibution_money'],
  399. ["sum({$first_field}) as first_distreibution_money"],
  400. ["sum({$second_field}) as second_distreibution_money"],
  401. [strtotime(date('Y-m-01')) . ' as create_time'],
  402. ];
  403. $this->group('l.user_id');
  404. } else {
  405. $field = [
  406. 'l.user_id',
  407. 'd.nick_name',
  408. 'l.game_log_id',
  409. 'l.money',
  410. 'l.distreibution_money',
  411. 'l.first_distreibution_money',
  412. 'l.second_distreibution_money',
  413. 'l.create_time',
  414. ];
  415. }
  416. $list = $this->table('user d', 'game_distribution l')->field($field)->select($where);
  417. return compact('sum_first', 'sum_second', 'sum_child', 'sum_money', 'sum_distribution', 'list');
  418. }
  419. /**
  420. * [childPropDistribution description]
  421. * @param [type] $user_id [description]
  422. * @param string $where [description]
  423. * @return [type] [description]
  424. */
  425. public function childPropDistribution($user_id, $where = '', $is_group = true)
  426. {
  427. $where['l.game_log_id'] = 0;
  428. return $this->childDistribution($user_id, $where, $is_group);
  429. }
  430. /**
  431. * [childGameDistribution description]
  432. * @param [type] $user_id [description]
  433. * @param string $where [description]
  434. * @return [type] [description]
  435. */
  436. public function childGameDistribution($user_id, $where = '', $is_group = true)
  437. {
  438. $where['l.game_log_id'] = ['<>', 0];
  439. return $this->childDistribution($user_id, $where, $is_group);
  440. }
  441. }