PaymentCenter/app/services/merchant.go

92 lines
2.6 KiB
Go

package services
import (
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/data"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/models/merchantmodel"
"database/sql"
"strings"
"xorm.io/builder"
)
// MerchantList 商户列表
func MerchantList(req backend.MerchantListRequest) (result []merchantmodel.Merchant, total int64, code int) {
repo := data.NewMerchantRepo(merchantmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
if req.Name != "" {
conn = conn.And(builder.Like{"name", req.Name})
}
if req.Contact != "" {
conn = conn.And(builder.Like{"contact", req.Contact})
}
if req.Phone != "" {
conn = conn.And(builder.Like{"phone", req.Phone})
}
if req.GroupInfo != "" {
conn = conn.And(builder.In("creator", strings.Split(req.GroupInfo, ",")))
}
// 调用repo
merchantList := make([]merchantmodel.Merchant, 0)
count, err := repo.MerchantList(conn, req.PageRequest, &merchantList)
code = handErr(err)
return merchantList, count, code
}
func MerchantCreate(merchant *merchantmodel.Merchant) (code int) {
repo := data.NewMerchantRepo(merchantmodel.GetInstance().GetDb())
_, err := repo.MerchantInsertOne(merchant)
code = handErr(err)
return
}
func MerchantUpdate(merchant *merchantmodel.Merchant) (code int) {
repo := data.NewMerchantRepo(merchantmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"Id": merchant.Id})
_, err := repo.MerchantUpdate(merchant, conn, "remark")
code = handErr(err)
return
}
func MerchantDelete(req entities.IdRequest) (code int) {
repo := data.NewMerchantRepo(merchantmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"Id": req.Id})
m := merchantmodel.Merchant{Id: req.Id}
_, err := repo.MerchantDelete(&m, conn)
code = handErr(err)
return
}
func MerchantFindOne(merchant *merchantmodel.Merchant, conn builder.Cond, col ...string) (merchantInfo *merchantmodel.Merchant, code int) {
repo := data.NewMerchantRepo(merchantmodel.GetInstance().GetDb())
// 拼接查询条件
merchantInfo, err := repo.MerchantFindOne(merchant, conn, col...)
if err != nil {
if err == sql.ErrNoRows {
return nil, errorcode.MerchantNotFound
}
return nil, errorcode.SystemError
}
return merchantInfo, errorcode.Success
}
func GetAndCheckMerchant(merchant *merchantmodel.Merchant, conn builder.Cond, col ...string) (merchantInfo *merchantmodel.Merchant, code int) {
merchantInfo, code = MerchantFindOne(merchant, conn, col...)
if code != errorcode.Success {
return nil, code
}
return
}