Files
tyapi-server/internal/domains/user/entities/enterprise_info.go
2025-07-30 00:51:22 +08:00

352 lines
10 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package entities
import (
"fmt"
"strings"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// EnterpriseInfo 企业信息聚合根
// 存储用户在认证过程中验证后的企业信息,认证完成后不可修改
// 与用户是一对一关系,每个用户最多对应一个企业信息
type EnterpriseInfo struct {
// 基础标识
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"企业信息唯一标识"`
UserID string `gorm:"type:varchar(36);not null;uniqueIndex" json:"user_id" comment:"关联用户ID"`
// 企业四要素 - 企业认证的核心信息
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:"法定代表人身份证号"`
LegalPersonPhone string `gorm:"type:varchar(50);not null" json:"legal_person_phone" comment:"法定代表人手机号"`
EnterpriseAddress string `json:"enterprise_address" gorm:"type:varchar(200);not null" comment:"企业地址"`
// 时间戳字段
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:"软删除时间"`
// 关联关系
User *User `gorm:"foreignKey:UserID" json:"user,omitempty" comment:"关联的用户信息"`
// 领域事件 (不持久化)
domainEvents []interface{} `gorm:"-" json:"-"`
}
// TableName 指定数据库表名
func (EnterpriseInfo) TableName() string {
return "enterprise_infos"
}
// BeforeCreate GORM钩子创建前自动生成UUID
func (e *EnterpriseInfo) BeforeCreate(tx *gorm.DB) error {
if e.ID == "" {
e.ID = uuid.New().String()
}
return nil
}
// ================ 工厂方法 ================
// NewEnterpriseInfo 创建新的企业信息
func NewEnterpriseInfo(userID, companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone,enterpriseAddress string) (*EnterpriseInfo, error) {
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 更新企业信息
func (e *EnterpriseInfo) UpdateEnterpriseInfo(companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone, enterpriseAddress string) error {
// 验证输入参数
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
}
// ================ 查询方法 ================
// IsComplete 检查企业四要素是否完整
func (e *EnterpriseInfo) IsComplete() bool {
return e.CompanyName != "" &&
e.UnifiedSocialCode != "" &&
e.LegalPersonName != "" &&
e.LegalPersonID != "" &&
e.LegalPersonPhone != ""
}
// GetCertificationProgress 获取认证进度
func (e *EnterpriseInfo) GetCertificationProgress() int {
if e.IsComplete() {
return 100
}
return 50
}
// GetCertificationStatus 获取认证状态描述
func (e *EnterpriseInfo) GetCertificationStatus() string {
if e.IsComplete() {
return "信息完整"
}
return "信息不完整"
}
// CanUpdate 检查是否可以更新
func (e *EnterpriseInfo) CanUpdate() bool {
return true
}
// ================ 领域事件管理 ================
// addDomainEvent 添加领域事件
func (e *EnterpriseInfo) addDomainEvent(event interface{}) {
if e.domainEvents == nil {
e.domainEvents = make([]interface{}, 0)
}
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
}
// ================ 领域事件定义 ================
// 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"`
}