@@ -0,0 +1,16 @@
|
||||
package locmod
|
||||
|
||||
// CityResp 热门城市
|
||||
type CityResp struct {
|
||||
ID string `json:"id" bson:"_id"`
|
||||
City string `json:"city" bson:"city"`
|
||||
Province string `json:"province" bson:"province"`
|
||||
Hot int `json:"hot" bson:"hot"`
|
||||
Visit int `json:"visit" bson:"visit"`
|
||||
}
|
||||
|
||||
// LInfoResp 位置响应信息
|
||||
type LInfoResp struct {
|
||||
Location
|
||||
PlayCount int `json:"visit,omitempty" bson:"visit,omitempty"` //访问人数
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package locmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/pageopt"
|
||||
"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"
|
||||
)
|
||||
|
||||
// M M from Map
|
||||
type M = bson.M
|
||||
|
||||
// ObjectID ObjectID
|
||||
type ObjectID = primitive.ObjectID
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const table = models.HotCity
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// initIndexHot 设置index
|
||||
func initIndexHot() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "city", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// GetHotCity 获取热门城市
|
||||
func GetHotCity() ([]HotCity, error) {
|
||||
var back []HotCity
|
||||
if err := coll(nil).Find(&back, bson.M{"enable": true}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetHotCity", table, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
// Insert 插入记录
|
||||
func Insert(doc HotCityDoc) error {
|
||||
now := time.Now()
|
||||
doc.CreatedAt = &now
|
||||
doc.UpdatedAt = &now
|
||||
if _, err := coll(nil).InsertOne(doc); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateTo UpdateTo
|
||||
func UpdateTo(id ObjectID, doc HotCityDoc) error {
|
||||
now := time.Now()
|
||||
doc.UpdatedAt = &now
|
||||
update, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateTo", table, "ToBsonM", err),
|
||||
log.Any("id", id),
|
||||
log.Any("doc", doc),
|
||||
)
|
||||
return err
|
||||
}
|
||||
result, err := coll(nil).UpdateOne(M{"_id": id}, bson.M{"$set": update})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateTo", table, "UpdateOne", err),
|
||||
log.Any("id", id),
|
||||
log.Any("doc", doc),
|
||||
)
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount+result.UpsertedCount != 1 {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateTo", table, "UpdateOne", err),
|
||||
log.Any("id", id),
|
||||
log.Any("doc", doc),
|
||||
log.Any("mCount", result.ModifiedCount),
|
||||
log.Any("uCount", result.UpsertedCount),
|
||||
)
|
||||
return fmt.Errorf("wrong update count")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteMany DeleteMany
|
||||
func DeleteMany(idArray []ObjectID) error {
|
||||
if idArray == nil {
|
||||
idArray = []ObjectID{}
|
||||
}
|
||||
if _, err := coll(nil).DeleteMany(M{"_id": M{"$in": idArray}}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteMany", table, "DeleteMany", err), log.Any("idArray", idArray))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HotCityPages 热门城市
|
||||
func HotCityPages(sort bson.D, skip int64, limit int64, matchs ...pageopt.Matcher) (HotCityPage, error) {
|
||||
pipeline := []bson.M{
|
||||
{"$match": pageopt.MergeM(matchs)},
|
||||
{
|
||||
"$facet": bson.M{
|
||||
"total": bson.A{bson.M{"$count": "total"}},
|
||||
"list": bson.A{
|
||||
bson.M{"$sort": sort},
|
||||
bson.M{"$skip": skip},
|
||||
bson.M{"$limit": limit},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$project": bson.M{
|
||||
"total": bson.M{"$arrayElemAt": bson.A{"$total.total", 0}},
|
||||
"list": 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
var page HotCityPage
|
||||
if err := coll(nil).AggregateDecode(&page, pipeline); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "HotCityPages", table, "AggregateDecode", err),
|
||||
log.Any("sort", sort),
|
||||
log.Any("matchs", matchs),
|
||||
)
|
||||
return page, err
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package locmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"91porn-server/common"
|
||||
"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"
|
||||
)
|
||||
|
||||
const tableLoc = models.Location
|
||||
|
||||
// initIndex 初始化索引
|
||||
func initIndexLoc() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "city", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "visit", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := collLoc(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", tableLoc, err))
|
||||
}
|
||||
}
|
||||
|
||||
func collLoc(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(tableLoc)
|
||||
}
|
||||
return t.Coll(tableLoc)
|
||||
}
|
||||
|
||||
// GetVisit 获取城市的访问人数
|
||||
func GetVisit(city string) (int, error) {
|
||||
var l Location
|
||||
if err := collLoc(nil).FindOne(&l, bson.M{"city": city}); err != nil {
|
||||
log.Error("GetVisit error", log.Any("city", city), log.E(err))
|
||||
return 0, err
|
||||
}
|
||||
return l.Visit, nil
|
||||
}
|
||||
|
||||
// IncVisit 增加城市的访问人数
|
||||
func IncVisit(id primitive.ObjectID) error {
|
||||
if _, err := collLoc(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$inc": bson.M{"visit": 1, "fakeVisit": 1}}); err != nil {
|
||||
log.Error("IncVisit error", log.Any("id", id), log.E(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLocationInfoByID 获取城市信息
|
||||
func GetLocationInfoByID(id primitive.ObjectID) (Location, error) {
|
||||
var l Location
|
||||
if err := collLoc(nil).FindOne(&l, bson.M{"_id": id}); err != nil {
|
||||
log.Error("GetLocationInfoByID error", log.Any("id", id), log.E(err))
|
||||
return l, err
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
// GetLocationInfoByIDs 获取城市信息
|
||||
func GetLocationInfoByIDs(ids []primitive.ObjectID) (map[primitive.ObjectID]Location, error) {
|
||||
if ids == nil {
|
||||
ids = []primitive.ObjectID{}
|
||||
}
|
||||
m := make(map[primitive.ObjectID]Location, len(ids))
|
||||
infos := make([]Location, 0, len(m))
|
||||
if err := collLoc(nil).Find(&infos, bson.M{"_id": bson.M{"$in": ids}}); err != nil {
|
||||
log.Error("GetLocationInfoByIDs error", log.Any("ids", ids), log.E(err))
|
||||
return m, err
|
||||
}
|
||||
for _, i := range infos {
|
||||
m[i.ID] = i
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetLocationIDByCity 获取城市id
|
||||
func GetLocationIDByCity(city string) (primitive.ObjectID, error) {
|
||||
var l Location
|
||||
if err := collLoc(nil).FindOne(&l, bson.M{"city": city}); err != nil {
|
||||
log.Error("GetLocationIDByCity error", log.Any("city", city), log.E(err))
|
||||
return l.ID, err
|
||||
}
|
||||
return l.ID, nil
|
||||
}
|
||||
|
||||
// InsertLocationInfo 插入一条未存在的城市信息
|
||||
func InsertLocationInfo(l Location) error {
|
||||
if l.City == "" {
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
m := make(map[string]interface{})
|
||||
m2 := make(map[string]interface{})
|
||||
m["createdAt"] = now
|
||||
m["updatedAt"] = now
|
||||
m["country"] = l.Country
|
||||
m["countryCode"] = l.CountryCode
|
||||
m["province"] = l.Province
|
||||
m["provinceCode"] = l.ProvinceCode
|
||||
m["city"] = l.City
|
||||
m["cityCode"] = l.CityCode
|
||||
m2["address"] = l.Address
|
||||
m2["longitude"] = l.Longitude
|
||||
m2["latitude"] = l.Latitude
|
||||
if _, err := collLoc(nil).UpsertOne(bson.M(m), bson.M{"$set": bson.M(m2)}); err != nil {
|
||||
if !mongo.IsDuplicateKeyError(err) {
|
||||
log.Error("InsertLocationInfo error", log.Any("l", l), log.E(err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindLoactionsByIDs 通过id获取位置信息数组
|
||||
func FindLoactionsByIDs(ids []primitive.ObjectID) ([]Location, error) {
|
||||
if ids == nil {
|
||||
ids = []primitive.ObjectID{}
|
||||
}
|
||||
var lInfos []Location
|
||||
if err := collLoc(nil).Find(&lInfos, bson.M{"_id": bson.M{"$in": ids}}); err != nil {
|
||||
log.Error("FindLoactionsByIDs error", log.Any("ids", ids), log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
return lInfos, nil
|
||||
}
|
||||
|
||||
// UpdateToLocation UpdateToLocation
|
||||
func UpdateToLocation(id ObjectID, doc LocationDoc) error {
|
||||
now := time.Now()
|
||||
doc.UpdatedAt = &now
|
||||
update, err := common.ToBsonM(doc)
|
||||
if err != nil {
|
||||
log.Error("UpdateToLocation ToBsonM error", log.Any("id", id), log.Any("doc", doc), log.E(err))
|
||||
return fmt.Errorf("UpdateToLocation ToBsonM error")
|
||||
}
|
||||
result, err := collLoc(nil).UpdateOne(M{"_id": id}, bson.M{"$set": update})
|
||||
if err != nil {
|
||||
log.Error("UpdateToLocation error", log.Any("id", id), log.Any("doc", doc), log.E(err))
|
||||
return fmt.Errorf("UpdateToLocation error")
|
||||
}
|
||||
if result.ModifiedCount+result.UpsertedCount != 1 {
|
||||
log.Error("UpdateToLocation count error", log.Any("id", id), log.Any("doc", doc), log.Any("mCount",
|
||||
result.ModifiedCount), log.Any("uCount", result.UpsertedCount), log.E(err))
|
||||
return fmt.Errorf("wrong update count")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getLocCnt 获取条件查询总数
|
||||
func getLocCnt(cond bson.M) (int64, error) {
|
||||
total, err := collLoc(nil).Count(cond)
|
||||
if err != nil {
|
||||
log.Error("getTotalCnt error", log.Any("cond", cond), log.E(err))
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// getSkipSize 计算跳转
|
||||
func getSkipSize(page, size uint64, cond bson.M) (uint64, uint64, int64, error) {
|
||||
total, err := getLocCnt(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
|
||||
}
|
||||
|
||||
// GetLocationList 获取城市列表
|
||||
func GetLocationList(city string, page, size uint64) ([]*Location, int64, error) {
|
||||
cond := bson.M{}
|
||||
if len(city) != 0 {
|
||||
cond = bson.M{"city": city}
|
||||
}
|
||||
skip, _, total, err := getSkipSize(page, size, cond)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "createdAt", Value: -1}}).SetSkip(int64(skip)).SetLimit(int64(size))
|
||||
var back []*Location
|
||||
if err = collLoc(nil).Find(&back, cond, &opts); err != nil {
|
||||
log.Error("GetLocationList error", log.Any("page", page), log.Any("size", size), log.Any("city", city), log.E(err))
|
||||
return nil, total, err
|
||||
}
|
||||
return back, total, nil
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package locmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// Location 位置
|
||||
type Location struct {
|
||||
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
|
||||
Longitude string `json:"longitude,omitempty" bson:"longitude,omitempty"` //经度
|
||||
Latitude string `json:"latitude,omitempty" bson:"latitude,omitempty"` //纬度
|
||||
Country string `json:"country,omitempty" bson:"country,omitempty"` //国家
|
||||
CountryCode string `json:"countryCode,omitempty" bson:"countryCode,omitempty"` //国家编码
|
||||
Province string `json:"province,omitempty" bson:"province,omitempty"` //省份
|
||||
ProvinceCode string `json:"provinceCode,omitempty" bson:"provinceCode,omitempty"` //省份编码
|
||||
City string `json:"city,omitempty" bson:"city"` //城市
|
||||
CityCode string `json:"cityCode,omitempty" bson:"cityCode,omitempty"` //城市编码
|
||||
Address string `json:"address,omitempty" bson:"address,omitempty"` //地址
|
||||
Cover string `json:"cover,omitempty" bson:"cover,omitempty"` //封面
|
||||
Visit int `json:"visit,omitempty" bson:"visit,omitempty"` //访问人数
|
||||
FakeVisit int `json:"fakeVisit,omitempty" bson:"fakeVisit,omitempty"` //假访问数
|
||||
SortKey int `json:"sortKey,omitempty" bson:"sortKey,omitempty"` //web列表排序字段
|
||||
CreatedAt time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
type LocationDoc struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Longitude *string `json:"longitude,omitempty" bson:"longitude,omitempty"` //经度
|
||||
Latitude *string `json:"latitude,omitempty" bson:"latitude,omitempty"` //纬度
|
||||
Country *string `json:"country,omitempty" bson:"country,omitempty"` //国家
|
||||
CountryCode *string `json:"countryCode,omitempty" bson:"countryCode,omitempty"` //国家编码
|
||||
Province *string `json:"province,omitempty" bson:"province,omitempty"` //省份
|
||||
ProvinceCode *string `json:"provinceCode,omitempty" bson:"provinceCode,omitempty"` //省份编码
|
||||
City *string `json:"city,omitempty" bson:"city,omitempty" binding:"required"` //城市
|
||||
CityCode *string `json:"cityCode,omitempty" bson:"cityCode,omitempty"` //城市编码
|
||||
Address *string `json:"address,omitempty" bson:"address,omitempty"` //地址
|
||||
Cover *string `json:"cover,omitempty" bson:"cover,omitempty"` //封面
|
||||
Visit *int `json:"visit,omitempty" bson:"visit,omitempty"` //访问人数
|
||||
FakeVisit *int `json:"fakeVisit,omitempty" bson:"fakeVisit,omitempty"` //假访问数
|
||||
SortKey *int `json:"sortKey,omitempty" bson:"sortKey,omitempty"` //web列表排序字段
|
||||
CreatedAt *time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt *time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
type HotCity struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
City string `json:"city,omitempty" bson:"city,omitempty"` //城市
|
||||
Province string `json:"province,omitempty" bson:"province,omitempty"` //省份
|
||||
SortKey int `json:"sortKey,omitempty" bson:"sortKey,omitempty"` //排序
|
||||
Enable bool `json:"enable,omitempty" bson:"enable,omitempty"` //使能
|
||||
CreatedAt time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
type HotCityDoc struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
City *string `json:"city,omitempty" bson:"city,omitempty"` //城市
|
||||
Province *string `json:"province,omitempty" bson:"province,omitempty"` //省份
|
||||
SortKey *int `json:"sortKey,omitempty" bson:"sortKey,omitempty"` //排序
|
||||
Enable *bool `json:"enable,omitempty" bson:"enable,omitempty"` //使能
|
||||
CreatedAt *time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt *time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndexLoc()
|
||||
initIndexHot()
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package locmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/models/commod"
|
||||
)
|
||||
|
||||
// CityPageReq 城市列表
|
||||
type CityPageReq struct {
|
||||
City string `form:"city" json:"city" bson:"city,omitempty"`
|
||||
commod.Page
|
||||
}
|
||||
|
||||
// LocInfo 位置信息
|
||||
type LocInfo struct {
|
||||
ID string `json:"id"`
|
||||
City string `json:"city"` //城市
|
||||
Cover string `json:"cover"` //封面
|
||||
Visit int `json:"visit"` //访问人数
|
||||
FakeVisit int `json:"fakeVisit"` //假访问数
|
||||
SortKey int `json:"sortKey"` //web列表排序字段
|
||||
CreatedAt time.Time `json:"createdAt"` //创建时间
|
||||
VideoCount int `json:"videoCount"` //播放量
|
||||
PlayCount int `json:"playCount"` //播放总数
|
||||
}
|
||||
|
||||
// CityPageResp 城市列表应答
|
||||
type CityPageResp struct {
|
||||
List []LocInfo `json:"list"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
// HotCityPage HotCityPage
|
||||
type HotCityPage struct {
|
||||
Total int `json:"total" bson:"total"`
|
||||
List []HotCityDoc `json:"list" bson:"list"`
|
||||
}
|
||||
Reference in New Issue
Block a user