warehouse_order.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/common/constant"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "Cold_Logistic/internal/pkg/utils/sqlutil"
  6. "Cold_Logistic/internal/server/infra/models"
  7. "context"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  10. "time"
  11. )
  12. // WarehouseOrderStore 仓库订单
  13. type WarehouseOrderStore interface {
  14. DbBaseStore
  15. BatchSave(ctx context.Context, values []*models.WarehouseOrder, omit ...string) error
  16. GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.WarehouseOrder, error)
  17. Page(ctx context.Context, dto WarehouseOrderPageDTO) ([]WarehouseOrderPageVO, int64, error)
  18. OrderOutHouse(ctx context.Context, orderId, houseId int) error
  19. FindByHouseIdAndOrderId(ctx context.Context, orderId, houseId int) (models.WarehouseOrder, error)
  20. CountWarehouseOrder(ctx context.Context, houseIds []int, status ...int) (map[int]int64, error)
  21. }
  22. var _ WarehouseOrderStore = &warehouseOrder{}
  23. type warehouseOrder struct {
  24. dbBase
  25. }
  26. func newWarehouseOrder(ds *DataStore) *warehouseOrder {
  27. return &warehouseOrder{dbBase: dbBase{
  28. store: ds,
  29. baseEntity: &models.WarehouseOrder{},
  30. }}
  31. }
  32. func (ds *DataStore) WarehouseOrder() WarehouseOrderStore {
  33. return newWarehouseOrder(ds)
  34. }
  35. func (a *warehouseOrder) BatchSave(ctx context.Context, values []*models.WarehouseOrder, omit ...string) error {
  36. if len(values) == 0 {
  37. return nil
  38. }
  39. db := a.store.optionDB(ctx)
  40. //tokenInfo := ctxtoken.GetTokenInfoFromContext(ctx)
  41. for i := range values {
  42. if values[i].Id == 0 {
  43. //values[i].CreatedBy = tokenInfo.AccountId
  44. values[i].CreatedTime.Time = time.Now()
  45. values[i].Deleted = models.DeleteNo
  46. } else {
  47. //values[i].UpdatedBy = tokenInfo.AccountId
  48. values[i].UpdatedTime.Time = time.Now()
  49. }
  50. }
  51. return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
  52. }
  53. func (a *warehouseOrder) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.WarehouseOrder, error) {
  54. res := make(map[int]models.WarehouseOrder, len(ids))
  55. if len(ids) == 0 {
  56. return res, nil
  57. }
  58. var list []models.WarehouseOrder
  59. db := a.store.optionDB(ctx)
  60. err := db.Table(a.baseEntity.TableName()).
  61. Where("deleted = ?", models.DeleteNo).
  62. Where("id IN (?)", ids).
  63. Omit(omit...).
  64. Find(&list).Error
  65. if err != nil {
  66. return res, err
  67. }
  68. for i := range list {
  69. res[list[i].Id] = list[i]
  70. }
  71. return res, nil
  72. }
  73. type WarehouseOrderPageDTO struct {
  74. Page core.Page
  75. WarehouseId int
  76. OrderNo string
  77. Status int
  78. TimeStart models.MyTime
  79. TimeEnd models.MyTime
  80. }
  81. type WarehouseOrderPageVO struct {
  82. OrderId int `gorm:"column:order_id" json:"orderId"` //订单id
  83. OrderNo string `gorm:"column:order_no" json:"orderNo"` //订单号
  84. Status int `gorm:"column:status" json:"status"` //状态:1-库中 2-已出库
  85. StorageTime models.MyTime `gorm:"column:storage_time" json:"storageTime"` //入库时间
  86. StorageBy int `gorm:"column:storage_by" json:"storageBy"` //入库人
  87. OutboundTime models.MyTime `gorm:"column:outbound_time" json:"outboundTime"` //出库时间
  88. OutboundBy int `gorm:"column:outbound_by" json:"outboundBy"` //出库人
  89. }
  90. func (a *warehouseOrder) Page(ctx context.Context, dto WarehouseOrderPageDTO) ([]WarehouseOrderPageVO, int64, error) {
  91. stmt := a.store.optionDB(ctx).Table(a.baseEntity.TableName()+" AS w").
  92. Joins("LEFT JOIN express_order AS eo ON eo.id = w.order_id ").
  93. Where("w.deleted = ?", models.DeleteNo).
  94. Where("eo.deleted = ?", models.DeleteNo).
  95. Where("w.warehouse_id = ?", dto.WarehouseId).
  96. Where("w.status = ?", dto.Status)
  97. stmt.Select("id")
  98. if dto.OrderNo != "" {
  99. stmt.Where("w.order_no LIKE ?", sqlutil.LikeFormat(dto.OrderNo))
  100. }
  101. if !dto.TimeStart.IsZero() {
  102. stmt.Where("w.storage_time >= ?", dto.TimeStart)
  103. }
  104. if !dto.TimeEnd.IsZero() {
  105. stmt.Where("w.storage_time >= ?", dto.TimeEnd)
  106. }
  107. var total int64
  108. var ret []WarehouseOrderPageVO
  109. if err := stmt.Count(&total).Error; err != nil {
  110. return nil, 0, err
  111. }
  112. if total == 0 {
  113. return ret, total, nil
  114. }
  115. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  116. if dto.Status == constant.WarehouseStatusIn {
  117. stmt.Order("w.storage_time asc")
  118. }
  119. if dto.Status == constant.WarehouseStatusIn {
  120. stmt.Order("w.outbound_time desc")
  121. }
  122. if err := stmt.Find(&ret).Error; err != nil {
  123. return nil, 0, err
  124. }
  125. return ret, total, nil
  126. }
  127. func (a *warehouseOrder) OrderOutHouse(ctx context.Context, orderId, houseId int) error {
  128. t := models.NewMyTime(time.Now())
  129. tokenInfo := global.GetTokenInfoFromContext(ctx)
  130. up := models.WarehouseOrder{
  131. UpdatedTime: t,
  132. UpdatedBy: tokenInfo.AccountId,
  133. Status: constant.WarehouseStatusOut,
  134. OutboundTime: t,
  135. OutboundBy: tokenInfo.AccountId,
  136. }
  137. err := a.store.optionDB(ctx).Model(a.baseEntity).
  138. Where("deleted = ?", models.DeleteNo).
  139. Where("status = ?", constant.WarehouseStatusIn).
  140. Where("warehouse_id = ?", houseId).
  141. Where("order_id = ?", orderId).
  142. Updates(up).Error
  143. return errors.WithStackOnce(err)
  144. }
  145. func (a *warehouseOrder) FindByHouseIdAndOrderId(ctx context.Context, orderId, houseId int) (models.WarehouseOrder, error) {
  146. ret := models.WarehouseOrder{}
  147. err := a.store.optionDB(ctx).Model(a.baseEntity).
  148. Where("deleted = ?", models.DeleteNo).
  149. Where("warehouse_id = ?", houseId).
  150. Where("order_id = ?", orderId).
  151. Find(&ret).Error
  152. return ret, errors.WithStackOnce(err)
  153. }
  154. type WarehouseOrderCount struct {
  155. WarehouseId int `gorm:"column:warehouse_id"`
  156. OrderNum int64 `gorm:"column:order_num"`
  157. }
  158. func (a *warehouseOrder) CountWarehouseOrder(ctx context.Context, houseIds []int, status ...int) (map[int]int64, error) {
  159. stmt := a.store.optionDB(ctx).Model(a.baseEntity).
  160. Where("deleted = ?", models.DeleteNo).
  161. Where("warehouse_id IN(?) ", houseIds)
  162. if len(status) > 0 {
  163. stmt.Where("status IN(?) ", status)
  164. }
  165. stmt.Select("warehouse_id, COUNT(DISTINCT order_id) AS order_num")
  166. stmt.Group("warehouse_id")
  167. ent := make([]WarehouseOrderCount, 0, len(houseIds))
  168. err := stmt.Find(&ent).Error
  169. ret := make(map[int]int64)
  170. for _, v := range ent {
  171. ret[v.WarehouseId] = v.OrderNum
  172. }
  173. return ret, errors.WithStackOnce(err)
  174. }