2026-03-18 10:18:03 +08:00
|
|
|
|
// 库存明细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
|
2026-03-19 17:45:06 +08:00
|
|
|
|
func (d *stockDetails) GetStockCountBySkuId(ctx context.Context, assetSkuId int64) (total int64, err error) {
|
2026-03-18 10:18:03 +08:00
|
|
|
|
// 构建查询过滤条件
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|