192 lines
7.2 KiB
Go
192 lines
7.2 KiB
Go
// 库位DAO层
|
||
// 职责:库位CRUD、状态更新、批量状态更新、删除前检查(3个库存集合)、更新容量
|
||
// 紧密耦合:service.Location(删除检查)、service.Capacity(容量计算入口)
|
||
// 注意:DeleteFake前需检查StockDetails/StockBatch/PrivateStock三个集合
|
||
package dao
|
||
|
||
import (
|
||
"assets/consts/public"
|
||
"assets/consts/stock"
|
||
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 Location = new(location)
|
||
|
||
type location struct{}
|
||
|
||
func (d *location) Insert(ctx context.Context, req *dto.CreateLocationReq) (ids []interface{}, err error) {
|
||
var result *entity.Location
|
||
if err = utils.Struct(req, &result); err != nil {
|
||
return
|
||
}
|
||
// 如果未传入状态,设置默认值为空闲
|
||
if result.Status == "" {
|
||
result.Status = stock.LocationStatusIdle
|
||
}
|
||
// 初始容量默认为0
|
||
//if result.CurrentCapacity == 0 {
|
||
// result.CurrentCapacity = 0
|
||
//}
|
||
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.LocationCollection)
|
||
return
|
||
}
|
||
|
||
func (d *location) GetOne(ctx context.Context, req *dto.GetLocationReq) (res *entity.Location, err error) {
|
||
filter := bson.M{"_id": req.Id}
|
||
err = mongo.DB().FindOne(ctx, filter, &res, public.LocationCollection)
|
||
return
|
||
}
|
||
|
||
func (d *location) Update(ctx context.Context, req *dto.UpdateLocationReq) (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.LocationCollection)
|
||
return
|
||
}
|
||
|
||
func (d *location) DeleteFake(ctx context.Context, req *dto.DeleteLocationReq) (err error) {
|
||
filter := bson.M{"_id": req.Id}
|
||
_, err = mongo.DB().DeleteSoft(ctx, filter, public.LocationCollection)
|
||
return
|
||
}
|
||
|
||
// UpdateStatus 更新库位状态
|
||
func (d *location) UpdateStatus(ctx context.Context, req *dto.UpdateLocationStatusReq) (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.LocationCollection)
|
||
return
|
||
}
|
||
|
||
func (d *location) List(ctx context.Context, req *dto.ListLocationReq) (res []entity.Location, total int64, err error) {
|
||
filter, err := d.buildListFilter(ctx, req)
|
||
if err != nil {
|
||
return
|
||
}
|
||
total, err = mongo.DB().Find(ctx, filter, &res, public.LocationCollection, req.Page, req.OrderBy)
|
||
return
|
||
}
|
||
|
||
func (d *location) buildListFilter(ctx context.Context, req *dto.ListLocationReq) (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 len(req.ZoneIds) > 0 {
|
||
filter["zoneId"] = bson.M{"$in": req.ZoneIds}
|
||
} else if !g.IsEmpty(req.ZoneId) {
|
||
filter["zoneId"] = req.ZoneId
|
||
}
|
||
if !g.IsEmpty(req.LocationType) {
|
||
filter["locationType"] = req.LocationType
|
||
}
|
||
if !g.IsEmpty(req.Status) {
|
||
filter["status"] = req.Status
|
||
}
|
||
if !g.IsEmpty(req.Keyword) {
|
||
filter["$or"] = bson.A{
|
||
bson.M{"locationCode": bson.M{"$regex": req.Keyword, "$options": "i"}},
|
||
bson.M{"locationName": bson.M{"$regex": req.Keyword, "$options": "i"}},
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
// CountByZoneId 统计库区下的库位数量(用于删除前检查)
|
||
func (d *location) CountByZoneId(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 *location) CountByWarehouseId(ctx context.Context, warehouseId string) (count int64, err error) {
|
||
filter := bson.M{"warehouseId": warehouseId}
|
||
count, err = mongo.DB().Count(ctx, filter, public.LocationCollection)
|
||
return
|
||
}
|
||
|
||
// BatchUpdateStatusByZoneId 批量更新库区下所有库位状态(用于状态联动)
|
||
func (d *location) BatchUpdateStatusByZoneId(ctx context.Context, zoneId string, status stock.LocationStatus) (modifiedCount int64, err error) {
|
||
filter := bson.M{"zoneId": zoneId}
|
||
update := bson.M{"$set": bson.M{"status": status}}
|
||
modifiedCount, err = mongo.DB().Update(ctx, filter, update, public.LocationCollection)
|
||
return
|
||
}
|
||
|
||
// BatchUpdateStatusByWarehouseId 批量更新仓库下所有库位状态(用于状态联动)
|
||
func (d *location) BatchUpdateStatusByWarehouseId(ctx context.Context, warehouseId string, status stock.LocationStatus) (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.LocationCollection)
|
||
return
|
||
}
|
||
|
||
// DeleteByZoneId 批量删除库区下所有库位(用于级联删除)
|
||
func (d *location) DeleteByZoneId(ctx context.Context, zoneId string) (modifiedCount int64, err error) {
|
||
filter := bson.M{"zoneId": zoneId}
|
||
modifiedCount, err = mongo.DB().DeleteSoft(ctx, filter, public.LocationCollection)
|
||
return
|
||
}
|
||
|
||
// DeleteByWarehouseId 批量删除仓库下所有库位(用于级联删除)
|
||
func (d *location) DeleteByWarehouseId(ctx context.Context, warehouseId string) (modifiedCount int64, err error) {
|
||
filter := bson.M{"warehouseId": warehouseId}
|
||
modifiedCount, err = mongo.DB().DeleteSoft(ctx, filter, public.LocationCollection)
|
||
return
|
||
}
|
||
|
||
// CountStockDetailsByLocationId 统计库位上的库存明细数量(用于删除前检查)
|
||
func (d *location) CountStockDetailsByLocationId(ctx context.Context, locationId string) (count int64, err error) {
|
||
filter := bson.M{"locationId": locationId}
|
||
count, err = mongo.DB().Count(ctx, filter, public.StockDetailsCollection)
|
||
return
|
||
}
|
||
|
||
// CountStockBatchByLocationId 统计库位上的批次库存数量(用于删除前检查)
|
||
func (d *location) CountStockBatchByLocationId(ctx context.Context, locationId string) (count int64, err error) {
|
||
filter := bson.M{"locationId": locationId}
|
||
count, err = mongo.DB().Count(ctx, filter, public.StockBatchCollection)
|
||
return
|
||
}
|
||
|
||
// CountPrivateStockByLocationId 统计库位上的私域库存数量(用于删除前检查)
|
||
func (d *location) CountPrivateStockByLocationId(ctx context.Context, locationId string) (count int64, err error) {
|
||
filter := bson.M{"locationId": locationId}
|
||
count, err = mongo.DB().Count(ctx, filter, public.PrivateStockCollection)
|
||
return
|
||
}
|
||
|
||
// UpdateCapacity 更新库位容量
|
||
func (d *location) UpdateCapacity(ctx context.Context, locationId *bson.ObjectID, currentCapacity int) (err error) {
|
||
filter := bson.M{"_id": locationId}
|
||
update := bson.M{"$set": bson.M{"capacity.currentCapacity": currentCapacity}}
|
||
_, err = mongo.DB().Update(ctx, filter, update, public.LocationCollection)
|
||
return
|
||
}
|
||
|
||
// ListByZoneAndUnitType 按库区和容量单位类型查询库位列表
|
||
func (d *location) ListByZoneAndUnitType(ctx context.Context, zoneId *bson.ObjectID, unitType stock.CapacityUnitType) (res []entity.Location, err error) {
|
||
filter := bson.M{
|
||
"zoneId": zoneId,
|
||
"capacityUnitType": unitType,
|
||
}
|
||
_, err = mongo.DB().Find(ctx, filter, &res, public.LocationCollection, nil, nil)
|
||
return
|
||
}
|