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

1056 lines
32 KiB
Go
Raw 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/infrastructure/db/entity/mongo"
"finclip-app-manager/infrastructure/utility"
"finclip-app-manager/infrastructure/utils"
"time"
"gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo/bson"
)
type AppRepo struct {
}
func NewAppRepo() *AppRepo {
return &AppRepo{}
}
func (ar *AppRepo) AppCount(ctx context.Context, organId string) (int, error) {
match := bson.M{"isForbidden": 0}
if organId != "" {
match["organId"] = organId
}
return appTable.Count(ctx, match)
}
func (ar *AppRepo) InsertApp(ctx context.Context, info entity.App) error {
mgoInfo := tr.CovertAppToMgo(&info)
return appTable.Insert(ctx, mgoInfo)
}
func (ar *AppRepo) InsertAppVer(ctx context.Context, info entity.AppVersion) error {
mgoInfo := tr.CovertAppVerToMgo(&info)
return appVerTable.Insert(ctx, mgoInfo)
}
func (ar *AppRepo) GetAppInfo(ctx context.Context, appId string) (entity.App, error) {
mgoInfo := mongo.App{}
err := appTable.GetOne(ctx, bson.M{"appId": appId}, &mgoInfo)
if err != nil {
if NotFound(err) {
return entity.App{}, entity.NotFoundErr
}
log.Errorf("GetAppInfo err:%s", err.Error())
return entity.App{}, err
}
return *tr.CovertAppToEntity(&mgoInfo), nil
}
func (ar *AppRepo) UpdateApp(ctx context.Context, info entity.App) error {
mgoInfo := tr.CovertAppToMgo(&info)
return appTable.UpSert(bson.M{"appId": info.AppID}, mgoInfo)
}
func (ar *AppRepo) GetAppVerInfo(ctx context.Context, appId string, seq int) (entity.AppVersion, error) {
mgoInfo := mongo.AppVersion{}
err := appVerTable.GetOne(ctx, bson.M{"appId": appId, "sequence": seq}, &mgoInfo)
if err != nil {
if NotFound(err) {
return entity.AppVersion{}, entity.NotFoundErr
}
log.Errorf("GetAppInfo err:%s", err.Error())
return entity.AppVersion{}, err
}
return *tr.CovertAppVerToEntity(&mgoInfo), nil
}
func (ar *AppRepo) GetOnlineAppVer(ctx context.Context, appId string) (entity.AppVersion, error) {
mgoInfo := mongo.AppVersion{}
match := bson.M{"appId": appId, "status.value": entity.StPublished}
err := appVerTable.GetSortOne(ctx, match, []string{"-sequence"}, &mgoInfo)
if err != nil {
if NotFound(err) {
return entity.AppVersion{}, entity.NotFoundErr
}
return entity.AppVersion{}, err
}
return *tr.CovertAppVerToEntity(&mgoInfo), nil
}
func (ar *AppRepo) SubmitApp(ctx context.Context, req entity.SubmitAppReq, expire int64, userId string) error {
nowMaxSeq := 0
maxAppVerInfo, err := ar.GetMaxSeqAppVer(ctx, req.AppId)
if err == nil {
nowMaxSeq = maxAppVerInfo.Sequence
} else {
if err != entity.NotFoundErr {
return err
}
}
buildInfo, err := NewAppBuildInfoMongoRepo().GetInfoById(ctx, req.BuildId)
if NotFound(err) {
buildInfo, err = NewAppBuildInfoMongoRepo().GetInfoByBuildId(ctx, req.BuildId)
if err != nil {
return err
}
}
log.Debugln("SubmitApp buildInfo:%+v", buildInfo)
appInfo, err := ar.GetAppInfo(ctx, req.AppId)
if err != nil {
return err
}
now := utils.GetNowMs()
appVerInfo := mongo.AppVersion{
AppID: appInfo.AppID,
Name: appInfo.Name,
AppClass: appInfo.AppClass,
AppTag: appInfo.AppTag,
AppType: entity.AppTypeApplet,
Status: mongo.Status{
Reason: "",
Value: entity.StPublishing,
LastUpdated: now,
ModifiedBy: req.Account,
},
PublishingStatus: mongo.SpecificStatus{
Reason: "",
LastUpdated: now,
ModifiedBy: req.Account,
},
UnpublishingStatus: mongo.SpecificStatus{},
PublishingApprovalStatus: mongo.SpecificStatus{},
UnpublishingApprovalStatus: mongo.SpecificStatus{},
PublishedStatus: mongo.SpecificStatus{},
UnpublishedStatus: mongo.UnpublishedStatus{},
RequestStatus: mongo.SpecificStatus{},
ApprovalStatus: mongo.SpecificStatus{},
ActionStatus: mongo.SpecificStatus{
Reason: "",
LastUpdated: now,
ModifiedBy: req.Account,
},
DeveloperID: userId,
GroupID: appInfo.GroupID,
Created: now,
CreatedBy: req.Account,
CustomData: tr.CovertCustomDataToMgo(buildInfo.CustomData),
Version: buildInfo.Version,
Sequence: nowMaxSeq + 1,
CoreDescription: appInfo.CoreDescription,
Logo: appInfo.Logo,
TestInfo: tr.CovertTestInfoToMgo(req.TestInfo),
NeedAutoPub: req.NeedAutoPub,
InGrayRelease: false,
Expire: expire,
AppBuildID: req.BuildId,
}
appVerInfo.CustomData.DetailDescription = appInfo.CustomData.DetailDescription
return appVerTable.Insert(ctx, appVerInfo)
}
func (ar *AppRepo) WithdrawPubApp(ctx context.Context, appId string, seq int, account string) error {
params := map[string]interface{}{}
now := utils.GetNowMs()
params["status"] = mongo.Status{
Value: entity.StPublishWithdrawed,
LastUpdated: now,
ModifiedBy: account,
}
filter := bson.M{
"appId": appId,
"sequence": seq,
}
return appVerTable.UpdateOne(ctx, filter, bson.M{"$set": params})
}
func (ar *AppRepo) GetMaxSeqAppVer(ctx context.Context, appId string) (entity.AppVersion, error) {
mgoInfo := mongo.AppVersion{}
match := bson.M{"appId": appId}
err := appVerTable.GetSortOne(ctx, match, []string{"-sequence"}, &mgoInfo)
if err != nil {
if NotFound(err) {
return entity.AppVersion{}, entity.NotFoundErr
}
return entity.AppVersion{}, err
}
return *tr.CovertAppVerToEntity(&mgoInfo), nil
}
func (ar *AppRepo) GetLatestPubAppVer(ctx context.Context, appId string) (entity.AppVersion, error) {
mgoInfo := mongo.AppVersion{}
match := bson.M{"appId": appId, "publishedStatus.lastUpdated": bson.M{"$ne": 0}}
err := appVerTable.GetSortOne(ctx, match, []string{"-publishedStatus.lastUpdated"}, &mgoInfo)
if err != nil {
if NotFound(err) {
return entity.AppVersion{}, entity.NotFoundErr
}
return entity.AppVersion{}, err
}
return *tr.CovertAppVerToEntity(&mgoInfo), nil
}
func (ar *AppRepo) UpdateAppVerAppName(ctx context.Context, appId, appName string) error {
return nil
}
func (ar *AppRepo) GetNowPubAppVer(ctx context.Context, appId string) (entity.AppVersion, error) {
mgoInfo := mongo.AppVersion{}
match := bson.M{"appId": appId, "status.value": entity.StPublished}
err := appVerTable.GetSortOne(ctx, match, []string{"-status.lastUpdated"}, &mgoInfo)
if err != nil {
if NotFound(err) {
return entity.AppVersion{}, entity.NotFoundErr
}
return entity.AppVersion{}, err
}
return *tr.CovertAppVerToEntity(&mgoInfo), nil
}
func (ar *AppRepo) GetLatestReviewAppVer(ctx context.Context, appId string) (entity.AppVersion, error) {
mgoInfo := mongo.AppVersion{}
//match := bson.M{"appId": appId, "status.value": bson.M{"$in": []string{entity.StPublishing, entity.StPublishWithdrawed, entity.StPublishApproved, entity.StPublishRejected, entity.StPublished, entity.StUnpublished}}}
match := bson.M{"appId": appId, "publishingStatus.lastUpdated": bson.M{"$ne": 0}}
err := appVerTable.GetSortOne(ctx, match, []string{"-sequence"}, &mgoInfo)
if err != nil {
if NotFound(err) {
return entity.AppVersion{}, entity.NotFoundErr
}
return entity.AppVersion{}, err
}
return *tr.CovertAppVerToEntity(&mgoInfo), nil
}
func (ar *AppRepo) GetAllPermitGrayPubVers(ctx context.Context, appId string, maxSeq int) ([]entity.AppVersion, error) {
filter := bson.M{"appId": appId, "status.value": entity.StPublishApproved, "sequence": bson.M{"$gt": maxSeq}}
list := make([]mongo.AppVersion, 0)
_, err := appVerTable.GetAll(ctx, filter, []string{"-sequence"}, &list)
if err != nil {
return nil, err
}
result := make([]entity.AppVersion, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return result, nil
}
func (ar *AppRepo) PubApp(ctx context.Context, appId string, seq int, account string, isDev, isRollback bool) error {
//结束所有低版本的灰度
err := appVerTable.UpdateAll(ctx, bson.M{"appId": appId, "sequence": bson.M{"$lte": seq}}, bson.M{"$set": bson.M{"inGrayRelease": false}})
if err != nil {
return err
}
now := time.Now().UnixNano() / 1e6
//下架之前上架的小程序版本
oldAppVer, err := ar.GetNowPubAppVer(ctx, appId)
if err != nil && !NotFound(err) {
return err
}
if !NotFound(err) {
unpublishedStatus := mongo.UnpublishedStatus{
LastUpdated: now,
ModifiedBy: account,
Type: entity.TypeUnpublishedDueToNewSeq, // Mark the type.
}
//if !isDev {
// unpublishedStatus.Type = entity.TypeUnpublishedByAdmin
//}
unPubUpInfo := map[string]interface{}{
"unpublishedStatus": unpublishedStatus,
"status": mongo.Status{
Value: entity.StUnpublished,
LastUpdated: now,
ModifiedBy: account,
},
}
err = appVerTable.UpdateOne(ctx, bson.M{"appId": oldAppVer.AppID, "sequence": oldAppVer.Sequence}, bson.M{"$set": unPubUpInfo})
if err != nil {
return err
}
}
//上架当前小程序版本
upInfo := make(map[string]interface{})
upInfo["status"] = mongo.Status{
Value: entity.StPublished,
Reason: "",
LastUpdated: now,
ModifiedBy: account,
}
upInfo["publishedStatus"] = mongo.SpecificStatus{
Reason: "",
LastUpdated: now,
ModifiedBy: account,
}
upInfo["actionStatus"] = mongo.SpecificStatus{
LastUpdated: now,
ModifiedBy: account,
}
//修改小程序元数据信息
err = appTable.UpdateOne(ctx, bson.M{"appId": appId}, bson.M{"$set": upInfo})
if err != nil {
return err
}
upInfo["isRollback"] = isRollback
err = appVerTable.UpdateOne(ctx, bson.M{"appId": appId, "sequence": seq}, bson.M{"$set": upInfo})
if err != nil {
return err
}
return nil
}
func (ar *AppRepo) ListApps(ctx context.Context, groupId string, pageNo, pageSize int, searchText string, sortType, pullType string) (int, []entity.App, error) {
list := make([]mongo.App, 0)
sortList := make([]string, 0)
switch sortType {
case "-created":
sortList = append(sortList, "-created") //按创建时间倒序排序
case "created":
sortList = append(sortList, "created") //按创建时间正序排序
case "-expire":
sortList = append(sortList, "-expire") //按过期时间倒序排序
case "expire":
sortList = append(sortList, "expire") //按过期时间正序排序
default:
sortList = append(sortList, "-created")
}
filter := bson.M{}
if groupId != "" {
filter["groupId"] = groupId
}
if searchText != "" {
r := bson.RegEx{Pattern: searchText, Options: "i"}
filter["$or"] = []bson.M{{"name": r}, {"appId": r}}
}
switch pullType {
case "published":
filter["status.value"] = entity.StPublished
}
total, err := appTable.GetSome(ctx, filter, sortList, pageSize, pageNo, &list)
if err != nil {
return 0, nil, err
}
result := make([]entity.App, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppToEntity(&temp))
}
return total, result, err
}
func (ar *AppRepo) ListAppVers(ctx context.Context, pageNo, pageSize int, searchText string, t string, groupId string) (int, []entity.AppVersion, error) {
filter := bson.M{}
filterList := make([]bson.M, 0)
if groupId != "" {
filterList = append(filterList, bson.M{
"groupId": groupId,
})
}
filterList = append(filterList, bson.M{
"sequence": bson.M{"$ne": 0},
})
if searchText != "" {
r := bson.RegEx{Pattern: searchText, Options: "i"}
filterList = append(filterList, bson.M{"$or": []bson.M{
{"name": r},
{"appId": r},
}})
}
sortList := make([]string, 0)
sortList = append(sortList, "-publishingStatus.lastUpdated")
switch t {
case "pendingReview": //待审核
filterList = append(filterList, bson.M{"status.value": bson.M{"$in": []string{entity.StPublishing, entity.StUnpublishing}}})
case "reviewed": //已审核
filterList = append(filterList, bson.M{"status.value": bson.M{"$nin": []string{entity.StPublishing, entity.StPublishWithdrawed}}})
case "revoked": //已撤销
//PublishWithdrawed
filterList = append(filterList, bson.M{"status.value": bson.M{"$in": []string{entity.StPublishWithdrawed}}})
//default:
}
filter["$and"] = filterList
log.Debugln("filter:%v", filter)
list := make([]mongo.AppVersion, 0)
total, err := appVerTable.GetSome(ctx, filter, sortList, pageSize, pageNo, &list)
if err != nil {
return 0, nil, err
}
result := make([]entity.AppVersion, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return total, result, nil
}
func (ar *AppRepo) UnpubApp(ctx context.Context, appId string, seq int, account, reason string, isDev bool) error {
unpublishedType := ""
if isDev {
unpublishedType = entity.TypeUnpublishedByDev
} else {
unpublishedType = entity.TypeUnpublishedByAdmin
}
now := time.Now().UnixNano() / 1e6
params := map[string]interface{}{}
params["status"] = mongo.Status{
Value: entity.StUnpublished,
ModifiedBy: account,
LastUpdated: now,
Reason: reason,
}
params["unpublishedStatus"] = mongo.UnpublishedStatus{
ModifiedBy: account,
LastUpdated: now,
Reason: reason,
Type: unpublishedType,
}
params["actionStatus"] = mongo.SpecificStatus{
ModifiedBy: account,
LastUpdated: now,
Reason: reason,
}
err := appVerTable.UpdateOne(ctx, bson.M{"appId": appId, "sequence": seq}, bson.M{"$set": params})
if err != nil {
return err
}
return appTable.UpdateOne(ctx, bson.M{"appId": appId}, bson.M{"$set": params})
}
func (ar *AppRepo) ApproveApp(ctx context.Context, appId string, seq int, account, reason string, isPass bool) error {
params := map[string]interface{}{}
now := utils.GetNowMs()
if isPass {
params["status"] = entity.Status{
Value: entity.StPublishApproved,
LastUpdated: now,
ModifiedBy: account,
Reason: reason,
}
} else {
params["status"] = entity.Status{
Value: entity.StPublishRejected,
LastUpdated: now,
ModifiedBy: account,
Reason: reason,
}
}
params["publishingApprovalStatus"] = mongo.SpecificStatus{
LastUpdated: now,
ModifiedBy: account,
Reason: reason,
}
params["approvalStatus"] = mongo.SpecificStatus{
LastUpdated: now,
ModifiedBy: account,
Reason: reason,
}
return appVerTable.UpdateOne(ctx, bson.M{"appId": appId, "sequence": seq}, bson.M{"$set": params})
}
func (ar *AppRepo) ListAppVersByAppId(ctx context.Context, appId string, pageNo, pageSize int) (int, []entity.AppVersion, error) {
filter := bson.M{
"appId": appId,
"sequence": bson.M{"$ne": 0},
"status.value": bson.M{"$ne": entity.StDeleted},
}
list := make([]mongo.AppVersion, 0)
total, err := appVerTable.GetSome(ctx, filter, []string{"-sequence"}, pageSize, pageNo, &list)
if err != nil {
return 0, nil, err
}
result := make([]entity.AppVersion, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return total, result, nil
}
func (ar *AppRepo) GetAppReviews(ctx context.Context, req apiproto.AdminGetAppReviewsReq) (int, []entity.AppVersion, error) {
return 0, nil, nil
}
func (ar *AppRepo) GetAllPublishedVerList(ctx context.Context, appId string) ([]entity.AppVersion, int, error) {
filter := bson.M{
"appId": appId,
"status.value": entity.StPublished,
}
list := make([]mongo.AppVersion, 0)
total, err := appVerTable.GetAll(ctx, filter, []string{"-sequence"}, &list)
if err != nil {
return nil, 0, err
}
result := make([]entity.AppVersion, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return result, total, nil
}
func (ar *AppRepo) GetPublishedAppList(ctx context.Context, pageNo, pageSize int, searchText string) ([]entity.AppVersion, int, error) {
filter := bson.M{
"status.value": entity.StPublished,
}
if searchText != "" {
r := bson.RegEx{Pattern: searchText, Options: "i"}
filter["$or"] = []bson.M{
{"name": r},
{"appId": r},
}
}
list := make([]mongo.AppVersion, 0)
total, err := appVerTable.GetSome(ctx, filter, []string{"-created"}, pageSize, pageNo, &list)
if err != nil {
return nil, 0, err
}
result := make([]entity.AppVersion, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return result, total, nil
}
func (ar *AppRepo) GetAllVers(ctx context.Context, appId string) ([]entity.AppVersion, int, error) {
filter := bson.M{
"appId": appId,
}
list := make([]mongo.AppVersion, 0)
total, err := appVerTable.GetAll(ctx, filter, []string{"-sequence"}, &list)
if err != nil {
return nil, 0, err
}
result := make([]entity.AppVersion, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return result, total, nil
}
func (ar *AppRepo) GetAppsByAppIds(ctx context.Context, appIds []string) ([]entity.App, error) {
list := make([]mongo.App, 0)
_, err := appTable.GetAllV2(ctx, bson.M{"appId": bson.M{"$in": appIds}}, []string{"-created"}, &list)
if err != nil {
return nil, err
}
result := make([]entity.App, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppToEntity(&temp))
}
return result, nil
}
func (ar *AppRepo) GetAppsByGroupIds(ctx context.Context, groupIds []string) (int, []entity.App, error) {
list := make([]mongo.App, 0)
total, err := appTable.GetAllV2(ctx, bson.M{"groupId": bson.M{"$in": groupIds}}, []string{"-created"}, &list)
if err != nil {
return 0, nil, err
}
result := make([]entity.App, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppToEntity(&temp))
}
return total, result, nil
}
func (ar *AppRepo) GetAppsToBinding(ctx context.Context, bindingId string, pageNo, pageSize int, searchText string) (int, []entity.App, error) {
br := NewBindingByMongoRepo()
bindingInfo, err := br.GetBindingByBindingId(ctx, bindingId)
if err != nil {
return 0, nil, err
}
//获取应用关联的小程序Id
assAppIdList := make([]string, 0)
for _, v := range bindingInfo.AppInfos {
assAppIdList = append(assAppIdList, v.AppID)
}
filter := bson.M{"groupId": bindingInfo.GroupID, "appId": bson.M{"$nin": assAppIdList}}
if searchText != "" {
filter["name"] = bson.RegEx{
Pattern: searchText,
Options: "i",
}
}
list := make([]mongo.App, 0)
total, err := appTable.GetSome(ctx, filter, []string{"-created"}, pageSize, pageNo, &list)
if err != nil {
return 0, nil, err
}
result := make([]entity.App, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppToEntity(&temp))
}
return total, result, nil
}
func (ar *AppRepo) GetLinkAppsByBindingId(ctx context.Context, bindingId string, pageNo, pageSize int, searchText string) (int, []entity.App, error) {
br := NewBindingByMongoRepo()
bindingInfo, err := br.GetBindingByBindingId(ctx, bindingId)
if err != nil {
return 0, nil, err
}
//获取应用关联的小程序Id
assAppIdList := make([]string, 0)
for _, v := range bindingInfo.AppInfos {
assAppIdList = append(assAppIdList, v.AppID)
}
list := make([]mongo.App, 0)
total, err := appTable.GetSome(ctx, bson.M{"groupId": bindingInfo.GroupID, "appId": bson.M{"$in": assAppIdList}}, []string{"-created"}, pageSize, pageNo, &list)
if err != nil {
return 0, nil, err
}
result := make([]entity.App, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppToEntity(&temp))
}
return total, result, nil
}
func (ar *AppRepo) GetLinkAppsBySDKKey(ctx context.Context, sdkKey string, pageNo, pageSize int) (int, []entity.App, error) {
br := NewBindingByMongoRepo()
bindingInfos, err := br.GetBindListBySdkKey(ctx, sdkKey)
log.Infof(utility.InterfaceToJsonString(bindingInfos))
if err != nil {
return 0, nil, err
}
//获取应用关联的小程序Id
assAppIdList := make([]string, 0)
for _, v := range bindingInfos {
for _, y := range v.AppInfos {
assAppIdList = append(assAppIdList, y.AppID)
}
}
list := make([]mongo.App, 0)
total, err := appTable.GetSome(ctx, bson.M{"status.value": entity.StPublished, "appId": bson.M{"$in": assAppIdList}}, []string{"-created"}, pageSize, pageNo, &list)
if err != nil {
return 0, nil, err
}
result := make([]entity.App, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppToEntity(&temp))
}
return total, result, nil
}
func (ar *AppRepo) AdminGetLinkApps(ctx context.Context, pageNo, pageSize int, searchText string) (int, []entity.AdminGetLinkAppsRspItem, error) {
//这里需要连接三个表 app表、binding表、typeConfig表
//todo 做优化的时候进行修改
type TempItem struct {
Id bson.ObjectId `bson:"_id"`
AppIdDetail struct {
AppId string `bson:"appId"`
Name string `bson:"name"`
Sequence int `bson:"sequence"`
} `bson:"appIdDetail"`
AppInfos struct {
AssociatedAt int64 `bson:"associatedAt"`
} `bson:"app_infos"`
BindingId string `bson:"binding_id"`
GroupName string `bson:"group_name"`
Name string `bson:"name"`
}
binds := make([]TempItem, 0)
//连接app表
lookupFind := bson.M{"from": AppTableName, "localField": "app_infos.appId", "foreignField": "appId", "as": "appIdDetail"}
//分组
unwindValue := "$app_infos"
//小程序创建时间倒序
sortValue := bson.M{"app_infos.associatedAt": -1}
skipValue := pageNo * pageSize
limitValue := pageSize
//模糊匹配
matchValue := bson.M{
"$or": []bson.M{
{"name": bson.M{"$regex": bson.RegEx{Pattern: searchText, Options: "i"}}},
{"appIdDetail.name": bson.M{"$regex": bson.RegEx{Pattern: searchText, Options: "i"}}},
{"group_name": bson.M{"$regex": bson.RegEx{Pattern: searchText, Options: "i"}}},
},
}
//typeConfig 小程序开发匹配namespace为appClass的
//typeConfigMatchValue := bson.M{"appClassDetail.namespace": "appClass"}
//需要的字段
projectValue := bson.M{
"binding_id": 1,
"appClassDetail.customData.chinese": 1,
"appIdDetail.name": 1,
"appIdDetail.appId": 1,
"appIdDetail.sequence": 1,
"name": 1,
"group_name": 1,
"app_infos.associatedAt": 1,
}
//连接typeConfig表
//appClassLookup := bson.M{"from": "typeConfig", "localField": "appIdDetail.appClass", "foreignField": "value", "as": "appClassDetail"}
pipSelector := []bson.M{
{"$unwind": unwindValue},
{"$lookup": lookupFind},
{"$match": matchValue},
{"$sort": sortValue},
{"$skip": skipValue},
{"$limit": limitValue},
{"$unwind": "$appIdDetail"},
//{"$lookup": appClassLookup},
//{"$match": typeConfigMatchValue},
//{"$unwind": "$appClassDetail"},
{"$project": projectValue},
}
countSelector := []bson.M{
{"$unwind": unwindValue},
{"$lookup": lookupFind},
{"$match": matchValue},
{"$group": bson.M{"_id": nil, "total": bson.M{"$sum": 1}}},
}
log.Infof("pipe selector:%+v,countSelector:%+v\n", pipSelector, countSelector)
err := bindingTable.Aggregate(ctx, pipSelector, &binds)
if err != nil && !NotFound(err) {
return 0, nil, err
}
total, err := bindingTable.AggregateCount(ctx, countSelector)
if err != nil {
return 0, nil, err
}
result := make([]entity.AdminGetLinkAppsRspItem, 0)
for _, v := range binds {
item := entity.AdminGetLinkAppsRspItem{}
item.Id = v.Id.Hex()
item.AppIdDetail.AppId = v.AppIdDetail.AppId
item.AppIdDetail.Name = v.AppIdDetail.Name
item.AppIdDetail.Sequence = v.AppIdDetail.Sequence
item.AppInfos.AssociatedAt = v.AppInfos.AssociatedAt
item.BindingId = v.BindingId
item.GroupName = v.GroupName
item.Name = v.Name
result = append(result, item)
}
return total, result, nil
}
func (ar *AppRepo) UpdateGrayStatus(ctx context.Context, appId string, seq int, status bool) error {
err := appVerTable.UpdateOne(ctx, bson.M{"appId": appId, "sequence": seq}, bson.M{"$set": bson.M{"inGrayRelease": status}})
if err != nil {
return err
}
return nil
}
func (ar *AppRepo) UpdateExpire(ctx context.Context, appId string, expire int64) error {
err := appTable.UpdateOne(ctx, bson.M{"appId": appId}, bson.M{"$set": bson.M{"expire": expire}})
if err != nil {
return err
}
return nil
}
func (ar *AppRepo) UpdateExpireAppVersion(ctx context.Context, appId string, seq int, expire int64) error {
err := appVerTable.UpdateOne(ctx, bson.M{"appId": appId, "sequence": seq}, bson.M{"$set": bson.M{"expire": expire}})
if err != nil {
return err
}
return nil
}
func (ar *AppRepo) GetRollbackAbleList(ctx context.Context, groupId, appId string, latestPubSeq int) ([]entity.AppVersion, error) {
appVers := make([]mongo.AppVersion, 0)
limitVersion := 5 // 最近5个版本, 不是最近下架的5个版本
filter := bson.M{
"appId": appId,
"groupId": groupId,
"status.value": bson.M{"$eq": entity.StUnpublished},
"sequence": bson.M{"$gte": latestPubSeq - limitVersion, "$lt": latestPubSeq},
"$or": []bson.M{
{"unpublishedStatus.type": entity.TypeUnpublishedByDev},
{"unpublishedStatus.type": entity.TypeUnpublishedDueToNewSeq},
},
}
_, err := appVerTable.GetSome(ctx, filter, []string{"-sequence"}, limitVersion, 1, &appVers)
if err != nil {
return nil, err
}
result := make([]entity.AppVersion, 0)
for _, v := range appVers {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return result, nil
}
func (ar *AppRepo) GetCreatedStatistics(ctx context.Context, startTime, endTime int64, groupId string, isForbidden int) (int, error) {
filter := bson.M{}
if groupId != "" {
filter["groupId"] = groupId
}
if startTime != -1 && startTime != 0 {
filter["created"] = bson.M{"$gt": startTime}
}
if endTime != -1 && endTime != 0 {
if _, ok := filter["created"]; ok {
filter["created"] = bson.M{"$gt": startTime, "$lte": endTime}
}
}
switch isForbidden {
case entity.APP_FORBIDDEN_NOT_STATUS:
filter["$or"] = []bson.M{{"isForbidden": entity.APP_FORBIDDEN_NOT_STATUS}, {"isForbidden": bson.M{"$exists": false}}}
case entity.APP_FORBIDDEN_IS_STATUS:
filter["isForbidden"] = entity.APP_FORBIDDEN_IS_STATUS
}
log.Debugf("GetCreatedStatistics filter:%+v", filter)
return appTable.Count(ctx, filter)
}
func (ar *AppRepo) GetPublishedStatistics(ctx context.Context, startTime, endTime int64, groupId string) (int, error) {
filter := bson.M{}
if groupId != "" {
filter["groupId"] = groupId
}
if startTime != -1 && startTime != 0 {
filter["publishedStatus.lastUpdated"] = bson.M{"$gt": startTime}
}
if endTime != -1 && endTime != 0 {
filter["publishedStatus.lastUpdated"] = bson.M{"$gt": startTime, "$lte": endTime}
}
filter["status.value"] = entity.StPublished
return appTable.Count(ctx, filter)
}
//GetSubmittedStatistics 累计审核的小程序----只要小程序被审核过都算
func (ar *AppRepo) GetSubmittedStatistics(ctx context.Context, startTime, endTime int64, groupId string, distinct bool) (int, error) {
filter := bson.M{}
if groupId != "" {
filter["groupId"] = groupId
}
if startTime != -1 && startTime != 0 {
filter = bson.M{"$and": []bson.M{
filter,
{
"publishingStatus.lastUpdated": bson.M{"$gt": startTime},
},
},
}
}
if endTime != -1 && endTime != 0 {
filter = bson.M{"$and": []bson.M{
filter,
{
"publishingStatus.lastUpdated": bson.M{"$lte": endTime},
},
},
}
}
filter = bson.M{"$and": []bson.M{
filter,
{
"status.value": bson.M{
"$ne": entity.StInDevelopment,
},
},
},
}
if distinct {
aggregate := []bson.M{
{
"$project": bson.M{
"appId": 1,
"publishingStatus": 1,
"status": 1,
},
},
{
"$match": filter,
},
{
"$group": bson.M{
"_id": "appId",
"total": bson.M{"$sum": 1},
},
},
}
return appVerTable.AggregateCount(ctx, aggregate)
}
return appVerTable.Count(ctx, filter)
}
//审核通过小程序
func (ar *AppRepo) GetApprovedStatistics(ctx context.Context, startTime, endTime int64, groupId string, distinct bool) (int, error) {
filter := bson.M{}
if groupId != "" {
filter["groupId"] = groupId
}
filter = bson.M{
"$and": []bson.M{
filter,
{
"status.value": bson.M{
"$in": []string{
entity.StPublishApproved,
entity.StPublished,
entity.StUnpublishing,
entity.StUnpublishApproved,
entity.StUnpublishRejected,
entity.StUnpublished,
},
},
},
},
}
if distinct {
return appTable.Count(ctx, filter)
}
return appVerTable.Count(ctx, filter)
}
func (ar *AppRepo) GetAppClassPer(ctx context.Context, status string) ([]entity.AppClassPerRsp, error) {
match := bson.M{}
if status != "" {
if status == "published" {
match["status.value"] = entity.StPublished
}
}
group := bson.M{"_id": "$appClass", "count": bson.M{"$sum": 1}}
type perRsp struct {
Class string `bson:"class"`
Count int `bson:"count"`
Name []string `bson:"name"`
}
lookUp := bson.M{
"from": TypeConfigTableName,
"localField": "_id",
"foreignField": "value",
"as": "classInfo",
}
project := bson.M{
"class": "$_id",
"count": 1,
"name": "$classInfo.chinese",
}
perRspList := make([]perRsp, 0)
pipSelector := []bson.M{
{"$match": match},
{"$group": group},
{"$lookup": lookUp},
{"$project": project},
}
err := appTable.AggregateOnly(ctx, pipSelector, &perRspList)
if err != nil {
return nil, err
}
rspData := make([]entity.AppClassPerRsp, 0)
for _, v := range perRspList {
rspData = append(rspData, entity.AppClassPerRsp{
Class: v.Class,
Count: v.Count,
Name: v.Name,
})
}
return rspData, nil
}
func (ar *AppRepo) GetAppVerLimitByIdentity(ctx context.Context, t, appId, identity string, limit int) ([]entity.AppVersion, error) {
list := make([]mongo.AppVersion, 0)
var filter bson.M
//默认md5
filter = bson.M{"appId": appId, "customData.sourceFile.fileMd5": identity}
if t == "sha256" {
filter = bson.M{"appId": appId, "customData.sourceFile.encryptedFileSha256": identity}
}
err := appVerTable.OnlyGetSome(ctx, filter, []string{"-sequence"}, limit, 1, &list)
if err != nil {
return nil, err
}
result := make([]entity.AppVersion, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return result, nil
}
func (ar *AppRepo) GetGrayStatisticsVerList(ctx context.Context, appId string, seq int, begin, end int64) (int, []entity.AppVersion, error) {
filter := bson.M{
"appId": appId,
"sequence": bson.M{"$lt": seq},
}
if end == 0 {
filter["$or"] = []bson.M{
{"status.value": entity.StPublished},
{"status.value": entity.StUnpublished, "unpublishedStatus.lastUpdated": bson.M{"$gte": begin}},
}
} else {
filter["$or"] = []bson.M{
{"status.value": entity.StPublished, "publishedStatus.lastUpdated": bson.M{"$lte": end}},
{"status.value": entity.StUnpublished, "$or": []bson.M{
{"publishedStatus.lastUpdated": bson.M{"$gte": begin, "$lte": end}},
{"unpublishedStatus.lastUpdated": bson.M{"$gte": begin, "$lte": end}},
},
},
}
}
log.Debugf("GetGrayStatisticsVerList filter:%+v", filter)
sort := []string{"sequence"}
list := make([]mongo.AppVersion, 0)
num, err := appVerTable.GetAll(ctx, filter, sort, &list)
if err != nil {
log.Errorf("GetGrayStatisticsVerList db err:%s", err.Error())
return 0, nil, err
}
result := make([]entity.AppVersion, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertAppVerToEntity(&temp))
}
return num, result, nil
}
func (ar *AppRepo) GetAppClassList(ctx context.Context) ([]entity.CItem, error) {
appClassList := make([]entity.CItem, 0)
group := bson.M{"_id": "$appClass"}
pipSelector := []bson.M{
{"$group": group},
}
err := appTable.AggregateOnly(ctx, pipSelector, &appClassList)
if err != nil {
return make([]entity.CItem, 0), err
}
return appClassList, nil
}
func (ar *AppRepo) GetAppTagList(ctx context.Context) ([]entity.TagItem, error) {
appTagList := make([]entity.TagItem, 0)
group := bson.M{"_id": "$appTag"}
pipSelector := []bson.M{
{"$group": group},
}
err := appTable.AggregateOnly(ctx, pipSelector, &appTagList)
if err != nil {
return make([]entity.TagItem, 0), err
}
return appTagList, nil
}
func (ar *AppRepo) GetAppsBySearchText(ctx context.Context, searchText string) ([]entity.App, error) {
return nil, nil
}
func (ar *AppRepo) GetAppByCreator(ctx context.Context, phone string) ([]entity.App, error) {
return nil, nil
}