* @version : HUOSDK 8.0 */ namespace huo\logic\data; use huo\controller\common\CommonFunc; use huo\model\data\DayAgentModel; use huo\model\data\DayAgentSwitchModel; use huo\model\data\DayGameAgentModel; use huo\model\data\DayGameAgentSwitchModel; use huo\model\data\DayGameModel; use huo\model\data\DayGameSwitchModel; use huo\model\data\DayModel; use huo\model\data\DaySwitchModel; use huolib\constant\CommonConst; use huolib\constant\DataConst; use huolib\tool\StrUtils; class DayDataSwitchLogic extends DayDataLogic { private $cal_base_field = [ 'user_cnt' => 'user_cnt', /* 活跃玩家数量 */ 'reg_device_cnt' => 'reg_device_cnt', /* 新增设备 */ 'reg_cnt' => 'reg_cnt', /* 注册玩家数 */ 'pay_user_cnt' => 'pay_user_cnt',/* 付费玩家数 */ 'order_cnt' => 'order_cnt', /* 成功订单数 */ 'reg_pay_cnt' => 'reg_pay_cnt', /* 新增即付费人数 */ 'first_pay_cnt' => 'first_pay_user_cnt', /* 新增付费人数 */ 'sum_money' => 'sum_money', /* 充值金额 */ 'reg_sum_money' => 'reg_sum_money', /* 新增即付总额 */ 'first_pay_money' => 'first_pay_sum_money', /* 新增付费总额 */ 'reg_order_cnt' => 'reg_order_cnt', /* 新增订单 */ 'cpa_cnt' => 'cpa_cnt', /* cpa有效用户 */ 'openid_cnt' => 'openid_cnt', /* 第三方注册用户 */ 'ip_cnt' => 'ip_cnt', /* 独立ip */ 'reg_ip_cnt' => 'reg_ip_cnt', /* 独立ip */ ]; private $cal_sum_field = [ 'user_cnt' => 'user_cnt', 'reg_device_cnt' => 'reg_device_cnt', 'reg_cnt' => 'reg_cnt', 'pay_user_cnt' => 'pay_user_cnt', 'reg_order_cnt' => 'reg_order_cnt', 'reg_pay_cnt' => 'reg_pay_cnt', 'first_pay_user_cnt' => 'first_pay_user_cnt', 'sum_money' => 'sum_money', 'reg_sum_money' => 'reg_sum_money', 'first_pay_sum_money' => 'first_pay_sum_money', 'order_cnt' => 'order_cnt', 'cpa_cnt' => 'cpa_cnt', 'openid_cnt' => 'openid_cnt', 'ip_cnt' => 'ip_cnt', 'reg_ip_cnt' => 'reg_ip_cnt', ]; /** * @param array $where * * @return DayAgentModel|DayGameAgentModel|DayGameModel|DayModel|null|\think\Model */ public function getDataModel($where) { if (isset($where['agent_id']) && isset($where['app_id'])) { return new DayGameAgentModel(); } elseif (isset($where['agent_id'])) { return new DayAgentModel(); } elseif (isset($where['app_id'])) { return new DayGameModel(); } else { return new DayModel(); } } /** * @param array $where * * @return DayAgentSwitchModel|DayGameAgentSwitchModel|DayGameSwitchModel|DaySwitchModel|null|\think\Model */ public function getDataSwitchModel($where) { if (isset($where['agent_id']) && isset($where['app_id'])) { return new DayGameAgentSwitchModel(); } elseif (isset($where['agent_id'])) { return new DayAgentSwitchModel(); } elseif (isset($where['app_id'])) { return new DayGameSwitchModel(); } else { return new DaySwitchModel(); } } /** * 获取后台列表 * * @param array $where 查询条件 * @param string $page 分页数据 * @param string $order 排序方式 * * @return array */ public function getAdminList($where, $page = '1,10', $order = '-date') { $_map = $this->getWhere($where); $_group = ''; if (!empty($where['data_summary']) && DataConst::DATA_SUMMARY_YES == $where['data_summary']) { $_field = $this->getSumField($_map); $_group = 'date'; } else { $_field = $this->getField($_map); } $_rdata = $this->NewgetList($_map, $page, $order, $_field, $_group); if (empty($_rdata['count'])) { return [ 'count' => CommonConst::CONST_ZERO, 'sum' => [], 'list' => [] ]; } $_datas = get_val($_rdata, 'list', []); /* 取切量数据 */ $_switch_model = $this->getDataSwitchModel($_map); $_switch_count = $_switch_model->where($_map)->count(); if (!empty($_switch_count)) { $_rdata_switch = $this->NewgetListSwitch($_map, $page, $order, $_field, $_group); foreach ($_rdata_switch['list'] as $_sk => $_sv) { foreach ($_datas as $_k => $_v) { if ($_sv['date'] == $_v['date']) { foreach ($this->cal_base_field as $_field) { $_datas[$_k][$_field] = CommonFunc::safeDivMinValue($_v[$_field], $_sv[$_field]); } } } } } $_static_logic = new StatisticsSwitchLogic(); $_today_date = date('Y-m-d'); foreach ($_datas as $_k => $_v) { if ($_today_date == $_v['date']) { unset($_datas[$_k]); continue; } if (!isset($_v['agent_id'])) { $_datas[$_k]['agent_id'] = 0; } if (!isset($_v['app_id'])) { $_datas[$_k]['app_id'] = 0; } $_datas[$_k]['reg_pay_rate'] = StrUtils::getRate($_v['reg_pay_cnt'], $_v['reg_cnt']); $_datas[$_k]['user_pay_rate'] = StrUtils::getRate($_v['pay_user_cnt'], $_v['user_cnt']); $_datas[$_k]['reg_arpu'] = StrUtils::getArpu($_v['reg_sum_money'], $_v['reg_cnt']); $_datas[$_k]['arppu'] = StrUtils::getArpu($_v['sum_money'], $_v['pay_user_cnt']); $_datas[$_k]['arpu'] = StrUtils::getArpu($_v['sum_money'], $_v['user_cnt']); $_start_time = strtotime($_v['date']); $_end_time = $_start_time + CommonConst::CONST_DAY_SECONDS; $_role_map = [ 'start_time' => $_start_time, 'end_time' => $_end_time, 'app_id' => get_val($where, 'app_id', 0), 'agent_id' => get_val($where, 'agent_id', 0) ]; $_datas[$_k]['role_cnt'] = $_static_logic->getDayRole($_role_map); } $_rdata['list'] = $_datas; $_sum_field = $this->sum_field; $_rdata['sum'] = $this->getSumData($where, $_sum_field); return $_rdata; } /** * 获取切量列表 * * @param array $where 查询条件 * @param string $page 分页数据 * @param string $order 排序方式 * @param array $field 查询字段 * @param string $group 组查询 * * @return array */ public function NewgetListSwitch($where, $page = '1,10', $order = '-date', $field = [], $group = '') { $_map = $where; $_model = $this->getDataSwitchModel($_map); if (!empty($_map['agent_id']) && -1 == $_map['agent_id']) { $_map['agent_id'] = 0; } if (!empty($group)) { $_count = $_model->where($_map)->group($group)->count(); } else { $_count = $_model->where($_map)->count(); } $_field = $field; $_order = $_model->orderFilter($order); $_datas = $_model->where($_map) ->field($_field) ->order($_order) ->group($group) ->page($page) ->select(); if (is_object($_datas)) { $_datas = $_datas->toArray(); } if (empty($_datas)) { return [ 'count' => $_count, 'list' => [] ]; } return [ 'count' => $_count, 'list' => $_datas ]; } /** * 获取汇总数据 * * @param array $where 查询条件 * @param array $sum_field 查询汇总字段 * * @return array */ public function getSumData($where, $sum_field) { $_date = date('Y-m-d'); $_where = $where; if (isset($_where['start_time']) && $_date == $_where['start_time']) { $_where['start_time'] = date('Y-m-d', strtotime('+1 day')); } if (isset($_where['end_time']) && strtotime($_where['end_time']) >= strtotime($_date)) { $_where['end_time'] = date('Y-m-d', strtotime('-1 day')); } $_map = $this->getWhere($_where); $_model = $this->getDataModel($_map); $_sum_field = $sum_field; $_sum_data = $_model ->field($_sum_field) ->where($_map) ->find(); if (is_object($_sum_data)) { $_sum_data = $_sum_data->toArray(); } foreach ($sum_field as $_v) { if (empty($_sum_data[$_v])) { $_sum_data[$_v] = 0; } } $_sum = $_sum_data; /* 取切量数据 */ $_switch_model = $this->getDataSwitchModel($_map); $_sum_data_switch = $_switch_model->field($_sum_field)->where($_map)->find(); if (is_object($_sum_data_switch)) { $_sum_data_switch = $_sum_data_switch->toArray(); } if (!empty($_sum_data_switch)) { foreach ($this->cal_sum_field as $_field) { $_sum[$_field] = CommonFunc::safeDivMinValue($_sum[$_field], $_sum_data_switch[$_field]); } } // $_user_cnt = $this->getActiveUserSum($where); // $_sum['user_cnt'] = $_user_cnt; if (isset($_sum_data['reg_pay_cnt']) && isset($_sum_data['reg_cnt'])) { $_sum['reg_pay_rate'] = StrUtils::getRate($_sum_data['reg_pay_cnt'], $_sum_data['reg_cnt']); } if (isset($_sum_data['pay_user_cnt']) && isset($_sum_data['user_cnt'])) { $_sum['user_pay_rate'] = StrUtils::getRate($_sum_data['pay_user_cnt'], $_sum_data['user_cnt']); } if (isset($_sum_data['reg_sum_money']) && isset($_sum_data['reg_cnt'])) { $_sum['reg_arpu'] = StrUtils::getArpu($_sum_data['reg_sum_money'], $_sum_data['reg_cnt']); } if (isset($_sum_data['sum_money']) && isset($_sum_data['user_cnt'])) { $_sum['arpu'] = StrUtils::getArpu($_sum_data['sum_money'], $_sum_data['user_cnt']); } if (isset($_sum_data['sum_money']) && isset($_sum_data['pay_user_cnt'])) { $_sum['arppu'] = StrUtils::getArpu($_sum_data['sum_money'], $_sum_data['pay_user_cnt']); } list($_start_time, $_end_time) = $this->getTime($where); $_role_map = [ 'start_time' => $_start_time, 'end_time' => $_end_time, 'app_id' => get_val($where, 'app_id', 0), 'agent_id' => get_val($where, 'agent_id', 0) ]; $_sum['role_cnt'] = (new StatisticsSwitchLogic())->getDayRole($_role_map); /* 创角数 */ return $_sum; } }