Files
assets/dao/stock/inventory_count_detail_dao.go

186 lines
5.9 KiB
Go
Raw Permalink Normal View History

2026-03-18 10:18:03 +08:00
// 盘点明细DAO层
// 职责明细CRUD、批量插入(性能优化)、按盘点ID查询、更新调整状态
// 紧密耦合service.InventoryCountDetail、service.InventoryCount(统计更新)
// 注意InsertBatch批量插入减少网络往返ListByCountId使用mongo.DB(false)跳过缓存
package dao
import (
"assets/consts/public"
dto "assets/model/dto/stock"
entity "assets/model/entity/stock"
"context"
"fmt"
"gitea.com/red-future/common/db/mongo"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"go.mongodb.org/mongo-driver/v2/bson"
)
var InventoryCountDetail = new(inventoryCountDetail)
type inventoryCountDetail struct{}
func (d *inventoryCountDetail) Insert(ctx context.Context, req *dto.CreateInventoryCountDetailReq) (ids []interface{}, err error) {
result := &entity.InventoryCountDetail{
BookQuantity: req.BookQuantity,
BookBatchInfo: req.BookBatchInfo,
ActualQuantity: req.ActualQuantity,
ActualBatchInfo: req.ActualBatchInfo,
Remark: req.Remark,
}
// required string → *bson.ObjectID
for _, item := range []struct {
val string
dest **bson.ObjectID
name string
}{
{req.CountID, &result.CountID, "盘点单ID"},
{req.AssetID, &result.AssetID, "资产ID"},
{req.AssetSkuID, &result.AssetSkuID, "资产SKU ID"},
{req.WarehouseID, &result.WarehouseID, "仓库ID"},
} {
id, e := bson.ObjectIDFromHex(item.val)
if e != nil {
return nil, fmt.Errorf("%s格式错误: %v", item.name, e)
}
*item.dest = &id
}
// optional string → *bson.ObjectID
if !g.IsEmpty(req.ZoneID) {
id, e := bson.ObjectIDFromHex(req.ZoneID)
if e != nil {
return nil, fmt.Errorf("库区ID格式错误: %v", e)
}
result.ZoneID = &id
}
if !g.IsEmpty(req.LocationID) {
id, e := bson.ObjectIDFromHex(req.LocationID)
if e != nil {
return nil, fmt.Errorf("库位ID格式错误: %v", e)
}
result.LocationID = &id
}
ids, err = mongo.DB().Insert(ctx, []interface{}{result}, public.InventoryCountDetailCollection)
return
}
// InsertBatch 批量插入盘点明细性能优化100条/批,减少网络往返)
func (d *inventoryCountDetail) InsertBatch(ctx context.Context, details []*entity.InventoryCountDetail) (ids []interface{}, err error) {
if len(details) == 0 {
return
}
docs := make([]interface{}, len(details))
for i, detail := range details {
docs[i] = detail
}
ids, err = mongo.DB().Insert(ctx, docs, public.InventoryCountDetailCollection)
return
}
func (d *inventoryCountDetail) GetOne(ctx context.Context, req *dto.GetInventoryCountDetailReq) (res *entity.InventoryCountDetail, err error) {
filter := bson.M{"_id": req.Id}
err = mongo.DB().FindOne(ctx, filter, &res, public.InventoryCountDetailCollection)
return
}
func (d *inventoryCountDetail) Update(ctx context.Context, req *dto.UpdateInventoryCountDetailReq) (err error) {
buildFilter, err := mongo.BuildUpdateData(ctx, req)
if err != nil {
return
}
filter := bson.M{"_id": req.Id}
update := bson.M{"$set": buildFilter}
_, err = mongo.DB().Update(ctx, filter, update, public.InventoryCountDetailCollection)
return
}
func (d *inventoryCountDetail) DeleteFake(ctx context.Context, req *dto.DeleteInventoryCountDetailReq) (err error) {
filter := bson.M{"_id": req.Id}
_, err = mongo.DB().DeleteSoft(ctx, filter, public.InventoryCountDetailCollection)
return
}
// DeleteByCountId 按盘点任务ID真删除所有明细
func (d *inventoryCountDetail) DeleteByCountId(ctx context.Context, countId *bson.ObjectID) (err error) {
filter := bson.M{"countId": countId}
_, err = mongo.DB().Delete(ctx, filter, public.InventoryCountDetailCollection)
return
}
func (d *inventoryCountDetail) List(ctx context.Context, req *dto.ListInventoryCountDetailReq) (res []entity.InventoryCountDetail, total int64, err error) {
filter, err := d.buildListFilter(ctx, req)
if err != nil {
return
}
total, err = mongo.DB().Find(ctx, filter, &res, public.InventoryCountDetailCollection, req.Page, req.OrderBy)
return
}
func (d *inventoryCountDetail) buildListFilter(ctx context.Context, req *dto.ListInventoryCountDetailReq) (filter bson.M, err error) {
_ = ctx
filter = bson.M{}
if !g.IsEmpty(req.CountID) {
filter["countId"] = req.CountID
}
if !g.IsEmpty(req.AssetID) {
filter["assetId"] = req.AssetID
}
if !g.IsEmpty(req.AssetSkuID) {
filter["assetSkuId"] = req.AssetSkuID
}
if !g.IsEmpty(req.WarehouseID) {
filter["warehouseId"] = req.WarehouseID
}
if !g.IsEmpty(req.ZoneID) {
filter["zoneId"] = req.ZoneID
}
if !g.IsEmpty(req.LocationID) {
filter["locationId"] = req.LocationID
}
if !g.IsEmpty(req.DiscrepancyType) {
filter["discrepancyType"] = req.DiscrepancyType
}
if !g.IsEmpty(req.Status) {
filter["status"] = req.Status
}
if !g.IsEmpty(req.IsAdjusted) {
filter["isAdjusted"] = req.IsAdjusted
}
return
}
// ListByCountId 按盘点ID查询所有明细
func (d *inventoryCountDetail) ListByCountId(ctx context.Context, countId *bson.ObjectID) (res []entity.InventoryCountDetail, err error) {
filter := bson.M{"countId": countId}
_, err = mongo.DB(false).Find(ctx, filter, &res, public.InventoryCountDetailCollection, nil, nil)
return
}
// UpdateAdjusted 更新调整状态
func (d *inventoryCountDetail) UpdateAdjusted(ctx context.Context, id *bson.ObjectID, adjustedBy string) (err error) {
filter := bson.M{"_id": id}
update := bson.M{
"$set": bson.M{
"isAdjusted": true,
"adjustedBy": adjustedBy,
"adjustedAt": gtime.Now(),
},
}
_, err = mongo.DB().Update(ctx, filter, update, public.InventoryCountDetailCollection)
return
}
// UpdateDiscrepancyReason 更新差异原因
func (d *inventoryCountDetail) UpdateDiscrepancyReason(ctx context.Context, id *bson.ObjectID, reason string) (err error) {
filter := bson.M{"_id": id}
update := bson.M{
"$set": bson.M{
"discrepancyReason": reason,
"updatedAt": gtime.Now(),
},
}
_, err = mongo.DB().Update(ctx, filter, update, public.InventoryCountDetailCollection)
return
}