163 lines
5.8 KiB
Go
163 lines
5.8 KiB
Go
|
|
// 库区DAO层
|
|||
|
|
// 职责:库区CRUD、状态更新、批量更新库位状态(状态联动)、更新容量
|
|||
|
|
// 紧密耦合:service.Zone(状态联动)、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 Zone = new(zone)
|
|||
|
|
|
|||
|
|
type zone struct{}
|
|||
|
|
|
|||
|
|
func (d *zone) Insert(ctx context.Context, req *dto.CreateZoneReq) (ids []interface{}, err error) {
|
|||
|
|
var result *entity.Zone
|
|||
|
|
if err = utils.Struct(req, &result); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
// 如果未传入状态,设置默认值为启用
|
|||
|
|
if result.Status == "" {
|
|||
|
|
result.Status = stock.ZoneStatusEnabled
|
|||
|
|
}
|
|||
|
|
// 初始化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.ZoneCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (d *zone) GetOne(ctx context.Context, req *dto.GetZoneReq) (res *entity.Zone, err error) {
|
|||
|
|
filter := bson.M{"_id": req.Id}
|
|||
|
|
err = mongo.DB().FindOne(ctx, filter, &res, public.ZoneCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (d *zone) Update(ctx context.Context, req *dto.UpdateZoneReq) (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.ZoneCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (d *zone) DeleteFake(ctx context.Context, req *dto.DeleteZoneReq) (err error) {
|
|||
|
|
filter := bson.M{"_id": req.Id}
|
|||
|
|
_, err = mongo.DB().DeleteSoft(ctx, filter, public.ZoneCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateStatus 更新库区状态
|
|||
|
|
func (d *zone) UpdateStatus(ctx context.Context, req *dto.UpdateZoneStatusReq) (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.ZoneCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (d *zone) List(ctx context.Context, req *dto.ListZoneReq) (res []entity.Zone, total int64, err error) {
|
|||
|
|
filter, err := d.buildListFilter(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
total, err = mongo.DB().Find(ctx, filter, &res, public.ZoneCollection, req.Page, req.OrderBy)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (d *zone) buildListFilter(ctx context.Context, req *dto.ListZoneReq) (filter bson.M, err error) {
|
|||
|
|
_ = ctx
|
|||
|
|
filter = bson.M{}
|
|||
|
|
// 兼容单值和数组参数(优先使用数组)
|
|||
|
|
if len(req.WarehouseIds) > 0 {
|
|||
|
|
filter["warehouseId"] = bson.M{"$in": req.WarehouseIds}
|
|||
|
|
} else if !g.IsEmpty(req.WarehouseId) {
|
|||
|
|
filter["warehouseId"] = req.WarehouseId
|
|||
|
|
}
|
|||
|
|
if !g.IsEmpty(req.ZoneType) {
|
|||
|
|
filter["zoneType"] = req.ZoneType
|
|||
|
|
}
|
|||
|
|
if !g.IsEmpty(req.Status) {
|
|||
|
|
filter["status"] = req.Status
|
|||
|
|
}
|
|||
|
|
if !g.IsEmpty(req.Keyword) {
|
|||
|
|
filter["$or"] = bson.A{
|
|||
|
|
bson.M{"zoneCode": bson.M{"$regex": req.Keyword, "$options": "i"}},
|
|||
|
|
bson.M{"zoneName": bson.M{"$regex": req.Keyword, "$options": "i"}},
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CountLocationsByZoneId 统计库区下的库位数量(用于删除前检查)
|
|||
|
|
func (d *zone) CountLocationsByZoneId(ctx context.Context, zoneId string) (count int64, err error) {
|
|||
|
|
filter := bson.M{"zoneId": zoneId}
|
|||
|
|
count, err = mongo.DB().Count(ctx, filter, public.LocationCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// CountByWarehouseId 统计仓库下的库区数量(用于删除前检查)
|
|||
|
|
func (d *zone) CountByWarehouseId(ctx context.Context, warehouseId string) (count int64, err error) {
|
|||
|
|
filter := bson.M{"warehouseId": warehouseId}
|
|||
|
|
count, err = mongo.DB().Count(ctx, filter, public.ZoneCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BatchUpdateStatusByWarehouseId 批量更新仓库下所有库区状态(用于状态联动)
|
|||
|
|
func (d *zone) BatchUpdateStatusByWarehouseId(ctx context.Context, warehouseId string, status stock.ZoneStatus) (modifiedCount int64, err error) {
|
|||
|
|
filter := bson.M{"warehouseId": warehouseId}
|
|||
|
|
update := bson.M{"$set": bson.M{"status": status}}
|
|||
|
|
modifiedCount, err = mongo.DB().Update(ctx, filter, update, public.ZoneCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BatchUpdateLocationStatus 批量更新库区下所有库位状态(用于状态联动)
|
|||
|
|
// fromStatus 可选:指定时只更新当前状态为 fromStatus 的记录,避免覆盖其他状态
|
|||
|
|
func (d *zone) BatchUpdateLocationStatus(ctx context.Context, zoneId string, status stock.LocationStatus, fromStatus ...stock.LocationStatus) (modifiedCount int64, err error) {
|
|||
|
|
filter := bson.M{"zoneId": zoneId}
|
|||
|
|
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
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateCapacityByUnitType 更新库区指定容量单位类型的容量
|
|||
|
|
func (d *zone) UpdateCapacityByUnitType(ctx context.Context, zoneId *bson.ObjectID, unitType stock.CapacityUnitType, currentCapacity int, maxCapacity int, capacityUnit string) (err error) {
|
|||
|
|
filter := bson.M{"_id": zoneId}
|
|||
|
|
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.ZoneCollection)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ListByWarehouseAndUnitType 按仓库ID查询所有库区(用于汇总仓库容量)
|
|||
|
|
func (d *zone) ListByWarehouseAndUnitType(ctx context.Context, warehouseId string) (res []entity.Zone, err error) {
|
|||
|
|
filter := bson.M{
|
|||
|
|
"warehouseId": warehouseId,
|
|||
|
|
}
|
|||
|
|
_, err = mongo.DB().Find(ctx, filter, &res, public.ZoneCollection, nil, nil)
|
|||
|
|
return
|
|||
|
|
}
|