测试库不再使用 SQLite 内存库,改为每个测试包独立的 MySQL 库, 表结构由 backend/migrations 的真实迁移建立。 基础设施 - 新增 internal/database/testdb.go:NewTestDB() 保持原签名(28 处调用零改动), 内部连接 MySQL 测试库,首次调用重建库并执行全部真实迁移,之后每次调用清空全表 - 库名 = 测试库基础名 + 包目录名,同包共享、跨包隔离,使 go test ./... 可并发执行 (共用一库时各包会互相 DROP/CREATE 导致 database exists) - 新增 NewDryRunDB():MySQL 方言 + DryRun,供断言 SQL 形态的 5 处测试使用 - 新增 AutoMigrateIfMissing():只对不存在的表 AutoMigrate, 避免改动迁移定义的索引与约束 - 新增 scripts/prepare-test-db.sh:建库与通配授权,可重复执行 - 33 处 sqlite.Open 全部替换,26 个测试文件改用 MySQL - 删除 46 处手写 SQLite 方言 DDL(AUTOINCREMENT / INTEGER PRIMARY KEY / TEXT), 表统一由真实迁移建立 - go.mod 移除 gorm.io/driver/sqlite 与 mattn/go-sqlite3 - 删除生产代码里 distribution/repository.go 的 sqlite 方言分支(strftime) 对齐环境后暴露并修复的真实问题 - DATE(col) 在 MySQL 返回 DATE 类型,使 Date string 字段变成 RFC3339 时间串, 大盘日期会带出时间部分;28 处改为 DATE_FORMAT(col, '%Y-%m-%d') - 系统自动创建分销结算批次时 created_by 写入 0,违反指向 admin_users 的外键, 生产会直接插入失败;改为写 NULL(新增 adminRef helper) - MySQL DATETIME 仅到秒,静默截断 Go 侧写入的时间, 导致租用时长计算与结算时点回读比对偏差; rental_orders 的 9 个业务时间列提到 DATETIME(6) - 测试数据补齐严格模式要求的 NOT NULL 列、修正超出列长的取值、补齐外键父记录 迁移 - 000065 的 ALTER TABLE rental_orders 一并完成业务时间列精度调整, feature 侧不再单开迁移文件 脚本与文档 - check.sh / check-backend-coverage.sh 去掉 -p 1 串行与 SQLite 相关注释 - 更新 docs/Repository层测试补充总结.md 的测试运行说明
209 lines
5.9 KiB
Go
209 lines
5.9 KiB
Go
package notification
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Repository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
type Entry struct {
|
|
UserID uint64
|
|
Type string
|
|
Title string
|
|
Content string
|
|
BizType string
|
|
BizID *uint64
|
|
DistributionSiteID *uint64
|
|
}
|
|
|
|
func NewRepository(db *gorm.DB) *Repository {
|
|
return &Repository{db: db}
|
|
}
|
|
|
|
func (r *Repository) resolveSite(ctx context.Context, siteCode string) (uint64, bool, error) {
|
|
siteCode = strings.TrimSpace(siteCode)
|
|
if siteCode == "" {
|
|
return 0, false, nil
|
|
}
|
|
var site model.DistributionSite
|
|
if err := r.db.WithContext(ctx).Where("site_code = ? AND status = 'active'", siteCode).First(&site).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return 0, false, nil
|
|
}
|
|
return 0, false, err
|
|
}
|
|
return site.ID, true, nil
|
|
}
|
|
|
|
func (r *Repository) applySiteFilter(db *gorm.DB, siteID uint64, isSite bool) *gorm.DB {
|
|
if isSite && siteID > 0 {
|
|
return db.Where("distribution_site_id = ?", siteID)
|
|
}
|
|
return db.Where("distribution_site_id IS NULL OR distribution_site_id = 0")
|
|
}
|
|
|
|
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int, siteCode ...string) (*PaginatedResult, error) {
|
|
var code string
|
|
if len(siteCode) > 0 {
|
|
code = siteCode[0]
|
|
}
|
|
siteID, isSite, err := r.resolveSite(ctx, code)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ?", userID)
|
|
query = r.applySiteFilter(query, siteID, isSite)
|
|
|
|
var total int64
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
offset := (page - 1) * pageSize
|
|
var rows []model.Notification
|
|
dataQuery := r.db.WithContext(ctx).Where("user_id = ?", userID)
|
|
dataQuery = r.applySiteFilter(dataQuery, siteID, isSite)
|
|
if err := dataQuery.Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]NotificationDTO, 0, len(rows))
|
|
for _, row := range rows {
|
|
items = append(items, toDTO(row))
|
|
}
|
|
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
|
|
}
|
|
|
|
func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64, siteCode ...string) error {
|
|
var code string
|
|
if len(siteCode) > 0 {
|
|
code = siteCode[0]
|
|
}
|
|
siteID, isSite, err := r.resolveSite(ctx, code)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
now := time.Now()
|
|
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("id = ? AND user_id = ?", id, userID)
|
|
query = r.applySiteFilter(query, siteID, isSite)
|
|
|
|
result := query.Update("read_at", now)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
if result.RowsAffected == 0 {
|
|
checkQuery := r.db.WithContext(ctx).Model(&model.Notification{}).Where("id = ? AND user_id = ?", id, userID)
|
|
checkQuery = r.applySiteFilter(checkQuery, siteID, isSite)
|
|
var total int64
|
|
if err := checkQuery.Count(&total).Error; err != nil {
|
|
return err
|
|
}
|
|
if total == 0 {
|
|
return ErrNotificationNotFound
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Repository) UnreadCount(ctx context.Context, userID uint64, siteCode ...string) (int64, error) {
|
|
var code string
|
|
if len(siteCode) > 0 {
|
|
code = siteCode[0]
|
|
}
|
|
siteID, isSite, err := r.resolveSite(ctx, code)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", userID)
|
|
query = r.applySiteFilter(query, siteID, isSite)
|
|
|
|
var total int64
|
|
err = query.Count(&total).Error
|
|
return total, err
|
|
}
|
|
|
|
func (r *Repository) MarkAllRead(ctx context.Context, userID uint64, siteCode ...string) (int64, error) {
|
|
var code string
|
|
if len(siteCode) > 0 {
|
|
code = siteCode[0]
|
|
}
|
|
siteID, isSite, err := r.resolveSite(ctx, code)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
now := time.Now()
|
|
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", userID)
|
|
query = r.applySiteFilter(query, siteID, isSite)
|
|
|
|
result := query.Update("read_at", now)
|
|
return result.RowsAffected, result.Error
|
|
}
|
|
|
|
func Append(tx *gorm.DB, entries ...Entry) error {
|
|
for _, entry := range entries {
|
|
if entry.UserID == 0 || entry.Title == "" {
|
|
continue
|
|
}
|
|
siteID := entry.DistributionSiteID
|
|
if siteID == nil {
|
|
if (entry.BizType == "order" || entry.BizType == "order_handoff") && entry.BizID != nil && *entry.BizID > 0 {
|
|
var orderSiteID *uint64
|
|
var mode string
|
|
row := tx.Table("rental_orders").Where("id = ?", *entry.BizID).Select("distribution_site_id, distribution_mode").Row()
|
|
if err := row.Scan(&orderSiteID, &mode); err == nil && orderSiteID != nil && mode != "two_party_distributor_receive" {
|
|
siteID = orderSiteID
|
|
}
|
|
} else if entry.BizType == "dispute" && entry.BizID != nil && *entry.BizID > 0 {
|
|
var orderSiteID *uint64
|
|
var mode string
|
|
row := tx.Table("order_disputes d").Joins("JOIN rental_orders o ON o.id = d.order_id").Where("d.id = ?", *entry.BizID).Select("o.distribution_site_id, o.distribution_mode").Row()
|
|
if err := row.Scan(&orderSiteID, &mode); err == nil && orderSiteID != nil && mode != "two_party_distributor_receive" {
|
|
siteID = orderSiteID
|
|
}
|
|
}
|
|
}
|
|
row := model.Notification{
|
|
UserID: entry.UserID,
|
|
Type: entry.Type,
|
|
Title: entry.Title,
|
|
Content: entry.Content,
|
|
BizType: entry.BizType,
|
|
BizID: entry.BizID,
|
|
DistributionSiteID: siteID,
|
|
}
|
|
if row.Type == "" {
|
|
row.Type = "system"
|
|
}
|
|
if err := tx.Create(&row).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func toDTO(row model.Notification) NotificationDTO {
|
|
return NotificationDTO{
|
|
ID: row.ID,
|
|
UserID: row.UserID,
|
|
Type: row.Type,
|
|
Title: row.Title,
|
|
Content: row.Content,
|
|
BizType: row.BizType,
|
|
BizID: row.BizID,
|
|
DistributionSiteID: row.DistributionSiteID,
|
|
ReadAt: row.ReadAt,
|
|
CreatedAt: row.CreatedAt,
|
|
}
|
|
}
|