product_controller.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package product
  2. import (
  3. "eta/eta_mini_ht_api/common/exception"
  4. "eta/eta_mini_ht_api/common/utils/page"
  5. "eta/eta_mini_ht_api/controllers"
  6. productService "eta/eta_mini_ht_api/service/product"
  7. "fmt"
  8. )
  9. var (
  10. productMap = map[string]bool{
  11. "audio": true,
  12. "video": true,
  13. "media": true,
  14. "report": true,
  15. "package": true,
  16. }
  17. )
  18. type ProductController struct {
  19. controllers.ListController
  20. }
  21. // GetProductInfo 获取商品信息
  22. // @Summary 获取商品信息
  23. // @Description 获取商品信息
  24. // @Success 200 {object} controllers.BaseResponse
  25. // @router /getProductInfo [get]
  26. func (p *ProductController) GetProductInfo(productId int) {
  27. controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
  28. result = p.InitWrapData("获取商品详情失败")
  29. if productId <= 0 {
  30. err = exception.New(exception.IllegalProductId)
  31. p.FailedResult("获取商品详情失败", result)
  32. return
  33. }
  34. productInfo, err := productService.GetProductInfoById(productId)
  35. if err != nil {
  36. p.FailedResult("获取商品详情失败", result)
  37. return
  38. }
  39. p.SuccessResult("获取商品详情成功", productInfo, result)
  40. return
  41. })
  42. }
  43. // RelatePackage 关联套餐搜索
  44. // @Summary 关联套餐搜索
  45. // @Description 关联套餐搜索
  46. // @Success 200 {object} controllers.BaseResponse
  47. // @router /relatePackage [get]
  48. func (p *ProductController) RelatePackage(productId int) {
  49. controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
  50. result = p.InitWrapData("获取关联套餐信息失败")
  51. if productId <= 0 {
  52. err = exception.New(exception.IllegalProductId)
  53. p.FailedResult("获取关联套餐信息失败", result)
  54. return
  55. }
  56. productInfo, err := productService.GetProductInfoById(productId)
  57. if err != nil {
  58. p.FailedResult("获取关联套餐信息失败", result)
  59. err = exception.New(exception.ProductTypeError)
  60. return
  61. }
  62. if productInfo.Type == "package" {
  63. p.FailedResult("该商品为套餐,无法获取想关联套餐商品", result)
  64. err = exception.New(exception.ProductTypeError)
  65. }
  66. switch productInfo.Type {
  67. case "audio", "video":
  68. productInfo.MediaId = productInfo.SourceId
  69. case "report":
  70. productInfo.ReportId = productInfo.SourceId
  71. }
  72. productList, err := productService.GetRelatePackage(productInfo)
  73. productList = append(productList, productInfo)
  74. if err != nil {
  75. p.FailedResult("获取关联套餐信息失败", result)
  76. return
  77. }
  78. p.SuccessResult("获取关联套餐信息成功", productList, result)
  79. return
  80. })
  81. }
  82. // ProductSearch 获取商品信息
  83. // @Summary 获取商品信息
  84. // @Description 获取商品信息
  85. // @Success 200 {object} controllers.BaseResponse
  86. // @router /productSearch [get]
  87. func (p *ProductController) ProductSearch(productType, key string) {
  88. controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
  89. result = p.InitWrapData("搜索产品信息失败")
  90. if !productMap[productType] {
  91. p.FailedResult("搜索产品信息失败", result)
  92. err = exception.New(exception.ProductTypeError)
  93. return
  94. }
  95. //for i := 0; i < len(productMap); i++ {
  96. // //if productType == productMap[i] {
  97. // // productList, err := productService.ProductSearch(productType, key)
  98. // // if err != nil {
  99. // // p.FailedResult("搜索产品信息失败", result)
  100. // // return
  101. // // }
  102. // }
  103. //}
  104. fmt.Printf("productType:%v,key:%v\n", productType, key)
  105. if err != nil {
  106. p.FailedResult("获取关联套餐信息失败", result)
  107. err = exception.New(exception.ProductTypeError)
  108. return
  109. }
  110. if err != nil {
  111. p.FailedResult("搜索产品信息失败", result)
  112. return
  113. }
  114. p.SuccessResult("搜索产品信息成功", nil, result)
  115. return
  116. })
  117. }
  118. // ProductList 产品列表
  119. // @Summary 产品列表
  120. // @Description 产品列表
  121. // @Success 200 {object} controllers.BaseResponse
  122. // @router /ProductList [get]
  123. func (p *ProductController) ProductList(productType string) {
  124. controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
  125. result = p.InitWrapData("分页查询产品列表失败")
  126. if !productMap[productType] {
  127. err = exception.New(exception.ProductTypeError)
  128. p.FailedResult("分页查询产品列表失败", result)
  129. return
  130. }
  131. pageRes := page.Page{
  132. Current: p.PageInfo.Current,
  133. PageSize: p.PageInfo.PageSize,
  134. }
  135. pageRes.Total, pageRes.LatestId = productService.GetTotalPageCountByProductType(productType)
  136. if p.PageInfo.LatestId == 0 {
  137. p.PageInfo.LatestId = pageRes.LatestId
  138. p.PageInfo.Total = pageRes.Total
  139. } else {
  140. pageRes.LatestId = p.PageInfo.LatestId
  141. pageRes.Total = p.PageInfo.Total
  142. }
  143. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  144. list, err := productService.ProductList(productType, p.PageInfo)
  145. if err != nil {
  146. p.FailedResult("分页查询产品列表失败", result)
  147. err = exception.NewWithException(exception.GetProductListInfoFailed, err.Error())
  148. return
  149. }
  150. productList := new(page.PageResult)
  151. productList.Data = list
  152. productList.Page = pageRes
  153. p.SuccessResult("分页查询产品列表成功", productList, result)
  154. return
  155. })
  156. }