master
cody 3 months ago
parent 7aa440f193
commit e76ad42930

@ -2,10 +2,9 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Models\Config;
use App\Models\Course; use App\Models\Course;
use App\Models\CourseSign;
use App\Models\User; use App\Models\User;
use App\Repositories\MeetRepository;
use Illuminate\Console\Command; use Illuminate\Console\Command;
@ -116,37 +115,75 @@ class UpdateUserNo extends Command
} }
/** /**
* 给元和同事打标签 * 给元和同事打标签:检查所有有公司名称的用户,若公司名匹配元和员工则追加 from 标签
*/ */
protected function tagYuanheColleague() protected function tagYuanheColleague()
{ {
$this->info('开始给元和同事打标签...'); $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;
}
// 获取元和员工用户列表 // 所有有公司名称、且 from 中尚无「元禾同事」的用户(有该标签的不再追加)
$users = CourseSign::companyJoin(null, null, null, true, false); $users = User::whereNotNull('company_name')
->where('company_name', '!=', '')
->where(function ($q) use ($tag) {
$q->whereNull('from')
->orWhere('from', 'not like', '%' . $tag . '%');
})
->get();
$count = 0; $count = 0;
foreach ($users as $user) { foreach ($users as $user) {
// 获取当前的 from 字段 $companyName = (string) ($user->company_name ?? '');
$from = $user->from ?? '';
// 检查是否元和员工:公司名包含任一关键词
$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))); $fromArray = array_filter(array_map('trim', explode(',', $from)));
// 检查是否已存在该标签
if (in_array($tag, $fromArray)) { if (in_array($tag, $fromArray)) {
continue; continue;
} }
// 追加标签 // 追加标签并去重后写回
$fromArray[] = $tag; $fromArray[] = $tag;
$fromArray = array_values(array_unique($fromArray));
// 更新 from 字段
$user->from = implode(',', $fromArray); $user->from = implode(',', $fromArray);
$user->save(); $user->save();
$this->info('已为用户 ' . $user->name . '(' . $user->mobile . ') 添加标签: ' . $tag); $this->info('已为用户 ' . $user->name . '(' . $user->mobile . ') 添加标签: ' . $tag . ',公司: ' . $companyName);
$count++; $count++;
} }

@ -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) 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); $courseSignsQuery = self::getStudentList($start_date, $end_date, null, $course_ids);
$courseSignByType = $courseSignsQuery->get(); $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')) $list = User::whereIn('id', $courseSignByType->pluck('user_id'))
->where(function ($query) use ($companyNameKeyword) { ->where('from', 'like', '%' . User::FROM_TAG_YUANHE_COLLEAGUE . '%')
foreach ($companyNameKeyword as $item) { ->get();
$query->orWhere('company_name', 'like', '%' . $item . '%');
}
})->get();
if ($retList) { if ($retList) {
// 返回列表 // 返回列表

@ -18,6 +18,9 @@ class User extends Authenticatable implements Auditable
use \OwenIt\Auditing\Auditable; use \OwenIt\Auditing\Auditable;
use SoftDeletes; use SoftDeletes;
/** from 标签:元禾同事(元和员工参与人员) */
public const FROM_TAG_YUANHE_COLLEAGUE = '元禾同事';
protected $fillable = [ protected $fillable = [
'remember_token', 'remember_token',
'created_at', 'created_at',

Loading…
Cancel
Save