BookSearchInfoStat.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Book\Models\BookSearchStat;
  4. use App\Modules\Statistic\Services\SendStatsEmailService;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Storage;
  7. class BookSearchInfoStat extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'book_search_stat';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '用户搜索图书记录信息';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $end_time = date("Y-m-d", strtotime("-1 day"));
  38. $begin_time = date("Y-m-d", strtotime("-8 day"));
  39. $type = 'WECHAT';
  40. $wechat_search_result = BookSearchStat::getInfo(compact('begin_time', 'end_time', 'type'), true);
  41. $type = 'WAP';
  42. $wap_search_result = BookSearchStat::getInfo(compact('begin_time', 'end_time', 'type'), true);
  43. $this->sendResultEmail($wechat_search_result, $wap_search_result, $begin_time, $end_time);
  44. }
  45. public function sendResultEmail($wechat_search_result, $wap_search_result, $start_date, $end_date)
  46. {
  47. if ($wechat_search_result && count($wechat_search_result) > 0) {
  48. $filename = '微信内搜索.' . 'csv';
  49. Storage::append($filename, mb_convert_encoding("关键词,搜索人数,搜索次数", 'gbk'));
  50. $str = '';
  51. foreach ($wechat_search_result as $item) {
  52. $str .= "$item->content,$item->user_num,$item->search_num\r\n";
  53. }
  54. Storage::append($filename, mb_convert_encoding($str, 'gbk'));
  55. }
  56. if ($wap_search_result && count($wap_search_result) > 0) {
  57. $filename = 'wap内搜索.' . 'csv';
  58. Storage::append($filename, mb_convert_encoding("关键词,搜索人数,搜索次数", 'gbk'));
  59. $str = '';
  60. foreach ($wap_search_result as $item) {
  61. $str .= "$item->content,$item->user_num,$item->search_num\r\n";
  62. }
  63. Storage::append($filename, mb_convert_encoding($str, 'gbk'));
  64. }
  65. $to_user = array(
  66. ['address'=>'songdb@iqiyoo.com','name'=>'songdb'],
  67. ['address'=>'zhaojp@yqsd.net','name'=>'赵君平'],
  68. ['address' => 'zhoulj@iqiyoo.com', 'name' => '周灵杰']
  69. );
  70. $file_path1 = storage_path('app/微信内搜索.csv');
  71. $file_path2 = storage_path('app/wap内搜索.csv');
  72. $attach = [$file_path1, $file_path2];
  73. $body = '搜索关键字统计,详情请见附件';
  74. SendStatsEmailService::SendMultyHtmlEmailWithAcce($to_user, ['subject' => $start_date . '至' . $end_date . " 搜索关键字统计", 'body' => $body], $attach);
  75. unlink($file_path1);
  76. unlink($file_path2);
  77. }
  78. }