query_service.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package ordersrv
  2. import (
  3. "Cold_Logistic/internal/server/infra/dao"
  4. "Cold_Logistic/internal/server/infra/models"
  5. "context"
  6. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/core"
  7. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  8. )
  9. // QueryExpressOrder 订单号查订单
  10. func (srv *OrderService) QueryExpressOrder(ctx context.Context, req QueryExpressOrderReqVO) (res QueryExpressOrderRespVO, err error) {
  11. order, err := srv.store.ExpressOrder().FindByOrderNo(ctx, req.OrderNo)
  12. if err != nil {
  13. return res, errors.WithStackOnce(err)
  14. }
  15. if order.Id == 0 {
  16. return res, nil
  17. }
  18. logisticName, err := srv.store.LogisticCompany().FindNameById(ctx, order.LogisticId)
  19. if err != nil {
  20. return res, errors.WithStackOnce(err)
  21. }
  22. log := models.OrderLogisticLog{}
  23. err = srv.store.OrderLogisticLog().FirstById(ctx, &log, order.LatestLogId)
  24. if err != nil {
  25. return res, errors.WithStackOnce(err)
  26. }
  27. res.OrderId = order.Id
  28. res.OrderNo = order.OrderNo
  29. res.OrderStatus = order.OrderStatus
  30. res.LogisticName = logisticName
  31. res.LatestLocation = LocationInfoVO{
  32. LocationTime: log.CreatedTime,
  33. Location: log.Location,
  34. ContactPerson: log.ContactPerson,
  35. ContactPhone: log.ContactPhone,
  36. Extend: log.Extend,
  37. }
  38. return res, nil
  39. }
  40. // OrderDetail 订单详情
  41. func (srv *OrderService) OrderDetail(ctx context.Context, req OrderDetailReqVO) (res OrderDetailRespVO, err error) {
  42. order := models.ExpressOrder{}
  43. if err = srv.store.ExpressOrder().FirstById(ctx, &order, req.OrderId); err != nil {
  44. return res, errors.WithStackOnce(err)
  45. }
  46. logisticName, err := srv.store.LogisticCompany().FindNameById(ctx, order.LogisticId)
  47. if err != nil {
  48. return res, errors.WithStackOnce(err)
  49. }
  50. addressIds := []int{order.SenderAddressId, order.ConsigneeAddressId}
  51. addressMap, err := srv.store.AddressBook().GetMapByIds(ctx, addressIds)
  52. if err != nil {
  53. return res, errors.WithStackOnce(err)
  54. }
  55. res = OrderDetailRespVO{
  56. OrderId: order.Id,
  57. OrderNo: order.OrderNo,
  58. LogisticName: logisticName,
  59. OrderStatus: order.OrderStatus,
  60. Sender: ConvertAddressReqVO(addressMap[order.SenderAddressId]),
  61. Consignee: ConvertAddressReqVO(addressMap[order.ConsigneeAddressId]),
  62. CargoType: order.CargoType,
  63. TemperatureInterval: order.TemperatureInterval,
  64. CargoNumber: order.CargoNumber,
  65. DeliveryCondition: order.DeliveryCondition,
  66. DeliveryRemark: order.DeliveryRemark,
  67. TimeLimit: order.TimeLimit,
  68. PickupTime: order.PickupTime,
  69. IsWeekendPickup: order.IsWeekendPickup,
  70. IsInsuredValue: order.IsInsuredValue,
  71. }
  72. return res, nil
  73. }
  74. // OrderLogisticDetail 订单物流详情
  75. func (srv *OrderService) OrderLogisticDetail(ctx context.Context, req OrderLogisticDetailReqVO) (res OrderLogisticDetailRespVO, err error) {
  76. order := models.ExpressOrder{}
  77. if err = srv.store.ExpressOrder().FirstById(ctx, &order, req.OrderId); err != nil {
  78. return res, errors.WithStackOnce(err)
  79. }
  80. logLink, err := srv.getLogisticLink(ctx, order)
  81. if err != nil {
  82. return res, errors.WithStackOnce(err)
  83. }
  84. addressIds := []int{order.SenderAddressId, order.ConsigneeAddressId}
  85. addressMap, err := srv.store.AddressBook().GetMapByIds(ctx, addressIds)
  86. if err != nil {
  87. return res, errors.WithStackOnce(err)
  88. }
  89. res.Sender = ConvertAddressReqVO(addressMap[order.SenderAddressId])
  90. res.Consignee = ConvertAddressReqVO(addressMap[order.ConsigneeAddressId])
  91. res.LogisticLink = logLink
  92. return res, nil
  93. }
  94. func (srv *OrderService) getLogisticLink(ctx context.Context, order models.ExpressOrder) ([]LocationInfoVO, error) {
  95. logs, err := srv.store.OrderLogisticLog().FindAllByOrderId(ctx, order.Id)
  96. if err != nil {
  97. return nil, errors.WithStackOnce(err)
  98. }
  99. ret := make([]LocationInfoVO, 0, len(logs))
  100. for _, v := range logs {
  101. ret = append(ret, LocationInfoVO{
  102. LocationTime: v.CreatedTime,
  103. Location: v.Location,
  104. ContactPerson: v.ContactPerson,
  105. ContactPhone: v.ContactPhone,
  106. Extend: v.Extend,
  107. SnCode: v.SnCode,
  108. })
  109. }
  110. return ret, nil
  111. }
  112. // ManageOrderPage 管理后台订单列表
  113. func (srv *OrderService) ManageOrderPage(ctx context.Context, req ManageOrderPageReqVO) (res core.PageListResponse, err error) {
  114. dto := dao.ExpressOrderPageDTO{
  115. Page: req.Page,
  116. OrderNo: req.Search.OrderNo,
  117. OrderStatus: []int{req.Search.OrderStatus},
  118. }
  119. list, total, err := srv.store.ExpressOrder().ManagePage(ctx, dto)
  120. if err != nil {
  121. return res, errors.WithStackOnce(err)
  122. }
  123. res.Total = total
  124. res.TotalPage = total / int64(req.Page.Size)
  125. res.Count = len(list)
  126. res.List = list
  127. return res, nil
  128. }