70 lines
2.5 KiB
Go
70 lines
2.5 KiB
Go
|
|
package model
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||
|
|
)
|
||
|
|
|
||
|
|
var _ PurPurchaseOrderDetailModel = (*customPurPurchaseOrderDetailModel)(nil)
|
||
|
|
|
||
|
|
type (
|
||
|
|
PurPurchaseOrderDetailModel interface {
|
||
|
|
purPurchaseOrderDetailModel
|
||
|
|
FindByOrderId(ctx context.Context, orderId string) ([]*PurPurchaseOrderDetail, error)
|
||
|
|
DeleteByOrderId(ctx context.Context, orderId string) error
|
||
|
|
BulkInsert(ctx context.Context, details []*PurPurchaseOrderDetail) error
|
||
|
|
UpdateReceivedQty(ctx context.Context, detailId string, addQty float64) error
|
||
|
|
}
|
||
|
|
|
||
|
|
customPurPurchaseOrderDetailModel struct {
|
||
|
|
*defaultPurPurchaseOrderDetailModel
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func NewPurPurchaseOrderDetailModel(conn sqlx.SqlConn) PurPurchaseOrderDetailModel {
|
||
|
|
return &customPurPurchaseOrderDetailModel{
|
||
|
|
defaultPurPurchaseOrderDetailModel: newPurPurchaseOrderDetailModel(conn),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customPurPurchaseOrderDetailModel) FindByOrderId(ctx context.Context, orderId string) ([]*PurPurchaseOrderDetail, error) {
|
||
|
|
var list []*PurPurchaseOrderDetail
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE order_id = ?", purPurchaseOrderDetailRows, m.table)
|
||
|
|
if err := m.conn.QueryRowsCtx(ctx, &list, query, orderId); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return list, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customPurPurchaseOrderDetailModel) DeleteByOrderId(ctx context.Context, orderId string) error {
|
||
|
|
query := fmt.Sprintf("DELETE FROM %s WHERE order_id = ?", m.table)
|
||
|
|
_, err := m.conn.ExecCtx(ctx, query, orderId)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customPurPurchaseOrderDetailModel) BulkInsert(ctx context.Context, details []*PurPurchaseOrderDetail) error {
|
||
|
|
if len(details) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
placeholder := "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||
|
|
placeholders := make([]string, len(details))
|
||
|
|
args := make([]interface{}, 0, len(details)*12)
|
||
|
|
for i, d := range details {
|
||
|
|
placeholders[i] = placeholder
|
||
|
|
args = append(args, d.DetailId, d.TenantId, d.OrderId, d.ProductId, d.ProductName, d.Spec,
|
||
|
|
d.Color, d.Quantity, d.UnitPrice, d.Amount, d.ReceivedQty, d.Remark)
|
||
|
|
}
|
||
|
|
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", m.table, purPurchaseOrderDetailRowsExpectAutoSet, strings.Join(placeholders, ","))
|
||
|
|
_, err := m.conn.ExecCtx(ctx, query, args...)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customPurPurchaseOrderDetailModel) UpdateReceivedQty(ctx context.Context, detailId string, addQty float64) error {
|
||
|
|
query := fmt.Sprintf("UPDATE %s SET received_qty = received_qty + ? WHERE detail_id = ?", m.table)
|
||
|
|
_, err := m.conn.ExecCtx(ctx, query, addQty, detailId)
|
||
|
|
return err
|
||
|
|
}
|