finclip-app-manager/infrastructure/db/repo/mongo/binding.go

998 lines
37 KiB
Go
Raw Permalink Normal View History

2023-10-31 14:07:26 +08:00
package mongo
import (
"context"
"finclip-app-manager/domain/entity"
"finclip-app-manager/domain/entity/proto/apiproto"
"finclip-app-manager/domain/repository"
"strings"
"time"
mgo "gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo"
"gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo/bson"
)
var _ repository.IBindingRepo = new(BindingByMongoRepo)
type BindingByMongoRepo struct {
}
func NewBindingByMongoRepo() *BindingByMongoRepo {
return &BindingByMongoRepo{}
}
// 这张表暂时废弃
//type BundleInfoMongo struct {
// BindingId string `json:"bindingId" bson:"binding_id"`
// BundleID string `json:"bundleId" bson:"bundle_id"`
// GroupID string `json:"groupId" bson:"group_id"` //企业ID
// Remark string `json:"remark" bson:"remark"`
// SDKKey string `json:"SDKKey" bson:"sdk_key"`
// SDKID string `json:"SDKID" bson:"sdk_id"`
// IsFirstCreate string `json:"isFirstCreate" bson:"is_first_create"` //是否第一次创建
// CreatedAt int64 `json:"createdAt" bson:"created_at"`
// CreatedAccount string `json:"createdAccount" bson:"created_account"`
// CreatedBy string `json:"createdBy" bson:"created_by"`
//}
// BundleInfoData 驼峰换下划线给BindingMongo使用
type BundleInfoData struct {
BundleID string `json:"bundle_id" bson:"bundle_id"`
Remark string `json:"remark" bson:"remark"`
SDKKey string `json:"sdk_key" bson:"sdk_key"`
SDKID string `json:"sdk_id" bson:"sdk_id"`
IsFirstCreate bool `json:"is_first_create" bson:"is_first_create"` //是否第一次创建
CreatedAt int64 `json:"created_at" bson:"created_at"`
CreatedAccount string `json:"created_account" bson:"created_account"`
CreatedBy string `json:"created_by" bson:"created_by"`
IsForbidden int `json:"is_forbidden" bson:"is_forbidden"`
IsReview int `json:"is_review" bson:"is_review"`
}
type AppInfo struct {
AppID string `json:"appId" bson:"appId"`
AssociatedAt int64 `json:"associatedAt" bson:"associatedAt"`
AssociatedBy string `json:"associatedBy" bson:"associatedBy"`
}
type BindingMongo struct {
BindingID string `json:"bindingId" bson:"binding_id"` //应用的id
Name string `json:"name" bson:"name"` //应用名称
BundleInfos []BundleInfoData `json:"bundleInfos" bson:"bundle_infos"` //bundle ids
CreatedInfoBy string `json:"createdInfoBy" bson:"created_info_by"`
CreatedInfoAt int64 `json:"createdInfoAt" bson:"created_info_at"`
CreatedInfoAccount string `json:"createdInfoAccount" bson:"created_info_account"`
GroupID string `json:"groupId" bson:"group_id"` //企业ID
GroupName string `json:"groupName" bson:"group_name"` //企业名称(为了查询)
CooperateStatusValue string `json:"cooperateStatusValue" bson:"cooperate_status_value"`
CooperateStatusReason string `json:"cooperateStatusReason" bson:"cooperate_status_reason"`
CooperateStatusLastUpdated int64 `json:"cooperateStatusLastUpdated" bson:"cooperate_status_last_updated"`
CooperateStatusModifiedBy string `json:"cooperateStatusModifiedBy" bson:"cooperate_status_modified_by"`
CooperateValidStatusReason string `json:"cooperateValidStatusReason" bson:"cooperate_valid_status_reson"`
CooperateValidStatusLastUpdated int64 `json:"cooperateValidStatusLastUpdated" bson:"cooperate_valid_status_last_updated"`
CooperateValidStatusModifiedBy string `json:"cooperateValidStatusModifiedBy" bson:"cooperate_valid_status_modified_by"`
CooperateInvalidStatusReason string `json:"cooperateInvalidStatusReason" bson:"cooperate_invalid_status_reson"`
CooperateInvalidStatusLastUpdated int64 `json:"cooperateInvalidStatusLastUpdated" bson:"cooperate_invalid_status_last_updated"`
CooperateInvalidStatusModifiedBy string `json:"cooperateInvalidStatusModifiedBy" bson:"cooperate_invalid_status_modified_by"`
AppInfos []AppInfo `json:"appInfos" bson:"app_infos"` //appid 的信息
Owner string `json:"owner" bson:"owner"` //所属企业
Expire int64 `json:"expire" bson:"expire"`
ApiServer string `json:"apiServer" bson:"api_server"` //子域名
PlatForm int `json:"platform" bson:"platform"` //来源平台
AutoBind int `json:"autoBind" bson:"auto_bind"` //自动关联小程序
HiddenBundle int `json:"hiddenBundle" bson:"hidden_bundle"` //隐藏bundle信息
FromBindingID string `json:"fromBindingId" bson:"from_binding_id"` //来源ID
}
func (b BindingByMongoRepo) GetInfo(ctx context.Context, bindingId string) (*entity.Binding, error) {
bindingMongo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"binding_id": bindingId}, &bindingMongo)
if err != nil {
return nil, err
}
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) GetByGroupIdAndName(ctx context.Context, groupId string, name string, isAdmin bool) (*entity.Binding, error) {
bindingMongo := BindingMongo{}
filter := bson.M{}
if isAdmin {
filter["group_id"] = groupId
filter["name"] = name
filter["platform"] = entity.BINGING_PLATFORM_OPER
} else {
filter["group_id"] = groupId
filter["name"] = name
filter["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}
}
err := bindingTable.GetOne(ctx, filter, &bindingMongo)
if err != nil {
return nil, err
}
//bundleInfoMongos := make([]BundleInfoMongo, 0)
//_, err = bindingTable.GetAll(ctx, bson.M{"binding_id": bindingMongo.BindingID}, []string{}, &bundleInfoMongos)
//if err != nil {
// return nil, err
//}
//
//result := convertBindingMongoToBinding(bindingMongo, bundleInfoMongos)
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) GetByApiServer(ctx context.Context, apiServer string) (entity.Binding, error) {
bindingMongo := BindingMongo{}
res := entity.Binding{}
err := bindingTable.GetOne(ctx, bson.M{"api_server": apiServer}, &bindingMongo)
if err != nil {
return res, err
}
//bundleInfoMongos := make([]BundleInfoMongo, 0)
//_, err = bindingTable.GetAll(ctx, bson.M{"binding_id": bindingMongo.BindingID}, []string{}, &bundleInfoMongos)
//if err != nil {
// return nil, err
//}
//
//result := convertBindingMongoToBinding(bindingMongo, bundleInfoMongos)
result := convertBindingMongoToBinding(bindingMongo)
return result, err
}
func (b BindingByMongoRepo) GetInfoByParams(ctx context.Context, sdkKey, organId, appId string) (*entity.Binding, error) {
bindingMongo := BindingMongo{}
filters := []bson.M{}
filters = append(filters, bson.M{"bundle_infos": bson.M{"$elemMatch": bson.M{"sdk_key": sdkKey}}}) //bson.M{"bundleInfos.SDKKey": bson.M{"$in": []string{sdkKey}}}
if appId != "" {
filters = append(filters, bson.M{"app_infos": bson.M{"$elemMatch": bson.M{"appId": appId}}})
}
if organId != "" {
filters = append(filters, bson.M{"group_id": organId})
}
filter := bson.M{"$and": filters}
err := bindingTable.GetOne(ctx, filter, &bindingMongo)
if err != nil {
return nil, err
}
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) GetInfoBySdkKeyOrganId(ctx context.Context, organId, sdkKey string) (*entity.Binding, error) {
bindingMongo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"group_id": organId, "bundle_infos.sdk_key": sdkKey}, &bindingMongo)
if err != nil {
return nil, err
}
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) GetAllSdkKey(ctx context.Context, organId string) ([]string, error) {
bindingMongos := make([]BindingMongo, 0)
_, err := bindingTable.GetAll(ctx, bson.M{"group_id": organId}, []string{}, &bindingMongos)
if err != nil {
return nil, err
}
result := make([]string, 0)
for _, v := range bindingMongos {
//result := make([]string, 0)
//bundleInfoMongos := make([]BundleInfoMongo, 0)
//_, err = bundleInfoTable.GetAll(ctx, bson.M{"binding_id": v.BindingID}, []string{}, bundleInfoMongos)
//if err != nil {
// return nil, err
//}
for _, bundleInfo := range v.BundleInfos {
result = append(result, bundleInfo.SDKKey)
}
}
return result, err
}
func (b BindingByMongoRepo) GetBindListBySdkKey(ctx context.Context, sdkKey string) ([]entity.Binding, error) {
bindingMongos := make([]BindingMongo, 0)
_, err := bindingTable.GetAll(ctx, bson.M{"bundle_infos.sdk_key": sdkKey}, []string{"created_info_at"}, &bindingMongos)
if err != nil {
return nil, err
}
bindings := make([]entity.Binding, 0)
for _, bindingMongo := range bindingMongos {
//bundleInfoMongos := make([]BundleInfoMongo, 0)
//_, err = bundleInfoTable.GetAll(ctx, bson.M{"binding_id": bindingMongo.BindingID}, []string{}, bundleInfoMongos)
//if err != nil {
// return nil, err
//}
//bindings = append(bindings, convertBindingMongoToBinding(bindingMongo, bundleInfoMongos))
bindings = append(bindings, convertBindingMongoToBinding(bindingMongo))
}
return bindings, err
}
func (b BindingByMongoRepo) SdkKeyExi(ctx context.Context, sdkKey string) (bool, error) {
if _, ok := entity.SdkExiCache.Get(sdkKey); ok {
return true, nil
}
count, err := bindingTable.Count(ctx, bson.M{"bundle_infos.sdk_key": sdkKey})
if err != nil {
return false, err
}
if count > 0 {
entity.SdkExiCache.Set(sdkKey, true, -1)
return true, err
}
return false, err
}
func (b BindingByMongoRepo) Insert(ctx context.Context, bind *entity.Binding) error {
err := bindingTable.Insert(ctx, convertBindingToBindingMongo(bind))
if err != nil {
return err
}
//for _, v := range bind.BundleInfos {
// err = bundleInfoTable.Insert(ctx, convertBundleInfoToBundleInfoMongo(bind.BindingID, bind.GroupID, v))
//}
return err
}
func (b BindingByMongoRepo) Count(ctx context.Context) (int, error) {
return bindingTable.Count(ctx, bson.M{})
}
func (b BindingByMongoRepo) GetCountByStatus(ctx context.Context, status string, platform int) (int, error) {
filter := bson.M{}
filter["cooperate_status_value"] = "Valid"
/*switch platform {
case entity.BINGING_PLATFORM_OPER:
filter["platform"] = entity.BINGING_PLATFORM_OPER
filter["fromBindingId"] = ""
case entity.BINGING_PLATFORM_ORGAN:
filter["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}
default:
filter["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_OPER, "fromBindingId": ""}, {"$or": []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}}}
}*/
filter["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_OPER, "group_id": ""}, {"$or": []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}}}
return bindingTable.Count(ctx, filter)
}
func (b BindingByMongoRepo) GetBundleIdNum(ctx context.Context) (int, error) {
//校验bundingId的数量
pipSelect := []bson.M{
{"$match": bson.M{"bundle_infos": bson.M{"$elemMatch": bson.M{"is_forbidden": 0}}}},
{"$unwind": "$bundleInfos"},
{"$group": bson.M{"_id": nil, "total": bson.M{"$sum": 1}}},
}
bundleIdTotal, err := bindingTable.AggregateCount(ctx, pipSelect)
if err != nil && !b.NotFound(err) {
log.Errorf("GetBundleIdNum err:", err.Error())
return 0, err
}
if b.NotFound(err) {
bundleIdTotal = 0
return 0, nil
}
return bundleIdTotal, nil
}
func (b BindingByMongoRepo) GetBundleIdLimitHand(ctx context.Context, groupId string) (int, error) {
//校验bundingId的数量
pipSelect := []bson.M{
//{"$match": bson.M{"group_id": groupId}},
{"$match": bson.M{"group_id": groupId, "bundle_infos": bson.M{"$elemMatch": bson.M{"is_forbidden": 0}}}},
{"$unwind": "$bundle_infos"},
{"$group": bson.M{"_id": nil, "total": bson.M{"$sum": 1}}},
}
bundleIdTotal, err := bindingTable.AggregateCount(ctx, pipSelect)
if err != nil && !b.NotFound(err) {
return 0, err
}
if b.NotFound(err) {
bundleIdTotal = 0
return 0, nil
}
return bundleIdTotal, nil
}
func (b BindingByMongoRepo) GetBundleLimit(ctx context.Context) (int, error) {
//return bundleTable.Count(ctx, bson.M{})
return bundleTable.Count(ctx, bson.M{"is_forbidden": 0})
}
func (b BindingByMongoRepo) GetBindingByBindingId(ctx context.Context, bindingId string) (*entity.Binding, error) {
bindingMongo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"binding_id": bindingId}, &bindingMongo)
if err != nil {
return nil, err
}
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) GetBindingByGroupIdAndBindingId(ctx context.Context, groupId string, bindingId string) (*entity.Binding, error) {
bindingMongo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"group_id": groupId, "binding_id": bindingId}, &bindingMongo)
if err != nil {
return nil, err
}
//bundleInfoMongos := make([]BundleInfoMongo, 0)
//_, err = bindingTable.GetAll(ctx, bson.M{"binding_id": bindingMongo.BindingID}, []string{}, &bundleInfoMongos)
//if err != nil {
// return nil, err
//}
//
//result := convertBindingMongoToBinding(bindingMongo, bundleInfoMongos)
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) GetBindingByGroupIdAndSdkKey(ctx context.Context, groupId string, sdkKey string) (*entity.Binding, error) {
bindingMongo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"group_id": groupId, "bundle_infos.sdk_key": sdkKey}, &bindingMongo)
if err != nil {
return nil, err
}
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) UpdateBundleInfosByGroupIdAndBindId(ctx context.Context, groupId string, bindingId string, infos []entity.BundleInfo) error {
bundleInfoDatas := make([]BundleInfoData, 0)
for _, v := range infos {
bundleInfoData := BundleInfoData{
BundleID: v.BundleID,
Remark: v.Remark,
SDKKey: v.SDKKey,
SDKID: v.SDKID,
IsFirstCreate: v.IsFirstCreate,
CreatedAt: v.CreatedAt,
CreatedAccount: v.CreatedAccount,
CreatedBy: v.CreatedBy,
IsForbidden: v.IsForbidden,
}
bundleInfoDatas = append(bundleInfoDatas, bundleInfoData)
}
return bindingTable.UpdateOne(ctx, bson.M{"group_id": groupId, "binding_id": bindingId}, bson.M{"$set": bson.M{"bundle_infos": bundleInfoDatas}})
}
func (b BindingByMongoRepo) AppendBundles(ctx context.Context, bindingId, groupId string, bundles []entity.BundleInfo) error {
bindingInfo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"binding_id": bindingId}, &bindingInfo)
if err != nil {
return err
}
bundleInfoDatas := make([]BundleInfoData, 0)
bundleInfoDatas = append(bundleInfoDatas, bindingInfo.BundleInfos...)
for _, v := range bundles {
bundleInfoData := BundleInfoData{
BundleID: v.BundleID,
Remark: v.Remark,
SDKKey: v.SDKKey,
SDKID: v.SDKID,
IsFirstCreate: v.IsFirstCreate,
CreatedAt: v.CreatedAt,
CreatedAccount: v.CreatedAccount,
CreatedBy: v.CreatedBy,
IsForbidden: v.IsForbidden,
}
bundleInfoDatas = append(bundleInfoDatas, bundleInfoData)
}
return bindingTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, bson.M{"$set": bson.M{"bundle_infos": bundleInfoDatas}})
}
func (b BindingByMongoRepo) UpdateBundleIsView(ctx context.Context, reviews []apiproto.BundleReviewItem, isReview int) error {
return nil
}
func (b BindingByMongoRepo) ListReviewBundle(ctx context.Context, pageSize int, pageNo int, searchText string, isReview int) (int64, []entity.ReviewBundleInfo, error) {
return 0, nil, nil
}
func (b BindingByMongoRepo) CheckReviewBySdkKey(ctx context.Context, sdkKey string) bool {
return false
}
func (b BindingByMongoRepo) GetReviewBindBySdkKey(ctx context.Context, sdkKey string) (*entity.ReviewBundleInfo, error) {
return nil, nil
}
func (b BindingByMongoRepo) UpdateBundles(ctx context.Context, bindingId, groupId string, bundles []entity.BundleInfo) error {
bundleInfoDatas := make([]BundleInfoData, 0)
for _, v := range bundles {
bundleInfoData := BundleInfoData{
BundleID: v.BundleID,
Remark: v.Remark,
SDKKey: v.SDKKey,
SDKID: v.SDKID,
IsFirstCreate: v.IsFirstCreate,
CreatedAt: v.CreatedAt,
CreatedAccount: v.CreatedAccount,
CreatedBy: v.CreatedBy,
IsForbidden: v.IsForbidden,
IsReview: v.IsReview,
}
bundleInfoDatas = append(bundleInfoDatas, bundleInfoData)
}
return bindingTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, bson.M{"$set": bson.M{"bundle_infos": bundleInfoDatas}})
}
func (b BindingByMongoRepo) GetByBindIdList(ctx context.Context, ids []string) ([]entity.Binding, error) {
bindingMongos := make([]BindingMongo, 0)
_, err := bindingTable.GetAll(ctx, bson.M{"binding_id": bson.M{"$in": ids}}, []string{}, &bindingMongos)
if err != nil {
return nil, err
}
bindings := make([]entity.Binding, 0)
for _, bindingMongo := range bindingMongos {
//bundleInfoMongos := make([]BundleInfoMongo, 0)
//_, err = bundleInfoTable.GetAll(ctx, bson.M{"binding_id": bindingMongo.BindingID}, []string{}, bundleInfoMongos)
//if err != nil {
// return nil, err
//}
//
//bindings = append(bindings, convertBindingMongoToBinding(bindingMongo, bundleInfoMongos))
bindings = append(bindings, convertBindingMongoToBinding(bindingMongo))
}
return bindings, err
}
func (b BindingByMongoRepo) GetBindListByGroupId(ctx context.Context, groupId string, pageSize int, pageNo int) ([]entity.Binding, int, error) {
bindingMongos := make([]BindingMongo, 0)
filter := bson.M{"group_id": groupId}
filter["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}
total, err := bindingTable.GetSome(ctx, filter, []string{"-created_info_at"}, pageSize, pageNo, &bindingMongos)
if err != nil {
return make([]entity.Binding, 0), 0, err
}
bindings := make([]entity.Binding, 0)
for _, bindingMongo := range bindingMongos {
//bundleInfoMongos := make([]BundleInfoMongo, 0)
//_, err = bundleInfoTable.GetAll(ctx, bson.M{"binding_id": bindingMongo.BindingID}, []string{}, bundleInfoMongos)
//if err != nil {
// return make([]entity.Binding, 0), 0, err
//}
//
//bindings = append(bindings, convertBindingMongoToBinding(bindingMongo, bundleInfoMongos))
bindings = append(bindings, convertBindingMongoToBinding(bindingMongo))
}
return bindings, total, err
}
func (b BindingByMongoRepo) UpdateRelatedBindingCooperate(ctx context.Context, bindingId string, status entity.Status, specificStatus entity.SpecificStatus, cooperate bool) error {
if cooperate {
return bindingTable.UpdateAll(ctx, bson.M{"from_binding_id": bindingId, "platform": entity.BINGING_PLATFORM_OPER},
bson.M{"$set": bson.M{"cooperate_status_value": status.Value, "cooperate_status_last_updated": status.LastUpdated, "cooperate_status_modified_by": status.ModifiedBy,
"cooperate_valid_status_last_updated": specificStatus.LastUpdated, "cooperate_valid_status_modified_by": specificStatus.ModifiedBy}})
} else {
return bindingTable.UpdateAll(ctx, bson.M{"from_binding_id": bindingId, "platform": entity.BINGING_PLATFORM_OPER},
bson.M{"$set": bson.M{"cooperate_status_value": status.Value, "cooperate_status_last_updated": status.LastUpdated, "cooperate_status_modified_by": status.ModifiedBy,
"cooperate_invalid_status_last_updated": specificStatus.LastUpdated, "cooperate_invalid_status_modified_by": specificStatus.ModifiedBy}})
}
}
func (b BindingByMongoRepo) AppendApps(ctx context.Context, bindingId string, apps []entity.AppInfo) error {
bindingInfo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"binding_id": bindingId}, &bindingInfo)
if err != nil {
return err
}
appsInfo := make([]AppInfo, 0)
appsInfo = append(appsInfo, bindingInfo.AppInfos...)
for _, v := range apps {
app := AppInfo{
AppID: v.AppID,
AssociatedAt: v.AssociatedAt,
AssociatedBy: v.AssociatedBy,
}
appsInfo = append(appsInfo, app)
}
return bindingTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, bson.M{"$set": bson.M{"app_infos": appsInfo}})
}
func (b BindingByMongoRepo) RemoveApps(ctx context.Context, bindingId string, appIds []string) error {
rmAppsMap := make(map[string]bool)
for _, appId := range appIds {
rmAppsMap[appId] = true
}
bindingInfo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"binding_id": bindingId}, &bindingInfo)
if err != nil {
return err
}
appsInfo := make([]AppInfo, 0)
for _, app := range bindingInfo.AppInfos {
if _, ok := rmAppsMap[app.AppID]; !ok {
appsInfo = append(appsInfo, app)
}
}
return bindingTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, bson.M{"$set": bson.M{"app_infos": appsInfo}})
}
func (b BindingByMongoRepo) UpdateByBindId(ctx context.Context, bindingId string, bind *entity.Binding) error {
err := bindingTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, convertBindingToBindingMongo(bind))
if err != nil {
return err
}
//for _, v := range bind.BundleInfos {
// err = bundleInfoTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, convertBundleInfoToBundleInfoMongo(bind.BindingID, bind.GroupID, v))
//}
return err
}
func (b BindingByMongoRepo) UpdateBindingInfo(ctx context.Context, bindingId string, info map[string]interface{}, platform int) error {
if platform == entity.BINGING_PLATFORM_OPER {
err := bindingTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, bson.M{"$set": info})
if err != nil {
return err
}
return bindingTable.UpdateAll(ctx, bson.M{"from_binding_id": bindingId}, bson.M{"$set": info})
} else {
return bindingTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, bson.M{"$set": info})
}
}
func (b BindingByMongoRepo) UpdateBindingGroupName(ctx context.Context, groupId, groupName string) error {
return nil
}
func (b BindingByMongoRepo) UpdateExpire(ctx context.Context, bindingId string, expire int64) error {
err := bindingTable.UpdateOne(ctx, bson.M{"binding_id": bindingId}, bson.M{"$set": bson.M{"expire": expire}})
if err != nil {
return err
}
return nil
}
func (b BindingByMongoRepo) BundleIdCount(ctx context.Context, groupId string) (int, error) {
var pipSelect []bson.M
if groupId == "" {
pipSelect = []bson.M{
{"$match": bson.M{"bundle_infos": bson.M{"$elemMatch": bson.M{"is_forbidden": 0}}}},
{"$unwind": "$bundleInfos"},
{"$group": bson.M{"_id": nil, "total": bson.M{"$sum": 1}}},
}
} else {
pipSelect = []bson.M{
{"$match": bson.M{"groupId": groupId, "bundle_infos": bson.M{"$elemMatch": bson.M{"is_forbidden": 0}}}},
{"$unwind": "$bundleInfos"},
{"$group": bson.M{"_id": nil, "total": bson.M{"$sum": 1}}},
}
}
bundleIdTotal, err := bindingTable.AggregateCount(ctx, pipSelect)
if err != nil && !b.NotFound(err) {
log.Errorf("updateLicenseValid get bundle id count err:%s", err.Error())
return 0, err
}
if b.NotFound(err) {
bundleIdTotal = 0
return 0, nil
}
return bundleIdTotal, nil
}
func (b BindingByMongoRepo) GetBundleByGroupIdAndBundleId(ctx context.Context, groupId string, bundleId string) (*entity.Binding, error) {
//bundleInfoMongo := BundleInfoMongo{}
//err := bundleInfoTable.GetOne(ctx, bson.M{"group_id": groupId, "bundle_id": bundleId}, &bundleInfoMongo)
//if err != nil {
// return nil, err
//}
//
//return convertBundleInfoMongoToBundleInfo(bundleInfoMongo), err
bindingMongo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"group_id": groupId, "bundle_infos.bundle_id": bson.M{"$in": []string{bundleId}}}, &bindingMongo)
if err != nil {
return nil, err
}
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) GetbundlesByBundleId(ctx context.Context, bundleId string) (*entity.Binding, error) {
//bundleInfoMongo := BundleInfoMongo{}
//err := bundleInfoTable.GetOne(ctx, bson.M{"bundle_id": bundleId}, &bundleInfoMongo)
//if err != nil {
// return nil, err
//}
//
//return convertBundleInfoMongoToBundleInfo(bundleInfoMongo), err
bindingMongo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"bundle_infos.bundle_id": bson.M{"$in": []string{bundleId}}}, &bindingMongo)
if err != nil {
return nil, err
}
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) GetBindingsBySearch(ctx context.Context, pageSize int, pageNo int, sort string, searchText string, searchFields string, cooperateStatus string, platform int) ([]entity.Binding, int, error) {
bindingMongos := make([]BindingMongo, 0)
sortColumn := make([]string, 0)
switch sort {
case "-created":
sortColumn = append(sortColumn, "-created_info_at") //按创建时间排序
case "-expire":
sortColumn = append(sortColumn, "-expire") //按过期时间倒序排序
case "expire":
sortColumn = append(sortColumn, "expire") //按过期时间正序排序
default:
sortColumn = append(sortColumn, "-created_info_at") //按创建时间倒序排序
}
filter := []bson.M{}
if searchText != "" && searchFields != "" {
sf := bson.M{}
r := bson.RegEx{Pattern: searchText, Options: "i"}
searchFilter := make([]bson.M, 0)
for _, v := range strings.Split(searchFields, ",") {
searchFilter = append(searchFilter, bson.M{v: r})
}
sf["$or"] = searchFilter
filter = append(filter, sf)
}
if cooperateStatus != "all" {
q := bson.M{"cooperateStatus.value": cooperateStatus}
filter = append(filter, q)
}
switch platform {
case entity.BINGING_PLATFORM_OPER:
filter = append(filter, bson.M{"platform": entity.BINGING_PLATFORM_OPER, "group_id": ""})
case entity.BINGING_PLATFORM_ORGAN:
q := bson.M{}
q["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}
filter = append(filter, q)
default:
q := bson.M{}
q["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_OPER, "group_id": ""}, {"$or": []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}}}
filter = append(filter, q)
}
total, err := bindingTable.GetSome(ctx, bson.M{"$and": filter}, sortColumn, pageSize, pageNo, &bindingMongos)
bindings := make([]entity.Binding, 0)
for _, bindingMongo := range bindingMongos {
bindings = append(bindings, convertBindingMongoToBinding(bindingMongo))
}
return bindings, total, err
}
func (b BindingByMongoRepo) GetBindingsByAppId(ctx context.Context, pageSize int, pageNo int, appId string) ([]entity.Binding, int, error) {
bindingMongos := make([]BindingMongo, 0)
filter := bson.M{"app_infos.appId": appId}
total, err := bindingTable.GetSome(ctx, filter, []string{"-created_info_at"}, pageSize, pageNo, &bindingMongos)
bindings := make([]entity.Binding, 0)
for _, bindingMongo := range bindingMongos {
bindings = append(bindings, convertBindingMongoToBinding(bindingMongo))
}
return bindings, total, err
}
func (b BindingByMongoRepo) GetDevListBinding(ctx context.Context, pageSize int, pageNo int, sort string, searchText string, pullType string, groupId string, bindStatus string, platform int) ([]entity.Binding, int, error) {
bindingMongos := make([]BindingMongo, 0)
sortColumn := make([]string, 0)
switch sort {
case "-created":
sortColumn = append(sortColumn, "-created_info_at") //按创建时间排序
case "-expire":
sortColumn = append(sortColumn, "-expire") //按过期时间倒序排序
case "expire":
sortColumn = append(sortColumn, "expire") //按过期时间正序排序
default:
sortColumn = append(sortColumn, "-created_info_at") //按创建时间倒序排序
}
filter := []bson.M{}
filter = append(filter, bson.M{"group_id": groupId})
switch pullType {
case "recently-expire":
q := bson.M{}
beginData := time.Now().UnixNano() / 1e6
endData := time.Now().AddDate(0, 0, 30).UnixNano() / 1e6
q["expire"] = bson.M{"$gte": beginData, "$lte": endData}
filter = append(filter, q)
}
switch bindStatus {
case "Valid":
filter = append(filter, bson.M{"cooperate_status_value": "Valid"})
case "Invalid":
filter = append(filter, bson.M{"cooperate_status_value": "Invalid"})
}
if searchText != "" {
q := bson.M{}
r := bson.RegEx{Pattern: searchText, Options: "i"}
q["$or"] = []bson.M{{"name": bson.M{"$regex": r}}}
filter = append(filter, q)
}
switch platform {
case entity.BINGING_PLATFORM_OPER:
filter = append(filter, bson.M{"platform": entity.BINGING_PLATFORM_OPER})
case entity.BINGING_PLATFORM_ORGAN:
q := bson.M{}
q["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}
filter = append(filter, q)
default:
q := bson.M{}
q["$or"] = []bson.M{{"platform": entity.BINGING_PLATFORM_OPER}, {"$or": []bson.M{{"platform": entity.BINGING_PLATFORM_ORGAN}, {"platform": bson.M{"$exists": false}}}}}
filter = append(filter, q)
}
total, err := bindingTable.GetSome(ctx, bson.M{"$and": filter}, sortColumn, pageSize, pageNo, &bindingMongos)
bindings := make([]entity.Binding, 0)
for _, bindingMongo := range bindingMongos {
bindings = append(bindings, convertBindingMongoToBinding(bindingMongo))
}
return bindings, total, err
}
func (b BindingByMongoRepo) GetBindingBySdkKey(ctx context.Context, sdkKey string) (*entity.Binding, error) {
bindingMongo := BindingMongo{}
err := bindingTable.GetOne(ctx, bson.M{"bundle_infos.sdk_key": sdkKey}, &bindingMongo)
if err != nil {
return nil, err
}
result := convertBindingMongoToBinding(bindingMongo)
return &result, err
}
func (b BindingByMongoRepo) ListAutoBindAppBinding(ctx context.Context, groupId string) ([]string, error) {
bindingMongos := make([]BindingMongo, 0)
_, err := bindingTable.GetAll(ctx, bson.M{"auto_bind": 1, "platform": entity.BINGING_PLATFORM_OPER, "group_id": groupId}, []string{}, &bindingMongos)
if err != nil {
return nil, err
}
result := []string{}
for _, binding := range bindingMongos {
result = append(result, binding.BindingID)
}
return result, nil
}
func (b BindingByMongoRepo) ListCopyOperBinding(ctx context.Context, bindingId string) ([]string, []entity.Binding, error) {
bindingMongos := make([]BindingMongo, 0)
_, err := bindingTable.GetAll(ctx, bson.M{"platform": entity.BINGING_PLATFORM_OPER, "from_binding_id": bindingId}, []string{}, &bindingMongos)
if err != nil {
return nil, nil, err
}
result := []string{}
bindings := make([]entity.Binding, 0)
for _, binding := range bindingMongos {
result = append(result, binding.BindingID)
bindings = append(bindings, convertBindingMongoToBinding(binding))
}
return result, bindings, nil
}
func (b BindingByMongoRepo) ListBindings(ctx context.Context, groupId string, searchText string, pageNo int, pageSize int) ([]entity.Binding, int, error) {
bindingMongos := make([]BindingMongo, 0)
sortColumn := make([]string, 0)
sortColumn = append(sortColumn, "-created_info_at") //按创建时间排序
filter := bson.M{}
if groupId != "" {
filter["group_id"] = groupId
}
if searchText != "" {
r := bson.RegEx{Pattern: searchText, Options: "i"}
filter["$or"] = []bson.M{{"name": r}, {"group_name": r}, {"bundle_infos.bundle_id": r}}
}
total, err := bindingTable.GetSome(ctx, filter, sortColumn, pageSize, pageNo, &bindingMongos)
bindings := make([]entity.Binding, 0)
for _, bindingMongo := range bindingMongos {
bindings = append(bindings, convertBindingMongoToBinding(bindingMongo))
}
return bindings, total, err
}
func (b BindingByMongoRepo) GetAllAssBinds(ctx context.Context, appId string) ([]entity.Binding, error) {
bindings := make([]BindingMongo, 0)
err := bindingTable.OnlyGetAll(ctx, bson.M{"app_infos.appId": appId}, []string{}, &bindings)
if err != nil {
return nil, err
}
result := make([]entity.Binding, 0)
for _, bindingMongo := range bindings {
result = append(result, convertBindingMongoToBinding(bindingMongo))
}
return result, nil
}
func (b BindingByMongoRepo) NotFound(err error) bool {
return err == mgo.ErrNotFound
}
func (b BindingByMongoRepo) UpdateBundleIdIsForbidden(ctx context.Context, bundleId string) error {
return bindingTable.UpdateAll(ctx, bson.M{"bundle_infos": bson.M{"$elemMatch": bson.M{"bundle_id": bundleId}}}, bson.M{"$set": bson.M{"is_forbidden": 1}})
}
func (b BindingByMongoRepo) UpdateBundleIdPlatform(ctx context.Context, bundleId, platform string) error {
return bindingTable.UpdateAll(ctx, bson.M{"bundle_infos": bson.M{"$elemMatch": bson.M{"bundle_id": bundleId}}}, bson.M{"$set": bson.M{"remark": platform}})
}
func (b BindingByMongoRepo) UpdateBundleBindingId(ctx context.Context, bundleId, bindingId, toBindingId string) error {
return nil
}
func convertBindingMongoToBinding(bindingMongo BindingMongo) entity.Binding {
result := entity.Binding{}
bundleInfos := make([]entity.BundleInfo, 0)
for _, v := range bindingMongo.BundleInfos {
bundleInfo := entity.BundleInfo{
BundleID: v.BundleID,
Remark: v.Remark,
SDKKey: v.SDKKey,
SDKID: v.SDKID,
IsFirstCreate: v.IsFirstCreate,
CreatedAt: v.CreatedAt,
CreatedAccount: v.CreatedAccount,
CreatedBy: v.CreatedBy,
IsForbidden: v.IsForbidden,
IsReview: v.IsReview,
}
bundleInfos = append(bundleInfos, bundleInfo)
}
appInfos := make([]entity.AppInfo, 0)
for _, v := range bindingMongo.AppInfos {
item := entity.AppInfo{
AppID: v.AppID,
AssociatedAt: v.AssociatedAt,
AssociatedBy: v.AssociatedBy,
}
appInfos = append(appInfos, item)
}
createInfo := entity.CreatedInfo{
CreatedBy: bindingMongo.CreatedInfoBy,
CreatedAt: bindingMongo.CreatedInfoAt,
}
cooperateStatus := entity.Status{
Value: bindingMongo.CooperateStatusValue,
Reason: bindingMongo.CooperateStatusReason,
LastUpdated: bindingMongo.CooperateStatusLastUpdated,
ModifiedBy: bindingMongo.CooperateStatusModifiedBy,
}
cooperateValidStatus := entity.SpecificStatus{
Reason: bindingMongo.CooperateValidStatusReason,
LastUpdated: bindingMongo.CooperateValidStatusLastUpdated,
ModifiedBy: bindingMongo.CooperateValidStatusModifiedBy,
}
cooperateInvalidStatus := entity.SpecificStatus{
Reason: bindingMongo.CooperateInvalidStatusReason,
LastUpdated: bindingMongo.CooperateInvalidStatusLastUpdated,
ModifiedBy: bindingMongo.CooperateInvalidStatusModifiedBy,
}
//var appInfos []entity.AppInfo
//json.Unmarshal([]byte(bindingMongo.AppInfos), &appInfos)
result.BindingID = bindingMongo.BindingID
result.Name = bindingMongo.Name
result.BundleInfos = bundleInfos
result.CreatedInfo = createInfo
result.GroupID = bindingMongo.GroupID
result.GroupName = bindingMongo.GroupName
result.CooperateStatus = cooperateStatus
result.CooperateValidStatus = cooperateValidStatus
result.CooperateInvalidStatus = cooperateInvalidStatus
result.AppInfos = appInfos
result.Owner = bindingMongo.Owner
result.Expire = bindingMongo.Expire
result.ApiServer = bindingMongo.ApiServer
result.PlatForm = bindingMongo.PlatForm
result.AutoBind = bindingMongo.AutoBind
result.HiddenBundle = bindingMongo.HiddenBundle
result.FromBindingID = bindingMongo.FromBindingID
return result
}
func convertBindingToBindingMongo(binding *entity.Binding) BindingMongo {
result := BindingMongo{}
bundleInfos := make([]BundleInfoData, 0)
for _, v := range binding.BundleInfos {
bundleInfo := BundleInfoData{
BundleID: v.BundleID,
Remark: v.Remark,
SDKKey: v.SDKKey,
SDKID: v.SDKID,
IsFirstCreate: v.IsFirstCreate,
CreatedAt: v.CreatedAt,
CreatedAccount: v.CreatedAccount,
CreatedBy: v.CreatedBy,
IsForbidden: v.IsForbidden,
IsReview: v.IsReview,
}
bundleInfos = append(bundleInfos, bundleInfo)
}
result.BindingID = binding.BindingID
result.Name = binding.Name
result.BundleInfos = bundleInfos
result.CreatedInfoBy = binding.CreatedInfo.CreatedBy
result.CreatedInfoAt = binding.CreatedInfo.CreatedAt
result.CreatedInfoAccount = binding.CreatedInfo.CreatedAccount
result.GroupID = binding.GroupID
result.GroupName = binding.GroupName
result.CooperateStatusValue = binding.CooperateStatus.Value
result.CooperateStatusReason = binding.CooperateStatus.Reason
result.CooperateStatusLastUpdated = binding.CooperateStatus.LastUpdated
result.CooperateStatusModifiedBy = binding.CooperateStatus.ModifiedBy
result.CooperateValidStatusReason = binding.CooperateValidStatus.Reason
result.CooperateValidStatusLastUpdated = binding.CooperateValidStatus.LastUpdated
result.CooperateValidStatusModifiedBy = binding.CooperateValidStatus.ModifiedBy
result.CooperateInvalidStatusReason = binding.CooperateInvalidStatus.Reason
result.CooperateInvalidStatusLastUpdated = binding.CooperateInvalidStatus.LastUpdated
result.CooperateInvalidStatusModifiedBy = binding.CooperateInvalidStatus.ModifiedBy
appInfos := make([]AppInfo, 0)
for _, v := range binding.AppInfos {
item := AppInfo{
AppID: v.AppID,
AssociatedAt: v.AssociatedAt,
AssociatedBy: v.AssociatedBy,
}
appInfos = append(appInfos, item)
}
//result.AppInfos = utility.InterfaceToJsonString(binding.AppInfos)
result.AppInfos = appInfos
result.Owner = binding.Owner
result.Expire = binding.Expire
result.ApiServer = binding.ApiServer
result.PlatForm = binding.PlatForm
result.AutoBind = binding.AutoBind
result.HiddenBundle = binding.HiddenBundle
result.FromBindingID = binding.FromBindingID
return result
}