Files
assets/dao/stock/inventory_warning_history_dao.go

81 lines
2.6 KiB
Go
Raw Permalink Normal View History

2026-03-18 10:18:03 +08:00
// 库存预警历史DAO层
// 职责:预警历史查询、删除(无Insert/Update由预警状态变更时自动归档)
// 紧密耦合service.InventoryWarningHistory
// 注意:历史记录由系统自动归档,非用户手动创建
package dao
import (
"assets/consts/public"
dto "assets/model/dto/stock"
entity "assets/model/entity/stock"
"context"
"gitea.com/red-future/common/db/mongo"
"github.com/gogf/gf/v2/frame/g"
"go.mongodb.org/mongo-driver/v2/bson"
)
var InventoryWarningHistory = new(inventoryWarningHistory)
type inventoryWarningHistory struct{}
func (d *inventoryWarningHistory) GetOne(ctx context.Context, req *dto.GetInventoryWarningHistoryReq) (res *entity.InventoryWarningHistory, err error) {
filter := bson.M{"_id": req.Id}
err = mongo.DB().FindOne(ctx, filter, &res, public.InventoryWarningHistoryCollection)
return
}
func (d *inventoryWarningHistory) List(ctx context.Context, req *dto.ListInventoryWarningHistoryReq) (res []entity.InventoryWarningHistory, total int64, err error) {
filter, err := d.buildListFilter(ctx, req)
if err != nil {
return
}
total, err = mongo.DB().Find(ctx, filter, &res, public.InventoryWarningHistoryCollection, req.Page, req.OrderBy)
return
}
func (d *inventoryWarningHistory) DeleteFake(ctx context.Context, req *dto.DeleteInventoryWarningHistoryReq) (err error) {
filter := bson.M{"_id": req.Id}
_, err = mongo.DB().DeleteSoft(ctx, filter, public.InventoryWarningHistoryCollection)
return
}
func (d *inventoryWarningHistory) buildListFilter(ctx context.Context, req *dto.ListInventoryWarningHistoryReq) (filter bson.M, err error) {
_ = ctx
filter = bson.M{}
if !g.IsEmpty(req.WarningType) {
filter["warningType"] = req.WarningType
}
// 兼容单值和数组参数(优先使用数组)
if len(req.BatchIDs) > 0 {
filter["batchId"] = bson.M{"$in": req.BatchIDs}
} else if !g.IsEmpty(req.BatchID) {
filter["batchId"] = req.BatchID
}
if len(req.AssetIDs) > 0 {
filter["assetId"] = bson.M{"$in": req.AssetIDs}
} else if !g.IsEmpty(req.AssetID) {
filter["assetId"] = req.AssetID
}
if len(req.AssetSkuIDs) > 0 {
filter["assetSkuId"] = bson.M{"$in": req.AssetSkuIDs}
} else if !g.IsEmpty(req.AssetSkuID) {
filter["assetSkuId"] = req.AssetSkuID
}
if len(req.SupplierIDs) > 0 {
filter["supplierId"] = bson.M{"$in": req.SupplierIDs}
} else if !g.IsEmpty(req.SupplierID) {
filter["supplierId"] = req.SupplierID
}
if !g.IsEmpty(req.Status) {
filter["status"] = req.Status
}
if !g.IsEmpty(req.ProcessMethod) {
filter["processMethod"] = req.ProcessMethod
}
if !g.IsEmpty(req.Keyword) {
filter["batchNo"] = bson.M{"$regex": req.Keyword, "$options": "i"}
}
return
}