package productmod import ( "fmt" "strings" "time" "91porn-server/common" "91porn-server/common/constant" "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.Product // InitIndex 设置index func initIndex() { coll := coll(nil) many := []mongo.IndexModel{ { Keys: bson.D{{Key: "vipLevel", Value: 1}}, }, { Keys: bson.D{{Key: "productType", Value: 1}}, }, { Keys: bson.D{{Key: "discountedPrice", Value: 1}}, }, { Keys: bson.D{{Key: "productName", Value: 1}}, Options: options.Index().SetUnique(true), }, { Keys: bson.D{{Key: "createdAt", Value: 1}}, }, { Keys: bson.D{{Key: "updatedAt", 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) } func (this *Product) DiscountedPriceFill(sys string) { sys = common.HandleSysType(sys) if strings.Contains(sys, constant.SysTypeIOS) { this.DiscountedPrice = *this.DiscountedPriceIos } else { this.DiscountedPrice = *this.DiscountedPriceAnd } } // FindProduct 查询 func FindProduct(id primitive.ObjectID, sys string) (p *Product, err error) { if err = coll(nil).FindOne(&p, bson.M{"_id": id}); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProduct", table, "FindOne", err), log.Any("id", id)) return } if p == nil || p.ID.IsZero() { log.Info("product FindProduct not found", log.Any("id", id)) return nil, nil } p.DiscountedPriceFill(sys) return } // FindByProductType 查询 func FindByProductType(productType commod.ProductType) (data []Product, err error) { data = make([]Product, 0) opts := options.FindOptions{Sort: bson.D{{Key: "sort", Value: 1}}} if err = coll(nil).Find(&data, bson.M{"productType": productType, "status": true}, &opts); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProduct", table, "FindOne", err), log.Any("productType", productType)) return } return } // FindByProductTypes 查询 func FindByProductTypes(productTypes []commod.ProductType) (data []Product, err error) { data = make([]Product, 0) opts := options.FindOptions{Sort: bson.D{{Key: "sort", Value: 1}}} if err = coll(nil).Find(&data, bson.M{"productType": bson.M{"$in": productTypes}, "status": true}, &opts); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProduct", table, "Find", err), log.Any("productType", productTypes)) return } return } // FindActiveDramaCards 返回配置了短剧权益天数的在售商品。 func FindActiveDramaCards() (data []Product, err error) { opts := options.Find().SetSort(bson.D{{Key: "sort", Value: 1}, {Key: "_id", Value: 1}}) err = coll(nil).Find(&data, bson.M{"status": true, "dramaDays": bson.M{"$gt": 0}}, opts) return } // FindByVipLevel 查询 func FindByVipLevel(vipLevel int) (data []Product, err error) { data = make([]Product, 0) if err = coll(nil).Find(&data, bson.M{"vipLevel": vipLevel, "status": true}); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindByVipLevel", table, "FindOne", err), log.Any("vipLevel", vipLevel)) return } return } func FindByProductIDs(ids []primitive.ObjectID) ([]Product, error) { var data []Product return data, coll(nil).Find(&data, bson.M{"_id": bson.M{"$in": ids}}) } // InsertProduct 插入一条数据 func InsertProduct(p *Product) error { p.CreatedAt = time.Now() if _, err := coll(nil).InsertOne(p); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertProduct", table, "InsertOne", err)) return err } return nil } // UpdateProduct 修改Product类型 func UpdateProduct(id string, set *ProductSelector) error { set.UpdatedAt = time.Now() oid, err := primitive.ObjectIDFromHex(id) if err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateProduct", table, "ObjectIDFromHex", err), log.Any("id", id), log.Any("set", set), ) return err } if _, err = coll(nil).UpdateOne(bson.M{"_id": oid}, bson.M{"$set": set}); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateProduct", table, "UpdateOne", err), log.Any("oid", oid), log.Any("set", set), ) return err } return nil } // UpdateProductRights 清空产品权益 func UpdateProductRights(id string) error { oid, err := primitive.ObjectIDFromHex(id) if err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateProduct", table, "ObjectIDFromHex", err), log.Any("id", id), ) return err } if _, err := coll(nil).UpdateOne(bson.M{"_id": oid}, bson.M{"$set": bson.M{"newPrivilege": nil}}); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateProduct", table, "UpdateOne", err), log.Any("oid", oid), ) return err } return nil } // RemoveProduct 删除Product类型 func RemoveProduct(id string) error { OID, err := primitive.ObjectIDFromHex(id) if err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "RemoveProduct", table, "ObjectIDFromHex", err), log.Any("id", id), ) return err } if _, err = coll(nil).DeleteOne(bson.M{"_id": OID}); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "RemoveProduct", table, "DeleteOne", err), log.Any("OID", OID), ) return err } return nil } // FindStd 查询vip和约会卡 func FindStd(req ProductListWeb) ([]ProductWeb, error) { query := bson.M{} if req.Status != nil { query["status"] = req.Status } if req.ProductType != nil { query["productType"] = req.ProductType } opts := options.FindOptions{ Sort: bson.D{{Key: "sort", Value: 1}}, } if req.ProductType == nil { query["productType"] = bson.M{"$in": []commod.ProductType{GameAdvanceCard, AdvanceCard, VIP, MeetingCard, OTHER, NewUser, PHYSICALGOODS, WhoringCard, commod.VideoDiscount, commod.VideoFreeCard, commod.CoinMonthCard}} } data := make([]ProductWeb, 0) if err := coll(nil).Find(&data, query, &opts); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductsByProductType", table, "Find", err)) return data, err } return data, nil } // FindProductsByProductType 查询vip和约会卡 func FindProductsByProductType(status *bool, newUser bool, sys string) ([]Product, error) { query := bson.M{} if status != nil { query["status"] = *status } types := []commod.ProductType{VIP, MeetingCard, OTHER, PHYSICALGOODS, VideoDiscount, VideoFreeCard, CoinMonthCard, AdvanceCard, GameAdvanceCard, WhoringCard} if newUser { types = append(types, NewUser) } opts := options.FindOptions{ Sort: bson.D{{Key: "sort", Value: 1}}, } query["productType"] = bson.M{"$in": types} data := make([]Product, 0) if err := coll(nil).Find(&data, query, &opts); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductsByProductType", table, "Find", err)) return data, err } for i := range data { data[i].DiscountedPriceFill(sys) } return data, nil } func FindOne(t *db.MongoTool, cond bson.M, opts ...*options.FindOneOptions) (data *Product, err error) { if err = coll(nil).FindOne(&data, cond, opts...); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindOne", table, "FindOne", err), log.Any("cond", cond), ) return } return } // ListToIDs 获取商品列表 func ListToIDs(ids []primitive.ObjectID, sys string) ([]*Product, error) { var items []*Product if err := coll(nil).Find(&items, bson.M{"_id": bson.M{"$in": ids}}); err != nil { return nil, err } for i := range items { items[i].DiscountedPriceFill(sys) } return items, nil } func ListByIDsMap(ids []primitive.ObjectID) (map[primitive.ObjectID]*Product, error) { var m = make(map[primitive.ObjectID]*Product, 0) list, err := ListToIDs(ids, constant.SysTypeIOS) if err != nil { return nil, err } for _, product := range list { m[product.ID] = product } return m, nil } // CheckboxByType 通过类型获取商品复选框列表 func CheckboxByType(t commod.ProductType) ([]*ProductCheckbox, error) { var items []*ProductCheckbox return items, coll(nil).Find(&items, bson.M{"productType": t}) } // FindMany 查询vip和约会卡 func FindMany() ([]ProductWeb, error) { query := bson.M{} opts := options.FindOptions{ Sort: bson.D{{Key: "sort", Value: 1}}, } data := make([]ProductWeb, 0) if err := coll(nil).Find(&data, query, &opts); err != nil { log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductsByProductType", table, "Find", err)) return data, err } return data, nil } // GetLargestDiscountVIPCard 获取最大折扣VIP卡(PayVidDiscount越小表示折扣力度越大) func GetLargestDiscountVIPCard() (int, error) { sort := bson.D{{Key: "payVidDiscount", Value: 1}} filter := bson.M{ "productType": commod.VIP, "status": true, "payVidDiscount": bson.M{ "$gt": 0, }, } opts := options.FindOptions{} opts.SetSort(sort).SetLimit(1) discounts := []Product{} if err := coll(nil).Find(&discounts, filter, &opts); err != nil { return 0, err } if len(discounts) <= 0 { return 0, nil } return discounts[0].PayVidDiscount, nil } //func GetRecommendVip() (data *Product, err error) { // return FindOne(nil, bson.M{ // "isRecommend": true, // "status": true, // }) //}