@@ -0,0 +1,172 @@
|
||||
package annoumod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.Annou
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InitIndex 初始化索引
|
||||
func initAnnouIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "active", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "updatedAt", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "type", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// AddAnnou 增加公告
|
||||
func AddAnnou(annou []Annou) error {
|
||||
if _, err := coll(nil).InsertMany(&annou); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddAnnou", table, "InsertMany", err), log.Any("annou", annou))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteAnnou 删除公告
|
||||
func DeleteAnnou(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:", "DeleteAnnou", table, "DeleteMany", err), log.Any("ids", ids))
|
||||
return 0, err
|
||||
}
|
||||
return result.DeletedCount, nil
|
||||
}
|
||||
|
||||
// UpdateAnnou 更新公告
|
||||
func UpdateAnnou(id primitive.ObjectID, update map[string]interface{}) (int64, error) {
|
||||
cond := bson.M{"_id": id}
|
||||
result, err := coll(nil).UpdateMany(cond, bson.M{"$set": bson.M(update)})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateAnnou", table, "UpdateMany", err),
|
||||
log.Any("id", id),
|
||||
log.Any("update", update),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
// getTotalCnt 获取查询总数总数
|
||||
func getTotalCnt(cond bson.M) (int64, error) {
|
||||
total, err := coll(nil).Count(cond)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getTotalCnt", table, "Count", err), log.Any("cond", cond))
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetSkipSize 计算跳转
|
||||
func getSkipSize(page, size uint64, cond bson.M) (uint64, uint64, int64, error) {
|
||||
total, err := getTotalCnt(cond)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
totalpages := uint64(math.Ceil(float64(total) / float64(size)))
|
||||
if page > totalpages {
|
||||
page = totalpages
|
||||
}
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
return (page - 1) * size, totalpages, total, nil
|
||||
}
|
||||
|
||||
// GetAnnou 查询公告
|
||||
func GetAnnou(page, size uint64, field string, desc int) ([]*Annou, uint64, int64, error) {
|
||||
cond := bson.M{}
|
||||
sort := bson.D{{Key: field, Value: desc}}
|
||||
skip, totalPages, total, err := getSkipSize(page, size, cond)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(sort).SetSkip(int64(skip)).SetLimit(int64(size))
|
||||
var back []*Annou
|
||||
if err = coll(nil).Find(&back, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnou", table, "Find", err),
|
||||
log.Any("page", page),
|
||||
log.Any("size", size),
|
||||
log.Any("field", field),
|
||||
log.Any("desc", desc),
|
||||
)
|
||||
return nil, totalPages, total, err
|
||||
}
|
||||
return back, totalPages, total, nil
|
||||
}
|
||||
|
||||
// GetAnnouByType 获取指定类型的公告
|
||||
func GetAnnouByType(types int) ([]*Annou, error) {
|
||||
cond := bson.M{"active": true}
|
||||
if types != 2 {
|
||||
cond = bson.M{"type": types, "active": true}
|
||||
}
|
||||
sort := bson.D{{Key: "updatedAt", Value: -1}}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(sort)
|
||||
var back []*Annou
|
||||
if err := coll(nil).Find(&back, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnouByType", table, "Find", err), log.Any("types", types))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
const (
|
||||
TotalType = 1 //系统公告
|
||||
VIPUP = 3 //vip升级公告
|
||||
)
|
||||
|
||||
// GetAnnouForDomain 获取指定类型的公告
|
||||
func GetAnnouForDomain(types int) ([]AnnounInfo, error) {
|
||||
cond := bson.M{"active": true}
|
||||
if types != 2 {
|
||||
cond = bson.M{"type": types, "active": true}
|
||||
}
|
||||
sort := bson.D{{Key: "updatedAt", Value: -1}}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(sort)
|
||||
var back []AnnounInfo
|
||||
if err := coll(nil).Find(&back, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnouForDomain", table, "Find", err), log.Any("types", types))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package annoumod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
var mdbAnnounce *db.MongoDB
|
||||
|
||||
const tableAnnounce = models.Announce
|
||||
|
||||
func collAnnounce(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdbAnnounce.Coll(tableAnnounce)
|
||||
}
|
||||
return t.Coll(tableAnnounce)
|
||||
}
|
||||
|
||||
// InitIndex 初始化索引
|
||||
func initAnnounceIndex() {
|
||||
indexModels := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "active", Value: 1}},
|
||||
Options: options.Index(),
|
||||
},
|
||||
}
|
||||
if _, err := collAnnounce(nil).CreateIndex(indexModels); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", tableAnnounce, err))
|
||||
}
|
||||
}
|
||||
|
||||
// AddAnnounce 增加会员中心跑马灯
|
||||
func AddAnnounce(announce []Announce) error {
|
||||
if _, err := collAnnounce(nil).InsertMany(&announce); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddAnnounce", tableAnnounce, "InsertMany", err), log.Any("announce", announce))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteAnnounce 删除会员中心跑马灯
|
||||
func DeleteAnnounce(ids []primitive.ObjectID) (int64, error) {
|
||||
if ids == nil {
|
||||
ids = []primitive.ObjectID{}
|
||||
}
|
||||
cond := bson.M{"_id": bson.M{"$in": ids}}
|
||||
result, err := collAnnounce(nil).DeleteMany(cond)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteAnnounce", tableAnnounce, "DeleteMany", err), log.Any("ids", ids))
|
||||
return 0, err
|
||||
}
|
||||
return result.DeletedCount, nil
|
||||
}
|
||||
|
||||
// UpdateAnnounce 更新会员中心跑马灯
|
||||
func UpdateAnnounce(id primitive.ObjectID, update map[string]interface{}) (int64, error) {
|
||||
cond := bson.M{"_id": id}
|
||||
result, err := collAnnounce(nil).UpdateMany(cond, bson.M{"$set": bson.M(update)})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateAnnou", tableAnnounce, "UpdateMany", err),
|
||||
log.Any("id", id),
|
||||
log.Any("update", update),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
// GetAnnounce 查询会员中心跑马灯
|
||||
func GetAnnounceApi(field string, desc int) ([]Announce, error) {
|
||||
cond := bson.M{"active": true, "type": 0}
|
||||
sort := bson.D{{Key: field, Value: desc}}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(sort)
|
||||
var back []Announce
|
||||
if err := collAnnounce(nil).Find(&back, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnounce", tableAnnounce, "Find", err),
|
||||
log.Any("field", field),
|
||||
log.Any("desc", desc),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
// GetAnnounce 查询会员中心跑马灯
|
||||
func GetAnnounce(field string, desc int) ([]Announce, error) {
|
||||
cond := bson.M{"type": 0}
|
||||
sort := bson.D{{Key: field, Value: desc}}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(sort)
|
||||
var back []Announce
|
||||
if err := collAnnounce(nil).Find(&back, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnounce", tableAnnounce, "Find", err),
|
||||
log.Any("field", field),
|
||||
log.Any("desc", desc),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package annoumod
|
||||
|
||||
import "go.mongodb.org/mongo-driver/bson/primitive"
|
||||
|
||||
// PopReq 公告列表
|
||||
type PopReq struct {
|
||||
Type int `form:"type" json:"type"` //公告类型,0,2,所有 3会员升级公告
|
||||
}
|
||||
|
||||
// AppRes 公告模型
|
||||
type AnnounInfo struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` //公告id
|
||||
Title string `json:"title" bson:"title"` //公告标题
|
||||
Content string `json:"content" bson:"content"` //公告内容
|
||||
Cover string `json:"cover" bson:"cover"` //公告封面图
|
||||
Href string `json:"href" bson:"href"` //公告跳转地址
|
||||
Type int `json:"type" bson:"type"` //公告类型
|
||||
}
|
||||
|
||||
// MsgListReq 公告列表
|
||||
type MsgListReq struct {
|
||||
PageNumber int `form:"pageNumber" json:"pageNumber"`
|
||||
PageSize int `form:"pageSize" json:"pageSize"`
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package annoumod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// Annou 公告模型
|
||||
type Annou struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` //公告id
|
||||
Title string `json:"title" bson:"title"` //公告标题
|
||||
Content string `json:"content" bson:"content"` //公告内容
|
||||
Cover string `json:"cover" bson:"cover"` //公告封面图
|
||||
Type int `json:"type" bson:"type"` //公告类型,0,外部连接;1,直接安装app
|
||||
Href string `json:"href" bson:"href"` //公告跳转地址
|
||||
Active bool `json:"active" bson:"active"` //激活状态
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //更新时间
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
mdbAnnounce = db.Init(tableAnnounce)
|
||||
initAnnouIndex()
|
||||
initAnnounceIndex()
|
||||
}
|
||||
|
||||
// Announce 会员中心跑马灯模型
|
||||
type Announce struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` //公告id
|
||||
Content string `json:"content" bson:"content"` //跑马灯内容
|
||||
Type int `json:"type" bson:"type"` //跑马灯类型,0,会员中心;
|
||||
Url string `json:"url" bson:"url"` //跳转连接
|
||||
Active bool `json:"active" bson:"active"` //激活状态
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //更新时间
|
||||
}
|
||||
|
||||
const UserCenter = 0 //会员中心
|
||||
@@ -0,0 +1,85 @@
|
||||
package annoumod
|
||||
|
||||
import "91porn-server/models/commod"
|
||||
|
||||
// ListReq 视频列表请求
|
||||
type ListReq struct {
|
||||
Sort string `form:"sort" json:"sort"`
|
||||
Desc int `form:"desc" json:"desc"`
|
||||
commod.Page
|
||||
}
|
||||
|
||||
// EditInfo 更新的内容
|
||||
type EditInfo struct {
|
||||
Title string `form:"title" json:"title"` //公告标题
|
||||
Cover string `form:"cover" json:"cover"` //公告封面图
|
||||
Content string `form:"content" json:"content"` //公告内容
|
||||
Type int `form:"type" json:"type"` //公告类型,0,外部连接;1,直接安装app
|
||||
Href string `form:"href" json:"href"` //公告跳转地址
|
||||
Active bool `form:"active" json:"active"` //激活状态
|
||||
}
|
||||
|
||||
// EditReq 更新公告
|
||||
type EditReq struct {
|
||||
ID string `form:"id" json:"id"`
|
||||
EditInfo
|
||||
}
|
||||
|
||||
// DeleteReq 删除公告
|
||||
type DeleteReq struct {
|
||||
IDs []string `form:"ids" json:"ids"`
|
||||
}
|
||||
|
||||
// AddReq 增加一条公告
|
||||
type AddReq struct {
|
||||
Title string `form:"title" json:"title"` //公告标题
|
||||
Content string `form:"content" json:"content"` //公告内容
|
||||
Cover string `form:"cover" json:"cover"` //公告封面图
|
||||
Type int `form:"type" json:"type"` //公告类型,0,外部连接;1,直接安装app 3,会员升级公告
|
||||
Href string `form:"href" json:"href"` //公告跳转地址
|
||||
Active bool `form:"active" json:"active"` //激活状态
|
||||
}
|
||||
|
||||
// ListResp 列表应答
|
||||
type ListResp struct {
|
||||
AInfos []*Annou `json:"aInfos"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// OpeResp 操作应答
|
||||
type OpeResp struct {
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
// EditInfo 更新的内容
|
||||
type EditAnnounceInfo struct {
|
||||
Content string `form:"content" json:"content"` //跑马灯内容
|
||||
Type int `form:"type" json:"type"` //跑马灯类型
|
||||
Url string `form:"url" json:"url"` //跳转链接
|
||||
Active bool `form:"active" json:"active"` //激活状态
|
||||
}
|
||||
|
||||
// EditReq 更新公告
|
||||
type EditAnnounceReq struct {
|
||||
ID string `form:"id" json:"id"`
|
||||
EditAnnounceInfo
|
||||
}
|
||||
|
||||
// DeleteReq 删除公告
|
||||
type DeleteAnnounceReq struct {
|
||||
IDs []string `form:"ids" json:"ids"`
|
||||
}
|
||||
|
||||
// AddReq 增加一条公告
|
||||
type AddAnnounceReq struct {
|
||||
Content string `form:"content" json:"content,omitempty" binding:"required"` //跑马灯内容
|
||||
Url string `form:"url" json:"url,omitempty"` //跳转链接
|
||||
Active bool `form:"active" json:"active,omitempty"` //激活状态
|
||||
}
|
||||
|
||||
// 3、走马灯通知
|
||||
// 接口:goldegg/announce
|
||||
// AnnounceInfoReq 走马灯通告信息请求
|
||||
type AnnounceInfoReq struct {
|
||||
commod.Page
|
||||
}
|
||||
Reference in New Issue
Block a user