DayMemLogModel.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * DayMemLogModel.php UTF-8
  4. * 玩家每日数据
  5. *
  6. * @date : 2018/5/31 10:58
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\log;
  13. use huo\controller\member\MemCache;
  14. use huo\model\game\GameModel;
  15. use huolib\tool\Time;
  16. class DayMemLogModel extends LogModel {
  17. protected $name = 'log_day_mem';
  18. /**
  19. * LogModel constructor.
  20. *
  21. * @param array $data
  22. */
  23. public function __construct($data = []) {
  24. $_db_conf = [];
  25. if (file_exists(GLOBAL_CONF_PATH.'database_log.php')) {
  26. $_db_conf = include GLOBAL_CONF_PATH.'database_log.php';
  27. }
  28. $this->connection = $_db_conf;
  29. parent::db();
  30. parent::__construct($data);
  31. }
  32. public function game() {
  33. return $this->hasOne(GameModel::className(), 'id', 'app_id')
  34. ->field(['id', 'name', 'icon']);
  35. }
  36. /**
  37. * @param array $where
  38. * date
  39. * mem_id
  40. * app_id
  41. *
  42. * @return array|bool|false
  43. */
  44. public function getDetail($where) {
  45. $_map['date'] = isset($where['date']) ? $where['date'] : date('Y-m-d');
  46. $_map['mem_id'] = isset($where['mem_id']) ? $where['mem_id'] : 0;
  47. $_map['app_id'] = isset($where['app_id']) ? $where['app_id'] : 0;
  48. $_data = $this->computeTable($_map['date'])->where($_map)->find();
  49. if (is_object($_data)) {
  50. $_data = $_data->toArray();
  51. }
  52. if (empty($_data)) {
  53. return false;
  54. }
  55. return $_data;
  56. }
  57. /**
  58. * 计算总登陆次数
  59. *
  60. * @param $where
  61. *
  62. * @return float|int
  63. */
  64. public function getSumLoginCnt($where) {
  65. $_map['date'] = isset($where['date']) ? $where['date'] : date('Y-m-d');
  66. $_map['mem_id'] = isset($where['mem_id']) ? $where['mem_id'] : 0;
  67. $_sum_login_cnt = $this->computeTable($_map['date'])->where($_map)->sum('login_cnt');
  68. if (empty($_sum_login_cnt)) {
  69. return 0;
  70. }
  71. return $_sum_login_cnt;
  72. }
  73. /**
  74. * @param $data
  75. * @param bool $replace
  76. * @param bool $get_last_insert_id
  77. *
  78. * @return bool|int|string
  79. */
  80. public function insertLog($data, $replace = true, $get_last_insert_id = true) {
  81. return parent::insertLog($data, $replace, $get_last_insert_id);
  82. }
  83. /**
  84. * 更新每日玩家表
  85. *
  86. * @param array $data
  87. *
  88. * @param int $id 主键ID
  89. *
  90. * @return bool
  91. */
  92. public function updateLog($data, $id) {
  93. $_map['id'] = $id;
  94. if (false === $this->computeTable($data['date'])->where($_map)->update($data)) {
  95. return false;
  96. } else {
  97. return true;
  98. }
  99. }
  100. /**
  101. * 添加数据
  102. *
  103. * @param array $data 需要添加的数据
  104. *
  105. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  106. */
  107. public function addData($data) {
  108. if (empty($data['date'])) {
  109. return false;
  110. }
  111. $_data = $data;
  112. $_id = $this->computeTableTwo($_data['date'])->insert($_data, true, true);
  113. if (false === $_id) {
  114. return false;
  115. }
  116. /* TAG缓存操作 */
  117. return $_id;
  118. }
  119. /**
  120. * 获取信息
  121. *
  122. * @param string $date 日期
  123. * @param int $mem_id 玩家ID
  124. * @param int $app_id 应用ID
  125. * @param string $device_id 设备
  126. *
  127. * @return array|false
  128. */
  129. public function getInfoByDateMemAppDevice($date, $mem_id, $app_id, $device_id = '') {
  130. /* 缓存操作 */
  131. $_map = [
  132. 'date' => $date,
  133. 'mem_id' => $mem_id,
  134. 'app_id' => $app_id,
  135. // 'device_id' => $device_id,
  136. ];
  137. $_data = $this->computeTableTwo($_map['date'])->where($_map)->find();
  138. if (is_object($_data)) {
  139. $_data = $_data->toArray();
  140. }
  141. if (empty($_data)) {
  142. return [];
  143. }
  144. return $_data;
  145. }
  146. /**
  147. * 更新单条数据
  148. *
  149. * @param array $data 数据
  150. * @param int $id ID
  151. *
  152. * @return bool
  153. */
  154. public function updateData($data, $id) {
  155. if (empty($data['date'])) {
  156. return false;
  157. }
  158. $_data = $this->getInfoByDateMemAppDevice($data['date'], $data['mem_id'], $data['app_id']);
  159. if (!empty($_data)) {
  160. $_data = array_merge($_data, $data);
  161. } else {
  162. $_data = $data;
  163. $_time = time();
  164. $_mem_data = (new MemCache())->getInfoById($data['mem_id']);
  165. $_data['last_login_ip'] = ip2long($_mem_data['reg_ip']);
  166. $_data['device_id'] = $_mem_data['device_id'];
  167. $_data['reg_time'] = $_mem_data['create_time'];
  168. $_data['reg_days'] = Time::timeDateDiff($_data['reg_time'], $_time);
  169. }
  170. $_id = $this->addData($_data);
  171. return $_id;
  172. }
  173. /**
  174. * 创建表
  175. *
  176. * @param int $create_time
  177. *
  178. * @return int
  179. */
  180. function checkTable($create_time = 0) {
  181. $_time = !empty($create_time) ? $create_time : time();
  182. $this->partition_data[$this->partition_field] = date('Y-m-d', $_time);
  183. $this->table = $this->getPartitionTableName(
  184. $this->partition_data, $this->partition_field, $this->partition_rule
  185. );
  186. $_sql = "CREATE TABLE IF NOT EXISTS `$this->table` (";
  187. $_sql .= " `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '',";
  188. $_sql .= " `date` date NOT NULL COMMENT '日期',";
  189. $_sql .= " `mem_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '玩家ID',";
  190. $_sql .= " `agent_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '归属渠道',";
  191. $_sql .= " `app_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '游戏ID',";
  192. $_sql .= " `reg_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '玩家注册时间',";
  193. $_sql .= " `reg_days` int(11) NOT NULL DEFAULT '0' COMMENT '注册天数',";
  194. $_sql .= " `role_days` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创角天数',";
  195. $_sql .= " `device_id` varchar(64) NOT NULL DEFAULT '' COMMENT '手机设备码',";
  196. $_sql .= " `device_cnt` int(11) NOT NULL DEFAULT '1' COMMENT '登陆的设备个数',";
  197. $_sql .= " `login_cnt` int(11) NOT NULL DEFAULT '1' COMMENT '登陆次数',";
  198. $_sql .= " `sum_money` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '累计充值',";
  199. $_sql .= " `sum_real_money` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '累计真实充值',";
  200. $_sql .= " `first_pay_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '第一笔充值时间',";
  201. $_sql .= " `last_pay_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '最近充值时间',";
  202. $_sql .= " `last_money` decimal(20,2) NOT NULL DEFAULT '0.00' COMMENT '最近充值金额',";
  203. $_sql .= " `order_cnt` int(20) unsigned NOT NULL DEFAULT '0' COMMENT '订单数量',";
  204. $_sql .= " `order_suc_cnt` int(20) unsigned NOT NULL DEFAULT '0' COMMENT '支付成功订单数量',";
  205. $_sql .= " `last_login_ip` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '玩家最近登陆ip',";
  206. $_sql .= " `auth_cnt` int(11) NOT NULL DEFAULT '0' COMMENT '授权登陆次数',";
  207. $_sql .= " `is_cpa` tinyint(4) UNSIGNED unsigned NOT NULL DEFAULT 1 COMMENT '是否计算cpa 1 否 2是',";
  208. $_sql .= " `is_auth` tinyint(4) UNSIGNED unsigned NOT NULL DEFAULT 1 COMMENT '是否第三方注册 1 否 2是',";
  209. $_sql .= " `reg_app_id` int(11) NOT NULL DEFAULT '0' COMMENT '注册游戏ID',";
  210. $_sql .= " `is_new_app` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否游戏新增注册玩家 1 否 2是',";
  211. $_sql .= " `is_new_role` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否游戏新增角色 1 否 2是',";
  212. $_sql .= " PRIMARY KEY (`id`),";
  213. $_sql .= " UNIQUE KEY `ldm_date_mem_app_unique` (`date`,`mem_id`,`app_id`),";
  214. $_sql .= " KEY `ldm_ip_index` (`last_login_ip`),";
  215. $_sql .= " KEY `ldm_mem_index` (`mem_id`)";
  216. $_sql .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='玩家每日';";
  217. return $this->execute($_sql);
  218. }
  219. /**
  220. * 获取活跃数据汇总排重
  221. *
  222. * @param array $where
  223. * @param $start_time
  224. * @param $end_time
  225. * @param string $group
  226. *
  227. * @return int|string
  228. */
  229. public function getActiveUserSum($where = [], $start_time, $end_time, $group = 'mem_id') {
  230. $_cnt = 0;
  231. $_start_date = date('Ym', $start_time);
  232. $_end_date = date('Ym', $end_time);
  233. if ($_end_date < $_start_date) {
  234. return $_cnt;
  235. }
  236. if ($_start_date == $_end_date) {
  237. $_date = date('Y-m-d', $start_time);
  238. $_cnt = $this->computeTableTwo($_date)->where($where)->group($group)->count();
  239. } else {
  240. $tableName = [];
  241. for ($_i = $_start_date; $_i <= $_end_date; $_i++) {
  242. $_table = $this->getTable().'_'.($_i);
  243. /* 检查表是否存在 */
  244. $_rs = $this->query('show tables like "'.$_table.'"');
  245. if (empty($_rs)) {
  246. continue;
  247. }
  248. /* 构造sql 查询 */
  249. $tableName[] = 'SELECT * FROM '.$_table;
  250. }
  251. if (empty($tableName)) {
  252. return $_cnt;
  253. }
  254. $tableName = '( '.implode(" UNION ", $tableName).') AS '.$this->name;
  255. $this->setTable($tableName);
  256. $_cnt = $this->where($where)->group($group)->count();
  257. }
  258. return $_cnt;
  259. }
  260. }