product_controller.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package product
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/exception"
  5. "eta/eta_mini_ht_api/common/utils/page"
  6. "eta/eta_mini_ht_api/controllers"
  7. productService "eta/eta_mini_ht_api/service/product"
  8. "eta/eta_mini_ht_api/service/user"
  9. "sync/atomic"
  10. )
  11. var (
  12. productMap = map[string]bool{
  13. "audio": true,
  14. "video": true,
  15. "media": true,
  16. "report": true,
  17. "package": true,
  18. }
  19. )
  20. type ProductController struct {
  21. controllers.ListController
  22. }
  23. // GetProductInfo 获取商品信息
  24. // @Summary 获取商品信息
  25. // @Description 获取商品信息
  26. // @Success 200 {object} controllers.BaseResponse
  27. // @router /getProductInfo [get]
  28. func (p *ProductController) GetProductInfo(productId int) {
  29. controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
  30. result = p.InitWrapData("获取商品详情失败")
  31. if productId <= 0 {
  32. err = exception.New(exception.IllegalProductId)
  33. p.FailedResult("获取商品详情失败", result)
  34. return
  35. }
  36. productInfo, err := productService.GetProductInfoById(productId)
  37. if err != nil {
  38. p.FailedResult("获取商品详情失败", result)
  39. return
  40. }
  41. p.SuccessResult("获取商品详情成功", productInfo, result)
  42. return
  43. })
  44. }
  45. // RelatePackage 关联套餐搜索
  46. // @Summary 关联套餐搜索
  47. // @Description 关联套餐搜索
  48. // @Success 200 {object} controllers.BaseResponse
  49. // @router /relatePackage [get]
  50. func (p *ProductController) RelatePackage(productId int) {
  51. controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
  52. result = p.InitWrapData("获取关联套餐信息失败")
  53. if productId <= 0 {
  54. err = exception.New(exception.IllegalProductId)
  55. p.FailedResult("获取关联套餐信息失败", result)
  56. return
  57. }
  58. productInfo, err := productService.GetProductInfoById(productId)
  59. if err != nil {
  60. p.FailedResult("获取关联套餐信息失败", result)
  61. err = exception.New(exception.ProductTypeError)
  62. return
  63. }
  64. if productInfo.Type == "package" {
  65. p.FailedResult("该商品为套餐,无法获取想关联套餐商品", result)
  66. err = exception.New(exception.ProductTypeError)
  67. }
  68. switch productInfo.Type {
  69. case "audio", "video":
  70. productInfo.MediaId = productInfo.SourceId
  71. case "report":
  72. productInfo.ReportId = productInfo.SourceId
  73. }
  74. productList, err := productService.GetRelatePackage(productInfo)
  75. productList = append(productList, productInfo)
  76. if err != nil {
  77. p.FailedResult("获取关联套餐信息失败", result)
  78. return
  79. }
  80. p.SuccessResult("获取关联套餐信息成功", productList, result)
  81. return
  82. })
  83. }
  84. // ProductSearch 搜索产品
  85. // @Summary 搜索产品
  86. // @Description 搜索产品
  87. // @Success 200 {object} controllers.BaseResponse
  88. // @router /productSearch [get]
  89. func (p *ProductController) ProductSearch(key string, isSignal bool) {
  90. controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
  91. result = p.InitWrapData("搜索产品信息失败")
  92. userInfo := p.Data["user"].(user.User)
  93. var productList []productService.ProductDTO
  94. var permissionWeightMap map[int]*atomic.Int32
  95. pageRes := page.Page{
  96. Current: p.PageInfo.Current,
  97. PageSize: p.PageInfo.PageSize,
  98. }
  99. productIdMap, err := productService.RangeProductList()
  100. if err != nil {
  101. logger.Error("搜索产品信息失败")
  102. err = exception.NewWithException(exception.GetProductListFailed, err.Error())
  103. return
  104. }
  105. productPage := new(page.PageResult)
  106. if isSignal {
  107. if len(productIdMap["report"]) == 0 && len(productIdMap["audio"]) == 0 && len(productIdMap["video"]) == 0 {
  108. logger.Info("没有相关的产品类型产品")
  109. productPage.Page = pageRes
  110. p.SuccessResult("搜索产品信息成功", productPage, result)
  111. return
  112. }
  113. } else {
  114. if len(productIdMap["package"]) == 0 {
  115. logger.Info("没有相关的产品类型产品")
  116. productPage.Page = pageRes
  117. p.SuccessResult("搜索产品信息成功", productList, result)
  118. return
  119. }
  120. }
  121. //搜索出相关的报告和音视频
  122. productSearchList, searchErr := productService.SearchRelateProduct(key, productIdMap)
  123. if searchErr != nil {
  124. logger.Error("搜索报告产品失败")
  125. return
  126. }
  127. if len(productSearchList) == 0 {
  128. logger.Info("没有相关的产品类型产品")
  129. productPage.Page = pageRes
  130. p.SuccessResult("搜索产品信息成功", productList, result)
  131. return
  132. }
  133. if isSignal {
  134. pageRes.Total = int64(len(productSearchList))
  135. pageRes.LatestId = productService.LatestId()
  136. } else {
  137. pageRes.Total, pageRes.LatestId, permissionWeightMap = productService.CountSearchPackageList(productSearchList)
  138. }
  139. if p.PageInfo.LatestId == 0 {
  140. p.PageInfo.LatestId = pageRes.LatestId
  141. p.PageInfo.Total = pageRes.Total
  142. } else {
  143. pageRes.LatestId = p.PageInfo.LatestId
  144. pageRes.Total = p.PageInfo.Total
  145. }
  146. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  147. productList, err = productService.ProductListBySort(isSignal, productSearchList, permissionWeightMap, userInfo.Id, p.PageInfo)
  148. productPage.Data = productList
  149. productPage.Page = pageRes
  150. p.SuccessResult("搜索产品信息成功", productPage, result)
  151. return
  152. })
  153. }
  154. // ProductList 产品列表
  155. // @Summary 产品列表
  156. // @Description 产品列表
  157. // @Success 200 {object} controllers.BaseResponse
  158. // @router /ProductList [get]
  159. func (p *ProductController) ProductList(productType string, permissionIds string) {
  160. controllers.Wrap(&p.BaseController, func() (result *controllers.WrapData, err error) {
  161. result = p.InitWrapData("分页查询产品列表失败")
  162. userInfo := p.Data["user"].(user.User)
  163. if !productMap[productType] {
  164. err = exception.New(exception.ProductTypeError)
  165. p.FailedResult("分页查询产品列表失败", result)
  166. return
  167. }
  168. permissionIdList, err := p.TransPermissionIds(permissionIds)
  169. if err != nil {
  170. logger.Error("品种列表解析错误:%v", err)
  171. p.FailedResult("分页查询产品列表失败", result)
  172. err = exception.New(exception.GetProductListFailed)
  173. return
  174. }
  175. pageRes := page.Page{
  176. Current: p.PageInfo.Current,
  177. PageSize: p.PageInfo.PageSize,
  178. }
  179. var productIds []int
  180. pageRes.Total, pageRes.LatestId, productIds = productService.GetProductListByProductType(productType, permissionIdList, userInfo.Id)
  181. if p.PageInfo.LatestId == 0 {
  182. p.PageInfo.LatestId = pageRes.LatestId
  183. p.PageInfo.Total = pageRes.Total
  184. } else {
  185. pageRes.LatestId = p.PageInfo.LatestId
  186. pageRes.Total = p.PageInfo.Total
  187. }
  188. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  189. list, err := productService.ProductList(productIds, userInfo.Id, p.PageInfo)
  190. productList := new(page.PageResult)
  191. productList.Data = list
  192. productList.Page = pageRes
  193. p.SuccessResult("分页查询产品列表成功", productList, result)
  194. return
  195. })
  196. }