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 ( purSupplierFieldNames = builder.RawFieldNames(&PurSupplier{}) purSupplierRows = strings.Join(purSupplierFieldNames, ",") purSupplierRowsExpectAutoSet = strings.Join(stringx.Remove(purSupplierFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") purSupplierRowsWithPlaceHolder = strings.Join(stringx.Remove(purSupplierFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" cachePurSupplierIdPrefix = "cache:purSupplier:id:" cachePurSupplierSupplierIdPrefix = "cache:purSupplier:supplierId:" ) type ( purSupplierModel interface { Insert(ctx context.Context, data *PurSupplier) (sql.Result, error) FindOne(ctx context.Context, id int64) (*PurSupplier, error) FindOneBySupplierId(ctx context.Context, supplierId string) (*PurSupplier, error) Update(ctx context.Context, data *PurSupplier) error Delete(ctx context.Context, id int64) error } defaultPurSupplierModel struct { sqlc.CachedConn table string } PurSupplier struct { Id int64 `db:"id"` SupplierId string `db:"supplier_id"` TenantId string `db:"tenant_id"` SupplierName string `db:"supplier_name"` Phone string `db:"phone"` PurchaseCount int64 `db:"purchase_count"` DeliveredQty float64 `db:"delivered_qty"` PayableAmount float64 `db:"payable_amount"` PaidAmount float64 `db:"paid_amount"` Status int64 `db:"status"` Remark sql.NullString `db:"remark"` CreatedAt time.Time `db:"created_at"` UpdatedAt time.Time `db:"updated_at"` } ) func newPurSupplierModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultPurSupplierModel { return &defaultPurSupplierModel{ CachedConn: sqlc.NewConn(conn, c, opts...), table: "`pur_supplier`", } } func (m *defaultPurSupplierModel) Delete(ctx context.Context, id int64) error { data, err := m.FindOne(ctx, id) if err != nil { return err } purSupplierIdKey := fmt.Sprintf("%s%v", cachePurSupplierIdPrefix, id) purSupplierSupplierIdKey := fmt.Sprintf("%s%v", cachePurSupplierSupplierIdPrefix, data.SupplierId) _, 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) }, purSupplierIdKey, purSupplierSupplierIdKey) return err } func (m *defaultPurSupplierModel) FindOne(ctx context.Context, id int64) (*PurSupplier, error) { purSupplierIdKey := fmt.Sprintf("%s%v", cachePurSupplierIdPrefix, id) var resp PurSupplier err := m.QueryRowCtx(ctx, &resp, purSupplierIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", purSupplierRows, 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 *defaultPurSupplierModel) FindOneBySupplierId(ctx context.Context, supplierId string) (*PurSupplier, error) { purSupplierSupplierIdKey := fmt.Sprintf("%s%v", cachePurSupplierSupplierIdPrefix, supplierId) var resp PurSupplier err := m.QueryRowIndexCtx(ctx, &resp, purSupplierSupplierIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { query := fmt.Sprintf("select %s from %s where `supplier_id` = ? limit 1", purSupplierRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, supplierId); 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 *defaultPurSupplierModel) Insert(ctx context.Context, data *PurSupplier) (sql.Result, error) { purSupplierIdKey := fmt.Sprintf("%s%v", cachePurSupplierIdPrefix, data.Id) purSupplierSupplierIdKey := fmt.Sprintf("%s%v", cachePurSupplierSupplierIdPrefix, data.SupplierId) 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, purSupplierRowsExpectAutoSet) return conn.ExecCtx(ctx, query, data.SupplierId, data.TenantId, data.SupplierName, data.Phone, data.PurchaseCount, data.DeliveredQty, data.PayableAmount, data.PaidAmount, data.Status, data.Remark) }, purSupplierIdKey, purSupplierSupplierIdKey) return ret, err } func (m *defaultPurSupplierModel) Update(ctx context.Context, newData *PurSupplier) error { data, err := m.FindOne(ctx, newData.Id) if err != nil { return err } purSupplierIdKey := fmt.Sprintf("%s%v", cachePurSupplierIdPrefix, data.Id) purSupplierSupplierIdKey := fmt.Sprintf("%s%v", cachePurSupplierSupplierIdPrefix, data.SupplierId) _, 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, purSupplierRowsWithPlaceHolder) return conn.ExecCtx(ctx, query, newData.SupplierId, newData.TenantId, newData.SupplierName, newData.Phone, newData.PurchaseCount, newData.DeliveredQty, newData.PayableAmount, newData.PaidAmount, newData.Status, newData.Remark, newData.Id) }, purSupplierIdKey, purSupplierSupplierIdKey) return err } func (m *defaultPurSupplierModel) formatPrimary(primary any) string { return fmt.Sprintf("%s%v", cachePurSupplierIdPrefix, primary) } func (m *defaultPurSupplierModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", purSupplierRows, m.table) return conn.QueryRowCtx(ctx, v, query, primary) } func (m *defaultPurSupplierModel) tableName() string { return m.table }