package train import ( "zhiyuan/pkg/app" "zhiyuan/pkg/utils" "zhiyuan/services/form" "zhiyuan/services/train/course" "zhiyuan/services/train/courses" "github.com/gin-gonic/gin" ) func CoursesList(c *gin.Context) { course_id := utils.ToInt(c.Query("course_id")) if course_id <= 0 { app.Error(c, "课程id有误") return } courseInfo, err := course.GetOne(map[string]interface{}{"id": course_id}, []string{"`id`", "`name`", "`intro`", "`role_ids`", "`show`"}, nil) if err != nil { app.Error(c, err.Error()) return } page := app.HandlePageNums(c) where := map[string]string{ "where": "`zy_courses`.`id`>0 AND `zy_courses`.`deleted_at`=0", "_group_by": "`zy_courses`.`id`", "_order_by": "`zy_courses`.`orders` asc", } if page.PageSize != 0 { where["_page_size"] = utils.ToStr(page.PageSize) where["_page_num"] = utils.ToStr(page.PageNum) } param := make(map[string]interface{}) where["where"] = where["where"] + " AND `zy_courses`.`course_id` = {{course_id}}" param["course_id"] = course_id name := c.Query("name") if name != "" { where["where"] = where["where"] + " AND `zy_courses`.`name` LIKE {{name}}" param["name"] = "%" + name + "%" } type_ := utils.ToInt(c.Query("type")) if type_ != 0 { where["where"] = where["where"] + " AND `zy_courses`.`type` = {{type}}" param["type"] = type_ } total, err := courses.CountRaw(where["where"], param) if err != nil { app.Error(c, err.Error()) return } type CoursesList struct { ID int `json:"id"` CourseId int `json:"course_id"` Name string `json:"name"` Type int `json:"type"` Content string `json:"content"` Orders int `json:"orders"` CreatedAt string `json:"created_at"` UpdatedAt int `json:"updated_at"` } coursesList := make([]CoursesList, 0) if err = courses.GetCoursessRaw(where, param, &coursesList); err != nil { app.Error(c, err.Error()) return } for k, v := range coursesList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") coursesList[k] = v } data := gin.H{ "courseInfo": courseInfo, "list": coursesList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func CoursesAdd(c *gin.Context) { var addForm form.CoursesAdd if app.Bind(c, &addForm) != nil { return } id, err := courses.Add(addForm) if err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"id": id}) } func CoursesEdit(c *gin.Context) { id := utils.ToInt(c.Param("id")) if id <= 0 { app.ErrorMsg(c, "courses id must be a number", nil) return } var addForm form.CoursesAdd if app.Bind(c, &addForm) != nil { return } err := courses.EditByID(addForm, id) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, nil) } func CoursesDel(c *gin.Context) { id := utils.ToInt(c.Param("id")) if id <= 0 { app.ErrorMsg(c, "courses id must be a number", nil) return } err := courses.DeleteByID(id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) }