courses.go 3.0 KB

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