package train import ( "zhiyuan/pkg/app" "zhiyuan/pkg/utils" "zhiyuan/services/form" "zhiyuan/services/train/course" "github.com/gin-gonic/gin" ) func CourseList(c *gin.Context) { page := app.HandlePageNums(c) where := map[string]string{ "where": "`zy_course`.`id`>0 AND `zy_course`.`deleted_at`=0", "_group_by": "`zy_course`.`id`", "_order_by": "`zy_course`.`id` desc", } if page.PageSize != 0 { where["_page_size"] = utils.ToStr(page.PageSize) where["_page_num"] = utils.ToStr(page.PageNum) } param := make(map[string]interface{}) name := c.Query("name") if name != "" { where["where"] = where["where"] + " AND `zy_course`.`name` LIKE {{name}}" param["name"] = "%" + name + "%" } type_ := c.Query("type") if type_ != "" { where["where"] = where["where"] + " AND `zy_course`.`type` = {{type_}}" param["type"] = type_ } roleId := utils.ToInt(c.Query("role_id")) if roleId != 0 { where["where"] = where["where"] + " AND FIND_IN_SET({{role_id}}, `zy_course`.`role_ids`)" param["role_id"] = roleId } total, err := course.CountRaw(where["where"], param) if err != nil { app.Error(c, err.Error()) return } type CourseList struct { ID int `json:"id"` Name string `json:"name"` Intro string `json:"intro"` Type string `json:"type"` RoleIds string `json:"role_ids"` RoleNames string `json:"role_names"` Checks int `json:"checks"` Show int `json:"show"` CreatedAt string `json:"created_at"` UpdatedAt int `json:"updated_at"` } courseList := make([]CourseList, 0) if err = course.GetCoursesRaw(where, param, &courseList); err != nil { app.Error(c, err.Error()) return } for k, v := range courseList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") courseList[k] = v } data := gin.H{ "list": courseList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func CourseAdd(c *gin.Context) { var addForm form.CourseAdd if app.Bind(c, &addForm) != nil { return } id, err := course.Add(addForm) if err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"id": id}) } func CourseEdit(c *gin.Context) { id := utils.ToInt(c.Param("id")) if id <= 0 { app.ErrorMsg(c, "course id must be a number", nil) return } var addForm form.CourseAdd if app.Bind(c, &addForm) != nil { return } err := course.EditByID(addForm, id) if err != nil { app.ErrorMsg(c, err.Error(), nil) return } app.Success(c, nil) } func CourseDel(c *gin.Context) { id := utils.ToInt(c.Param("id")) if id <= 0 { app.ErrorMsg(c, "course id must be a number", nil) return } err := course.DeleteByID(id) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) }