muyu-apiserver/model/purpurchaseordermodel.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

104 lines
3.5 KiB
Go

package model
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ PurPurchaseOrderModel = (*customPurPurchaseOrderModel)(nil)
type (
PurPurchaseOrderModel interface {
purPurchaseOrderModel
FindList(ctx context.Context, tenantId string, page, pageSize int64, supplierId string, status int64, startDate, endDate string) ([]*PurPurchaseOrder, int64, error)
CountTodayOrders(ctx context.Context, tenantId, dateStr string) (int64, error)
UpdateReceiptProgress(ctx context.Context, orderId string, addQty float64) error
UpdatePaymentProgress(ctx context.Context, orderId string, addAmount float64) error
}
customPurPurchaseOrderModel struct {
*defaultPurPurchaseOrderModel
}
)
func NewPurPurchaseOrderModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) PurPurchaseOrderModel {
return &customPurPurchaseOrderModel{
defaultPurPurchaseOrderModel: newPurPurchaseOrderModel(conn, c, opts...),
}
}
func (m *customPurPurchaseOrderModel) FindList(ctx context.Context, tenantId string, page, pageSize int64, supplierId string, status int64, startDate, endDate string) ([]*PurPurchaseOrder, int64, error) {
where := "WHERE tenant_id = ?"
args := []interface{}{tenantId}
if supplierId != "" {
where += " AND supplier_id = ?"
args = append(args, supplierId)
}
if status != -1 {
where += " AND status = ?"
args = append(args, status)
}
if startDate != "" {
where += " AND order_date >= ?"
args = append(args, startDate)
}
if endDate != "" {
where += " AND order_date <= ?"
args = append(args, endDate)
}
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 []*PurPurchaseOrder
query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", purPurchaseOrderRows, 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 *customPurPurchaseOrderModel) CountTodayOrders(ctx context.Context, tenantId, dateStr string) (int64, error) {
var count int64
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE tenant_id = ? AND DATE(created_at) = ?", m.table)
if err := m.QueryRowNoCacheCtx(ctx, &count, query, tenantId, dateStr); err != nil {
return 0, err
}
return count, nil
}
func (m *customPurPurchaseOrderModel) UpdateReceiptProgress(ctx context.Context, orderId string, addQty float64) error {
// Recalculate receipt_status after updating received_qty
query := fmt.Sprintf(`UPDATE %s SET
received_qty = received_qty + ?,
receipt_status = CASE
WHEN (received_qty + ?) >= (SELECT COALESCE(SUM(quantity),0) FROM pur_purchase_order_detail WHERE order_id = ?) THEN 2
WHEN (received_qty + ?) > 0 THEN 1
ELSE 0
END
WHERE order_id = ?`, m.table)
_, err := m.ExecNoCacheCtx(ctx, query, addQty, addQty, orderId, addQty, orderId)
return err
}
func (m *customPurPurchaseOrderModel) UpdatePaymentProgress(ctx context.Context, orderId string, addAmount float64) error {
query := fmt.Sprintf(`UPDATE %s SET
paid_amount = paid_amount + ?,
payment_status = CASE
WHEN (paid_amount + ?) >= contract_amount THEN 2
WHEN (paid_amount + ?) > 0 THEN 1
ELSE 0
END
WHERE order_id = ?`, m.table)
_, err := m.ExecNoCacheCtx(ctx, query, addAmount, addAmount, addAmount, orderId)
return err
}