100 lines
3.8 KiB
Go
100 lines
3.8 KiB
Go
|
|
package model
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/builder"
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
||
|
|
)
|
||
|
|
|
||
|
|
var (
|
||
|
|
purPurchasePaymentFieldNames = builder.RawFieldNames(&PurPurchasePayment{})
|
||
|
|
purPurchasePaymentRows = strings.Join(purPurchasePaymentFieldNames, ",")
|
||
|
|
purPurchasePaymentRowsExpectAutoSet = strings.Join(stringx.Remove(purPurchasePaymentFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||
|
|
purPurchasePaymentRowsWithPlaceHolder = strings.Join(stringx.Remove(purPurchasePaymentFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||
|
|
)
|
||
|
|
|
||
|
|
type (
|
||
|
|
purPurchasePaymentModel interface {
|
||
|
|
Insert(ctx context.Context, data *PurPurchasePayment) (sql.Result, error)
|
||
|
|
FindOne(ctx context.Context, id int64) (*PurPurchasePayment, error)
|
||
|
|
FindOneByPaymentId(ctx context.Context, paymentId string) (*PurPurchasePayment, error)
|
||
|
|
Update(ctx context.Context, data *PurPurchasePayment) error
|
||
|
|
Delete(ctx context.Context, id int64) error
|
||
|
|
}
|
||
|
|
|
||
|
|
defaultPurPurchasePaymentModel struct {
|
||
|
|
conn sqlx.SqlConn
|
||
|
|
table string
|
||
|
|
}
|
||
|
|
|
||
|
|
PurPurchasePayment struct {
|
||
|
|
Id int64 `db:"id"`
|
||
|
|
PaymentId string `db:"payment_id"`
|
||
|
|
TenantId string `db:"tenant_id"`
|
||
|
|
OrderId string `db:"order_id"`
|
||
|
|
SupplierId string `db:"supplier_id"`
|
||
|
|
PaymentDate time.Time `db:"payment_date"`
|
||
|
|
Amount float64 `db:"amount"`
|
||
|
|
PaymentMethod string `db:"payment_method"`
|
||
|
|
Operator string `db:"operator"`
|
||
|
|
Remark sql.NullString `db:"remark"`
|
||
|
|
CreatedAt time.Time `db:"created_at"`
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func newPurPurchasePaymentModel(conn sqlx.SqlConn) *defaultPurPurchasePaymentModel {
|
||
|
|
return &defaultPurPurchasePaymentModel{
|
||
|
|
conn: conn,
|
||
|
|
table: "`pur_purchase_payment`",
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultPurPurchasePaymentModel) Insert(ctx context.Context, data *PurPurchasePayment) (sql.Result, error) {
|
||
|
|
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, purPurchasePaymentRowsExpectAutoSet)
|
||
|
|
return m.conn.ExecCtx(ctx, query,
|
||
|
|
data.PaymentId, data.TenantId, data.OrderId, data.SupplierId, data.PaymentDate,
|
||
|
|
data.Amount, data.PaymentMethod, data.Operator, data.Remark)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultPurPurchasePaymentModel) FindOne(ctx context.Context, id int64) (*PurPurchasePayment, error) {
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE `id` = ? LIMIT 1", purPurchasePaymentRows, m.table)
|
||
|
|
var resp PurPurchasePayment
|
||
|
|
if err := m.conn.QueryRowCtx(ctx, &resp, query, id); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &resp, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultPurPurchasePaymentModel) FindOneByPaymentId(ctx context.Context, paymentId string) (*PurPurchasePayment, error) {
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE `payment_id` = ? LIMIT 1", purPurchasePaymentRows, m.table)
|
||
|
|
var resp PurPurchasePayment
|
||
|
|
if err := m.conn.QueryRowCtx(ctx, &resp, query, paymentId); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &resp, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultPurPurchasePaymentModel) Update(ctx context.Context, data *PurPurchasePayment) error {
|
||
|
|
query := fmt.Sprintf("UPDATE %s SET %s WHERE `id` = ?", m.table, purPurchasePaymentRowsWithPlaceHolder)
|
||
|
|
_, err := m.conn.ExecCtx(ctx, query,
|
||
|
|
data.PaymentId, data.TenantId, data.OrderId, data.SupplierId, data.PaymentDate,
|
||
|
|
data.Amount, data.PaymentMethod, data.Operator, data.Remark, data.Id)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultPurPurchasePaymentModel) Delete(ctx context.Context, id int64) error {
|
||
|
|
query := fmt.Sprintf("DELETE FROM %s WHERE `id` = ?", m.table)
|
||
|
|
_, err := m.conn.ExecCtx(ctx, query, id)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *defaultPurPurchasePaymentModel) tableName() string {
|
||
|
|
return m.table
|
||
|
|
}
|