GenDataSwitchLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. * GenDataSwitchLogic.php UTF-8
  4. *
  5. *
  6. * @date : 2019/11/21 10:40
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : ouzhongfu <ozf@huosdk.com>
  10. * @version : HUOSDK_NOVEL 1.0
  11. */
  12. namespace huo\logic\data;
  13. use huo\model\agent\AgentCpaLogSwitchModel;
  14. use huo\model\common\CommonModel;
  15. use huo\model\log\DayMemLogSwitchModel;
  16. use huo\model\member\MemberModel;
  17. use huo\model\member\MemGameModel;
  18. use huo\model\member\MemoauthModel;
  19. use huo\model\order\OrderModel;
  20. use huolib\constant\CommonConst;
  21. use huolib\constant\OrderConst;
  22. use think\Exception;
  23. use think\Log;
  24. class GenDataSwitchLogic extends CommonModel {
  25. private $log_db_conf = [];
  26. private $db_conf = [];
  27. private $dw_conf = [];
  28. public function __construct($name = null) {
  29. parent::__construct($name);
  30. if (file_exists(GLOBAL_CONF_PATH.'database_log.php')) {
  31. $this->log_db_conf = include GLOBAL_CONF_PATH.'database_log.php';
  32. }
  33. if (file_exists(GLOBAL_CONF_PATH.'database.php')) {
  34. $this->db_conf = include GLOBAL_CONF_PATH.'database.php';
  35. }
  36. if (file_exists(GLOBAL_CONF_PATH.'database_dw.php')) {
  37. $this->dw_conf = include GLOBAL_CONF_PATH.'database_dw.php';
  38. }
  39. }
  40. /**
  41. * 执行每日数据
  42. *
  43. * @param string $date 日期
  44. *
  45. * @return void
  46. */
  47. function archiveDaily($date = '') {
  48. set_time_limit(0);
  49. $_ts = time();
  50. if (empty($date)) {
  51. $_date = date('Y-m-d', strtotime("-1 day"));
  52. } else {
  53. $_date = $date;
  54. }
  55. $_log_date = date('Y-m-d H:i:s', $_ts);
  56. $_config = $this->getDbConfig();
  57. /* 数据库处理 */
  58. try {
  59. $_start_time = time();
  60. Log::write("ETL archiveDailySwitch at $_log_date and date = $_date", 'etl', true);
  61. $this->genLogMemByLoginData($_date);
  62. $this->genLogMem($_date);
  63. $this->genLogMemGame($_date);
  64. $this->genLogMemCpa($_date);
  65. db('', $_config, true)->query("call rundailyswitch('$_date')");
  66. $_end_time = time();
  67. $_diff = $_end_time - $_start_time;
  68. Log::write("ETL archiveDailySwitch end use time $_diff s", 'etl', true);
  69. } catch (Exception $_e) {
  70. Log::write(
  71. "Error:code:".$_e->getCode().";msg:".$_e->getMessage().';qqq.'.$_e->getTraceAsString(), 'etl', true
  72. );
  73. }
  74. return;
  75. }
  76. /**
  77. * @param $date
  78. */
  79. public function genLogMemByLoginData($date) {
  80. $_date = $date;
  81. $_start_time = strtotime($_date);
  82. $_end_time = strtotime($_date);
  83. $_log_database = $this->log_db_conf['database'];
  84. $_database = $this->db_conf['database'];
  85. $_day_mem_switch_model = new DayMemLogSwitchModel();
  86. while ($_start_time <= $_end_time) {
  87. $_day_mem_switch_model->checkTable($_start_time);
  88. $_day_end_time = $_start_time + 86400;
  89. $_date_key = date('Ym', $_start_time);
  90. $_date_month_key = date('Ym', $_start_time);
  91. $_date = date('Y-m-d', $_start_time);
  92. $_delete_sql = "DELETE FROM ".$_log_database.".log_log_day_mem_switch_".$_date_key." WHERE date='".$_date
  93. ."' ";
  94. db()->execute($_delete_sql);
  95. $_sql = "REPLACE INTO ".$_log_database.".log_log_day_mem_switch_".$_date_key."
  96. (
  97. `date`,
  98. `mem_id`,
  99. `app_id`,
  100. `agent_id`,
  101. `reg_time`,
  102. `reg_days`,
  103. `device_id`,
  104. `login_cnt`,
  105. `last_login_ip`,
  106. `reg_app_id`
  107. )
  108. SELECT
  109. '".$_date."',
  110. `ll`.`mem_id` AS `mem_id`,
  111. `ll`.`app_id` AS `app_id`,
  112. IFNULL(`m`.`agent_id` , '0') AS `agent_id`,
  113. IFNULL(`m`.`create_time` , '0') AS `reg_time`,
  114. (TO_DAYS(FROM_UNIXTIME(`ll`.`create_time`)) - TO_DAYS(FROM_UNIXTIME(IFNULL(`m`.`create_time`,0)))) AS `reg_days`,
  115. `ll`.`device_id` AS `device_id`,
  116. COUNT(`ll`.`id`) AS `login_cnt`,
  117. inet_aton(`ll`.`ip`) AS `last_login_ip`,
  118. `ll`.`reg_app_id` AS `reg_app_id`
  119. FROM
  120. (".$_database.".`h_log_mem_login_".$_date_month_key."` `ll`)
  121. LEFT JOIN ".$_database.".`h_member` m ON ll.mem_id = m.id
  122. WHERE
  123. (FROM_UNIXTIME(`ll`.`create_time`, '%Y-%m-%d') = '".$_date."')
  124. AND m.is_switch = 1
  125. GROUP BY `ll`.`mem_id` , `ll`.`app_id`;";
  126. db()->execute($_sql);
  127. $_start_time = $_day_end_time;
  128. }
  129. }
  130. public function genLogMem($date) {
  131. $_date = $date;
  132. $_start_time = strtotime($_date);
  133. $_end_time = strtotime($_date);
  134. $_order_model = new OrderModel();
  135. $_day_mem_switch_model = new DayMemLogSwitchModel();
  136. $_field = [
  137. 'count(`id`)' => 'order_suc_cnt',
  138. 'app_id' => 'app_id',
  139. 'agent_id' => 'agent_id',
  140. 'mem_id' => 'mem_id',
  141. 'sum(`amount`)' => 'sum_money',
  142. 'sum(`real_amount`)' => 'sum_real_money',
  143. 'create_time' => 'last_pay_time',
  144. 'amount' => 'last_money',
  145. ];
  146. while ($_start_time <= $_end_time) {
  147. $_day_end_time = $_start_time + 86400;
  148. $_map = [
  149. 'status' => 2,
  150. 'create_time' => ['between', [$_start_time, $_day_end_time]],
  151. 'is_switch' => OrderConst::PAY_SWITCH_YES,
  152. ];
  153. $_group = 'mem_id,app_id';
  154. $_datas = $_order_model->where($_map)->field($_field)->group($_group)->select();
  155. if (is_object($_datas)) {
  156. $_datas = $_datas->toArray();
  157. }
  158. foreach ($_datas as $_data) {
  159. $_data['date'] = date('Y-m-d', $_start_time);
  160. $_data['order_cnt'] = $_data['order_suc_cnt'];
  161. unset($_data['id']);
  162. $_rs = $_day_mem_switch_model->updateData($_data, 0);
  163. if (false === $_rs) {
  164. var_dump($_data);
  165. exit;
  166. }
  167. }
  168. $_start_time = $_day_end_time;
  169. }
  170. }
  171. public function genLogMemCpa($date) {
  172. $_date = $date;
  173. $_start_time = strtotime($_date);
  174. $_end_time = strtotime($_date);
  175. $_mem_model = new MemberModel();
  176. $_day_mem_switch_model = new DayMemLogSwitchModel();
  177. $_acl_switch_model = new AgentCpaLogSwitchModel();
  178. $_ma_model = new MemoauthModel();
  179. $_field = [
  180. 'id' => 'mem_id',
  181. 'app_id' => 'app_id',
  182. 'agent_id' => 'agent_id'
  183. ];
  184. while ($_start_time <= $_end_time) {
  185. $_day_end_time = $_start_time + 86400;
  186. $_map = [
  187. 'create_time' => ['between', [$_start_time, $_day_end_time]],
  188. 'is_switch' => OrderConst::PAY_SWITCH_YES,
  189. ];
  190. $_datas = $_mem_model->where($_map)->field($_field)->select();
  191. if (is_object($_datas)) {
  192. $_datas = $_datas->toArray();
  193. }
  194. foreach ($_datas as $_data) {
  195. $_data['date'] = date('Y-m-d', $_start_time);
  196. $_id = $_acl_switch_model->getIdByMemId($_data['mem_id']);
  197. if (!empty($_id)) {
  198. $_data['is_cpa'] = 2;
  199. }
  200. $_openid = $_ma_model->getOpenidByMemId('', $_data['mem_id']);
  201. if (!empty($_openid)) {
  202. $_data['is_auth'] = 2;
  203. }
  204. $_rs = $_day_mem_switch_model->updateData($_data, 0);
  205. if (false === $_rs) {
  206. var_dump($_data);
  207. exit;
  208. }
  209. }
  210. $_start_time = $_day_end_time;
  211. }
  212. }
  213. /**
  214. * 查询玩家游戏是否新创
  215. *
  216. * @param $date
  217. *
  218. * @return bool
  219. */
  220. public function genLogMemGame($date) {
  221. $_date = $date;
  222. $_start_time = strtotime($_date);
  223. $_end_time = $_start_time + CommonConst::CONST_DAY_SECONDS;
  224. $_map = [
  225. 'mem_game_model.create_time' => ['between', [$_start_time, $_end_time]]
  226. ];
  227. $_field = [
  228. 'mem_game_model.id' => 'id',
  229. 'mem_game_model.mem_id' => 'mem_id',
  230. 'mem_game_model.app_id' => 'app_id',
  231. ];
  232. $_mg_model = new MemGameModel();
  233. $_datas = $_mg_model->with(
  234. ['leftmem' => function ($query) {
  235. $query->where(['is_switch' => OrderConst::PAY_SWITCH_YES]);
  236. }]
  237. )->where($_map)->column($_field);
  238. $_day_mem_switch_model = new DayMemLogSwitchModel();
  239. foreach ($_datas as $_mem) {
  240. $_data = [];
  241. $_data['date'] = $_date;
  242. $_data['app_id'] = $_mem['app_id'];
  243. $_data['mem_id'] = $_mem['mem_id'];
  244. $_data['is_new_app'] = CommonConst::STATUS_YES;
  245. $_rs = $_day_mem_switch_model->updateData($_data, 0);
  246. if (false === $_rs) {
  247. var_dump($_data);
  248. exit;
  249. }
  250. }
  251. return true;
  252. }
  253. /**
  254. * 获取数据仓库配置
  255. */
  256. public function getDbConfig() {
  257. $_db_dw = [ // 数据库类型
  258. 'type' => '',
  259. // 服务器地址
  260. 'hostname' => '',
  261. // 数据库名
  262. 'database' => '',
  263. // 用户名
  264. 'username' => '',
  265. // 密码
  266. 'password' => '',
  267. // 端口
  268. 'hostport' => '',
  269. // 数据库编码默认采用utf8
  270. 'charset' => 'utf8mb4',
  271. // 数据库表前缀
  272. 'prefix' => 'dw_',
  273. // 连接dsn
  274. 'dsn' => '',
  275. // 数据库连接参数
  276. 'params' => [],
  277. // 数据库调试模式
  278. 'debug' => true,
  279. // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
  280. 'deploy' => 0,
  281. // 数据库读写是否分离 主从式有效
  282. 'rw_separate' => false,
  283. // 读写分离后 主服务器数量
  284. 'master_num' => 1,
  285. // 指定从服务器序号
  286. 'slave_no' => '',
  287. // 是否严格检查字段是否存在
  288. 'fields_strict' => true,
  289. // 数据集返回类型
  290. 'resultset_type' => 'collection',
  291. // 自动写入时间戳字段
  292. 'auto_timestamp' => false,
  293. // 时间字段取出后的默认时间格式
  294. 'datetime_format' => false,
  295. // 是否需要进行SQL性能分析
  296. 'sql_explain' => false,];
  297. if (file_exists(CMF_ROOT.'data/conf/database_dw.php')) {
  298. $_db_dw = include CMF_ROOT.'data/conf/database_dw.php';
  299. }
  300. return $_db_dw;
  301. }
  302. }