155 lines
3.6 KiB
Go
155 lines
3.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"gateway/internal/otelTrace"
|
|
|
|
"time"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type AgentInfo struct {
|
|
Id int
|
|
Status string
|
|
AgentName string
|
|
AgentPassword string
|
|
PayPassword string
|
|
AgentRemark string
|
|
AgentUid string
|
|
AgentPhone string
|
|
CreateTime time.Time
|
|
UpdateTime time.Time
|
|
}
|
|
|
|
const AGENT_INFO = "agent_info"
|
|
|
|
func IsEixstByAgentName(agentName string) bool {
|
|
o := orm.NewOrm()
|
|
exist := o.QueryTable(AGENT_INFO).Filter("agent_name", agentName).Exist()
|
|
|
|
return exist
|
|
}
|
|
|
|
func IsExistByAgentUid(uid string) bool {
|
|
o := orm.NewOrm()
|
|
exist := o.QueryTable(AGENT_INFO).Filter("agent_uid", uid).Exist()
|
|
|
|
return exist
|
|
}
|
|
|
|
func IsEixstByAgentPhone(agentPhone string) bool {
|
|
o := orm.NewOrm()
|
|
exist := o.QueryTable(AGENT_INFO).Filter("agent_phone", agentPhone).Exist()
|
|
return exist
|
|
}
|
|
|
|
func InsertAgentInfo(ctx context.Context, agentInfo AgentInfo) bool {
|
|
o := orm.NewOrm()
|
|
_, err := o.Insert(&agentInfo)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("insert agent info fail: ", zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func GetAgentInfoByAgentUid(ctx context.Context, agentUid string) AgentInfo {
|
|
o := orm.NewOrm()
|
|
var agentInfo AgentInfo
|
|
_, err := o.QueryTable(AGENT_INFO).Filter("agent_uid", agentUid).Limit(1).All(&agentInfo)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("get agent info by agentUid fail: ", zap.Error(err))
|
|
}
|
|
|
|
return agentInfo
|
|
}
|
|
|
|
func GetAgentInfoByPhone(ctx context.Context, phone string) AgentInfo {
|
|
o := orm.NewOrm()
|
|
var agentInfo AgentInfo
|
|
_, err := o.QueryTable(AGENT_INFO).Filter("agent_phone", phone).Limit(1).All(&agentInfo)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("get agent info by phone fail: ", zap.Error(err))
|
|
}
|
|
|
|
return agentInfo
|
|
}
|
|
|
|
func GetAgentInfoLenByMap(ctx context.Context, params map[string]string) int {
|
|
o := orm.NewOrm()
|
|
qs := o.QueryTable(AGENT_INFO)
|
|
for k, v := range params {
|
|
if len(v) > 0 {
|
|
qs = qs.Filter(k, v)
|
|
}
|
|
}
|
|
cnt, err := qs.Limit(-1).Count()
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("get agentinfo len by map fail: ", zap.Error(err))
|
|
}
|
|
|
|
return int(cnt)
|
|
}
|
|
|
|
func GetAgentInfoByMap(ctx context.Context, params map[string]string, displayCount, offset int) []AgentInfo {
|
|
o := orm.NewOrm()
|
|
var agentInfoList []AgentInfo
|
|
|
|
qs := o.QueryTable(AGENT_INFO)
|
|
for k, v := range params {
|
|
if len(v) > 0 {
|
|
qs = qs.Filter(k, v)
|
|
}
|
|
}
|
|
|
|
_, err := qs.Limit(displayCount, offset).OrderBy("-update_time").All(&agentInfoList)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("get agentInfo by map fail: ", zap.Error(err))
|
|
}
|
|
|
|
return agentInfoList
|
|
}
|
|
|
|
func GetAllAgentByMap(ctx context.Context, parmas map[string]string) []AgentInfo {
|
|
o := orm.NewOrm()
|
|
var agentList []AgentInfo
|
|
|
|
qs := o.QueryTable(AGENT_INFO)
|
|
for k, v := range parmas {
|
|
if len(v) > 0 {
|
|
qs = qs.Filter(k, v)
|
|
}
|
|
}
|
|
|
|
_, err := qs.Limit(-1).All(&agentList)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("get all agent by map fail: ", zap.Error(err))
|
|
}
|
|
|
|
return agentList
|
|
}
|
|
|
|
func UpdateAgentInfo(ctx context.Context, agentInfo AgentInfo) bool {
|
|
o := orm.NewOrm()
|
|
_, err := o.Update(&agentInfo)
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("update agentinfo fail: ", zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func DeleteAgentByAgentUid(ctx context.Context, agentUid string) bool {
|
|
o := orm.NewOrm()
|
|
_, err := o.QueryTable(AGENT_INFO).Filter("agent_uid", agentUid).Delete()
|
|
if err != nil {
|
|
otelTrace.Logger.WithContext(ctx).Error("delete agent by agent uid fail: ", zap.Error(err))
|
|
return false
|
|
}
|
|
return true
|
|
}
|