diff --git a/app/Console/Commands/UpdateUserNo.php b/app/Console/Commands/UpdateUserNo.php index 10f24ad..371ce75 100755 --- a/app/Console/Commands/UpdateUserNo.php +++ b/app/Console/Commands/UpdateUserNo.php @@ -2,10 +2,9 @@ namespace App\Console\Commands; +use App\Models\Config; use App\Models\Course; -use App\Models\CourseSign; use App\Models\User; -use App\Repositories\MeetRepository; use Illuminate\Console\Command; @@ -116,37 +115,75 @@ class UpdateUserNo extends Command } /** - * 给元和同事打标签 + * 给元和同事打标签:检查所有有公司名称的用户,若公司名匹配元和员工则追加 from 标签 */ protected function tagYuanheColleague() { $this->info('开始给元和同事打标签...'); - $tag = '元禾同事'; + $tag = User::FROM_TAG_YUANHE_COLLEAGUE; + + // 从 config 获取元和公司名称关键词(用于判断是否元和员工) + $companyNameKeyword = []; + $configValue = Config::getValueByKey('yuanhe_company'); + if ($configValue) { + $configData = json_decode($configValue, true); + if (is_array($configData)) { + foreach ($configData as $item) { + if (!empty($item['company_name'])) { + $companyNameKeyword[] = $item['company_name']; + } + if (!empty($item['short_company_name'])) { + $companyNameKeyword[] = $item['short_company_name']; + } + } + $companyNameKeyword = array_values(array_unique($companyNameKeyword)); + } + } + if (empty($companyNameKeyword)) { + $this->warn('config(yuanhe_company) 无数据,跳过元和同事打标签'); + return; + } - // 获取元和员工用户列表 - $users = CourseSign::companyJoin(null, null, null, true, false); + // 所有有公司名称、且 from 中尚无「元禾同事」的用户(有该标签的不再追加) + $users = User::whereNotNull('company_name') + ->where('company_name', '!=', '') + ->where(function ($q) use ($tag) { + $q->whereNull('from') + ->orWhere('from', 'not like', '%' . $tag . '%'); + }) + ->get(); $count = 0; foreach ($users as $user) { - // 获取当前的 from 字段 - $from = $user->from ?? ''; + $companyName = (string) ($user->company_name ?? ''); + + // 检查是否元和员工:公司名包含任一关键词 + $isYuanhe = false; + foreach ($companyNameKeyword as $keyword) { + $keyword = (string) $keyword; + if ($keyword !== '' && mb_strpos($companyName, $keyword) !== false) { + $isYuanhe = true; + break; + } + } + if (!$isYuanhe) { + continue; + } - // 将 from 字段按逗号分隔成数组 + // 再次确认 from 未有该标签,避免重复 + $from = $user->from ?? ''; $fromArray = array_filter(array_map('trim', explode(',', $from))); - - // 检查是否已存在该标签 if (in_array($tag, $fromArray)) { continue; } - // 追加标签 + // 追加标签并去重后写回 $fromArray[] = $tag; - - // 更新 from 字段 + $fromArray = array_values(array_unique($fromArray)); $user->from = implode(',', $fromArray); $user->save(); - $this->info('已为用户 ' . $user->name . '(' . $user->mobile . ') 添加标签: ' . $tag); + $this->info('已为用户 ' . $user->name . '(' . $user->mobile . ') 添加标签: ' . $tag . ',公司: ' . $companyName); $count++; } diff --git a/app/Models/CourseSign.php b/app/Models/CourseSign.php index 7bda817..c22c043 100755 --- a/app/Models/CourseSign.php +++ b/app/Models/CourseSign.php @@ -694,48 +694,16 @@ class CourseSign extends SoftDeletesModel } /** - * 元和员工参人员 + * 元和员工参与人员(user.from 包含「元和同事」的用户) */ public static function companyJoin($start_date = null, $end_date = null, $course_ids = null, $retList = false, $needHistory = true) { $courseSignsQuery = self::getStudentList($start_date, $end_date, null, $course_ids); $courseSignByType = $courseSignsQuery->get(); - // 从 config 表中获取公司名称关键词 - $companyNameKeyword = []; - $configValue = Config::getValueByKey('yuanhe_company'); - if ($configValue) { - $configData = json_decode($configValue, true); - if (is_array($configData)) { - foreach ($configData as $item) { - // 提取 company_name 和 short_company_name,过滤空值 - if (!empty($item['company_name'])) { - $companyNameKeyword[] = $item['company_name']; - } - if (!empty($item['short_company_name'])) { - $companyNameKeyword[] = $item['short_company_name']; - } - } - // 去重关键词并重新索引 - $companyNameKeyword = array_values(array_unique($companyNameKeyword)); - } - } - - // 如果 config 中没有数据,直接返回空 - if (empty($companyNameKeyword)) { - if ($retList) { - return collect([]); - } else { - return 0; - } - } - $list = User::whereIn('id', $courseSignByType->pluck('user_id')) - ->where(function ($query) use ($companyNameKeyword) { - foreach ($companyNameKeyword as $item) { - $query->orWhere('company_name', 'like', '%' . $item . '%'); - } - })->get(); + ->where('from', 'like', '%' . User::FROM_TAG_YUANHE_COLLEAGUE . '%') + ->get(); if ($retList) { // 返回列表 diff --git a/app/Models/User.php b/app/Models/User.php index 35c01e8..35d18ba 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -18,6 +18,9 @@ class User extends Authenticatable implements Auditable use \OwenIt\Auditing\Auditable; use SoftDeletes; + /** from 标签:元禾同事(元和员工参与人员) */ + public const FROM_TAG_YUANHE_COLLEAGUE = '元禾同事'; + protected $fillable = [ 'remember_token', 'created_at',