qa.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package aftersale
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "zhiyuan/pkg/app"
  5. "zhiyuan/pkg/utils"
  6. "zhiyuan/services/aftersale"
  7. "zhiyuan/services/form"
  8. )
  9. func QaList(c *gin.Context) {
  10. page := app.HandlePageNum(c)
  11. where := map[string]interface{}{"deleted_at": 0}
  12. title := c.Query("title")
  13. if title != "" {
  14. where["title like"] = "%" + title + "%"
  15. }
  16. total, err := aftersale.CountQa(where)
  17. if err != nil {
  18. app.Error(c, err.Error())
  19. return
  20. }
  21. type Qa struct {
  22. ID int `json:"id"`
  23. Title string `json:"title"`
  24. Content string `json:"content"`
  25. DeletedAt int `json:"deleted_at"`
  26. CreatedAt string `json:"created_at"`
  27. UpdatedAt string `json:"updated_at"`
  28. }
  29. qaList := make([]*Qa, 0)
  30. if _, err := aftersale.GetQaList(where, nil, page, &qaList); err != nil {
  31. app.Error(c, err.Error())
  32. return
  33. }
  34. for k, v := range qaList {
  35. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  36. v.UpdatedAt = utils.DateS(v.UpdatedAt, "YYYY-MM-DD")
  37. qaList[k] = v
  38. }
  39. data := gin.H{
  40. "list": qaList,
  41. "total": total,
  42. "limit": page.PageSize,
  43. }
  44. app.Success(c, data)
  45. }
  46. func QaAdd(c *gin.Context) {
  47. var form form.ASQaAdd
  48. if app.Bind(c, &form) != nil {
  49. return
  50. }
  51. if id, err := aftersale.AddQa(form); err != nil {
  52. app.Error(c, err.Error())
  53. return
  54. } else {
  55. app.Success(c, gin.H{"id": id})
  56. }
  57. }
  58. func QaEdit(c *gin.Context) {
  59. id := utils.StrTo(c.Param("id")).MustInt()
  60. if id <= 0 {
  61. app.Error(c, "类型id有误")
  62. return
  63. }
  64. var form form.ASQaAdd
  65. if app.Bind(c, &form) != nil {
  66. return
  67. }
  68. if _, err := aftersale.EditQa(form, id); err != nil {
  69. app.ErrorMsg(c, err.Error(), nil)
  70. return
  71. }
  72. app.Success(c, nil)
  73. }
  74. func QaDel(c *gin.Context) {
  75. id := utils.StrTo(c.Param("id")).MustInt()
  76. if id <= 0 {
  77. app.Error(c, "类型id有误")
  78. return
  79. }
  80. if _, err := aftersale.DelQa(id); err != nil {
  81. app.ErrorMsg(c, err.Error(), nil)
  82. return
  83. }
  84. app.Success(c, nil)
  85. }