@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user