108 lines
4.2 KiB
Go
108 lines
4.2 KiB
Go
package entities
|
||
|
||
import (
|
||
"time"
|
||
|
||
"github.com/google/uuid"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// ContractRecord 合同记录实体
|
||
// 记录电子合同的详细信息,包括合同生成、审核、签署的完整流程
|
||
// 支持合同状态跟踪、签署信息记录、审核流程管理等功能
|
||
type ContractRecord struct {
|
||
// 基础标识
|
||
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"合同记录唯一标识"`
|
||
CertificationID string `gorm:"type:varchar(36);not null;index" json:"certification_id" comment:"关联的认证申请ID"`
|
||
UserID string `gorm:"type:varchar(36);not null;index" json:"user_id" comment:"合同申请人ID"`
|
||
AdminID *string `gorm:"type:varchar(36);index" json:"admin_id,omitempty" comment:"审核管理员ID"`
|
||
|
||
// 合同信息 - 电子合同的基本信息
|
||
ContractType string `gorm:"type:varchar(50);not null" json:"contract_type" comment:"合同类型(ENTERPRISE_CERTIFICATION)"`
|
||
ContractURL string `gorm:"type:varchar(500)" json:"contract_url,omitempty" comment:"合同文件访问链接"`
|
||
SigningURL string `gorm:"type:varchar(500)" json:"signing_url,omitempty" comment:"电子签署链接"`
|
||
|
||
// 签署信息 - 记录用户签署的详细信息
|
||
SignatureData string `gorm:"type:text" json:"signature_data,omitempty" comment:"签署数据(JSON格式)"`
|
||
SignedAt *time.Time `json:"signed_at,omitempty" comment:"签署完成时间"`
|
||
ClientIP string `gorm:"type:varchar(50)" json:"client_ip,omitempty" comment:"签署客户端IP"`
|
||
UserAgent string `gorm:"type:varchar(500)" json:"user_agent,omitempty" comment:"签署客户端信息"`
|
||
|
||
// 状态信息 - 合同的生命周期状态
|
||
Status string `gorm:"type:varchar(50);not null;index" json:"status" comment:"合同状态(PENDING/APPROVED/SIGNED/EXPIRED)"`
|
||
ApprovalNotes string `gorm:"type:text" json:"approval_notes,omitempty" comment:"审核备注信息"`
|
||
RejectReason string `gorm:"type:text" json:"reject_reason,omitempty" comment:"拒绝原因说明"`
|
||
ExpiresAt *time.Time `json:"expires_at,omitempty" 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:"软删除时间"`
|
||
|
||
// 关联关系
|
||
Certification *Certification `gorm:"foreignKey:CertificationID" json:"certification,omitempty" comment:"关联的认证申请"`
|
||
}
|
||
|
||
// TableName 指定数据库表名
|
||
func (ContractRecord) TableName() string {
|
||
return "contract_records"
|
||
}
|
||
|
||
// IsPending 检查合同是否待审核
|
||
// 判断合同是否处于等待管理员审核的状态
|
||
func (c *ContractRecord) IsPending() bool {
|
||
return c.Status == "PENDING"
|
||
}
|
||
|
||
// IsApproved 检查合同是否已审核通过
|
||
// 判断合同是否已通过管理员审核,可以进入签署阶段
|
||
func (c *ContractRecord) IsApproved() bool {
|
||
return c.Status == "APPROVED"
|
||
}
|
||
|
||
// IsSigned 检查合同是否已签署
|
||
// 判断合同是否已完成电子签署,认证流程即将完成
|
||
func (c *ContractRecord) IsSigned() bool {
|
||
return c.Status == "SIGNED"
|
||
}
|
||
|
||
// IsExpired 检查合同是否已过期
|
||
// 判断合同是否已超过有效期,过期后需要重新申请
|
||
func (c *ContractRecord) IsExpired() bool {
|
||
if c.ExpiresAt == nil {
|
||
return false
|
||
}
|
||
return time.Now().After(*c.ExpiresAt)
|
||
}
|
||
|
||
// HasSigningURL 检查是否有签署链接
|
||
// 判断是否已生成电子签署链接,用于前端判断是否显示签署按钮
|
||
func (c *ContractRecord) HasSigningURL() bool {
|
||
return c.SigningURL != ""
|
||
}
|
||
|
||
// GetStatusName 获取状态的中文名称
|
||
// 将英文状态码转换为中文显示名称,用于前端展示和用户理解
|
||
func (c *ContractRecord) GetStatusName() string {
|
||
statusNames := map[string]string{
|
||
"PENDING": "待审核",
|
||
"APPROVED": "已审核",
|
||
"SIGNED": "已签署",
|
||
"EXPIRED": "已过期",
|
||
"REJECTED": "已拒绝",
|
||
}
|
||
|
||
if name, exists := statusNames[c.Status]; exists {
|
||
return name
|
||
}
|
||
return c.Status
|
||
}
|
||
|
||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||
func (c *ContractRecord) BeforeCreate(tx *gorm.DB) error {
|
||
if c.ID == "" {
|
||
c.ID = uuid.New().String()
|
||
}
|
||
return nil
|
||
}
|