142 lines
5.2 KiB
Go
142 lines
5.2 KiB
Go
// 仓库DAO层
|
||
// 职责:仓库CRUD、状态更新、批量更新库区/库位状态(状态联动)、更新容量
|
||
// 紧密耦合:service.Warehouse(状态联动)、service.Capacity(容量汇总)
|
||
// 注意:UpdateCapacityByUnitType使用嵌套路径更新map字段
|
||
package dao
|
||
|
||
import (
|
||
"assets/consts/public"
|
||
"assets/consts/stock"
|
||
"assets/model/config"
|
||
dto "assets/model/dto/stock"
|
||
entity "assets/model/entity/stock"
|
||
"context"
|
||
|
||
"gitea.com/red-future/common/db/mongo"
|
||
"gitea.com/red-future/common/utils"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"go.mongodb.org/mongo-driver/v2/bson"
|
||
)
|
||
|
||
var Warehouse = new(warehouse)
|
||
|
||
type warehouse struct{}
|
||
|
||
func (d *warehouse) Insert(ctx context.Context, req *dto.CreateWarehouseReq) (ids []interface{}, err error) {
|
||
var result *entity.Warehouse
|
||
if err = utils.Struct(req, &result); err != nil {
|
||
return
|
||
}
|
||
// 如果未传入状态,设置默认值为启用
|
||
if result.Status == "" {
|
||
result.Status = stock.WarehouseStatusEnabled
|
||
}
|
||
// 初始化Capacity为空map,避免后续UpdateCapacityByUnitType panic
|
||
if result.Capacity == nil {
|
||
emptyMap := make(map[stock.CapacityUnitType]config.Capacity)
|
||
result.Capacity = &emptyMap
|
||
}
|
||
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.WarehouseCollection)
|
||
return
|
||
}
|
||
|
||
func (d *warehouse) GetOne(ctx context.Context, req *dto.GetWarehouseReq) (res *entity.Warehouse, err error) {
|
||
filter := bson.M{"_id": req.Id}
|
||
err = mongo.DB().FindOne(ctx, filter, &res, public.WarehouseCollection)
|
||
return
|
||
}
|
||
|
||
func (d *warehouse) Update(ctx context.Context, req *dto.UpdateWarehouseReq) (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.WarehouseCollection)
|
||
return
|
||
}
|
||
|
||
func (d *warehouse) DeleteFake(ctx context.Context, req *dto.DeleteWarehouseReq) (err error) {
|
||
filter := bson.M{"_id": req.Id}
|
||
_, err = mongo.DB().DeleteSoft(ctx, filter, public.WarehouseCollection)
|
||
return
|
||
}
|
||
|
||
// UpdateStatus 更新仓库状态
|
||
func (d *warehouse) UpdateStatus(ctx context.Context, req *dto.UpdateWarehouseStatusReq) (err error) {
|
||
filter := bson.M{"_id": req.Id}
|
||
update := bson.M{"$set": bson.M{"status": req.Status}}
|
||
_, err = mongo.DB().Update(ctx, filter, update, public.WarehouseCollection)
|
||
return
|
||
}
|
||
|
||
func (d *warehouse) List(ctx context.Context, req *dto.ListWarehouseReq) (res []entity.Warehouse, total int64, err error) {
|
||
filter, err := d.buildListFilter(ctx, req)
|
||
if err != nil {
|
||
return
|
||
}
|
||
total, err = mongo.DB().Find(ctx, filter, &res, public.WarehouseCollection, req.Page, req.OrderBy)
|
||
return
|
||
}
|
||
|
||
func (d *warehouse) buildListFilter(ctx context.Context, req *dto.ListWarehouseReq) (filter bson.M, err error) {
|
||
_ = ctx
|
||
filter = bson.M{}
|
||
if !g.IsEmpty(req.Status) {
|
||
filter["status"] = req.Status
|
||
}
|
||
if !g.IsEmpty(req.Keyword) {
|
||
filter["$or"] = bson.A{
|
||
bson.M{"warehouseCode": bson.M{"$regex": req.Keyword, "$options": "i"}},
|
||
bson.M{"warehouseName": bson.M{"$regex": req.Keyword, "$options": "i"}},
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
// CountZonesByWarehouseId 统计仓库下的库区数量(用于删除前检查)
|
||
func (d *warehouse) CountZonesByWarehouseId(ctx context.Context, warehouseId string) (count int64, err error) {
|
||
filter := bson.M{"warehouseId": warehouseId}
|
||
count, err = mongo.DB().Count(ctx, filter, public.ZoneCollection)
|
||
return
|
||
}
|
||
|
||
// BatchUpdateZoneStatus 批量更新仓库下所有库区状态(用于状态联动)
|
||
// fromStatus 可选:指定时只更新当前状态为 fromStatus 的记录,避免覆盖其他状态
|
||
func (d *warehouse) BatchUpdateZoneStatus(ctx context.Context, warehouseId string, status stock.ZoneStatus, fromStatus ...stock.ZoneStatus) (modifiedCount int64, err error) {
|
||
filter := bson.M{"warehouseId": warehouseId}
|
||
if len(fromStatus) > 0 {
|
||
filter["status"] = fromStatus[0]
|
||
}
|
||
update := bson.M{"$set": bson.M{"status": status}}
|
||
modifiedCount, err = mongo.DB().Update(ctx, filter, update, public.ZoneCollection)
|
||
return
|
||
}
|
||
|
||
// UpdateCapacityByUnitType 更新仓库指定容量单位类型的容量
|
||
func (d *warehouse) UpdateCapacityByUnitType(ctx context.Context, warehouseId *bson.ObjectID, unitType stock.CapacityUnitType, currentCapacity int, maxCapacity int, capacityUnit string) (err error) {
|
||
filter := bson.M{"_id": warehouseId}
|
||
update := bson.M{
|
||
"$set": bson.M{
|
||
"capacity." + string(unitType) + ".currentCapacity": currentCapacity,
|
||
"capacity." + string(unitType) + ".maxCapacity": maxCapacity,
|
||
"capacity." + string(unitType) + ".capacityUnit": capacityUnit,
|
||
},
|
||
}
|
||
_, err = mongo.DB().Update(ctx, filter, update, public.WarehouseCollection)
|
||
return
|
||
}
|
||
|
||
// BatchUpdateLocationStatus 批量更新仓库下所有库位状态(用于状态联动)
|
||
// fromStatus 可选:指定时只更新当前状态为 fromStatus 的记录,避免覆盖其他状态
|
||
func (d *warehouse) BatchUpdateLocationStatus(ctx context.Context, warehouseId string, status stock.LocationStatus, fromStatus ...stock.LocationStatus) (modifiedCount int64, err error) {
|
||
filter := bson.M{"warehouseId": warehouseId}
|
||
if len(fromStatus) > 0 {
|
||
filter["status"] = fromStatus[0]
|
||
}
|
||
update := bson.M{"$set": bson.M{"status": status}}
|
||
modifiedCount, err = mongo.DB().Update(ctx, filter, update, public.LocationCollection)
|
||
return
|
||
}
|