check.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package train
  2. import (
  3. "strconv"
  4. "strings"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/services/dept"
  9. "zhiyuan/services/train/check"
  10. "zhiyuan/services/train/checks"
  11. "zhiyuan/services/train/course"
  12. "github.com/gin-gonic/gin"
  13. )
  14. func CheckList(c *gin.Context) {
  15. course_id := utils.ToInt(c.Query("course_id"))
  16. if course_id <= 0 {
  17. app.Error(c, "课程id有误")
  18. return
  19. }
  20. courseInfo, err := course.GetOne(map[string]interface{}{"id": course_id}, []string{"`id`", "`name`", "`intro`", "`role_ids`", "`show`"}, nil)
  21. if err != nil {
  22. app.Error(c, err.Error())
  23. return
  24. }
  25. page := app.HandlePageNums(c)
  26. where := map[string]string{
  27. "where": "`zy_check`.`id`>0 ",
  28. "_group_by": "`zy_check`.`id`",
  29. "_order_by": "`zy_check`.`id` desc",
  30. }
  31. if page.PageSize != 0 {
  32. where["_page_size"] = utils.ToStr(page.PageSize)
  33. where["_page_num"] = utils.ToStr(page.PageNum)
  34. }
  35. param := make(map[string]interface{})
  36. where["where"] = where["where"] + " AND `zy_check`.`course_id` = {{course_id}}"
  37. param["course_id"] = course_id
  38. state := utils.ToInt(c.Query("state"))
  39. if state != 0 {
  40. where["where"] = where["where"] + " AND `zy_check`.`state`={{state}}"
  41. param["state"] = state - 1
  42. }
  43. user := c.Query("user")
  44. if user != "" {
  45. where["where"] = where["where"] + " AND (`zy_admin`.`username` LIKE {{user}} OR `zy_admin`.`phone` LIKE {{user}})"
  46. param["user"] = "%" + user + "%"
  47. }
  48. total, err := db.CountRaw("`zy_check` left join `zy_admin` on `zy_admin`.`id` = `zy_check`.`admin_id` WHERE "+where["where"], param)
  49. if err != nil {
  50. app.Error(c, err.Error())
  51. return
  52. }
  53. type CheckList struct {
  54. ID int `json:"id"`
  55. CourseId int `json:"course_id"`
  56. AdminId int `json:"admin_id"`
  57. Total int `json:"total"`
  58. Progress int `json:"progress"`
  59. Right int `json:"right"`
  60. State int `json:"state"`
  61. Username string `json:"username"`
  62. Phone string `json:"phone"`
  63. CreatedAt string `json:"created_at"`
  64. UpdatedAt string `json:"updated_at"`
  65. }
  66. 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` "
  67. checkList := make([]CheckList, 0)
  68. if err = db.GetMultiRaw(field, where, param, &checkList); err != nil {
  69. app.Error(c, err.Error())
  70. return
  71. }
  72. for k, v := range checkList {
  73. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  74. v.UpdatedAt = utils.DateS(v.UpdatedAt, "YYYY-MM-DD HH:mm")
  75. checkList[k] = v
  76. }
  77. data := gin.H{
  78. "courseInfo": courseInfo,
  79. "list": checkList,
  80. "total": total,
  81. "limit": page.PageSize,
  82. }
  83. app.Success(c, data)
  84. }
  85. func CheckInfo(c *gin.Context) {
  86. check_id := utils.ToInt(c.Param("id"))
  87. if check_id <= 0 {
  88. app.Error(c, "考核id有误")
  89. return
  90. }
  91. checkInfo, err := check.GetOne(map[string]interface{}{"id": check_id}, nil, nil)
  92. if err != nil {
  93. app.Error(c, err.Error())
  94. return
  95. }
  96. where := map[string]string{
  97. "where": "`zy_checks`.`id`>0",
  98. "_group_by": "`zy_checks`.`id`",
  99. "_order_by": "`zy_checks`.`id` asc",
  100. }
  101. param := make(map[string]interface{})
  102. where["where"] = where["where"] + " AND `zy_checks`.`check_id` = {{check_id}}"
  103. param["check_id"] = checkInfo.ID
  104. type ChecksList struct {
  105. ID int `json:"id"`
  106. CheckId int `json:"check_id"`
  107. Type int `json:"type"`
  108. QuestionId int `json:"question_id"`
  109. Content string `json:"content"`
  110. Options string `json:"options"`
  111. Answer string `json:"answer"`
  112. Right string `json:"right"`
  113. CreatedAt string `json:"created_at"`
  114. UpdatedAt string `json:"updated_at"`
  115. }
  116. checksList := make([]ChecksList, 0)
  117. if err = checks.GetCheckssRaw(where, param, &checksList); err != nil {
  118. app.Error(c, err.Error())
  119. return
  120. }
  121. for k, v := range checksList {
  122. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  123. v.UpdatedAt = utils.DateS(v.UpdatedAt, "YYYY-MM-DD HH:mm")
  124. checksList[k] = v
  125. }
  126. data := gin.H{
  127. "info": checkInfo,
  128. "checks": checksList,
  129. }
  130. app.Success(c, data)
  131. }
  132. func CheckAdminList(c *gin.Context) {
  133. course_id := utils.ToInt(c.Query("course_id"))
  134. if course_id <= 0 {
  135. app.Error(c, "课程id有误")
  136. return
  137. }
  138. courseInfo, err := course.GetOne(map[string]interface{}{"id": course_id}, []string{"`id`", "`name`", "`intro`", "`role_ids`", "`show`"}, nil)
  139. if err != nil {
  140. app.Error(c, err.Error())
  141. return
  142. }
  143. page := app.HandlePageNums(c)
  144. where := map[string]string{
  145. "where": "`zy_admin`.`id`>0 AND `zy_admin`.`state` = 1 AND `zy_admin`.`deleted_at`=0",
  146. "_group_by": "`zy_admin`.`id`",
  147. "_order_by": "`zy_admin`.`id` desc",
  148. }
  149. if page.PageSize != 0 {
  150. where["_page_size"] = utils.ToStr(page.PageSize)
  151. where["_page_num"] = utils.ToStr(page.PageNum)
  152. }
  153. param := make(map[string]interface{})
  154. //where["where"] = where["where"] + " AND `zy_check`.`course_id` = {{course_id}}"
  155. param["course_id"] = course_id
  156. //where["where"] = where["where"] + " AND `zy_check`.`state` = 1"
  157. user := c.Query("user")
  158. if user != "" {
  159. where["where"] = where["where"] + " AND (`zy_admin`.`username` LIKE {{user}} OR `zy_admin`.`phone` LIKE {{user}})"
  160. param["user"] = "%" + user + "%"
  161. }
  162. deptID := utils.ToInt(c.Query("dept_id"))
  163. if deptID > 0 {
  164. where["where"] = where["where"] + " AND `zy_admin`.`dept_id` IN {{dept_id}}"
  165. param["dept_id"] = dept.GetSubDeptIds(deptID, []int{deptID})
  166. }
  167. roleSlice := strings.Split(courseInfo.RoleIds, ",")
  168. if len(roleSlice) > 0 {
  169. where["where"] = where["where"] + " AND ("
  170. for i, v := range roleSlice {
  171. if i != 0 {
  172. where["where"] = where["where"] + " OR "
  173. }
  174. where["where"] = where["where"] + "FIND_IN_SET({{role_id_" + strconv.Itoa(i) + "}}, `zy_admin`.`role_ids`)"
  175. param["role_id_"+strconv.Itoa(i)] = v
  176. }
  177. where["where"] = where["where"] + ")"
  178. }
  179. res, err := db.GetOneMapRaw("SELECT count(distinct `zy_admin`.`id`) as count FROM `zy_admin` WHERE "+where["where"], param)
  180. if err != nil {
  181. app.Error(c, err.Error())
  182. return
  183. }
  184. total := utils.ToInt64(res["count"])
  185. 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)
  186. if err != nil {
  187. app.Error(c, err.Error())
  188. return
  189. }
  190. checks := utils.ToInt64(res["count"])
  191. 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)
  192. if err != nil {
  193. app.Error(c, err.Error())
  194. return
  195. }
  196. pass := utils.ToInt64(res["count"])
  197. type CheckList struct {
  198. Id int `json:"id"`
  199. Username string `json:"username"`
  200. Phone string `json:"phone"`
  201. Checks int `json:"checks"`
  202. Max float64 `json:"max"`
  203. Min float64 `json:"min"`
  204. Avg float64 `json:"avg"`
  205. }
  206. 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}} "
  207. checkList := make([]CheckList, 0)
  208. if err = db.GetMultiRaw(field, where, param, &checkList); err != nil {
  209. app.Error(c, err.Error())
  210. return
  211. }
  212. data := gin.H{
  213. "courseInfo": courseInfo,
  214. "list": checkList,
  215. "total": total,
  216. "checks": checks,
  217. "pass": pass,
  218. "limit": page.PageSize,
  219. }
  220. app.Success(c, data)
  221. }