123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- /*
- * This file is part of the ZhMead/laravel-logger.
- *
- * (c) Mead <751066209@qql.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Jobs\Manage;
- use App\Repositories\Models\Base\Setting;
- use App\Repositories\Models\Manage\Message;
- use Carbon\Carbon;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class AutoAnalysisMessageJob implements ShouldQueue
- {
- use InteractsWithQueue;
- use Queueable;
- public $tries = 1;
- public $timeout = 1440;
- private $messageId;
- /**
- * Create a new job instance.
- */
- public function __construct($messageId)
- {
- $this->messageId = $messageId;
- }
- /**
- * 确定任务应该超时的时间
- *
- * @return \DateTime
- */
- public function retryUntil()
- {
- return Carbon::now()->addHours(24);
- }
- /**
- * Execute the job.
- */
- public function handle()
- {
- $this->analysis();
- }
- /**
- * 导入数据
- * @return false|void
- */
- public function analysis()
- {
- $model = Message::query()->where('id', $this->messageId)->first();
- DB::beginTransaction();
- try {
- Log::error('*************分析数据中*****************');
- $messages = Message::query()
- ->whereDate('created_at', '>=', Carbon::now()->addDays(-Setting::byCodeGetSetting('auto_analysis_days'))->toDateString())
- ->where("more_pid", 0)
- ->where("id", '<>', $model->id)
- ->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'])
- ->get();
- $rateData = null;
- foreach ($messages as $m) {
- $rate = $this->get_similar_percent($model->keywords, $m->keywords);
- Log::error("{$m->id}==>{$rate}");
- if ($rate >= (int)Setting::byCodeGetSetting('auto_analysis_similarity')) {
- $rateData[] = [
- 'mid' => $m->id,
- 'rate' => $rate,
- ];
- }
- }
- $model->similar_data = $rateData;
- $model->save();
- DB::commit();
- } catch (\Exception $exception) {
- DB::rollBack();
- Log::error("
- **********信息录入对比*****************
- exception: {$exception->getMessage()}
- ");
- }
- Log::error("-----------完成---------------");
- }
- /**
- * 相似度
- * @param $str1_arr
- * @param $str2_arr
- * @return float|int
- */
- function get_similar_percent($str1_arr, $str2_arr)
- {
- $res_arr = [];
- foreach ($str1_arr as $word) {
- if (!isset($res_arr[$word])) {
- $res_arr[$word] = ['A' => 1, 'B' => 0];
- } else {
- $res_arr[$word]['A'] += 1;
- }
- }
- foreach ($str2_arr as $word2) {
- if (!isset($res_arr[$word2])) {
- $res_arr[$word2] = ['A' => 0, 'B' => 1];
- } else {
- $res_arr[$word2]['B'] += 1;
- }
- }
- $x = 0;
- $y1 = 0;
- $y2 = 0;
- foreach ($res_arr as $v) {
- $x += $v['A'] * $v['B'];
- $y1 += $v['A'] * $v['A'];
- $y2 += $v['B'] * $v['B'];
- }
- return ($y1 == 0 || $y2 == 0) ? -1 : ($x / (sqrt($y1) * sqrt($y2))) * 100;
- }
- public function C($n, $m)
- {
- return $this->A($n, $m) / $this->factorial($m);
- }
- // 计算排列个数
- public function A($n, $m)
- {
- return $this->factorial($n) / $this->factorial($n - $m);
- }
- // 计算组合个数
- public function factorial($n)
- {
- return array_product(range(1, $n));
- }
- }
|