|
- package db
-
- import (
- "applet/supply/db/model"
- "applet/supply/md"
- "errors"
- "xorm.io/xorm"
- )
-
- func GetMallShippingTemplateList(eg *xorm.Engine, req md.MallShippingTemplateListReq) ([]*model.MallShippingTemplate, int64, error) {
- var list []*model.MallShippingTemplate
- sess := eg.NewSession()
- if req.MerchantId != 0 {
- sess.Where("merchant_id = ?", req.MerchantId)
- }
- if req.Name != "" {
- sess.Where("name LIKE ?", "'%"+req.Name+"%'")
- }
- if req.CalculateType != 0 {
- sess.Where("calculate_type = ?", req.CalculateType)
- }
- if req.P == 0 || req.PSize == 0 {
- req.P = 1
- req.PSize = 10
- }
- count, err := sess.Limit(req.PSize, (req.P-1)*req.PSize).FindAndCount(&list)
- if err != nil {
- return nil, 0, err
- }
- return list, count, nil
- }
-
- func GetMallShippingTemplateById(eg *xorm.Engine, templateId int) (*model.MallShippingTemplate, error) {
- var m model.MallShippingTemplate
- has, err := eg.Where("id = ?", templateId).Get(&m)
- if err != nil {
- return nil, err
- }
- if !has {
- return nil, errors.New("没有找到相应的运费模板")
- }
- return &m, nil
- }
- func GetMallShippingTemplateByColumn(eg *xorm.Engine, column string, value interface{}) (*model.MallShippingTemplate, error) {
- var m model.MallShippingTemplate
- sql := "\"" + column + " = ?\""
- has, err := eg.Where(sql, value).Get(&m)
- if err != nil {
- return nil, err
- }
- if !has {
- return nil, errors.New("没有找到相应的运费模板")
- }
- return &m, nil
- }
-
- func InsertOneMallShippingTemplate(eg *xorm.Engine, m *model.MallShippingTemplate) error {
- affected, err := eg.InsertOne(m)
- if err != nil {
- return err
- }
- if affected == 0 {
- return errors.New("插入运费模板数据失败,稍后再试")
- }
- return nil
- }
-
- func UpdateMallShippingTemplateById(eg *xorm.Engine, m *model.MallShippingTemplate, columns ...string) error {
- var (
- affected int64
- err error
- )
- if columns == nil || len(columns) == 0 {
- affected, err = eg.Where("id = ?", m.Id).AllCols().Update(m)
- } else {
- affected, err = eg.Where("id = ?", m.Id).Cols(columns...).Update(m)
- }
- if err != nil {
- return err
- }
- if affected == 0 {
- return errors.New("插入运费模板数据失败,稍后再试")
- }
- return nil
- }
|