manage_service.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package warehousesrv
  2. import (
  3. "Cold_Logistic/internal/pkg/common/codex"
  4. "Cold_Logistic/internal/pkg/common/constant"
  5. "Cold_Logistic/internal/pkg/common/global"
  6. "Cold_Logistic/internal/server/domain/domainservice"
  7. "Cold_Logistic/internal/server/infra/dao"
  8. "Cold_Logistic/internal/server/infra/models"
  9. "context"
  10. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  11. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  12. )
  13. // WarehousePage 仓库列表
  14. func (srv *WarehouseService) WarehousePage(ctx context.Context, req WarehousePageReqVO) (res core.PageListResponse, err error) {
  15. dto := dao.WarehousePageDTO{
  16. Page: req.Page,
  17. Name: req.Search.Name,
  18. SnCode: req.Search.SnCode,
  19. }
  20. ents, total, err := srv.store.Warehouse().Page(ctx, dto)
  21. if err != nil {
  22. return res, errors.WithStackOnce(err)
  23. }
  24. warehouseIds := make([]int, 0, len(ents))
  25. accUuids := make([]string, 0, len(ents))
  26. for _, v := range ents {
  27. warehouseIds = append(warehouseIds, v.Id)
  28. if err = core.DatatypeJSONConvertTo(v.ManagerUuid, &accUuids); err != nil {
  29. return res, errors.WithStackOnce(err)
  30. }
  31. }
  32. orderCount, err := srv.store.WarehouseOrder().CountWarehouseOrder(ctx, warehouseIds, constant.WarehouseStatusIn)
  33. if err != nil {
  34. return res, errors.WithStackOnce(err)
  35. }
  36. accMap, err := srv.store.Account().FindByUuids(ctx, accUuids)
  37. if err != nil {
  38. return res, errors.WithStackOnce(err)
  39. }
  40. list := make([]WarehousePageVO, 0, len(ents))
  41. for _, v := range ents {
  42. manager := make([]string, 0, 2)
  43. if err = core.DatatypeJSONConvertTo(v.ManagerUuid, &manager); err != nil {
  44. return res, errors.WithStackOnce(err)
  45. }
  46. accList := make([]WarehouseManageVO, 0, len(manager))
  47. for i := range manager {
  48. accList = append(accList, WarehouseManageVO{
  49. AccountId: accMap[manager[i]].Id,
  50. AccountUuid: manager[i],
  51. AccountName: accMap[manager[i]].Name,
  52. })
  53. }
  54. list = append(list, WarehousePageVO{
  55. WarehouseId: v.Id,
  56. Name: v.Name,
  57. SnCode: v.SnCode,
  58. Address: v.Address,
  59. OrderCount: orderCount[v.Id],
  60. AccountList: accList,
  61. })
  62. }
  63. res.Total = total
  64. res.TotalPage = total / int64(req.Page.Size)
  65. res.Count = len(list)
  66. res.List = list
  67. return res, nil
  68. }
  69. // AddWarehouse 添加仓库
  70. func (srv *WarehouseService) AddWarehouse(ctx context.Context, req AddWarehouseReqVO) (res WarehouseIdRespVO, err error) {
  71. count, err := srv.store.Warehouse().CountByNameOrSnCode(ctx, req.Name, req.SnCode)
  72. if err != nil {
  73. return res, errors.WithStackOnce(err)
  74. }
  75. if count > 0 {
  76. return res, errors.WithCode(codex.ErrParamValidate, "名称重复或SN编码已使用")
  77. }
  78. uuids := make([]string, 0, len(req.Manage))
  79. for _, v := range req.Manage {
  80. uuids = append(uuids, v.Uuid)
  81. }
  82. n, _ := core.ConvertJSONDatatype(uuids)
  83. warehouse := models.Warehouse{
  84. Pid: global.GetTokenInfoFromContext(ctx).UsePid,
  85. Name: req.Name,
  86. SnCode: req.SnCode,
  87. ManagerUuid: n,
  88. Address: req.Address,
  89. Enable: req.Enable,
  90. }
  91. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  92. if err = srv.store.Warehouse().Create(ctx, &warehouse); err != nil {
  93. return errors.WithStackOnce(err)
  94. }
  95. if err = domainservice.ProcessWareHouse(ctx, warehouse.Id, req.Manage); err != nil {
  96. return errors.WithStackOnce(err)
  97. }
  98. return nil
  99. })
  100. if err != nil {
  101. return res, errors.WithStackOnce(err)
  102. }
  103. res.WarehouseId = warehouse.Id
  104. return res, nil
  105. }
  106. // UpdateWarehouse 修改仓库
  107. func (srv *WarehouseService) UpdateWarehouse(ctx context.Context, req UpdateWarehouseReqVO) (res WarehouseIdRespVO, err error) {
  108. warehouse := models.Warehouse{}
  109. if err = srv.store.Warehouse().FirstById(ctx, &warehouse, req.WarehouseId); err != nil {
  110. return res, errors.WithStackOnce(err)
  111. }
  112. if req.Name != warehouse.Name || req.SnCode == warehouse.SnCode {
  113. count, err := srv.store.Warehouse().CountByNameOrSnCode(ctx, req.Name, req.SnCode, warehouse.Id)
  114. if err != nil {
  115. return res, errors.WithStackOnce(err)
  116. }
  117. if count > 0 {
  118. return res, errors.WithCode(codex.ErrParamValidate, "名称重复或SN编码已使用")
  119. }
  120. }
  121. uuids := make([]string, 0, len(req.Manage))
  122. for _, v := range req.Manage {
  123. uuids = append(uuids, v.Uuid)
  124. }
  125. n, _ := core.ConvertJSONDatatype(uuids)
  126. warehouse.Name = req.Name
  127. warehouse.SnCode = req.SnCode
  128. warehouse.ManagerUuid = n
  129. warehouse.Address = req.Address
  130. warehouse.Enable = req.Enable
  131. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  132. if err = srv.store.Warehouse().UpdateById(ctx, &warehouse); err != nil {
  133. return errors.WithStackOnce(err)
  134. }
  135. if err = domainservice.ProcessWareHouse(ctx, warehouse.Id, req.Manage); err != nil {
  136. return errors.WithStackOnce(err)
  137. }
  138. return nil
  139. })
  140. if err != nil {
  141. return res, errors.WithStackOnce(err)
  142. }
  143. res.WarehouseId = warehouse.Id
  144. return res, nil
  145. }