learn.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package mobile
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "zhiyuan/pkg/app"
  7. "zhiyuan/pkg/db"
  8. "zhiyuan/pkg/errcode"
  9. "zhiyuan/pkg/utils"
  10. "zhiyuan/services/admin"
  11. "zhiyuan/services/form"
  12. "zhiyuan/services/train/check"
  13. "zhiyuan/services/train/checks"
  14. "zhiyuan/services/train/course"
  15. "zhiyuan/services/train/courses"
  16. "zhiyuan/services/train/learn"
  17. "github.com/gin-gonic/gin"
  18. )
  19. func CourseList(c *gin.Context) {
  20. page := app.HandlePageNums(c)
  21. where := map[string]string{
  22. "where": "`zy_course`.`id`>0 AND `zy_course`.`deleted_at`=0 AND `zy_course`.`show`=1",
  23. "_group_by": "`zy_course`.`id`",
  24. "_order_by": "`zy_course`.`id` desc",
  25. }
  26. if page.PageSize != 0 {
  27. where["_page_size"] = utils.ToStr(page.PageSize)
  28. where["_page_num"] = utils.ToStr(page.PageNum)
  29. }
  30. param := make(map[string]interface{})
  31. admin, _ := admin.GetInfoByID(c.GetInt("adminID"), nil, nil)
  32. if admin == nil {
  33. app.Response(c, http.StatusUnauthorized, errcode.TokenInvalid, nil)
  34. return
  35. }
  36. roleSlice := strings.Split(admin.RoleIds, ",")
  37. if len(roleSlice) > 0 {
  38. where["where"] = where["where"] + " AND ("
  39. for i, v := range roleSlice {
  40. if i != 0 {
  41. where["where"] = where["where"] + " OR "
  42. }
  43. where["where"] = where["where"] + "FIND_IN_SET({{role_id_" + strconv.Itoa(i) + "}}, `zy_course`.`role_ids`)"
  44. param["role_id_"+strconv.Itoa(i)] = v
  45. }
  46. where["where"] = where["where"] + ")"
  47. }
  48. name := c.Query("name")
  49. if name != "" {
  50. where["where"] = where["where"] + " AND `zy_course`.`name` LIKE {{name}}"
  51. param["name"] = "%" + name + "%"
  52. }
  53. type_ := c.Query("type")
  54. if type_ != "" {
  55. where["where"] = where["where"] + " AND `zy_course`.`type` = {{type}}"
  56. param["type"] = type_
  57. }
  58. state := utils.ToInt(c.Query("state"))
  59. if state != 0 {
  60. where["where"] = where["where"] + " AND `state`={{state}}"
  61. param["state"] = state
  62. }
  63. total, err := course.CountRaw(where["where"], param)
  64. if err != nil {
  65. app.Error(c, err.Error())
  66. return
  67. }
  68. type CourseList struct {
  69. ID int `json:"id"`
  70. Name string `json:"name"`
  71. Intro string `json:"intro"`
  72. RoleIds string `json:"role_ids"`
  73. Courses int `json:"courses"`
  74. Learns int `json:"learns"`
  75. State int `json:"state"`
  76. CreatedAt string `json:"created_at"`
  77. UpdatedAt int `json:"updated_at"`
  78. }
  79. 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")) + ") "
  80. courseList := make([]CourseList, 0)
  81. if err = db.GetMultiRaw(field, where, param, &courseList); err != nil {
  82. app.Error(c, err.Error())
  83. return
  84. }
  85. for k, v := range courseList {
  86. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  87. courseList[k] = v
  88. }
  89. data := gin.H{
  90. "list": courseList,
  91. "total": total,
  92. "limit": page.PageSize,
  93. }
  94. app.Success(c, data)
  95. }
  96. func CoursesList(c *gin.Context) {
  97. course_id := utils.ToInt(c.Query("course_id"))
  98. if course_id <= 0 {
  99. app.Error(c, "课程id有误")
  100. return
  101. }
  102. err := check.CencelCheck(course_id, c.GetInt("adminID"))
  103. if err != nil {
  104. app.Error(c, err.Error())
  105. return
  106. }
  107. courseInfo, err := course.GetOne(map[string]interface{}{"id": course_id}, []string{"`id`", "`name`", "`intro`", "`role_ids`", "`show`"}, nil)
  108. if err != nil {
  109. app.Error(c, err.Error())
  110. return
  111. }
  112. learns, err := learn.Count(map[string]interface{}{"course_id": course_id})
  113. if err != nil {
  114. app.Error(c, err.Error())
  115. return
  116. }
  117. learn, err := learn.GetOne(map[string]interface{}{"course_id": course_id, "admin_id": c.GetInt("adminID")}, nil, nil)
  118. if err != nil {
  119. app.Error(c, err.Error())
  120. return
  121. }
  122. page := app.HandlePageNums(c)
  123. where := map[string]string{
  124. "where": "`zy_courses`.`id`>0 AND `zy_courses`.`deleted_at`=0",
  125. "_group_by": "`zy_courses`.`id`",
  126. "_order_by": "`zy_courses`.`orders` asc",
  127. }
  128. if page.PageSize != 0 {
  129. where["_page_size"] = utils.ToStr(page.PageSize)
  130. where["_page_num"] = utils.ToStr(page.PageNum)
  131. }
  132. param := make(map[string]interface{})
  133. where["where"] = where["where"] + " AND `zy_courses`.`course_id` = {{course_id}}"
  134. param["course_id"] = course_id
  135. name := c.Query("name")
  136. if name != "" {
  137. where["where"] = where["where"] + " AND `zy_courses`.`name` LIKE {{name}}"
  138. param["name"] = "%" + name + "%"
  139. }
  140. type_ := utils.ToInt(c.Query("type"))
  141. if type_ != 0 {
  142. where["where"] = where["where"] + " AND `zy_courses`.`type` = {{type}}"
  143. param["type"] = type_
  144. }
  145. total, err := courses.CountRaw(where["where"], param)
  146. if err != nil {
  147. app.Error(c, err.Error())
  148. return
  149. }
  150. type CoursesList struct {
  151. ID int `json:"id"`
  152. CourseId int `json:"course_id"`
  153. Name string `json:"name"`
  154. Type int `json:"type"`
  155. Content string `json:"content"`
  156. Orders int `json:"orders"`
  157. State int `json:"state"`
  158. CreatedAt string `json:"created_at"`
  159. UpdatedAt int `json:"updated_at"`
  160. }
  161. 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")) + ") "
  162. coursesList := make([]CoursesList, 0)
  163. if err = db.GetMultiRaw(field, where, param, &coursesList); err != nil {
  164. app.Error(c, err.Error())
  165. return
  166. }
  167. for k, v := range coursesList {
  168. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  169. coursesList[k] = v
  170. }
  171. data := gin.H{
  172. "learn": learn,
  173. "learns": learns,
  174. "courseInfo": courseInfo,
  175. "list": coursesList,
  176. "total": total,
  177. "limit": page.PageSize,
  178. }
  179. app.Success(c, data)
  180. }
  181. func CoursesInfo(c *gin.Context) {
  182. courses_id := utils.ToInt(c.Param("id"))
  183. if courses_id <= 0 {
  184. app.Error(c, "章节id有误")
  185. return
  186. }
  187. courses, err := courses.GetOne(map[string]interface{}{"id": courses_id}, nil, nil)
  188. if err != nil {
  189. app.Error(c, err.Error())
  190. return
  191. }
  192. err = check.CencelCheck(courses.CourseId, c.GetInt("adminID"))
  193. if err != nil {
  194. app.Error(c, err.Error())
  195. return
  196. }
  197. err = learn.LearnCourses(courses_id, c.GetInt("adminID"))
  198. if err != nil {
  199. app.Error(c, err.Error())
  200. return
  201. }
  202. app.Success(c, courses)
  203. }
  204. func CheckCourse(c *gin.Context) {
  205. course_id := utils.ToInt(c.Param("id"))
  206. if course_id <= 0 {
  207. app.Error(c, "课程id有误")
  208. return
  209. }
  210. id, err := check.CheckCourse(course_id, c.GetInt("adminID"))
  211. if err != nil {
  212. app.Error(c, err.Error())
  213. return
  214. }
  215. app.Success(c, gin.H{"id": id})
  216. }
  217. func CheckList(c *gin.Context) {
  218. course_id := utils.ToInt(c.Query("course_id"))
  219. if course_id <= 0 {
  220. app.Error(c, "课程id有误")
  221. return
  222. }
  223. err := check.CencelCheck(course_id, c.GetInt("adminID"))
  224. if err != nil {
  225. app.Error(c, err.Error())
  226. return
  227. }
  228. courseInfo, err := course.GetOne(map[string]interface{}{"id": course_id}, []string{"`id`", "`name`", "`intro`", "`role_ids`", "`show`"}, nil)
  229. if err != nil {
  230. app.Error(c, err.Error())
  231. return
  232. }
  233. page := app.HandlePageNums(c)
  234. where := map[string]string{
  235. "where": "`zy_check`.`id`>0 AND `zy_check`.`state` < 2",
  236. "_group_by": "`zy_check`.`id`",
  237. "_order_by": "`zy_check`.`id` desc",
  238. }
  239. if page.PageSize != 0 {
  240. where["_page_size"] = utils.ToStr(page.PageSize)
  241. where["_page_num"] = utils.ToStr(page.PageNum)
  242. }
  243. param := make(map[string]interface{})
  244. where["where"] = where["where"] + " AND `zy_check`.`course_id` = {{course_id}}"
  245. param["course_id"] = course_id
  246. where["where"] = where["where"] + " AND `zy_check`.`admin_id` = {{admin_id}}"
  247. param["admin_id"] = c.GetInt("adminID")
  248. state := utils.ToInt(c.Query("state"))
  249. if state != 0 {
  250. where["where"] = where["where"] + " AND `zy_courses`.`state` = {{state}}"
  251. param["state"] = state
  252. }
  253. total, err := check.CountRaw(where["where"], param)
  254. if err != nil {
  255. app.Error(c, err.Error())
  256. return
  257. }
  258. type CheckList struct {
  259. ID int `json:"id"`
  260. CourseId int `json:"course_id"`
  261. AdminId int `json:"admin_id"`
  262. Total int `json:"total"`
  263. Progress int `json:"progress"`
  264. Right int `json:"right"`
  265. State int `json:"state"`
  266. CreatedAt string `json:"created_at"`
  267. UpdatedAt int `json:"updated_at"`
  268. }
  269. checkList := make([]CheckList, 0)
  270. if err = check.GetChecksRaw(where, param, &checkList); err != nil {
  271. app.Error(c, err.Error())
  272. return
  273. }
  274. for k, v := range checkList {
  275. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  276. checkList[k] = v
  277. }
  278. data := gin.H{
  279. "courseInfo": courseInfo,
  280. "list": checkList,
  281. "total": total,
  282. "limit": page.PageSize,
  283. }
  284. app.Success(c, data)
  285. }
  286. func CheckInfo(c *gin.Context) {
  287. check_id := utils.ToInt(c.Param("id"))
  288. if check_id <= 0 {
  289. app.Error(c, "考核id有误")
  290. return
  291. }
  292. checkInfo, err := check.GetOne(map[string]interface{}{"id": check_id}, nil, nil)
  293. if err != nil {
  294. app.Error(c, err.Error())
  295. return
  296. }
  297. where := map[string]string{
  298. "where": "`zy_checks`.`id`>0",
  299. "_group_by": "`zy_checks`.`id`",
  300. "_order_by": "`zy_checks`.`id` asc",
  301. }
  302. param := make(map[string]interface{})
  303. where["where"] = where["where"] + " AND `zy_checks`.`check_id` = {{check_id}}"
  304. param["check_id"] = checkInfo.ID
  305. type ChecksList struct {
  306. ID int `json:"id"`
  307. CheckId int `json:"check_id"`
  308. Type int `json:"type"`
  309. QuestionId int `json:"question_id"`
  310. Content string `json:"content"`
  311. Options string `json:"options"`
  312. Answer string `json:"answer"`
  313. Right string `json:"right"`
  314. CreatedAt string `json:"created_at"`
  315. UpdatedAt string `json:"updated_at"`
  316. }
  317. checksList := make([]ChecksList, 0)
  318. if err = checks.GetCheckssRaw(where, param, &checksList); err != nil {
  319. app.Error(c, err.Error())
  320. return
  321. }
  322. for k, v := range checksList {
  323. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
  324. v.UpdatedAt = utils.DateS(v.UpdatedAt, "YYYY-MM-DD HH:mm")
  325. checksList[k] = v
  326. }
  327. data := gin.H{
  328. "info": checkInfo,
  329. "checks": checksList,
  330. }
  331. app.Success(c, data)
  332. }
  333. func SelectChecks(c *gin.Context) {
  334. var form form.ChecksSelect
  335. if app.Bind(c, &form) != nil {
  336. return
  337. }
  338. err := checks.SelectChecks(form, c.GetInt("adminID"))
  339. if err != nil {
  340. app.Error(c, err.Error())
  341. return
  342. }
  343. app.Success(c, nil)
  344. }
  345. func SubmitCheck(c *gin.Context) {
  346. check_id := utils.ToInt(c.Param("id"))
  347. if check_id <= 0 {
  348. app.Error(c, "考核id有误")
  349. return
  350. }
  351. err := check.SubmitCheck(check_id, c.GetInt("adminID"))
  352. if err != nil {
  353. app.Error(c, err.Error())
  354. return
  355. }
  356. app.Success(c, nil)
  357. }