Dockerfile
This commit is contained in:
110
dao/asset/asset_dao.go
Normal file
110
dao/asset/asset_dao.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"assets/consts/public"
|
||||
dto "assets/model/dto/asset"
|
||||
entity "assets/model/entity/asset"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Asset = new(assetDao)
|
||||
|
||||
type assetDao struct {
|
||||
}
|
||||
|
||||
// Insert 插入资产
|
||||
func (d *assetDao) Insert(ctx context.Context, req *dto.CreateAssetReq) (id int64, err error) {
|
||||
var result entity.Asset
|
||||
if err = gconv.Struct(req, &result); err != nil {
|
||||
return
|
||||
}
|
||||
return gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Data(&result).InsertAndGetId()
|
||||
}
|
||||
|
||||
// GetOne 获取单个资产
|
||||
func (d *assetDao) GetOne(ctx context.Context, req *dto.GetAssetReq) (res *entity.Asset, err error) {
|
||||
err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", req.Id).Scan(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新资产
|
||||
func (d *assetDao) Update(ctx context.Context, req *dto.UpdateAssetReq) (err error) {
|
||||
data := g.Map{
|
||||
"name": req.Name,
|
||||
"description": req.Description,
|
||||
"type": req.Type,
|
||||
"category_id": req.CategoryId,
|
||||
"image_url": req.ImageURL,
|
||||
"images": req.Images,
|
||||
"status": req.Status,
|
||||
"online_time": req.OnlineTime,
|
||||
"offline_time": req.OfflineTime,
|
||||
"physical_asset_config": req.PhysicalAssetConfig,
|
||||
"service_asset_config": req.ServiceAssetConfig,
|
||||
"virtual_asset_config": req.VirtualAssetConfig,
|
||||
"metadata": req.Metadata,
|
||||
}
|
||||
_, err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", req.Id).Update(data)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteFake 删除资产-根据id进行假删
|
||||
func (d *assetDao) DeleteFake(ctx context.Context, req *dto.DeleteAssetReq) (err error) {
|
||||
_, err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", req.Id).Update(g.Map{
|
||||
"is_deleted": true,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// GetOneById 通过ID获取单个资产(内部使用uint64)
|
||||
func (d *assetDao) GetOneById(ctx context.Context, id uint64) (res *entity.Asset, err error) {
|
||||
err = gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where("id", id).Scan(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取资产数量
|
||||
func (d *assetDao) Count(ctx context.Context, req *dto.ListAssetReq) (count int64, err error) {
|
||||
m := d.buildListFilter(ctx, req)
|
||||
c, err := m.Count()
|
||||
return int64(c), err
|
||||
}
|
||||
|
||||
// List 获取资产列表
|
||||
func (d *assetDao) List(ctx context.Context, req *dto.ListAssetReq) (res []entity.Asset, total int, err error) {
|
||||
m := d.buildListFilter(ctx, req)
|
||||
if req.Page != nil {
|
||||
m = m.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
err = m.ScanAndCount(&res, &total, false)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *assetDao) buildListFilter(ctx context.Context, req *dto.ListAssetReq) *gdb.Model {
|
||||
m := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Cache(ctx).Where("is_deleted", false)
|
||||
|
||||
if !g.IsEmpty(req.Name) {
|
||||
m = m.Where("name", req.Name)
|
||||
}
|
||||
if !g.IsEmpty(req.Type) {
|
||||
m = m.Where("type", req.Type)
|
||||
}
|
||||
if !g.IsEmpty(req.CategoryId) {
|
||||
m = m.Where("category_id", req.CategoryId)
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
m = m.Where("status", req.Status)
|
||||
}
|
||||
if !g.IsEmpty(req.CategoryPath) {
|
||||
m = m.WhereLike("category_path", req.CategoryPath+"%")
|
||||
}
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
m = m.WhereLike("name", "%"+req.Keyword+"%")
|
||||
}
|
||||
return m
|
||||
}
|
||||
130
dao/asset/asset_sku_dao.go
Normal file
130
dao/asset/asset_sku_dao.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"assets/consts/public"
|
||||
"assets/model/dto/asset"
|
||||
"assets/model/entity/asset"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
var AssetSku = new(assetSku)
|
||||
|
||||
type assetSku struct {
|
||||
}
|
||||
|
||||
// Insert 插入SKU
|
||||
func (d *assetSku) Insert(ctx context.Context, req *dto.CreateAssetSkuReq) (ids []any, err error) {
|
||||
var result *entity.AssetSku
|
||||
if err = utils.Struct(req, &result); err != nil {
|
||||
return
|
||||
}
|
||||
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.AssetSkuCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新SKU
|
||||
func (d *assetSku) Update(ctx context.Context, req *dto.UpdateAssetSkuReq) (err error) {
|
||||
buildUpdateData, err := mongo.BuildUpdateData(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": req.Id}
|
||||
update := bson.M{"$set": buildUpdateData}
|
||||
if !g.IsEmpty(req.Stock) {
|
||||
// 从$set中移除stock字段避免$set和$inc冲突
|
||||
delete(buildUpdateData, "stock")
|
||||
update = bson.M{
|
||||
"$inc": bson.M{
|
||||
"stock": req.Stock,
|
||||
},
|
||||
}
|
||||
if len(buildUpdateData) > 0 {
|
||||
update["$set"] = buildUpdateData
|
||||
}
|
||||
}
|
||||
_, err = mongo.DB().Update(ctx, filter, update, public.AssetSkuCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteFake 删除SKU-根据id进行假删
|
||||
func (d *assetSku) DeleteFake(ctx context.Context, req *dto.DeleteAssetSkuReq) (err error) {
|
||||
filter := bson.M{"_id": req.Id}
|
||||
_, err = mongo.DB().DeleteSoft(ctx, filter, public.AssetSkuCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个SKU
|
||||
func (d *assetSku) GetOne(ctx context.Context, req *dto.GetAssetSkuReq, noTenantId bool) (res *entity.AssetSku, err error) {
|
||||
filter := bson.M{"_id": req.Id}
|
||||
if noTenantId {
|
||||
err = mongo.DB().NoTenantId().FindOne(ctx, filter, &res, public.AssetSkuCollection)
|
||||
} else {
|
||||
err = mongo.DB().FindOne(ctx, filter, &res, public.AssetSkuCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetListByAssetIdExcludeCurrentSku 根据资产ID获取SKU列表并且排除当前SKU
|
||||
func (d *assetSku) GetListByAssetIdExcludeCurrentSku(ctx context.Context, assetId *bson.ObjectID, req *dto.ListAssetSkuReq) (res []entity.AssetSku, total int64, err error) {
|
||||
filter := bson.M{"assetId": assetId, "_id": bson.M{"$ne": req.Id}}
|
||||
total, err = mongo.DB().Find(ctx, filter, &res, public.AssetSkuCollection, req.Page, req.OrderBy)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取SKU列表
|
||||
func (d *assetSku) List(ctx context.Context, req *dto.ListAssetSkuReq, noTenantId bool) (res []entity.AssetSku, total int64, err error) {
|
||||
// 构建查询过滤条件
|
||||
filter, err := d.buildListFilter(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 排序处理
|
||||
req.OrderBy = []beans.OrderBy{
|
||||
{Field: "sort", Order: beans.Asc},
|
||||
{Field: "createdAt", Order: beans.Desc},
|
||||
}
|
||||
if noTenantId {
|
||||
total, err = mongo.DB().NoTenantId().Find(ctx, filter, &res, public.AssetSkuCollection, req.Page, req.OrderBy)
|
||||
} else {
|
||||
total, err = mongo.DB().Find(ctx, filter, &res, public.AssetSkuCollection, req.Page, req.OrderBy)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *assetSku) buildListFilter(ctx context.Context, req *dto.ListAssetSkuReq) (filter bson.M, err error) {
|
||||
_ = ctx
|
||||
filter = bson.M{}
|
||||
if !g.IsEmpty(req.AssetId) {
|
||||
filter["assetId"] = req.AssetId
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
filter["status"] = req.Status
|
||||
}
|
||||
if !g.IsEmpty(req.CategoryPath) {
|
||||
filter["categoryPath"] = bson.M{"$regex": "^" + req.CategoryPath, "$options": "i"}
|
||||
}
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
orConditions := bson.A{
|
||||
bson.M{"skuName": bson.M{"$regex": req.Keyword, "$options": "i"}},
|
||||
bson.M{"assetName": bson.M{"$regex": req.Keyword, "$options": "i"}},
|
||||
}
|
||||
filter["$or"] = orConditions
|
||||
}
|
||||
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
|
||||
}
|
||||
95
dao/asset/category_dao.go
Normal file
95
dao/asset/category_dao.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"assets/consts/public"
|
||||
dto "assets/model/dto/asset"
|
||||
entity "assets/model/entity/asset"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
var Category = new(category)
|
||||
|
||||
type category struct {
|
||||
}
|
||||
|
||||
// Insert 插入分类
|
||||
func (d *category) Insert(ctx context.Context, req *dto.CreateCategoryReq) (res *entity.Category, err error) {
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
res.Bid = guid.S()
|
||||
_, err = gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Insert(&res)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Update 更新分类
|
||||
func (d *category) Update(ctx context.Context, req *dto.UpdateCategoryReq) (err error) {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Data(gconv.Map(&req)).OmitEmpty()
|
||||
if !g.IsEmpty(req.Bid) {
|
||||
model.Where("bid", req.Bid)
|
||||
}
|
||||
if !g.IsEmpty(req.Id) {
|
||||
model.Where("id", req.Id)
|
||||
}
|
||||
_, err = model.Update()
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个分类
|
||||
func (d *category) GetOne(ctx context.Context, req *dto.GetCategoryReq, fields ...string) (category *entity.Category, err error) {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection)
|
||||
if !g.IsEmpty(req.Bid) {
|
||||
model.Where(entity.CategoryCol.Bid, req.Bid)
|
||||
}
|
||||
if !g.IsEmpty(req.Id) {
|
||||
model.Where(entity.CategoryCol.Id, req.Id)
|
||||
}
|
||||
res, err := model.Fields(fields).One()
|
||||
err = res.Struct(&category)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteFake 删除分类-根据id及父id进行假删
|
||||
func (d *category) DeleteFake(ctx context.Context, req *dto.DeleteCategoryReq) (err error) {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection)
|
||||
model.Where(entity.CategoryCol.Bid, req.Bid)
|
||||
_, err = model.Delete()
|
||||
return
|
||||
}
|
||||
|
||||
// Count 根据条件统计分类数量
|
||||
func (d *category) Count(ctx context.Context, req *dto.ListCategoryReq) (count int64, err error) {
|
||||
m := d.buildListFilter(ctx, req)
|
||||
c, err := m.Count()
|
||||
return int64(c), err
|
||||
}
|
||||
|
||||
// List 获取分类列表
|
||||
func (d *category) List(ctx context.Context, req *dto.ListCategoryReq, fields ...string) (res []entity.Category, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.Fields(fields)
|
||||
r, total, err := model.AllAndCount(false)
|
||||
err = gconv.Structs(r.List(), &res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *category) buildListFilter(ctx context.Context, req *dto.ListCategoryReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Model
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
model.WhereLike(entity.CategoryCol.Name, "%"+req.Keyword+"%")
|
||||
}
|
||||
model.Where(entity.CategoryCol.ParentId, req.ParentId)
|
||||
model.Where(entity.CategoryCol.Status, req.Status)
|
||||
model.OrderAsc(entity.CategoryCol.Sort)
|
||||
model.OrderDesc(entity.CategoryCol.CreatedAt)
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
116
dao/asset/private_category_dao.go
Normal file
116
dao/asset/private_category_dao.go
Normal file
@@ -0,0 +1,116 @@
|
||||
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 PrivateCategory = new(privateCategory)
|
||||
|
||||
type privateCategory struct{}
|
||||
|
||||
// Insert 插入私域分类
|
||||
func (d *privateCategory) Insert(ctx context.Context, req *dto.CreatePrivateCategoryReq) (ids []interface{}, err error) {
|
||||
var result *entity.PrivateCategory
|
||||
if err = gconv.Struct(req, &result); err != nil {
|
||||
return
|
||||
}
|
||||
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.PrivateCategoryCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// BatchInsert 批量插入私域分类
|
||||
func (d *privateCategory) BatchInsert(ctx context.Context, req *dto.BatchCreatePrivateCategoryReq) (ids []interface{}, err error) {
|
||||
items := make([]*entity.PrivateCategory, 0, len(req.Categories))
|
||||
for _, item := range req.Categories {
|
||||
var result *entity.PrivateCategory
|
||||
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.PrivateCategoryCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个私域分类
|
||||
func (d *privateCategory) GetOne(ctx context.Context, id *bson.ObjectID) (category *entity.PrivateCategory, err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
err = mongo.DB().FindOne(ctx, filter, &category, public.PrivateCategoryCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新私域分类
|
||||
func (d *privateCategory) Update(ctx context.Context, req *dto.UpdatePrivateCategoryReq) (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.PrivateCategoryCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteFake 删除私域分类-根据id进行假删
|
||||
func (d *privateCategory) DeleteFake(ctx context.Context, id *bson.ObjectID) (err error) {
|
||||
filter := bson.M{"_id": id}
|
||||
_, err = mongo.DB().DeleteSoft(ctx, filter, public.PrivateCategoryCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取私域分类列表
|
||||
func (d *privateCategory) List(ctx context.Context, req *dto.ListPrivateCategoryReq) (res []*entity.PrivateCategory, total int64, err error) {
|
||||
filter, err := d.buildListFilter(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
total, err = mongo.DB().Find(ctx, filter, &res, public.PrivateCategoryCollection, nil, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTree 获取私域分类树
|
||||
func (d *privateCategory) GetTree(ctx context.Context) (res []*entity.PrivateCategory, err error) {
|
||||
filter := bson.M{}
|
||||
_, err = mongo.DB().Find(ctx, filter, &res, public.PrivateCategoryCollection, nil, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByParentId 根据父ID获取子分类列表
|
||||
func (d *privateCategory) GetByParentId(ctx context.Context, parentId string) (res []*entity.PrivateCategory, err error) {
|
||||
filter := bson.M{"parentId": parentId}
|
||||
_, err = mongo.DB().Find(ctx, filter, &res, public.PrivateCategoryCollection, nil, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *privateCategory) buildListFilter(ctx context.Context, req *dto.ListPrivateCategoryReq) (filter bson.M, err error) {
|
||||
_ = ctx
|
||||
filter = bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.Name) {
|
||||
filter["name"] = bson.M{"$regex": req.Name, "$options": "i"}
|
||||
}
|
||||
if !g.IsEmpty(req.ParentID) {
|
||||
filter["parentId"] = req.ParentID
|
||||
}
|
||||
if !g.IsEmpty(req.Level) {
|
||||
filter["level"] = req.Level
|
||||
}
|
||||
if req.IsLeafNode != nil {
|
||||
filter["isLeafNode"] = *req.IsLeafNode
|
||||
}
|
||||
return
|
||||
}
|
||||
119
dao/asset/private_sku_dao.go
Normal file
119
dao/asset/private_sku_dao.go
Normal file
@@ -0,0 +1,119 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user