RankDataLogic.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * RankDataLogic.php UTF-8
  4. *
  5. *
  6. * @date : 5/22/2018 5:25 PM
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\data;
  13. use huo\logic\agent\AgentLogic;
  14. use huo\model\common\CommonModel;
  15. use huo\model\data\DayAgentModel;
  16. use huo\model\data\DayGameAgentModel;
  17. use huo\model\data\DayGameModel;
  18. use huo\model\data\DayModel;
  19. use huo\model\game\GameModel;
  20. class RankDataLogic extends CommonModel {
  21. private $sum_field
  22. = [
  23. 'sum(user_cnt)' => 'user_cnt',
  24. 'sum(reg_device_cnt)' => 'reg_device_cnt',
  25. 'sum(reg_cnt)' => 'reg_cnt',
  26. 'sum(pay_user_cnt)' => 'pay_user_cnt',
  27. 'sum(reg_order_cnt)' => 'reg_order_cnt',
  28. 'sum(reg_pay_cnt)' => 'reg_pay_cnt',
  29. 'sum(sum_money)' => 'sum_money',
  30. 'sum(reg_sum_money)' => 'reg_sum_money',
  31. ];
  32. /**
  33. * @param array $param
  34. *
  35. * @return array
  36. */
  37. protected function getWhere($param = []) {
  38. $_map = [];
  39. if (!empty($param['start_time']) && !empty($param['end_time'])) {
  40. $_map['date'] = ['between', [$param['start_time'], $param['end_time']]];
  41. } elseif (!empty($param['start_time'])) {
  42. $_map['date'] = ['egt', $param['start_time']];
  43. } elseif (!empty($param['end_time'])) {
  44. $_map['date'] = ['elt', $param['end_time']];
  45. }
  46. if (!empty($param['app_id'])) {
  47. $_map['app_id'] = $param['app_id'];
  48. }
  49. if (!empty($param['agent_id'])) {
  50. if ($param['agent_id'] == -1) {
  51. $_map['agent_id'] = 0;
  52. } else {
  53. $_map['agent_id'] = $param['agent_id'];
  54. }
  55. }
  56. return $_map;
  57. }
  58. public function getDataModel($where) {
  59. if (isset($where['agent_id']) && isset($where['app_id'])) {
  60. return new DayGameAgentModel();
  61. } elseif (isset($where['agent_id'])) {
  62. return new DayGameAgentModel();
  63. } elseif (isset($where['app_id'])) {
  64. return new DayGameAgentModel();
  65. } else {
  66. return new DayGameModel();
  67. }
  68. }
  69. /**
  70. * 获取组条件
  71. *
  72. * @param array $where
  73. *
  74. * @return string
  75. */
  76. public function getGroup($where) {
  77. if (isset($where['agent_id']) && isset($where['app_id'])) {
  78. $_group = 'app_id,agent_id';
  79. } elseif (isset($where['agent_id'])) {
  80. $_group = 'app_id,agent_id';
  81. } elseif (isset($where['app_id'])) {
  82. $_group = 'app_id,agent_id';
  83. } else {
  84. $_group = 'app_id';
  85. }
  86. return $_group;
  87. }
  88. /**
  89. * 获取关联
  90. *
  91. * @param $where
  92. *
  93. * @return string
  94. */
  95. public function getWith($where) {
  96. if (isset($where['app_id']) && isset($where['agent_id'])) {
  97. $_with = 'agent,game';
  98. } elseif (isset($where['app_id'])) {
  99. $_with = 'agent,game';
  100. } elseif (isset($where['agent_id'])) {
  101. $_with = 'agent,game';
  102. } else {
  103. $_with = 'game';
  104. }
  105. return $_with;
  106. }
  107. /**
  108. * 获取关联
  109. *
  110. * @param $where
  111. *
  112. * @return array
  113. */
  114. public function getField($where) {
  115. $_field = $this->sum_field;
  116. if (isset($where['app_id']) && !isset($where['agent_id'])) {
  117. $_field['app_id'] = 'app_id';
  118. $_field['agent_id'] = 'agent_id';
  119. } elseif (isset($where['app_id'])) {
  120. $_field['agent_id'] = 'agent_id';
  121. $_field['app_id'] = 'app_id';
  122. } elseif (isset($where['agent_id'])) {
  123. $_field['agent_id'] = 'agent_id';
  124. $_field['app_id'] = 'app_id';
  125. } else {
  126. $_field['app_id'] = 'app_id';
  127. }
  128. return $_field;
  129. }
  130. public function getAgentDataModel($where) {
  131. if (isset($where['agent_id']) && isset($where['app_id'])) {
  132. return new DayGameAgentModel();
  133. } elseif (isset($where['agent_id'])) {
  134. return new DayGameAgentModel();
  135. } elseif (isset($where['app_id'])) {
  136. return new DayGameAgentModel();
  137. } else {
  138. return new DayAgentModel();
  139. }
  140. }
  141. /**
  142. * 获取关联
  143. *
  144. * @param $where
  145. *
  146. * @return string
  147. */
  148. public function getAgentWith($where) {
  149. if (isset($where['app_id']) && isset($where['agent_id'])) {
  150. $_with = 'agent,agent.parent,game';
  151. } elseif (isset($where['app_id'])) {
  152. $_with = 'agent,agent.parent,game';
  153. } elseif (isset($where['agent_id'])) {
  154. $_with = 'agent,agent.parent,game';
  155. } else {
  156. $_with = 'agent';
  157. }
  158. return $_with;
  159. }
  160. /**
  161. * 获取关联
  162. *
  163. * @param $where
  164. *
  165. * @return array
  166. */
  167. public function getAgentField($where) {
  168. $_field = $this->sum_field;
  169. if (isset($where['app_id']) && !isset($where['agent_id'])) {
  170. $_field['app_id'] = 'app_id';
  171. $_field['agent_id'] = 'agent_id';
  172. } elseif (isset($where['app_id'])) {
  173. $_field['agent_id'] = 'agent_id';
  174. $_field['app_id'] = 'app_id';
  175. } elseif (isset($where['agent_id'])) {
  176. $_field['agent_id'] = 'agent_id';
  177. $_field['app_id'] = 'app_id';
  178. } else {
  179. $_field['agent_id'] = 'agent_id';
  180. }
  181. return $_field;
  182. }
  183. /**
  184. * 获取组条件
  185. *
  186. * @param array $where
  187. *
  188. * @return string
  189. */
  190. public function getAgentGroup($where) {
  191. if (isset($where['agent_id']) && isset($where['app_id'])) {
  192. $_group = 'app_id,agent_id';
  193. } elseif (isset($where['agent_id'])) {
  194. $_group = 'app_id,agent_id';
  195. } elseif (isset($where['app_id'])) {
  196. $_group = 'app_id,agent_id';
  197. } else {
  198. $_group = 'agent_id';
  199. }
  200. return $_group;
  201. }
  202. /**
  203. * 游戏排行
  204. *
  205. * @param $param
  206. * @param int $_list_rows
  207. * @param string $order
  208. *
  209. * @return \think\Paginator
  210. * @throws \think\exception\DbException
  211. */
  212. public function game($param, $_list_rows = 10, $order = '-sum_money') {
  213. if (!empty($param['game_name'])) {
  214. $_where['name'] = ['like', "{$param['game_name']}%"];
  215. $_app_id = (new GameModel())->getIdsByWhere($_where);
  216. $param['app_id'] = ['in', $_app_id];
  217. }
  218. if (!empty($param['classify'])) {
  219. $_where['classify'] = ['=', "{$param['classify']}"];
  220. $_app_id = (new GameModel())->getIdsByWhere($_where);
  221. $param['app_id'] = ['in', $_app_id];
  222. }
  223. if (!empty($param['rank_by'])) {
  224. $order = '-'.$param['rank_by'];
  225. }
  226. if (!empty($param['agent_id'])) {
  227. if (isset($param['agent_summary'])) {
  228. $_agent_id_names = (new AgentLogic())->getIdNames($param['agent_id'], true);
  229. $_agent_ids = array_keys($_agent_id_names);
  230. $param['agent_id'] = ['in', $_agent_ids];
  231. }
  232. }
  233. $_map = $this->getWhere($param);
  234. // $_filed = $this->sum_field;
  235. // $_filed['app_id'] = 'app_id';
  236. // $_model = new DayGameModel();
  237. $_model = $this->getDataModel($_map);
  238. $_group = $this->getGroup($_map);
  239. $_with = $this->getWith($_map);
  240. $_filed = $this->getField($_map);
  241. $_order = $this->orderFilter($order);
  242. $_items = $_model->with($_with)
  243. ->field($_filed)
  244. ->where($_map)
  245. ->order($_order)
  246. ->group($_group)
  247. ->paginate($_list_rows);
  248. return $_items;
  249. }
  250. public function agent($param, $_list_rows = 10, $order = '-sum_money') {
  251. if (!empty($param['game_name'])) {
  252. $_where['name'] = ['like', "{$param['game_name']}%"];
  253. $_app_id = (new GameModel())->getIdsByWhere($_where);
  254. $param['app_id'] = ['in', $_app_id];
  255. }
  256. if (!empty($param['classify'])) {
  257. $_where['classify'] = ['=', "{$param['classify']}"];
  258. $_app_id = (new GameModel())->getIdsByWhere($_where);
  259. $param['app_id'] = ['in', $_app_id];
  260. }
  261. if (!empty($param['rank_by'])) {
  262. $order = '-'.$param['rank_by'];
  263. }
  264. if (!empty($param['agent_id'])) {
  265. if (isset($param['agent_summary'])) {
  266. $_agent_id_names = (new AgentLogic())->getIdNames($param['agent_id'], true);
  267. $_agent_ids = array_keys($_agent_id_names);
  268. $param['agent_id'] = ['in', $_agent_ids];
  269. }
  270. }
  271. $_map = $this->getWhere($param);
  272. // $_filed = $this->sum_field;
  273. // $_filed['agent_id'] = 'agent_id';
  274. // $_model = new DayAgentModel();
  275. $_model = $this->getAgentDataModel($_map);
  276. $_group = $this->getAgentGroup($_map);
  277. $_with = $this->getAgentWith($_map);
  278. $_filed = $this->getAgentField($_map);
  279. $_order = $this->orderFilter($order);
  280. $_items = $_model->with($_with)
  281. ->field($_filed)
  282. ->where($_map)
  283. ->order($_order)
  284. ->group($_group)
  285. ->paginate($_list_rows);;
  286. return $_items;
  287. }
  288. }