house_service.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/infra/dao"
  7. "Cold_Logistic/internal/server/infra/models"
  8. "Cold_Logistic/internal/server/infra/thirdparty/internalservice/clod"
  9. "context"
  10. "fmt"
  11. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  12. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  13. "time"
  14. )
  15. // WarehouseInfo 仓库信息
  16. func (srv *WarehouseService) WarehouseInfo(ctx context.Context) (res WarehouseInfoRespVO, err error) {
  17. tokenInfo := global.GetTokenInfoFromContext(ctx)
  18. warehouse, err := srv.store.Warehouse().FindByManageUuid(ctx, tokenInfo.AccountUuid)
  19. if err != nil {
  20. return res, errors.WithStackOnce(err)
  21. }
  22. res.Name = warehouse.Name
  23. res.SnCode = warehouse.SnCode
  24. res.SnIds = make([]global.SnIdVO, 0, 4)
  25. snIds, err := clod.NewBzdClodService().DeviceSensorList(ctx, warehouse.SnCode)
  26. if err != nil {
  27. return res, errors.WithStackOnce(err)
  28. }
  29. for i := range snIds {
  30. if i >= 4 {
  31. break
  32. }
  33. res.SnIds = append(res.SnIds, global.SnIdVO{
  34. Id: snIds[i].TId,
  35. Name: snIds[i].TName,
  36. })
  37. }
  38. return res, nil
  39. }
  40. // WarehouseOrder 库中订单列表
  41. func (srv *WarehouseService) WarehouseOrder(ctx context.Context, req WarehouseOrderReqVO) (res core.PageListResponse, err error) {
  42. dto := dao.WarehouseOrderPageDTO{
  43. Page: req.Page,
  44. Status: constant.WarehouseStatusIn,
  45. OrderNo: req.Search.OrderNo,
  46. WarehouseId: global.GetTokenInfoFromContext(ctx).WarehouseId,
  47. TimeStart: req.Search.TimeStart,
  48. TimeEnd: req.Search.TimeEnd,
  49. }
  50. list, total, err := srv.store.WarehouseOrder().Page(ctx, dto)
  51. if err != nil {
  52. return res, nil
  53. }
  54. res.Total = total
  55. res.TotalPage = total / int64(req.Page.Size)
  56. res.Count = len(list)
  57. res.List = list
  58. return res, nil
  59. }
  60. // OrderIntoHouse 订单入库
  61. func (srv *WarehouseService) OrderIntoHouse(ctx context.Context, req OrderIntoHouseReqVO) (res OrderIntoHouseRespVO, err error) {
  62. tokenInfo := global.GetTokenInfoFromContext(ctx)
  63. if tokenInfo.WarehouseId >= 0 {
  64. return res, errors.WithCode(codex.ErrNoDataPermission, "不是仓管无法入库操作")
  65. }
  66. order := models.ExpressOrder{}
  67. if err = srv.store.ExpressOrder().FirstById(ctx, &order, req.OrderId); err != nil {
  68. return res, errors.WithStackOnce(err)
  69. }
  70. logisticName, err := srv.store.LogisticCompany().FindNameById(ctx, order.LogisticId)
  71. if err != nil {
  72. return res, errors.WithStackOnce(err)
  73. }
  74. house := models.Warehouse{}
  75. if err = srv.store.Warehouse().FirstById(ctx, &house, tokenInfo.WarehouseId); err != nil {
  76. return res, errors.WithStackOnce(err)
  77. }
  78. task := models.OrderTransportTask{}
  79. if err = srv.store.OrderTransportTask().FirstById(ctx, &task, order.LatestTaskId); err != nil {
  80. return res, errors.WithStackOnce(err)
  81. }
  82. houseOrder := models.WarehouseOrder{
  83. WarehouseId: house.Id,
  84. SnCode: house.SnCode,
  85. OrderId: req.OrderId,
  86. OrderNo: order.OrderNo,
  87. Status: constant.WarehouseStatusIn,
  88. StorageTime: models.NewMyTime(time.Now()),
  89. StorageBy: tokenInfo.AccountId,
  90. }
  91. log := models.OrderLogisticLog{
  92. LogType: constant.LogisticLogWarehouse,
  93. OrderId: order.Id,
  94. OrderNo: order.OrderNo,
  95. SnCode: houseOrder.SnCode,
  96. ContactPersonUuid: tokenInfo.AccountUuid,
  97. ContactPerson: tokenInfo.Name,
  98. ContactPhone: tokenInfo.Phone,
  99. Extend: fmt.Sprintf(constant.TemplateIntoHouse, logisticName+"-"+house.Address),
  100. }
  101. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  102. if err = srv.store.WarehouseOrder().Create(ctx, &houseOrder); err != nil {
  103. return errors.WithStackOnce(err)
  104. }
  105. order.LatestTaskId = 0
  106. if err = srv.store.OrderTransportTask().FinishedTask(ctx, task.Id); err != nil {
  107. return errors.WithStackOnce(err)
  108. }
  109. if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil {
  110. return errors.WithStackOnce(err)
  111. }
  112. order.LatestLogId = log.Id
  113. if err = srv.store.ExpressOrder().UpdateById(ctx, &order); err != nil {
  114. return errors.WithStackOnce(err)
  115. }
  116. return nil
  117. })
  118. if err != nil {
  119. return res, errors.WithStackOnce(err)
  120. }
  121. res.OrderId = order.Id
  122. res.OrderNo = order.OrderNo
  123. res.IntoTime = houseOrder.StorageTime
  124. return res, nil
  125. }
  126. // OrderOutHouse 订单出库
  127. func (srv *WarehouseService) OrderOutHouse(ctx context.Context, req OrderOutHouseReqVO) (res OrderOutHouseRespVO, err error) {
  128. tokenInfo := global.GetTokenInfoFromContext(ctx)
  129. if tokenInfo.WarehouseId >= 0 {
  130. return res, errors.WithCode(codex.ErrNoDataPermission, "不是仓管无法出库操作")
  131. }
  132. houseOrder, err := srv.store.WarehouseOrder().FindByHouseIdAndOrderId(ctx, req.OrderId, tokenInfo.WarehouseId)
  133. if err != nil {
  134. return res, errors.WithStackOnce(err)
  135. }
  136. if houseOrder.Id == 0 {
  137. return res, errors.WithCode(codex.ErrNoDataPermission, "仓库中没有这个订单")
  138. }
  139. log := models.OrderLogisticLog{
  140. LogType: constant.LogisticLogWarehouse,
  141. OrderId: houseOrder.OrderId,
  142. OrderNo: houseOrder.OrderNo,
  143. SnCode: houseOrder.SnCode,
  144. ContactPersonUuid: tokenInfo.AccountUuid,
  145. ContactPerson: tokenInfo.Name,
  146. ContactPhone: tokenInfo.Phone,
  147. Extend: constant.TemplateOutHouse,
  148. }
  149. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  150. if err = srv.store.OrderLogisticLog().Create(ctx, &log); err != nil {
  151. return errors.WithStackOnce(err)
  152. }
  153. if err = srv.store.WarehouseOrder().OrderOutHouse(ctx, houseOrder.OrderId, houseOrder.WarehouseId); err != nil {
  154. return errors.WithStackOnce(err)
  155. }
  156. return nil
  157. })
  158. if err != nil {
  159. return res, errors.WithStackOnce(err)
  160. }
  161. res.OutTime = models.NewMyTime(time.Now())
  162. res.OrderId = houseOrder.OrderId
  163. res.OrderNo = houseOrder.OrderNo
  164. return res, nil
  165. }