* @version : HUOSDK 8.0 */ namespace huo\model\member; use huo\model\agent\AgentGameModel; use huo\model\common\CommonModel; use huo\model\user\UserModel; use huolib\constant\CacheConst; use huolib\constant\MemConst; use huolib\constant\OrderConst; use huomp\model\member\MemInvitedLogModel; class MemberModel extends CommonModel { protected $name = 'member'; protected $pk = 'id'; //设置只读字段 protected $readonly = ['create_time']; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; //类型转换 protected $type = [ 'mobile_prefix' => 'integer', 'app_id' => 'integer', 'agent_id' => 'integer', 'parent_mem_id' => 'integer' ]; protected $cache_key_prefix = CacheConst::CACHE_MEM_PREFIX; public function getAvatarAttr($value) { if (!empty($value)) { return cmf_get_image_url($value); } return $value; } public function getStatusLabelAttr($value) { if (!empty($value)) { return MemConst::getStatus($value); } return $value; } /** * 通过玩家ID获取注册时间 * * @param int $mem_id 玩家ID * * @return string */ public function getCreateTimeById($mem_id) { $_mem_data = $this->getInfoById($mem_id); return get_val($_mem_data, 'create_time', null); } public function operator() { return $this->hasOne('huo\model\game\OperatorModel', 'id', 'op_id') ->field(['id', 'name']); } public function country() { return $this->hasOne('huo\model\district\CountryConfModel', 'mobile_prefix', 'mobile_prefix'); } public function agent() { return $this->hasOne(UserModel::className(), 'id', 'agent_id') ->field('id,user_login,user_nicename,role_id'); } public function game() { return $this->hasOne('huo\model\game\GameModel', 'id', 'app_id') ->field(['id', 'name']); } public function mem() { return $this->hasOne(self::className(), 'id', 'parent_mem_id') ->field('id,username,nickname'); } public function submem() { return $this->hasMany('huo\model\member\MemberModel', 'member_model.id', 'parent_mem_id'); } public function invited() { return $this->belongsTo(MemInvitedLogModel::className(), 'id', 'mem_id'); } /** * 关联mem_ext表 * * @return \think\model\relation\HasOne */ public function ext() { return $this->hasOne('huo\model\member\MemberExtModel', 'mem_id', 'id', [], 'left')->setEagerlyType(0); } public function mg() { return $this->hasMany("huo\model\member\MemGameModel", 'mem_id', 'id'); } /** * @param string $value * * @return string */ public function setPayPwdAttr($value) { if (!empty($value)) { return cmf_password($value); } return $value; } /** * @param string $value * * @return string */ public function setPasswordAttr($value = '') { if (!empty($value)) { return cmf_password($value); } return $value; } /** * @param array $data * * @return bool|mixed */ public function addMem($data = []) { $_data['username'] = get_val($data, 'username', ''); $_data['nickname'] = get_val($data, 'nickname', ''); $_data['reg_mobile'] = get_val($data, 'reg_mobile', ''); $_data['reg_email'] = get_val($data, 'reg_email', ''); $_data['password'] = get_val($data, 'password', ''); $_data['pay_pwd'] = get_val($data, 'pay_pwd', ''); $_data['email'] = get_val($data, 'email', ''); $_data['mobile'] = get_val($data, 'mobile', ''); $_data['from_device'] = get_val($data, 'from_device', ''); $_data['device_id'] = get_val($data, 'device_id', ''); $_data['app_id'] = get_val($data, 'app_id', 0); $_data['agent_id'] = get_val($data, 'agent_id', 0); $_data['agent_game'] = get_val($data, 'agent_game', ''); $_data['status'] = get_val($data, 'status', MemConst::STATUS_NORMAL); $_data['is_switch'] = get_val($data, 'is_switch', OrderConst::PAY_SWITCH_NO); $_data['reg_ip'] = get_val($data, 'reg_ip', ''); $_data['avatar'] = get_val($data, 'avatar', ''); $_data['vb_id'] = get_val($data, 'vb_id', 0); $_data['parent_mem_id'] = get_val($data, 'parent_mem_id', 0); $_data['create_time'] = get_val($data, 'create_time', time()); if ($_obj = self::create($_data, true)) { /* 写入扩展 */ $_ext_data['last_login_time'] = time(); $_ext_data['last_login_ip'] = $_data['reg_ip']; $_obj->ext()->save($_ext_data); /* Modified by guxiannong BEGIN 2018/8/01 ISSUES:5314 LTV */ // $_ltv_class = new \huolib\ads\Ltv(); // $_ltv_class->reg($_data['app_id'], $_data['agent_id'], $_ext_data['last_login_time']); /* END 2018/8/1 ISSUES:5314 */ return $_obj->id; } else { return false; } } /** * 计算付费玩家数 * * @param $where * * @return int|string */ public function paidCount($where) { $where['sum_money'] = ['>', 0]; return $this->with('ext')->where($where)->count('id'); } /** * 总计 * * @param array $where * * @return int */ public function total($where = []) { return $this->getRegisterCount(0, time(), $where); // return $this->where($where)->count('id'); } /** * 昨天新增 * * @param array $where * * @return int */ public function yesterdayCount($where = []) { return $this->getRegisterCount(strtotime('yesterday'), strtotime('today') - 1, $where); } /** * 今天新增 * * @param array $map * * @return int */ public function todayCount($map = []) { return $this->getRegisterCount(strtotime('today'), time(), $map); } /** * 本周新增 * * @param array $map * * @return int */ public function thisWeekCount($map = []) { $_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('this week'))); return $this->getRegisterCount($_start_time, time(), $map); } /** * 本月新增 * * @param array $map * * @return int */ public function thisMonthCount($map = []) { $_start_time = strtotime(date('Y-m-01 00:00:00')); return $this->getRegisterCount($_start_time, time(), $map); } /** * 注册数 * * @param int $start_time 开始时间,包括 * @param int $end_time 结束时间,包括 * * @param array $map * * @return int */ public function getRegisterCount($start_time, $end_time, $map = []) { $_map = [ 'create_time' => ['between', [$start_time, $end_time]], // 'status' => MemConst::STATUS_NORMAL, ]; return $this->where($map)->where($_map)->count('id'); } /** * 自然注册玩家数 * * @param array $map * * @return int|string */ public function nonAgentUserCount($map = []) { $_map = $map; return $this->where($_map)->count('id'); } /** * 渠道注册玩家数 * * @param array $map * * @return int|string */ public function agentUserCount($map = []) { $_map = $map; $_map['agent_id'] = ['>', 0]; return $this->where($_map)->count('id'); } /** * 今日活跃 * * @param array $map * * @return int */ public function todayActive($map = []) { $_map = $map; $_map['last_login_time'] = ['gt', strtotime('today')]; return $this->with('ext')->where($_map)->count('id'); } /** * 总活跃数 * * @param array $where * * @return int */ public function totalActive($where = []) { $where['last_login_time'] = ['gt', 0]; $result = $this->with('ext')->where($where)->count('id'); return $result; } /** * 昨天活跃数 * * @param array $where * * @return int */ public function yesterdayActive($where = []) { return $this->getActiveCount(strtotime('yesterday'), strtotime('today') - 1, $where); } /** * 本周活跃数 * * @param array $where * * @return int */ public function thisWeekActive($where = []) { return $this->getActiveCount(strtotime(date('Y-m-d 00:00:00', strtotime('this week'))), time(), $where); } /** * 本月活跃数 * * @param array $where * * @return int */ public function thisMonthActive($where = []) { return $this->getActiveCount(strtotime(date('Y-m-01 00:00:00')), time(), $where); } /** * 获取活跃数 * * @param int $start_time 开始时间 * @param int $end_time 结束时间 * @param array $where 其他条件 * * @return int|string */ public function getActiveCount($start_time, $end_time, $where = []) { return $this->with('ext')->where($where)->where('last_login_time', 'between', [$start_time, $end_time]) ->count('DISTINCT id'); } public function changeMemAgent($mem_id = 0, $agent_id = 0, $app_id = 0) { if (empty($mem_id)) { $mem_id = $this->id; } if (empty($agent_id)) { return false; } $_data = array(); if ($app_id) { $_data['app_id'] = $app_id; $_agent_game_model = new AgentGameModel(); $_agent_game = $_agent_game_model->where(array('agent_id' => $agent_id, 'app_id' => $app_id))->value( 'agent_game' ); if ($_agent_game) { $_data['agent_game'] = $_agent_game; } } $_data['agent_id'] = $agent_id; $_re = self::update($_data, array('id' => $mem_id)); if ($_re === false) { return false; } return true; } /*** * 获取玩家ids * * @param array $map * * @return array */ public function getMemIds($map = []) { return $this->where($map)->column('id'); } public function getIdsByParentMemId($parent_mem_id, $where = []) { return $this->where('parent_mem_id', '=', $parent_mem_id)->where($where)->column('id'); } public function getIdByUsername($username) { return $this->where('username', '=', $username)->value('id'); } /** * 计算数量 * * @param array $where * * @return int */ public function getCnt($where = []) { $_map = $where; $_cnt = $this->useGlobalScope(false)->where($_map)->count(); if (empty($_cnt)) { return 0; } return $_cnt; } /** * 查找玩家支付宝是否绑定玩家 * * @param string $alipay_account * * @return int */ public function getMemIdByAlipayAccount($alipay_account) { $_map = [ 'alipay_account' => $alipay_account ]; $_mem_id = $this->where($_map)->value('id'); if (empty($_mem_id)) { return 0; } return $_mem_id; } public function getMemListByMobile($mobile) { if (empty($mobile)) { return []; } $_map = [ 'mobile' => $mobile ]; $_list = $this->where($_map)->select(); if (empty($_list)) { return []; } return $_list; } public function getMemNumber($mobile) { if (empty($mobile)) { return []; } $_map = [ 'mobile' => $mobile ]; $_count = $this->where($_map)->count(); if (empty($_count)) { return 0; } return $_count; } /** * 通过玩家ID获取手机号 * * @param int $mem_id 玩家ID * * @return string */ public function getMobileById($mem_id) { $_mem_data = $this->getInfoById($mem_id); return get_val($_mem_data, 'mobile', ''); } /** * 查询玩家状态 * * @param int $mem_id 玩家ID * * @return int */ public function getStatus($mem_id) { $_data = $this->getInfoById($mem_id); return get_val($_data, 'status', 0); } }