dao-exam.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. _, 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. return result.SuccessResult(examId)
  193. })
  194. }
  195. func ExamDetail(id string) *result.Result {
  196. // 1.查询考试
  197. 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"
  198. var e vo.ExamVo
  199. err := config.DB.Get(&e, sqlStr, id)
  200. if err != nil {
  201. if err == sql.ErrNoRows {
  202. return result.DATA_NOT_FOUND
  203. }
  204. return result.UNKNOW_ERROR.SetMsg(err.Error())
  205. }
  206. // 2.查询班级
  207. var classes []*entity.Class
  208. sqlStr = `SELECT
  209. c.*
  210. FROM
  211. exam e
  212. LEFT JOIN exam_class ec ON e.id = ec.exam_id
  213. LEFT JOIN class c ON c.id = ec.class_id
  214. WHERE
  215. e.id = ? and c.delete_at is null
  216. GROUP BY
  217. c.id
  218. `
  219. err = config.DB.Select(&classes, sqlStr, id)
  220. if err != nil {
  221. return result.UNKNOW_ERROR.SetMsg(err.Error())
  222. }
  223. e.Classes = classes
  224. return result.SuccessResult(e)
  225. }
  226. func ExamList(page, size int, sort string, query map[string]interface{}) *result.Result {
  227. 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 "
  228. params := make([]interface{}, 0)
  229. if query["name"] != nil {
  230. params = append(params, "%"+query["name"].(string)+"%")
  231. sqlStr += "and e.name like ? "
  232. }
  233. if query["start_at"] != nil {
  234. params = append(params, query["start_at"])
  235. sqlStr += "and e.start_at >= ? "
  236. }
  237. if query["end_at"] != nil {
  238. params = append(params, query["end_at"])
  239. sqlStr += "and e.end_at <= ? "
  240. }
  241. sqlStr += "group by e.id order by e.create_at "
  242. if sort == "asc" {
  243. sqlStr += "asc "
  244. } else {
  245. sqlStr += "desc "
  246. }
  247. sqlStr += "limit ?,?"
  248. params = append(params, utils.PostionSize(page, size), size)
  249. var evs []*vo.ExamVo
  250. err := config.DB.Select(&evs, sqlStr, params...)
  251. if err != nil {
  252. return result.UNKNOW_ERROR.SetMsg(err.Error())
  253. }
  254. // 2.查询班级
  255. for _, ev := range evs {
  256. var classes []*entity.Class
  257. sqlStr = `SELECT
  258. c.*
  259. FROM
  260. exam e
  261. LEFT JOIN exam_class ec ON e.id = ec.exam_id
  262. LEFT JOIN class c ON c.id = ec.class_id
  263. WHERE
  264. e.id = ? and c.delete_at is null and c.id is not null
  265. GROUP BY
  266. c.id
  267. `
  268. err = config.DB.Select(&classes, sqlStr, ev.Id)
  269. if err != nil {
  270. return result.UNKNOW_ERROR.SetMsg(err.Error())
  271. }
  272. ev.Classes = classes
  273. }
  274. // 总数
  275. 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 "
  276. params = make([]interface{}, 0)
  277. if query["name"] != nil {
  278. params = append(params, "%"+query["name"].(string)+"%")
  279. sqlStr += "and e.name like ? "
  280. }
  281. if query["start_at"] != nil {
  282. params = append(params, query["start_at"])
  283. sqlStr += "and e.start_at = ? "
  284. }
  285. if query["end_at"] != nil {
  286. params = append(params, query["end_at"])
  287. sqlStr += "and e.end_at = ? "
  288. }
  289. var total int
  290. err = config.DB.Get(&total, sqlStr, params...)
  291. if err != nil {
  292. return result.UNKNOW_ERROR.SetMsg(err.Error())
  293. }
  294. return result.SUCCESS.SetData(result.NewPage(page, len(evs), total, evs))
  295. }
  296. func UpdateExam(e *entity.Exam) *result.Result {
  297. tx, err := config.DB.Beginx()
  298. return utils.Transation(tx, err, func(tx *sqlx.Tx) *result.Result {
  299. // 1、获取考生人数
  300. classIds := strings.Split(e.ClassIds, ",")
  301. var users []*entity.User
  302. sqlStr := "select id,class_id from user where class_id in (?)"
  303. var query string
  304. var args []interface{}
  305. query, args, err = sqlx.In(sqlStr, classIds)
  306. if err != nil {
  307. return result.UNKNOW_ERROR.SetData(err.Error())
  308. }
  309. query = config.DB.Rebind(query)
  310. err = config.DB.Select(&users, query, args...)
  311. if err != nil {
  312. return result.UNKNOW_ERROR.SetMsg(err.Error())
  313. }
  314. // 2、更新
  315. 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 "
  316. var res sql.Result
  317. 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)
  318. if err != nil {
  319. return result.UNKNOW_ERROR.SetData(err)
  320. }
  321. affected, _ := res.RowsAffected()
  322. // 3、删除不是该班级学生的考试记录(班级 用户)
  323. 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=?`
  324. query, args, err = sqlx.In(sqlStr, classIds, e.Id)
  325. _, err = tx.Exec(query, args...)
  326. if err != nil {
  327. return result.UNKNOW_ERROR.SetData(err)
  328. }
  329. // 4、插入考试班级表
  330. // 4.1、 先删除所有关联
  331. sqlStr = "delete from exam_class where exam_id = ?"
  332. _, err = tx.Exec(sqlStr, e.Id)
  333. if err != nil {
  334. return result.UNKNOW_ERROR.SetData(err)
  335. }
  336. // 4.2、 重新建立关联
  337. sqlStr = "insert into exam_class(exam_id,class_id,create_at,update_at) values (?,?,?,?)"
  338. var stmt *sql.Stmt
  339. stmt, err = tx.Prepare(sqlStr)
  340. classMap := map[string]byte{}
  341. for _, classId := range classIds {
  342. classMap[classId] = 0
  343. _, err = stmt.Exec(e.Id, classId, time.Now(), time.Now())
  344. if err != nil {
  345. return result.UNKNOW_ERROR.SetMsg(err.Error())
  346. }
  347. }
  348. // 4、插入考试记录表,如果user的classid存在则(不管,或者插入),不存在删除
  349. sqlStr = "insert into exam_record(create_at, update_at, user_id, exam_id,status) values (?,?,?,?,?) "
  350. stmt, err = tx.Prepare(sqlStr)
  351. if err != nil {
  352. return result.UNKNOW_ERROR.SetMsg(err.Error())
  353. }
  354. for _, user := range users {
  355. sqlStr = "select id from exam_record er where user_id = ? limit 1"
  356. var userId int64
  357. err = tx.Get(&userId, sqlStr, user.ID)
  358. if err != nil && err != sql.ErrNoRows {
  359. return result.UNKNOW_ERROR.SetMsg(err.Error())
  360. }
  361. if userId == 0 {
  362. _, err = stmt.Exec(time.Now(), time.Now(), user.ID, e.Id, entity.NOT_TESTED)
  363. if err != nil {
  364. return result.UNKNOW_ERROR.SetMsg(err.Error())
  365. }
  366. }
  367. }
  368. return result.SuccessResult(result.NewResultChange(affected))
  369. })
  370. }
  371. func DeleteExams(ids []string) *result.Result {
  372. sqlStr := "update exam set delete_at=? where id in (?)"
  373. query, param, err := sqlx.In(sqlStr, time.Now(), ids)
  374. if err != nil {
  375. return result.UNKNOW_ERROR.SetMsg(err.Error())
  376. }
  377. res, err := config.DB.Exec(query, param...)
  378. if err != nil {
  379. return result.UNKNOW_ERROR.SetMsg(err.Error())
  380. }
  381. affected, _ := res.RowsAffected()
  382. return result.SUCCESS.SetData(result.NewResultChange(affected))
  383. }