package product import ( "context" "database/sql" "errors" "fmt" "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 // 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 } 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 }