weixin_distribution_logModel.class.php 18 KB

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