@@ -0,0 +1,123 @@
|
||||
package discount_area_mod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/models"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const (
|
||||
table = models.DiscountArea
|
||||
)
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func GetNoStatusDiscountAreaByIds(ids []primitive.ObjectID) (list []DiscountArea, err error) {
|
||||
cond := bson.M{}
|
||||
cond["_id"] = bson.M{"$in": ids}
|
||||
if err = coll(nil).Find(&list, cond); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetNoStatusDiscountAreaByIds", table, "Find", err),
|
||||
log.Any("cond", cond),
|
||||
)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetDiscountAreaById 通过id获取上架的折扣区
|
||||
func GetDiscountAreaById(id primitive.ObjectID) (data DiscountArea, err error) {
|
||||
cond := bson.M{}
|
||||
cond["status"] = 1
|
||||
cond["_id"] = id
|
||||
if err = coll(nil).FindOne(&data, cond); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetDiscountAreaById", table, "FindOne", err),
|
||||
log.Any("cond", cond),
|
||||
)
|
||||
return data, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetDiscountAreaByIds 通过ids获取上架的折扣区
|
||||
func GetDiscountAreaByIds(ids []primitive.ObjectID) (list []DiscountArea, err error) {
|
||||
opts := options.FindOptions{}
|
||||
sort := bson.D{{Key: "sortCode", Value: -1}}
|
||||
opts.SetSort(sort)
|
||||
cond := bson.M{}
|
||||
cond["status"] = 1
|
||||
if len(ids) > 0 {
|
||||
cond["_id"] = bson.M{"$in": ids}
|
||||
}
|
||||
if err = coll(nil).Find(&list, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetDiscountAreaByIds", table, "Find", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("sort", sort),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllDiscountArea 获取所有上架的折扣区
|
||||
func GetAllDiscountArea() (list []DiscountArea, err error) {
|
||||
opts := options.FindOptions{}
|
||||
sort := bson.D{{Key: "sortCode", Value: -1}}
|
||||
opts.SetSort(sort)
|
||||
cond := bson.M{}
|
||||
cond["status"] = 1
|
||||
if err = coll(nil).Find(&list, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAllDiscountArea", table, "Find", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("sort", sort),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllNoDeleteDiscountArea 获取没有被删除的专区
|
||||
func GetAllNoDeleteDiscountArea() (list []DiscountArea, err error) {
|
||||
opts := options.FindOptions{}
|
||||
sort := bson.D{{Key: "sortCode", Value: -1}}
|
||||
opts.SetSort(sort)
|
||||
cond := bson.M{}
|
||||
cond["status"] = bson.M{"$in": []int{0, 1}}
|
||||
if err = coll(nil).Find(&list, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAllNoDeleteDiscountArea", table, "Find", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("sort", sort),
|
||||
)
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddDiscountArea 新增折扣专区
|
||||
func AddDiscountArea(model *DiscountArea) (err error) {
|
||||
if _, err = coll(nil).InsertOne(model); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddDiscountArea", table, "InsertOne", err))
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateDiscountArea 修改折扣专区
|
||||
func UpdateDiscountArea(id primitive.ObjectID, update bson.M) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
if _, err = coll(nil).UpdateOne(filter, bson.M{"$set": update}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateDiscountArea", table, "UpdateOne", err))
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user