Files
tyapi-server/internal/domains/certification/value_objects/enterprise_info.go
2025-07-21 15:13:26 +08:00

181 lines
4.9 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 value_objects
import (
"fmt"
"regexp"
"strings"
)
// EnterpriseInfo 企业信息值对象
// 负责封装和验证企业认证相关的信息
type EnterpriseInfo struct {
CompanyName string `json:"company_name" validate:"required,min=2,max=100"`
UnifiedSocialCode string `json:"unified_social_code" validate:"required,len=18"`
LegalPersonName string `json:"legal_person_name" validate:"required,min=2,max=50"`
LegalPersonID string `json:"legal_person_id" validate:"required,len=18"`
LegalPersonPhone string `json:"legal_person_phone" validate:"required,mobile"`
}
// NewEnterpriseInfo 创建企业信息值对象
func NewEnterpriseInfo(
companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone string,
) (*EnterpriseInfo, error) {
// 清理输入数据
info := &EnterpriseInfo{
CompanyName: strings.TrimSpace(companyName),
UnifiedSocialCode: strings.TrimSpace(unifiedSocialCode),
LegalPersonName: strings.TrimSpace(legalPersonName),
LegalPersonID: strings.TrimSpace(legalPersonID),
LegalPersonPhone: strings.TrimSpace(legalPersonPhone),
}
// 验证数据
if err := info.Validate(); err != nil {
return nil, err
}
return info, nil
}
// Validate 验证企业信息
func (e *EnterpriseInfo) Validate() error {
// 验证公司名称
if e.CompanyName == "" {
return fmt.Errorf("公司名称不能为空")
}
if len(e.CompanyName) < 2 || len(e.CompanyName) > 100 {
return fmt.Errorf("公司名称长度应在2-100个字符之间")
}
// 验证统一社会信用代码
if err := e.validateUnifiedSocialCode(); err != nil {
return err
}
// 验证法人姓名
if e.LegalPersonName == "" {
return fmt.Errorf("法人姓名不能为空")
}
if len(e.LegalPersonName) < 2 || len(e.LegalPersonName) > 50 {
return fmt.Errorf("法人姓名长度应在2-50个字符之间")
}
// 验证法人身份证
if err := e.validateLegalPersonID(); err != nil {
return err
}
// 验证法人手机号
if err := e.validateLegalPersonPhone(); err != nil {
return err
}
return nil
}
// validateUnifiedSocialCode 验证统一社会信用代码
func (e *EnterpriseInfo) validateUnifiedSocialCode() error {
if e.UnifiedSocialCode == "" {
return fmt.Errorf("统一社会信用代码不能为空")
}
if len(e.UnifiedSocialCode) != 18 {
return fmt.Errorf("统一社会信用代码应为18位")
}
// 检查格式:应为数字和大写字母组合
matched, _ := regexp.MatchString(`^[0-9A-Z]{18}$`, e.UnifiedSocialCode)
if !matched {
return fmt.Errorf("统一社会信用代码格式不正确")
}
// TODO: 可以添加更严格的校验算法
return nil
}
// validateLegalPersonID 验证法人身份证号
func (e *EnterpriseInfo) validateLegalPersonID() error {
if e.LegalPersonID == "" {
return fmt.Errorf("法人身份证号不能为空")
}
if len(e.LegalPersonID) != 18 {
return fmt.Errorf("法人身份证号应为18位")
}
// 检查格式前17位为数字最后一位为数字或X
matched, _ := regexp.MatchString(`^\d{17}[\dX]$`, e.LegalPersonID)
if !matched {
return fmt.Errorf("法人身份证号格式不正确")
}
// TODO: 可以添加身份证校验算法
return nil
}
// validateLegalPersonPhone 验证法人手机号
func (e *EnterpriseInfo) validateLegalPersonPhone() error {
if e.LegalPersonPhone == "" {
return fmt.Errorf("法人手机号不能为空")
}
// 检查中国大陆手机号格式
matched, _ := regexp.MatchString(`^1[3-9]\d{9}$`, e.LegalPersonPhone)
if !matched {
return fmt.Errorf("法人手机号格式不正确")
}
return nil
}
// ================ 业务方法 ================
// GetDisplayName 获取显示名称
func (e *EnterpriseInfo) GetDisplayName() string {
return fmt.Sprintf("%s%s", e.CompanyName, e.LegalPersonName)
}
// IsSame 判断是否为同一企业信息
func (e *EnterpriseInfo) IsSame(other *EnterpriseInfo) bool {
if other == nil {
return false
}
return e.UnifiedSocialCode == other.UnifiedSocialCode &&
e.LegalPersonID == other.LegalPersonID
}
// GetMaskedPhone 获取脱敏手机号
func (e *EnterpriseInfo) GetMaskedPhone() string {
if len(e.LegalPersonPhone) != 11 {
return e.LegalPersonPhone
}
return e.LegalPersonPhone[:3] + "****" + e.LegalPersonPhone[7:]
}
// GetMaskedIDNumber 获取脱敏身份证号
func (e *EnterpriseInfo) GetMaskedIDNumber() string {
if len(e.LegalPersonID) != 18 {
return e.LegalPersonID
}
return e.LegalPersonID[:6] + "******" + e.LegalPersonID[14:]
}
// ToMap 转换为Map格式
func (e *EnterpriseInfo) ToMap() map[string]string {
return map[string]string{
"company_name": e.CompanyName,
"unified_social_code": e.UnifiedSocialCode,
"legal_person_name": e.LegalPersonName,
"legal_person_id": e.LegalPersonID,
"legal_person_phone": e.LegalPersonPhone,
}
}
// String 字符串表示
func (e *EnterpriseInfo) String() string {
return fmt.Sprintf("企业信息[公司=%s, 法人=%s, 信用代码=%s]",
e.CompanyName, e.LegalPersonName, e.UnifiedSocialCode)
}