order_service.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package order
  2. import (
  3. "errors"
  4. logger "eta/eta_mini_ht_api/common/component/log"
  5. "eta/eta_mini_ht_api/common/exception"
  6. "eta/eta_mini_ht_api/common/utils/page"
  7. productService "eta/eta_mini_ht_api/domian/merchant"
  8. orderService "eta/eta_mini_ht_api/domian/order"
  9. userService "eta/eta_mini_ht_api/domian/user"
  10. "eta/eta_mini_ht_api/service/user"
  11. "fmt"
  12. "gorm.io/gorm"
  13. "sync"
  14. "time"
  15. )
  16. const (
  17. permanentlyValid = "永久有效"
  18. )
  19. type ProductOrderInfo struct {
  20. Buyer BuyerInfo `json:"buyer"`
  21. ProductInfo ProductInfo `json:"productInfo"`
  22. OrderNo string `json:"orderNo"`
  23. }
  24. type ProductInfo struct {
  25. Name string
  26. Type string
  27. Price string
  28. ValidDuration string
  29. }
  30. type BuyerInfo struct {
  31. Name string
  32. Mobile string
  33. AreaCode string
  34. }
  35. func GenerateProductOrderNo() string {
  36. return orderService.GenerateProductOrderNo()
  37. }
  38. func CreateProductOrder(templateUser user.User, productId int, orderNo string) (orderInfo ProductOrderInfo, err error) {
  39. if productId <= 0 {
  40. err = exception.New(exception.IllegalProductId)
  41. }
  42. productInfo, err := productService.GetMerchantProductById(productId)
  43. if err != nil {
  44. err = exception.NewWithException(exception.ProductInfoError, err.Error())
  45. return
  46. }
  47. officialUser, err := userService.GetUserByTemplateUserId(templateUser.Id)
  48. if err != nil {
  49. if errors.Is(err, gorm.ErrRecordNotFound) {
  50. err = exception.New(exception.OfficialUserNotFound)
  51. } else {
  52. err = exception.NewWithException(exception.OfficialUserFoundError, err.Error())
  53. }
  54. return
  55. }
  56. orderNo, err = orderService.CreateProductOrder(orderService.ProductOrderDTO{
  57. OrderID: orderNo,
  58. UserID: templateUser.Id,
  59. ProductID: productId,
  60. TotalAmount: productInfo.Price.String(),
  61. })
  62. if err != nil {
  63. logger.Error("创建订单失败:%v", err)
  64. if errors.Is(err, gorm.ErrDuplicatedKey) {
  65. err = exception.NewWithException(exception.SubscribeFailed, "请勿重复下单")
  66. return
  67. }
  68. err = exception.NewWithException(exception.SubscribeFailed, err.Error())
  69. return
  70. }
  71. buyer := BuyerInfo{
  72. Name: officialUser.RealName,
  73. Mobile: templateUser.Mobile,
  74. AreaCode: templateUser.AreaCode,
  75. }
  76. var duration string
  77. if !productInfo.IsPermanent {
  78. beginDate := time.Now().Format(time.DateOnly)
  79. endDate := time.Now().Add(time.Duration(productInfo.ValidDays*24) * time.Hour).Format(time.DateOnly)
  80. duration = fmt.Sprintf("%s~%s", beginDate, endDate)
  81. } else {
  82. duration = permanentlyValid
  83. }
  84. product := ProductInfo{
  85. Name: productInfo.Title,
  86. Type: productInfo.Type,
  87. Price: productInfo.Price.String(),
  88. ValidDuration: duration,
  89. }
  90. orderInfo = ProductOrderInfo{
  91. Buyer: buyer,
  92. ProductInfo: product,
  93. OrderNo: orderNo,
  94. }
  95. return
  96. }
  97. func GetTotalPageCountByUserId(userId int, orderStatus string) (total int64, latestId int64) {
  98. return orderService.GetTotalPageCountByUserId(userId, orderStatus)
  99. }
  100. func GetOrderPage(pageInfo page.PageInfo, userId int, orderStatus string) (orderList []orderService.ProductOrderDTO, err error) {
  101. orderList, err = orderService.GetOrderPage(pageInfo, userId, orderStatus)
  102. if err != nil {
  103. err = exception.NewWithException(exception.GetOrderListFailed, err.Error())
  104. }
  105. var wg sync.WaitGroup
  106. wg.Add(len(orderList))
  107. logger.Info("获取产品信息")
  108. for i := 0; i < len(orderList); i++ {
  109. go func(order *orderService.ProductOrderDTO) {
  110. defer wg.Done()
  111. product, pdErr := productService.GetMerchantProductById(order.ProductID)
  112. if pdErr != nil {
  113. logger.Error("获取产品信息失败:%v", err)
  114. }
  115. order.ProductName = product.Title
  116. }(&orderList[i])
  117. }
  118. wg.Wait()
  119. return
  120. }
  121. func GetOrderDetail(productId int, userId int) (order orderService.ProductOrderDetailDTO, err error) {
  122. order, err = orderService.GetOrderDetail(productId, userId)
  123. productInfo, err := productService.GetMerchantProductById(order.ProductID)
  124. if err != nil {
  125. return
  126. }
  127. order.ProductName = productInfo.Title
  128. order.ProductPrice = productInfo.Price.String()
  129. return
  130. }