DayDataSwitchLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * DayDataSwitchLogic.php UTF-8
  4. * 每日切量数据
  5. *
  6. * @date : 2018/5/21 21:49
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\data;
  13. use huo\controller\common\CommonFunc;
  14. use huo\model\data\DayAgentModel;
  15. use huo\model\data\DayAgentSwitchModel;
  16. use huo\model\data\DayGameAgentModel;
  17. use huo\model\data\DayGameAgentSwitchModel;
  18. use huo\model\data\DayGameModel;
  19. use huo\model\data\DayGameSwitchModel;
  20. use huo\model\data\DayModel;
  21. use huo\model\data\DaySwitchModel;
  22. use huolib\constant\CommonConst;
  23. use huolib\constant\DataConst;
  24. use huolib\tool\StrUtils;
  25. class DayDataSwitchLogic extends DayDataLogic {
  26. private $cal_base_field
  27. = [
  28. 'user_cnt' => 'user_cnt', /* 活跃玩家数量 */
  29. 'reg_device_cnt' => 'reg_device_cnt', /* 新增设备 */
  30. 'reg_cnt' => 'reg_cnt', /* 注册玩家数 */
  31. 'pay_user_cnt' => 'pay_user_cnt',/* 付费玩家数 */
  32. 'order_cnt' => 'order_cnt', /* 成功订单数 */
  33. 'reg_pay_cnt' => 'reg_pay_cnt', /* 新增即付费人数 */
  34. 'first_pay_cnt' => 'first_pay_user_cnt', /* 新增付费人数 */
  35. 'sum_money' => 'sum_money', /* 充值金额 */
  36. 'reg_sum_money' => 'reg_sum_money', /* 新增即付总额 */
  37. 'first_pay_money' => 'first_pay_sum_money', /* 新增付费总额 */
  38. 'reg_order_cnt' => 'reg_order_cnt', /* 新增订单 */
  39. 'cpa_cnt' => 'cpa_cnt', /* cpa有效用户 */
  40. 'openid_cnt' => 'openid_cnt', /* 第三方注册用户 */
  41. 'ip_cnt' => 'ip_cnt', /* 独立ip */
  42. 'reg_ip_cnt' => 'reg_ip_cnt', /* 独立ip */
  43. ];
  44. private $cal_sum_field
  45. = [
  46. 'user_cnt' => 'user_cnt',
  47. 'reg_device_cnt' => 'reg_device_cnt',
  48. 'reg_cnt' => 'reg_cnt',
  49. 'pay_user_cnt' => 'pay_user_cnt',
  50. 'reg_order_cnt' => 'reg_order_cnt',
  51. 'reg_pay_cnt' => 'reg_pay_cnt',
  52. 'first_pay_user_cnt' => 'first_pay_user_cnt',
  53. 'sum_money' => 'sum_money',
  54. 'reg_sum_money' => 'reg_sum_money',
  55. 'first_pay_sum_money' => 'first_pay_sum_money',
  56. 'order_cnt' => 'order_cnt',
  57. 'cpa_cnt' => 'cpa_cnt',
  58. 'openid_cnt' => 'openid_cnt',
  59. 'ip_cnt' => 'ip_cnt',
  60. 'reg_ip_cnt' => 'reg_ip_cnt',
  61. ];
  62. /**
  63. * @param array $where
  64. *
  65. * @return DayAgentModel|DayGameAgentModel|DayGameModel|DayModel|null|\think\Model
  66. */
  67. public function getDataModel($where) {
  68. if (isset($where['agent_id']) && isset($where['app_id'])) {
  69. return new DayGameAgentModel();
  70. } elseif (isset($where['agent_id'])) {
  71. return new DayAgentModel();
  72. } elseif (isset($where['app_id'])) {
  73. return new DayGameModel();
  74. } else {
  75. return new DayModel();
  76. }
  77. }
  78. /**
  79. * @param array $where
  80. *
  81. * @return DayAgentSwitchModel|DayGameAgentSwitchModel|DayGameSwitchModel|DaySwitchModel|null|\think\Model
  82. */
  83. public function getDataSwitchModel($where) {
  84. if (isset($where['agent_id']) && isset($where['app_id'])) {
  85. return new DayGameAgentSwitchModel();
  86. } elseif (isset($where['agent_id'])) {
  87. return new DayAgentSwitchModel();
  88. } elseif (isset($where['app_id'])) {
  89. return new DayGameSwitchModel();
  90. } else {
  91. return new DaySwitchModel();
  92. }
  93. }
  94. /**
  95. * 获取后台列表
  96. *
  97. * @param array $where 查询条件
  98. * @param string $page 分页数据
  99. * @param string $order 排序方式
  100. *
  101. * @return array
  102. */
  103. public function getAdminList($where, $page = '1,10', $order = '-date') {
  104. $_map = $this->getWhere($where);
  105. $_group = '';
  106. if (!empty($where['data_summary']) && DataConst::DATA_SUMMARY_YES == $where['data_summary']) {
  107. $_field = $this->getSumField($_map);
  108. $_group = 'date';
  109. } else {
  110. $_field = $this->getField($_map);
  111. }
  112. $_rdata = $this->NewgetList($_map, $page, $order, $_field, $_group);
  113. if (empty($_rdata['count'])) {
  114. return [
  115. 'count' => CommonConst::CONST_ZERO,
  116. 'sum' => [],
  117. 'list' => []
  118. ];
  119. }
  120. $_datas = get_val($_rdata, 'list', []);
  121. /* 取切量数据 */
  122. $_switch_model = $this->getDataSwitchModel($_map);
  123. $_switch_count = $_switch_model->where($_map)->count();
  124. if (!empty($_switch_count)) {
  125. $_rdata_switch = $this->NewgetListSwitch($_map, $page, $order, $_field, $_group);
  126. foreach ($_rdata_switch['list'] as $_sk => $_sv) {
  127. foreach ($_datas as $_k => $_v) {
  128. if ($_sv['date'] == $_v['date']) {
  129. foreach ($this->cal_base_field as $_field) {
  130. $_datas[$_k][$_field] = CommonFunc::safeDivMinValue($_v[$_field], $_sv[$_field]);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. $_static_logic = new StatisticsSwitchLogic();
  137. $_today_date = date('Y-m-d');
  138. foreach ($_datas as $_k => $_v) {
  139. if ($_today_date == $_v['date']) {
  140. unset($_datas[$_k]);
  141. continue;
  142. }
  143. if (!isset($_v['agent_id'])) {
  144. $_datas[$_k]['agent_id'] = 0;
  145. }
  146. if (!isset($_v['app_id'])) {
  147. $_datas[$_k]['app_id'] = 0;
  148. }
  149. $_datas[$_k]['reg_pay_rate'] = StrUtils::getRate($_v['reg_pay_cnt'], $_v['reg_cnt']);
  150. $_datas[$_k]['user_pay_rate'] = StrUtils::getRate($_v['pay_user_cnt'], $_v['user_cnt']);
  151. $_datas[$_k]['reg_arpu'] = StrUtils::getArpu($_v['reg_sum_money'], $_v['reg_cnt']);
  152. $_datas[$_k]['arppu'] = StrUtils::getArpu($_v['sum_money'], $_v['pay_user_cnt']);
  153. $_datas[$_k]['arpu'] = StrUtils::getArpu($_v['sum_money'], $_v['user_cnt']);
  154. $_start_time = strtotime($_v['date']);
  155. $_end_time = $_start_time + CommonConst::CONST_DAY_SECONDS;
  156. $_role_map = [
  157. 'start_time' => $_start_time,
  158. 'end_time' => $_end_time,
  159. 'app_id' => get_val($where, 'app_id', 0),
  160. 'agent_id' => get_val($where, 'agent_id', 0)
  161. ];
  162. $_datas[$_k]['role_cnt'] = $_static_logic->getDayRole($_role_map);
  163. }
  164. $_rdata['list'] = $_datas;
  165. $_sum_field = $this->sum_field;
  166. $_rdata['sum'] = $this->getSumData($where, $_sum_field);
  167. return $_rdata;
  168. }
  169. /**
  170. * 获取切量列表
  171. *
  172. * @param array $where 查询条件
  173. * @param string $page 分页数据
  174. * @param string $order 排序方式
  175. * @param array $field 查询字段
  176. * @param string $group 组查询
  177. *
  178. * @return array
  179. */
  180. public function NewgetListSwitch($where, $page = '1,10', $order = '-date', $field = [], $group = '') {
  181. $_map = $where;
  182. $_model = $this->getDataSwitchModel($_map);
  183. if (!empty($_map['agent_id']) && -1 == $_map['agent_id']) {
  184. $_map['agent_id'] = 0;
  185. }
  186. if (!empty($group)) {
  187. $_count = $_model->where($_map)->group($group)->count();
  188. } else {
  189. $_count = $_model->where($_map)->count();
  190. }
  191. $_field = $field;
  192. $_order = $_model->orderFilter($order);
  193. $_datas = $_model->where($_map)
  194. ->field($_field)
  195. ->order($_order)
  196. ->group($group)
  197. ->page($page)
  198. ->select();
  199. if (is_object($_datas)) {
  200. $_datas = $_datas->toArray();
  201. }
  202. if (empty($_datas)) {
  203. return [
  204. 'count' => $_count,
  205. 'list' => []
  206. ];
  207. }
  208. return [
  209. 'count' => $_count,
  210. 'list' => $_datas
  211. ];
  212. }
  213. /**
  214. * 获取汇总数据
  215. *
  216. * @param array $where 查询条件
  217. * @param array $sum_field 查询汇总字段
  218. *
  219. * @return array
  220. */
  221. public function getSumData($where, $sum_field) {
  222. $_date = date('Y-m-d');
  223. $_where = $where;
  224. if (isset($_where['start_time']) && $_date == $_where['start_time']) {
  225. $_where['start_time'] = date('Y-m-d', strtotime('+1 day'));
  226. }
  227. if (isset($_where['end_time']) && strtotime($_where['end_time']) >= strtotime($_date)) {
  228. $_where['end_time'] = date('Y-m-d', strtotime('-1 day'));
  229. }
  230. $_map = $this->getWhere($_where);
  231. $_model = $this->getDataModel($_map);
  232. $_sum_field = $sum_field;
  233. $_sum_data = $_model
  234. ->field($_sum_field)
  235. ->where($_map)
  236. ->find();
  237. if (is_object($_sum_data)) {
  238. $_sum_data = $_sum_data->toArray();
  239. }
  240. foreach ($sum_field as $_v) {
  241. if (empty($_sum_data[$_v])) {
  242. $_sum_data[$_v] = 0;
  243. }
  244. }
  245. $_sum = $_sum_data;
  246. /* 取切量数据 */
  247. $_switch_model = $this->getDataSwitchModel($_map);
  248. $_sum_data_switch = $_switch_model->field($_sum_field)->where($_map)->find();
  249. if (is_object($_sum_data_switch)) {
  250. $_sum_data_switch = $_sum_data_switch->toArray();
  251. }
  252. if (!empty($_sum_data_switch)) {
  253. foreach ($this->cal_sum_field as $_field) {
  254. $_sum[$_field] = CommonFunc::safeDivMinValue($_sum[$_field], $_sum_data_switch[$_field]);
  255. }
  256. }
  257. // $_user_cnt = $this->getActiveUserSum($where);
  258. // $_sum['user_cnt'] = $_user_cnt;
  259. if (isset($_sum_data['reg_pay_cnt']) && isset($_sum_data['reg_cnt'])) {
  260. $_sum['reg_pay_rate'] = StrUtils::getRate($_sum_data['reg_pay_cnt'], $_sum_data['reg_cnt']);
  261. }
  262. if (isset($_sum_data['pay_user_cnt']) && isset($_sum_data['user_cnt'])) {
  263. $_sum['user_pay_rate'] = StrUtils::getRate($_sum_data['pay_user_cnt'], $_sum_data['user_cnt']);
  264. }
  265. if (isset($_sum_data['reg_sum_money']) && isset($_sum_data['reg_cnt'])) {
  266. $_sum['reg_arpu'] = StrUtils::getArpu($_sum_data['reg_sum_money'], $_sum_data['reg_cnt']);
  267. }
  268. if (isset($_sum_data['sum_money']) && isset($_sum_data['user_cnt'])) {
  269. $_sum['arpu'] = StrUtils::getArpu($_sum_data['sum_money'], $_sum_data['user_cnt']);
  270. }
  271. if (isset($_sum_data['sum_money']) && isset($_sum_data['pay_user_cnt'])) {
  272. $_sum['arppu'] = StrUtils::getArpu($_sum_data['sum_money'], $_sum_data['pay_user_cnt']);
  273. }
  274. list($_start_time, $_end_time) = $this->getTime($where);
  275. $_role_map = [
  276. 'start_time' => $_start_time,
  277. 'end_time' => $_end_time,
  278. 'app_id' => get_val($where, 'app_id', 0),
  279. 'agent_id' => get_val($where, 'agent_id', 0)
  280. ];
  281. $_sum['role_cnt'] = (new StatisticsSwitchLogic())->getDayRole($_role_map); /* 创角数 */
  282. return $_sum;
  283. }
  284. }