AutoAnalysisMessageJob.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the ZhMead/laravel-logger.
  4. *
  5. * (c) Mead <751066209@qql.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Jobs\Manage;
  11. use App\Repositories\Models\Base\Setting;
  12. use App\Repositories\Models\Manage\Message;
  13. use Carbon\Carbon;
  14. use Illuminate\Bus\Queueable;
  15. use Illuminate\Contracts\Queue\ShouldQueue;
  16. use Illuminate\Queue\InteractsWithQueue;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. class AutoAnalysisMessageJob implements ShouldQueue
  20. {
  21. use InteractsWithQueue;
  22. use Queueable;
  23. public $tries = 1;
  24. public $timeout = 1440;
  25. private $messageId;
  26. /**
  27. * Create a new job instance.
  28. */
  29. public function __construct($messageId)
  30. {
  31. $this->messageId = $messageId;
  32. }
  33. /**
  34. * 确定任务应该超时的时间
  35. *
  36. * @return \DateTime
  37. */
  38. public function retryUntil()
  39. {
  40. return Carbon::now()->addHours(24);
  41. }
  42. /**
  43. * Execute the job.
  44. */
  45. public function handle()
  46. {
  47. $this->analysis();
  48. }
  49. /**
  50. * 导入数据
  51. * @return false|void
  52. */
  53. public function analysis()
  54. {
  55. $model = Message::query()->where('id', $this->messageId)->first();
  56. DB::beginTransaction();
  57. try {
  58. Log::error('*************分析数据中*****************');
  59. $messages = Message::query()
  60. ->whereDate('created_at', '>=', Carbon::now()->addDays(-Setting::byCodeGetSetting('auto_analysis_days'))->toDateString())
  61. ->where("more_pid", 0)
  62. ->where("id", '<>', $model->id)
  63. ->select(['id', 'keywords', 'type_id', 'category_1_id', 'category_2_id', 'category_3_id', 'category_4_id', 'category_5_id', 'name', 'mobile', 'address_name', 'address_id', 'body', 'complain_date', 'warn_type_id', 'deal_department_id'])
  64. ->get();
  65. $rateData = null;
  66. foreach ($messages as $m) {
  67. $rate = $this->get_similar_percent($model->keywords, $m->keywords);
  68. Log::error("{$m->id}==>{$rate}");
  69. if ($rate >= (int)Setting::byCodeGetSetting('auto_analysis_similarity')) {
  70. $rateData[] = [
  71. 'mid' => $m->id,
  72. 'rate' => $rate,
  73. ];
  74. }
  75. }
  76. $model->similar_data = $rateData;
  77. $model->save();
  78. DB::commit();
  79. } catch (\Exception $exception) {
  80. DB::rollBack();
  81. Log::error("
  82. **********信息录入对比*****************
  83. exception: {$exception->getMessage()}
  84. ");
  85. }
  86. Log::error("-----------完成---------------");
  87. }
  88. /**
  89. * 相似度
  90. * @param $str1_arr
  91. * @param $str2_arr
  92. * @return float|int
  93. */
  94. function get_similar_percent($str1_arr, $str2_arr)
  95. {
  96. $res_arr = [];
  97. foreach ($str1_arr as $word) {
  98. if (!isset($res_arr[$word])) {
  99. $res_arr[$word] = ['A' => 1, 'B' => 0];
  100. } else {
  101. $res_arr[$word]['A'] += 1;
  102. }
  103. }
  104. foreach ($str2_arr as $word2) {
  105. if (!isset($res_arr[$word2])) {
  106. $res_arr[$word2] = ['A' => 0, 'B' => 1];
  107. } else {
  108. $res_arr[$word2]['B'] += 1;
  109. }
  110. }
  111. $x = 0;
  112. $y1 = 0;
  113. $y2 = 0;
  114. foreach ($res_arr as $v) {
  115. $x += $v['A'] * $v['B'];
  116. $y1 += $v['A'] * $v['A'];
  117. $y2 += $v['B'] * $v['B'];
  118. }
  119. return ($y1 == 0 || $y2 == 0) ? -1 : ($x / (sqrt($y1) * sqrt($y2))) * 100;
  120. }
  121. public function C($n, $m)
  122. {
  123. return $this->A($n, $m) / $this->factorial($m);
  124. }
  125. // 计算排列个数
  126. public function A($n, $m)
  127. {
  128. return $this->factorial($n) / $this->factorial($n - $m);
  129. }
  130. // 计算组合个数
  131. public function factorial($n)
  132. {
  133. return array_product(range(1, $n));
  134. }
  135. }