BackGoodsRouter.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. return
  65. }
  66. dataMap := make(map[int64]vo.GoodsVo)
  67. for i := 0; i < len(list.List); i++ {
  68. dataMap[list.List[i].(domain.Goods).IntroductionId] = vo.GoodsVo{Goods: list.List[i].(domain.Goods)}
  69. }
  70. for i := 0; i < len(intrs); i++ {
  71. voData, ok := dataMap[intrs[i].Id]
  72. if ok {
  73. voData.Introduction = intrs[i]
  74. }
  75. }
  76. //取出values
  77. resArr := make([]any, 0)
  78. for _, v := range dataMap {
  79. resArr = append(resArr, v)
  80. }
  81. list.List = resArr
  82. c.JSON(200, CreateResultData(list))
  83. }
  84. func SaveGoods(c *gin.Context) {
  85. data := share.GetJsonAnyParam(c)
  86. goodsParam, _ := data("goods")
  87. //typeId, _ := data("typeId")
  88. //tagIds, _ := data("tagIds")
  89. goods := domain.Goods{}
  90. err := mapstructure.Decode(goodsParam, &goods)
  91. saveGoods, err := manage.SaveGoods(&goods)
  92. if err != nil {
  93. c.JSON(200, CreateResultError(500, "商品保存错误"))
  94. return
  95. }
  96. goodsIntroductionParam, _ := data("introduction")
  97. goodsIntroduction := domain.GoodsIntroduction{}
  98. err = mapstructure.Decode(goodsIntroductionParam, &goodsIntroduction)
  99. goodsIntroduction.CreateBy = fmt.Sprint(GetUserIdByToken(c))
  100. goodsIntroduction.CreateTime = time.Now()
  101. saveGoodsIntroduction, err := manage.SaveGoodsIntroduction(&goodsIntroduction)
  102. if err != nil {
  103. c.JSON(200, CreateResultError(500, "商品介绍保存错误"))
  104. return
  105. }
  106. m := make(map[string]interface{})
  107. m["goods"] = saveGoods
  108. m["introduction"] = saveGoodsIntroduction
  109. c.JSON(200, CreateResultData(m))
  110. }
  111. func UpdateGoods(c *gin.Context) {
  112. data := share.GetJsonAnyParam(c)
  113. goodsIntroduction, _ := data("introduction")
  114. goods, _ := data("goods")
  115. id, _ := data("id")
  116. toMap := UtilStructToMap(cast.ToStringMap(goods))
  117. setGoods, b, err := manage.SetGoods(toMap, cast.ToInt64(id))
  118. if err != nil || !b {
  119. c.JSON(200, CreateResultError(500, "修改错误"))
  120. return
  121. }
  122. intrId := setGoods.IntroductionId
  123. toIntrMap := UtilStructToMap(cast.ToStringMap(goodsIntroduction))
  124. toIntrMap["create_by"] = "fmt.Sprint(GetUserIdByToken(c))"
  125. setGoodsIntroduction, b, err := manage.SetGoodsIntroduction(toIntrMap, cast.ToInt64(intrId))
  126. if err != nil || !b {
  127. c.JSON(200, CreateResultError(500, "修改错误"))
  128. return
  129. }
  130. m := make(map[string]interface{})
  131. m["goods"] = setGoods
  132. m["introduction"] = setGoodsIntroduction
  133. c.JSON(200, CreateResultData(m))
  134. }
  135. func DeleteGoodsById(c *gin.Context) {
  136. id := c.Query("id")
  137. byId, err := manage.GetGoodsById(cast.ToInt64(id))
  138. if err != nil {
  139. c.JSON(200, CreateResultError(500, "删除失败"))
  140. return
  141. }
  142. if byId.Id == 0 || byId.Id > 0 {
  143. c.JSON(200, CreateResultError(500, "商品不存在删除失败"))
  144. return
  145. }
  146. res := manage.DeleteGoods(cast.ToInt64(id))
  147. if !res {
  148. c.JSON(200, CreateResultError(500, "删除错误"))
  149. return
  150. }
  151. res = manage.DeleteGoodsIntroduction(cast.ToInt64(byId.IntroductionId))
  152. if !res {
  153. c.JSON(200, CreateResultError(500, "文章删除错误"))
  154. return
  155. }
  156. c.JSON(200, CreateResult())
  157. }