120 lines
3.7 KiB
Go
120 lines
3.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"assets/consts/public"
|
|
dto "assets/model/dto/asset"
|
|
entity "assets/model/entity/asset"
|
|
"context"
|
|
|
|
"gitea.com/red-future/common/db/mongo"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
var PrivateSku = new(privateSku)
|
|
|
|
type privateSku struct{}
|
|
|
|
// Insert 插入私域SKU
|
|
func (d *privateSku) Insert(ctx context.Context, req *dto.CreatePrivateSkuReq) (ids []interface{}, err error) {
|
|
var result *entity.PrivateSku
|
|
if err = gconv.Struct(req, &result); err != nil {
|
|
return
|
|
}
|
|
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.PrivateSkuCollection)
|
|
return
|
|
}
|
|
|
|
// BatchInsert 批量插入私域SKU
|
|
func (d *privateSku) BatchInsert(ctx context.Context, req *dto.BatchCreatePrivateSkuReq) (ids []interface{}, err error) {
|
|
items := make([]*entity.PrivateSku, 0, len(req.Skus))
|
|
for _, item := range req.Skus {
|
|
var result *entity.PrivateSku
|
|
if err = gconv.Struct(item, &result); err != nil {
|
|
return
|
|
}
|
|
items = append(items, result)
|
|
}
|
|
interfaces := make([]interface{}, len(items))
|
|
for i, item := range items {
|
|
interfaces[i] = item
|
|
}
|
|
ids, err = mongo.DB().Insert(ctx, interfaces, public.PrivateSkuCollection)
|
|
return
|
|
}
|
|
|
|
// GetOne 获取单个私域SKU
|
|
func (d *privateSku) GetOne(ctx context.Context, id *bson.ObjectID) (sku *entity.PrivateSku, err error) {
|
|
filter := bson.M{"_id": id}
|
|
err = mongo.DB().FindOne(ctx, filter, &sku, public.PrivateSkuCollection)
|
|
return
|
|
}
|
|
|
|
// Update 更新私域SKU
|
|
func (d *privateSku) Update(ctx context.Context, req *dto.UpdatePrivateSkuReq) (err error) {
|
|
buildUpdateData, err := mongo.BuildUpdateData(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
filter := bson.M{"_id": req.ID}
|
|
update := bson.M{"$set": buildUpdateData}
|
|
_, err = mongo.DB().Update(ctx, filter, update, public.PrivateSkuCollection)
|
|
return
|
|
}
|
|
|
|
// UpdateStock 更新库存
|
|
func (d *privateSku) UpdateStock(ctx context.Context, id *bson.ObjectID, stockChange int) (err error) {
|
|
filter := bson.M{"_id": id}
|
|
update := bson.M{"$inc": bson.M{"stock": stockChange}}
|
|
_, err = mongo.DB().Update(ctx, filter, update, public.PrivateSkuCollection)
|
|
return
|
|
}
|
|
|
|
// DeleteFake 删除私域SKU-根据id进行假删
|
|
func (d *privateSku) DeleteFake(ctx context.Context, id *bson.ObjectID) (err error) {
|
|
filter := bson.M{"_id": id}
|
|
_, err = mongo.DB().DeleteSoft(ctx, filter, public.PrivateSkuCollection)
|
|
return
|
|
}
|
|
|
|
// List 获取私域SKU列表
|
|
func (d *privateSku) List(ctx context.Context, req *dto.ListPrivateSkuReq) (res []*entity.PrivateSku, total int64, err error) {
|
|
filter, err := d.buildListFilter(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
total, err = mongo.DB().Find(ctx, filter, &res, public.PrivateSkuCollection, nil, nil)
|
|
return
|
|
}
|
|
|
|
// ListByCategoryPath 根据分类路径获取SKU列表
|
|
func (d *privateSku) ListByCategoryPath(ctx context.Context, categoryPath string) (res []*entity.PrivateSku, err error) {
|
|
filter := bson.M{"privateCategoryPath": bson.M{"$regex": "^" + categoryPath, "$options": "i"}}
|
|
_, err = mongo.DB().Find(ctx, filter, &res, public.PrivateSkuCollection, nil, nil)
|
|
return
|
|
}
|
|
|
|
// buildListFilter 构建列表查询的过滤条件
|
|
func (d *privateSku) buildListFilter(ctx context.Context, req *dto.ListPrivateSkuReq) (filter bson.M, err error) {
|
|
_ = ctx
|
|
filter = bson.M{}
|
|
|
|
if !g.IsEmpty(req.SkuName) {
|
|
filter["skuName"] = bson.M{"$regex": req.SkuName, "$options": "i"}
|
|
}
|
|
if !g.IsEmpty(req.PrivateCategoryPath) {
|
|
filter["privateCategoryPath"] = bson.M{"$regex": "^" + req.PrivateCategoryPath, "$options": "i"}
|
|
}
|
|
if req.MinPrice > 0 {
|
|
filter["price"] = bson.M{"$gte": req.MinPrice}
|
|
}
|
|
if req.MaxPrice > 0 {
|
|
if filter["price"] == nil {
|
|
filter["price"] = bson.M{}
|
|
}
|
|
filter["price"].(bson.M)["$lte"] = req.MaxPrice
|
|
}
|
|
return
|
|
}
|