@@ -0,0 +1,6 @@
|
||||
package officialmod
|
||||
|
||||
// QueryCond 查询列表
|
||||
type QueryCond struct {
|
||||
Type string `form:"type" json:"type" binding:"required"`
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package officialmod
|
||||
|
||||
import (
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/localcache"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"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"
|
||||
"time"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.OfficialConfig
|
||||
|
||||
// Coll 获取表名
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InitIndex 初始化索引
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{"officialName", 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{"officialType", 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{"updatedAt", 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{"isActive", 1}},
|
||||
},
|
||||
}
|
||||
_, err := coll(nil).CreateIndex(many)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// InsertOne 新增
|
||||
func InsertOne(t *OfficialConfig) (data primitive.ObjectID, err error) {
|
||||
result, err := coll(nil).InsertOne(&t)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "InsertOne", err))
|
||||
return
|
||||
}
|
||||
byteID, err := json.Marshal(result.InsertedID)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "Marshal", err))
|
||||
return
|
||||
}
|
||||
err = data.UnmarshalJSON(byteID)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "UnmarshalJSON", err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// QueryAll 查询配置列表
|
||||
func QueryAll(in *QueryAllCond, opts ...*options.FindOptions) ([]*OfficialConfig, error) {
|
||||
var out []*OfficialConfig = []*OfficialConfig{}
|
||||
fliter := bson.M{}
|
||||
if in.Status != nil {
|
||||
fliter["isActive"] = in.Status
|
||||
}
|
||||
if in.OfficialType != "" {
|
||||
fliter["officialType"] = in.OfficialType
|
||||
}
|
||||
if err := coll(nil).Find(&out, fliter, opts...); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAll", models.Activity, "Find", err),
|
||||
log.Any("filter", fliter),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Count 查询总条数
|
||||
func Count(filter primitive.M) (int64, error) {
|
||||
if count, err := coll(nil).Count(filter); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Count", models.Activity, "Count", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return 0, err
|
||||
} else {
|
||||
return count, nil
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateOneOfficiaByID 编辑标签
|
||||
func UpdateOneOfficiaByID(id primitive.ObjectID, doc OfficiaUpdateDoc) (err error) {
|
||||
docM, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateOneOfficiaByID", table, "ToBsonM", err),
|
||||
log.Any("id", id),
|
||||
log.Any("doc", doc),
|
||||
)
|
||||
return
|
||||
}
|
||||
update := bson.M{}
|
||||
update["$set"] = docM
|
||||
_, err = coll(nil).UpdateOne(bson.M{"_id": id}, update)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateOneOfficiaByID", table, "UpdateOne", err),
|
||||
log.Any("id", id),
|
||||
log.Any("doc", doc),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteOfficias 删除标签
|
||||
func DeleteOfficias(ids []primitive.ObjectID) (int64, error) {
|
||||
if ids == nil {
|
||||
ids = []primitive.ObjectID{}
|
||||
}
|
||||
cond := bson.M{"_id": bson.M{"$in": ids}}
|
||||
result, err := coll(nil).DeleteMany(cond)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteOfficias", table, "DeleteMany", err), log.Any("ids", ids))
|
||||
return 0, err
|
||||
}
|
||||
return result.DeletedCount, nil
|
||||
}
|
||||
|
||||
// FindTagByID 根据id获取标签信息
|
||||
func FindOneTagByID(id primitive.ObjectID) (data OfficialConfig, err error) {
|
||||
err = coll(nil).FindOne(&data, bson.M{"_id": id})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOneTagByID", table, "FindOne", err),
|
||||
log.Any("id", id),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 查询配置列表,app使用
|
||||
func Query(officialType string) ([]*OfficialConfigApp, error) {
|
||||
var out []*OfficialConfigApp = []*OfficialConfigApp{}
|
||||
fliter := bson.M{"officialType": officialType, "isActive": true}
|
||||
opts := (&options.FindOptions{}).SetSort(bson.D{{"sort", -1}})
|
||||
if err := coll(nil).Find(&out, fliter, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Query", models.Activity, "Find", err),
|
||||
log.Any("filter", fliter),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func QueryOfficeList() ([]*OfficialConfigApp, error) {
|
||||
cfg, ok := localcache.C.Get(GetOfficialList)
|
||||
if ok {
|
||||
return cfg.([]*OfficialConfigApp), nil
|
||||
}
|
||||
|
||||
var out []*OfficialConfigApp
|
||||
filter := bson.M{"officialType": "1", "isActive": true, "isHomepageAds": true}
|
||||
opts := (&options.FindOptions{}).SetSort(bson.D{{"sort", -1}})
|
||||
if err := coll(nil).Find(&out, filter, opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Query", models.Activity, "QueryOfficeList", err),
|
||||
log.Any("filter", filter),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
common.Go(func() {
|
||||
localcache.C.Set(GetOfficialList, out, 5*time.Minute)
|
||||
})
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package officialmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Init 初始化标签model索引
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
const (
|
||||
GetOfficialList = "GetOfficialList"
|
||||
)
|
||||
|
||||
type PositionType int
|
||||
|
||||
const (
|
||||
ZJ = 1 // 装机必备APP
|
||||
GC = 2 // 国产APP
|
||||
)
|
||||
|
||||
// OfficialConfig 官方配置
|
||||
type OfficialConfig struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // 官方id
|
||||
OfficialName string `json:"officialName" bson:"officialName"` // 官方名字
|
||||
OfficialDesc string `json:"officialDesc" bson:"officialDesc"` // 官方描述
|
||||
OfficialImg string `json:"officialImg" bson:"officialImg"` // 官方图片
|
||||
OfficialUrl string `json:"officialUrl" bson:"officialUrl"` // 官方链接
|
||||
Position int `json:"position" bson:"position"` // 官方位置
|
||||
OfficialType string `json:"officialType" bson:"officialType"` // 官方类型,1:下载 2:社区
|
||||
IsActive bool `json:"isActive" bson:"isActive"` // 开关
|
||||
Sort int64 `json:"sort" bson:"sort"` // 排序
|
||||
IsHomepageAds bool `json:"isHomepageAds" bson:"isHomepageAds"` // 是否首页广告开关
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 更新时间
|
||||
}
|
||||
|
||||
// OfficiaUpdateDoc 官方更新信息
|
||||
type OfficiaUpdateDoc struct {
|
||||
OfficialName *string `bson:"officialName,omitempty"` // 官方名字
|
||||
OfficialDesc *string `bson:"officialDesc,omitempty"` // 官方描述
|
||||
OfficialImg *string `bson:"officialImg,omitempty"` // 官方图片
|
||||
OfficialUrl *string `bson:"officialUrl,omitempty"` // 官方链接
|
||||
CreatedAt time.Time `bson:"createdAt,omitempty"` // 创建时
|
||||
UpdatedAt time.Time `bson:"updatedAt,omitempty"` // 更新时间
|
||||
IsActive *bool `bson:"isActive,omitempty"` // 是否启用
|
||||
Sort *int64 `bson:"sort,omitempty"` // 是否启用
|
||||
Position *int `json:"position" bson:"position"` // 官方位置
|
||||
IsHomepageAds *bool `bson:"isHomepageAds,omitempty"` // 是否首页广告开关
|
||||
}
|
||||
|
||||
// OfficialConfigApp 官方配置app返回实体
|
||||
type OfficialConfigApp struct {
|
||||
OfficialName string `json:"officialName" bson:"officialName"` // 官方名字
|
||||
OfficialDesc string `json:"officialDesc" bson:"officialDesc"` // 官方描述
|
||||
OfficialImg string `json:"officialImg" bson:"officialImg"` // 官方图片
|
||||
OfficialUrl string `json:"officialUrl" bson:"officialUrl"` // 官方链接
|
||||
Position int `json:"position" bson:"position"` // 官方位置
|
||||
OfficialType string `json:"officialType" bson:"officialType"` // 官方类型,1:下载 2:社区
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package officialmod
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"time"
|
||||
)
|
||||
|
||||
// QueryAllCond 查询列表
|
||||
type QueryAllCond struct {
|
||||
OfficialType string `form:"officialType" json:"officialType"`
|
||||
Status *bool `form:"isActive" json:"officialName"` // 状态---启用/禁用
|
||||
PageNumber int64 `form:"pageNumber" json:"pageNumber" binding:"required,min=1"` // 当前页
|
||||
PageSize int64 `form:"pageSize" json:"pageSize" binding:"required,min=10,max=50"` // 每页条数
|
||||
}
|
||||
|
||||
func (q *QueryAllCond) Options() *options.FindOptions {
|
||||
return options.Find().SetLimit(q.PageSize).SetSkip((q.PageNumber - 1) * q.PageSize).SetSort(bson.M{"updatedAt": -1}).SetSort(bson.M{"sort": -1})
|
||||
}
|
||||
|
||||
type WebOfficialDeleteReqInfo struct {
|
||||
IDs []primitive.ObjectID `form:"id" json:"id" binding:"required"`
|
||||
}
|
||||
|
||||
type WebOfficialAddReqInfo struct {
|
||||
OfficialName string `form:"officialName" json:"officialName" binding:"required"`
|
||||
OfficialDesc string `form:"officialDesc" json:"officialDesc" binding:"omitempty"`
|
||||
OfficialImg string `form:"officialImg" json:"officialImg" binding:"omitempty"`
|
||||
OfficialUrl string `form:"officialUrl" json:"officialUrl" binding:"required"`
|
||||
Position int `json:"position" bson:"position"` // 官方位置
|
||||
OfficialType string `form:"officialType" json:"officialType" binding:"required"`
|
||||
IsActive bool `form:"isActive" json:"isActive" binding:"omitempty"`
|
||||
Sort int64 `form:"sort" json:"sort" binding:"omitempty"` //排序
|
||||
IsHomepageAds bool `form:"isHomepageAds" json:"isHomepageAds" binding:"omitempty"`
|
||||
CreatedAt time.Time `bson:"createdAt" json:"createdAt" binding:"omitempty"` // 创建时间
|
||||
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt" binding:"omitempty"` // 更新时间
|
||||
}
|
||||
|
||||
type WebTagUpdateReqInfo struct {
|
||||
ID primitive.ObjectID `form:"id" json:"id" binding:"required"`
|
||||
OfficialName string `form:"officialName" json:"officialName" binding:"omitempty"`
|
||||
OfficialDesc string `form:"officialDesc" json:"officialDesc" binding:"omitempty"`
|
||||
OfficialImg string `form:"officialImg" json:"officialImg" binding:"omitempty"`
|
||||
OfficialUrl string `form:"officialUrl" json:"officialUrl" binding:"omitempty"`
|
||||
IsActive bool `form:"isActive" json:"isActive" binding:"omitempty"`
|
||||
Sort int64 `form:"sort" json:"sort" binding:"omitempty"` // 排序
|
||||
Position int `form:"position" json:"position" bson:"position"` // 官方位置
|
||||
IsHomepageAds bool `form:"isHomepageAds" json:"isHomepageAds" binding:"omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user