123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- /**
- * DayMemLogModel.php UTF-8
- * 玩家每日数据
- *
- * @date : 2018/5/31 10:58
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace huo\model\log;
- use huo\controller\member\MemCache;
- use huo\model\game\GameModel;
- use huolib\tool\Time;
- class DayMemLogModel extends LogModel {
- protected $name = 'log_day_mem';
- /**
- * LogModel constructor.
- *
- * @param array $data
- */
- public function __construct($data = []) {
- $_db_conf = [];
- if (file_exists(GLOBAL_CONF_PATH.'database_log.php')) {
- $_db_conf = include GLOBAL_CONF_PATH.'database_log.php';
- }
- $this->connection = $_db_conf;
- parent::db();
- parent::__construct($data);
- }
- public function game() {
- return $this->hasOne(GameModel::className(), 'id', 'app_id')
- ->field(['id', 'name', 'icon']);
- }
- /**
- * @param array $where
- * date
- * mem_id
- * app_id
- *
- * @return array|bool|false
- */
- public function getDetail($where) {
- $_map['date'] = isset($where['date']) ? $where['date'] : date('Y-m-d');
- $_map['mem_id'] = isset($where['mem_id']) ? $where['mem_id'] : 0;
- $_map['app_id'] = isset($where['app_id']) ? $where['app_id'] : 0;
- $_data = $this->computeTable($_map['date'])->where($_map)->find();
- if (is_object($_data)) {
- $_data = $_data->toArray();
- }
- if (empty($_data)) {
- return false;
- }
- return $_data;
- }
- /**
- * 计算总登陆次数
- *
- * @param $where
- *
- * @return float|int
- */
- public function getSumLoginCnt($where) {
- $_map['date'] = isset($where['date']) ? $where['date'] : date('Y-m-d');
- $_map['mem_id'] = isset($where['mem_id']) ? $where['mem_id'] : 0;
- $_sum_login_cnt = $this->computeTable($_map['date'])->where($_map)->sum('login_cnt');
- if (empty($_sum_login_cnt)) {
- return 0;
- }
- return $_sum_login_cnt;
- }
- /**
- * @param $data
- * @param bool $replace
- * @param bool $get_last_insert_id
- *
- * @return bool|int|string
- */
- public function insertLog($data, $replace = true, $get_last_insert_id = true) {
- return parent::insertLog($data, $replace, $get_last_insert_id);
- }
- /**
- * 更新每日玩家表
- *
- * @param array $data
- *
- * @param int $id 主键ID
- *
- * @return bool
- */
- public function updateLog($data, $id) {
- $_map['id'] = $id;
- if (false === $this->computeTable($data['date'])->where($_map)->update($data)) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * 添加数据
- *
- * @param array $data 需要添加的数据
- *
- * @return false|int 添加失败返回 false 添加成功 返回添加的ID
- */
- public function addData($data) {
- if (empty($data['date'])) {
- return false;
- }
- $_data = $data;
- $_id = $this->computeTableTwo($_data['date'])->insert($_data, true, true);
- if (false === $_id) {
- return false;
- }
- /* TAG缓存操作 */
- return $_id;
- }
- /**
- * 获取信息
- *
- * @param string $date 日期
- * @param int $mem_id 玩家ID
- * @param int $app_id 应用ID
- * @param string $device_id 设备
- *
- * @return array|false
- */
- public function getInfoByDateMemAppDevice($date, $mem_id, $app_id, $device_id = '') {
- /* 缓存操作 */
- $_map = [
- 'date' => $date,
- 'mem_id' => $mem_id,
- 'app_id' => $app_id,
- // 'device_id' => $device_id,
- ];
- $_data = $this->computeTableTwo($_map['date'])->where($_map)->find();
- if (is_object($_data)) {
- $_data = $_data->toArray();
- }
- if (empty($_data)) {
- return [];
- }
- return $_data;
- }
- /**
- * 更新单条数据
- *
- * @param array $data 数据
- * @param int $id ID
- *
- * @return bool
- */
- public function updateData($data, $id) {
- if (empty($data['date'])) {
- return false;
- }
- $_data = $this->getInfoByDateMemAppDevice($data['date'], $data['mem_id'], $data['app_id']);
- if (!empty($_data)) {
- $_data = array_merge($_data, $data);
- } else {
- $_data = $data;
- $_time = time();
- $_mem_data = (new MemCache())->getInfoById($data['mem_id']);
- $_data['last_login_ip'] = ip2long($_mem_data['reg_ip']);
- $_data['device_id'] = $_mem_data['device_id'];
- $_data['reg_time'] = $_mem_data['create_time'];
- $_data['reg_days'] = Time::timeDateDiff($_data['reg_time'], $_time);
- }
- $_id = $this->addData($_data);
- return $_id;
- }
- /**
- * 创建表
- *
- * @param int $create_time
- *
- * @return int
- */
- function checkTable($create_time = 0) {
- $_time = !empty($create_time) ? $create_time : time();
- $this->partition_data[$this->partition_field] = date('Y-m-d', $_time);
- $this->table = $this->getPartitionTableName(
- $this->partition_data, $this->partition_field, $this->partition_rule
- );
- $_sql = "CREATE TABLE IF NOT EXISTS `$this->table` (";
- $_sql .= " `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '',";
- $_sql .= " `date` date NOT NULL COMMENT '日期',";
- $_sql .= " `mem_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '玩家ID',";
- $_sql .= " `agent_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '归属渠道',";
- $_sql .= " `app_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '游戏ID',";
- $_sql .= " `reg_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '玩家注册时间',";
- $_sql .= " `reg_days` int(11) NOT NULL DEFAULT '0' COMMENT '注册天数',";
- $_sql .= " `role_days` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创角天数',";
- $_sql .= " `device_id` varchar(64) NOT NULL DEFAULT '' COMMENT '手机设备码',";
- $_sql .= " `device_cnt` int(11) NOT NULL DEFAULT '1' COMMENT '登陆的设备个数',";
- $_sql .= " `login_cnt` int(11) NOT NULL DEFAULT '1' COMMENT '登陆次数',";
- $_sql .= " `sum_money` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '累计充值',";
- $_sql .= " `sum_real_money` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '累计真实充值',";
- $_sql .= " `first_pay_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '第一笔充值时间',";
- $_sql .= " `last_pay_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '最近充值时间',";
- $_sql .= " `last_money` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '最近充值金额',";
- $_sql .= " `order_cnt` int(20) unsigned NOT NULL DEFAULT '0' COMMENT '订单数量',";
- $_sql .= " `order_suc_cnt` int(20) unsigned NOT NULL DEFAULT '0' COMMENT '支付成功订单数量',";
- $_sql .= " `last_login_ip` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '玩家最近登陆ip',";
- $_sql .= " `auth_cnt` int(11) NOT NULL DEFAULT '0' COMMENT '授权登陆次数',";
- $_sql .= " `is_cpa` tinyint(4) UNSIGNED unsigned NOT NULL DEFAULT 1 COMMENT '是否计算cpa 1 否 2是',";
- $_sql .= " `is_auth` tinyint(4) UNSIGNED unsigned NOT NULL DEFAULT 1 COMMENT '是否第三方注册 1 否 2是',";
- $_sql .= " `reg_app_id` int(11) NOT NULL DEFAULT '0' COMMENT '注册游戏ID',";
- $_sql .= " `is_new_app` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否游戏新增注册玩家 1 否 2是',";
- $_sql .= " `is_new_role` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否游戏新增角色 1 否 2是',";
- $_sql .= " PRIMARY KEY (`id`),";
- $_sql .= " UNIQUE KEY `ldm_date_mem_app_unique` (`date`,`mem_id`,`app_id`),";
- $_sql .= " KEY `ldm_ip_index` (`last_login_ip`),";
- $_sql .= " KEY `ldm_mem_index` (`mem_id`)";
- $_sql .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='玩家每日';";
- return $this->execute($_sql);
- }
- /**
- * 获取活跃数据汇总排重
- *
- * @param array $where
- * @param $start_time
- * @param $end_time
- * @param string $group
- *
- * @return int|string
- */
- public function getActiveUserSum($where = [], $start_time, $end_time, $group = 'mem_id') {
- $_cnt = 0;
- $_start_date = date('Ym', $start_time);
- $_end_date = date('Ym', $end_time);
- if ($_end_date < $_start_date) {
- return $_cnt;
- }
- if ($_start_date == $_end_date) {
- $_date = date('Y-m-d', $start_time);
- $_cnt = $this->computeTableTwo($_date)->where($where)->group($group)->count();
- } else {
- $tableName = [];
- for ($_i = $_start_date; $_i <= $_end_date; $_i++) {
- $_table = $this->getTable().'_'.($_i);
- /* 检查表是否存在 */
- $_rs = $this->query('show tables like "'.$_table.'"');
- if (empty($_rs)) {
- continue;
- }
- /* 构造sql 查询 */
- $tableName[] = 'SELECT * FROM '.$_table;
- }
- if (empty($tableName)) {
- return $_cnt;
- }
- $tableName = '( '.implode(" UNION ", $tableName).') AS '.$this->name;
- $this->setTable($tableName);
- $_cnt = $this->where($where)->group($group)->count();
- }
- return $_cnt;
- }
- }
|