Files
tyapi-server/internal/domains/user/entities/enterprise_info.go

352 lines
10 KiB
Go
Raw Normal View History

2025-07-11 21:05:58 +08:00
package entities
import (
2025-07-13 16:36:20 +08:00
"fmt"
2025-07-28 01:46:39 +08:00
"strings"
2025-07-11 21:05:58 +08:00
"time"
2025-07-13 16:36:20 +08:00
"github.com/google/uuid"
2025-07-11 21:05:58 +08:00
"gorm.io/gorm"
)
2025-07-28 01:46:39 +08:00
// EnterpriseInfo 企业信息聚合根
2025-07-13 16:36:20 +08:00
// 存储用户在认证过程中验证后的企业信息,认证完成后不可修改
// 与用户是一对一关系,每个用户最多对应一个企业信息
type EnterpriseInfo struct {
2025-07-11 21:05:58 +08:00
// 基础标识
2025-07-13 16:36:20 +08:00
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"企业信息唯一标识"`
UserID string `gorm:"type:varchar(36);not null;uniqueIndex" json:"user_id" comment:"关联用户ID"`
2025-07-11 21:05:58 +08:00
// 企业四要素 - 企业认证的核心信息
CompanyName string `gorm:"type:varchar(255);not null" json:"company_name" comment:"企业名称"`
UnifiedSocialCode string `gorm:"type:varchar(50);not null;index" json:"unified_social_code" comment:"统一社会信用代码"`
LegalPersonName string `gorm:"type:varchar(100);not null" json:"legal_person_name" comment:"法定代表人姓名"`
LegalPersonID string `gorm:"type:varchar(50);not null" json:"legal_person_id" comment:"法定代表人身份证号"`
2025-07-20 20:53:26 +08:00
LegalPersonPhone string `gorm:"type:varchar(50);not null" json:"legal_person_phone" comment:"法定代表人手机号"`
2025-07-28 01:46:39 +08:00
EnterpriseAddress string `json:"enterprise_address" gorm:"type:varchar(200);not null" comment:"企业地址"`
2025-07-11 21:05:58 +08:00
// 时间戳字段
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"`
// 关联关系
2025-07-13 16:36:20 +08:00
User *User `gorm:"foreignKey:UserID" json:"user,omitempty" comment:"关联的用户信息"`
2025-07-28 01:46:39 +08:00
// 领域事件 (不持久化)
domainEvents []interface{} `gorm:"-" json:"-"`
2025-07-11 21:05:58 +08:00
}
// TableName 指定数据库表名
2025-07-13 16:36:20 +08:00
func (EnterpriseInfo) TableName() string {
return "enterprise_infos"
2025-07-11 21:05:58 +08:00
}
2025-07-28 01:46:39 +08:00
// BeforeCreate GORM钩子创建前自动生成UUID
func (e *EnterpriseInfo) BeforeCreate(tx *gorm.DB) error {
if e.ID == "" {
e.ID = uuid.New().String()
}
return nil
}
// ================ 工厂方法 ================
// NewEnterpriseInfo 创建新的企业信息
2025-07-30 00:51:22 +08:00
func NewEnterpriseInfo(userID, companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone,enterpriseAddress string) (*EnterpriseInfo, error) {
2025-07-28 01:46:39 +08:00
if userID == "" {
return nil, fmt.Errorf("用户ID不能为空")
}
if companyName == "" {
return nil, fmt.Errorf("企业名称不能为空")
}
if unifiedSocialCode == "" {
return nil, fmt.Errorf("统一社会信用代码不能为空")
}
if legalPersonName == "" {
return nil, fmt.Errorf("法定代表人姓名不能为空")
}
if legalPersonID == "" {
return nil, fmt.Errorf("法定代表人身份证号不能为空")
}
if legalPersonPhone == "" {
return nil, fmt.Errorf("法定代表人手机号不能为空")
}
if enterpriseAddress == "" {
return nil, fmt.Errorf("企业地址不能为空")
}
enterpriseInfo := &EnterpriseInfo{
ID: uuid.New().String(),
UserID: userID,
CompanyName: companyName,
UnifiedSocialCode: unifiedSocialCode,
LegalPersonName: legalPersonName,
LegalPersonID: legalPersonID,
LegalPersonPhone: legalPersonPhone,
EnterpriseAddress: enterpriseAddress,
domainEvents: make([]interface{}, 0),
}
// 添加领域事件
enterpriseInfo.addDomainEvent(&EnterpriseInfoCreatedEvent{
EnterpriseInfoID: enterpriseInfo.ID,
UserID: userID,
CompanyName: companyName,
UnifiedSocialCode: unifiedSocialCode,
CreatedAt: time.Now(),
})
return enterpriseInfo, nil
}
// ================ 聚合根核心方法 ================
// UpdateEnterpriseInfo 更新企业信息
2025-07-30 00:51:22 +08:00
func (e *EnterpriseInfo) UpdateEnterpriseInfo(companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone, enterpriseAddress string) error {
2025-07-28 01:46:39 +08:00
// 验证输入参数
if companyName == "" {
return fmt.Errorf("企业名称不能为空")
}
if unifiedSocialCode == "" {
return fmt.Errorf("统一社会信用代码不能为空")
}
if legalPersonName == "" {
return fmt.Errorf("法定代表人姓名不能为空")
}
if legalPersonID == "" {
return fmt.Errorf("法定代表人身份证号不能为空")
}
if legalPersonPhone == "" {
return fmt.Errorf("法定代表人手机号不能为空")
}
if enterpriseAddress == "" {
return fmt.Errorf("企业地址不能为空")
}
// 记录原始值用于事件
oldCompanyName := e.CompanyName
oldUnifiedSocialCode := e.UnifiedSocialCode
// 更新字段
e.CompanyName = companyName
e.UnifiedSocialCode = unifiedSocialCode
e.LegalPersonName = legalPersonName
e.LegalPersonID = legalPersonID
e.LegalPersonPhone = legalPersonPhone
e.EnterpriseAddress = enterpriseAddress
// 添加领域事件
e.addDomainEvent(&EnterpriseInfoUpdatedEvent{
EnterpriseInfoID: e.ID,
UserID: e.UserID,
OldCompanyName: oldCompanyName,
NewCompanyName: companyName,
OldUnifiedSocialCode: oldUnifiedSocialCode,
NewUnifiedSocialCode: unifiedSocialCode,
UpdatedAt: time.Now(),
})
return nil
}
// ================ 业务规则验证 ================
// ValidateBusinessRules 验证业务规则
func (e *EnterpriseInfo) ValidateBusinessRules() error {
// 基础字段验证
if err := e.validateBasicFields(); err != nil {
return fmt.Errorf("基础字段验证失败: %w", err)
}
// 业务规则验证
if err := e.validateBusinessLogic(); err != nil {
return fmt.Errorf("业务规则验证失败: %w", err)
}
return nil
}
// validateBasicFields 验证基础字段
func (e *EnterpriseInfo) validateBasicFields() error {
if e.UserID == "" {
return fmt.Errorf("用户ID不能为空")
}
if e.CompanyName == "" {
return fmt.Errorf("企业名称不能为空")
}
if e.UnifiedSocialCode == "" {
return fmt.Errorf("统一社会信用代码不能为空")
}
if e.LegalPersonName == "" {
return fmt.Errorf("法定代表人姓名不能为空")
}
if e.LegalPersonID == "" {
return fmt.Errorf("法定代表人身份证号不能为空")
}
if e.LegalPersonPhone == "" {
return fmt.Errorf("法定代表人手机号不能为空")
}
// 统一社会信用代码格式验证
if !e.isValidUnifiedSocialCode(e.UnifiedSocialCode) {
return fmt.Errorf("统一社会信用代码格式无效")
}
// 身份证号格式验证
if !e.isValidIDCard(e.LegalPersonID) {
return fmt.Errorf("法定代表人身份证号格式无效")
}
// 手机号格式验证
if !e.isValidPhone(e.LegalPersonPhone) {
return fmt.Errorf("法定代表人手机号格式无效")
}
return nil
}
// validateBusinessLogic 验证业务逻辑
func (e *EnterpriseInfo) validateBusinessLogic() error {
// 企业名称长度限制
if len(e.CompanyName) > 255 {
return fmt.Errorf("企业名称长度不能超过255个字符")
}
// 法定代表人姓名长度限制
if len(e.LegalPersonName) > 100 {
return fmt.Errorf("法定代表人姓名长度不能超过100个字符")
}
return nil
}
// ================ 查询方法 ================
2025-07-11 21:05:58 +08:00
// IsComplete 检查企业四要素是否完整
2025-07-13 16:36:20 +08:00
func (e *EnterpriseInfo) IsComplete() bool {
2025-07-11 21:05:58 +08:00
return e.CompanyName != "" &&
e.UnifiedSocialCode != "" &&
e.LegalPersonName != "" &&
2025-07-28 01:46:39 +08:00
e.LegalPersonID != "" &&
2025-07-30 00:51:22 +08:00
e.LegalPersonPhone != ""
2025-07-11 21:05:58 +08:00
}
2025-07-28 01:46:39 +08:00
// GetCertificationProgress 获取认证进度
func (e *EnterpriseInfo) GetCertificationProgress() int {
if e.IsComplete() {
return 100
2025-07-13 16:36:20 +08:00
}
2025-07-28 01:46:39 +08:00
return 50
2025-07-11 21:05:58 +08:00
}
2025-07-13 16:36:20 +08:00
2025-07-28 01:46:39 +08:00
// GetCertificationStatus 获取认证状态描述
func (e *EnterpriseInfo) GetCertificationStatus() string {
if e.IsComplete() {
return "信息完整"
}
return "信息不完整"
}
2025-07-13 16:36:20 +08:00
2025-07-28 01:46:39 +08:00
// CanUpdate 检查是否可以更新
func (e *EnterpriseInfo) CanUpdate() bool {
return true
}
// ================ 领域事件管理 ================
// addDomainEvent 添加领域事件
func (e *EnterpriseInfo) addDomainEvent(event interface{}) {
if e.domainEvents == nil {
e.domainEvents = make([]interface{}, 0)
2025-07-13 16:36:20 +08:00
}
2025-07-28 01:46:39 +08:00
e.domainEvents = append(e.domainEvents, event)
}
// GetDomainEvents 获取领域事件
func (e *EnterpriseInfo) GetDomainEvents() []interface{} {
return e.domainEvents
}
// ClearDomainEvents 清除领域事件
func (e *EnterpriseInfo) ClearDomainEvents() {
e.domainEvents = make([]interface{}, 0)
}
// ================ 私有验证方法 ================
// isValidUnifiedSocialCode 验证统一社会信用代码格式
func (e *EnterpriseInfo) isValidUnifiedSocialCode(code string) bool {
// 统一社会信用代码为18位
if len(code) != 18 {
return false
}
// 这里可以添加更详细的格式验证逻辑
return true
}
// isValidIDCard 验证身份证号格式
func (e *EnterpriseInfo) isValidIDCard(id string) bool {
// 身份证号为18位
if len(id) != 18 {
return false
}
// 这里可以添加更详细的格式验证逻辑
return true
}
// isValidPhone 验证手机号格式
func (e *EnterpriseInfo) isValidPhone(phone string) bool {
// 手机号格式验证
if len(phone) != 11 {
return false
}
// 这里可以添加更详细的格式验证逻辑
return true
}
// isValidEmail 验证邮箱格式
func (e *EnterpriseInfo) isValidEmail(email string) bool {
// 简单的邮箱格式验证,实际应更严格
if len(email) > 255 || len(email) < 3 { // 长度限制
return false
}
if !strings.Contains(email, "@") {
return false
}
if !strings.Contains(email, ".") {
return false
}
return true
2025-07-13 16:36:20 +08:00
}
2025-07-28 01:46:39 +08:00
// ================ 领域事件定义 ================
// EnterpriseInfoCreatedEvent 企业信息创建事件
type EnterpriseInfoCreatedEvent struct {
EnterpriseInfoID string `json:"enterprise_info_id"`
UserID string `json:"user_id"`
CompanyName string `json:"company_name"`
UnifiedSocialCode string `json:"unified_social_code"`
CreatedAt time.Time `json:"created_at"`
}
// EnterpriseInfoUpdatedEvent 企业信息更新事件
type EnterpriseInfoUpdatedEvent struct {
EnterpriseInfoID string `json:"enterprise_info_id"`
UserID string `json:"user_id"`
OldCompanyName string `json:"old_company_name"`
NewCompanyName string `json:"new_company_name"`
OldUnifiedSocialCode string `json:"old_unified_social_code"`
NewUnifiedSocialCode string `json:"new_unified_social_code"`
UpdatedAt time.Time `json:"updated_at"`
}