123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- /**
- * HourDataLogic.php UTF-8
- * 时表数据处理
- *
- * @date : 2019/12/11 18:35
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : dengcongshuai <dcs@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huomp\logic\data;
- use huo\model\common\CommonModel;
- use huo\model\user\UserModel;
- use huolib\constant\AgentConst;
- use huolib\constant\CommonConst;
- use huomp\model\hour\HourAgentModel;
- use huomp\model\hour\HourGameAgentModel;
- use huomp\model\hour\HourGameModel;
- use huomp\model\hour\HourModel;
- class HourDataLogic extends CommonModel {
- private $base_field
- = [
- 'date',
- 'hour_key',
- 'user_cnt',
- 'reg_cnt',
- 'reg_pay_mem_cnt',
- 'reg_sum_money',
- ];
- private $sum_field
- = [
- 'date' => 'date',
- 'hour_key' => 'hour_key',
- 'sum(user_cnt)' => 'user_cnt',
- 'sum(reg_cnt)' => 'reg_cnt',
- 'sum(reg_pay_mem_cnt)' => 'reg_pay_mem_cnt',
- 'sum(reg_sum_money)' => 'reg_sum_money',
- ];
- /**
- * @param array $param
- *
- * @return array
- */
- protected function getWhere($param = []) {
- $_map = [];
- if (!empty($param['start_time']) && !empty($param['end_time'])) {
- $_map['date'] = ['between', [$param['start_time'], $param['end_time']]];
- } elseif (!empty($param['start_time'])) {
- $_map['date'] = ['egt', $param['start_time']];
- } elseif (!empty($param['end_time'])) {
- $_map['date'] = ['elt', $param['end_time']];
- }
- $_agent_model = new UserModel();
- if (!empty($param['cp_id'])) {
- $_ids = $_agent_model->getIdsByCpId(
- $param['cp_id'], ['in', [AgentConst::AGENT_ROLE_MP_AGENT, AgentConst::AGENT_ROLE_MP_AD]]
- );
- if (empty($_ids)) {
- $_ids = [-1];
- }
- $_map['agent_id'] = ['in', $_ids];
- }
- if (!empty($param['agent_level_1_id'])) {
- $_agent_map = [
- 'role_id' => ['in', [AgentConst::AGENT_ROLE_MP_AGENT, AgentConst::AGENT_ROLE_MP_AD]]
- ];
- $_ids = $_agent_model->getIdsByParentId($param['agent_level_1_id'], $_agent_map);
- $_ids[] = $param['agent_level_1_id'];
- $_map['agent_id'] = ['in', $_ids];
- }
- if (!empty($param['agent_level_2_id'])) {
- $_map['agent_id'] = $param['agent_level_2_id'];
- }
- if (!empty($param['agent_id'])) {
- $_map['agent_id'] = $param['agent_id'];
- }
- if (!empty($param['app_id'])) {
- $_map['app_id'] = $param['app_id'];
- }
- return $_map;
- }
- /**
- * @param array $where
- *
- * @return HourModel|HourGameAgentModel|HourGameModel|HourModel|null|\think\Model
- */
- public function getDataModel($where) {
- if (!empty($where['agent_id']) && !empty($where['app_id'])) {
- return new HourGameAgentModel();
- } elseif (!empty($where['agent_id'])) {
- return new HourAgentModel();
- } elseif (!empty($where['app_id'])) {
- return new HourGameModel();
- } else {
- return new HourModel();
- }
- }
- /**
- * 获取字段
- *
- * @param $where
- *
- * @param int $is_summary
- *
- * @return array
- */
- public function getField($where, $is_summary = 0) {
- if (CommonConst::STATUS_YES == $is_summary) {
- $_field = $this->sum_field;
- $_field['date'] = 'date';
- } else {
- $_field = $this->base_field;
- }
- if (!empty($where['agent_id']) && !empty($where['app_id'])) {
- $_field['agent_id'] = 'agent_id';
- $_field['app_id'] = 'app_id';
- } elseif (!empty($where['agent_id'])) {
- $_field['agent_id'] = 'agent_id';
- } elseif (!empty($where['app_id'])) {
- $_field['app_id'] = 'app_id';
- }
- return $_field;
- }
- /**
- * 获取with
- *
- * @param $where
- *
- * @return string
- */
- public function getWith($where) {
- $_with = '';
- if (!empty($where['agent_id']) && !empty($where['app_id'])) {
- $_with = 'agent,game';
- } elseif (!empty($where['agent_id'])) {
- $_with = 'agent';
- $_field['agent_id'] = 'agent_id';
- } elseif (!empty($where['app_id'])) {
- $_with = 'game';
- }
- return $_with;
- }
- /**
- * 获取列表底层函数
- *
- * @param array $where 搜索条件
- * @param string $page 列表个数
- * @param string $order 排序
- * @param array $field 附加字段
- * @param string $group 归类
- * @param string $with
- *
- * @return array ['count'=>0,'list'=>[]]
- */
- public function getList($where = [], $page = '1,10', $order = '-date', $field = [], $group = '', $with = '') {
- $_map = $where;
- $_field = $field;
- $_model = $this->getDataModel($_map);
- if (!empty($group)) {
- $_count = $_model->where($_map)->group($group)->count();
- } else {
- $_count = $_model->where($_map)->count();
- }
- if (empty($_count)) {
- return [
- 'count' => 0,
- 'list' => []
- ];
- }
- $_order = $this->orderFilter($order);
- if (!empty($group)) {
- $_datas = $_model->with($with)
- ->field($_field)
- ->where($_map)
- ->order($_order)
- ->group($group)
- ->page($page)
- ->select();
- } else {
- $_datas = $_model->with($with)
- ->field($_field)
- ->where($_map)
- ->order($_order)
- ->page($page)
- ->select();
- }
- if (is_object($_datas)) {
- $_datas = $_datas->toArray();
- }
- return [
- 'count' => $_count,
- 'list' => $_datas
- ];
- }
- /**
- * 获取汇总数据
- *
- * @param $where
- * @param $sum_file
- * @param $group
- *
- * @return array
- */
- public function getSumData($where, $sum_file, $group = '') {
- $_model = $this->getDataModel($where);
- $_map = $where;
- $_sum_field = $sum_file;
- $_sum_data = $_model
- ->field($_sum_field)
- ->where($_map)
- ->group($group)
- ->find();
- if (is_object($_sum_data)) {
- $_sum_data = $_sum_data->toArray();
- }
- return $_sum_data;
- }
- /**
- * 获取后台列表
- *
- * @param array $where 搜索条件
- * @param string $page 列表个数
- * @param string $order 排序
- * @param array $field 附加字段
- *
- * @return array ['count'=>0,'list'=>[]]
- */
- public function getAdminList($where = [], $page = '1,10', $order = '-date,-hour_key', $field = []) {
- $_map = $this->getWhere($where);
- $_field = $this->getField($_map, $where['is_summary']);
- $_with = $this->getWith($_map);
- if (!empty($field)) {
- $_field = array_merge($_field, $field);/* 获取后台字段 */
- }
- $_group = '';
- if (isset($where['include_agent']) && 2 == $where['include_agent']
- || isset($where['is_summary'])
- && 2 == $where['is_summary']) {
- $_group = 'date,hour_key';
- }
- $_rdata = $this->getList($_map, $page, $order, $_field, $_group, $_with);
- if (empty($_rdata['count'])) {
- $_sum_data = [];
- foreach ($this->sum_field as $_v) {
- $_sum_data[$_v] = 0;
- }
- $_rdata['sum'] = $_sum_data;
- return $_rdata;
- }
- $_datas = $_rdata['list'];
- foreach ($_datas as $_k => $_v) {
- $_datas[$_k]['game_name'] = '';
- $_datas[$_k]['game_icon'] = '';
- $_datas[$_k]['classify'] = 0;
- if (!empty($_v['game'])) {
- $_datas[$_k]['game_name'] = get_val($_v['game'], 'name', '');
- $_datas[$_k]['game_icon'] = get_val($_v['game'], 'icon', '');
- $_datas[$_k]['classify'] = get_val($_v['game'], 'classify', '');
- unset($_datas[$_k]['game']);
- }
- $_datas[$_k]['agent_nickname'] = '';
- $_datas[$_k]['agent_name'] = '';
- if (!empty($_v['agent'])) {
- $_datas[$_k]['agent_nickname'] = get_val($_v['agent'], 'user_nicename', '');
- $_datas[$_k]['agent_name'] = get_val($_v['agent'], 'user_login', '');
- unset($_datas[$_k]['agent']);
- }
- }
- $_rdata['list'] = $_datas;
- $_sum_filed = $this->sum_field;
- $_sum_group = '';
- if (!empty($_map['agent_id'])) {
- $_sum_group = 'date';
- }
- $_rdata['sum'] = $this->getSumData($_map, $_sum_filed, $_sum_group);
- return $_rdata;
- }
- }
|