StatisticsSwitchLogic.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /**
  3. * StatisticsSwitchLogic.php UTF-8
  4. * 汇总切量数据
  5. *
  6. * @date : 2019/11/7 15:08
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOUNION 8.5
  11. */
  12. namespace huo\logic\data;
  13. use huo\controller\common\CommonFunc;
  14. use huo\logic\agent\AgentLogic;
  15. use huo\logic\member\MemberLogic;
  16. use huo\model\common\CommonModel;
  17. use huo\model\game\GameModel;
  18. use huo\model\log\MemLoginLogModel;
  19. use huo\model\log\MemLoginLogSwitchModel;
  20. use huo\model\member\MemberModel;
  21. use huo\model\member\MemoauthModel;
  22. use huo\model\member\MgRoleModel;
  23. use huo\model\order\OrderModel;
  24. use huo\model\user\UserModel;
  25. use huolib\constant\CacheConst;
  26. use huolib\constant\CommonConst;
  27. use huolib\constant\OrderConst;
  28. use huolib\tool\StrUtils;
  29. use huolib\tool\Time;
  30. use think\Cache;
  31. class StatisticsSwitchLogic extends CommonModel {
  32. protected $cache_key_prefix = CacheConst::CACHE_STATISTICS_DATA_SWITCH_PREFIX;
  33. protected $expire_time = 1;
  34. /**
  35. * @param array $param
  36. *
  37. * @return array
  38. */
  39. protected function getWhere($param = []) {
  40. $_map = [];
  41. /* 时间搜索 */
  42. list($_start_time, $_end_time) = $this->getTime($param);
  43. $_map['create_time'] = ['between', [$_start_time, $_end_time]];
  44. if (!empty($param['agent_id'])) {
  45. if (is_array($param['agent_id'])) {
  46. if (in_array('in', $param['agent_id'])) {
  47. $_map['agent_id'] = $param['agent_id'];
  48. } else {
  49. $_map['agent_id'] = ['in', $param['agent_id']];
  50. }
  51. } else {
  52. $_map['agent_id'] = $param['agent_id'];
  53. }
  54. }
  55. if (!empty($param['app_id'])) {
  56. if (is_array($param['app_id'])) {
  57. $_map['app_id'] = ['in', $param['app_id']];
  58. } else {
  59. $_map['app_id'] = $param['app_id'];
  60. }
  61. }
  62. return $_map;
  63. }
  64. public function getTime($param) {
  65. list($_start_time, $_end_time) = time::today();
  66. $_start_time = 0;
  67. if (!empty($param['start_time'])) {
  68. $_start_time = strtotime($param['start_time']);
  69. }
  70. if (!empty($param['end_time'])) {
  71. $_end_time = strtotime($param['end_time']) + CommonConst::CONST_DAY_SECONDS;
  72. }
  73. return [$_start_time, $_end_time];
  74. }
  75. public function getTodayData($where) {
  76. $where['start_time'] = $where['start_time'] = date('Y-m-d');
  77. $_data = $this->getStatisticsData($where);
  78. $_data['date'] = $where['start_time'];
  79. return $_data;
  80. }
  81. /**
  82. * 获取聚合今日数据
  83. *
  84. * @param $where
  85. *
  86. * @return array
  87. */
  88. public function getStatisticsData($where) {
  89. list($_start_time, $_end_time) = $this->getTime($where);
  90. $_map = $this->getWhere($where);
  91. $_data_by_login = $this->getDataByLoginData($_map, $_start_time, $_end_time);
  92. $_data_by_member = $this->getDataByMember($_map, $_start_time, $_end_time);
  93. $_data_by_order = $this->getDataByOrder($_map, $_start_time, $_end_time);
  94. $_data = array_merge($_data_by_login, $_data_by_member, $_data_by_order);
  95. if ($_data['user_cnt'] < $_data['reg_cnt']) {
  96. $_data['user_cnt'] = $_data['reg_cnt'];
  97. }
  98. if ($_data['ip_cnt'] < $_data['reg_ip_cnt']) {
  99. $_data['ip_cnt'] = $_data['reg_ip_cnt'];
  100. }
  101. if ($_data['device_cnt'] < $_data['reg_device_cnt']) {
  102. $_data['device_cnt'] = $_data['reg_device_cnt'];
  103. }
  104. /* 新增付费率 */
  105. if (isset($_data['reg_pay_cnt'])) {
  106. $_data['reg_pay_rate'] = StrUtils::getRate($_data['reg_pay_cnt'], $_data['reg_cnt']);
  107. /* 活跃付费率 */
  108. $_data['user_pay_rate'] = StrUtils::getRate($_data['pay_user_cnt'], $_data['user_cnt']);
  109. /* 新增ARPU */
  110. $_data['reg_arpu'] = StrUtils::getArpu($_data['reg_sum_money'], $_data['reg_pay_cnt']);
  111. /* ARPU */
  112. $_data['arppu'] = StrUtils::getArpu($_data['sum_money'], $_data['pay_user_cnt']);
  113. /* ARPPU */
  114. $_data['arpu'] = StrUtils::getArpu($_data['sum_money'], $_data['user_cnt']);
  115. }
  116. if (!empty($where['app_id'])) {
  117. $_game_data = (new GameModel())->getInfoById($where['app_id']);
  118. $_data['game_name'] = get_val($_game_data, 'name', '');
  119. $_data['game_icon'] = get_val($_game_data, 'icon', '');
  120. $_data['classify'] = get_val($_game_data, 'classify', '');
  121. }
  122. if (!empty($where['agent_id'])) {
  123. $_agent_info = (new UserModel())->getInfoById($where['agent_id']);
  124. $_data['agent_name'] = get_val($_agent_info, 'user_login', '');
  125. $_data['agent_nickname'] = get_val($_agent_info, 'user_nicename', '');
  126. }
  127. $_data['cpa_cnt'] = 0; /* cpa有效用户 */
  128. $_role_map = [
  129. 'start_time' => $_start_time,
  130. 'end_time' => $_end_time,
  131. 'app_id' => get_val($where, 'app_id', 0),
  132. 'agent_id' => get_val($where, 'agent_id', 0)
  133. ];
  134. $_data['role_cnt'] = $this->getDayRole($_role_map); /* 创角数 */
  135. $_data['openid_cnt'] = $this->getOpenidCnt($_role_map); /* 第三方注册用户 */
  136. return $_data;
  137. }
  138. /**
  139. * 获取创角数
  140. *
  141. * @param $param
  142. *
  143. * @return int|string
  144. */
  145. public function getDayRole($param) {
  146. list($_start_time, $_end_time) = Time::today();
  147. $_start_time = get_val($param, 'start_time', $_start_time);
  148. $_end_time = get_val($param, 'end_time', $_end_time);
  149. $_role_model = new MgRoleModel();
  150. $_map = [
  151. 'mg_role_model.create_time' => ['between', [$_start_time, $_end_time]],
  152. ];
  153. $_app_id = get_val($param, 'app_id', 0);
  154. if (!empty($_app_id)) {
  155. $_map['mg_role_model.app_id'] = $_app_id;
  156. }
  157. $_agent_id = get_val($param, 'agent_id', 0);
  158. $_mem_ids = [];
  159. if (!empty($_agent_id)) {
  160. if (-1 == $_agent_id) {
  161. $_mem_ids = (new MemberLogic())->getIdsByAgentId(0);
  162. $_map['mg.mem_id'] = ['in', $_mem_ids];
  163. } else {
  164. if (is_array($_agent_id)) {
  165. $_agent_mem_ids = (new MemberLogic())->getIdsByAgentId(
  166. $_agent_id, ['is_switch' => OrderConst::PAY_SWITCH_NO]
  167. );
  168. } else {
  169. $_agent_ids = (new AgentLogic())->getAgentIds($_agent_id, true);
  170. $_agent_mem_ids = (new MemberLogic())->getIdsByAgentId(
  171. ['in', $_agent_ids], ['is_switch' => OrderConst::PAY_SWITCH_NO]
  172. );
  173. }
  174. $_mem_ids = $_agent_mem_ids;
  175. }
  176. if (empty($_mem_ids)) {
  177. return 0;
  178. }
  179. }
  180. $_role_mem_ids = $_role_model->with('mg')->where($_map)->column('mem_id');
  181. if (!empty($_mem_ids)) {
  182. $_role_mem_ids = array_intersect($_role_mem_ids, $_mem_ids);
  183. }
  184. return count($_role_mem_ids);
  185. }
  186. /**
  187. * 获取单条记录缓存key
  188. *
  189. * @param array $where 条件
  190. *
  191. * @return string
  192. */
  193. protected function getCacheKeyByLoginData($where) {
  194. return $this->cache_key_prefix.'ld_'.md5(json_encode($where));
  195. }
  196. /**
  197. * 获取单条记录缓存key
  198. *
  199. * @param array $where 条件
  200. *
  201. * @return string
  202. */
  203. protected function getCacheKeyByMember($where) {
  204. return $this->cache_key_prefix.'m_'.md5(json_encode($where));
  205. }
  206. /**
  207. * 获取单条记录缓存key
  208. *
  209. * @param array $where 条件
  210. *
  211. * @return string
  212. */
  213. protected function getCacheKeyByMemberRetain($where) {
  214. return $this->cache_key_prefix.'mr_'.md5(json_encode($where));
  215. }
  216. /**
  217. * 获取单条记录缓存key
  218. *
  219. * @param array $where 条件
  220. *
  221. * @return string
  222. */
  223. protected function getCacheKeyByOpenidCnt($where) {
  224. return $this->cache_key_prefix.'oc_'.md5(json_encode($where));
  225. }
  226. /**
  227. * 获取第三方注册数
  228. *
  229. * @param $param
  230. *
  231. * @return mixed
  232. */
  233. public function getOpenidCnt($param) {
  234. /* 缓存操作 */
  235. $_cache_key = $this->getCacheKeyByOpenidCnt($param);
  236. $_data = Cache::get($_cache_key);
  237. if (!empty($_data)) {
  238. return $_data;
  239. }
  240. list($_start_time, $_end_time) = Time::today();
  241. $_start_time = get_val($param, 'start_time', $_start_time);
  242. $_end_time = get_val($param, 'end_time', $_end_time);
  243. $_map = [
  244. 'ljmem.create_time' => ['between', [$_start_time, $_end_time]],
  245. ];
  246. $_app_id = get_val($param, 'app_id', 0);
  247. if (!empty($_app_id)) {
  248. $_map['app_id'] = $_app_id;
  249. }
  250. $_agent_id = get_val($param, 'agent_id', 0);
  251. if (!empty($_agent_id)) {
  252. if (-1 == $_agent_id) {
  253. $_map['agent_id'] = 0;
  254. } else {
  255. $_map['agent_id'] = $_agent_id;
  256. }
  257. }
  258. $_cnt = (new MemoauthModel())->with('ljmem')->where($_map)->count('DISTINCT mem_id');
  259. if (!empty($_cnt)) {
  260. Cache::set($_cache_key, $_cnt, $this->expire_time);
  261. }
  262. return $_cnt;
  263. }
  264. /**
  265. * 'user_cnt' 活跃玩家数量
  266. * 'ip_cnt' IP数量
  267. * 'device_cnt' 设备数量
  268. *
  269. * @param array $where
  270. *
  271. * @param string $start_time 开始时间
  272. * @param string $end_time 结束时间
  273. *
  274. * @return array
  275. */
  276. public function getDataByLoginData($where, $start_time, $end_time) {
  277. $_date = date('Y-m', $start_time);
  278. $_date_end = date('Y-m', $end_time);
  279. $_cache_where = $where;
  280. $_map = $where;
  281. /* 缓存操作 */
  282. $_cache_key = $this->getCacheKeyByLoginData($_cache_where);
  283. $_data = Cache::get($_cache_key);
  284. if (!empty($_data)) {
  285. return $_data;
  286. }
  287. $_field = [
  288. 'count(distinct(`mem_id`))' => 'user_cnt',
  289. 'count(distinct(`device_id`))' => 'device_cnt',
  290. 'count(distinct(`ip`))' => 'ip_cnt',
  291. ];
  292. $_model = new MemLoginLogModel();
  293. $_data = $_model->computeTable($_date)->field($_field)->where($_map)->find();
  294. if (is_object($_data)) {
  295. $_data = $_data->toArray();
  296. }
  297. if (empty($_data)) {
  298. $_data = [
  299. 'user_cnt' => 0,
  300. 'device_cnt' => 0,
  301. 'ip_cnt' => 0,
  302. ];
  303. }
  304. if ($_date_end != $_date) {
  305. $_data2 = $_model->computeTable($_date_end)->field($_field)->where($_map)->find();
  306. if (is_object($_data2)) {
  307. $_data2 = $_data2->toArray();
  308. }
  309. if (!empty($_data2)) {
  310. $_data['user_cnt'] += $_data2['user_cnt'];
  311. $_data['device_cnt'] += $_data2['device_cnt'];
  312. $_data['ip_cnt'] += $_data2['ip_cnt'];
  313. }
  314. }
  315. /* 取切量数据 */
  316. $_switch_model = new MemLoginLogSwitchModel();
  317. $_switch_data = $_switch_model->computeTable($_date)->field($_field)->where($_map)->find();
  318. if (is_object($_switch_data)) {
  319. $_switch_data = $_switch_data->toArray();
  320. }
  321. if ($_date_end != $_date) {
  322. $_switch_data2 = $_switch_model->computeTable($_date_end)->field($_field)->where($_map)->find();
  323. if (is_object($_switch_data2)) {
  324. $_switch_data2 = $_switch_data2->toArray();
  325. }
  326. if (!empty($_switch_data2)) {
  327. $_switch_data['user_cnt'] += $_switch_data2['user_cnt'];
  328. $_switch_data['device_cnt'] += $_switch_data2['device_cnt'];
  329. $_switch_data['ip_cnt'] += $_switch_data2['ip_cnt'];
  330. }
  331. }
  332. $_data['user_cnt'] = CommonFunc::safeDivMinValue($_data['user_cnt'], $_switch_data['user_cnt']);
  333. $_data['device_cnt'] = CommonFunc::safeDivMinValue($_data['device_cnt'], $_switch_data['device_cnt']);
  334. $_data['ip_cnt'] = CommonFunc::safeDivMinValue($_data['ip_cnt'], $_switch_data['ip_cnt']);
  335. if (empty($_data)) {
  336. return [];
  337. }
  338. Cache::set($_cache_key, $_data, $this->expire_time);
  339. return $_data;
  340. }
  341. /**
  342. * 'history_user_cnt' 注册玩家数
  343. * 'reg_cnt' 注册玩家数
  344. * 'reg_device_cnt' 新增设备
  345. * 'reg_ip_cnt' 新增IP
  346. *
  347. * @param array $where 条件
  348. * @param string $start_time 开始时间
  349. * @param string $end_time 结束时间
  350. *
  351. * @return array
  352. */
  353. public function getDataByMember($where = [], $start_time = 0, $end_time = 0) {
  354. $_cache_where = $where;
  355. $_map = $where;
  356. $_map['is_switch'] = OrderConst::PAY_SWITCH_NO;
  357. /* 缓存操作 */
  358. $_cache_key = $this->getCacheKeyByMember($_cache_where);
  359. $_data = Cache::get($_cache_key);
  360. if (!empty($_data)) {
  361. return $_data;
  362. }
  363. $_model = (new MemberModel());
  364. $_field = [
  365. 'count(distinct(`id`))' => 'reg_cnt',
  366. 'count(distinct(`device_id`))' => 'reg_device_cnt',
  367. 'count(distinct(`reg_ip`))' => 'reg_ip_cnt',
  368. ];
  369. $_data = $_model->field($_field)->where($_map)->find();
  370. if (is_object($_data)) {
  371. $_data = $_data->toArray();
  372. }
  373. if (empty($_data)) {
  374. $_data = [
  375. 'reg_cnt' => 0,
  376. 'reg_device_cnt' => 0,
  377. 'reg_ip_cnt' => 0,
  378. ];
  379. }
  380. $_data['history_user_cnt'] = $_model->getRegisterCount(0, $end_time, $_map);
  381. Cache::set($_cache_key, $_data, $this->expire_time);
  382. return $_data;
  383. }
  384. /**
  385. * 通过订单获取今日数据
  386. * 'pay_user_cnt' 付费玩家数
  387. * 'order_cnt' 成功订单数
  388. * 'fail_order_cnt' 失败订单数
  389. * 'un_order_cnt' 待支付订单数
  390. * 'reg_order_cnt' 新增订单
  391. * 'reg_pay_cnt' 新增即付费人数
  392. * 'sum_money' 充值金额
  393. * 'sum_real_money' 充值流水
  394. * 'first_pay_cnt' 首次付费人数
  395. * 'first_pay_money' 首付金额
  396. * 'reg_sum_money' 新增即付总额
  397. * 'reg_real_sum_money' 自然流水金额
  398. *
  399. * @param array $where
  400. *
  401. * @param int $start_time 开始时间
  402. * @param int $end_time 结束时间
  403. *
  404. * @return array
  405. */
  406. public function getDataByOrder($where = [], $start_time = 0, $end_time = 0) {
  407. $_cache_where = $where;
  408. $_map = $where;
  409. $_map['is_switch'] = OrderConst::PAY_SWITCH_NO;
  410. /* 缓存操作 */
  411. $_cache_key = $this->getCacheKeyByMemberRetain($_cache_where);
  412. $_data = Cache::get($_cache_key);
  413. if (!empty($_data)) {
  414. return $_data;
  415. }
  416. $_model = (new OrderModel());
  417. $_map_order_cnt = $_map;
  418. $_map_pay_user_cnt = $_map;
  419. $_map_pay_user_cnt['status'] = OrderConst::PAY_STATUS_SUC;
  420. $_map_order_cnt['status'] = OrderConst::PAY_STATUS_SUC;
  421. $_map_fail_order_cnt['status'] = OrderConst::PAY_STATUS_FAIL;
  422. $_map_un_order_cnt['status'] = OrderConst::PAY_STATUS_NOT;
  423. $_data = [
  424. 'pay_user_cnt' => $_model->where($_map_pay_user_cnt)->count('distinct mem_id'),
  425. 'order_cnt' => $_model->where($_map_order_cnt)->count('id'),
  426. 'fail_order_cnt' => $_model->where($_map_fail_order_cnt)->count('id'),
  427. 'un_order_cnt' => $_model->where($_map_un_order_cnt)->count('id'),
  428. 'sum_money' => $_model->where($_map_order_cnt)->sum('amount'),
  429. 'sum_real_money' => $_model->where($_map_order_cnt)->sum('real_amount'),
  430. ];
  431. $_where_str = 'uo.`status` =2 and uo.create_time between '.$start_time.' AND '.$end_time
  432. .' and m.create_time between '.$start_time.' AND '.$end_time;
  433. if (!empty($_map['agent_id'])) {
  434. if (is_array($_map['agent_id'])) {
  435. $_where_str .= ' and uo.agent_id in ('.implode(',', $_map['agent_id'][1]).')';
  436. } else {
  437. $_where_str .= ' and uo.agent_id='.$_map['agent_id'];
  438. }
  439. }
  440. if (!empty($_map['app_id'])) {
  441. if (is_array($_map['app_id'])) {
  442. $_where_str .= ' and uo.app_id in ('.implode(',', $_map['app_id'][1]).')';
  443. } else {
  444. $_where_str .= ' and uo.app_id='.$_map['app_id'];
  445. }
  446. }
  447. $_sql
  448. = 'SELECT
  449. count(DISTINCT(uo.id)) AS reg_order_cnt,
  450. count(DISTINCT(uo.mem_id)) AS reg_pay_cnt,
  451. sum(uo.amount) AS reg_sum_money,
  452. sum(uo.real_amount) AS reg_real_sum_money
  453. FROM h_pay uo INNER JOIN h_member m ON m.id=uo.mem_id
  454. WHERE '.$_where_str;
  455. $_sql_data = db()->query($_sql);
  456. $_reg_data = $_sql_data[0];
  457. $_data = array_merge($_data, $_reg_data);
  458. if (empty($_data)) {
  459. $_data = [
  460. 'pay_user_cnt' => 0,
  461. 'order_cnt' => 0,
  462. 'fail_order_cnt' => 0,
  463. 'un_order_cnt' => 0,
  464. 'sum_money' => 0,
  465. 'sum_real_money' => 0,
  466. 'reg_order_cnt' => 0,
  467. 'reg_pay_cnt' => 0,
  468. 'reg_sum_money' => 0,
  469. 'reg_real_sum_money' => 0
  470. ];
  471. }
  472. if (empty($_data['reg_sum_money'])) {
  473. $_data['reg_sum_money'] = 0;
  474. }
  475. Cache::set($_cache_key, $_data, $this->expire_time);
  476. return $_data;
  477. }
  478. }