Dockerfile
This commit is contained in:
80
dao/stock/stock_details_dao.go
Normal file
80
dao/stock/stock_details_dao.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// 库存明细DAO层(逻辑库存)
|
||||
// 职责:批量插入/删除、按SKU统计数量、查询列表
|
||||
// 紧密耦合:service.StockDetails、service.StockManage(入库出库)
|
||||
// 注意:GetStockCountBySkuId使用NoCache()跳过缓存,BatchInsert用于批量入库
|
||||
package dao
|
||||
|
||||
import (
|
||||
"assets/consts/public"
|
||||
"assets/consts/stock"
|
||||
dto "assets/model/dto/stock"
|
||||
entity "assets/model/entity/stock"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
var StockDetails = new(stockDetails)
|
||||
|
||||
type stockDetails struct {
|
||||
}
|
||||
|
||||
// BatchInsert 批量插入库存
|
||||
func (d *stockDetails) BatchInsert(ctx context.Context, stockInterfaces []interface{}) (ids []interface{}, err error) {
|
||||
ids, err = mongo.DB().Insert(ctx, stockInterfaces, public.StockDetailsCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteManyByIds 根据ID批量删除库存
|
||||
func (d *stockDetails) DeleteManyByIds(ctx context.Context, allStockIds []*bson.ObjectID) (count int64, err error) {
|
||||
if len(allStockIds) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
filter := bson.M{"_id": bson.M{"$in": allStockIds}}
|
||||
count, err = mongo.DB().Delete(ctx, filter, public.StockDetailsCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetStockCountBySkuId 获取库存数根据SKU ID
|
||||
func (d *stockDetails) GetStockCountBySkuId(ctx context.Context, assetSkuId *bson.ObjectID) (total int64, err error) {
|
||||
// 构建查询过滤条件
|
||||
filter := bson.M{}
|
||||
filter["assetSkuId"] = assetSkuId
|
||||
filter["status"] = stock.StockStatusAvailable
|
||||
// 检查总数
|
||||
total, err = mongo.DB().NoCache().Count(ctx, filter, public.StockDetailsCollection)
|
||||
|
||||
return total, err
|
||||
}
|
||||
|
||||
// GetOneById 根据ID查询库存明细
|
||||
func (d *stockDetails) GetOneById(ctx context.Context, req *dto.GetStockDetailsReq) (res *entity.StockDetails, err error) {
|
||||
filter := bson.M{"_id": req.Id}
|
||||
err = mongo.DB().FindOne(ctx, filter, &res, public.StockDetailsCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取SKU列表
|
||||
func (d *stockDetails) List(ctx context.Context, req *dto.ListStockDetailsReq) (res []entity.StockDetails, total int64, err error) {
|
||||
// 构建查询过滤条件
|
||||
filter := bson.M{}
|
||||
if !g.IsEmpty(req.AssetId) {
|
||||
filter["assetId"] = req.AssetId
|
||||
}
|
||||
if !g.IsEmpty(req.AssetSkuId) {
|
||||
filter["assetSkuId"] = req.AssetSkuId
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
filter["status"] = req.Status
|
||||
}
|
||||
// 排序处理
|
||||
req.OrderBy = []beans.OrderBy{
|
||||
{Field: "sort", Order: beans.Asc},
|
||||
{Field: "createdAt", Order: beans.Desc},
|
||||
}
|
||||
total, err = mongo.DB().Find(ctx, filter, &res, public.StockDetailsCollection, req.Page, req.OrderBy)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user