v0.1
This commit is contained in:
@@ -2,13 +2,14 @@ package entities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// EnterpriseInfo 企业信息实体
|
||||
// EnterpriseInfo 企业信息聚合根
|
||||
// 存储用户在认证过程中验证后的企业信息,认证完成后不可修改
|
||||
// 与用户是一对一关系,每个用户最多对应一个企业信息
|
||||
type EnterpriseInfo struct {
|
||||
@@ -22,6 +23,8 @@ type EnterpriseInfo struct {
|
||||
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:"企业地址"`
|
||||
EnterpriseEmail string `json:"enterprise_email" gorm:"type:varchar(100);not null" comment:"企业邮箱"`
|
||||
|
||||
// 时间戳字段
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
|
||||
@@ -30,6 +33,9 @@ type EnterpriseInfo struct {
|
||||
|
||||
// 关联关系
|
||||
User *User `gorm:"foreignKey:UserID" json:"user,omitempty" comment:"关联的用户信息"`
|
||||
|
||||
// 领域事件 (不持久化)
|
||||
domainEvents []interface{} `gorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
@@ -37,28 +43,6 @@ func (EnterpriseInfo) TableName() string {
|
||||
return "enterprise_infos"
|
||||
}
|
||||
|
||||
// IsComplete 检查企业四要素是否完整
|
||||
// 验证企业名称、统一社会信用代码、法定代表人姓名、身份证号是否都已填写
|
||||
func (e *EnterpriseInfo) IsComplete() bool {
|
||||
return e.CompanyName != "" &&
|
||||
e.UnifiedSocialCode != "" &&
|
||||
e.LegalPersonName != "" &&
|
||||
e.LegalPersonID != ""
|
||||
}
|
||||
|
||||
// Validate 验证企业信息是否有效
|
||||
// 这里可以添加企业信息的业务验证逻辑
|
||||
// 比如统一社会信用代码格式验证、身份证号格式验证等
|
||||
func (e *EnterpriseInfo) Validate() error {
|
||||
if !e.IsComplete() {
|
||||
return fmt.Errorf("企业信息不完整")
|
||||
}
|
||||
// 这里可以添加企业信息的业务验证逻辑
|
||||
// 比如统一社会信用代码格式验证、身份证号格式验证等
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||||
func (e *EnterpriseInfo) BeforeCreate(tx *gorm.DB) error {
|
||||
if e.ID == "" {
|
||||
@@ -66,3 +50,327 @@ func (e *EnterpriseInfo) BeforeCreate(tx *gorm.DB) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ================ 工厂方法 ================
|
||||
|
||||
// NewEnterpriseInfo 创建新的企业信息
|
||||
func NewEnterpriseInfo(userID, companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone,enterpriseAddress, enterpriseEmail 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("企业地址不能为空")
|
||||
}
|
||||
if enterpriseEmail == "" {
|
||||
return nil, fmt.Errorf("企业邮箱不能为空")
|
||||
}
|
||||
|
||||
enterpriseInfo := &EnterpriseInfo{
|
||||
ID: uuid.New().String(),
|
||||
UserID: userID,
|
||||
CompanyName: companyName,
|
||||
UnifiedSocialCode: unifiedSocialCode,
|
||||
LegalPersonName: legalPersonName,
|
||||
LegalPersonID: legalPersonID,
|
||||
LegalPersonPhone: legalPersonPhone,
|
||||
EnterpriseAddress: enterpriseAddress,
|
||||
EnterpriseEmail: enterpriseEmail,
|
||||
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, enterpriseEmail 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("企业地址不能为空")
|
||||
}
|
||||
if enterpriseEmail == "" {
|
||||
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.EnterpriseEmail = enterpriseEmail
|
||||
|
||||
// 添加领域事件
|
||||
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.EnterpriseEmail == "" {
|
||||
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("法定代表人手机号格式无效")
|
||||
}
|
||||
|
||||
// 邮箱格式验证 (简单示例,实际应更严格)
|
||||
if !e.isValidEmail(e.EnterpriseEmail) {
|
||||
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个字符")
|
||||
}
|
||||
|
||||
// 企业邮箱格式验证 (简单示例,实际应更严格)
|
||||
if !e.isValidEmail(e.EnterpriseEmail) {
|
||||
return fmt.Errorf("企业邮箱格式无效")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ================ 查询方法 ================
|
||||
|
||||
// IsComplete 检查企业四要素是否完整
|
||||
func (e *EnterpriseInfo) IsComplete() bool {
|
||||
return e.CompanyName != "" &&
|
||||
e.UnifiedSocialCode != "" &&
|
||||
e.LegalPersonName != "" &&
|
||||
e.LegalPersonID != "" &&
|
||||
e.LegalPersonPhone != "" &&
|
||||
e.EnterpriseEmail != ""
|
||||
}
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user