132 lines
3.2 KiB
Go
132 lines
3.2 KiB
Go
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", "notify_url", "key_type", "public_key", "private_key", "merchant_public_key")
|
||
} 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
|
||
}
|