package product import ( "context" "database/sql" "errors" "fmt" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlx" ) var ErrUpdateConflict = errors.New("update conflict: data has been modified by another operation") var _ SysProductModel = (*customSysProductModel)(nil) type ( SysProductModel interface { sysProductModel FindList(ctx context.Context, page, pageSize int64) ([]*SysProduct, int64, error) UpdateWithOptLock(ctx context.Context, data *SysProduct, expectedUpdateTime int64) error // UpdateWithOptLockTx 与 UpdateWithOptLock 的 SQL 语义完全一致(WHERE id=? AND updateTime=?), // 区别仅在于 UPDATE 执行在调用方传入的事务里。用于 UpdateProduct 把"产品行 CAS 更新 + // 成员 userId 读取 + 批量 tokenVersion 递增"串成原子事务(审计 L-R15-3)。 // // 审计 L-R12-1:本方法**不做**缓存失效——事务尚未 commit 时失效会把未落盘的新值灌入缓存; // 调用方在事务 commit 成功后负责走相应的 post-commit 链路(通常由 UserDetailsLoader.CleanByProduct // + 底层 sysProduct cache 失效配合覆盖)。session==nil 时直接拒绝。 UpdateWithOptLockTx(ctx context.Context, session sqlx.Session, data *SysProduct, expectedUpdateTime int64) error // InvalidateProductCache 失效 sysProduct 的 id / appKey / code 三把低层缓存键。对齐 // sysUserModel.InvalidateProfileCache 的 L-R12-1 契约,仅应在事务 commit 成功后由调用方 // 显式调用;best-effort,失败只留日志。 InvalidateProductCache(ctx context.Context, id int64, appKey, code string) // LockByCodeTx 在当前事务里锁定 product 行(SELECT ... FOR UPDATE),用于把跨表写入(如权限同步) // 按 product 串行化,避免两次并发 SyncPermissions 在 sys_perm UNIQUE(productCode, code) 上撞 1062。 LockByCodeTx(ctx context.Context, session sqlx.Session, code string) (*SysProduct, error) } customSysProductModel struct { *defaultSysProductModel } ) func NewSysProductModel(conn sqlx.SqlConn, c cache.CacheConf, cachePrefix string, opts ...cache.Option) SysProductModel { return &customSysProductModel{ defaultSysProductModel: newSysProductModel(conn, c, cachePrefix, opts...), } } func (m *customSysProductModel) UpdateWithOptLock(ctx context.Context, data *SysProduct, expectedUpdateTime int64) error { sysProductIdKey := fmt.Sprintf("%s%v", cacheSysProductIdPrefix, data.Id) sysProductAppKeyKey := fmt.Sprintf("%s%v", cacheSysProductAppKeyPrefix, data.AppKey) sysProductCodeKey := fmt.Sprintf("%s%v", cacheSysProductCodePrefix, data.Code) res, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (sql.Result, error) { query := fmt.Sprintf("UPDATE %s SET `name`=?, `remark`=?, `status`=?, `updateTime`=? WHERE `id`=? AND `updateTime`=?", m.table) return conn.ExecCtx(ctx, query, data.Name, data.Remark, data.Status, data.UpdateTime, data.Id, expectedUpdateTime) }, sysProductIdKey, sysProductAppKeyKey, sysProductCodeKey) if err != nil { return err } affected, _ := res.RowsAffected() if affected == 0 { return ErrUpdateConflict } return nil } // InvalidateProductCache 见接口注释(审计 L-R12-1 / L-R15-3)。 func (m *customSysProductModel) InvalidateProductCache(ctx context.Context, id int64, appKey, code string) { sysProductIdKey := fmt.Sprintf("%s%v", cacheSysProductIdPrefix, id) sysProductAppKeyKey := fmt.Sprintf("%s%v", cacheSysProductAppKeyPrefix, appKey) sysProductCodeKey := fmt.Sprintf("%s%v", cacheSysProductCodePrefix, code) if err := m.DelCacheCtx(ctx, sysProductIdKey, sysProductAppKeyKey, sysProductCodeKey); err != nil { if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { logx.WithContext(ctx).Errorw("cache invalidation skipped: ctx canceled", logx.Field("audit", "cache_invalidation_skipped_due_to_ctx_cancel"), logx.Field("scope", "sysProductModel.InvalidateProductCache"), logx.Field("id", id), logx.Field("err", err.Error()), ) } else { logx.WithContext(ctx).Errorf("sysProductModel.InvalidateProductCache failed: id=%d err=%v", id, err) } } } // UpdateWithOptLockTx 见接口注释(审计 L-R15-3 / L-R12-1)。 func (m *customSysProductModel) UpdateWithOptLockTx(ctx context.Context, session sqlx.Session, data *SysProduct, expectedUpdateTime int64) error { if session == nil { return errors.New("UpdateWithOptLockTx requires a non-nil session") } query := fmt.Sprintf("UPDATE %s SET `name`=?, `remark`=?, `status`=?, `updateTime`=? WHERE `id`=? AND `updateTime`=?", m.table) res, err := session.ExecCtx(ctx, query, data.Name, data.Remark, data.Status, data.UpdateTime, data.Id, expectedUpdateTime) if err != nil { return err } affected, _ := res.RowsAffected() if affected == 0 { return ErrUpdateConflict } return nil } func (m *customSysProductModel) LockByCodeTx(ctx context.Context, session sqlx.Session, code string) (*SysProduct, error) { var resp SysProduct query := fmt.Sprintf("SELECT %s FROM %s WHERE `code` = ? LIMIT 1 FOR UPDATE", sysProductRows, m.table) if err := session.QueryRowCtx(ctx, &resp, query, code); err != nil { return nil, err } return &resp, nil } func (m *customSysProductModel) FindList(ctx context.Context, page, pageSize int64) ([]*SysProduct, int64, error) { var total int64 countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", m.table) if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery); err != nil { return nil, 0, err } var list []*SysProduct query := fmt.Sprintf("SELECT %s FROM %s ORDER BY id DESC LIMIT ?,?", sysProductRows, m.table) if err := m.QueryRowsNoCacheCtx(ctx, &list, query, (page-1)*pageSize, pageSize); err != nil { return nil, 0, err } return list, total, nil }