* @version : HUOSDK 8.0 */ namespace huo\logic\finance; use huo\controller\agent\Agent; use huo\model\agent\AgentOrderModel; use huo\model\common\CommonModel; use huolib\constant\AgentConst; use huolib\constant\CommonConst; use huolib\constant\PaywayConst; use huolib\tool\StrUtils; use huolib\tool\Time; class AgentOrderLogic extends CommonModel { /** * 统计总收入 今日收入 昨日收入 * * @param int $agent_id 渠道ID * @param int $role_type 角色类型 * * @return array * sum_share_total 统计总收入 * today_share_total 今日收入 * yesterday_share_total 昨日收入 */ public function getStaticShareMoney($agent_id, $role_type = AgentConst::ROLE_TYPE_AGENT) { $_sum = 'agent_gain'; if (AgentConst::ROLE_TYPE_GROUP == $role_type) { $_sum = 'parent_gain'; } $_map['agent_id'] = $agent_id; $_order_model = new AgentOrderModel(); $_rdata['sum_share_total'] = $_order_model->where($_map)->sum($_sum); list($today_start, $today_end) = Time::today(); $_map['create_time'] = ['gt', $today_start]; $_rdata['today_share_total'] = $_order_model->where($_map)->sum($_sum); $_map['create_time'] = ['between', [$today_start - CommonConst::CONST_DAY_SECONDS, $today_start]]; $_rdata['yesterday_share_total'] = $_order_model->where($_map)->sum($_sum); return $_rdata; } /** * @param array $param * * @param int $agent_id * * @return array */ protected function getWhere($param = [], $agent_id = 0) { $_map = []; if (!empty($param['start_time']) && !empty($param['start_time'])) { $_map['agent_order_model.create_time'] = ['between', [strtotime($param['start_time']), CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])]]; } else if (!empty($param['start_time'])) { $_map['agent_order_model.create_time'] = ['gt', strtotime($param['start_time'])]; } else if (!empty($param['end_time'])) { $_map['agent_order_model.create_time'] = ['lt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])]; } if (!empty($param['mem_id'])) { $_map['mem_id'] = $param['mem_id']; } if (!empty($param['status'])) { $_map['status'] = $param['status']; } if (!empty($param['from_id'])) { $_map['from_id'] = $param['from_id']; } if (!empty($param['agent_id'])) { $_map['agent_order_model.agent_id'] = $param['agent_id']; } if (!empty($param['parent_id'])) { $_map['agent_order_model.parent_id'] = $param['parent_id']; } if (!empty($param['order_id'])) { $_map['agent_order_model.order_id'] = $param['order_id']; } if (!empty($param['app_id'])) { $_map['agent_order_model.app_id'] = $param['app_id']; } if (!empty($param['username'])) { $_map['username'] = $param['username']; } if (!empty($param['agent_id'])) { $_map['agent_order_model.agent_id'] = $param['agent_id']; } if (!empty($agent_id)) { $_role_type = (new Agent())->getRoleType($agent_id); if (AgentConst::ROLE_TYPE_GROUP == $_role_type) { $_map['agent_order_model.parent_id'] = $agent_id; } elseif (AgentConst::ROLE_TYPE_AGENT == $_role_type) { $_map['agent_order_model.agent_id'] = $agent_id; } } /*平台币、游戏币支付的不显示*/ if (!empty($param['pay_ignore'])) { $_map['agent_order_model.payway'] = $param['pay_ignore']; } return $_map; } public function getField($agent_id = '') { return [ 'order_id' => 'order_id', 'payway' => 'payway', 'mem_id' => 'mem_id', 'username' => 'username', 'from_id' => 'from_id', 'agent_order_model.agent_id' => 'agent_id', 'agent_order_model.app_id' => 'app_id', 'amount' => 'amount', 'real_amount' => 'real_amount', 'rebate_cnt' => 'rebate_cnt', 'agent_order_model.create_time' => 'create_time', 'agent_gain' => 'agent_gain', 'parent_gain' => 'parent_gain', ]; } /** * 获取玩家带来的收益 * * @param int $agent_id * @param array $param * @param string $page * @param string $order * * @return array */ public function getMemOrderList($agent_id, $param, $page = '1,10', $order = '-create_time') { $_map = $this->getWhere($param, $agent_id); $_map['agent_order_model.from_id'] = 0; $field = $this->getField($agent_id); return $this->getList($field, $_map, $page, $order); } /** * 获取下级带来的收益 * * @param $agent_id * @param $param * @param string $page * @param string $order * * @return array */ public function getSubAgentOrderList($agent_id, $param, $page = '1,10', $order = '-create_time') { $_map = $this->getWhere($param, $agent_id); $_map['agent_order_model.from_id'] = ['gt', 0]; $field = $this->getField($agent_id); return $this->getList($field, $_map, $page, $order); } /** * 获取收入记录 * * @param $param * @param string $page * @param string $order * * @return array */ public function getIncomeList($param, $page = '1,10', $order = '-create_time') { $_map = $this->getWhere($param); $_field = [ 'order_id' => 'order_id', 'mem_id' => 'mem_id', 'username' => 'username', 'from_id' => 'from_id', 'agent_order_model.agent_id' => 'agent_id', 'agent_order_model.app_id' => 'app_id', 'amount' => 'amount', 'real_amount' => 'real_amount', 'rebate_cnt' => 'rebate_cnt', 'payway' => 'payway', 'agent_order_model.create_time' => 'create_time', 'agent_order_model.agent_gain' => 'agent_gain', 'agent_order_model.parent_gain' => 'parent_gain', ]; return $this->getList($_field, $_map, $page, $order); } /** * @param array $field * @param array $where * @param string $page * @param string $order * * @return array * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getList($field, $where, $page = '1,10', $order = '-create_time') { $_sum_data['amount'] = 0; $_sum_data['real_amount'] = 0; $_sum_data['rebate_cnt'] = 0; $_sum_data['agent_gain'] = 0; $_sum_data['parent_gain'] = 0; $_map = $where; $_ao_model = new AgentOrderModel(); $_count = $_ao_model->with('mem')->where($_map)->count(); if (empty($_count)) { return [ 'count' => 0, 'sum' => $_sum_data, 'list' => [] ]; } $_sum_field = [ 'sum(amount)' => 'sum_amount', 'sum(real_amount)' => 'sum_real_amount', 'sum(rebate_cnt)' => 'sum_rebate_cnt', 'sum(agent_gain)' => 'sum_agent_gain', 'sum(parent_gain)' => 'sum_parent_gain', ]; $_ao_sum_data = $_ao_model ->with('mem') ->field($_sum_field) ->where($where) ->find(); if (is_object($_ao_sum_data)) { $_ao_sum_data = $_ao_sum_data->toArray(); } $_sum_data['amount'] = StrUtils::formatNumber($_ao_sum_data['sum_amount']); $_sum_data['real_amount'] = StrUtils::formatNumber($_ao_sum_data['sum_real_amount']); $_sum_data['rebate_cnt'] = StrUtils::formatNumber($_ao_sum_data['sum_rebate_cnt']); $_sum_data['agent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_agent_gain']); $_sum_data['parent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_parent_gain']); $_sum_data['sum_amount'] = StrUtils::formatNumber($_ao_sum_data['sum_amount']); $_sum_data['sum_real_amount'] = StrUtils::formatNumber($_ao_sum_data['sum_real_amount']); $_sum_data['sum_rebate_cnt'] = StrUtils::formatNumber($_ao_sum_data['sum_rebate_cnt']); $_sum_data['sum_agent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_agent_gain']); $_sum_data['sum_parent_gain'] = StrUtils::formatNumber($_ao_sum_data['sum_parent_gain']); $_field = []; $_field = array_merge($_field, $field); $_order = $_ao_model->orderFilter($order); $_ao_datas = $_ao_model ->with('mem') ->with('game') ->with('agent') ->with('parentagent') ->with('fromagent') ->field($_field) ->where($where) ->order($_order) ->page($page) ->select(); if (is_object($_ao_datas)) { $_ao_datas = $_ao_datas->toArray(); } if (empty($_ao_datas)) { return [ 'count' => $_count, 'sum' => $_sum_data, 'list' => [] ]; } $_data = []; foreach ($_ao_datas as $_ao_data) { foreach ($_field as $_v) { $_list[$_v] = $_ao_data[$_v]; } $_list['payway'] = PaywayConst::getMsg($_ao_data['payway']); $_list['agent_name'] = $_ao_data['agent']['user_login']; $_list['from_name'] = !empty($_ao_data['fromagent']) ? $_ao_data['fromagent']['user_login'] : ''; $_list['from_nicename'] = !empty($_ao_data['fromagent']) ? $_ao_data['fromagent']['user_nicename'] : ''; $_list['parent_name'] = !empty($_ao_data['parentagent']) ? $_ao_data['parentagent']['user_login'] : ''; $_list['gamename'] = !empty($_ao_data['game']) ? $_ao_data['game']['name'] : ''; $_data[] = $_list; } return [ 'count' => $_count, 'sum' => $_sum_data, 'list' => $_data ]; } }