UserRegisterDayStat.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qincp
  5. * Date: 2017/11/20
  6. * Time: 下午5:26
  7. */
  8. namespace App\Console\Commands;
  9. use App\Modules\Statistic\Services\SendStatsEmailService;
  10. use App\Modules\Trade\Services\OrderService;
  11. use App\Modules\User\Models\RegisterUserDayStat;
  12. use App\Modules\User\Models\User;
  13. use DB;
  14. use GuzzleHttp\Client;
  15. use Illuminate\Console\Command;
  16. use Log;
  17. class UserRegisterDayStat extends Command
  18. {
  19. /**
  20. * 执行命令 php artisan force_user_active
  21. *
  22. * The name and signature of the console command.
  23. *
  24. * @var string
  25. */
  26. protected $signature = 'user_register_day_stat';
  27. /**
  28. * The console command description.
  29. *
  30. * @var string
  31. */
  32. protected $description = '获取每日注册的用户的地域信息';
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. Log::info('UserRegisterDayStat begin');
  41. /* for ($index = -443; $index <= -2; $index++) {
  42. $date = date("Y-m-d", strtotime($index . " day"));
  43. $this->loadInfo($date);
  44. }*/
  45. $date = date("Y-m-d", strtotime("-2 day"));
  46. $this->loadInfo($date);
  47. //如果是 周一
  48. if (date("l") == 'Monday') {
  49. $this->sendEmail();
  50. }
  51. }
  52. function loadInfo($date)
  53. {
  54. $begin_time = $date;
  55. $end_time = $begin_time . ' 23:59:59';
  56. $idIndex = User::getIdIndex(compact('begin_time', 'end_time'));
  57. if ($idIndex) {
  58. $page_size = 2000;
  59. $max_id = $idIndex->max_id;
  60. $min_id = $idIndex->min_id;
  61. echo 'max_id: ' . $max_id . "\r\n";
  62. echo 'min_id: ' . $min_id . "\r\n";
  63. $begin_index = $min_id;
  64. while ($begin_index < $max_id) {
  65. //最后一页获取剩下的条数
  66. if ($begin_index + $page_size > $max_id) {
  67. $page_size = $max_id - $begin_index;
  68. }
  69. //如果是访问后几页的数据,id 就从上一次的后一个开始获取/**/
  70. if ($begin_index > $min_id) {
  71. $begin_index = $begin_index + 1;
  72. }
  73. $users = DB::table('users')->where('id', '>=', $begin_index)->where('created_at', '<=', $end_time)->limit($page_size)->get();
  74. //获取上一次访问的用户的最大的id
  75. $lastUserId = $this->loadUserInfos($users, $begin_time);
  76. $begin_index = $lastUserId;
  77. }
  78. }
  79. }
  80. function loadUserInfos($users, $begin_time)
  81. {
  82. $userId = 0;
  83. foreach ($users as $user) {
  84. $userId = $user->id;
  85. $register_ip = $user->register_ip;
  86. $city = '未知';
  87. $country = '未知';
  88. $province = '未知';
  89. if ($register_ip) {
  90. //获取ip所在的城市
  91. $area = $this->getIpBelongArea($register_ip);
  92. if ($area) {
  93. if ('未知' != $area) {
  94. $country = $area[0];
  95. $province = $area[1];
  96. $city = $area[2];
  97. }
  98. //查询用户的24小时内的充值
  99. $orderNum = $this->getOneDayRechargeAmount($user->id, $user->created_at);
  100. $this->saveDetail($user->id, $country, $province, $city, $orderNum, $begin_time);
  101. $model = RegisterUserDayStat::getRecord($begin_time, $country, $province, $city);
  102. if ($model) {
  103. $model->register_user_num = $model->register_user_num + 1;
  104. $model->recharge_in_one_day = $model->recharge_in_one_day + $orderNum;
  105. $model->arpu_in_one_day = round($model->recharge_in_one_day / $model->register_user_num, 2);
  106. $model->save();
  107. } else {
  108. $data = ['date' => $begin_time, 'country' => $country,
  109. 'province' => $province, 'city' => $city,
  110. 'register_user_num' => 1, 'recharge_in_one_day' => $orderNum,
  111. 'arpu_in_one_day' => $orderNum];
  112. RegisterUserDayStat::addItem($data);
  113. }
  114. }
  115. }
  116. }
  117. return $userId;
  118. }
  119. public function saveDetail($uid, $country, $province, $city, $recharge_in_one_day, $date)
  120. {
  121. $created_at = date('Y-m-d H:i:s');
  122. $updated_at = date('Y-m-d H:i:s');
  123. $data = compact('uid', 'country', 'province', 'city',
  124. 'recharge_in_one_day', 'date', 'updated_at', 'created_at');
  125. DB::table('promotion_day_city_user_detail_stats')->insert($data);
  126. }
  127. /**
  128. * 获取用户24小时内的充值
  129. * @param $uid 用户id
  130. * @param $time 开始时间
  131. * @return float 充值金额
  132. */
  133. function getOneDayRechargeAmount($uid, $time)
  134. {
  135. $order_benin_time = date('Y-m-d H:i:s', strtotime($time));
  136. $order_end_time = date('Y-m-d H:i:s', strtotime($time) + 86400);
  137. $order_params = ['uid' => $uid, 'begin_time' => $order_benin_time, 'end_time' => $order_end_time];
  138. return OrderService::getAmount($order_params);
  139. }
  140. /**
  141. * 获取ip 所归属的省份、城市信息
  142. * @param $ip ip
  143. * @return string 省份城市
  144. */
  145. function getIpBelongArea($ip)
  146. {
  147. $area = '未知';
  148. $finalIp = $this->getFinalIp($ip);
  149. if ($finalIp) {
  150. $city = new \ipip\db\City(app_path('Libs/ipip/ipipfree.ipdb'));
  151. try {
  152. $city = $city->find($ip, 'CN');
  153. if ($city && count($city) == 3) {
  154. //$area = $city[1] . $city[2];
  155. $area = $city;
  156. }
  157. } catch (\Exception $e) {
  158. \Log::info($e->getMessage());
  159. }
  160. }
  161. return $area;
  162. }
  163. function getFinalIp($ip)
  164. {
  165. $finalIp = '';
  166. $reg = "/(?:(?:2[0-4][0-9]\.)|(?:25[0-5]\.)|(?:1[0-9][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.)){3}(?:(?:2[0-5][0-5])|(?:25[0-5])|(?:1[0-9][0-9])|(?:[1-9][0-9])|(?:[0-9]))/";
  167. preg_match_all($reg, $ip, $matches);
  168. if ($matches && count($matches) > 0) {
  169. if ($matches[0] && count($matches[0] > 0)) {
  170. $finalIp = $matches[0][0];
  171. }
  172. }
  173. return $finalIp;
  174. }
  175. function sendEmail()
  176. {
  177. $begin_date = date("Y-m-d", strtotime("-8 day"));
  178. $end_date = date("Y-m-d", strtotime("-2 day"));
  179. $result = RegisterUserDayStat::getWeekTotalInfo($begin_date, $end_date);
  180. if ($result && count($result) > 0) {
  181. $to_user = array(
  182. ['address'=>'songdb@iqiyoo.com','name'=>'songdb'],
  183. ['address'=>'zhaojp@yqsd.net','name'=>'赵君平'],
  184. ['address' => 'zhoulj@iqiyoo.com', 'name' => '周灵杰'],
  185. );
  186. $content = "<table border='1' cellpadding='10' cellspacing='0'><tr><td align='center'>序号</td><td align='center'>地区</td><td align='center'>注册人数</td><td align='center'>注册用户24小时充值</td><td align='center'>注册用户24小时arpu值</td></tr>";
  187. $index = 0;
  188. foreach ($result as $item) {
  189. $index++;
  190. $arpu = round($item['sum_arup'], 2);
  191. $aera = $item['country'] . '-' . $item['province'];
  192. $content .= "<tr><td align='center'>{$index}</td><td align='center'>{$aera}</td><td align='center'>{$item['sum_register_user_num']}</td><td align='center'>{$item['sum_recharge_in_one_day']}</td><td align='center'>{$arpu}</td></tr>";
  193. }
  194. $content .= "</table>";
  195. SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => '网读云'.$begin_date . '至' . $end_date . " 注册用户信息汇总情况", 'body' => $content]);
  196. }
  197. }
  198. }