智慧食堂
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

136 lignes
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 NoticeList(c *gin.Context) {
  13. noticeDb := db.NoticeDb{}
  14. noticeDb.Set()
  15. notices, err := noticeDb.FindNotice(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": notices,
  22. }, nil)
  23. return
  24. }
  25. func NoticeSort(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. noticeDb := db.NoticeDb{}
  34. noticeDb.Set()
  35. notices, err := noticeDb.FindNoticeById(args.Ids)
  36. if err != nil {
  37. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  38. return
  39. }
  40. for k, v := range *notices {
  41. v.Sort = k
  42. _, err1 := noticeDb.NoticeUpdate(&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 NoticeAdd(c *gin.Context) {
  51. var req md.NoticeAddReq
  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. noticeDb := db.NoticeDb{}
  60. noticeDb.Set()
  61. now := time.Now()
  62. notice := model.Notice{
  63. Name: req.Name,
  64. Content: req.Content,
  65. CreateAt: now.Format("2006-01-02 15:04:05"),
  66. UpdateAt: now.Format("2006-01-02 15:04:05"),
  67. }
  68. _, err = noticeDb.NoticeInsert(&notice)
  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 NoticeUpdate(c *gin.Context) {
  77. var req md.NoticeUpdateReq
  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. noticeDb := db.NoticeDb{}
  86. noticeDb.Set()
  87. notice, err := noticeDb.GetNotice(req.Id)
  88. if err != nil {
  89. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  90. return
  91. }
  92. if notice == nil {
  93. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  94. return
  95. }
  96. now := time.Now()
  97. notice.Name = req.Name
  98. notice.Content = req.Content
  99. notice.UpdateAt = now.Format("2006-01-02 15:04:05")
  100. _, err = noticeDb.NoticeUpdate(notice, "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 NoticeDelete(c *gin.Context) {
  109. id := c.Param("id")
  110. noticeDb := db.NoticeDb{}
  111. noticeDb.Set()
  112. company, err := noticeDb.GetNotice(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 = noticeDb.NoticeDelete(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. }