refactor: 重构资产实体和DTO结构类型
将gjson.Json类型替换为具体的结构体和map类型,修正DAO层链式调用,启用SKU元数据校验逻辑
This commit is contained in:
@@ -23,7 +23,7 @@ func (d *assetDao) Insert(ctx context.Context, req *dto.CreateAssetReq) (id int6
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Data(&res).Insert()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Data(&res).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func (d *assetDao) Insert(ctx context.Context, req *dto.CreateAssetReq) (id int6
|
||||
|
||||
// Update 更新资产
|
||||
func (d *assetDao) Update(ctx context.Context, req *dto.UpdateAssetReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).OmitEmpty().Where(entity.AssetCol.Id, req.Id).Update()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Data(&req).OmitEmpty().Where(entity.AssetCol.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (d *assetDao) Delete(ctx context.Context, req *dto.DeleteAssetReq) (rows in
|
||||
|
||||
// GetOne 获取单个资产
|
||||
func (d *assetDao) GetOne(ctx context.Context, req *dto.GetAssetReq, fields ...string) (res *entity.Asset, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Ctx(ctx).Where(entity.AssetCol.Id, req.Id).Fields(fields).One()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetCollection).Where(entity.AssetCol.Id, req.Id).Fields(fields).One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func (d *assetSku) Insert(ctx context.Context, req *dto.CreateAssetSkuReq) (id i
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Ctx(ctx).Data(&res).Insert()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Data(&res).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func (d *assetSku) Insert(ctx context.Context, req *dto.CreateAssetSkuReq) (id i
|
||||
|
||||
// Update 更新SKU
|
||||
func (d *assetSku) Update(ctx context.Context, req *dto.UpdateAssetSkuReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Ctx(ctx).OmitEmpty().Where(entity.AssetCol.Id, req.Id).Update()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Data(&req).OmitEmpty().Where(entity.AssetCol.Id, req.Id).Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (d *assetSku) Delete(ctx context.Context, req *dto.DeleteAssetSkuReq) (rows
|
||||
|
||||
// GetOne 获取单个SKU
|
||||
func (d *assetSku) GetOne(ctx context.Context, req *dto.GetAssetSkuReq, fields ...string) (res *entity.AssetSku, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Ctx(ctx).Where(entity.AssetCol.Id, req.Id).Fields(fields).One()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.AssetSkuCollection).Where(entity.AssetCol.Id, req.Id).Fields(fields).One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -104,8 +104,10 @@ func (d *assetSku) buildListFilter(ctx context.Context, req *dto.ListAssetSkuReq
|
||||
}
|
||||
model.Where(entity.AssetSkuCol.Id, req.Id)
|
||||
model.Where(entity.AssetSkuCol.Status, req.Status)
|
||||
model.WhereGT(entity.AssetSkuCol.Price, req.MinPrice)
|
||||
model.WhereLT(entity.AssetSkuCol.Price, req.MaxPrice)
|
||||
if req.MinPrice > 0 && req.MaxPrice > 0 {
|
||||
model.WhereGT(entity.AssetSkuCol.Price, req.MinPrice)
|
||||
model.WhereLT(entity.AssetSkuCol.Price, req.MaxPrice)
|
||||
}
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
@@ -10,9 +10,10 @@ import (
|
||||
entity "assets/model/entity/stock"
|
||||
"context"
|
||||
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"gitea.com/red-future/common/utils"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"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 StockBatch = new(stockBatch)
|
||||
@@ -21,58 +22,48 @@ type stockBatch struct {
|
||||
}
|
||||
|
||||
// Insert 插入
|
||||
func (d *stockBatch) Insert(ctx context.Context, req *dto.CreateBatchReq) (ids []interface{}, err error) {
|
||||
var result *entity.StockBatch
|
||||
if err = utils.Struct(req, &result); err != nil {
|
||||
func (d *stockBatch) Insert(ctx context.Context, req *dto.CreateSockBatchReq) (id int64, err error) {
|
||||
var res *entity.StockBatch
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.StockBatchCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新批次数量(使用$inc原子操作,并发安全)
|
||||
func (d *stockBatch) Update(ctx context.Context, req *dto.UpdateBatchReq) (err error) {
|
||||
filter := bson.M{"_id": req.Id}
|
||||
update := bson.M{
|
||||
"$inc": bson.M{
|
||||
"batchQty": req.BatchQty,
|
||||
"availableQty": req.AvailableQty,
|
||||
},
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.StockBatchCollection).Data(&res).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = mongo.DB().Update(ctx, filter, update, public.StockBatchCollection)
|
||||
return
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// GetOne 根据批次号查询(使用NoCache跳过缓存,确保获取最新数据)
|
||||
func (d *stockBatch) GetOne(ctx context.Context, batchNo string) (res *entity.StockBatch, err error) {
|
||||
filter := bson.M{"batchNo": batchNo}
|
||||
err = mongo.DB().NoCache().FindOne(ctx, filter, &res, public.StockBatchCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOneById 根据ID查询批次
|
||||
func (d *stockBatch) GetOneById(ctx context.Context, req *dto.GetBatchReq) (res *entity.StockBatch, err error) {
|
||||
filter := bson.M{"_id": req.Id}
|
||||
err = mongo.DB().FindOne(ctx, filter, &res, public.StockBatchCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteFake 软删除批次
|
||||
func (d *stockBatch) DeleteFake(ctx context.Context, req *dto.DeleteBatchReq) error {
|
||||
filter := bson.M{"_id": req.Id}
|
||||
_, err := mongo.DB().DeleteSoft(ctx, filter, public.StockBatchCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
// List 查询批次列表
|
||||
func (d *stockBatch) List(ctx context.Context, req *dto.ListBatchReq) (res []entity.StockBatch, total int64, err error) {
|
||||
filter := bson.M{}
|
||||
if req.AssetId != nil {
|
||||
filter["assetId"] = req.AssetId
|
||||
func (d *stockBatch) Update(ctx context.Context, req *dto.UpdateSockBatchReq) (rows int64, err error) {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.StockBatchCollection).OmitEmpty().Where(entity.StockBatchCol.Id, req.Id)
|
||||
model.Data(entity.StockBatchCol.BatchQty, &gdb.Counter{
|
||||
Field: entity.StockBatchCol.BatchQty,
|
||||
Value: gconv.Float64(req.BatchQty),
|
||||
})
|
||||
model.Data(entity.StockBatchCol.AvailableQty, &gdb.Counter{
|
||||
Field: entity.StockBatchCol.AvailableQty,
|
||||
Value: gconv.Float64(req.AvailableQty),
|
||||
})
|
||||
r, err := model.Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if req.AssetSkuId != nil {
|
||||
filter["assetSkuId"] = req.AssetSkuId
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// One 根据批次号查询(使用NoCache跳过缓存,确保获取最新数据)
|
||||
func (d *stockBatch) One(ctx context.Context, req *dto.GetSockBatchReq, fields ...string) (res *entity.StockBatch, err error) {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.StockBatchCollection)
|
||||
if !g.IsEmpty(req.Id) {
|
||||
model.Where(entity.StockBatchCol.Id, req.Id)
|
||||
}
|
||||
total, err = mongo.DB().Find(ctx, filter, &res, public.StockBatchCollection, req.Page, req.OrderBy)
|
||||
if !g.IsEmpty(req.BatchNo) {
|
||||
model.Where(entity.StockBatchCol.BatchNo, req.BatchNo)
|
||||
}
|
||||
r, err := model.Fields(fields).One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,15 +6,13 @@ 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"
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var StockDetails = new(stockDetails)
|
||||
@@ -23,58 +21,52 @@ 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)
|
||||
func (d *stockDetails) BatchInsert(ctx context.Context, req []*dto.CreateSockDetailsReq) (rows int64, err error) {
|
||||
var res []*entity.StockDetails
|
||||
if err = gconv.Structs(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.StockDetailsCollection).Data(res).Save()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
func (d *stockDetails) Delete(ctx context.Context, req []dto.DeleteSockDetailsReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.StockDetailsCollection).Where(entity.StockDetailsCol.Id, &req).Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
func (d *stockDetails) Count(ctx context.Context, req *dto.GetSockDetailsReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
func (d *stockDetails) List(ctx context.Context, req *dto.GetSockDetailsReq, fields ...string) (res []entity.StockDetails, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req)
|
||||
model.Fields(fields)
|
||||
model.OrderDesc(entity.StockDetailsCol.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
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 int64) (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
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *stockDetails) buildListFilter(ctx context.Context, req *dto.GetSockDetailsReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.StockDetailsCollection).Model
|
||||
model.Where(entity.StockDetailsCol.Id, req.Id)
|
||||
model.Where(entity.StockDetailsCol.AssetId, req.AssetId)
|
||||
model.Where(entity.StockDetailsCol.AssetSkuId, req.AssetSkuId)
|
||||
model.Where(entity.StockDetailsCol.Status, req.Status)
|
||||
model.OmitEmptyWhere()
|
||||
return model
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user