72 lines
2.2 KiB
Go
72 lines
2.2 KiB
Go
// 库存预警DAO层
|
||
// 职责:预警查询(无Insert/Update/Delete,由系统自动生成)
|
||
// 紧密耦合:service.InventoryWarning
|
||
// 注意:预警记录由定时任务或库存变动触发,非用户手动创建
|
||
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 InventoryWarning = new(inventoryWarning)
|
||
|
||
type inventoryWarning struct{}
|
||
|
||
func (d *inventoryWarning) GetOne(ctx context.Context, req *dto.GetInventoryWarningReq) (res *entity.InventoryWarning, err error) {
|
||
filter := bson.M{"_id": req.Id}
|
||
err = mongo.DB().FindOne(ctx, filter, &res, public.InventoryWarningCollection)
|
||
return
|
||
}
|
||
|
||
func (d *inventoryWarning) List(ctx context.Context, req *dto.ListInventoryWarningReq) (res []entity.InventoryWarning, total int64, err error) {
|
||
filter, err := d.buildListFilter(ctx, req)
|
||
if err != nil {
|
||
return
|
||
}
|
||
total, err = mongo.DB().Find(ctx, filter, &res, public.InventoryWarningCollection, req.Page, req.OrderBy)
|
||
return
|
||
}
|
||
|
||
func (d *inventoryWarning) buildListFilter(ctx context.Context, req *dto.ListInventoryWarningReq) (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.Keyword) {
|
||
filter["batchNo"] = bson.M{"$regex": req.Keyword, "$options": "i"}
|
||
}
|
||
return
|
||
}
|