muyu-apiserver/model/pursuppliermodel.go
Chever John a25acdc5e4 chore: sync codebase with upstream
Pull latest changes from upstream/main (GitHub) including:
- Purchase RPC service (proto, server, logic, models)
- Gateway route updates for purchase endpoints
- SQL migration and config updates

Excludes deploy/bin/ pre-compiled arm64 binaries (builds use
multi-stage Dockerfiles targeting amd64).
2026-06-15 09:20:56 +08:00

82 lines
2.7 KiB
Go

package model
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ PurSupplierModel = (*customPurSupplierModel)(nil)
type (
PurSupplierModel interface {
purSupplierModel
FindList(ctx context.Context, tenantId string, page, pageSize int64, supplierName string, status int64) ([]*PurSupplier, int64, error)
FindOneByNameAndTenant(ctx context.Context, tenantId, supplierName string) (*PurSupplier, error)
UpdateStats(ctx context.Context, supplierId string, purchaseCountDelta int64, payableDelta, paidDelta, deliveredDelta float64) error
}
customPurSupplierModel struct {
*defaultPurSupplierModel
}
)
func NewPurSupplierModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) PurSupplierModel {
return &customPurSupplierModel{
defaultPurSupplierModel: newPurSupplierModel(conn, c, opts...),
}
}
func (m *customPurSupplierModel) FindList(ctx context.Context, tenantId string, page, pageSize int64, supplierName string, status int64) ([]*PurSupplier, int64, error) {
where := "WHERE tenant_id = ?"
args := []interface{}{tenantId}
if supplierName != "" {
where += " AND supplier_name LIKE ?"
args = append(args, "%"+supplierName+"%")
}
if status != -1 {
where += " AND status = ?"
args = append(args, status)
}
var total int64
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s %s", m.table, where)
if err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, args...); err != nil {
return nil, 0, err
}
var list []*PurSupplier
query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", purSupplierRows, m.table, where)
args = append(args, pageSize, (page-1)*pageSize)
if err := m.QueryRowsNoCacheCtx(ctx, &list, query, args...); err != nil {
return nil, 0, err
}
return list, total, nil
}
func (m *customPurSupplierModel) FindOneByNameAndTenant(ctx context.Context, tenantId, supplierName string) (*PurSupplier, error) {
var resp PurSupplier
query := fmt.Sprintf("SELECT %s FROM %s WHERE tenant_id = ? AND supplier_name = ? LIMIT 1", purSupplierRows, m.table)
err := m.QueryRowNoCacheCtx(ctx, &resp, query, tenantId, supplierName)
switch err {
case nil:
return &resp, nil
default:
return nil, err
}
}
func (m *customPurSupplierModel) UpdateStats(ctx context.Context, supplierId string, purchaseCountDelta int64, payableDelta, paidDelta, deliveredDelta float64) error {
query := fmt.Sprintf(`UPDATE %s SET
purchase_count = purchase_count + ?,
payable_amount = payable_amount + ?,
paid_amount = paid_amount + ?,
delivered_qty = delivered_qty + ?
WHERE supplier_id = ?`, m.table)
_, err := m.ExecNoCacheCtx(ctx, query, purchaseCountDelta, payableDelta, paidDelta, deliveredDelta, supplierId)
return err
}