course.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package train
  2. import (
  3. "zhiyuan/pkg/app"
  4. "zhiyuan/pkg/utils"
  5. "zhiyuan/services/form"
  6. "zhiyuan/services/train/course"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func CourseList(c *gin.Context) {
  10. page := app.HandlePageNums(c)
  11. where := map[string]string{
  12. "where": "`zy_course`.`id`>0 AND `zy_course`.`deleted_at`=0",
  13. "_group_by": "`zy_course`.`id`",
  14. "_order_by": "`zy_course`.`id` desc",
  15. }
  16. if page.PageSize != 0 {
  17. where["_page_size"] = utils.ToStr(page.PageSize)
  18. where["_page_num"] = utils.ToStr(page.PageNum)
  19. }
  20. param := make(map[string]interface{})
  21. name := c.Query("name")
  22. if name != "" {
  23. where["where"] = where["where"] + " AND `zy_course`.`name` LIKE {{name}}"
  24. param["name"] = "%" + name + "%"
  25. }
  26. type_ := c.Query("type")
  27. if type_ != "" {
  28. where["where"] = where["where"] + " AND `zy_course`.`type` = {{type_}}"
  29. param["type"] = type_
  30. }
  31. roleId := utils.ToInt(c.Query("role_id"))
  32. if roleId != 0 {
  33. where["where"] = where["where"] + " AND FIND_IN_SET({{role_id}}, `zy_course`.`role_ids`)"
  34. param["role_id"] = roleId
  35. }
  36. total, err := course.CountRaw(where["where"], param)
  37. if err != nil {
  38. app.Error(c, err.Error())
  39. return
  40. }
  41. type CourseList struct {
  42. ID int `json:"id"`
  43. Name string `json:"name"`
  44. Intro string `json:"intro"`
  45. Type string `json:"type"`
  46. RoleIds string `json:"role_ids"`
  47. RoleNames string `json:"role_names"`
  48. Checks int `json:"checks"`
  49. Show int `json:"show"`
  50. CreatedAt string `json:"created_at"`
  51. UpdatedAt int `json:"updated_at"`
  52. }
  53. courseList := make([]CourseList, 0)
  54. if err = course.GetCoursesRaw(where, param, &courseList); err != nil {
  55. app.Error(c, err.Error())
  56. return
  57. }
  58. for k, v := range courseList {
  59. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  60. courseList[k] = v
  61. }
  62. data := gin.H{
  63. "list": courseList,
  64. "total": total,
  65. "limit": page.PageSize,
  66. }
  67. app.Success(c, data)
  68. }
  69. func CourseAdd(c *gin.Context) {
  70. var addForm form.CourseAdd
  71. if app.Bind(c, &addForm) != nil {
  72. return
  73. }
  74. id, err := course.Add(addForm)
  75. if err != nil {
  76. app.Error(c, err.Error())
  77. return
  78. }
  79. app.Success(c, gin.H{"id": id})
  80. }
  81. func CourseEdit(c *gin.Context) {
  82. id := utils.ToInt(c.Param("id"))
  83. if id <= 0 {
  84. app.ErrorMsg(c, "course id must be a number", nil)
  85. return
  86. }
  87. var addForm form.CourseAdd
  88. if app.Bind(c, &addForm) != nil {
  89. return
  90. }
  91. err := course.EditByID(addForm, id)
  92. if err != nil {
  93. app.ErrorMsg(c, err.Error(), nil)
  94. return
  95. }
  96. app.Success(c, nil)
  97. }
  98. func CourseDel(c *gin.Context) {
  99. id := utils.ToInt(c.Param("id"))
  100. if id <= 0 {
  101. app.ErrorMsg(c, "course id must be a number", nil)
  102. return
  103. }
  104. err := course.DeleteByID(id)
  105. if err != nil {
  106. app.Error(c, err.Error())
  107. return
  108. }
  109. app.Success(c, nil)
  110. }