HourArchiveLogic.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * HourArchiveLogic.php UTF-8
  4. * 每小时数据逻辑 #13831
  5. *
  6. * @date : 2020/12/16 14:00
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HUOSDK-Mp
  11. */
  12. namespace huomp\logic\data;
  13. use huo\model\common\CommonModel;
  14. use huo\model\log\MemLoginLogModel;
  15. use huo\model\member\MemberModel;
  16. use huo\model\member\MemGameModel;
  17. use huo\model\order\OrderModel;
  18. use huolib\constant\CommonConst;
  19. use huolib\constant\OrderConst;
  20. use huolib\tool\Time;
  21. use huomp\model\hour\DayHourLogModel;
  22. use think\Exception;
  23. use think\Log;
  24. class HourArchiveLogic extends CommonModel {
  25. protected $date = '';
  26. protected $hour = 0;
  27. protected $dw_conf = '';
  28. protected $mem_model = '';
  29. protected $mem_game_model = '';
  30. public function __construct($name = null) {
  31. parent::__construct($name);
  32. if (file_exists(GLOBAL_CONF_PATH.'database_dw.php')) {
  33. $this->dw_conf = include GLOBAL_CONF_PATH.'database_dw.php';
  34. }
  35. }
  36. /**
  37. * 计算每个小时的数据
  38. *
  39. * @param string $date 日期 2020-01-01
  40. * @param int $hour 小时 0~23
  41. *
  42. * @return bool
  43. */
  44. public function archiveHour($date, $hour) {
  45. $_date = $date;
  46. $_hour = $hour;
  47. if (empty($_date)) {
  48. $_date = date('Y-m-d');
  49. }
  50. /* 数据库处理 */
  51. try {
  52. $this->date = $_date;
  53. $this->hour = $_hour;
  54. $_config = $this->dw_conf;
  55. $_ts = time();
  56. $_log_date = date('Y-m-d H:i:s', $_ts);
  57. Log::write("ETL archiveHour at $_log_date and date = $_date and hour= $_hour", 'etl', true);
  58. $this->mem_model = new MemberModel();
  59. $this->mem_game_model = new MemGameModel();
  60. $_start_time = strtotime($_date) + $_hour * CommonConst::CONST_HOUR_SECONDS; /* 开始时间 */
  61. $_end_time = $_start_time + CommonConst::CONST_HOUR_SECONDS; /* 结束时间 */
  62. {
  63. /* 从登陆记录获取每日玩家登陆信息 */
  64. $_dh_data = $this->getLoginLog($_start_time, $_end_time);
  65. /* 从订单表获取充值数据 */
  66. $_dh_data = $this->calculateOrderData($_dh_data, $_start_time, $_end_time);
  67. }
  68. /* 写入设备每时表 */
  69. $_dh_model = new DayHourLogModel();
  70. $_dh_model->checkTable($_start_time); /* 检查表 */
  71. if (!empty($_dh_data)) {
  72. $_dh_model->computeTable($this->date)->insertAll($_dh_data, true); /* 插入所有数据 */
  73. }
  74. /* 存储过程计算每小时数据 */
  75. db('', $_config, true)->query("call runhourdata('$_date',$_hour)");
  76. } catch (Exception $_e) {
  77. Log::write($_e->getTraceAsString(), 'etl', true);
  78. Log::write("Error:code:".$_e->getCode().";msg:".$_e->getMessage(), 'etl', true);
  79. }
  80. return true;
  81. }
  82. /**
  83. * 计算充值订单数据
  84. *
  85. * @param array $data 原始数据
  86. * @param int $start_time 开始时间
  87. * @param int $end_time 结束时间
  88. *
  89. * @return array
  90. */
  91. public function calculateOrderData($data, $start_time, $end_time) {
  92. if (empty($data)) {
  93. return $data;
  94. }
  95. $_model = new OrderModel();
  96. $_map = [
  97. 'create_time' => ['between', [$start_time, $end_time]],
  98. 'status' => OrderConst::PAY_STATUS_SUC,
  99. ];
  100. $_field = [
  101. 'mem_id' => 'mem_id',
  102. 'agent_id' => 'agent_id',
  103. 'mg_mem_id' => 'mg_mem_id',
  104. 'app_id' => 'app_id',
  105. "IFNULL(SUM(`amount`),0)" => 'sum_money',
  106. "IFNULL(SUM(`real_amount`),0)" => 'sum_real_money',
  107. "COUNT(`id`)" => 'order_cnt',
  108. ];
  109. $_group = 'mem_id,app_id';
  110. $_order_list = $_model->where($_map)
  111. ->field($_field)
  112. ->group($_group)
  113. ->select();
  114. if (is_object($_order_list)) {
  115. $_order_list = $_order_list->toArray();
  116. }
  117. foreach ($_order_list as $_k => $_v) {
  118. $_key = $_v['mem_id'].'_'.$_v['app_id'];
  119. if (!isset($data[$_key])) {
  120. $data[$_key] = $this->getDefaultData($_v['mem_id'], $_v['mg_mem_id']);
  121. }
  122. $data[$_key]['sum_money'] = $_v['sum_money'];
  123. $data[$_key]['sum_real_money'] = $_v['sum_real_money'];
  124. $data[$_key]['order_cnt'] = $_v['order_cnt'];
  125. unset($data[$_key]['mg_mem_id']);
  126. }
  127. return $data;
  128. }
  129. /**
  130. * 获取设备打开记录log
  131. *
  132. * @param int $start_time 开始时间
  133. * @param int $end_time 结束时间
  134. *
  135. * @return array
  136. */
  137. public function getLoginLog($start_time, $end_time) {
  138. $_model = new MemLoginLogModel();
  139. $_map = [
  140. 'create_time' => ['between', [$start_time, $end_time]]
  141. ];
  142. $_field = [
  143. 'date' => 'date',
  144. "FROM_UNIXTIME( create_time, '%H')" => 'hour_key',
  145. 'mem_id' => 'mem_id',
  146. 'agent_id' => 'agent_id',
  147. 'app_id' => 'app_id',
  148. 'mg_mem_id' => 'mg_mem_id',
  149. "COUNT(`id`)" => 'login_cnt',
  150. ];
  151. $_group = 'mem_id,app_id';
  152. $_list = $_model->computeTable(date('Y-m-d', $start_time))
  153. ->where($_map)
  154. ->field($_field)
  155. ->group($_group)
  156. ->select();
  157. if (is_object($_list)) {
  158. $_list = $_list->toArray();
  159. }
  160. if (empty($_list)) {
  161. return [];
  162. }
  163. $_rdata = [];
  164. foreach ($_list as $_k => $_v) {
  165. $_default_data = $this->getDefaultData($_v['mem_id'], $_v['mg_mem_id']);
  166. $_data = array_merge($_default_data, $_v);
  167. $_key = $_v['mem_id'].'_'.$_v['app_id'];
  168. $_rdata[$_key] = $_data;
  169. unset($_rdata[$_key]['mg_mem_id']);
  170. }
  171. return $_rdata;
  172. }
  173. /**
  174. * 获取初始数据
  175. *
  176. * @param $mem_id
  177. * @param $mg_mem_id
  178. *
  179. * @return array
  180. */
  181. protected function getDefaultData($mem_id, $mg_mem_id) {
  182. if (empty($this->date)) {
  183. $_time = time();
  184. } else {
  185. $_time = strtotime($this->date);
  186. }
  187. $_data = [
  188. 'date' => $this->date,
  189. 'hour_key' => $this->hour,
  190. 'mem_id' => $mem_id,
  191. 'agent_id' => 0,
  192. 'app_id' => 0,
  193. 'reg_time' => 0,
  194. 'reg_days' => 0,
  195. 'reg_hour_key' => 0,
  196. 'game_reg_time' => 0,
  197. 'game_reg_days' => 0,
  198. 'game_reg_hour_key' => 0,
  199. 'login_cnt' => 0,
  200. 'sum_money' => 0,
  201. 'sum_real_money' => 0,
  202. 'order_cnt' => 0,
  203. ];
  204. $_mem_reg_time = $this->mem_model->getCreateTimeById($mem_id);
  205. $_data['reg_time'] = $_mem_reg_time;
  206. $_data['reg_days'] = Time::timeDateDiff($_mem_reg_time, $_time);
  207. $_data['reg_hour_key'] = date('H', $_mem_reg_time);
  208. if (!empty($mg_mem_id)) {
  209. $_mg_info = $this->mem_game_model->getInfoById($mg_mem_id);
  210. $_data['app_id'] = get_val($_mg_info, 'app_id', 0);
  211. $_data['game_reg_time'] = get_val($_mg_info, 'create_time', 0);
  212. $_data['game_reg_days'] = Time::timeDateDiff($_data['game_reg_time'], $_time);
  213. $_data['game_reg_hour_key'] = date('H', $_data['game_reg_time']);
  214. }
  215. return $_data;
  216. }
  217. }