@@ -0,0 +1,323 @@
|
||||
package audioanchormod
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/app/appg"
|
||||
"91porn-server/app/service/searcher"
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
const table = models.AudioAnchor
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func GetByID(id primitive.ObjectID) (loufeng AudioAnchor, err error) {
|
||||
if err = coll(nil).FindOne(&loufeng, bson.M{"_id": id}); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByID", table, "FindOne", err), log.Any("id", id.Hex()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// todo app
|
||||
func GetInIds(ids []primitive.ObjectID) (res []AudioAnchor, err error) {
|
||||
data := make([]AudioAnchor, 0)
|
||||
f := bson.M{}
|
||||
if ids != nil {
|
||||
f["_id"] = bson.M{"$in": ids}
|
||||
}
|
||||
if err = coll(nil).Find(&data, f); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInIds", table, "Find", err), log.Any("cond", f))
|
||||
return
|
||||
}
|
||||
tempM := make(map[primitive.ObjectID]AudioAnchor)
|
||||
for i := range data {
|
||||
tempM[data[i].ID] = data[i]
|
||||
}
|
||||
res = make([]AudioAnchor, len(ids))
|
||||
for i := range ids {
|
||||
res[i] = tempM[ids[i]]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetRandomPush(size int) (data []AudioAnchor, err error) {
|
||||
data = make([]AudioAnchor, 0)
|
||||
f := bson.M{"name": bson.M{"$gt": ""}}
|
||||
if err = coll(nil).Find(&data, f); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetRandomPush", table, "Aggregate", err), log.Any("filter", f))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetByName(name string) (data AudioAnchor, err error) {
|
||||
f := bson.M{"name": name}
|
||||
if err = coll(nil).FindOne(&data, f); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByFType", table, "Find", err), log.Any("cond", f))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetFTypes() ([]interface{}, error) {
|
||||
data := make([]interface{}, 0)
|
||||
redisKey := RedisSetKey + ":fType"
|
||||
redisc := appg.Redis
|
||||
if redisc == nil || !redisc.Exists(redisKey) {
|
||||
data, err := coll(nil).Distinct("fType", bson.M{})
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetCitys", table, "Distinct", err))
|
||||
return nil, err
|
||||
}
|
||||
if redisc == nil {
|
||||
return data, nil
|
||||
}
|
||||
jsonBytes, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
_ = redisc.Set(redisKey, string(jsonBytes), time.Minute*10)
|
||||
return data, nil
|
||||
}
|
||||
str, err := redisc.Get(redisKey)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetFTypes", table, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
if str == nil {
|
||||
return data, errors.New("redis key is null")
|
||||
}
|
||||
return data, json.Unmarshal([]byte(*str), &data)
|
||||
}
|
||||
|
||||
func GetSortWord(page commod.Page, sortWord string) ([]AudioAnchor, bool, error) {
|
||||
data := make([]AudioAnchor, 0)
|
||||
skip := int64(page.Skip())
|
||||
limit := int64(page.Limit() + 1)
|
||||
opts := options.FindOptions{
|
||||
Skip: &skip,
|
||||
Limit: &limit,
|
||||
Sort: bson.D{{Key: sortWord, Value: -1}},
|
||||
}
|
||||
f := bson.M{"name": bson.M{"$gt": ""}}
|
||||
if err := coll(nil).Find(&data, f, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByFType", table, "Find", err), log.Any("cond", f))
|
||||
return data, false, err
|
||||
}
|
||||
hasNext := false
|
||||
if uint64(len(data)) > page.PageSize {
|
||||
hasNext = true
|
||||
data = data[:page.PageSize]
|
||||
}
|
||||
return data, hasNext, nil
|
||||
}
|
||||
|
||||
func SearchName(name string, page commod.Page) (data []AudioAnchorAppRes, hasNext bool, err error) {
|
||||
data = make([]AudioAnchorAppRes, 0)
|
||||
skip := int64(page.Skip())
|
||||
limit := int64(page.Limit() + 1)
|
||||
opts := options.FindOptions{
|
||||
Skip: &skip,
|
||||
Limit: &limit,
|
||||
Sort: bson.D{{Key: "createdAt", Value: 1}},
|
||||
}
|
||||
f := bson.M{}
|
||||
f["$or"] = bson.A{
|
||||
bson.M{"name": primitive.Regex{
|
||||
Pattern: name,
|
||||
Options: "",
|
||||
}},
|
||||
}
|
||||
if err = coll(nil).Find(&data, f, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "SearchName", table, "Find", err), log.Any("cond", f))
|
||||
return
|
||||
}
|
||||
if uint64(len(data)) > page.PageSize {
|
||||
hasNext = true
|
||||
data = data[:page.PageSize]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func IncCount(id primitive.ObjectID, inc CountInc) error {
|
||||
res, err := coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$inc": inc})
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IncCount", table, "UpdateOne", err), log.Any("id", id), log.Any("inc", inc))
|
||||
return err
|
||||
}
|
||||
if res.ModifiedCount <= 0 {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s none", "IncCount", table, "UpdateOne"), log.Any("id", id), log.Any("inc", inc), log.Any("res", res))
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetNew(opt searcher.Opter) (data []AudioAnchorAppRes, hasNext bool, err error) {
|
||||
data = make([]AudioAnchorAppRes, 0)
|
||||
skip := opt.Skip()
|
||||
limit := opt.Limit()
|
||||
opts := options.FindOptions{
|
||||
Skip: &skip,
|
||||
Limit: &limit,
|
||||
Sort: bson.D{{Key: "createdAt", Value: -1}},
|
||||
}
|
||||
f := bson.M{"isActive": true}
|
||||
if err = coll(nil).Find(&data, f, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetNew", table, "Find", err), log.Any("cond", f))
|
||||
return
|
||||
}
|
||||
if int64(len(data)) > limit {
|
||||
hasNext = true
|
||||
data = data[:limit]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// todo web
|
||||
// StdFind 通用查询
|
||||
func StdFind(q QuerySelector, page commod.Page) (data []AudioAnchor, total int64, err error) {
|
||||
data = make([]AudioAnchor, 0)
|
||||
skip := int64(page.Skip())
|
||||
limit := int64(page.Limit())
|
||||
opts := options.FindOptions{
|
||||
Skip: &skip,
|
||||
Limit: &limit,
|
||||
Sort: bson.D{{Key: "createdAt", Value: -1}},
|
||||
}
|
||||
f, err := common.ToBsonM(q)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "StdFind", table, "ToBsonM", err), log.Any("cond", f))
|
||||
return
|
||||
}
|
||||
if q.Title != nil {
|
||||
f["title"] = primitive.Regex{
|
||||
Pattern: *q.Title,
|
||||
Options: "",
|
||||
}
|
||||
}
|
||||
if err = coll(nil).Find(&data, f, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "StdFind", table, "Find", err), log.Any("cond", f))
|
||||
return
|
||||
}
|
||||
total, err = coll(nil).Count(f)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "StdFind", table, "Count", err), log.Any("cond", f))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// IsExisted 查询
|
||||
func IsExisted(id primitive.ObjectID) (ok bool, err error) {
|
||||
ok, err = coll(nil).Exists(bson.M{"_id": id})
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IsExisted", table, "Exists", err), log.Any("id", id))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// InsertAudioAnchor 插入一条数据
|
||||
func InsertAudioAnchor(p *AudioAnchor) error {
|
||||
p.CreatedAt = time.Now()
|
||||
if _, err := coll(nil).InsertOne(p); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertAudioAnchor", table, "InsertOne", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertBulket 插入一条数据
|
||||
func InsertBulket(p []EditSelector) (err error) {
|
||||
fmt.Println("------------", len(p))
|
||||
now := time.Now()
|
||||
wm := make([]mongo.WriteModel, len(p))
|
||||
for i := range p {
|
||||
filter := bson.M{"name": p[i].Name}
|
||||
update := bson.M{"$inc": bson.M{"totalRaido": 1}, "$set": p[i], "$setOnInsert": bson.M{"createdAt": now}}
|
||||
wm[i] = mongo.NewUpdateOneModel().
|
||||
SetFilter(filter).
|
||||
SetUpdate(update).
|
||||
SetUpsert(true)
|
||||
}
|
||||
ordered := false
|
||||
opts := options.BulkWriteOptions{
|
||||
Ordered: &ordered,
|
||||
}
|
||||
if _, err = coll(nil).Bulk(wm, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertMany", table, "InsertMany", err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// InsertAudioAnchor 插入一条数据
|
||||
func InsertMany(p []AudioAnchor) error {
|
||||
if _, err := coll(nil).InsertMany(p); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertMany", table, "InsertMany", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateAudioAnchor 修改AudioAnchor类型
|
||||
func UpdateAudioAnchor(set *EditSelector) error {
|
||||
set.UpdatedAt = time.Now()
|
||||
if _, err := coll(nil).UpdateOne(bson.M{"_id": set.ID}, bson.M{"$set": set}); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateAudioAnchor", table, "UpdateOne", err),
|
||||
log.Any("set", set),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveAudioAnchor 删除AudioAnchor类型
|
||||
func RemoveAudioAnchor(id string) error {
|
||||
OID, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "RemoveAudioAnchor", table, "ObjectIDFromHex", err),
|
||||
log.Any("id", id),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if _, err = coll(nil).DeleteOne(bson.M{"_id": OID}); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "RemoveAudioAnchor", table, "DeleteOne", err),
|
||||
log.Any("OID", OID),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// todo skd
|
||||
// 获取ES同步数据
|
||||
func GetListByUpdateTimeRange(start time.Time, end time.Time) (data []AudioAnchor, err error) {
|
||||
var query = bson.M{
|
||||
"updatedAt": bson.M{"$gte": start, "$lt": end},
|
||||
}
|
||||
if err = coll(nil).Find(&data, query); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetListByUpdateTimeRange", table, "Find", err),
|
||||
log.Any("start", start),
|
||||
log.Any("end", end),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package audioanchormod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/elastic"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
)
|
||||
|
||||
var es *elastic.Client
|
||||
|
||||
const ESTable = models.ESInfoLouFengTable
|
||||
|
||||
func InitESIndex() {
|
||||
es = elastic.Init()
|
||||
var setting = elastic.M{
|
||||
"settings": elastic.M{
|
||||
"number_of_shards": elastic.NumberOfShards,
|
||||
"number_of_replicas": elastic.NumberOfReplicas,
|
||||
"analysis": elastic.M{
|
||||
"analyzer": elastic.M{
|
||||
"ik": elastic.M{
|
||||
"tokenizer": elastic.AnalyzerIkMaxWord,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"mappings": elastic.M{
|
||||
"properties": elastic.M{
|
||||
"age": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkMaxWord,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
"price": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkMaxWord,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
|
||||
"title": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkMaxWord,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
"city": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkMaxWord,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
"district": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkMaxWord,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
"impression": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkMaxWord,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
"contact": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkSmart,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
"serviceItems": elastic.M{
|
||||
"type": "text",
|
||||
"analyzer": elastic.AnalyzerIkMaxWord,
|
||||
"search_analyzer": elastic.AnalyzerIkSmart,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := es.CreateIndices(ESTable, setting); err != nil {
|
||||
panic(fmt.Sprintf("%s index indeices err ==>[%+v]", ESTable, err))
|
||||
}
|
||||
}
|
||||
|
||||
func Search(keywords string, from int64, size int64) (data []ESUserSource, err error) {
|
||||
query := elastic.M{
|
||||
"query": elastic.M{
|
||||
"bool": elastic.M{
|
||||
"must": elastic.A{
|
||||
{"multi_match": elastic.M{
|
||||
"query": keywords,
|
||||
"fields": []string{"city", "impression", "title", "serviceItems", "price"}},
|
||||
},
|
||||
{"term": elastic.M{"isActive": true}},
|
||||
},
|
||||
},
|
||||
},
|
||||
"from": from,
|
||||
"size": size,
|
||||
}
|
||||
if err = es.Search(ESTable, &data, query); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Search", table, "CommonSearch", err),
|
||||
log.Any("keywords", keywords),
|
||||
log.Any("from", from),
|
||||
log.Any("size", size),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package audioanchormod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
type AudioAnchor struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Name string `json:"name" bson:"name"` //名字
|
||||
Avatar string `json:"avatar" bson:"avatar"` //头像
|
||||
TotalRaido int `json:"totalRaido" bson:"totalRaido"` //作品总量
|
||||
CountCollect int64 `json:"countCollect" bson:"countCollect"` //收藏数
|
||||
IsCollect bool `json:"isCollect" bson:"-"`
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //修改时间
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
}
|
||||
|
||||
type QuerySelector struct {
|
||||
ID *primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
|
||||
Title *string `json:"title,omitempty" bson:"title,omitempty"` //标题
|
||||
FType *string `json:"fType,omitempty" bson:"fType,omitempty"` //小说类型
|
||||
Summary *string `json:"summary,omitempty" bson:"summary,omitempty"` //简介
|
||||
ContentURL *string `json:"contentUrl,omitempty" bson:"contentUrl,omitempty"` //内容地址
|
||||
IsActive *bool `json:"isActive,omitempty" bson:"isActive,omitempty"` //是否激活
|
||||
Number *int `json:"number,omitempty" bson:"number,omitempty"` //编号
|
||||
}
|
||||
|
||||
type EditSelector struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Name string `json:"name" bson:"name"` //名字
|
||||
Avatar string `json:"avatar" bson:"avatar,omitempty"` //头像
|
||||
Number int `json:"number" bson:"number,omitempty"` //编号
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` //更新时间
|
||||
}
|
||||
|
||||
const RedisSetKey = "fiction"
|
||||
|
||||
// todo app
|
||||
type AudioAnchorAppRes struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Title string `json:"title" bson:"title,omitempty"` //标题
|
||||
FType string `json:"fType" bson:"fType"` //小说类型
|
||||
Summary string `json:"summary" bson:"summary"` //简介
|
||||
ContentURL string `json:"contentUrl" bson:"contentUrl"` //内容地址
|
||||
IsActive bool `json:"isActive" bson:"isActive"` //是否激活
|
||||
CountPurchases int64 `json:"countPurchases" bson:"countPurchases"` //购买数
|
||||
CountBrowse int64 `json:"countBrowse" bson:"countBrowse"` //浏览数
|
||||
CountCollect int64 `json:"countCollect" bson:"countCollect"` //收藏数
|
||||
IsCollect bool `json:"isCollect"` //是否收藏
|
||||
}
|
||||
|
||||
type CountInc struct {
|
||||
CountCollect *int `json:"countCollect" bson:"countCollect,omitempty"` //收藏数
|
||||
}
|
||||
|
||||
type ESUserSource struct {
|
||||
ID string `json:"_id"`
|
||||
Source AudioAnchorAppRes `json:"_source"`
|
||||
}
|
||||
Reference in New Issue
Block a user