BackGoodsRouter.go 4.7 KB

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