@@ -0,0 +1,265 @@
|
||||
package verifyreportmod
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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.VerifyReport
|
||||
|
||||
// InitIndex 设置index
|
||||
func initIndex() {
|
||||
coll := coll(nil)
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "processingStatus", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "productID", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", 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)
|
||||
}
|
||||
|
||||
// todo app
|
||||
func Insert(f VerifyReport) error {
|
||||
v, err := validInsert(f.UID, f.ProductID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !v.ID.IsZero() {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error: Repeat upload:", "Insert", table, "validInsert"),
|
||||
log.Any("LouFengFeedback", f))
|
||||
return errors.New("Repeat upload")
|
||||
}
|
||||
now := time.Now()
|
||||
f.UpdatedAt = now
|
||||
f.CreatedAt = now
|
||||
if _, err = coll(nil).InsertOne(f); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err), log.Any("LouFengFeedback", f))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validInsert
|
||||
func validInsert(uid uint64, id primitive.ObjectID) (vf VerifyReport, err error) {
|
||||
if err = coll(nil).FindOne(&vf, bson.M{"uid": uid, "productID": id, "processingStatus": ProcessingStatusDefault}); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "validInsert", table, "FindOne", err),
|
||||
log.Any("uid", uid),
|
||||
log.Any("id", id))
|
||||
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByPID 根据id获取验证报告
|
||||
func GetByPID(pid, objectType string, page commod.Page) (v []VerifyReport, hasNext bool, err error) {
|
||||
skip := int64(page.Skip())
|
||||
limit := int64(page.Limit()) + 1
|
||||
opts := options.FindOptions{
|
||||
Skip: &skip,
|
||||
Limit: &limit,
|
||||
Sort: bson.D{{Key: "createdAt", Value: -1}},
|
||||
}
|
||||
productID, err := primitive.ObjectIDFromHex(pid)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByPID", table, "ObjectIDFromHex", err),
|
||||
log.Any("objectType", objectType),
|
||||
log.Any("productID", pid))
|
||||
return
|
||||
}
|
||||
if err = coll(nil).Find(&v, bson.M{"objectType": objectType, "productID": productID, "processingStatus": ProcessingStatusComplete}, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByPID", table, "FindOne", err),
|
||||
log.Any("objectType", objectType),
|
||||
log.Any("productID", pid))
|
||||
return
|
||||
}
|
||||
if uint64(len(v)) > page.PageSize {
|
||||
v = v[:page.PageSize]
|
||||
hasNext = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetVeRport 获取验证报告
|
||||
func GetVeRport(objectType string, page commod.Page) (v []VerifyReport, hasNext bool, err error) {
|
||||
skip := int64(page.Skip())
|
||||
limit := int64(page.Limit()) + 1
|
||||
opts := options.FindOptions{
|
||||
Skip: &skip,
|
||||
Limit: &limit,
|
||||
Sort: bson.D{{Key: "createdAt", Value: -1}},
|
||||
}
|
||||
if err = coll(nil).Find(&v, bson.M{"objectType": objectType, "processingStatus": ProcessingStatusComplete}, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByPID", table, "FindOne", err),
|
||||
log.Any("objectType", objectType))
|
||||
return
|
||||
}
|
||||
if uint64(len(v)) > page.PageSize {
|
||||
v = v[:page.PageSize]
|
||||
hasNext = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByBrokerUID 根据经纪人id获取
|
||||
func GetByBrokerUID(brokerUID uint64, objectType string, page commod.Page) (v []VerifyReport, hasNext bool, err error) {
|
||||
skip := int64(page.Skip())
|
||||
limit := int64(page.Limit()) + 1
|
||||
opts := options.FindOptions{
|
||||
Skip: &skip,
|
||||
Limit: &limit,
|
||||
Sort: bson.D{{Key: "createdAt", Value: -1}},
|
||||
}
|
||||
if err = coll(nil).Find(&v, bson.M{"objectType": objectType, "brokerUID": brokerUID, "processingStatus": ProcessingStatusComplete}, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetByBrokerUID", table, "Find", err),
|
||||
log.Any("objectType", objectType),
|
||||
log.Any("brokerUID", brokerUID))
|
||||
return
|
||||
}
|
||||
if uint64(len(v)) > page.PageSize {
|
||||
v = v[:page.PageSize]
|
||||
hasNext = true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetByBrokerUID 根据经纪人id获取总量
|
||||
func CountByBrokerUID(brokerUID uint64, objectType string) (int64, error) {
|
||||
return coll(nil).Count(bson.M{"objectType": objectType, "brokerUID": brokerUID, "processingStatus": ProcessingStatusComplete})
|
||||
}
|
||||
|
||||
// todo web
|
||||
// StdFind 通用查询
|
||||
func StdFind(q QuerySelector, page commod.Page) (data []VerifyReport, total int64, err error) {
|
||||
data = make([]VerifyReport, 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
|
||||
}
|
||||
createdTime := bson.M{}
|
||||
if q.StartTime != nil {
|
||||
createdTime["$gte"] = q.StartTime
|
||||
}
|
||||
if q.EndTime != nil {
|
||||
createdTime["$lt"] = q.EndTime
|
||||
}
|
||||
if q.TimeString != nil {
|
||||
f[*q.TimeString] = createdTime
|
||||
} else {
|
||||
if len(createdTime) > 0 {
|
||||
f["createdAt"] = createdTime
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Update 修改LouFeng类型
|
||||
func Update(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:", "Update", table, "UpdateOne", err),
|
||||
log.Any("set", set),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update 修改LouFeng类型
|
||||
func FindUpdate(set *EditSelector) (vf VerifyReport, err error) {
|
||||
set.UpdatedAt = time.Now()
|
||||
if err = coll(nil).FindOneAndUpdate(&vf, bson.M{"_id": set.ID}, bson.M{"$set": set}); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateOne", err),
|
||||
log.Any("set", set),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Remove 删除LouFeng类型
|
||||
func Remove(id string) (err error) {
|
||||
OID, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Remove", table, "ObjectIDFromHex", err),
|
||||
log.Any("id", id),
|
||||
)
|
||||
return
|
||||
}
|
||||
if _, err = coll(nil).DeleteOne(bson.M{"_id": OID}); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Remove", table, "DeleteOne", err),
|
||||
log.Any("OID", OID),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// InsertBulket 插入一条数据
|
||||
func InsertBulket(vr []VerifyReportUpsert) (err error) {
|
||||
wm := make([]mongo.WriteModel, len(vr))
|
||||
for i := range vr {
|
||||
filter := bson.M{"productID": vr[i].ProductID}
|
||||
insert, _ := common.ToBsonM(vr[i])
|
||||
update := bson.M{"$set": bson.M{"updatedAt": vr[i].CreatedAt}, "$setOnInsert": insert}
|
||||
wm[i] = mongo.NewUpdateOneModel().
|
||||
SetFilter(filter).
|
||||
SetUpdate(update).
|
||||
SetUpsert(true)
|
||||
}
|
||||
ordered := false
|
||||
opts := options.BulkWriteOptions{
|
||||
Ordered: &ordered,
|
||||
}
|
||||
res, err := coll(nil).Bulk(wm, &opts)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertMany", table, "InsertMany", err))
|
||||
return
|
||||
}
|
||||
fmt.Println("=======res", res.InsertedCount)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user