You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

package internal
import (
"context"
"fmt"
"github.com/gogf/gf/v2/os/gtime"
"go.mongodb.org/mongo-driver/bson"
"log"
"strconv"
)
func GetModel(ctx context.Context, mtype int64, id int64) (all []map[string]interface{}, err error) {
filter := bson.M{"_t": "BazaarStall"}
if mtype != 0 {
filter["Type"] = mtype
}
if id != 0 {
filter["StallId"] = id
}
err = MongoDatabaseList["0"].Collection("BazaarStall").Find(ctx, filter).All(&all)
for _, v := range all {
v["StringId"] = fmt.Sprint(v["_id"])
}
if err != nil {
return
}
return all, err
}
func UpdateModel(ctx context.Context, data map[string]interface{}) (err error) {
filter := bson.M{"_t": "BazaarStall"}
data["_id"], _ = strconv.ParseInt(fmt.Sprint(data["_id"]), 10, 64)
filter["_id"] = data["_id"]
err = MongoDatabaseList["0"].Collection("BazaarStall").UpdateOne(ctx, filter, bson.M{"$set": data})
return
}
func InsertModel(ctx context.Context, data map[string]interface{}) (err error) {
data["_id"] = gtime.TimestampNano()
data["_t"] = "BazaarStall"
ss, err := MongoDatabaseList["0"].Collection("BazaarStall").InsertOne(ctx, data)
log.Print("InsertModel %v", ss)
return
}
func DelModel(ctx context.Context, id int64, stallId int32) (err error) {
filter := bson.M{"_t": "BazaarActivity"}
filter["$or"] = bson.A{bson.M{"HighPriceRecycle.StallId": stallId}}
all := make([]map[string]interface{}, 1)
_ = MongoDatabaseList["0"].Collection("BazaarActivity").Find(ctx, filter).All(&all)
dataLen := len(all)
log.Print("DelModel ", all, filter)
if dataLen > 0 {
return fmt.Errorf("id used!")
}
err = MongoDatabaseList["0"].Collection("BazaarStall").RemoveId(ctx, id)
log.Print("DelModel err: ", id, err)
return
}