PaymentCenter/app/services/pay_channel.go

118 lines
3.3 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"
"PaymentCenter/app/models/paychannelmodel"
"database/sql"
"xorm.io/builder"
)
func PayChannelList(req backend.PayChannelListRequest) (result []paychannelmodel.PayChannel, total int64, code int) {
repo := data.NewPayChannelRepo(paychannelmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
if req.MerchantId > 0 {
conn = conn.And(builder.Eq{"merchant_id": req.MerchantId})
}
if req.Id > 0 {
conn = conn.And(builder.Eq{"id": req.Id})
}
// 调用repo
paychannelList := make([]paychannelmodel.PayChannel, 0)
count, err := repo.PayChannelList(conn, req.PageRequest, &paychannelList)
code = handErr(err)
return paychannelList, count, code
}
func PayChannelCreate(payChannel *paychannelmodel.PayChannel) (code int) {
db := paychannelmodel.GetInstance().GetDb()
repo := data.NewPayChannelRepo(db)
merchantRepo := data.NewMerchantRepo(db)
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"id": payChannel.MerchantId})
merchant := merchantmodel.Merchant{}
has, err := merchantRepo.MerchantDetail(&merchant, conn)
if err != nil {
return handErr(err)
}
if !has {
return errorcode.MerchantNotFound
}
_, err = repo.PayChannelInsertOne(payChannel)
code = handErr(err)
return
}
func PayChannelUpdate(payChannel *paychannelmodel.PayChannel) (code int) {
db := paychannelmodel.GetInstance().GetDb()
repo := data.NewPayChannelRepo(db)
// 拼接查询条件
uconn := builder.NewCond()
uconn = uconn.And(builder.Eq{"Id": payChannel.Id})
_, err := repo.PayChannelUpdate(payChannel, uconn, "white_ip")
code = handErr(err)
return
}
func PayChannelDelete(req entities.IdRequest) (code int) {
repo := data.NewPayChannelRepo(paychannelmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"Id": req.Id})
m := paychannelmodel.PayChannel{Id: req.Id}
_, err := repo.PayChannelDelete(&m, conn)
code = handErr(err)
return
}
func PayChannelGet(payChannel *paychannelmodel.PayChannel) (code int) {
repo := data.NewPayChannelRepo(paychannelmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"id": payChannel.Id})
has, err := repo.PayChannelGet(payChannel, conn)
if err != nil {
return handErr(err)
}
if !has {
return errorcode.PayChannelNotFound
}
code = errorcode.Success
return
}
func PayChannelFindOne(channel *paychannelmodel.PayChannel, conn builder.Cond, col ...string) (merchantInfo *paychannelmodel.PayChannel, code int) {
repo := data.NewPayChannelRepo(paychannelmodel.GetInstance().GetDb())
// 拼接查询条件
channelInfo, err := repo.PayChannelFindOne(channel, conn, col...)
if err != nil {
if err == sql.ErrNoRows {
return nil, errorcode.PayChannelNotFound
}
return nil, errorcode.SystemError
}
return channelInfo, errorcode.Success
}
func GetAndCheckPayChannel(channel *paychannelmodel.PayChannel, conn builder.Cond, col ...string) (channelInfo *paychannelmodel.PayChannel, code int) {
channelInfo, code = PayChannelFindOne(channel, conn, col...)
if code != errorcode.Success {
return nil, code
}
return
}