muyu-apiserver/model/proplanyarnratiomodel.go

54 lines
1.6 KiB
Go

package model
import (
"context"
"fmt"
"strings"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ ProPlanYarnRatioModel = (*customProPlanYarnRatioModel)(nil)
type (
ProPlanYarnRatioModel interface {
proPlanYarnRatioModel
FindByPlanId(ctx context.Context, planId string) ([]*ProPlanYarnRatio, error)
BulkInsert(ctx context.Context, data []*ProPlanYarnRatio) error
}
customProPlanYarnRatioModel struct {
*defaultProPlanYarnRatioModel
}
)
func NewProPlanYarnRatioModel(conn sqlx.SqlConn) ProPlanYarnRatioModel {
return &customProPlanYarnRatioModel{
defaultProPlanYarnRatioModel: newProPlanYarnRatioModel(conn),
}
}
func (m *customProPlanYarnRatioModel) FindByPlanId(ctx context.Context, planId string) ([]*ProPlanYarnRatio, error) {
var list []*ProPlanYarnRatio
query := fmt.Sprintf("SELECT %s FROM %s WHERE plan_id = ? ORDER BY id", proPlanYarnRatioRows, m.table)
if err := m.conn.QueryRowsCtx(ctx, &list, query, planId); err != nil {
return nil, err
}
return list, nil
}
func (m *customProPlanYarnRatioModel) BulkInsert(ctx context.Context, data []*ProPlanYarnRatio) error {
if len(data) == 0 {
return nil
}
values := make([]string, 0, len(data))
args := make([]interface{}, 0, len(data)*6)
for _, d := range data {
values = append(values, "(?, ?, ?, ?, ?, ?)")
args = append(args, d.RatioId, d.PlanId, d.YarnName, d.Ratio, d.AmountPerMeter, d.TotalAmount)
}
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", m.table, proPlanYarnRatioRowsExpectAutoSet, strings.Join(values, ","))
_, err := m.conn.ExecCtx(ctx, query, args...)
return err
}