2026-03-18 10:18:03 +08:00
|
|
|
|
// 批次库存DAO层(逻辑库存)
|
|
|
|
|
|
// 职责:批次CRUD、使用$inc原子操作更新数量
|
|
|
|
|
|
// 紧密耦合:service.StockBatch、service.StockManage(入库出库)
|
|
|
|
|
|
// 注意:Update使用$inc原子操作,GetOne使用NoCache()跳过缓存
|
|
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"assets/consts/public"
|
|
|
|
|
|
dto "assets/model/dto/stock"
|
|
|
|
|
|
entity "assets/model/entity/stock"
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
2026-06-10 15:40:17 +08:00
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
2026-03-22 20:08:32 +08:00
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2026-03-18 10:18:03 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var StockBatch = new(stockBatch)
|
|
|
|
|
|
|
|
|
|
|
|
type stockBatch struct {
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Insert 插入
|
2026-03-22 20:08:32 +08:00
|
|
|
|
func (d *stockBatch) Insert(ctx context.Context, req *dto.CreateSockBatchReq) (id int64, err error) {
|
|
|
|
|
|
var res *entity.StockBatch
|
|
|
|
|
|
if err = gconv.Struct(req, &res); err != nil {
|
2026-03-18 10:18:03 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-03-27 11:10:11 +08:00
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameStockBatch).Data(&res).Insert()
|
2026-03-22 20:08:32 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
2026-03-18 10:18:03 +08:00
|
|
|
|
}
|
2026-03-22 20:08:32 +08:00
|
|
|
|
return r.LastInsertId()
|
2026-03-18 10:18:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-22 20:08:32 +08:00
|
|
|
|
func (d *stockBatch) Update(ctx context.Context, req *dto.UpdateSockBatchReq) (rows int64, err error) {
|
2026-03-27 14:55:44 +08:00
|
|
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameStockBatch).Where(entity.StockBatchCol.Id, req.Id)
|
2026-03-22 20:08:32 +08:00
|
|
|
|
model.Data(entity.StockBatchCol.BatchQty, &gdb.Counter{
|
|
|
|
|
|
Field: entity.StockBatchCol.BatchQty,
|
|
|
|
|
|
Value: gconv.Float64(req.BatchQty),
|
|
|
|
|
|
})
|
|
|
|
|
|
model.Data(entity.StockBatchCol.AvailableQty, &gdb.Counter{
|
|
|
|
|
|
Field: entity.StockBatchCol.AvailableQty,
|
|
|
|
|
|
Value: gconv.Float64(req.AvailableQty),
|
|
|
|
|
|
})
|
|
|
|
|
|
r, err := model.Update()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return r.RowsAffected()
|
2026-03-18 10:18:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-22 20:08:32 +08:00
|
|
|
|
// One 根据批次号查询(使用NoCache跳过缓存,确保获取最新数据)
|
|
|
|
|
|
func (d *stockBatch) One(ctx context.Context, req *dto.GetSockBatchReq, fields ...string) (res *entity.StockBatch, err error) {
|
2026-03-27 11:10:11 +08:00
|
|
|
|
model := gfdb.DB(ctx).Model(ctx, public.TableNameStockBatch)
|
2026-03-22 20:08:32 +08:00
|
|
|
|
if !g.IsEmpty(req.Id) {
|
|
|
|
|
|
model.Where(entity.StockBatchCol.Id, req.Id)
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.BatchNo) {
|
|
|
|
|
|
model.Where(entity.StockBatchCol.BatchNo, req.BatchNo)
|
2026-03-18 10:18:03 +08:00
|
|
|
|
}
|
2026-03-22 20:08:32 +08:00
|
|
|
|
r, err := model.Fields(fields).One()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
2026-03-18 10:18:03 +08:00
|
|
|
|
}
|
2026-03-22 20:08:32 +08:00
|
|
|
|
err = r.Struct(&res)
|
2026-03-18 10:18:03 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|