123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package order
- import (
- "errors"
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/common/exception"
- "eta/eta_mini_ht_api/common/utils/page"
- productService "eta/eta_mini_ht_api/domian/merchant"
- orderService "eta/eta_mini_ht_api/domian/order"
- userService "eta/eta_mini_ht_api/domian/user"
- "eta/eta_mini_ht_api/service/user"
- "fmt"
- "gorm.io/gorm"
- "sync"
- "time"
- )
- const (
- permanentlyValid = "永久有效"
- )
- type ProductOrderInfo struct {
- Buyer BuyerInfo `json:"buyer"`
- ProductInfo ProductInfo `json:"productInfo"`
- OrderNo string `json:"orderNo"`
- }
- type ProductInfo struct {
- Name string
- Type string
- Price string
- ValidDuration string
- }
- type BuyerInfo struct {
- Name string
- Mobile string
- AreaCode string
- }
- func GenerateProductOrderNo() string {
- return orderService.GenerateProductOrderNo()
- }
- func CreateProductOrder(templateUser user.User, productId int, orderNo string) (orderInfo ProductOrderInfo, err error) {
- if productId <= 0 {
- err = exception.New(exception.IllegalProductId)
- }
- productInfo, err := productService.GetMerchantProductById(productId)
- if err != nil {
- err = exception.NewWithException(exception.ProductInfoError, err.Error())
- return
- }
- officialUser, err := userService.GetUserByTemplateUserId(templateUser.Id)
- if err != nil {
- if errors.Is(err, gorm.ErrRecordNotFound) {
- err = exception.New(exception.OfficialUserNotFound)
- } else {
- err = exception.NewWithException(exception.OfficialUserFoundError, err.Error())
- }
- return
- }
- orderNo, err = orderService.CreateProductOrder(orderService.ProductOrderDTO{
- OrderID: orderNo,
- UserID: templateUser.Id,
- ProductID: productId,
- TotalAmount: productInfo.Price.String(),
- })
- if err != nil {
- logger.Error("创建订单失败:%v", err)
- if errors.Is(err, gorm.ErrDuplicatedKey) {
- err = exception.NewWithException(exception.SubscribeFailed, "请勿重复下单")
- return
- }
- err = exception.NewWithException(exception.SubscribeFailed, err.Error())
- return
- }
- buyer := BuyerInfo{
- Name: officialUser.RealName,
- Mobile: templateUser.Mobile,
- AreaCode: templateUser.AreaCode,
- }
- var duration string
- if !productInfo.IsPermanent {
- beginDate := time.Now().Format(time.DateOnly)
- endDate := time.Now().Add(time.Duration(productInfo.ValidDays*24) * time.Hour).Format(time.DateOnly)
- duration = fmt.Sprintf("%s~%s", beginDate, endDate)
- } else {
- duration = permanentlyValid
- }
- product := ProductInfo{
- Name: productInfo.Title,
- Type: productInfo.Type,
- Price: productInfo.Price.String(),
- ValidDuration: duration,
- }
- orderInfo = ProductOrderInfo{
- Buyer: buyer,
- ProductInfo: product,
- OrderNo: orderNo,
- }
- return
- }
- func GetTotalPageCountByUserId(userId int, orderStatus string) (total int64, latestId int64) {
- return orderService.GetTotalPageCountByUserId(userId, orderStatus)
- }
- func GetOrderPage(pageInfo page.PageInfo, userId int, orderStatus string) (orderList []orderService.ProductOrderDTO, err error) {
- orderList, err = orderService.GetOrderPage(pageInfo, userId, orderStatus)
- if err != nil {
- err = exception.NewWithException(exception.GetOrderListFailed, err.Error())
- }
- var wg sync.WaitGroup
- wg.Add(len(orderList))
- logger.Info("获取产品信息")
- for i := 0; i < len(orderList); i++ {
- go func(order *orderService.ProductOrderDTO) {
- defer wg.Done()
- product, pdErr := productService.GetMerchantProductById(order.ProductID)
- if pdErr != nil {
- logger.Error("获取产品信息失败:%v", err)
- }
- order.ProductName = product.Title
- }(&orderList[i])
- }
- wg.Wait()
- return
- }
- func GetOrderDetail(productId int, userId int) (order orderService.ProductOrderDetailDTO, err error) {
- order, err = orderService.GetOrderDetail(productId, userId)
- productInfo, err := productService.GetMerchantProductById(order.ProductID)
- if err != nil {
- return
- }
- order.ProductName = productInfo.Title
- order.ProductPrice = productInfo.Price.String()
- return
- }
|