@@ -0,0 +1,169 @@
|
||||
package adreviewmod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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/mongo"
|
||||
)
|
||||
|
||||
const table = models.AdReview
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InitIndex 初始化索引
|
||||
func initAdsIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "reviewStatus", Value: 1}, {Key: "createdAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "districtCode", Value: 1}, {Key: "createdAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "passAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "refussAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "optTypeAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOne 增加广告
|
||||
func InsertOne(t *db.MongoTool, doc InsertDoc) error {
|
||||
if doc.CreatedAt.IsZero() {
|
||||
doc.CreatedAt = time.Now()
|
||||
}
|
||||
if doc.ApplyAt.IsZero() {
|
||||
doc.ApplyAt = time.Now()
|
||||
}
|
||||
if _, err := coll(t).InsertOne(doc); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertOne", table, "InsertMany", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var processong = Processong
|
||||
|
||||
func updateOne(doc UpdateDoc, mats ...Matcher) error {
|
||||
var d struct {
|
||||
Doc UpdateDoc `bson:",inline"`
|
||||
ReviewStatus *ReviewStatus `bson:"reviewStatus,omitempty"` //审核状态
|
||||
}
|
||||
d.Doc = doc
|
||||
filter := pageopt.MergeM(mats)
|
||||
now := time.Now()
|
||||
if doc.OptType != nil {
|
||||
doc.ApplyAt = &now
|
||||
}
|
||||
if doc.Title != nil || doc.Cover != nil || doc.HrefType != nil || doc.Href != nil || doc.Position != nil || doc.SortCode != nil {
|
||||
d.ReviewStatus = &processong
|
||||
}
|
||||
if doc.UpdatedAt.IsZero() {
|
||||
doc.UpdatedAt = now
|
||||
}
|
||||
res, err := coll(nil).UpdateOne(filter, bson.M{"$set": d})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateAds", table, "UpdateOne", err), log.Any("doc", doc), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
if res.MatchedCount == 0 {
|
||||
return ADReviewNotExistError{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateOneByID(id ObjectID, doc UpdateDoc) error {
|
||||
return updateOne(doc, (&IDMatch{ID: &id}).New())
|
||||
}
|
||||
|
||||
func DeleteOne(t *db.MongoTool, id ObjectID) error {
|
||||
if _, err := coll(t).DeleteById(id); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteOne", table, "DeleteById", err))
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return ADReviewNotExistError{}
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateReview(review Review, now time.Time, mats ...Matcher) error {
|
||||
var doc struct {
|
||||
Review `bson:",inline"`
|
||||
UpdatedAt time.Time `bson:"updatedAt"` //更新时间
|
||||
}
|
||||
doc.Review = review
|
||||
if doc.ReviewStatus == Pass {
|
||||
doc.PassAt = now
|
||||
}
|
||||
if doc.ReviewStatus == Refuse {
|
||||
doc.RefussAt = now
|
||||
}
|
||||
doc.UpdatedAt = now
|
||||
filter := pageopt.MergeM(mats)
|
||||
res, err := coll(nil).UpdateOne(filter, bson.M{"$set": doc})
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateAds", table, "UpdateOne", err), log.Any("doc", doc), log.Any("filter", filter))
|
||||
return err
|
||||
}
|
||||
if res.MatchedCount == 0 {
|
||||
return ADReviewNotExistError{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateReviewByID(id ObjectID, curStatus ReviewStatus, targetStatus ReviewStatus, adID *ObjectID, reviewer string, receiptInfo string, now time.Time) error {
|
||||
review := Review{
|
||||
Reviewer: reviewer,
|
||||
ReviewStatus: targetStatus,
|
||||
ReceiptInfo: receiptInfo,
|
||||
}
|
||||
if adID != nil {
|
||||
review.ADID = *adID
|
||||
}
|
||||
mats := []Matcher{
|
||||
(&IDMatch{ID: &id}).New(),
|
||||
(&ReviewStatusMatch{&curStatus}).New(),
|
||||
}
|
||||
return updateReview(review, now, mats...)
|
||||
}
|
||||
|
||||
func findOne(mats ...Matcher) (AdReview, error) {
|
||||
filter := pageopt.MergeM(mats)
|
||||
ads := AdReview{}
|
||||
if err := coll(nil).FindOne(&ads, filter); err != nil {
|
||||
return AdReview{}, err
|
||||
}
|
||||
if ads.ID.IsZero() {
|
||||
return AdReview{}, ADReviewNotExistError{}
|
||||
}
|
||||
return ads, nil
|
||||
}
|
||||
|
||||
func FindOneByID(id ObjectID) (AdReview, error) {
|
||||
return findOne((&IDMatch{ID: &id}).New())
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package adreviewmod
|
||||
|
||||
// ADReviewNotExistError
|
||||
type ADReviewNotExistError struct {
|
||||
}
|
||||
|
||||
func (ADReviewNotExistError) Error() string {
|
||||
return "review is not exist"
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package adreviewmod
|
||||
|
||||
import (
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/pageopt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type (
|
||||
AdsSlice []AdReview
|
||||
|
||||
Matcher = pageopt.Matcher
|
||||
|
||||
// ReviewStatusMatch
|
||||
ReviewStatusMatch struct {
|
||||
ReviewStatus *ReviewStatus
|
||||
}
|
||||
|
||||
// DistrictCodeMatch
|
||||
DistrictCodeMatch struct {
|
||||
DistrictCode *string
|
||||
}
|
||||
|
||||
IDMatch = pageopt.IDMatch
|
||||
|
||||
Sort = bson.D
|
||||
)
|
||||
|
||||
func (a *ReviewStatusMatch) New() Matcher {
|
||||
return pageopt.NewAssignMatch("reviewStatus", a.ReviewStatus)
|
||||
}
|
||||
|
||||
func (c *DistrictCodeMatch) New() Matcher {
|
||||
return pageopt.NewAssignMatch("districtCode", c.DistrictCode)
|
||||
}
|
||||
|
||||
var Sort_createdAt_N1 = Sort{{Key: "createdAt", Value: -1}}
|
||||
|
||||
func List(sort Sort, skip, limit *int64, matchers ...Matcher) (AdsSlice, error) {
|
||||
filter := pageopt.MergeM(matchers)
|
||||
opt := (&options.FindOptions{})
|
||||
if len(sort) != 0 {
|
||||
opt.SetSort(sort)
|
||||
}
|
||||
if skip != nil {
|
||||
opt.SetSkip(*skip)
|
||||
}
|
||||
if limit != nil {
|
||||
opt.SetLimit(*limit)
|
||||
}
|
||||
list := AdsSlice{}
|
||||
if err := coll(nil).Find(&list, filter, opt); err != nil {
|
||||
log.Error("adsmod List error", log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func Count(matchers ...Matcher) (int64, error) {
|
||||
filter := pageopt.MergeM(matchers)
|
||||
count, err := coll(nil).Count(filter)
|
||||
if err != nil {
|
||||
log.Error("adsmod List error", log.E(err))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package adreviewmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/ysurl"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const (
|
||||
ADD OptType = iota //添加
|
||||
Modify //修改
|
||||
Delete //删除
|
||||
)
|
||||
|
||||
const (
|
||||
Processong ReviewStatus = iota // 审核中
|
||||
Pass // 通过
|
||||
Refuse // 拒绝
|
||||
)
|
||||
|
||||
type (
|
||||
ObjectID = primitive.ObjectID
|
||||
|
||||
// 操作类型
|
||||
OptType int
|
||||
|
||||
// RUL跳转类型
|
||||
URLJumpType = ysurl.URLJumpType
|
||||
|
||||
// 审核状态
|
||||
ReviewStatus int
|
||||
|
||||
// Operation 操作信息
|
||||
Operation struct {
|
||||
OptType OptType `bson:"optType"` //操作类型
|
||||
ApplyAt time.Time `bson:"applyAt"` //申请时间
|
||||
}
|
||||
|
||||
Review struct {
|
||||
ADID ObjectID `bson:"adID,omitempty"` //广告id
|
||||
PassAt time.Time `bson:"passAt,omitempty"` //通过时间
|
||||
RefussAt time.Time `bson:"refussAt,omitempty"` //拒绝时间
|
||||
Reviewer string `bson:"reviewer"` //审核人
|
||||
ReviewStatus ReviewStatus `bson:"reviewStatus"` //审核状态
|
||||
ReceiptInfo string `bson:"receiptInfo"` //回执信息
|
||||
}
|
||||
|
||||
CoverProp = common.PictureProp
|
||||
|
||||
// Ads 广告审核
|
||||
AdReview struct {
|
||||
ID ObjectID `bson:"_id"` //id
|
||||
Cover string `bson:"cover"` //广告封面图
|
||||
Title string `bson:"title"` //广告标题
|
||||
HrefType URLJumpType `bson:"hrefType"` //广告链接类型
|
||||
Href string `bson:"href"` //广告跳转地址
|
||||
Position int `bson:"position"` //广告位置
|
||||
SortCode int `bson:"sortCode"` //排序号
|
||||
Remark string `bson:"remark"` //备注
|
||||
DistrictCode string `bson:"districtCode"` //商区码
|
||||
UpdatedAt time.Time `bson:"updatedAt"` //更新时间
|
||||
CreatedAt time.Time `bson:"createdAt"` //创建时间
|
||||
Operation `bson:",inline"` //操作
|
||||
Review `bson:",inline"` //审核信息
|
||||
CoverProp `bson:",inline"` //图片属性
|
||||
}
|
||||
|
||||
InsertDoc struct {
|
||||
Cover string `bson:"cover"` //广告封面图
|
||||
Title string `bson:"title"` //广告标题
|
||||
HrefType URLJumpType `bson:"hrefType"` //广告链接类型
|
||||
Href string `bson:"href"` //广告跳转地址
|
||||
Position int `bson:"position"` //广告位置
|
||||
SortCode int `bson:"sortCode"` //排序号
|
||||
Remark string `bson:"remark"` //备注
|
||||
DistrictCode string `bson:"districtCode"` //商区码
|
||||
CreatedAt time.Time `bson:"createdAt"` //创建时间
|
||||
Operation `bson:",inline"` //操作
|
||||
Review `bson:",inline"` //审核信息
|
||||
CoverProp `bson:",inline"` //图片属性
|
||||
}
|
||||
|
||||
UpdateDoc struct {
|
||||
Title *string `bson:"title,omitempty"` //广告标题
|
||||
Cover *string `bson:"cover,omitempty"` //广告封面图
|
||||
HrefType *URLJumpType `bson:"hrefType,omitempty"` //广告链接类型
|
||||
Href *string `bson:"href,omitempty"` //广告跳转地址
|
||||
Position *int `bson:"position,omitempty"` //广告位置
|
||||
SortCode *int `bson:"sortCode,omitempty"` //排序号
|
||||
Remark *string `bson:"remark,omitempty"` //备注
|
||||
OptType *OptType `bson:"optType,omitempty"` //操作类型
|
||||
ApplyAt *time.Time `bson:"applyAt,omitempty"` //申请时间
|
||||
UpdatedAt time.Time `bson:"updatedAt"` //更新时间
|
||||
CoverProp `bson:",inline"` //图片属性
|
||||
}
|
||||
)
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initAdsIndex()
|
||||
}
|
||||
Reference in New Issue
Block a user