package dao import ( "assets/consts/public" dto "assets/model/dto/procurement" entity "assets/model/entity/procurement" "context" "gitea.com/red-future/common/beans" "gitea.com/red-future/common/db/mongo" "gitea.com/red-future/common/utils" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" "go.mongodb.org/mongo-driver/v2/bson" ) type purchaseInbound struct{} var PurchaseInbound = new(purchaseInbound) // Insert 插入 func (d *purchaseInbound) Insert(ctx context.Context, req *dto.CreatePurchaseInboundReq) (ids []interface{}, err error) { var result *entity.PurchaseInbound if err = utils.Struct(req, &result); err != nil { return } ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.PurchaseInboundCollection) return } // GetOne 查询单个 func (d *purchaseInbound) GetOne(ctx context.Context, req *dto.GetPurchaseInboundReq) (res *entity.PurchaseInbound, err error) { filter := bson.M{"_id": req.Id} err = mongo.DB(false).FindOne(ctx, filter, &res, public.PurchaseInboundCollection) return } // List 查询列表 func (d *purchaseInbound) List(ctx context.Context, req *dto.ListPurchaseInboundReq) (list []entity.PurchaseInbound, total int64, err error) { filter := d.buildListFilter(req) var orderBy []beans.OrderBy if req.OrderBy.Field != "" { orderBy = []beans.OrderBy{req.OrderBy} } total, err = mongo.DB().Find(ctx, filter, &list, public.PurchaseInboundCollection, &req.Page, orderBy) return } // UpdateDetails 更新入库记录详细信息 func (d *purchaseInbound) UpdateDetails(ctx context.Context, id *bson.ObjectID, orderId *bson.ObjectID, inboundNo, batchNo, warehouseName, zoneName, locationName, privateSkuName, privateCategoryPath string) (err error) { filter := bson.M{"_id": id} update := bson.M{ "$set": bson.M{ "orderId": orderId, "inboundNo": inboundNo, "batchNo": batchNo, "warehouseName": warehouseName, "zoneName": zoneName, "locationName": locationName, "privateSkuName": privateSkuName, "privateCategoryPath": privateCategoryPath, }, } _, err = mongo.DB().Update(ctx, filter, update, public.PurchaseInboundCollection) return } // UpdatePrivateStockId 更新入库记录关联的库存ID func (d *purchaseInbound) UpdatePrivateStockId(ctx context.Context, id, privateStockId *bson.ObjectID) (err error) { filter := bson.M{"_id": id} update := bson.M{ "$set": bson.M{ "privateStockId": privateStockId, }, } _, err = mongo.DB().Update(ctx, filter, update, public.PurchaseInboundCollection) return } // buildListFilter 构建列表过滤条件 func (d *purchaseInbound) buildListFilter(req *dto.ListPurchaseInboundReq) bson.M { filter := bson.M{} if !g.IsEmpty(req.OrderId) { filter["orderId"] = req.OrderId } if !g.IsEmpty(req.OrderItemId) { filter["orderItemId"] = req.OrderItemId } if !g.IsEmpty(req.InboundNo) { filter["inboundNo"] = bson.M{"$regex": req.InboundNo, "$options": "i"} } if !g.IsEmpty(req.StartDate) { startTime, _ := gtime.StrToTime(req.StartDate + " 00:00:00") filter["inboundDate"] = bson.M{"$gte": startTime} } if !g.IsEmpty(req.EndDate) { endTime, _ := gtime.StrToTime(req.EndDate + " 23:59:59") if filter["inboundDate"] != nil { filter["inboundDate"].(bson.M)["$lte"] = endTime } else { filter["inboundDate"] = bson.M{"$lte": endTime} } } return filter }