tianyuan-api-server/apps/user/internal/model/enterpriseinfomodel.go

61 lines
2.5 KiB
Go
Raw Permalink Normal View History

2024-10-02 00:57:17 +08:00
package model
import (
"context"
"database/sql"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ EnterpriseInfoModel = (*customEnterpriseInfoModel)(nil)
type (
// EnterpriseInfoModel is an interface to be customized, add more methods here,
// and implement the added methods in customEnterpriseInfoModel.
EnterpriseInfoModel interface {
enterpriseInfoModel
InsertEnterpriseInfoTrans(ctx context.Context, enterpriseInfo *EnterpriseInfo, session sqlx.Session) (sql.Result, error)
}
customEnterpriseInfoModel struct {
*defaultEnterpriseInfoModel
}
)
// NewEnterpriseInfoModel returns a model for the database table.
func NewEnterpriseInfoModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) EnterpriseInfoModel {
return &customEnterpriseInfoModel{
defaultEnterpriseInfoModel: newEnterpriseInfoModel(conn, c, opts...),
}
}
func (m *defaultEnterpriseInfoModel) InsertEnterpriseInfoTrans(ctx context.Context, enterpriseInfo *EnterpriseInfo, session sqlx.Session) (sql.Result, error) {
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, enterpriseInfo.CreditCode)
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, enterpriseInfo.EnterpriseName)
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, enterpriseInfo.Id)
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, enterpriseInfo.UserId)
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?, ?, ?, ?)", m.table, enterpriseInfoRowsExpectAutoSet)
ret, err := session.ExecCtx(ctx, query, enterpriseInfo.UserId, enterpriseInfo.EnterpriseName, enterpriseInfo.CreditCode, enterpriseInfo.LegalPerson, enterpriseInfo.BusinessLicense, enterpriseInfo.EnterpriseContact)
if err != nil {
return nil, err
}
// 2. 更新缓存,保证所有缓存操作成功
cacheKeys := []string{enterpriseInfoCreditCodeKey, enterpriseInfoEnterpriseNameKey, enterpriseInfoIdKey, enterpriseInfoUserIdKey}
cacheErrors := make([]error, len(cacheKeys))
cacheErrors[0] = m.DelCacheCtx(ctx, enterpriseInfoCreditCodeKey)
cacheErrors[1] = m.DelCacheCtx(ctx, enterpriseInfoEnterpriseNameKey)
cacheErrors[2] = m.DelCacheCtx(ctx, enterpriseInfoIdKey)
cacheErrors[3] = m.DelCacheCtx(ctx, enterpriseInfoUserIdKey)
// 3. 检查缓存操作是否全部成功
for _, cacheErr := range cacheErrors {
if cacheErr != nil {
return nil, cacheErr // 返回第一个缓存更新失败的错误
}
}
return ret, err
}