PaymentCenter/app/services/app.go

132 lines
3.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package services
import (
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/data"
"PaymentCenter/app/http/entities"
"PaymentCenter/app/http/entities/backend"
"PaymentCenter/app/models/appmodel"
"PaymentCenter/app/models/merchantmodel"
"PaymentCenter/app/models/paychannelmodel"
"database/sql"
"xorm.io/builder"
)
func AppList(req backend.AppListRequest) (result []appmodel.App, total int64, code int) {
repo := data.NewAppRepo(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
appList := make([]appmodel.App, 0)
count, err := repo.AppList(conn, req.PageRequest, &appList)
code = handErr(err)
return appList, count, code
}
func AppCreate(app *appmodel.App) (code int) {
db := paychannelmodel.GetInstance().GetDb()
repo := data.NewAppRepo(db)
merchantRepo := data.NewMerchantRepo(db)
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"id": app.MerchantId})
merchant := merchantmodel.Merchant{}
has, err := merchantRepo.MerchantDetail(&merchant, conn)
if err != nil {
return handErr(err)
}
if !has {
return errorcode.MerchantNotFound
}
_, err = repo.AppInsertOne(app)
code = handErr(err)
return
}
func AppUpdate(app *appmodel.App) (code int) {
var err error
db := paychannelmodel.GetInstance().GetDb()
repo := data.NewAppRepo(db)
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"Id": app.Id})
if app.AppName != "" {
// 编辑页面更新备注和白名单IP可更新为空
_, err = repo.AppUpdate(app, conn, "app_remark", "white_ip")
} else {
_, err = repo.AppUpdate(app, conn)
}
code = handErr(err)
return
}
func AppDelete(req entities.IdRequest) (code int) {
repo := data.NewAppRepo(paychannelmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"Id": req.Id})
m := appmodel.App{Id: req.Id}
_, err := repo.AppDelete(&m, conn)
code = handErr(err)
return
}
func AppFindOne(req entities.IdRequest, col ...string) (row *appmodel.App, code int) {
repo := data.NewAppRepo(paychannelmodel.GetInstance().GetDb())
// 拼接查询条件
conn := builder.NewCond()
conn = conn.And(builder.Eq{"Id": req.Id})
m := appmodel.App{Id: req.Id}
row, err := repo.AppFindOne(&m, conn, col...)
if err != nil {
if err == sql.ErrNoRows {
return nil, errorcode.AppNotFound
}
return row, errorcode.SystemError
}
return row, errorcode.Success
}
func CheckApp(appCheckIn *AppCheck) {
errCode := errorcode.Success
appCheckIn.App, errCode = AppFindOne(entities.IdRequest{Id: appCheckIn.AppId})
if errCode != errorcode.Success {
appCheckIn.Code = errCode
return
}
//检查app可用性
appCheckIn.Check()
if appCheckIn.GetCode() != errorcode.Success {
return
}
//检查白名单
if appCheckIn.Ip != "" && !appCheckIn.IpCheck(appCheckIn.Ip) {
return
}
return
}
func GetAppCheck(appId int64, ip string) *AppCheck {
appCheck := &AppCheck{
AppId: appId,
Ip: ip,
Code: errorcode.Success,
}
CheckApp(appCheck)
return appCheck
}