forked from all_user/stat_center
191 lines
4.3 KiB
Go
191 lines
4.3 KiB
Go
package versionMod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"stat/common/log"
|
|
"stat/common/mongos"
|
|
"time"
|
|
|
|
"github.com/jinzhu/copier"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// 初始化model
|
|
func Init() {
|
|
// 获取数据库表
|
|
db = mongos.New(mongos.MgoManager, CollAppVersion)
|
|
|
|
// 初始化索引
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "_id", Value: 1},
|
|
{Key: "appId", Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "terminal", Value: 1},
|
|
{Key: "appId", Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "appId", Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "status", Value: 1},
|
|
},
|
|
},
|
|
}
|
|
|
|
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", CollAppVersion, err))
|
|
}
|
|
}
|
|
|
|
// GetVersionList 查询版本信息
|
|
func GetVersionList(ctx context.Context, appId *int32, skip, limit int64) (list VersionList, count int64, err error) {
|
|
list = make(VersionList, 0)
|
|
filter := bson.M{}
|
|
if appId != nil {
|
|
filter["appId"] = *appId
|
|
}
|
|
sort := bson.D{{Key: "createdAt", Value: -1}}
|
|
|
|
opts := options.Find().SetSkip(skip).SetLimit(limit).SetSort(sort)
|
|
|
|
if err = db.Find(ctx, &list, filter, opts); err != nil {
|
|
log.Error("version GetVersionList Find err", log.E(err))
|
|
return
|
|
}
|
|
|
|
count, err = db.Count(ctx, filter)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// DelVersion 删除版本
|
|
func DelVersion(ctx context.Context, id int64) (err error) {
|
|
_, err = db.DeleteOne(ctx, primitive.M{"_id": id})
|
|
return err
|
|
}
|
|
|
|
// AddVersion 添加一个版本
|
|
func AddVersion(ctx context.Context, data *VersionModel) (err error) {
|
|
_, err = db.InsertOne(ctx, data)
|
|
return
|
|
}
|
|
|
|
// 获取版本信息
|
|
func FindVersion(ctx context.Context, id int64) (data VersionModel, err error) {
|
|
|
|
err = db.FindOne(ctx, &data, bson.M{"_id": id})
|
|
return
|
|
}
|
|
|
|
// 更新版本信息
|
|
func UpdateVersion(ctx context.Context, data *VersionModel) (err error) {
|
|
data.UpdatedAt = time.Now()
|
|
update := &VersionUpdate{}
|
|
err = copier.Copy(update, data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = db.UpdateOne(ctx, primitive.M{"_id": data.Id}, bson.M{"$set": update})
|
|
return
|
|
}
|
|
|
|
// ButtonClick 查询最新下载地址ios android
|
|
func ButtonClick(ctx context.Context, appId int32, terminal string, packageType PkgType) (data *VersionModel, err error) {
|
|
data = &VersionModel{}
|
|
match := bson.M{"status": true, "appId": appId}
|
|
if terminal == "ios" {
|
|
match["terminal"] = terminal
|
|
match["packageType"] = packageType
|
|
} else {
|
|
match["terminal"] = terminal
|
|
}
|
|
|
|
opts := options.FindOne().SetSort(bson.M{
|
|
"createdAt": -1,
|
|
})
|
|
|
|
err = db.FindOne(ctx, data, match, opts)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func ButtonList(ctx context.Context, appId int32) (res VersionList, err error) {
|
|
res = make([]*VersionModel, 0)
|
|
opt := &options.FindOptions{}
|
|
opt.SetSort(bson.M{"packageType": 1})
|
|
|
|
err = db.Find(ctx, &res, primitive.M{"status": true, "appId": appId}, opt)
|
|
return
|
|
}
|
|
|
|
func FindVersByPkgTyps(ctx context.Context, appId int32, terminal string, curVersion string, packageType PkgType) (list VersionList, err error) {
|
|
list = make(VersionList, 0)
|
|
conf := bson.M{
|
|
"appId": appId,
|
|
"status": true,
|
|
"terminal": terminal}
|
|
if packageType != 0 {
|
|
conf["packageType"] = packageType
|
|
}
|
|
opts := options.Find().SetSort(bson.M{
|
|
"createdAt": -1,
|
|
})
|
|
|
|
err = db.Find(ctx, &list, conf, opts)
|
|
return
|
|
}
|
|
|
|
func FindIosVersion(ctx context.Context, appId int32, terminal string, curVersion string, packageType []PkgType) (list VersionList, err error) {
|
|
list = make(VersionList, 0)
|
|
conf := bson.M{
|
|
"appId": appId,
|
|
"status": true,
|
|
"terminal": terminal}
|
|
if len(packageType) > 0 {
|
|
conf["packageType"] = bson.M{"$in": packageType}
|
|
}
|
|
opts := options.Find().SetSort(bson.M{
|
|
"createdAt": -1,
|
|
})
|
|
|
|
err = db.Find(ctx, &list, conf, opts)
|
|
return
|
|
}
|
|
|
|
// GetVersionList 查询版本信息
|
|
func GetTfUrlList(ctx context.Context, appId int32) (list VersionModel, err error) {
|
|
filter := bson.M{"terminal": "ios", "packageType": Tf, "status": true, "appId": appId}
|
|
|
|
opts := options.FindOne().SetSort(bson.M{
|
|
"createdAt": -1,
|
|
})
|
|
|
|
if err = db.FindOne(ctx, &list, filter, opts); err != nil {
|
|
log.Error("version GetVersionList Find err", log.E(err))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|