package mobile import ( "net/http" "strconv" "strings" "zhiyuan/pkg/app" "zhiyuan/pkg/db" "zhiyuan/pkg/errcode" "zhiyuan/pkg/utils" "zhiyuan/services/admin" "zhiyuan/services/form" "zhiyuan/services/train/check" "zhiyuan/services/train/checks" "zhiyuan/services/train/course" "zhiyuan/services/train/courses" "zhiyuan/services/train/learn" "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 AND `zy_course`.`show`=1", "_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{}) admin, _ := admin.GetInfoByID(c.GetInt("adminID"), nil, nil) if admin == nil { app.Response(c, http.StatusUnauthorized, errcode.TokenInvalid, nil) return } roleSlice := strings.Split(admin.RoleIds, ",") if len(roleSlice) > 0 { where["where"] = where["where"] + " AND (" for i, v := range roleSlice { if i != 0 { where["where"] = where["where"] + " OR " } where["where"] = where["where"] + "FIND_IN_SET({{role_id_" + strconv.Itoa(i) + "}}, `zy_course`.`role_ids`)" param["role_id_"+strconv.Itoa(i)] = v } where["where"] = where["where"] + ")" } 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_ } state := utils.ToInt(c.Query("state")) if state != 0 { where["where"] = where["where"] + " AND `state`={{state}}" param["state"] = state } 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"` RoleIds string `json:"role_ids"` Courses int `json:"courses"` Learns int `json:"learns"` State int `json:"state"` CreatedAt string `json:"created_at"` UpdatedAt int `json:"updated_at"` } field := "SELECT `zy_course`.*, COUNT(`zy_courses`.`id`) as `courses`, COUNT(FIND_IN_SET(`zy_courses`.`id`, `zy_learn`.`courses_ids`) OR NULL) as `learns`, case COUNT(FIND_IN_SET(`zy_courses`.`id`, `zy_learn`.`courses_ids`) OR NULL) when 0 then 1 when COUNT(`zy_courses`.`id`) then 3 else 2 end as `state` FROM `zy_course` left join `zy_courses` on `zy_courses`.`course_id` = `zy_course`.`id` left join `zy_learn` on (`zy_learn`.`course_id` = `zy_course`.`id` AND `zy_learn`.`admin_id` = " + strconv.Itoa(c.GetInt("adminID")) + ") " courseList := make([]CourseList, 0) if err = db.GetMultiRaw(field, 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 CoursesList(c *gin.Context) { course_id := utils.ToInt(c.Query("course_id")) if course_id <= 0 { app.Error(c, "课程id有误") return } err := check.CencelCheck(course_id, c.GetInt("adminID")) if err != nil { app.Error(c, err.Error()) 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 } learns, err := learn.Count(map[string]interface{}{"course_id": course_id}) if err != nil { app.Error(c, err.Error()) return } learn, err := learn.GetOne(map[string]interface{}{"course_id": course_id, "admin_id": c.GetInt("adminID")}, nil, 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"` State int `json:"state"` CreatedAt string `json:"created_at"` UpdatedAt int `json:"updated_at"` } field := "SELECT `zy_courses`.*, case when FIND_IN_SET(`zy_courses`.`id`, `zy_learn`.`courses_ids`) then 2 else 1 end as `state` FROM `zy_courses` left join `zy_learn` on (`zy_learn`.`course_id` = `zy_courses`.`course_id` AND `zy_learn`.`admin_id` = " + strconv.Itoa(c.GetInt("adminID")) + ") " coursesList := make([]CoursesList, 0) if err = db.GetMultiRaw(field, 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{ "learn": learn, "learns": learns, "courseInfo": courseInfo, "list": coursesList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func CoursesInfo(c *gin.Context) { courses_id := utils.ToInt(c.Param("id")) if courses_id <= 0 { app.Error(c, "章节id有误") return } courses, err := courses.GetOne(map[string]interface{}{"id": courses_id}, nil, nil) if err != nil { app.Error(c, err.Error()) return } err = check.CencelCheck(courses.CourseId, c.GetInt("adminID")) if err != nil { app.Error(c, err.Error()) return } err = learn.LearnCourses(courses_id, c.GetInt("adminID")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, courses) } func CheckCourse(c *gin.Context) { course_id := utils.ToInt(c.Param("id")) if course_id <= 0 { app.Error(c, "课程id有误") return } id, err := check.CheckCourse(course_id, c.GetInt("adminID")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, gin.H{"id": id}) } func CheckList(c *gin.Context) { course_id := utils.ToInt(c.Query("course_id")) if course_id <= 0 { app.Error(c, "课程id有误") return } err := check.CencelCheck(course_id, c.GetInt("adminID")) if err != nil { app.Error(c, err.Error()) 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_check`.`id`>0 AND `zy_check`.`state` < 2", "_group_by": "`zy_check`.`id`", "_order_by": "`zy_check`.`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{}) where["where"] = where["where"] + " AND `zy_check`.`course_id` = {{course_id}}" param["course_id"] = course_id where["where"] = where["where"] + " AND `zy_check`.`admin_id` = {{admin_id}}" param["admin_id"] = c.GetInt("adminID") state := utils.ToInt(c.Query("state")) if state != 0 { where["where"] = where["where"] + " AND `zy_courses`.`state` = {{state}}" param["state"] = state } total, err := check.CountRaw(where["where"], param) if err != nil { app.Error(c, err.Error()) return } type CheckList struct { ID int `json:"id"` CourseId int `json:"course_id"` AdminId int `json:"admin_id"` Total int `json:"total"` Progress int `json:"progress"` Right int `json:"right"` State int `json:"state"` CreatedAt string `json:"created_at"` UpdatedAt int `json:"updated_at"` } checkList := make([]CheckList, 0) if err = check.GetChecksRaw(where, param, &checkList); err != nil { app.Error(c, err.Error()) return } for k, v := range checkList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") checkList[k] = v } data := gin.H{ "courseInfo": courseInfo, "list": checkList, "total": total, "limit": page.PageSize, } app.Success(c, data) } func CheckInfo(c *gin.Context) { check_id := utils.ToInt(c.Param("id")) if check_id <= 0 { app.Error(c, "考核id有误") return } checkInfo, err := check.GetOne(map[string]interface{}{"id": check_id}, nil, nil) if err != nil { app.Error(c, err.Error()) return } where := map[string]string{ "where": "`zy_checks`.`id`>0", "_group_by": "`zy_checks`.`id`", "_order_by": "`zy_checks`.`id` asc", } param := make(map[string]interface{}) where["where"] = where["where"] + " AND `zy_checks`.`check_id` = {{check_id}}" param["check_id"] = checkInfo.ID type ChecksList struct { ID int `json:"id"` CheckId int `json:"check_id"` Type int `json:"type"` QuestionId int `json:"question_id"` Content string `json:"content"` Options string `json:"options"` Answer string `json:"answer"` Right string `json:"right"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } checksList := make([]ChecksList, 0) if err = checks.GetCheckssRaw(where, param, &checksList); err != nil { app.Error(c, err.Error()) return } for k, v := range checksList { v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm") v.UpdatedAt = utils.DateS(v.UpdatedAt, "YYYY-MM-DD HH:mm") checksList[k] = v } data := gin.H{ "info": checkInfo, "checks": checksList, } app.Success(c, data) } func SelectChecks(c *gin.Context) { var form form.ChecksSelect if app.Bind(c, &form) != nil { return } err := checks.SelectChecks(form, c.GetInt("adminID")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) } func SubmitCheck(c *gin.Context) { check_id := utils.ToInt(c.Param("id")) if check_id <= 0 { app.Error(c, "考核id有误") return } err := check.SubmitCheck(check_id, c.GetInt("adminID")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, nil) }