// Code generated by goctl. DO NOT EDIT. package genModel 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 ( merchantFieldNames = builder.RawFieldNames(&Merchant{}) merchantRows = strings.Join(merchantFieldNames, ",") merchantRowsExpectAutoSet = strings.Join(stringx.Remove(merchantFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") merchantRowsWithPlaceHolder = strings.Join(stringx.Remove(merchantFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" cacheCouponOpenMerchantIdPrefix = "cache:couponOpen:merchant:id:" cacheCouponOpenMerchantMerchantIdPrefix = "cache:couponOpen:merchant:merchantId:" ) type ( merchantModel interface { Insert(ctx context.Context, data *Merchant) (sql.Result, error) FindOne(ctx context.Context, id uint64) (*Merchant, error) FindOneByMerchantId(ctx context.Context, merchantId string) (*Merchant, error) Update(ctx context.Context, data *Merchant) error Delete(ctx context.Context, id uint64) error } defaultMerchantModel struct { sqlc.CachedConn table string } Merchant struct { Id uint64 `db:"id"` MerchantId string `db:"merchant_id"` // 商户号 FullName string `db:"full_name"` // 商户全称 Status int64 `db:"status"` // 状态 1可用 2禁用 SignatureMethod uint64 `db:"signature_method"` // 签名方式1 Rsa2 2 Sm RsaPublicKey sql.NullString `db:"rsa_public_key"` // rsa公钥 RsaPrivateKey sql.NullString `db:"rsa_private_key"` // rsa私钥 MchRsaPublicKey sql.NullString `db:"mch_rsa_public_key"` Sm2PublicKey sql.NullString `db:"sm2_public_key"` // 国密sm2公钥 Sm2PrivateKey sql.NullString `db:"sm2_private_key"` // 国密sm2私钥 MchSm2PublicKey sql.NullString `db:"mch_sm2_public_key"` // 商户侧国密sm2公钥 Sm4SecretKey sql.NullString `db:"sm4_secret_key"` // 国密sm4报文加密密钥 IpAccess sql.NullString `db:"ip_access"` // ip白名单["192.168.6.18"] RequestRate sql.NullString `db:"request_rate"` // 请求频率限制{ "default": "500", "wechat_coupon_send" : "500"} UpdateTime sql.NullTime `db:"update_time"` // 更新时间 CreateTime time.Time `db:"create_time"` // 创建时间 Password string `db:"password"` // 登陆密码 Phone sql.NullString `db:"phone"` // 手机号 } ) func newMerchantModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultMerchantModel { return &defaultMerchantModel{ CachedConn: sqlc.NewConn(conn, c, opts...), table: "`merchant`", } } func (m *defaultMerchantModel) Delete(ctx context.Context, id uint64) error { data, err := m.FindOne(ctx, id) if err != nil { return err } couponOpenMerchantIdKey := fmt.Sprintf("%s%v", cacheCouponOpenMerchantIdPrefix, id) couponOpenMerchantMerchantIdKey := fmt.Sprintf("%s%v", cacheCouponOpenMerchantMerchantIdPrefix, data.MerchantId) _, 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) }, couponOpenMerchantIdKey, couponOpenMerchantMerchantIdKey) return err } func (m *defaultMerchantModel) FindOne(ctx context.Context, id uint64) (*Merchant, error) { couponOpenMerchantIdKey := fmt.Sprintf("%s%v", cacheCouponOpenMerchantIdPrefix, id) var resp Merchant err := m.QueryRowCtx(ctx, &resp, couponOpenMerchantIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", merchantRows, 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 *defaultMerchantModel) FindOneByMerchantId(ctx context.Context, merchantId string) (*Merchant, error) { couponOpenMerchantMerchantIdKey := fmt.Sprintf("%s%v", cacheCouponOpenMerchantMerchantIdPrefix, merchantId) var resp Merchant err := m.QueryRowIndexCtx(ctx, &resp, couponOpenMerchantMerchantIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { query := fmt.Sprintf("select %s from %s where `merchant_id` = ? limit 1", merchantRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, merchantId); 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 *defaultMerchantModel) Insert(ctx context.Context, data *Merchant) (sql.Result, error) { couponOpenMerchantIdKey := fmt.Sprintf("%s%v", cacheCouponOpenMerchantIdPrefix, data.Id) couponOpenMerchantMerchantIdKey := fmt.Sprintf("%s%v", cacheCouponOpenMerchantMerchantIdPrefix, data.MerchantId) 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, merchantRowsExpectAutoSet) return conn.ExecCtx(ctx, query, data.MerchantId, data.FullName, data.Status, data.SignatureMethod, data.RsaPublicKey, data.RsaPrivateKey, data.MchRsaPublicKey, data.Sm2PublicKey, data.Sm2PrivateKey, data.MchSm2PublicKey, data.Sm4SecretKey, data.IpAccess, data.RequestRate, data.Password, data.Phone) }, couponOpenMerchantIdKey, couponOpenMerchantMerchantIdKey) return ret, err } func (m *defaultMerchantModel) Update(ctx context.Context, newData *Merchant) error { data, err := m.FindOne(ctx, newData.Id) if err != nil { return err } couponOpenMerchantIdKey := fmt.Sprintf("%s%v", cacheCouponOpenMerchantIdPrefix, data.Id) couponOpenMerchantMerchantIdKey := fmt.Sprintf("%s%v", cacheCouponOpenMerchantMerchantIdPrefix, data.MerchantId) _, 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, merchantRowsWithPlaceHolder) return conn.ExecCtx(ctx, query, newData.MerchantId, newData.FullName, newData.Status, newData.SignatureMethod, newData.RsaPublicKey, newData.RsaPrivateKey, newData.MchRsaPublicKey, newData.Sm2PublicKey, newData.Sm2PrivateKey, newData.MchSm2PublicKey, newData.Sm4SecretKey, newData.IpAccess, newData.RequestRate, newData.Password, newData.Phone, newData.Id) }, couponOpenMerchantIdKey, couponOpenMerchantMerchantIdKey) return err } func (m *defaultMerchantModel) formatPrimary(primary any) string { return fmt.Sprintf("%s%v", cacheCouponOpenMerchantIdPrefix, primary) } func (m *defaultMerchantModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", merchantRows, m.table) return conn.QueryRowCtx(ctx, v, query, primary) } func (m *defaultMerchantModel) tableName() string { return m.table }