Initial commit
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
package domainser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"stat/api/proto"
|
||||
"stat/common/stdLog"
|
||||
"stat/common/utils"
|
||||
"stat/models/appInfoMod"
|
||||
"stat/models/domainmod"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// 获取DomainInfo列表
|
||||
func GetDomainInfoList(ctx context.Context, param proto.ListDomainInfoReq) (code stdLog.CodeType, data interface{}, err error) {
|
||||
resp := proto.ListDomainInfoRes{
|
||||
Total: 0,
|
||||
List: make(domainmod.DomainList, 0),
|
||||
}
|
||||
|
||||
skip, limit := utils.ToDBPage(param.PageNum, param.PageSize)
|
||||
|
||||
filter := bson.M{}
|
||||
if param.Name != nil {
|
||||
filter["name"] = bson.M{"$regex": fmt.Sprintf("^%s", *param.Name)}
|
||||
}
|
||||
if param.AppId != nil {
|
||||
filter["appId"] = *param.AppId
|
||||
}
|
||||
if param.Enable != nil {
|
||||
filter["enable"] = *param.Enable
|
||||
}
|
||||
if param.Status != nil {
|
||||
filter["status"] = *param.Status
|
||||
}
|
||||
if param.Category != nil {
|
||||
filter["category"] = *param.Category
|
||||
}
|
||||
if param.Check != nil {
|
||||
filter["checks"] = bson.M{"$in": *param.Check}
|
||||
}
|
||||
opts := options.Find().SetSkip(skip).SetLimit(limit)
|
||||
count, err := domainmod.CountDomains(ctx, filter)
|
||||
if err != nil {
|
||||
code = stdLog.Failure
|
||||
return
|
||||
}
|
||||
aList, err := domainmod.FindManyDomain(ctx, filter, opts)
|
||||
if err != nil {
|
||||
code = stdLog.Failure
|
||||
return
|
||||
}
|
||||
appMap, _ := appInfoMod.GetAllAppIdNameMap(ctx)
|
||||
for _, item := range aList {
|
||||
item.AppName = appMap[item.AppId]
|
||||
}
|
||||
|
||||
resp.Total = count
|
||||
resp.List = aList
|
||||
|
||||
data = resp
|
||||
code = stdLog.Success
|
||||
return
|
||||
}
|
||||
|
||||
// 添加DomainInfo
|
||||
func AddDomainInfo(ctx context.Context, param proto.AddDomainInfoReq) (code stdLog.CodeType, data interface{}, err error) {
|
||||
name, err := ParseDomain(param.Name)
|
||||
if err != nil {
|
||||
code = stdLog.ParamError
|
||||
return
|
||||
}
|
||||
param.Name = name
|
||||
now := time.Now()
|
||||
param.UpdatedAt = now
|
||||
param.CreatedAt = now
|
||||
|
||||
if param.CycleHour < 1 {
|
||||
param.CycleHour = 1
|
||||
}
|
||||
|
||||
if err = domainmod.InsertOneDomain(ctx, ¶m.Domain); err != nil {
|
||||
code = stdLog.Failure
|
||||
return
|
||||
}
|
||||
|
||||
code = stdLog.Success
|
||||
return
|
||||
}
|
||||
|
||||
// 更新DomainInfo
|
||||
func UpdateDomainInfo(ctx context.Context, param proto.UpdateDomainInfoReq) (code stdLog.CodeType,
|
||||
data interface{}, err error) {
|
||||
now := time.Now()
|
||||
param.Update.UpdatedAt = &now
|
||||
if param.Update.Name != nil {
|
||||
var name string
|
||||
name, err = ParseDomain(*param.Update.Name)
|
||||
if err != nil {
|
||||
code = stdLog.ParamError
|
||||
return
|
||||
}
|
||||
param.Update.Name = &name
|
||||
}
|
||||
if param.Update.CycleHour != nil {
|
||||
if *param.Update.CycleHour < 1 {
|
||||
hour := int64(1)
|
||||
param.Update.CycleHour = &hour
|
||||
}
|
||||
}
|
||||
|
||||
if err = domainmod.UpdateOneDomain(ctx, bson.M{"_id": param.Id}, bson.M{"$set": param.Update}); err != nil {
|
||||
code = stdLog.Failure
|
||||
return
|
||||
}
|
||||
|
||||
code = stdLog.Success
|
||||
return
|
||||
}
|
||||
|
||||
// 删除DomainInfo
|
||||
func DelDomainInfo(ctx context.Context, param proto.DelDomainInfoReq) (code stdLog.CodeType, data interface{}, err error) {
|
||||
if _, err = domainmod.DelOneDomain(ctx, bson.M{"_id": param.Id}); err != nil {
|
||||
code = stdLog.Failure
|
||||
return
|
||||
}
|
||||
|
||||
code = stdLog.Success
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user