dao-exam.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package dao
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "exam_system/config"
  6. "exam_system/entity"
  7. "exam_system/result"
  8. "exam_system/utils"
  9. "exam_system/vo"
  10. "github.com/jmoiron/sqlx"
  11. "strings"
  12. "time"
  13. )
  14. func ExamInfoList(page, size int, sort string, query map[string]interface{}, uid int) *result.Result {
  15. // 1、获取list
  16. sqlStr := `select er.id, e.name,e.status, e.start_at, e.end_at, e.duration, e.publish_at,e.tp_id,er.status state,tp.score,tp.pass_score
  17. from exam_record er left join user u on er.user_id = u.id
  18. left join exam e on e.id = er.exam_id
  19. left join test_paper tp on e.tp_id = tp.id
  20. left join exam_class ec on e.id = ec.exam_id
  21. left join class c on ec.class_id = c.id
  22. where er.status =0 and u.id = ? `
  23. params := []interface{}{uid}
  24. if query["name"] != nil {
  25. sqlStr += "and e.name like ? "
  26. params = append(params, "%"+query["name"].(string)+"%")
  27. }
  28. // 0 进行中 1已结束
  29. if query["status"] != nil {
  30. if query["status"].(float64) == 0 {
  31. sqlStr += "and e.end_at < ? "
  32. } else {
  33. sqlStr += "and e.end_at > ? "
  34. }
  35. params = append(params, time.Now())
  36. }
  37. sqlStr += "group by er.id "
  38. if sort == "asc" {
  39. sqlStr += "order by e.create_at asc "
  40. } else {
  41. sqlStr += "order by e.create_at desc "
  42. }
  43. sqlStr += "limit ?,?"
  44. params = append(params, utils.PostionSize(page, size), size)
  45. var evo []*vo.ExamVo1
  46. err := config.DB.Select(&evo, sqlStr, params...)
  47. if err != nil {
  48. return result.UNKNOW_ERROR.SetMsg(err.Error())
  49. }
  50. // 2、总数
  51. sqlStr = `select count(er.id)
  52. from exam_record er left join user u on er.user_id = u.id
  53. left join exam e on e.id = er.exam_id
  54. left join test_paper tp on e.tp_id = tp.id
  55. left join exam_class ec on e.id = ec.exam_id
  56. left join class c on ec.class_id = c.id
  57. where er.status =0 and u.id = ? `
  58. params = []interface{}{uid}
  59. if query["name"] != nil {
  60. sqlStr += "and e.name like ? "
  61. params = append(params, "%"+query["name"].(string)+"%")
  62. }
  63. // 0 进行中 1已结束
  64. if query["status"] != nil {
  65. if query["status"].(float64) == 0 {
  66. sqlStr += "and e.end_at < ? "
  67. } else {
  68. sqlStr += "and e.end_at > ? "
  69. }
  70. params = append(params, time.Now())
  71. }
  72. var total int
  73. err = config.DB.Get(&total, sqlStr, params...)
  74. if err != nil {
  75. return result.UNKNOW_ERROR.SetMsg(err.Error())
  76. }
  77. return result.SUCCESS.SetData(result.NewPage(page, len(evo), total, evo))
  78. }
  79. func ExamInfoList1(page, size int, sort string, query map[string]interface{}, uid int) *result.Result {
  80. // 1、获取list
  81. sqlStr := `select e.*,tp.score,tp.pass_score
  82. from exam e
  83. left join test_paper tp on e.tp_id = tp.id
  84. left join exam_class ec on e.id = ec.exam_id
  85. left join class c on ec.class_id = c.id
  86. left join user u on u.class_id = c.id
  87. where u.id = ? `
  88. params := []interface{}{uid}
  89. if query["name"] != nil {
  90. sqlStr += "and e.name like ? "
  91. params = append(params, "%"+query["name"].(string)+"%")
  92. }
  93. // 0 进行中 1已结束
  94. if query["status"] != nil {
  95. if query["status"].(float64) == 0 {
  96. sqlStr += "and e.end_at < ? "
  97. } else {
  98. sqlStr += "and e.end_at > ? "
  99. }
  100. params = append(params, time.Now())
  101. }
  102. sqlStr += "group by e.id "
  103. if sort == "asc" {
  104. sqlStr += "order by e.create_at asc "
  105. } else {
  106. sqlStr += "order by e.create_at desc "
  107. }
  108. sqlStr += "limit ?,?"
  109. params = append(params, utils.PostionSize(page, size), size)
  110. var evo []*vo.ExamVo1
  111. err := config.DB.Select(&evo, sqlStr, params...)
  112. if err != nil {
  113. return result.UNKNOW_ERROR.SetMsg(err.Error())
  114. }
  115. // 2、总数
  116. sqlStr = `select count(e.id)
  117. from exam e
  118. left join test_paper tp on e.tp_id = tp.id
  119. left join exam_class ec on e.id = ec.exam_id
  120. left join class c on ec.class_id = c.id
  121. left join user u on u.class_id = c.id
  122. where u.id = ? `
  123. params = []interface{}{uid}
  124. if query["name"] != nil {
  125. sqlStr += "and e.name like ? "
  126. params = append(params, "%"+query["name"].(string)+"%")
  127. }
  128. // 0 进行中 1已结束
  129. if query["status"] != nil {
  130. if query["status"].(float64) == 0 {
  131. sqlStr += "and e.end_at < ? "
  132. } else {
  133. sqlStr += "and e.end_at > ? "
  134. }
  135. params = append(params, time.Now())
  136. }
  137. var total int
  138. err = config.DB.Get(&total, sqlStr, params...)
  139. if err != nil {
  140. return result.UNKNOW_ERROR.SetMsg(err.Error())
  141. }
  142. return result.SUCCESS.SetData(result.NewPage(page, len(evo), total, evo))
  143. }
  144. func AddExam(e *entity.Exam) *result.Result {
  145. tx, err := config.DB.Beginx()
  146. return utils.Transation(tx, err, func(tx *sqlx.Tx) *result.Result {
  147. // 1、获取考生人数
  148. classIds := strings.Split(e.ClassIds, ",")
  149. var users []*entity.User
  150. sqlStr := "select id from user where class_id in (?)"
  151. var query string
  152. var args []interface{}
  153. query, args, err = sqlx.In(sqlStr, classIds)
  154. if err != nil {
  155. return result.UNKNOW_ERROR.SetData(err)
  156. }
  157. query = tx.Rebind(query)
  158. err = tx.Select(&users, query, args...)
  159. if err != nil {
  160. return result.UNKNOW_ERROR.SetMsg(err.Error())
  161. }
  162. // 2、插入
  163. sqlStr = "insert into exam(name,create_at,update_at,start_at,end_at,publish_at,duration,status,tp_id,user_count) values (?,?,?,?,?,?,?,?,?,?)"
  164. var res sql.Result
  165. res, err = tx.Exec(sqlStr, e.Name, time.Now(), time.Now(), e.StartAt, e.EndAt, e.PublishAt, e.Duration, e.Status, e.TpId, len(users))
  166. if err != nil {
  167. return result.UNKNOW_ERROR.SetMsg(err.Error())
  168. }
  169. examId, _ := res.LastInsertId()
  170. e.Id = int(examId)
  171. // 3、插入考试班级表
  172. sqlStr = "insert into exam_class(exam_id,class_id,create_at,update_at) values (?,?,?,?)"
  173. var stmt *sql.Stmt
  174. stmt, err = tx.Prepare(sqlStr)
  175. for _, classId := range classIds {
  176. _, err = stmt.Exec(examId, classId, time.Now(), time.Now())
  177. if err != nil {
  178. return result.UNKNOW_ERROR.SetMsg(err.Error())
  179. }
  180. }
  181. // 4、插入考试记录表
  182. sqlStr = "insert into exam_record(create_at, update_at, user_id, exam_id,status) values (?,?,?,?,?)"
  183. stmt, err = tx.Prepare(sqlStr)
  184. if err != nil {
  185. return result.UNKNOW_ERROR.SetMsg(err.Error())
  186. }
  187. for _, user := range users {
  188. res, err = stmt.Exec(time.Now(), time.Now(), user.ID, examId, entity.NOT_TESTED)
  189. if err != nil {
  190. return result.UNKNOW_ERROR.SetMsg(err.Error())
  191. }
  192. }
  193. var data []byte
  194. data, err = json.Marshal(&e)
  195. if err != nil {
  196. return result.UNKNOW_ERROR.SetMsg(err.Error())
  197. }
  198. // 用来判断考试过期的
  199. config.JS.PublishAsync("EXAMS.info", data)
  200. return result.SuccessResult(examId)
  201. })
  202. }
  203. func ExamDetail(id string) *result.Result {
  204. // 1.查询考试
  205. sqlStr := "select e.*,t.name tp_name,t.score from exam e left join test_paper t on e.tp_id=t.id where e.id=? and e.delete_at is null"
  206. var e vo.ExamVo
  207. err := config.DB.Get(&e, sqlStr, id)
  208. if err != nil {
  209. if err == sql.ErrNoRows {
  210. return result.DATA_NOT_FOUND
  211. }
  212. return result.UNKNOW_ERROR.SetMsg(err.Error())
  213. }
  214. // 2.查询班级
  215. var classes []*entity.Class
  216. sqlStr = `SELECT
  217. c.*
  218. FROM
  219. exam e
  220. LEFT JOIN exam_class ec ON e.id = ec.exam_id
  221. LEFT JOIN class c ON c.id = ec.class_id
  222. WHERE
  223. e.id = ? and c.delete_at is null
  224. GROUP BY
  225. c.id
  226. `
  227. err = config.DB.Select(&classes, sqlStr, id)
  228. if err != nil {
  229. return result.UNKNOW_ERROR.SetMsg(err.Error())
  230. }
  231. e.Classes = classes
  232. return result.SuccessResult(e)
  233. }
  234. func ExamList(page, size int, sort string, query map[string]interface{}) *result.Result {
  235. sqlStr := "select e.*,t.name tp_name,t.score from exam e left join test_paper t on e.tp_id=t.id where e.delete_at is null "
  236. params := make([]interface{}, 0)
  237. if query["name"] != nil {
  238. params = append(params, "%"+query["name"].(string)+"%")
  239. sqlStr += "and e.name like ? "
  240. }
  241. if query["start_at"] != nil {
  242. params = append(params, query["start_at"])
  243. sqlStr += "and e.start_at >= ? "
  244. }
  245. if query["end_at"] != nil {
  246. params = append(params, query["end_at"])
  247. sqlStr += "and e.end_at <= ? "
  248. }
  249. sqlStr += "group by e.id order by e.create_at "
  250. if sort == "asc" {
  251. sqlStr += "asc "
  252. } else {
  253. sqlStr += "desc "
  254. }
  255. sqlStr += "limit ?,?"
  256. params = append(params, utils.PostionSize(page, size), size)
  257. var evs []*vo.ExamVo
  258. err := config.DB.Select(&evs, sqlStr, params...)
  259. if err != nil {
  260. return result.UNKNOW_ERROR.SetMsg(err.Error())
  261. }
  262. // 2.查询班级
  263. for _, ev := range evs {
  264. var classes []*entity.Class
  265. sqlStr = `SELECT
  266. c.*
  267. FROM
  268. exam e
  269. LEFT JOIN exam_class ec ON e.id = ec.exam_id
  270. LEFT JOIN class c ON c.id = ec.class_id
  271. WHERE
  272. e.id = ? and c.delete_at is null and c.id is not null
  273. GROUP BY
  274. c.id
  275. `
  276. err = config.DB.Select(&classes, sqlStr, ev.Id)
  277. if err != nil {
  278. return result.UNKNOW_ERROR.SetMsg(err.Error())
  279. }
  280. ev.Classes = classes
  281. }
  282. // 总数
  283. sqlStr = "select count(e.id) from exam e left join test_paper t on e.tp_id=t.id where e.delete_at is null "
  284. params = make([]interface{}, 0)
  285. if query["name"] != nil {
  286. params = append(params, "%"+query["name"].(string)+"%")
  287. sqlStr += "and e.name like ? "
  288. }
  289. if query["start_at"] != nil {
  290. params = append(params, query["start_at"])
  291. sqlStr += "and e.start_at = ? "
  292. }
  293. if query["end_at"] != nil {
  294. params = append(params, query["end_at"])
  295. sqlStr += "and e.end_at = ? "
  296. }
  297. var total int
  298. err = config.DB.Get(&total, sqlStr, params...)
  299. if err != nil {
  300. return result.UNKNOW_ERROR.SetMsg(err.Error())
  301. }
  302. return result.SUCCESS.SetData(result.NewPage(page, len(evs), total, evs))
  303. }
  304. func UpdateExam(e *entity.Exam) *result.Result {
  305. tx, err := config.DB.Beginx()
  306. return utils.Transation(tx, err, func(tx *sqlx.Tx) *result.Result {
  307. // 1、获取考生人数
  308. classIds := strings.Split(e.ClassIds, ",")
  309. var users []*entity.User
  310. sqlStr := "select id,class_id from user where class_id in (?)"
  311. var query string
  312. var args []interface{}
  313. query, args, err = sqlx.In(sqlStr, classIds)
  314. if err != nil {
  315. return result.UNKNOW_ERROR.SetData(err.Error())
  316. }
  317. query = config.DB.Rebind(query)
  318. err = config.DB.Select(&users, query, args...)
  319. if err != nil {
  320. return result.UNKNOW_ERROR.SetMsg(err.Error())
  321. }
  322. // 2、更新
  323. sqlStr = "update exam set name=?,update_at=?,start_at=?,end_at=?,publish_at=?,duration=?,status=?,tp_id=?,user_count=? where id=? and delete_at is null "
  324. var res sql.Result
  325. res, err = config.DB.Exec(sqlStr, e.Name, time.Now(), e.StartAt, e.EndAt, e.PublishAt, e.Duration, e.Status, e.TpId, len(users), e.Id)
  326. if err != nil {
  327. return result.UNKNOW_ERROR.SetData(err)
  328. }
  329. affected, _ := res.RowsAffected()
  330. // 3、删除不是该班级学生的考试记录(班级 用户)
  331. sqlStr = `DELETE er FROM exam_record er,exam_class ec WHERE er.exam_id = ec.exam_id and ec.class_id not in (?) and er.exam_id=?`
  332. query, args, err = sqlx.In(sqlStr, classIds, e.Id)
  333. _, err = tx.Exec(query, args...)
  334. if err != nil {
  335. return result.UNKNOW_ERROR.SetData(err)
  336. }
  337. // 4、插入考试班级表
  338. // 4.1、 先删除所有关联
  339. sqlStr = "delete from exam_class where exam_id = ?"
  340. res, err = tx.Exec(sqlStr, e.Id)
  341. if err != nil {
  342. return result.UNKNOW_ERROR.SetData(err)
  343. }
  344. // 4.2、 重新建立关联
  345. sqlStr = "insert into exam_class(exam_id,class_id,create_at,update_at) values (?,?,?,?)"
  346. var stmt *sql.Stmt
  347. stmt, err = tx.Prepare(sqlStr)
  348. classMap := map[string]byte{}
  349. for _, classId := range classIds {
  350. classMap[classId] = 0
  351. _, err = stmt.Exec(e.Id, classId, time.Now(), time.Now())
  352. if err != nil {
  353. return result.UNKNOW_ERROR.SetMsg(err.Error())
  354. }
  355. }
  356. // 4、插入考试记录表,如果user的classid存在则(不管,或者插入),不存在删除
  357. sqlStr = "insert into exam_record(create_at, update_at, user_id, exam_id,status) values (?,?,?,?,?) "
  358. stmt, err = tx.Prepare(sqlStr)
  359. if err != nil {
  360. return result.UNKNOW_ERROR.SetMsg(err.Error())
  361. }
  362. for _, user := range users {
  363. sqlStr = "select id from exam_record er where user_id = ? limit 1"
  364. var userId int64
  365. err = tx.Get(&userId, sqlStr, user.ID)
  366. if err != nil && err != sql.ErrNoRows {
  367. return result.UNKNOW_ERROR.SetMsg(err.Error())
  368. }
  369. if userId == 0 {
  370. res, err = stmt.Exec(time.Now(), time.Now(), user.ID, e.Id, entity.NOT_TESTED)
  371. if err != nil {
  372. return result.UNKNOW_ERROR.SetMsg(err.Error())
  373. }
  374. }
  375. }
  376. var data []byte
  377. data, err = json.Marshal(&e)
  378. if err != nil {
  379. return result.UNKNOW_ERROR.SetMsg(err.Error())
  380. }
  381. // 用来判断考试过期的
  382. config.JS.PublishAsync("EXAMS.info", data)
  383. return result.SuccessResult(result.NewResultChange(affected))
  384. })
  385. }
  386. func DeleteExams(ids []string) *result.Result {
  387. sqlStr := "update exam set delete_at=? where id in (?)"
  388. query, param, err := sqlx.In(sqlStr, time.Now(), ids)
  389. if err != nil {
  390. return result.UNKNOW_ERROR.SetMsg(err.Error())
  391. }
  392. res, err := config.DB.Exec(query, param...)
  393. if err != nil {
  394. return result.UNKNOW_ERROR.SetMsg(err.Error())
  395. }
  396. affected, _ := res.RowsAffected()
  397. return result.SUCCESS.SetData(result.NewResultChange(affected))
  398. }