93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
|
|
// 库位服务
|
|||
|
|
// 职责:库位CRUD、状态更新、删除前检查(3个库存集合)
|
|||
|
|
// 调用链:Delete → CountStockDetailsByLocationId/CountStockBatchByLocationId/CountPrivateStockByLocationId
|
|||
|
|
// 紧密耦合:dao.Location、Capacity(容量更新入口)
|
|||
|
|
// 注意:删除前检查StockDetails/StockBatch/PrivateStock三个集合是否有库存
|
|||
|
|
package service
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
dao "assets/dao/stock"
|
|||
|
|
dto "assets/model/dto/stock"
|
|||
|
|
"context"
|
|||
|
|
|
|||
|
|
"gitea.com/red-future/common/utils"
|
|||
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|||
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type location struct{}
|
|||
|
|
|
|||
|
|
var Location = new(location)
|
|||
|
|
|
|||
|
|
func (s *location) Create(ctx context.Context, req *dto.CreateLocationReq) (res *dto.CreateLocationRes, err error) {
|
|||
|
|
ids, err := dao.Location.Insert(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
id := ids[0].(bson.ObjectID)
|
|||
|
|
res = &dto.CreateLocationRes{
|
|||
|
|
Id: &id,
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *location) Update(ctx context.Context, req *dto.UpdateLocationReq) error {
|
|||
|
|
return dao.Location.Update(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *location) Delete(ctx context.Context, req *dto.DeleteLocationReq) error {
|
|||
|
|
locationId := req.Id.Hex()
|
|||
|
|
// 检查库位上是否有库存(3个集合)
|
|||
|
|
stockDetailsCount, err := dao.Location.CountStockDetailsByLocationId(ctx, locationId)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
if stockDetailsCount > 0 {
|
|||
|
|
return gerror.Newf("库位上存在%d件库存明细,无法删除", stockDetailsCount)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
stockBatchCount, err := dao.Location.CountStockBatchByLocationId(ctx, locationId)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
if stockBatchCount > 0 {
|
|||
|
|
return gerror.Newf("库位上存在%d个批次库存,无法删除", stockBatchCount)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
privateStockCount, err := dao.Location.CountPrivateStockByLocationId(ctx, locationId)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
if privateStockCount > 0 {
|
|||
|
|
return gerror.Newf("库位上存在%d件实物库存批次,无法删除", privateStockCount)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return dao.Location.DeleteFake(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateStatus 更新库位状态(单独的状态修改接口)
|
|||
|
|
func (s *location) UpdateStatus(ctx context.Context, req *dto.UpdateLocationStatusReq) error {
|
|||
|
|
return dao.Location.UpdateStatus(ctx, req)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *location) GetOne(ctx context.Context, req *dto.GetLocationReq) (res *dto.GetLocationRes, err error) {
|
|||
|
|
one, err := dao.Location.GetOne(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
err = utils.Struct(one, &res)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (s *location) List(ctx context.Context, req *dto.ListLocationReq) (res *dto.ListLocationRes, err error) {
|
|||
|
|
list, total, err := dao.Location.List(ctx, req)
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
res = &dto.ListLocationRes{
|
|||
|
|
Total: total,
|
|||
|
|
}
|
|||
|
|
err = utils.Struct(list, &res.List)
|
|||
|
|
return
|
|||
|
|
}
|