HourDataLogic.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * HourDataLogic.php UTF-8
  4. * 时表数据处理
  5. *
  6. * @date : 2019/12/11 18:35
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : dengcongshuai <dcs@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huomp\logic\data;
  13. use huo\model\common\CommonModel;
  14. use huo\model\user\UserModel;
  15. use huolib\constant\AgentConst;
  16. use huolib\constant\CommonConst;
  17. use huomp\model\hour\HourAgentModel;
  18. use huomp\model\hour\HourGameAgentModel;
  19. use huomp\model\hour\HourGameModel;
  20. use huomp\model\hour\HourModel;
  21. class HourDataLogic extends CommonModel {
  22. private $base_field
  23. = [
  24. 'date',
  25. 'hour_key',
  26. 'user_cnt',
  27. 'reg_cnt',
  28. 'reg_pay_mem_cnt',
  29. 'reg_sum_money',
  30. ];
  31. private $sum_field
  32. = [
  33. 'date' => 'date',
  34. 'hour_key' => 'hour_key',
  35. 'sum(user_cnt)' => 'user_cnt',
  36. 'sum(reg_cnt)' => 'reg_cnt',
  37. 'sum(reg_pay_mem_cnt)' => 'reg_pay_mem_cnt',
  38. 'sum(reg_sum_money)' => 'reg_sum_money',
  39. ];
  40. /**
  41. * @param array $param
  42. *
  43. * @return array
  44. */
  45. protected function getWhere($param = []) {
  46. $_map = [];
  47. if (!empty($param['start_time']) && !empty($param['end_time'])) {
  48. $_map['date'] = ['between', [$param['start_time'], $param['end_time']]];
  49. } elseif (!empty($param['start_time'])) {
  50. $_map['date'] = ['egt', $param['start_time']];
  51. } elseif (!empty($param['end_time'])) {
  52. $_map['date'] = ['elt', $param['end_time']];
  53. }
  54. $_agent_model = new UserModel();
  55. if (!empty($param['cp_id'])) {
  56. $_ids = $_agent_model->getIdsByCpId(
  57. $param['cp_id'], ['in', [AgentConst::AGENT_ROLE_MP_AGENT, AgentConst::AGENT_ROLE_MP_AD]]
  58. );
  59. if (empty($_ids)) {
  60. $_ids = [-1];
  61. }
  62. $_map['agent_id'] = ['in', $_ids];
  63. }
  64. if (!empty($param['agent_level_1_id'])) {
  65. $_agent_map = [
  66. 'role_id' => ['in', [AgentConst::AGENT_ROLE_MP_AGENT, AgentConst::AGENT_ROLE_MP_AD]]
  67. ];
  68. $_ids = $_agent_model->getIdsByParentId($param['agent_level_1_id'], $_agent_map);
  69. $_ids[] = $param['agent_level_1_id'];
  70. $_map['agent_id'] = ['in', $_ids];
  71. }
  72. if (!empty($param['agent_level_2_id'])) {
  73. $_map['agent_id'] = $param['agent_level_2_id'];
  74. }
  75. if (!empty($param['agent_id'])) {
  76. $_map['agent_id'] = $param['agent_id'];
  77. }
  78. if (!empty($param['app_id'])) {
  79. $_map['app_id'] = $param['app_id'];
  80. }
  81. return $_map;
  82. }
  83. /**
  84. * @param array $where
  85. *
  86. * @return HourModel|HourGameAgentModel|HourGameModel|HourModel|null|\think\Model
  87. */
  88. public function getDataModel($where) {
  89. if (!empty($where['agent_id']) && !empty($where['app_id'])) {
  90. return new HourGameAgentModel();
  91. } elseif (!empty($where['agent_id'])) {
  92. return new HourAgentModel();
  93. } elseif (!empty($where['app_id'])) {
  94. return new HourGameModel();
  95. } else {
  96. return new HourModel();
  97. }
  98. }
  99. /**
  100. * 获取字段
  101. *
  102. * @param $where
  103. *
  104. * @param int $is_summary
  105. *
  106. * @return array
  107. */
  108. public function getField($where, $is_summary = 0) {
  109. if (CommonConst::STATUS_YES == $is_summary) {
  110. $_field = $this->sum_field;
  111. $_field['date'] = 'date';
  112. } else {
  113. $_field = $this->base_field;
  114. }
  115. if (!empty($where['agent_id']) && !empty($where['app_id'])) {
  116. $_field['agent_id'] = 'agent_id';
  117. $_field['app_id'] = 'app_id';
  118. } elseif (!empty($where['agent_id'])) {
  119. $_field['agent_id'] = 'agent_id';
  120. } elseif (!empty($where['app_id'])) {
  121. $_field['app_id'] = 'app_id';
  122. }
  123. return $_field;
  124. }
  125. /**
  126. * 获取with
  127. *
  128. * @param $where
  129. *
  130. * @return string
  131. */
  132. public function getWith($where) {
  133. $_with = '';
  134. if (!empty($where['agent_id']) && !empty($where['app_id'])) {
  135. $_with = 'agent,game';
  136. } elseif (!empty($where['agent_id'])) {
  137. $_with = 'agent';
  138. $_field['agent_id'] = 'agent_id';
  139. } elseif (!empty($where['app_id'])) {
  140. $_with = 'game';
  141. }
  142. return $_with;
  143. }
  144. /**
  145. * 获取列表底层函数
  146. *
  147. * @param array $where 搜索条件
  148. * @param string $page 列表个数
  149. * @param string $order 排序
  150. * @param array $field 附加字段
  151. * @param string $group 归类
  152. * @param string $with
  153. *
  154. * @return array ['count'=>0,'list'=>[]]
  155. */
  156. public function getList($where = [], $page = '1,10', $order = '-date', $field = [], $group = '', $with = '') {
  157. $_map = $where;
  158. $_field = $field;
  159. $_model = $this->getDataModel($_map);
  160. if (!empty($group)) {
  161. $_count = $_model->where($_map)->group($group)->count();
  162. } else {
  163. $_count = $_model->where($_map)->count();
  164. }
  165. if (empty($_count)) {
  166. return [
  167. 'count' => 0,
  168. 'list' => []
  169. ];
  170. }
  171. $_order = $this->orderFilter($order);
  172. if (!empty($group)) {
  173. $_datas = $_model->with($with)
  174. ->field($_field)
  175. ->where($_map)
  176. ->order($_order)
  177. ->group($group)
  178. ->page($page)
  179. ->select();
  180. } else {
  181. $_datas = $_model->with($with)
  182. ->field($_field)
  183. ->where($_map)
  184. ->order($_order)
  185. ->page($page)
  186. ->select();
  187. }
  188. if (is_object($_datas)) {
  189. $_datas = $_datas->toArray();
  190. }
  191. return [
  192. 'count' => $_count,
  193. 'list' => $_datas
  194. ];
  195. }
  196. /**
  197. * 获取汇总数据
  198. *
  199. * @param $where
  200. * @param $sum_file
  201. * @param $group
  202. *
  203. * @return array
  204. */
  205. public function getSumData($where, $sum_file, $group = '') {
  206. $_model = $this->getDataModel($where);
  207. $_map = $where;
  208. $_sum_field = $sum_file;
  209. $_sum_data = $_model
  210. ->field($_sum_field)
  211. ->where($_map)
  212. ->group($group)
  213. ->find();
  214. if (is_object($_sum_data)) {
  215. $_sum_data = $_sum_data->toArray();
  216. }
  217. return $_sum_data;
  218. }
  219. /**
  220. * 获取后台列表
  221. *
  222. * @param array $where 搜索条件
  223. * @param string $page 列表个数
  224. * @param string $order 排序
  225. * @param array $field 附加字段
  226. *
  227. * @return array ['count'=>0,'list'=>[]]
  228. */
  229. public function getAdminList($where = [], $page = '1,10', $order = '-date,-hour_key', $field = []) {
  230. $_map = $this->getWhere($where);
  231. $_field = $this->getField($_map, $where['is_summary']);
  232. $_with = $this->getWith($_map);
  233. if (!empty($field)) {
  234. $_field = array_merge($_field, $field);/* 获取后台字段 */
  235. }
  236. $_group = '';
  237. if (isset($where['include_agent']) && 2 == $where['include_agent']
  238. || isset($where['is_summary'])
  239. && 2 == $where['is_summary']) {
  240. $_group = 'date,hour_key';
  241. }
  242. $_rdata = $this->getList($_map, $page, $order, $_field, $_group, $_with);
  243. if (empty($_rdata['count'])) {
  244. $_sum_data = [];
  245. foreach ($this->sum_field as $_v) {
  246. $_sum_data[$_v] = 0;
  247. }
  248. $_rdata['sum'] = $_sum_data;
  249. return $_rdata;
  250. }
  251. $_datas = $_rdata['list'];
  252. foreach ($_datas as $_k => $_v) {
  253. $_datas[$_k]['game_name'] = '';
  254. $_datas[$_k]['game_icon'] = '';
  255. $_datas[$_k]['classify'] = 0;
  256. if (!empty($_v['game'])) {
  257. $_datas[$_k]['game_name'] = get_val($_v['game'], 'name', '');
  258. $_datas[$_k]['game_icon'] = get_val($_v['game'], 'icon', '');
  259. $_datas[$_k]['classify'] = get_val($_v['game'], 'classify', '');
  260. unset($_datas[$_k]['game']);
  261. }
  262. $_datas[$_k]['agent_nickname'] = '';
  263. $_datas[$_k]['agent_name'] = '';
  264. if (!empty($_v['agent'])) {
  265. $_datas[$_k]['agent_nickname'] = get_val($_v['agent'], 'user_nicename', '');
  266. $_datas[$_k]['agent_name'] = get_val($_v['agent'], 'user_login', '');
  267. unset($_datas[$_k]['agent']);
  268. }
  269. }
  270. $_rdata['list'] = $_datas;
  271. $_sum_filed = $this->sum_field;
  272. $_sum_group = '';
  273. if (!empty($_map['agent_id'])) {
  274. $_sum_group = 'date';
  275. }
  276. $_rdata['sum'] = $this->getSumData($_map, $_sum_filed, $_sum_group);
  277. return $_rdata;
  278. }
  279. }