DataCreate.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * DataCreate.php UTF-8
  4. * 测试数据生成
  5. *
  6. * @date : 2018/10/25 15:47
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace console\index\controller;
  13. use huo\controller\agent\Agent;
  14. use huo\controller\member\MemCache;
  15. use huo\model\game\GameModel;
  16. use huo\model\log\DayMemLogModel;
  17. use huo\model\member\MemberModel;
  18. use huo\model\user\RoleModel;
  19. use huo\model\user\UserModel;
  20. use huolib\constant\AgentConst;
  21. use huolib\constant\GameConst;
  22. use huolib\status\CommonStatus;
  23. use huolib\tool\Ip;
  24. use huolib\tool\Time;
  25. use think\console\Command;
  26. use think\console\Input;
  27. use think\console\Output;
  28. class DataCreate extends Command {
  29. protected function configure() {
  30. $this->setName('datacreate')->setDescription('数据生成');
  31. }
  32. /**
  33. * /www/wdlinux/php/bin/php /huosdk/mini/think datacreate
  34. *
  35. * @param Input $input
  36. * @param Output $output
  37. *
  38. * @return int|null|void
  39. */
  40. protected function execute(Input $input, Output $output) {
  41. $this->addAgent();
  42. $this->addMember(10000000, '2018-09-01');
  43. $this->addData('2018-07-01', '2018-10-27');
  44. }
  45. public function getAppIds() {
  46. $_map['classify'] = ['in', [GameConst::GAME_MP, GameConst::GAME_MP_BOX, GameConst::GAME_MP_RPBOX]];
  47. return (new GameModel())->where($_map)->column('id');
  48. }
  49. public function addData($start_date = '', $end_date = '') {
  50. // (new DayMemLogModel())->execute('TRUNCATE `h_log_day_mem_201810`;');
  51. $_add_cnt = 0;
  52. $_start_date = $start_date;
  53. $_end_date = $end_date;
  54. $_start_time = strtotime($_start_date);
  55. $_end_time = strtotime($_end_date);
  56. $_mem_ids = (new MemberModel())->getIdsByParentMemId(0);
  57. $_app_ids = $this->getAppIds();
  58. $_mem_cnt = count($_mem_ids);
  59. $_game_cnt = count($_app_ids);
  60. while ($_start_time <= $_end_time) {
  61. $_date = date('Y-m-d', $_start_time);
  62. $_rand_mem_cnt = rand(10, $_mem_cnt);
  63. $_rand_game_cnt = rand(1, $_game_cnt);
  64. for ($_game_i = 0; $_game_i < $_rand_game_cnt; $_game_i++) {
  65. $_game_id = $_app_ids[array_rand($_app_ids, 1)];
  66. for ($_mem_i = 0; $_mem_i < $_rand_mem_cnt; $_mem_i++) {
  67. $_mem_id = $_mem_ids[array_rand($_mem_ids, 1)];
  68. $_mem_data = MemCache::ins()->getInfoById($_mem_id);
  69. if ($_mem_data['create_time'] > $_start_time + 86400) {
  70. break;
  71. }
  72. $_time = $_start_time + rand(1, 86400);
  73. $_login_cnt = rand(1, 200);
  74. $_auth_cnt = rand(0, $_login_cnt);
  75. $_sum_money = rand(0, 200);
  76. $_diff = $_time - $_start_time;
  77. $_first_pay_time = 0;
  78. $_last_pay_time = 0;
  79. $_order_cnt = 0;
  80. $_order_suc_cnt = 0;
  81. $_last_money = 0;
  82. if (!empty($_sum_money)) {
  83. $_order_cnt = rand(0, 200);
  84. $_order_suc_cnt = rand(0, $_order_cnt);
  85. if ($_order_suc_cnt > 0) {
  86. $_first_pay_time = $_mem_data['create_time'] + rand(1, $_diff);
  87. }
  88. if ($_order_suc_cnt > 1) {
  89. $_last_money = rand(0, $_sum_money);
  90. if (!empty($_last_money)) {
  91. $_last_pay_time = $_first_pay_time + rand(1, $_diff);
  92. }
  93. }
  94. }
  95. $_sum_real_money = rand(0, $_sum_money);
  96. $_data = [
  97. 'date' => $_date,
  98. 'mem_id' => $_mem_id,
  99. 'agent_id' => $_mem_data['agent_id'],
  100. 'app_id' => $_game_id,
  101. 'reg_time' => $_mem_data['create_time'],
  102. 'reg_days' => Time::timeDateDiff($_mem_data['create_time'], $_time),
  103. 'device_id' => '',
  104. 'device_cnt' => 0,
  105. 'login_cnt' => $_login_cnt,
  106. 'sum_money' => $_sum_money,
  107. 'sum_real_money' => $_sum_real_money,
  108. 'first_pay_time' => $_first_pay_time,
  109. 'last_pay_time' => $_last_pay_time,
  110. 'last_money' => $_last_money,
  111. 'order_cnt' => $_order_cnt,
  112. 'order_suc_cnt' => $_order_suc_cnt,
  113. 'last_login_ip' => Ip::ipToLong($_mem_data['reg_ip']),
  114. 'auth_cnt' => $_auth_cnt,
  115. ];
  116. $_rs = (new DayMemLogModel())->insertLog($_data, true);
  117. if (false !== $_rs) {
  118. $_add_cnt++;
  119. }
  120. }
  121. }
  122. $_start_time = $_start_time + 86400;
  123. }
  124. print_r($_add_cnt.' mem create ok'.PHP_EOL);
  125. }
  126. /**
  127. * @param int $cnt
  128. */
  129. public function addAgent($cnt = 100) {
  130. for ($_i = 0; $_i < $cnt; $_i++) {
  131. $_user_login = 'login'.$_i;
  132. $_user_nicename = $_user_login;
  133. $_user_pass = $_user_login;
  134. $_parent_id = 0;
  135. $_role_id = AgentConst::AGENT_ROLE_MP_AGENT;
  136. $_rs = (new Agent())->addAgent($_user_login, $_user_nicename, $_user_pass, $_parent_id, $_role_id);
  137. if (CommonStatus::NO_ERROR == $_rs['code']) {
  138. print_r('create ok'.PHP_EOL.$_i);
  139. }
  140. }
  141. }
  142. /**
  143. *
  144. *
  145. * // (new MemberModel())->execute('TRUNCATE `h_member`;');
  146. * // (new MemberExtModel())->execute('TRUNCATE `h_mem_ext`;');
  147. *
  148. * @param int $cnt
  149. * @param string $start_date
  150. */
  151. public function addMember($cnt = 1000, $start_date = '') {
  152. $_cnt = $cnt;
  153. $_role_type = AgentConst::ROLE_TYPE_AGENT;
  154. $_role_ids = (new RoleModel())->getIdsByRoleType($_role_type);
  155. $_agent_ids = (new UserModel())->getIdsByRoleId($_role_ids);
  156. if (empty($start_date)) {
  157. $start_date = date('Y-m-d');
  158. }
  159. $_now = time();
  160. $_time = strtotime($start_date);
  161. $_ocnt = 0;
  162. $_app_ids = $this->getAppIds();
  163. for ($_i = 1; $_i < $_cnt; $_i++) {
  164. $_key = array_rand($_agent_ids, 1);
  165. $_ip = rand(0xFFFFF10F, 0xFFFFFFFF);
  166. $_time = $_time + rand(120, 600);
  167. if ($_time > $_now) {
  168. break;
  169. }
  170. $_username = 'testmem'.$_i * 3;
  171. $_status_rand = rand(0, 98);
  172. $_status = 1;
  173. if ($_status_rand) {
  174. $_status = 2;
  175. }
  176. $_mem_data = [
  177. 'username' => $_username,
  178. 'nickname' => $_username,
  179. 'password' => $_username,
  180. 'reg_ip' => Ip::longToIp($_ip),
  181. 'create_time' => $_time,
  182. 'agent_id' => $_agent_ids[$_key],
  183. 'status' => $_status
  184. ];
  185. $_rs = (new MemberModel())->addMem($_mem_data);
  186. if (false != $_rs) {
  187. $_game_id = $_app_ids[array_rand($_app_ids, 1)];
  188. $_data = [
  189. 'date' => date('Y-m-d', $_time),
  190. 'mem_id' => $_rs,
  191. 'agent_id' => $_agent_ids[$_key],
  192. 'app_id' => $_game_id,
  193. 'reg_time' => $_time,
  194. 'reg_days' => '0',
  195. 'device_id' => '',
  196. 'device_cnt' => 0,
  197. 'login_cnt' => 1,
  198. 'last_login_ip' => $_ip,
  199. 'auth_cnt' => 0,
  200. ];
  201. (new DayMemLogModel())->insertLog($_data, true);
  202. $_ocnt++;
  203. print_r($_ocnt.' data create ok'.PHP_EOL);
  204. }
  205. }
  206. // print_r($_ocnt.' data create ok'.PHP_EOL);
  207. }
  208. }