feat: add multi-company Feishu integration and data migration

- Add companies table enhancements (abbr, tenant_key, feishu_enabled)
- Add users table enhancements (feishu fields, sn, report_to, etc.)
- Create company_feishu_apps table for multi-app per company
- Create user_sn_pool table for user number management
- Update FeishuService to support multiple Feishu apps per company
- Add data migration script (002_merge_old_data.sql)
- Add import script (scripts/import_old_data.php)
- Update User and Company models with new scopes and relationships
- Add CompanyFeishuApp model
- Add Git workflow documentation (GIT_WORKFLOW.md)

This supports:
- Multi-company architecture with Feishu integration
- Multiple Feishu apps per company (X-Server, Ticket, CRM)
- User number assignment system
- Data migration from old project

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-02-24 18:12:18 +08:00
parent 8d78e18267
commit 1f6d346f6a
8 changed files with 875 additions and 35 deletions
+54 -13
View File
@@ -6,25 +6,32 @@ use support\Model;
class Company extends Model
{
protected $table = 'company';
protected $table = 'companies';
protected $fillable = [
'name',
'feishu_app_id',
'feishu_app_secret',
'feishu_tenant_key',
'abbr',
'tenant_key',
'sort',
'status',
'feishu_enabled',
];
protected $casts = [
'sort' => 'integer',
'status' => 'integer',
'feishu_enabled' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
protected $hidden = [
'feishu_app_secret',
];
/**
* 关联用户
*/
public function users()
{
return $this->hasMany(User::class, 'company_id');
}
/**
* 获取启用的公司
@@ -35,18 +42,52 @@ class Company extends Model
}
/**
* 根据飞书租户Key查找
* 根据租户Key查找
*/
public function scopeByFeishuTenantKey($query, string $tenantKey)
public function scopeByTenantKey($query, string $tenantKey)
{
return $query->where('feishu_tenant_key', $tenantKey);
return $query->where('tenant_key', $tenantKey);
}
/**
* 根据飞书AppID查找
* 根据缩写查找
*/
public function scopeByFeishuAppId($query, string $appId)
public function scopeByAbbr($query, string $abbr)
{
return $query->where('feishu_app_id', $appId);
return $query->where('abbr', $abbr);
}
/**
* 搜索公司
*/
public function scopeSearch($query, ?string $keyword)
{
if (!$keyword) {
return $query;
}
return $query->where(function ($q) use ($keyword) {
$q->where('name', 'like', "%{$keyword}%")
->orWhere('abbr', 'like', "%{$keyword}%");
});
}
/**
* 获取飞书应用配置
*/
public function feishuApps()
{
return $this->hasMany(CompanyFeishuApp::class, 'company_id');
}
/**
* 获取默认飞书应用(X-Server)
*/
public function getDefaultFeishuApp()
{
return $this->hasOne(CompanyFeishuApp::class, 'company_id')
->where('system_id', 1)
->where('status', 1)
->orderBy('sort');
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
namespace App\Model;
use support\Model;
class CompanyFeishuApp extends Model
{
protected $table = 'company_feishu_apps';
protected $fillable = [
'company_id',
'system_id',
'name',
'tenant_key',
'app_id',
'app_secret',
'encrypt_key',
'verification_token',
'base_uri',
'callback_url',
'mobile_callback_url',
'scopes',
'state',
'sort',
'status',
];
protected $casts = [
'company_id' => 'integer',
'system_id' => 'integer',
'sort' => 'integer',
'status' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
protected $hidden = [
'app_secret',
'encrypt_key',
'verification_token',
];
/**
* 关联公司
*/
public function company()
{
return $this->belongsTo(Company::class, 'company_id');
}
/**
* 获取启用的应用
*/
public function scopeActive($query)
{
return $query->where('status', 1);
}
/**
* 根据系统ID查找
*/
public function scopeBySystemId($query, int $systemId)
{
return $query->where('system_id', $systemId);
}
/**
* 获取X-Server应用
*/
public function scopeXServer($query)
{
return $query->where('system_id', 1);
}
/**
* 获取工单应用
*/
public function scopeTicket($query)
{
return $query->where('system_id', 2);
}
/**
* 获取CRM应用
*/
public function scopeCrm($query)
{
return $query->where('system_id', 3);
}
}
+81 -7
View File
@@ -6,26 +6,42 @@ use support\Model;
class User extends Model
{
protected $table = 'user';
protected $table = 'users';
protected $fillable = [
'company_id',
'feishu_user_id',
'feishu_union_id',
'sn',
'report_to',
'name',
'avatar',
'sex',
'email',
'phone',
'avatar',
'password',
'status',
'user_type',
'union_id',
'feishu_user_id',
'feishu_open_id',
'tenant_key',
'position',
'sign_img',
];
protected $casts = [
'company_id' => 'integer',
'report_to' => 'integer',
'sex' => 'integer',
'status' => 'integer',
'user_type' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
protected $hidden = [
'password',
];
/**
* 关联公司
*/
@@ -34,6 +50,22 @@ class User extends Model
return $this->belongsTo(Company::class, 'company_id');
}
/**
* 上级用户
*/
public function parent()
{
return $this->belongsTo(User::class, 'report_to');
}
/**
* 下级用户
*/
public function children()
{
return $this->hasMany(User::class, 'report_to');
}
/**
* 获取启用的用户
*/
@@ -42,19 +74,61 @@ class User extends Model
return $query->where('status', 1);
}
/**
* 根据公司ID查找
*/
public function scopeByCompany($query, int $companyId)
{
return $query->where('company_id', $companyId);
}
/**
* 根据飞书用户ID查找
*/
public function scopeByFeishuUserId($query, string $feishuUserId)
public function scopeByFeishuUserId($query, ?string $feishuUserId)
{
if ($feishuUserId === null) {
return $query;
}
return $query->where('feishu_user_id', $feishuUserId);
}
/**
* 根据飞书 UnionID 查找
*/
public function scopeByFeishuUnionId($query, string $feishuUnionId)
public function scopeByFeishuUnionId($query, ?string $feishuUnionId)
{
return $query->where('feishu_union_id', $feishuUnionId);
if ($feishuUnionId === null) {
return $query;
}
return $query->where('union_id', $feishuUnionId);
}
/**
* 根据OpenID查找
*/
public function scopeByFeishuOpenId($query, ?string $openId)
{
if ($openId === null) {
return $query;
}
return $query->where('feishu_open_id', $openId);
}
/**
* 搜索用户
*/
public function scopeSearch($query, ?string $keyword)
{
if (!$keyword) {
return $query;
}
return $query->where(function ($q) use ($keyword) {
$q->where('name', 'like', "%{$keyword}%")
->orWhere('email', 'like', "%{$keyword}%")
->orWhere('phone', 'like', "%{$keyword}%")
->orWhere('sn', 'like', "%{$keyword}%");
});
}
}