220 lines
5.4 KiB
Go
220 lines
5.4 KiB
Go
package merchant
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/beego/beego/v2/client/orm"
|
||
"github.com/beego/beego/v2/core/logs"
|
||
)
|
||
|
||
type MerchantInfo struct {
|
||
Id int
|
||
Status string
|
||
BelongAgentUid string
|
||
BelongAgentName string
|
||
MerchantName string
|
||
MerchantUid string
|
||
MerchantKey string
|
||
MerchantSecret string
|
||
LoginPassword string
|
||
LoginAccount string
|
||
AutoSettle string
|
||
AutoPayFor string
|
||
WhiteIps string
|
||
Remark string
|
||
SinglePayForRoadUid string
|
||
SinglePayForRoadName string
|
||
RollPayForRoadCode string
|
||
RollPayForRoadName string
|
||
PayforFee float64
|
||
UpdateTime string
|
||
CreateTime string
|
||
OtpSecret string
|
||
}
|
||
|
||
type ProfitMargin struct {
|
||
Sort int `json:"sort" orm:"sort,omitempty"`
|
||
ShowLabel float64 `json:"showLabel" orm:"show_label,omitempty"`
|
||
FactLabel float64 `json:"factLabel" orm:"fact_label,omitempty"`
|
||
Value float64 `json:"value" orm:"value,omitempty"`
|
||
PlatformLabel string `json:"platformLabel" description:"平台" orm:"platform_label,omitempty"`
|
||
IsLinkSingle bool `json:"isLinkSingle" description:"链接是否单独放置" orm:"is_link_single,omitempty"`
|
||
LinkID string `json:"linkID" orm:"link_id,omitempty"`
|
||
}
|
||
|
||
const MERCHANT_INFO = "merchant_info"
|
||
|
||
func IsExistByMerchantName(ctx context.Context, merchantName string) bool {
|
||
o := orm.NewOrm()
|
||
exist := o.QueryTable(MERCHANT_INFO).Filter("merchant_name", merchantName).ExistWithCtx(ctx)
|
||
return exist
|
||
}
|
||
|
||
func IsExistByMerchantUid(ctx context.Context, uid string) bool {
|
||
o := orm.NewOrm()
|
||
exist := o.QueryTable(MERCHANT_INFO).Filter("merchant_uid", uid).ExistWithCtx(ctx)
|
||
|
||
return exist
|
||
}
|
||
|
||
func IsExistMerchantByAgentUid(ctx context.Context, uid string) bool {
|
||
o := orm.NewOrm()
|
||
exist := o.QueryTable(MERCHANT_INFO).Filter("belong_agent_uid", uid).ExistWithCtx(ctx)
|
||
|
||
return exist
|
||
}
|
||
|
||
func IsExistByMerchantPhone(phone string) bool {
|
||
o := orm.NewOrm()
|
||
exist := o.QueryTable(MERCHANT_INFO).Filter("LoginAccount", phone).Exist()
|
||
|
||
return exist
|
||
}
|
||
|
||
func GetMerchantByPhone(phone string) (m MerchantInfo) {
|
||
o := orm.NewOrm()
|
||
_, e := o.QueryTable(MERCHANT_INFO).Filter("LoginAccount", phone).Limit(1).All(&m)
|
||
if e != nil {
|
||
logs.Error("GetMerchantByPhone merchant fail: ", e)
|
||
}
|
||
return m
|
||
}
|
||
|
||
func InsertMerchantInfo(merchantInfo MerchantInfo) bool {
|
||
o := orm.NewOrm()
|
||
_, err := o.Insert(&merchantInfo)
|
||
if err != nil {
|
||
logs.Error("insert merchant fail: ", err)
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
func GetMerchantLenByMap(params map[string]string) int {
|
||
o := orm.NewOrm()
|
||
qs := o.QueryTable(MERCHANT_INFO)
|
||
for k, v := range params {
|
||
if len(v) > 0 {
|
||
qs = qs.Filter(k, v)
|
||
}
|
||
}
|
||
cnt, err := qs.Count()
|
||
if err != nil {
|
||
logs.Error("get merchant len by map fail: ", err)
|
||
}
|
||
return int(cnt)
|
||
}
|
||
|
||
func GetMerchantListByMap(params map[string]string, displayCount, offset int) []MerchantInfo {
|
||
o := orm.NewOrm()
|
||
qs := o.QueryTable(MERCHANT_INFO)
|
||
for k, v := range params {
|
||
if len(v) > 0 {
|
||
qs = qs.Filter(k, v)
|
||
}
|
||
}
|
||
var merchantList []MerchantInfo
|
||
_, err := qs.Limit(displayCount, offset).OrderBy("-update_time").All(&merchantList)
|
||
if err != nil {
|
||
logs.Error("get merchant list by map fail: ", err)
|
||
}
|
||
return merchantList
|
||
}
|
||
|
||
func GetAllMerchant() []MerchantInfo {
|
||
o := orm.NewOrm()
|
||
var merchantList []MerchantInfo
|
||
|
||
_, err := o.QueryTable(MERCHANT_INFO).Limit(-1).All(&merchantList)
|
||
if err != nil {
|
||
logs.Error("get all merchant fail:", err)
|
||
}
|
||
|
||
return merchantList
|
||
}
|
||
|
||
func GetMerchantByParams(params map[string]string, displayCount, offset int) []MerchantInfo {
|
||
o := orm.NewOrm()
|
||
var merchantList []MerchantInfo
|
||
qs := o.QueryTable(MERCHANT_INFO)
|
||
for k, v := range params {
|
||
if len(v) > 0 {
|
||
qs = qs.Filter(k, v)
|
||
}
|
||
}
|
||
|
||
qs.Limit(displayCount, offset).All(&merchantList)
|
||
|
||
return merchantList
|
||
}
|
||
|
||
func GetMerchantLenByParams(params map[string]string) int {
|
||
o := orm.NewOrm()
|
||
qs := o.QueryTable(MERCHANT_INFO)
|
||
for k, v := range params {
|
||
if len(v) > 0 {
|
||
qs = qs.Filter(k, v)
|
||
}
|
||
}
|
||
|
||
cnt, err := qs.Limit(-1).Count()
|
||
|
||
if err != nil {
|
||
logs.Error("get merchant len by params fail: ", err)
|
||
}
|
||
return int(cnt)
|
||
}
|
||
|
||
func GetMerchantByUid(merchantUid string) MerchantInfo {
|
||
o := orm.NewOrm()
|
||
var merchantInfo MerchantInfo
|
||
_, err := o.QueryTable(MERCHANT_INFO).Filter("merchant_uid", merchantUid).Limit(1).All(&merchantInfo)
|
||
if err != nil {
|
||
logs.Error("get merchant info fail: ", err)
|
||
}
|
||
return merchantInfo
|
||
}
|
||
|
||
func GetMerchantByPaykey(payKey string) MerchantInfo {
|
||
o := orm.NewOrm()
|
||
var merchantInfo MerchantInfo
|
||
_, err := o.QueryTable(MERCHANT_INFO).Filter("merchant_key", payKey).Limit(1).All(&merchantInfo)
|
||
if err != nil {
|
||
logs.Error("get merchant by merchantKey fail: ", err)
|
||
}
|
||
return merchantInfo
|
||
}
|
||
|
||
func UpdateMerchant(merchantInfo MerchantInfo) bool {
|
||
o := orm.NewOrm()
|
||
_, err := o.Update(&merchantInfo)
|
||
|
||
if err != nil {
|
||
logs.Error("update merchant fail: ", err)
|
||
return false
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
func DeleteMerchantByUid(merchantUid string) bool {
|
||
o := orm.NewOrm()
|
||
_, err := o.QueryTable(MERCHANT_INFO).Filter("merchant_uid", merchantUid).Delete()
|
||
if err != nil {
|
||
logs.Error("delete merchant fail: ", err)
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
func UpdateOtpByUserID(merchantUid, otpSecret string) error {
|
||
o := orm.NewOrm()
|
||
_, err := o.QueryTable(MERCHANT_INFO).
|
||
Filter("merchant_uid", merchantUid).
|
||
Update(orm.Params{"otp_secret": otpSecret})
|
||
if err != nil {
|
||
logs.Error("更新totp失败", err)
|
||
}
|
||
return err
|
||
}
|