package dao import ( "Cold_Logistic/internal/pkg/common/constant" "Cold_Logistic/internal/pkg/common/global" "Cold_Logistic/internal/pkg/utils/sqlutil" "Cold_Logistic/internal/server/infra/models" "context" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" "time" ) // WarehouseOrderStore 仓库订单 type WarehouseOrderStore interface { DbBaseStore BatchSave(ctx context.Context, values []*models.WarehouseOrder, omit ...string) error GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.WarehouseOrder, error) Page(ctx context.Context, dto WarehouseOrderPageDTO) ([]WarehouseOrderPageVO, int64, error) OrderOutHouse(ctx context.Context, orderId, houseId int) error FindByHouseIdAndOrderId(ctx context.Context, orderId, houseId int) (models.WarehouseOrder, error) CountWarehouseOrder(ctx context.Context, houseIds []int, status ...int) (map[int]int64, error) } var _ WarehouseOrderStore = &warehouseOrder{} type warehouseOrder struct { dbBase } func newWarehouseOrder(ds *DataStore) *warehouseOrder { return &warehouseOrder{dbBase: dbBase{ store: ds, baseEntity: &models.WarehouseOrder{}, }} } func (ds *DataStore) WarehouseOrder() WarehouseOrderStore { return newWarehouseOrder(ds) } func (a *warehouseOrder) BatchSave(ctx context.Context, values []*models.WarehouseOrder, omit ...string) error { if len(values) == 0 { return nil } db := a.store.optionDB(ctx) //tokenInfo := ctxtoken.GetTokenInfoFromContext(ctx) for i := range values { if values[i].Id == 0 { //values[i].CreatedBy = tokenInfo.AccountId values[i].CreatedTime.Time = time.Now() values[i].Deleted = models.DeleteNo } else { //values[i].UpdatedBy = tokenInfo.AccountId values[i].UpdatedTime.Time = time.Now() } } return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error } func (a *warehouseOrder) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.WarehouseOrder, error) { res := make(map[int]models.WarehouseOrder, len(ids)) if len(ids) == 0 { return res, nil } var list []models.WarehouseOrder db := a.store.optionDB(ctx) err := db.Table(a.baseEntity.TableName()). Where("deleted = ?", models.DeleteNo). Where("id IN (?)", ids). Omit(omit...). Find(&list).Error if err != nil { return res, err } for i := range list { res[list[i].Id] = list[i] } return res, nil } type WarehouseOrderPageDTO struct { Page core.Page WarehouseId int OrderNo string Status int TimeStart models.MyTime TimeEnd models.MyTime } type WarehouseOrderPageVO struct { OrderId int `gorm:"column:order_id" json:"orderId"` //订单id OrderNo string `gorm:"column:order_no" json:"orderNo"` //订单号 Status int `gorm:"column:status" json:"status"` //状态:1-库中 2-已出库 StorageTime models.MyTime `gorm:"column:storage_time" json:"storageTime"` //入库时间 StorageBy int `gorm:"column:storage_by" json:"storageBy"` //入库人 OutboundTime models.MyTime `gorm:"column:outbound_time" json:"outboundTime"` //出库时间 OutboundBy int `gorm:"column:outbound_by" json:"outboundBy"` //出库人 } func (a *warehouseOrder) Page(ctx context.Context, dto WarehouseOrderPageDTO) ([]WarehouseOrderPageVO, int64, error) { stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()+" AS w"). Joins("LEFT JOIN express_order AS eo ON eo.id = w.order_id "). Where("w.deleted = ?", models.DeleteNo). Where("eo.deleted = ?", models.DeleteNo). Where("w.warehouse_id = ?", dto.WarehouseId). Where("w.status = ?", dto.Status) stmt.Select("id") if dto.OrderNo != "" { stmt.Where("w.order_no LIKE ?", sqlutil.LikeFormat(dto.OrderNo)) } if !dto.TimeStart.IsZero() { stmt.Where("w.storage_time >= ?", dto.TimeStart) } if !dto.TimeEnd.IsZero() { stmt.Where("w.storage_time >= ?", dto.TimeEnd) } var total int64 var ret []WarehouseOrderPageVO if err := stmt.Count(&total).Error; err != nil { return nil, 0, err } if total == 0 { return ret, total, nil } stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset) if dto.Status == constant.WarehouseStatusIn { stmt.Order("w.storage_time asc") } if dto.Status == constant.WarehouseStatusIn { stmt.Order("w.outbound_time desc") } if err := stmt.Find(&ret).Error; err != nil { return nil, 0, err } return ret, total, nil } func (a *warehouseOrder) OrderOutHouse(ctx context.Context, orderId, houseId int) error { t := models.NewMyTime(time.Now()) tokenInfo := global.GetTokenInfoFromContext(ctx) up := models.WarehouseOrder{ UpdatedTime: t, UpdatedBy: tokenInfo.AccountId, Status: constant.WarehouseStatusOut, OutboundTime: t, OutboundBy: tokenInfo.AccountId, } err := a.store.optionDB(ctx).Model(a.baseEntity). Where("deleted = ?", models.DeleteNo). Where("status = ?", constant.WarehouseStatusIn). Where("warehouse_id = ?", houseId). Where("order_id = ?", orderId). Updates(up).Error return errors.WithStackOnce(err) } func (a *warehouseOrder) FindByHouseIdAndOrderId(ctx context.Context, orderId, houseId int) (models.WarehouseOrder, error) { ret := models.WarehouseOrder{} err := a.store.optionDB(ctx).Model(a.baseEntity). Where("deleted = ?", models.DeleteNo). Where("warehouse_id = ?", houseId). Where("order_id = ?", orderId). Find(&ret).Error return ret, errors.WithStackOnce(err) } type WarehouseOrderCount struct { WarehouseId int `gorm:"column:warehouse_id"` OrderNum int64 `gorm:"column:order_num"` } func (a *warehouseOrder) CountWarehouseOrder(ctx context.Context, houseIds []int, status ...int) (map[int]int64, error) { stmt := a.store.optionDB(ctx).Model(a.baseEntity). Where("deleted = ?", models.DeleteNo). Where("warehouse_id IN(?) ", houseIds) if len(status) > 0 { stmt.Where("status IN(?) ", status) } stmt.Select("warehouse_id, COUNT(DISTINCT order_id) AS order_num") stmt.Group("warehouse_id") ent := make([]WarehouseOrderCount, 0, len(houseIds)) err := stmt.Find(&ent).Error ret := make(map[int]int64) for _, v := range ent { ret[v.WarehouseId] = v.OrderNum } return ret, errors.WithStackOnce(err) }