479 lines
12 KiB
Go
479 lines
12 KiB
Go
package fictionmod
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"strconv"
|
|
"time"
|
|
|
|
"91porn-server/app/appg"
|
|
"91porn-server/app/service/searcher"
|
|
"91porn-server/common"
|
|
"91porn-server/common/constant/redisconst"
|
|
"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.Fiction
|
|
|
|
// InitIndex 设置index
|
|
func initIndex() {
|
|
coll := coll(nil)
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "number", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "isActive", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "fType", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "title", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
func GetByID(id primitive.ObjectID) (f FictionAppRes, err error) {
|
|
redisKey := redisconst.DataCachKey(table, id.Hex())
|
|
redisc := appg.Redis
|
|
if redisc == nil || !redisc.Exists(redisKey) {
|
|
if err = coll(nil).FindOne(&f, 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
|
|
}
|
|
if redisc == nil {
|
|
return
|
|
}
|
|
var jsonBytes []byte
|
|
jsonBytes, err = json.Marshal(&f)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = redisc.Set(redisKey, string(jsonBytes), redisconst.DataCachExpire)
|
|
return
|
|
}
|
|
str, err := redisc.Get(redisKey)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if str == nil {
|
|
err = errors.New("redis key is null")
|
|
return
|
|
}
|
|
err = json.Unmarshal([]byte(*str), &f)
|
|
return
|
|
}
|
|
|
|
// todo app
|
|
func GetInIds(ids []primitive.ObjectID) (res []FictionAppRes, err error) {
|
|
data := make([]FictionAppRes, 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]FictionAppRes)
|
|
for i := range data {
|
|
tempM[data[i].ID] = data[i]
|
|
}
|
|
res = make([]FictionAppRes, len(ids))
|
|
for i := range ids {
|
|
res[i] = tempM[ids[i]]
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetByFType(fType string, page commod.Page) (data []FictionBase, hasNext bool, err error) {
|
|
data = make([]FictionBase, 0)
|
|
skip := int64(page.Skip())
|
|
limit := int64(page.Limit() + 1)
|
|
sortWord := "createdAt"
|
|
opts := options.FindOptions{
|
|
Skip: &skip,
|
|
Limit: &limit,
|
|
Sort: bson.D{{Key: sortWord, Value: -1}},
|
|
}
|
|
f := bson.M{"isActive": true}
|
|
if fType != "" {
|
|
f["fType"] = fType
|
|
}
|
|
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
|
|
}
|
|
if uint64(len(data)) > page.PageSize {
|
|
hasNext = true
|
|
data = data[:page.PageSize]
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetFTypes() (data []interface{}, err 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, err
|
|
}
|
|
jsonBytes, err := json.Marshal(data)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
_ = redisc.Set(redisKey, string(jsonBytes), time.Minute*10)
|
|
return data, err
|
|
}
|
|
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")
|
|
}
|
|
err = json.Unmarshal([]byte(*str), &data)
|
|
return
|
|
}
|
|
|
|
func GetHots(page commod.Page) (data []FictionBase, hasNext bool, err error) {
|
|
data = make([]FictionBase, 0)
|
|
skip := int64(page.Skip())
|
|
limit := int64(page.Limit() + 1)
|
|
sortWord := "countCollect"
|
|
opts := options.FindOptions{
|
|
Skip: &skip,
|
|
Limit: &limit,
|
|
Sort: bson.D{{Key: sortWord, 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:", "GetByFType", table, "Find", err), log.Any("cond", f))
|
|
return
|
|
}
|
|
if uint64(len(data)) > page.PageSize {
|
|
hasNext = true
|
|
data = data[:page.PageSize]
|
|
}
|
|
return
|
|
}
|
|
|
|
func SearchTitle(title string, page commod.Page) (data []FictionAppRes, hasNext bool, err error) {
|
|
data = make([]FictionAppRes, 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{"isActive": true}
|
|
f["$or"] = bson.A{
|
|
bson.M{"title": primitive.Regex{
|
|
Pattern: title,
|
|
Options: "",
|
|
}},
|
|
}
|
|
if err = coll(nil).Find(&data, f, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "SearchTitle", 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) (err 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
|
|
}
|
|
if res.ModifiedCount <= 0 {
|
|
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), log.Any("res", res))
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetNew(opt searcher.Opter) (data []FictionBase, hasNext bool, err error) {
|
|
data = make([]FictionBase, 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 []Fiction, total int64, err error) {
|
|
data = make([]Fiction, 0)
|
|
skip := int64(page.Skip())
|
|
limit := int64(page.Limit())
|
|
opts := options.FindOptions{
|
|
Skip: &skip,
|
|
Limit: &limit,
|
|
}
|
|
sortD := bson.D{}
|
|
for i := range page.Sort {
|
|
sortD = append(sortD, bson.E{Key: page.Sort[i].SortKey, Value: page.Sort[i].SortVal})
|
|
}
|
|
sortD = append(sortD, bson.E{Key: "createdAt", Value: -1})
|
|
opts.SetSort(sortD)
|
|
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
|
|
}
|
|
|
|
// InsertFiction 插入一条数据
|
|
func InsertFiction(p *Fiction) (err error) {
|
|
p.CreatedAt = time.Now()
|
|
_, err = coll(nil).InsertOne(p)
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertFiction", table, "InsertOne", err))
|
|
}
|
|
return
|
|
}
|
|
|
|
// InsertBulket 插入一条数据
|
|
func InsertBulket(p []FictionUpsert) (err error) {
|
|
fmt.Println("------------", len(p))
|
|
now := time.Now()
|
|
wm := make([]mongo.WriteModel, len(p))
|
|
for i := range p {
|
|
filter := bson.M{"number": p[i].Number}
|
|
update := bson.M{"$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
|
|
}
|
|
|
|
// InsertFiction 插入一条数据
|
|
func InsertMany(p []Fiction) (err 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
|
|
}
|
|
|
|
// UpdateFiction 修改Fiction类型
|
|
func UpdateFiction(set *EditSelector) (err 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:", "UpdateFiction", table, "UpdateOne", err),
|
|
log.Any("set", set),
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
// RemoveFiction 删除Fiction类型
|
|
func RemoveFiction(id string) error {
|
|
OID, err := primitive.ObjectIDFromHex(id)
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "RemoveFiction", 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:", "RemoveFiction", table, "DeleteOne", err),
|
|
log.Any("OID", OID),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// todo skd
|
|
// 获取ES同步数据
|
|
func GetListByUpdateTimeRange(start time.Time, end time.Time) (data []Fiction, 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
|
|
}
|
|
|
|
func GetRandomPush(size int) (data []FictionBase, err error) {
|
|
data = make([]FictionBase, 0)
|
|
redisK := "tempSet:" + RedisSetKey + ":randomData"
|
|
redisc := appg.Redis
|
|
if redisc == nil || !redisc.Exists(redisK) {
|
|
f := bson.M{"isActive": true}
|
|
var total int64
|
|
total, err = randomCount(f)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var limit int64 = 100
|
|
var skip int64 = 0
|
|
if total-limit > limit {
|
|
skip = rand.Int63n(total - limit)
|
|
}
|
|
opts := options.FindOptions{
|
|
Skip: &skip,
|
|
Limit: &limit,
|
|
}
|
|
if err = coll(nil).Find(&data, f, &opts); err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetRandomPush", table, "Find", err), log.Any("filter", f))
|
|
return
|
|
}
|
|
if len(data) < 1 {
|
|
return
|
|
}
|
|
if redisc == nil {
|
|
return
|
|
}
|
|
tempd := make([]string, len(data))
|
|
for i := range data {
|
|
var strB []byte
|
|
strB, err = json.Marshal(data[i])
|
|
if err != nil {
|
|
return
|
|
}
|
|
tempd[i] = string(strB)
|
|
}
|
|
if _, err = redisc.SAdd(redisK, tempd); err != nil {
|
|
return
|
|
}
|
|
_, _ = redisc.ExpireKey(redisK, time.Minute*10)
|
|
}
|
|
tempd, err := redisc.SRandMemberN(redisK, int64(size))
|
|
if len(tempd) < 1 {
|
|
return data, errors.New("redis key is null")
|
|
}
|
|
data = make([]FictionBase, len(tempd))
|
|
for i := range data {
|
|
_ = json.Unmarshal([]byte(tempd[i]), &data[i])
|
|
}
|
|
return
|
|
}
|
|
|
|
func randomCount(f bson.M) (count int64, err error) {
|
|
redisK := "temp:" + RedisSetKey + ":randomCount"
|
|
redisc := appg.Redis
|
|
if redisc == nil || !redisc.Exists(redisK) {
|
|
count, err = coll(nil).Count(f)
|
|
if err != nil {
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetCitys", table, "Distinct", err))
|
|
return
|
|
}
|
|
if redisc == nil {
|
|
return
|
|
}
|
|
var jsonBytes []byte
|
|
jsonBytes, err = json.Marshal(count)
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = redisc.Set(redisK, string(jsonBytes), time.Minute*10)
|
|
}
|
|
ptrV, err := redisc.Get(redisK)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if ptrV != nil {
|
|
count, err = strconv.ParseInt(*ptrV, 10, 64)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func GetListByCond(cond bson.M, opts *options.FindOptions) ([]Fiction, error) {
|
|
var data []Fiction
|
|
if err := coll(nil).Find(&data, cond, opts); err != nil {
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|