BackGoodsRouter.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package router
  2. import (
  3. "demo/data/dao/manage"
  4. "demo/data/domain"
  5. "demo/data/domain/vo"
  6. "demo/share"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "github.com/mitchellh/mapstructure"
  10. "github.com/spf13/cast"
  11. "time"
  12. )
  13. // BackGoodsRouter 废弃
  14. func BackGoodsRouter(engine *gin.RouterGroup) {
  15. user := engine.Group("/back/goods")
  16. PushRouter(user, "GET", "/introduction", GetIntroductionByGoodsId)
  17. PushRouter(user, "POST", "list", GetGoodsAndIntroductionList)
  18. PushRouter(user, "POST", "", SaveGoods)
  19. PushRouter(user, "PUT", "", UpdateGoods)
  20. PushRouter(user, "Delete", "/:id", DeleteGoodsById)
  21. }
  22. func GetIntroductionByGoodsId(c *gin.Context) {
  23. id := c.Query("id")
  24. byId, err := manage.GetGoodsById(cast.ToInt64(id))
  25. if err != nil {
  26. c.JSON(200, CreateResultError(500, "商品不存在"))
  27. return
  28. }
  29. introductionById, err := manage.GetGoodsIntroductionById(byId.IntroductionId)
  30. if err != nil {
  31. c.JSON(200, CreateResultError(500, "商品介绍不存在"))
  32. return
  33. }
  34. c.JSON(200, CreateResultData(introductionById))
  35. }
  36. // GetGoodsAndIntroductionList 根据商品id获取商品及商品相关文章
  37. func GetGoodsAndIntroductionList(c *gin.Context) {
  38. data := share.GetJsonAnyParam(c)
  39. param, _ := data("param")
  40. pageNum, _ := data("pageNum")
  41. pageSize, _ := data("pageSize")
  42. goods := domain.Goods{}
  43. err := mapstructure.Decode(param, &goods)
  44. if err != nil {
  45. c.JSON(200, CreateResultError(500, "json转换错误"))
  46. return
  47. }
  48. list, err := manage.GetGoodsList(goods, cast.ToInt(pageNum), cast.ToInt(pageSize))
  49. if err != nil {
  50. c.JSON(200, CreateResultError(500, "查询错误"))
  51. return
  52. }
  53. intrIds := make([]int64, 0)
  54. for i := range list.List {
  55. ///类型判断
  56. item, ok := list.List[i].(domain.Goods)
  57. if ok {
  58. intrIds = append(intrIds, item.IntroductionId)
  59. }
  60. }
  61. intrs, err := manage.GetGoodsIntroductionListInId(intrIds)
  62. if err != nil {
  63. c.JSON(200, CreateResultError(500, "查询错误"))
  64. }
  65. dataMap := make(map[int64]vo.GoodsVo)
  66. for i := 0; i < len(list.List); i++ {
  67. dataMap[list.List[i].(domain.Goods).IntroductionId] = vo.GoodsVo{Goods: list.List[i].(domain.Goods)}
  68. }
  69. for i := 0; i < len(intrs); i++ {
  70. voData, ok := dataMap[intrs[i].Id]
  71. if ok {
  72. voData.Introduction = intrs[i]
  73. }
  74. }
  75. //取出values
  76. resArr := make([]any, 0)
  77. for _, v := range dataMap {
  78. resArr = append(resArr, v)
  79. }
  80. list.List = resArr
  81. c.JSON(200, CreateResultData(list))
  82. }
  83. func SaveGoods(c *gin.Context) {
  84. data := share.GetJsonAnyParam(c)
  85. goodsParam, _ := data("goods")
  86. //typeId, _ := data("typeId")
  87. //tagIds, _ := data("tagIds")
  88. goods := domain.Goods{}
  89. err := mapstructure.Decode(goodsParam, &goods)
  90. saveGoods, err := manage.SaveGoods(&goods)
  91. if err != nil {
  92. c.JSON(200, CreateResultError(500, "商品保存错误"))
  93. return
  94. }
  95. goodsIntroductionParam, _ := data("introduction")
  96. goodsIntroduction := domain.GoodsIntroduction{}
  97. err = mapstructure.Decode(goodsIntroductionParam, &goodsIntroduction)
  98. goodsIntroduction.CreateBy = fmt.Sprint(GetUserIdByToken(c))
  99. goodsIntroduction.CreateTime = time.Now()
  100. saveGoodsIntroduction, err := manage.SaveGoodsIntroduction(&goodsIntroduction)
  101. if err != nil {
  102. c.JSON(200, CreateResultError(500, "商品介绍保存错误"))
  103. return
  104. }
  105. m := make(map[string]interface{})
  106. m["goods"] = saveGoods
  107. m["introduction"] = saveGoodsIntroduction
  108. c.JSON(200, CreateResultData(m))
  109. }
  110. func UpdateGoods(c *gin.Context) {
  111. data := share.GetJsonAnyParam(c)
  112. goodsIntroduction, _ := data("introduction")
  113. goods, _ := data("goods")
  114. id, _ := data("id")
  115. toMap := UtilStructToMap(cast.ToStringMap(goods))
  116. setGoods, b, err := manage.SetGoods(toMap, cast.ToInt64(id))
  117. if err != nil || !b {
  118. c.JSON(200, CreateResultError(500, "修改错误"))
  119. return
  120. }
  121. intrId := setGoods.IntroductionId
  122. toIntrMap := UtilStructToMap(cast.ToStringMap(goodsIntroduction))
  123. toIntrMap["create_by"] = "fmt.Sprint(GetUserIdByToken(c))"
  124. setGoodsIntroduction, b, err := manage.SetGoodsIntroduction(toIntrMap, cast.ToInt64(intrId))
  125. if err != nil || !b {
  126. c.JSON(200, CreateResultError(500, "修改错误"))
  127. return
  128. }
  129. m := make(map[string]interface{})
  130. m["goods"] = setGoods
  131. m["introduction"] = setGoodsIntroduction
  132. c.JSON(200, CreateResultData(m))
  133. }
  134. func DeleteGoodsById(c *gin.Context) {
  135. id := c.Query("id")
  136. byId, err := manage.GetGoodsById(cast.ToInt64(id))
  137. if err != nil {
  138. c.JSON(200, CreateResultError(500, "删除失败"))
  139. return
  140. }
  141. if byId.Id == 0 || byId.Id > 0 {
  142. c.JSON(200, CreateResultError(500, "商品不存在删除失败"))
  143. return
  144. }
  145. res := manage.DeleteGoods(cast.ToInt64(id))
  146. if !res {
  147. c.JSON(200, CreateResultError(500, "删除错误"))
  148. return
  149. }
  150. res = manage.DeleteGoodsIntroduction(cast.ToInt64(byId.IntroductionId))
  151. if !res {
  152. c.JSON(200, CreateResultError(500, "文章删除错误"))
  153. return
  154. }
  155. c.JSON(200, CreateResult())
  156. }