dao-exam.go 13 KB

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