warehouse.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package dao
  2. import (
  3. "Cold_Logistic/internal/pkg/common/global"
  4. "Cold_Logistic/internal/pkg/utils/sqlutil"
  5. "Cold_Logistic/internal/server/infra/models"
  6. "context"
  7. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  9. "gorm.io/datatypes"
  10. "time"
  11. )
  12. // WarehouseStore 仓库表
  13. type WarehouseStore interface {
  14. DbBaseStore
  15. BatchSave(ctx context.Context, values []*models.Warehouse, omit ...string) error
  16. GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Warehouse, error)
  17. Page(ctx context.Context, dto WarehousePageDTO) ([]models.Warehouse, int64, error)
  18. FindByManageUuid(ctx context.Context, uuid string) (models.Warehouse, error)
  19. CountByNameOrSnCode(ctx context.Context, name, snCode string, exclude ...int) (int64, error)
  20. }
  21. var _ WarehouseStore = &warehouse{}
  22. type warehouse struct {
  23. dbBase
  24. }
  25. func newWarehouse(ds *DataStore) *warehouse {
  26. return &warehouse{dbBase: dbBase{
  27. store: ds,
  28. baseEntity: &models.Warehouse{},
  29. }}
  30. }
  31. func (ds *DataStore) Warehouse() WarehouseStore {
  32. return newWarehouse(ds)
  33. }
  34. func (a *warehouse) BatchSave(ctx context.Context, values []*models.Warehouse, omit ...string) error {
  35. if len(values) == 0 {
  36. return nil
  37. }
  38. db := a.store.optionDB(ctx)
  39. tokenInfo := global.GetTokenInfoFromContext(ctx)
  40. for i := range values {
  41. if values[i].Id == 0 {
  42. values[i].CreatedBy = tokenInfo.AccountId
  43. values[i].CreatedTime.Time = time.Now()
  44. values[i].Deleted = models.DeleteNo
  45. } else {
  46. values[i].UpdatedBy = tokenInfo.AccountId
  47. values[i].UpdatedTime.Time = time.Now()
  48. }
  49. }
  50. return db.Table(a.baseEntity.TableName()).Omit(omit...).Save(&values).Error
  51. }
  52. func (a *warehouse) GetMapByIds(ctx context.Context, ids []int, omit ...string) (map[int]models.Warehouse, error) {
  53. res := make(map[int]models.Warehouse, len(ids))
  54. if len(ids) == 0 {
  55. return res, nil
  56. }
  57. var list []models.Warehouse
  58. db := a.store.optionDB(ctx)
  59. err := db.Table(a.baseEntity.TableName()).
  60. Where("deleted = ?", models.DeleteNo).
  61. Where("id IN (?)", ids).
  62. Omit(omit...).
  63. Find(&list).Error
  64. if err != nil {
  65. return res, err
  66. }
  67. for i := range list {
  68. res[list[i].Id] = list[i]
  69. }
  70. return res, nil
  71. }
  72. type WarehousePageDTO struct {
  73. Page core.Page
  74. Name string
  75. SnCode string
  76. }
  77. func (a *warehouse) Page(ctx context.Context, dto WarehousePageDTO) ([]models.Warehouse, int64, error) {
  78. db := a.store.optionDB(ctx, a.store.withCompanyDataPermis(ctx, "pid"))
  79. stmt := db.Model(&models.Warehouse{}).
  80. Where("deleted = ?", models.DeleteNo)
  81. stmt.Select("id, name, sn_code, address, manager_uuid")
  82. if dto.Name != "" {
  83. stmt.Where("name LIKE ?", sqlutil.LikeFormat(dto.Name))
  84. }
  85. if dto.SnCode != "" {
  86. stmt.Where("sn_code LIKE ?", sqlutil.LikeFormat(dto.SnCode))
  87. }
  88. var total int64
  89. var ret []models.Warehouse
  90. if err := stmt.Count(&total).Error; err != nil {
  91. return nil, 0, err
  92. }
  93. if total == 0 {
  94. return ret, total, nil
  95. }
  96. stmt.Limit(dto.Page.Size).Offset(dto.Page.Offset)
  97. stmt.Order("id desc")
  98. if err := stmt.Find(&ret).Error; err != nil {
  99. return nil, 0, err
  100. }
  101. return ret, total, nil
  102. }
  103. func (a *warehouse) FindByManageUuid(ctx context.Context, uuid string) (models.Warehouse, error) {
  104. ret := models.Warehouse{}
  105. err := a.store.optionDB(ctx).Model(&models.Warehouse{}).
  106. Where("deleted = ?", models.DeleteNo).
  107. Where(datatypes.JSONArrayQuery("manager_uuid").Contains(uuid)).
  108. Find(&ret).Error
  109. return ret, errors.WithStackOnce(err)
  110. }
  111. func (a *warehouse) CountByNameOrSnCode(ctx context.Context, name, snCode string, exclude ...int) (int64, error) {
  112. var ret int64
  113. stmt := a.store.optionDB(ctx).Model(&models.Warehouse{}).
  114. Where("deleted = ?", models.DeleteNo).
  115. Where("(name = ? OR sn_code = ?)", name, snCode)
  116. if len(exclude) > 0 {
  117. stmt.Where("id NOT IN (?)", exclude)
  118. }
  119. err := stmt.Count(&ret).Error
  120. return ret, errors.WithStackOnce(err)
  121. }