first commit
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package mysqlmodel
|
||||
|
||||
// AdminUser 管理员账号(管理后台自建表,唯一不共享业务服务的表)。
|
||||
// 字符串字段显式指定 type:varchar 长度:MySQL 无法对 TEXT/BLOB 列建普通唯一索引,
|
||||
// 且 varchar 长度受 utf8mb4 索引 3072 字节上限约束。
|
||||
type AdminUser struct {
|
||||
Model
|
||||
Username string `gorm:"column:username;type:varchar(32);uniqueIndex:uk_admin_username"` // 登录账号
|
||||
PasswordHash string `gorm:"column:password_hash;type:varchar(100)"` // bcrypt 密码哈希(60 字符)
|
||||
Nickname string `gorm:"column:nickname;type:varchar(32)"` // 显示名
|
||||
Role int8 `gorm:"column:role"` // 角色 1=超级管理员 2=普通管理员
|
||||
Status int8 `gorm:"column:status"` // 状态 1=启用 2=禁用
|
||||
LastLoginTime int64 `gorm:"column:last_login_time"` // 最近登录时间
|
||||
}
|
||||
|
||||
func (AdminUser) TableName() string {
|
||||
return "admin_users"
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
package mysqlmodel
|
||||
|
||||
// 本文件为业务服务(zt-closet-backend)数据表模型的镜像定义。
|
||||
// 管理后台与业务服务共用同一 MySQL 库,直接读写业务表;
|
||||
// 字段与表名必须与业务服务 internal/mysqlmodel 保持一致。
|
||||
|
||||
// ===== 用户 =====
|
||||
|
||||
type User struct {
|
||||
Model
|
||||
UserUUID string `gorm:"column:user_uuid"` // 用户UUID
|
||||
WxOpenID string `gorm:"column:wx_open_id;uniqueIndex:uk_user_wx_open_id"` // 微信openid
|
||||
Username string `gorm:"column:username"` // 用户名
|
||||
Avatar string `gorm:"column:avatar"` // 头像存储位置
|
||||
Sex int8 `gorm:"column:sex"` // 性别 1=男 2=女
|
||||
BirthDay int64 `gorm:"column:birth_day"` // 生日
|
||||
Phone string `gorm:"column:phone"` // 手机号
|
||||
IsNewUser bool `gorm:"column:is_new_user"` // 是否新用户
|
||||
Status int8 `gorm:"column:status"` // 状态 1=正常 2=临时禁用 3=永久禁用 4=已注销
|
||||
BanReason string `gorm:"column:ban_reason"` // 封禁原因
|
||||
BanStart int64 `gorm:"column:ban_start"` // 封禁开始时间
|
||||
BanExpire int64 `gorm:"column:ban_expire"` // 封禁到期时间
|
||||
VIPType int8 `gorm:"column:vip_type"` // VIP类型 1=非会员 2=月度会员 3=年度会员 4=体验会员
|
||||
VIPStart int64 `gorm:"column:vip_start"` // VIP开始时间
|
||||
VIPExpire int64 `gorm:"column:vip_expire"` // VIP到期时间
|
||||
LastLoginTime int64 `gorm:"column:last_login_time"` // 最近登录时间
|
||||
}
|
||||
|
||||
func (User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
// ===== 商品 =====
|
||||
|
||||
type Goods struct {
|
||||
Model
|
||||
GoodsCode string `gorm:"column:goods_code"` // 商品编码(唯一)
|
||||
GoodsName string `gorm:"column:goods_name"` // 商品名称
|
||||
GoodsType int8 `gorm:"column:goods_type"` // 商品类型 1=会员包 2=上传包 3=AI推荐包 4=衣柜分享包 5=搭配分享包
|
||||
Price int64 `gorm:"column:price"` // 商品原价(分)
|
||||
DiscountPrice int64 `gorm:"column:discount_price"` // 商品售价(分)
|
||||
MemberDays int64 `gorm:"column:member_days"` // 赠送会员天数
|
||||
CosQuota int64 `gorm:"column:cos_quota"` // 赠送总上传额度
|
||||
AIRecommendQuota int64 `gorm:"column:ai_recommend_quota"` // 赠送AI推荐额度
|
||||
ClosetShareQuota int64 `gorm:"column:closet_share_quota"` // 赠送衣柜分享额度
|
||||
OutfitShareQuota int64 `gorm:"column:outfit_share_quota"` // 赠送搭配分享额度
|
||||
ValidDays int64 `gorm:"column:valid_days"` // 赠送额度有效期天数(加量包用;会员包为 0 表示跟随 VIP)
|
||||
Description string `gorm:"column:description"` // 商品描述
|
||||
Status int8 `gorm:"column:status"` // 状态 1=上架 2=下架
|
||||
ProductID string `gorm:"column:product_id"` // 虚拟支付道具ID(空表示未发布)
|
||||
ProductStatus int8 `gorm:"column:product_status"` // 虚拟支付道具发布状态 0=未发布 1=发布中 2=已发布 3=发布失败
|
||||
}
|
||||
|
||||
func (Goods) TableName() string {
|
||||
return "goods"
|
||||
}
|
||||
|
||||
// ===== 订单 =====
|
||||
|
||||
type Order struct {
|
||||
Model
|
||||
// OrderNo 商户订单号;唯一索引保证订单号全局唯一。
|
||||
OrderNo string `gorm:"column:order_no;uniqueIndex:uk_order_no"`
|
||||
UserID int64 `gorm:"column:user_id"` // 下单用户ID
|
||||
GoodsID int64 `gorm:"column:goods_id"` // 关联商品ID
|
||||
GoodsName string `gorm:"column:goods_name"` // 商品名称快照
|
||||
UnitPrice int64 `gorm:"column:unit_price"` // 商品单价(分)
|
||||
TotalAmount int64 `gorm:"column:total_amount"` // 订单总金额(分)
|
||||
PayAmount int64 `gorm:"column:pay_amount"` // 实付金额(分)
|
||||
PayChannel int8 `gorm:"column:pay_channel"` // 支付渠道 1=微信支付 2=微信虚拟支付(XPay)
|
||||
OrderStatus int8 `gorm:"column:order_status"` // 订单状态 1=待支付 2=已支付 3=已取消 4=已过期 5=已退款
|
||||
TransactionID string `gorm:"column:transaction_id"` // 微信支付流水号
|
||||
MemberDays int64 `gorm:"column:member_days"` // 购买的会员天数(快照)
|
||||
CosQuota int64 `gorm:"column:cos_quota"` // 赠送上传额度(快照)
|
||||
AIRecommendQuota int64 `gorm:"column:ai_recommend_quota"` // 赠送AI推荐额度(快照)
|
||||
ClosetShareQuota int64 `gorm:"column:closet_share_quota"` // 赠送衣柜分享额度(快照)
|
||||
OutfitShareQuota int64 `gorm:"column:outfit_share_quota"` // 赠送搭配分享额度(快照)
|
||||
ExpireTime int64 `gorm:"column:expire_time"` // 支付过期时间
|
||||
PayTime int64 `gorm:"column:pay_time"` // 支付完成时间
|
||||
RefundTime int64 `gorm:"column:refund_time"` // 退款时间
|
||||
RefundAmount int64 `gorm:"column:refund_amount"` // 退款金额(分)
|
||||
Remark string `gorm:"column:remark"` // 订单备注
|
||||
CallbackRaw string `gorm:"column:callback_raw"` // 支付回调原始报文
|
||||
XPayProductID string `gorm:"column:xpay_product_id"` // 虚拟支付道具ID(快照,pay_channel=2 时使用)
|
||||
XPayPlatform string `gorm:"column:xpay_platform"` // 虚拟支付平台 android/ios/harmony/windows
|
||||
XPayEnv int8 `gorm:"column:xpay_env"` // 虚拟支付环境 0=现网 1=沙箱
|
||||
}
|
||||
|
||||
func (Order) TableName() string {
|
||||
return "orders"
|
||||
}
|
||||
|
||||
// ===== 退款 =====
|
||||
|
||||
type RefundFlowLog struct {
|
||||
Model
|
||||
OrderID int64 `gorm:"column:order_id"` // 关联订单ID
|
||||
OrderNo string `gorm:"column:order_no"` // 商户订单号(冗余,方便查询)
|
||||
UserID int64 `gorm:"column:user_id"` // 用户ID(冗余,方便按用户查)
|
||||
// RefundNo 商户退款单号;唯一索引为退款申请防重/重试幂等依据。
|
||||
RefundNo string `gorm:"column:refund_no;uniqueIndex:uk_refund_flow_no"`
|
||||
WechatRefundNo string `gorm:"column:wechat_refund_no"` // 微信退款单号
|
||||
RefundAmount int64 `gorm:"column:refund_amount"` // 本次退款金额(分)
|
||||
RefundReason string `gorm:"column:refund_reason"` // 退款原因
|
||||
Status int8 `gorm:"column:status"` // 退款状态 0=待处理 1=退款中 2=退款成功 3=退款失败
|
||||
ApplyTime int64 `gorm:"column:apply_time"` // 申请时间
|
||||
SuccessTime int64 `gorm:"column:success_time"` // 退款成功时间
|
||||
FailReason string `gorm:"column:fail_reason"` // 失败原因
|
||||
CallbackRaw string `gorm:"column:callback_raw"` // 微信退款回调原始报文
|
||||
}
|
||||
|
||||
func (RefundFlowLog) TableName() string {
|
||||
return "refund_flow_logs"
|
||||
}
|
||||
|
||||
// ===== 穿搭 =====
|
||||
|
||||
type Outfit struct {
|
||||
Model
|
||||
OutfitUUID string `gorm:"column:outfit_uuid"` // 穿搭UUID
|
||||
UserID int64 `gorm:"column:user_id"` // 所属用户ID
|
||||
OutfitName string `gorm:"column:outfit_name"` // 穿搭名称
|
||||
OutfitDesc string `gorm:"column:outfit_desc"` // 穿搭描述
|
||||
Scene string `gorm:"column:scene"` // 适合场景
|
||||
Source int8 `gorm:"column:source"` // 来源 1=用户创建 2=AI推荐
|
||||
IsPublic int8 `gorm:"column:is_public"` // 是否公开 1=不公开 2=公开
|
||||
AuditStatus int8 `gorm:"column:audit_status"` // 审核状态 1=待审核 2=审核通过 3=审核不通过
|
||||
Status int8 `gorm:"column:status"` // 状态 1=正常 2=封禁 3=待用户保存
|
||||
}
|
||||
|
||||
func (Outfit) TableName() string {
|
||||
return "outfits"
|
||||
}
|
||||
|
||||
type OutfitItem struct {
|
||||
Model
|
||||
OutfitID int64 `gorm:"column:outfit_id"` // 穿搭ID
|
||||
ClothesID int64 `gorm:"column:clothes_id"` // 衣物ID
|
||||
}
|
||||
|
||||
func (OutfitItem) TableName() string {
|
||||
return "outfit_items"
|
||||
}
|
||||
|
||||
type OutfitAuditLog struct {
|
||||
Model
|
||||
TargetID int64 `gorm:"column:target_id"` // 穿搭ID
|
||||
AuditResult int8 `gorm:"column:audit_result"` // 审核结果 1=通过 2=驳回
|
||||
RejectReason string `gorm:"column:reject_reason"` // 审核原因
|
||||
AuditTime int64 `gorm:"column:audit_time"` // 审核时间
|
||||
}
|
||||
|
||||
func (OutfitAuditLog) TableName() string {
|
||||
return "outfit_audit_logs"
|
||||
}
|
||||
|
||||
// ===== 衣物 =====
|
||||
|
||||
type Clothes struct {
|
||||
Model
|
||||
ClothesUUID string `gorm:"column:clothes_uuid"` // 衣物UUID
|
||||
UserID int64 `gorm:"column:user_id"` // 所属用户ID
|
||||
CategoryMain string `gorm:"column:category_main"` // 衣物大类 上装/下装/连衣裙/外套/鞋/配饰
|
||||
CategorySub string `gorm:"column:category_sub"` // 衣物小类 T恤/牛仔裤/运动鞋/发带/戒指等
|
||||
Style string `gorm:"column:style"` // 风格
|
||||
ColorMain string `gorm:"column:color"` // 主颜色
|
||||
ColorSub string `gorm:"column:color_sub"` // 副颜色
|
||||
Thickness string `gorm:"column:thickness"` // 厚度
|
||||
Version string `gorm:"column:version"` // 版型 宽松/修身/阔腿/紧身/常规合身
|
||||
Material string `gorm:"column:material"` // 材质 牛仔布/针织/皮质/纯棉/羊毛等
|
||||
Pattern string `gorm:"column:pattern"` // 花纹
|
||||
SleeveType string `gorm:"column:sleeve_type"` // 袖型
|
||||
ClothesType string `gorm:"column:clothes_type"` // 衣长类型 短款/常规/中长/长款
|
||||
NecklineType string `gorm:"column:neckline_type"` // 领型
|
||||
Formality string `gorm:"column:formality"` // 正式程度 1-5
|
||||
Occasions string `gorm:"column:occasions"` // 适用场合
|
||||
VisualImpact string `gorm:"column:visual_impact"` // 视觉冲击力 1-5
|
||||
Name string `gorm:"column:name"` // 名称
|
||||
Remark string `gorm:"column:remark"` // 备注
|
||||
ProcessedImage string `gorm:"column:processed_image"` // 处理后图片存储位置 相对路径
|
||||
ImageSize int64 `gorm:"column:image_size"` // 图片大小 单位:kb
|
||||
ImageProcessStatus int8 `gorm:"column:image_process_status"` // 处理状态 1=抠图中 2=分类中 3=排队中 4=成功 5=失败
|
||||
FailReason string `gorm:"column:fail_reason"` // 失败原因
|
||||
}
|
||||
|
||||
func (Clothes) TableName() string {
|
||||
return "clothes"
|
||||
}
|
||||
|
||||
// ===== 衣柜分享 =====
|
||||
|
||||
type ClosetShare struct {
|
||||
Model
|
||||
ShareUUID string `gorm:"column:share_uuid"` // 分享UUID
|
||||
UserID int64 `gorm:"column:user_id"` // 所属用户ID
|
||||
ShareType int8 `gorm:"column:share_type"` // 分享类型 1=部分衣物分享 2=全量快照分享
|
||||
ExpireTime int64 `gorm:"column:expire_time"` // 过期时间
|
||||
Status int8 `gorm:"column:status"` // 分享状态 1=正常 2=用户取消 3=已过期 4=管理员封禁
|
||||
}
|
||||
|
||||
func (ClosetShare) TableName() string {
|
||||
return "closet_shares"
|
||||
}
|
||||
|
||||
// ===== 额度 =====
|
||||
|
||||
type Quota struct {
|
||||
Model
|
||||
UserID int64 `gorm:"column:user_id;uniqueIndex:uk_user_quota,priority:1"` // 用户ID
|
||||
QuotaType int8 `gorm:"column:quota_type;uniqueIndex:uk_user_quota,priority:2"` // 1=cos 2=AI 3=社区分享 4=衣柜分享
|
||||
Total int64 `gorm:"column:total"` // 免费额度底值(仅 Cos 永久额度使用;AI/社区/衣柜 每日额度不再落库)
|
||||
Used int64 `gorm:"column:used"` // 免费额度已用(仅 Cos 使用,永久累计)
|
||||
}
|
||||
|
||||
func (Quota) TableName() string {
|
||||
return "user_quotas"
|
||||
}
|
||||
|
||||
// QuotaGrant 有有效期额度凭证(会员包/加量包/广告激励)。
|
||||
// AI/社区/衣柜 每日额度场景 Amount 表示「有效期内每天额外赠送次数」,Used 恒为 0;
|
||||
// Cos 永久额度场景 Amount/Used 表示「一次性赠送库存及其已用」。
|
||||
type QuotaGrant struct {
|
||||
Model
|
||||
UserID int64 `gorm:"column:user_id;index:idx_grant_user_type_expire,priority:1"` // 用户ID
|
||||
QuotaType int8 `gorm:"column:quota_type;index:idx_grant_user_type_expire,priority:2;uniqueIndex:uk_grant_ref_type,priority:2"` // 额度类型 1=cos 2=AI 3=社区分享 4=衣柜分享
|
||||
Amount int64 `gorm:"column:amount"` // 赠送总量
|
||||
Used int64 `gorm:"column:used"` // 已用量(仅 Cos 扣减)
|
||||
Source int8 `gorm:"column:source"` // 来源 1=会员包 2=加量包 3=广告激励
|
||||
ExpireTime int64 `gorm:"column:expire_time;index:idx_grant_user_type_expire,priority:3"` // 到期时间(秒,0 表示跟随 VIP)
|
||||
RefID string `gorm:"column:ref_id;type:varchar(64);uniqueIndex:uk_grant_ref_type,priority:1"` // 关联ID(订单号/广告记录ID)
|
||||
}
|
||||
|
||||
func (QuotaGrant) TableName() string {
|
||||
return "quota_grants"
|
||||
}
|
||||
|
||||
// UserDailyUsage 用户每日使用计数。
|
||||
type UserDailyUsage struct {
|
||||
Model
|
||||
// UserID 用户ID;与 StatDate 组成联合唯一索引,保证每日每用户仅一行。
|
||||
UserID int64 `gorm:"column:user_id;uniqueIndex:uk_daily_usage_user_date,priority:1"`
|
||||
StatDate int64 `gorm:"column:stat_date;uniqueIndex:uk_daily_usage_user_date,priority:2"` // 统计日期(YYYYMMDD)
|
||||
AIRecommendCount int64 `gorm:"column:ai_recommend_count"` // 当日AI推荐次数
|
||||
OutfitShareCount int64 `gorm:"column:outfit_share_count"` // 当日社区分享次数
|
||||
ClosetShareCount int64 `gorm:"column:closet_share_count"` // 当日衣柜分享次数
|
||||
}
|
||||
|
||||
func (UserDailyUsage) TableName() string {
|
||||
return "user_daily_usages"
|
||||
}
|
||||
|
||||
type QuotaFlowLog struct {
|
||||
Model
|
||||
QuotaFlowUUID string `gorm:"column:quota_flow_uuid"` // 配额流水UUID
|
||||
UserID int64 `gorm:"column:user_id"` // 用户ID
|
||||
ChangeTarget int8 `gorm:"column:change_target"` // 调整额度类型 1=配额调整 2=使用
|
||||
// ChangeType 变更类型 1=购买获赠 2=活动赠送 3=上传消耗 4=删除回退 5=人工调整 6=退款回收
|
||||
ChangeType int8 `gorm:"column:change_type;uniqueIndex:uk_quota_flow_idem,priority:3"`
|
||||
ChangeNum int64 `gorm:"column:change_num"` // 变更数量(正数增加,负数减少)
|
||||
OldValue int64 `gorm:"column:old_value"` // 变更前数值
|
||||
NewValue int64 `gorm:"column:new_value"` // 变更后数值
|
||||
// OrderNo 关联订单号(购买/退款类);人工调整类由 DAO 写入流水 UUID 保证联合唯一索引不冲突。
|
||||
OrderNo string `gorm:"column:order_no;uniqueIndex:uk_quota_flow_idem,priority:1"`
|
||||
QuotaType int8 `gorm:"column:quota_type;uniqueIndex:uk_quota_flow_idem,priority:2"` // 关联业务类型 1=cos 2=AI 3=社区分享 4=衣柜分享
|
||||
RelatedID int64 `gorm:"column:related_id"` // 关联业务ID
|
||||
Reason string `gorm:"column:reason"` // 变更原因/备注
|
||||
}
|
||||
|
||||
func (QuotaFlowLog) TableName() string {
|
||||
return "quota_flow_logs"
|
||||
}
|
||||
|
||||
// ===== VIP 流水 =====
|
||||
|
||||
type VIPFlowLog struct {
|
||||
Model
|
||||
UserID int64 `gorm:"column:user_id"` // 用户ID
|
||||
// ChangeType 变更类型 1=开通 2=续费 3=活动赠送 4=到期失效 5=人工调整 6=退款回收
|
||||
ChangeType int8 `gorm:"column:change_type;uniqueIndex:uk_vip_flow_idem,priority:2"`
|
||||
OldVIPType int8 `gorm:"column:old_vip_type"` // 变更前VIP类型 1=非会员 2=月度会员 3=年度会员 4=体验会员
|
||||
NewVIPType int8 `gorm:"column:new_vip_type"` // 变更后VIP类型 1=非会员 2=月度会员 3=年度会员 4=体验会员
|
||||
OldVIPExpire int64 `gorm:"column:old_vip_expire"` // 变更前到期时间(秒)
|
||||
NewVIPExpire int64 `gorm:"column:new_vip_expire"` // 变更后到期时间(秒)
|
||||
// OrderNo 关联订单号(购买类变更);人工调整类由 DAO 写入流水 UUID 满足联合唯一索引。
|
||||
OrderNo string `gorm:"column:order_no;uniqueIndex:uk_vip_flow_idem,priority:1"`
|
||||
Source int8 `gorm:"column:source"` // 变更来源 1=个人支付 2=活动赠送 3=系统 4=人工调整
|
||||
Reason string `gorm:"column:reason"` // 变更原因/备注
|
||||
}
|
||||
|
||||
func (VIPFlowLog) TableName() string {
|
||||
return "vip_flow_logs"
|
||||
}
|
||||
|
||||
// ===== 通知 =====
|
||||
|
||||
type UserNotification struct {
|
||||
Model
|
||||
NotificationUUID string `gorm:"column:notification_uuid"` // 消息UUID
|
||||
UserID int64 `gorm:"column:user_id"` // 接收用户ID
|
||||
MsgType int8 `gorm:"column:msg_type"` // 消息类型 1=点赞通知 2=订单通知 3=系统通知
|
||||
SenderID int64 `gorm:"column:sender_id"` // 发送者ID 系统消息为0
|
||||
Title string `gorm:"column:title"` // 消息标题
|
||||
Content string `gorm:"column:content"` // 消息内容
|
||||
IsRead int8 `gorm:"column:is_read"` // 是否已读 1=未读 2=已读
|
||||
ReadTime int64 `gorm:"column:read_time"` // 阅读时间
|
||||
}
|
||||
|
||||
func (UserNotification) TableName() string {
|
||||
return "user_notifications"
|
||||
}
|
||||
|
||||
// ===== 点赞/收藏 =====
|
||||
|
||||
type Like struct {
|
||||
Model
|
||||
// LikeType 业务类型 1=社区穿搭;与 TargetID、UserID 组成联合唯一索引,支撑点赞/取消点赞 upsert。
|
||||
LikeType int8 `gorm:"column:like_type;uniqueIndex:uk_like_user_target,priority:1"`
|
||||
TargetID int64 `gorm:"column:target_id;uniqueIndex:uk_like_user_target,priority:2"` // 穿搭ID
|
||||
UserID int64 `gorm:"column:user_id;uniqueIndex:uk_like_user_target,priority:3"` // 点赞用户ID
|
||||
Status int8 `gorm:"column:status"` // 状态 1=已点赞 0=已取消
|
||||
}
|
||||
|
||||
func (Like) TableName() string {
|
||||
return "likes"
|
||||
}
|
||||
|
||||
type Collect struct {
|
||||
Model
|
||||
// CollectType 业务类型 1=社区穿搭;与 TargetID、UserID 组成联合唯一索引,支撑收藏/取消收藏 upsert。
|
||||
CollectType int8 `gorm:"column:collect_type;uniqueIndex:uk_collect_user_target,priority:1"`
|
||||
TargetID int64 `gorm:"column:target_id;uniqueIndex:uk_collect_user_target,priority:2"` // 业务目标ID
|
||||
UserID int64 `gorm:"column:user_id;uniqueIndex:uk_collect_user_target,priority:3"` // 收藏用户ID
|
||||
Status int8 `gorm:"column:status"` // 状态 1=已收藏 0=已取消
|
||||
}
|
||||
|
||||
func (Collect) TableName() string {
|
||||
return "collects"
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package mysqlmodel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/pkg/setting"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/plugin/soft_delete"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
ID int64 `gorm:"column:id;primaryKey"` // 主键ID
|
||||
CreatedAt int64 `gorm:"column:created_at"` // 创建时间
|
||||
UpdatedAt int64 `gorm:"column:updated_at"` // 更新时间
|
||||
DeletedAt soft_delete.DeletedAt `gorm:"column:deleted_at"` // 删除时间
|
||||
CreatedBy int64 `gorm:"column:created_by"` // 创建人
|
||||
UpdatedBy int64 `gorm:"column:updated_by"` // 更新人
|
||||
DeletedBy int64 `gorm:"column:deleted_by"` // 删除人
|
||||
}
|
||||
|
||||
// NewMysqlEngine 根据配置创建 GORM MySQL 连接并完成连接池初始化。
|
||||
func NewMysqlEngine(mysqlSetting *setting.MysqlSettingS) (*gorm.DB, error) {
|
||||
s := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=10s&readTimeout=10s&writeTimeout=10s",
|
||||
mysqlSetting.UserName,
|
||||
mysqlSetting.Password,
|
||||
mysqlSetting.Host,
|
||||
mysqlSetting.Port,
|
||||
mysqlSetting.DBName,
|
||||
)
|
||||
db, err := gorm.Open(mysql.Open(s), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqlDB.SetMaxIdleConns(mysqlSetting.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(mysqlSetting.MaxOpenConns)
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(mysqlSetting.ConnMaxLifeTime) * time.Second)
|
||||
sqlDB.SetConnMaxIdleTime(time.Duration(mysqlSetting.ConnMaxIdleTime) * time.Second)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := sqlDB.PingContext(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// AutoMigrateAdminTables 仅迁移管理后台自建的表(admin_users)。
|
||||
// 业务表结构由业务服务的 golang-migrate 维护,管理后台不做 AutoMigrate,避免误改业务表。
|
||||
func AutoMigrateAdminTables(db *gorm.DB) error {
|
||||
return db.AutoMigrate(&AdminUser{})
|
||||
}
|
||||
Reference in New Issue
Block a user