DmlSwitchModel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * DayMemLogModel.php UTF-8
  4. * 玩家每日数据_切量
  5. *
  6. * @date : 2018/5/31 10:58
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling<cbl@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huoAgentSwitch\model;
  13. use huo\model\log\LogModel;
  14. class DmlSwitchModel extends LogModel {
  15. protected $name = 'ldm_switch';
  16. /**
  17. * @param array $where
  18. * date
  19. * mem_id
  20. * app_id
  21. *
  22. * @return array|bool|false
  23. */
  24. public function getDetail($where) {
  25. $_map['date'] = isset($where['date']) ? $where['date'] : date('Y-m-d');
  26. $_map['mem_id'] = isset($where['mem_id']) ? $where['mem_id'] : 0;
  27. $_map['app_id'] = isset($where['app_id']) ? $where['app_id'] : 0;
  28. $_data = $this->computeTable($_map['date'])->where($_map)->find();
  29. if (is_object($_data)) {
  30. $_data = $_data->toArray();
  31. }
  32. if (empty($_data)) {
  33. return false;
  34. }
  35. return $_data;
  36. }
  37. /**
  38. * 计算总登陆次数
  39. *
  40. * @param $where
  41. *
  42. * @return float|int
  43. */
  44. public function getSumLoginCnt($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. $_sum_login_cnt = $this->computeTable($_map['date'])->where($_map)->sum('login_cnt');
  48. if (empty($_sum_login_cnt)) {
  49. return 0;
  50. }
  51. return $_sum_login_cnt;
  52. }
  53. /**
  54. * @param $data
  55. * @param bool $replace
  56. * @param bool $get_last_insert_id
  57. *
  58. * @return bool|int|string
  59. */
  60. public function insertLog($data, $replace = false, $get_last_insert_id = true) {
  61. return parent::insertLog($data, $replace, $get_last_insert_id);
  62. }
  63. /**
  64. * 更新每日玩家表
  65. *
  66. * @param array $data
  67. *
  68. * @param int $id 主键ID
  69. *
  70. * @return bool
  71. */
  72. public function updateLog($data, $id) {
  73. $_map['id'] = $id;
  74. if (false === $this->computeTable($data['date'])->where($_map)->update($data)) {
  75. return false;
  76. } else {
  77. return true;
  78. }
  79. }
  80. /**
  81. * 删除每日玩家表
  82. *
  83. * @param int $mem_id 玩家id
  84. *
  85. * @param string $date 日期
  86. *
  87. * @return bool
  88. */
  89. public function deleteLog($mem_id, $date) {
  90. $_map['date'] = $mem_id;
  91. $_map['mem_id'] = $date;
  92. if (false === $this->computeTable($date)->where($_map)->delete()) {
  93. return false;
  94. } else {
  95. return true;
  96. }
  97. }
  98. /**
  99. * 创建表
  100. *
  101. * @return int
  102. * @throws \think\db\exception\BindParamException
  103. * @throws \think\exception\PDOException
  104. */
  105. function checkTable() {
  106. $this->table = $this->getPartitionTableName(
  107. $this->partition_data, $this->partition_field, $this->partition_rule
  108. );
  109. $_sql = "CREATE TABLE IF NOT EXISTS `$this->table` (";
  110. $_sql .= " `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '',";
  111. $_sql .= " `date` date NOT NULL COMMENT '日期',";
  112. $_sql .= " `mem_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '玩家ID',";
  113. $_sql .= " `agent_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '归属渠道',";
  114. $_sql .= " `app_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '游戏ID',";
  115. $_sql .= " `reg_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '玩家注册时间',";
  116. $_sql .= " `reg_days` int(11) NOT NULL DEFAULT '0' COMMENT '注册天数',";
  117. $_sql .= " `role_days` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创角天数',";
  118. $_sql .= " `device_id` varchar(64) NOT NULL DEFAULT '' COMMENT '手机设备码',";
  119. $_sql .= " `device_cnt` int(11) NOT NULL DEFAULT '1' COMMENT '登陆的设备个数',";
  120. $_sql .= " `login_cnt` int(11) NOT NULL DEFAULT '1' COMMENT '登陆次数',";
  121. $_sql .= " `sum_money` double(12,2) NOT NULL DEFAULT '0.00' COMMENT '累计充值',";
  122. $_sql .= " `sum_real_money` double(12,2) NOT NULL DEFAULT '0.00' COMMENT '累计真实充值',";
  123. $_sql .= " `first_pay_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '第一笔充值时间',";
  124. $_sql .= " `last_pay_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '最近充值时间',";
  125. $_sql .= " `last_money` double(12,2) NOT NULL DEFAULT '0.00' COMMENT '最近充值金额',";
  126. $_sql .= " `order_cnt` int(20) unsigned NOT NULL DEFAULT '0' COMMENT '订单数量',";
  127. $_sql .= " `order_suc_cnt` int(20) unsigned NOT NULL DEFAULT '0' COMMENT '支付成功订单数量',";
  128. $_sql .= " `last_login_ip` varchar(32) NOT NULL DEFAULT '' COMMENT '玩家最近登陆ip',";
  129. $_sql .= " `auth_cnt` int(11) NOT NULL DEFAULT '0' COMMENT '授权登陆次数',";
  130. $_sql .= " PRIMARY KEY (`id`),";
  131. $_sql .= " UNIQUE KEY `ldm_date_mem_app_unique` (`date`,`mem_id`,`app_id`),";
  132. $_sql .= " KEY `ldm_ip_index` (`last_login_ip`),";
  133. $_sql .= " KEY `ldm_mem_index` (`mem_id`)";
  134. $_sql .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='玩家每日';";
  135. return $this->execute($_sql);
  136. }
  137. /**
  138. * 获取活跃数据汇总排重
  139. *
  140. * @param array $where
  141. * @param $start_time
  142. * @param $end_time
  143. * @param string $group
  144. *
  145. * @return int|string
  146. */
  147. public function getActiveUserSum($where = [], $start_time, $end_time, $group = 'mem_id') {
  148. $_cnt = 0;
  149. $_start_date = date('Ym', $start_time);
  150. $_end_date = date('Ym', $end_time);
  151. if ($_end_date < $_start_date) {
  152. return $_cnt;
  153. }
  154. if ($_start_date == $_end_date) {
  155. $_cnt = $this->computeTable($start_time)->where($where)->group($group)->count();
  156. } else {
  157. $tableName = [];
  158. for ($_i = $_start_date; $_i <= $_end_date; $_i++) {
  159. $_table = $this->getTable().'_'.($_i);
  160. /* 检查表是否存在 */
  161. $_rs = $this->query('show tables like "'.$_table.'"');
  162. if (empty($_rs)) {
  163. continue;
  164. }
  165. /* 构造sql 查询 */
  166. $tableName[] = 'SELECT * FROM '.$_table;
  167. }
  168. if (empty($tableName)) {
  169. return $_cnt;
  170. }
  171. $tableName = '( '.implode(" UNION ", $tableName).') AS '.$this->name;
  172. $this->setTable($tableName);
  173. $_cnt = $this->where($where)->group($group)->count();
  174. }
  175. return $_cnt;
  176. }
  177. }