first commit
This commit is contained in:
102
apps/user/internal/model/enterpriseauthmodel.go
Normal file
102
apps/user/internal/model/enterpriseauthmodel.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ EnterpriseAuthModel = (*customEnterpriseAuthModel)(nil)
|
||||
|
||||
type (
|
||||
// EnterpriseAuthModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customEnterpriseAuthModel.
|
||||
EnterpriseAuthModel interface {
|
||||
enterpriseAuthModel
|
||||
FindLatestByUserId(ctx context.Context, userId int64) (*EnterpriseAuth, error)
|
||||
TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
|
||||
InsertEnterpriseAuthTrans(ctx context.Context, auth *EnterpriseAuth, session sqlx.Session) (sql.Result, error)
|
||||
UpdateEnterpriseAuthTrans(ctx context.Context, auth *EnterpriseAuth, session sqlx.Session) (sql.Result, error)
|
||||
FindPendingList(ctx context.Context, page, pageSize int64) ([]EnterpriseAuth, int64, error)
|
||||
}
|
||||
|
||||
customEnterpriseAuthModel struct {
|
||||
*defaultEnterpriseAuthModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewEnterpriseAuthModel returns a model for the database table.
|
||||
func NewEnterpriseAuthModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) EnterpriseAuthModel {
|
||||
return &customEnterpriseAuthModel{
|
||||
defaultEnterpriseAuthModel: newEnterpriseAuthModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
func (m *defaultEnterpriseAuthModel) FindLatestByUserId(ctx context.Context, userId int64) (*EnterpriseAuth, error) {
|
||||
query := fmt.Sprintf("SELECT * FROM %s WHERE user_id = ? ORDER BY created_at DESC LIMIT 1", m.table)
|
||||
var resp EnterpriseAuth
|
||||
err := m.QueryRowsNoCacheCtx(ctx, &resp, query, userId)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sql.ErrNoRows:
|
||||
return nil, sql.ErrNoRows
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
|
||||
// 使用带 ctx 的事务处理
|
||||
err := m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
return fn(ctx, session)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) InsertEnterpriseAuthTrans(ctx context.Context, auth *EnterpriseAuth, session sqlx.Session) (sql.Result, error) {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, auth.Id)
|
||||
|
||||
query := fmt.Sprintf("INSERT INTO %s (user_id, enterprise_name, credit_code, legal_person, business_license, enterprise_contact, auth_status) VALUES (?, ?, ?, ?, ?, ?, ?)", m.table)
|
||||
ret, err := session.ExecCtx(ctx, query, auth.UserId, auth.EnterpriseName, auth.CreditCode, auth.LegalPerson, auth.BusinessLicense, auth.EnterpriseContact, auth.AuthStatus)
|
||||
if err == nil {
|
||||
err = m.DelCacheCtx(ctx, enterpriseAuthIdKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
func (m *defaultEnterpriseAuthModel) UpdateEnterpriseAuthTrans(ctx context.Context, auth *EnterpriseAuth, session sqlx.Session) (sql.Result, error) {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, auth.Id)
|
||||
query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, enterpriseAuthRowsWithPlaceHolder)
|
||||
ret, err := session.ExecCtx(ctx, query, auth.UserId, auth.EnterpriseName, auth.CreditCode, auth.LegalPerson, auth.BusinessLicense, auth.EnterpriseContact, auth.AuthStatus, auth.Id)
|
||||
if err == nil {
|
||||
err = m.DelCacheCtx(ctx, enterpriseAuthIdKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
func (m *defaultEnterpriseAuthModel) FindPendingList(ctx context.Context, page, pageSize int64) ([]EnterpriseAuth, int64, error) {
|
||||
offset := (page - 1) * pageSize
|
||||
var enterprises []EnterpriseAuth
|
||||
|
||||
query := fmt.Sprintf("SELECT * FROM %s WHERE auth_status = 'pending' ORDER BY created_at DESC LIMIT ?,?", m.table)
|
||||
err := m.QueryRowsNoCacheCtx(ctx, &enterprises, query, offset, pageSize)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 查询总数量
|
||||
var total int64
|
||||
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE auth_status = 'pending'", m.table)
|
||||
err = m.QueryRowNoCacheCtx(ctx, &total, countQuery)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return enterprises, total, nil
|
||||
}
|
||||
119
apps/user/internal/model/enterpriseauthmodel_gen.go
Normal file
119
apps/user/internal/model/enterpriseauthmodel_gen.go
Normal file
@@ -0,0 +1,119 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.2
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
enterpriseAuthFieldNames = builder.RawFieldNames(&EnterpriseAuth{})
|
||||
enterpriseAuthRows = strings.Join(enterpriseAuthFieldNames, ",")
|
||||
enterpriseAuthRowsExpectAutoSet = strings.Join(stringx.Remove(enterpriseAuthFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
enterpriseAuthRowsWithPlaceHolder = strings.Join(stringx.Remove(enterpriseAuthFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
|
||||
cacheEnterpriseAuthIdPrefix = "cache:enterpriseAuth:id:"
|
||||
)
|
||||
|
||||
type (
|
||||
enterpriseAuthModel interface {
|
||||
Insert(ctx context.Context, data *EnterpriseAuth) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*EnterpriseAuth, error)
|
||||
Update(ctx context.Context, data *EnterpriseAuth) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultEnterpriseAuthModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
EnterpriseAuth struct {
|
||||
Id int64 `db:"id"` // 认证ID
|
||||
UserId int64 `db:"user_id"` // 关联的用户ID
|
||||
EnterpriseName string `db:"enterprise_name"` // 企业名称
|
||||
CreditCode string `db:"credit_code"` // 企业统一信用代码
|
||||
LegalPerson string `db:"legal_person"` // 法人代表
|
||||
BusinessLicense string `db:"business_license"` // 营业执照存储路径
|
||||
EnterpriseContact string `db:"enterprise_contact"` // 企业联系方式
|
||||
AuthStatus string `db:"auth_status"` // 认证状态:unverified=未提交,pending=待审核,approved=审核通过,rejected=审核拒绝
|
||||
CreatedAt time.Time `db:"created_at"` // 认证创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 认证更新时间
|
||||
}
|
||||
)
|
||||
|
||||
func newEnterpriseAuthModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultEnterpriseAuthModel {
|
||||
return &defaultEnterpriseAuthModel{
|
||||
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||||
table: "`enterprise_auth`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) Delete(ctx context.Context, id int64) error {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, id)
|
||||
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, enterpriseAuthIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) FindOne(ctx context.Context, id int64) (*EnterpriseAuth, error) {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, id)
|
||||
var resp EnterpriseAuth
|
||||
err := m.QueryRowCtx(ctx, &resp, enterpriseAuthIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", enterpriseAuthRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) Insert(ctx context.Context, data *EnterpriseAuth) (sql.Result, error) {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, data.Id)
|
||||
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, enterpriseAuthRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.UserId, data.EnterpriseName, data.CreditCode, data.LegalPerson, data.BusinessLicense, data.EnterpriseContact, data.AuthStatus)
|
||||
}, enterpriseAuthIdKey)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) Update(ctx context.Context, data *EnterpriseAuth) error {
|
||||
enterpriseAuthIdKey := fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, data.Id)
|
||||
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, enterpriseAuthRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, data.UserId, data.EnterpriseName, data.CreditCode, data.LegalPerson, data.BusinessLicense, data.EnterpriseContact, data.AuthStatus, data.Id)
|
||||
}, enterpriseAuthIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheEnterpriseAuthIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", enterpriseAuthRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary)
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseAuthModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
60
apps/user/internal/model/enterpriseinfomodel.go
Normal file
60
apps/user/internal/model/enterpriseinfomodel.go
Normal file
@@ -0,0 +1,60 @@
|
||||
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
|
||||
}
|
||||
203
apps/user/internal/model/enterpriseinfomodel_gen.go
Normal file
203
apps/user/internal/model/enterpriseinfomodel_gen.go
Normal file
@@ -0,0 +1,203 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.2
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
enterpriseInfoFieldNames = builder.RawFieldNames(&EnterpriseInfo{})
|
||||
enterpriseInfoRows = strings.Join(enterpriseInfoFieldNames, ",")
|
||||
enterpriseInfoRowsExpectAutoSet = strings.Join(stringx.Remove(enterpriseInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
enterpriseInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(enterpriseInfoFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
|
||||
cacheEnterpriseInfoIdPrefix = "cache:enterpriseInfo:id:"
|
||||
cacheEnterpriseInfoCreditCodePrefix = "cache:enterpriseInfo:creditCode:"
|
||||
cacheEnterpriseInfoEnterpriseNamePrefix = "cache:enterpriseInfo:enterpriseName:"
|
||||
cacheEnterpriseInfoUserIdPrefix = "cache:enterpriseInfo:userId:"
|
||||
)
|
||||
|
||||
type (
|
||||
enterpriseInfoModel interface {
|
||||
Insert(ctx context.Context, data *EnterpriseInfo) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*EnterpriseInfo, error)
|
||||
FindOneByCreditCode(ctx context.Context, creditCode string) (*EnterpriseInfo, error)
|
||||
FindOneByEnterpriseName(ctx context.Context, enterpriseName string) (*EnterpriseInfo, error)
|
||||
FindOneByUserId(ctx context.Context, userId int64) (*EnterpriseInfo, error)
|
||||
Update(ctx context.Context, data *EnterpriseInfo) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultEnterpriseInfoModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
EnterpriseInfo struct {
|
||||
Id int64 `db:"id"` // 企业信息ID
|
||||
UserId int64 `db:"user_id"` // 关联的用户ID
|
||||
EnterpriseName string `db:"enterprise_name"` // 企业名称
|
||||
CreditCode string `db:"credit_code"` // 企业统一信用代码
|
||||
LegalPerson string `db:"legal_person"` // 法人代表
|
||||
BusinessLicense string `db:"business_license"` // 营业执照存储路径
|
||||
EnterpriseContact string `db:"enterprise_contact"` // 企业联系方式
|
||||
CreatedAt time.Time `db:"created_at"` // 企业信息创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 企业信息更新时间
|
||||
}
|
||||
)
|
||||
|
||||
func newEnterpriseInfoModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultEnterpriseInfoModel {
|
||||
return &defaultEnterpriseInfoModel{
|
||||
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||||
table: "`enterprise_info`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, data.CreditCode)
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, data.EnterpriseName)
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, id)
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, data.UserId)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, enterpriseInfoCreditCodeKey, enterpriseInfoEnterpriseNameKey, enterpriseInfoIdKey, enterpriseInfoUserIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) FindOne(ctx context.Context, id int64) (*EnterpriseInfo, error) {
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, id)
|
||||
var resp EnterpriseInfo
|
||||
err := m.QueryRowCtx(ctx, &resp, enterpriseInfoIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) FindOneByCreditCode(ctx context.Context, creditCode string) (*EnterpriseInfo, error) {
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, creditCode)
|
||||
var resp EnterpriseInfo
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, enterpriseInfoCreditCodeKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `credit_code` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, creditCode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) FindOneByEnterpriseName(ctx context.Context, enterpriseName string) (*EnterpriseInfo, error) {
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, enterpriseName)
|
||||
var resp EnterpriseInfo
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, enterpriseInfoEnterpriseNameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `enterprise_name` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, enterpriseName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) FindOneByUserId(ctx context.Context, userId int64) (*EnterpriseInfo, error) {
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, userId)
|
||||
var resp EnterpriseInfo
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, enterpriseInfoUserIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `user_id` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, userId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) Insert(ctx context.Context, data *EnterpriseInfo) (sql.Result, error) {
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, data.CreditCode)
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, data.EnterpriseName)
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, data.Id)
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, data.UserId)
|
||||
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, enterpriseInfoRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.UserId, data.EnterpriseName, data.CreditCode, data.LegalPerson, data.BusinessLicense, data.EnterpriseContact)
|
||||
}, enterpriseInfoCreditCodeKey, enterpriseInfoEnterpriseNameKey, enterpriseInfoIdKey, enterpriseInfoUserIdKey)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) Update(ctx context.Context, newData *EnterpriseInfo) error {
|
||||
data, err := m.FindOne(ctx, newData.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
enterpriseInfoCreditCodeKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoCreditCodePrefix, data.CreditCode)
|
||||
enterpriseInfoEnterpriseNameKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoEnterpriseNamePrefix, data.EnterpriseName)
|
||||
enterpriseInfoIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, data.Id)
|
||||
enterpriseInfoUserIdKey := fmt.Sprintf("%s%v", cacheEnterpriseInfoUserIdPrefix, data.UserId)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, enterpriseInfoRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, newData.UserId, newData.EnterpriseName, newData.CreditCode, newData.LegalPerson, newData.BusinessLicense, newData.EnterpriseContact, newData.Id)
|
||||
}, enterpriseInfoCreditCodeKey, enterpriseInfoEnterpriseNameKey, enterpriseInfoIdKey, enterpriseInfoUserIdKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheEnterpriseInfoIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", enterpriseInfoRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary)
|
||||
}
|
||||
|
||||
func (m *defaultEnterpriseInfoModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
78
apps/user/internal/model/usersmodel.go
Normal file
78
apps/user/internal/model/usersmodel.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
var _ UsersModel = (*customUsersModel)(nil)
|
||||
|
||||
type (
|
||||
// UsersModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customUsersModel.
|
||||
UsersModel interface {
|
||||
usersModel
|
||||
UpdateUserTrans(ctx context.Context, user *Users, session sqlx.Session) (sql.Result, error)
|
||||
FindOneTrans(ctx context.Context, userId int64, session sqlx.Session) (*Users, error)
|
||||
}
|
||||
|
||||
customUsersModel struct {
|
||||
*defaultUsersModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewUsersModel returns a model for the database table.
|
||||
func NewUsersModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UsersModel {
|
||||
return &customUsersModel{
|
||||
defaultUsersModel: newUsersModel(conn, c, opts...),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) UpdateUserTrans(ctx context.Context, user *Users, session sqlx.Session) (sql.Result, error) {
|
||||
userIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, user.Id)
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, user.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, user.Username)
|
||||
query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, usersRowsWithPlaceHolder)
|
||||
ret, err := session.ExecCtx(ctx, query, user.Username, user.Password, user.Phone, user.AuthStatus, user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. 删除缓存,保证所有缓存操作成功
|
||||
cacheKeys := []string{userIdKey, usersPhoneKey, usersUsernameKey}
|
||||
cacheErrors := make([]error, len(cacheKeys))
|
||||
|
||||
cacheErrors[0] = m.DelCacheCtx(ctx, userIdKey)
|
||||
cacheErrors[1] = m.DelCacheCtx(ctx, usersPhoneKey)
|
||||
cacheErrors[2] = m.DelCacheCtx(ctx, usersUsernameKey)
|
||||
|
||||
// 3. 检查缓存操作是否全部成功
|
||||
for _, cacheErr := range cacheErrors {
|
||||
if cacheErr != nil {
|
||||
return nil, cacheErr // 返回第一个缓存更新失败的错误
|
||||
}
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
func (m *defaultUsersModel) FindOneTrans(ctx context.Context, userId int64, session sqlx.Session) (*Users, error) {
|
||||
// 定义 SQL 查询语句
|
||||
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? LIMIT 1", usersRows, m.table)
|
||||
|
||||
var user Users
|
||||
// 在事务上下文中执行查询
|
||||
err := session.QueryRowCtx(ctx, &user, query, userId)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
// 如果没有找到记录,返回 nil 和 ErrNotFound 错误
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 返回查询结果
|
||||
return &user, nil
|
||||
}
|
||||
176
apps/user/internal/model/usersmodel_gen.go
Normal file
176
apps/user/internal/model/usersmodel_gen.go
Normal file
@@ -0,0 +1,176 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.7.2
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
usersFieldNames = builder.RawFieldNames(&Users{})
|
||||
usersRows = strings.Join(usersFieldNames, ",")
|
||||
usersRowsExpectAutoSet = strings.Join(stringx.Remove(usersFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
usersRowsWithPlaceHolder = strings.Join(stringx.Remove(usersFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
|
||||
cacheUsersIdPrefix = "cache:users:id:"
|
||||
cacheUsersPhonePrefix = "cache:users:phone:"
|
||||
cacheUsersUsernamePrefix = "cache:users:username:"
|
||||
)
|
||||
|
||||
type (
|
||||
usersModel interface {
|
||||
Insert(ctx context.Context, data *Users) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*Users, error)
|
||||
FindOneByPhone(ctx context.Context, phone string) (*Users, error)
|
||||
FindOneByUsername(ctx context.Context, username string) (*Users, error)
|
||||
Update(ctx context.Context, data *Users) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultUsersModel struct {
|
||||
sqlc.CachedConn
|
||||
table string
|
||||
}
|
||||
|
||||
Users struct {
|
||||
Id int64 `db:"id"` // 用户ID
|
||||
Username string `db:"username"` // 用户名
|
||||
Password string `db:"password"` // 用户密码
|
||||
Phone string `db:"phone"` // 用户手机号
|
||||
AuthStatus string `db:"auth_status"` // 认证状态:unverified=未提交,pending=待审核,approved=审核通过,rejected=审核拒绝
|
||||
CreatedAt time.Time `db:"created_at"` // 用户创建时间
|
||||
UpdatedAt time.Time `db:"updated_at"` // 用户更新时间
|
||||
}
|
||||
)
|
||||
|
||||
func newUsersModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultUsersModel {
|
||||
return &defaultUsersModel{
|
||||
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||||
table: "`users`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, id)
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, usersIdKey, usersPhoneKey, usersUsernameKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) FindOne(ctx context.Context, id int64) (*Users, error) {
|
||||
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, id)
|
||||
var resp Users
|
||||
err := m.QueryRowCtx(ctx, &resp, usersIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", usersRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id)
|
||||
})
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) FindOneByPhone(ctx context.Context, phone string) (*Users, error) {
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, phone)
|
||||
var resp Users
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, usersPhoneKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `phone` = ? limit 1", usersRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, phone); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) FindOneByUsername(ctx context.Context, username string) (*Users, error) {
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, username)
|
||||
var resp Users
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, usersUsernameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `username` = ? limit 1", usersRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, username); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Id, nil
|
||||
}, m.queryPrimary)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlc.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) Insert(ctx context.Context, data *Users) (sql.Result, error) {
|
||||
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, data.Id)
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
|
||||
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, usersRowsExpectAutoSet)
|
||||
return conn.ExecCtx(ctx, query, data.Username, data.Password, data.Phone, data.AuthStatus)
|
||||
}, usersIdKey, usersPhoneKey, usersUsernameKey)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) Update(ctx context.Context, newData *Users) error {
|
||||
data, err := m.FindOne(ctx, newData.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, data.Id)
|
||||
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
|
||||
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
|
||||
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, usersRowsWithPlaceHolder)
|
||||
return conn.ExecCtx(ctx, query, newData.Username, newData.Password, newData.Phone, newData.AuthStatus, newData.Id)
|
||||
}, usersIdKey, usersPhoneKey, usersUsernameKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) formatPrimary(primary any) string {
|
||||
return fmt.Sprintf("%s%v", cacheUsersIdPrefix, primary)
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", usersRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, primary)
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
5
apps/user/internal/model/vars.go
Normal file
5
apps/user/internal/model/vars.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
Reference in New Issue
Block a user