package train import ( "strconv" "strings" "zhiyuan/pkg/app" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "zhiyuan/services/dept" "zhiyuan/services/train/check" "zhiyuan/services/train/checks" "zhiyuan/services/train/course" "github.com/gin-gonic/gin" ) func CheckList(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_check`.`id`>0 ", "_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 state := utils.ToInt(c.Query("state")) if state != 0 { where["where"] = where["where"] + " AND `zy_check`.`state`={{state}}" param["state"] = state - 1 } user := c.Query("user") if user != "" { where["where"] = where["where"] + " AND (`zy_admin`.`username` LIKE {{user}} OR `zy_admin`.`phone` LIKE {{user}})" param["user"] = "%" + user + "%" } total, err := db.CountRaw("`zy_check` left join `zy_admin` on `zy_admin`.`id` = `zy_check`.`admin_id` WHERE "+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"` Username string `json:"username"` Phone string `json:"phone"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } field := "SELECT `zy_check`.*, `zy_admin`.`username`, `zy_admin`.`phone` FROM `zy_check` left join `zy_admin` on `zy_admin`.`id` = `zy_check`.`admin_id` " checkList := make([]CheckList, 0) if err = db.GetMultiRaw(field, 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") v.UpdatedAt = utils.DateS(v.UpdatedAt, "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 CheckAdminList(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_admin`.`id`>0 AND `zy_admin`.`state` = 1 AND `zy_admin`.`deleted_at`=0", "_group_by": "`zy_admin`.`id`", "_order_by": "`zy_admin`.`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`.`state` = 1" user := c.Query("user") if user != "" { where["where"] = where["where"] + " AND (`zy_admin`.`username` LIKE {{user}} OR `zy_admin`.`phone` LIKE {{user}})" param["user"] = "%" + user + "%" } deptID := utils.ToInt(c.Query("dept_id")) if deptID > 0 { where["where"] = where["where"] + " AND `zy_admin`.`dept_id` IN {{dept_id}}" param["dept_id"] = dept.GetSubDeptIds(deptID, []int{deptID}) } roleSlice := strings.Split(courseInfo.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_admin`.`role_ids`)" param["role_id_"+strconv.Itoa(i)] = v } where["where"] = where["where"] + ")" } res, err := db.GetOneMapRaw("SELECT count(distinct `zy_admin`.`id`) as count FROM `zy_admin` WHERE "+where["where"], param) if err != nil { app.Error(c, err.Error()) return } total := utils.ToInt64(res["count"]) res, err = db.GetOneMapRaw("SELECT count(distinct `zy_admin`.`id`) as count FROM `zy_admin` left join `zy_check` on `zy_check`.`admin_id` = `zy_admin`.`id` AND `zy_check`.`state` = 1 AND `zy_check`.`course_id` = {{course_id}} WHERE "+where["where"]+" AND `zy_check`.`id` is not null", param) if err != nil { app.Error(c, err.Error()) return } checks := utils.ToInt64(res["count"]) res, err = db.GetOneMapRaw("SELECT count(distinct `zy_admin`.`id`) as count FROM `zy_admin` left join `zy_check` on `zy_check`.`admin_id` = `zy_admin`.`id` AND `zy_check`.`state` = 1 AND `zy_check`.`course_id` = {{course_id}} AND `zy_check`.`right` * 100 / `zy_check`.`progress` >= 60 WHERE "+where["where"]+" AND `zy_check`.`id` is not null", param) if err != nil { app.Error(c, err.Error()) return } pass := utils.ToInt64(res["count"]) type CheckList struct { Id int `json:"id"` Username string `json:"username"` Phone string `json:"phone"` Checks int `json:"checks"` Max float64 `json:"max"` Min float64 `json:"min"` Avg float64 `json:"avg"` } field := "SELECT `zy_admin`.`id`, `zy_admin`.`username`, `zy_admin`.`phone`, COUNT(`zy_check`.`id`) as `checks`, max(`zy_check`.`right` * 100 / `zy_check`.`progress`) as `max`, min(`zy_check`.`right` * 100 / `zy_check`.`progress`) as `min`, avg(`zy_check`.`right` * 100 / `zy_check`.`progress`) as `avg` FROM `zy_admin` left join `zy_check` on `zy_check`.`admin_id` = `zy_admin`.`id` AND `zy_check`.`state` = 1 AND `zy_check`.`course_id` = {{course_id}} " checkList := make([]CheckList, 0) if err = db.GetMultiRaw(field, where, param, &checkList); err != nil { app.Error(c, err.Error()) return } data := gin.H{ "courseInfo": courseInfo, "list": checkList, "total": total, "checks": checks, "pass": pass, "limit": page.PageSize, } app.Success(c, data) }