智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.8 KiB

  1. package hdl
  2. import (
  3. "applet/app/admin/lib/validate"
  4. "applet/app/admin/md"
  5. "applet/app/db"
  6. "applet/app/db/model"
  7. "applet/app/e"
  8. "applet/app/utils"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. func BannerList(c *gin.Context) {
  13. bannerDb := db.BannerDb{}
  14. bannerDb.Set()
  15. banners, err := bannerDb.FindBanner(0, 0)
  16. if err != nil {
  17. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  18. return
  19. }
  20. e.OutSuc(c, map[string]interface{}{
  21. "list": banners,
  22. }, nil)
  23. return
  24. }
  25. func BannerSort(c *gin.Context) {
  26. var args struct {
  27. Ids []string `json:"ids" binding:"required"`
  28. }
  29. if err := c.ShouldBindJSON(&args); err != nil {
  30. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  31. return
  32. }
  33. bannerDb := db.BannerDb{}
  34. bannerDb.Set()
  35. banners, err := bannerDb.FindBannerById(args.Ids)
  36. if err != nil {
  37. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  38. return
  39. }
  40. for k, v := range *banners {
  41. v.Sort = k
  42. _, err1 := bannerDb.BannerUpdate(&v, "sort")
  43. if err1 != nil {
  44. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  45. return
  46. }
  47. }
  48. e.OutSuc(c, "success", nil)
  49. }
  50. func BannerAdd(c *gin.Context) {
  51. var req md.BannerAddReq
  52. err := c.ShouldBindJSON(&req)
  53. if err != nil {
  54. err = validate.HandleValidateErr(err)
  55. err1 := err.(e.E)
  56. e.OutErr(c, err1.Code, err1.Error())
  57. return
  58. }
  59. bannerDb := db.BannerDb{}
  60. bannerDb.Set()
  61. now := time.Now()
  62. banner := model.Banner{
  63. Name: req.Name,
  64. ImgUrl: req.ImgUrl,
  65. CreateAt: now.Format("2006-01-02 15:04:05"),
  66. UpdateAt: now.Format("2006-01-02 15:04:05"),
  67. }
  68. _, err = bannerDb.BannerInsert(&banner)
  69. if err != nil {
  70. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  71. return
  72. }
  73. e.OutSuc(c, "success", nil)
  74. return
  75. }
  76. func BannerUpdate(c *gin.Context) {
  77. var req md.BannerUpdateReq
  78. err := c.ShouldBindJSON(&req)
  79. if err != nil {
  80. err = validate.HandleValidateErr(err)
  81. err1 := err.(e.E)
  82. e.OutErr(c, err1.Code, err1.Error())
  83. return
  84. }
  85. bannerDb := db.BannerDb{}
  86. bannerDb.Set()
  87. banner, err := bannerDb.GetBanner(req.Id)
  88. if err != nil {
  89. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  90. return
  91. }
  92. if banner == nil {
  93. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  94. return
  95. }
  96. now := time.Now()
  97. banner.Name = req.Name
  98. banner.ImgUrl = req.ImgUrl
  99. banner.UpdateAt = now.Format("2006-01-02 15:04:05")
  100. _, err = bannerDb.BannerUpdate(banner, "name", "content", "update_at")
  101. if err != nil {
  102. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  103. return
  104. }
  105. e.OutSuc(c, "success", nil)
  106. return
  107. }
  108. func BannerDelete(c *gin.Context) {
  109. id := c.Param("id")
  110. bannerDb := db.BannerDb{}
  111. bannerDb.Set()
  112. company, err := bannerDb.GetBanner(utils.StrToInt(id))
  113. if err != nil {
  114. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  115. return
  116. }
  117. if company == nil {
  118. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  119. return
  120. }
  121. _, err = bannerDb.BannerDelete(id)
  122. if err != nil {
  123. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  124. return
  125. }
  126. e.OutSuc(c, "success", nil)
  127. return
  128. }