dao-test-paper.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. "fmt"
  11. "time"
  12. "github.com/jmoiron/sqlx"
  13. )
  14. func AddTestPaper(t *vo.TestPaperVo) *result.Result {
  15. tx, err := config.DB.Beginx()
  16. return utils.Transation(tx, err, func(tx *sqlx.Tx) *result.Result {
  17. // 1.插入试卷
  18. sqlStr := "insert into test_paper(name,create_at,update_at,status,score,pass_score) values (?,?,?,?,?,?)"
  19. var res sql.Result
  20. res, err = tx.Exec(sqlStr, t.Name, time.Now(), time.Now(), entity.USE, t.Score, t.PassScore)
  21. if err != nil {
  22. return result.UNKNOW_ERROR.SetData(err)
  23. }
  24. id, _ := res.LastInsertId()
  25. var score int64
  26. for _, chapter := range t.Chapters {
  27. var subData []byte
  28. subData, err = json.Marshal(chapter.SubList)
  29. // 2.插入章节
  30. sqlStr = "insert into chapter(name,description,sub_ids,tp_id,`index`) values (?,?,?,?,?)"
  31. // sublist试题
  32. var res2 sql.Result
  33. res2, err = tx.Exec(sqlStr, chapter.Name, chapter.Description, string(subData), id, chapter.Index)
  34. if err != nil {
  35. return result.UNKNOW_ERROR.SetData(err)
  36. }
  37. affected, _ := res2.RowsAffected()
  38. score += affected
  39. }
  40. return result.SuccessResult(id)
  41. })
  42. }
  43. func TestPaperDetail(id string) *result.Result {
  44. // 1、查询试卷
  45. sqlStr := "select * from test_paper where id=? and delete_at is null"
  46. var t vo.TestPaperVo
  47. err := config.DB.Get(&t, sqlStr, id)
  48. if err != nil {
  49. if err == sql.ErrNoRows {
  50. return result.DATA_NOT_FOUND
  51. }
  52. return result.UNKNOW_ERROR.SetData(err.Error())
  53. }
  54. // 2、查询相关章节
  55. sqlStr = "select * from chapter where tp_id =? order by `index`"
  56. var chapters []*vo.ChapterVo
  57. err = config.DB.Select(&chapters, sqlStr, t.Id)
  58. if err != nil {
  59. return result.UNKNOW_ERROR.SetMsg(err.Error())
  60. }
  61. // 3、获取相关试题,chapter
  62. for _, chap := range chapters {
  63. var sidsvo []*vo.SubIdsVo
  64. err = json.Unmarshal([]byte(chap.SubIds), &sidsvo)
  65. if err != nil {
  66. return result.UNKNOW_ERROR.SetMsg(err.Error())
  67. }
  68. var subList []*vo.SubjectVo
  69. for _, sidvo := range sidsvo {
  70. var sub vo.SubjectVo
  71. sqlStr = "select * from subject where id = ?"
  72. err = config.DB.Get(&sub, sqlStr, sidvo.Id)
  73. if err != nil && err.Error() != "sql: no rows in result set" {
  74. return result.UNKNOW_ERROR.SetMsg(err.Error())
  75. }
  76. sub.Score = sidvo.Score
  77. subList = append(subList, &sub)
  78. }
  79. chap.SubList = subList
  80. }
  81. t.Chapters = chapters
  82. return result.SuccessResult(t)
  83. }
  84. func TestPaperDetail2(id string) *result.Result {
  85. // 1、查询试卷
  86. sqlStr := "select * from test_paper where id=? and delete_at is null"
  87. var t vo.TestPaperVo
  88. err := config.DB.Get(&t, sqlStr, id)
  89. if err != nil {
  90. if err == sql.ErrNoRows {
  91. return result.DATA_NOT_FOUND
  92. }
  93. return result.UNKNOW_ERROR.SetData(err.Error())
  94. }
  95. // 2、查询相关章节
  96. sqlStr = "select * from chapter where tp_id =? order by `index`"
  97. var chapters []*vo.ChapterVo
  98. err = config.DB.Select(&chapters, sqlStr, t.Id)
  99. if err != nil {
  100. return result.UNKNOW_ERROR.SetMsg(err.Error())
  101. }
  102. // 3、获取相关试题,chapter
  103. for _, chap := range chapters {
  104. var sidsvo []*vo.SubIdsVo
  105. err = json.Unmarshal([]byte(chap.SubIds), &sidsvo)
  106. if err != nil {
  107. return result.UNKNOW_ERROR.SetMsg(err.Error())
  108. }
  109. var subList []*vo.SubjectVo
  110. for _, sidvo := range sidsvo {
  111. var sub vo.SubjectVo
  112. sqlStr = "select id, create_at, update_at, delete_at, type, opt_a, opt_b, opt_c, opt_d, analysis, status, question, qb_id from subject where id = ?"
  113. err = config.DB.Get(&sub, sqlStr, sidvo.Id)
  114. if err != nil && err.Error() != "sql: no rows in result set" {
  115. return result.UNKNOW_ERROR.SetMsg(err.Error())
  116. }
  117. sub.Score = sidvo.Score
  118. subList = append(subList, &sub)
  119. }
  120. chap.SubList = subList
  121. }
  122. t.Chapters = chapters
  123. return result.SuccessResult(t)
  124. }
  125. func TestPaperList(page, size int, sort string, query map[string]interface{}) *result.Result {
  126. // 1、获取paper
  127. sqlStr := "select * from test_paper where delete_at is null "
  128. params := make([]interface{}, 0)
  129. if query["name"] != nil {
  130. sqlStr += "and name like ? "
  131. params = append(params, "%"+query["name"].(string)+"%")
  132. }
  133. if query["status"] != nil {
  134. sqlStr += "and status = ? "
  135. params = append(params, fmt.Sprintf("%1.0f", query["status"].(float64)))
  136. }
  137. sqlStr += "order by create_at "
  138. if sort == "asc" {
  139. sqlStr += "asc "
  140. } else {
  141. sqlStr += "desc "
  142. }
  143. sqlStr += "limit ?,?"
  144. params = append(params, utils.PostionSize(page, size), size)
  145. var tp []*vo.TestPaperVo
  146. err := config.DB.Select(&tp, sqlStr, params...)
  147. if err != nil {
  148. return result.UNKNOW_ERROR.SetMsg(err.Error())
  149. }
  150. // 2、获取章节
  151. for _, t := range tp {
  152. // 2、查询相关章节
  153. chapterStr := "select * from chapter where tp_id =?"
  154. var chapters []*vo.ChapterVo
  155. err = config.DB.Select(&chapters, chapterStr, t.Id)
  156. if err != nil {
  157. return result.UNKNOW_ERROR.SetData(err)
  158. }
  159. // 3、获取相关试题,chapter
  160. for _, chap := range chapters {
  161. var sidsvo []*vo.SubIdsVo
  162. err = json.Unmarshal([]byte(chap.SubIds), &sidsvo)
  163. if err != nil {
  164. return result.UNKNOW_ERROR.SetMsg(err.Error())
  165. }
  166. var subList []*vo.SubjectVo
  167. for _, sidvo := range sidsvo {
  168. var sub vo.SubjectVo
  169. sqlStr = "select * from subject where id = ?"
  170. err = config.DB.Get(&sub, sqlStr, sidvo.Id)
  171. if err != nil && err.Error() != "sql: no rows in result set" {
  172. return result.UNKNOW_ERROR.SetMsg(err.Error())
  173. }
  174. sub.Score = sidvo.Score
  175. subList = append(subList, &sub)
  176. }
  177. chap.SubList = subList
  178. }
  179. t.Chapters = chapters
  180. }
  181. // 获取总数
  182. sqlStr = "select count(id) from test_paper where delete_at is null "
  183. var total int
  184. params = make([]interface{}, 0)
  185. if query["name"] != nil {
  186. sqlStr += "and name like ? "
  187. params = append(params, "%"+query["name"].(string)+"%")
  188. }
  189. if query["status"] != nil {
  190. sqlStr += "and status = ? "
  191. params = append(params, fmt.Sprintf("%1.0f", query["status"].(float64)))
  192. }
  193. err = config.DB.Get(&total, sqlStr, params...)
  194. if err != nil {
  195. return result.UNKNOW_ERROR.SetData(err.Error())
  196. }
  197. return result.SUCCESS.SetData(result.NewPage(page, len(tp), total, tp))
  198. }
  199. func UpdateTestPaper(t *vo.TestPaperVo) *result.Result {
  200. tx, err := config.DB.Beginx()
  201. if err != nil {
  202. return result.UNKNOW_ERROR.SetData(err)
  203. }
  204. return utils.Transation(tx, err, func(tx *sqlx.Tx) *result.Result {
  205. sqlStr := "update test_paper set name=?,update_at=?,status=?,score=?,pass_score=? where id=? and delete_at is null "
  206. var res sql.Result
  207. res, err = config.DB.Exec(sqlStr, t.Name, time.Now(), t.Status, t.Score, t.PassScore, t.Id)
  208. if err != nil {
  209. return result.UNKNOW_ERROR.SetData(err)
  210. }
  211. affected, _ := res.RowsAffected()
  212. sqlStr = "delete from chapter where tp_id = ?"
  213. _, err = tx.Exec(sqlStr, t.Id)
  214. if err != nil {
  215. return result.UNKNOW_ERROR.SetData(err)
  216. }
  217. for _, chapter := range t.Chapters {
  218. sqlStr = "insert into chapter(name,description,sub_ids,tp_id) values (?,?,?,?)"
  219. var subData []byte
  220. subData, err = json.Marshal(chapter.SubList)
  221. // sublist试题
  222. res, err = tx.Exec(sqlStr, chapter.Name, chapter.Description, subData, t.Id)
  223. if err != nil {
  224. return result.UNKNOW_ERROR.SetData(err)
  225. }
  226. affected2, _ := res.RowsAffected()
  227. affected += affected2
  228. }
  229. return result.SuccessResult(result.NewResultChange(affected))
  230. })
  231. }
  232. func DeleteTestPapers(ids []string) *result.Result {
  233. parm := []interface{}{time.Now()}
  234. parmStr := ""
  235. for k, id := range ids {
  236. parm = append(parm, id)
  237. if k == (len(ids) - 1) {
  238. parmStr += "?"
  239. } else {
  240. parmStr += "?,"
  241. }
  242. }
  243. sqlStr := fmt.Sprintf("update test_paper set delete_at=? where id in (%s)", parmStr)
  244. res, err := config.DB.Exec(sqlStr, parm...)
  245. if err != nil {
  246. return result.UNKNOW_ERROR.SetMsg(err.Error())
  247. }
  248. affected, _ := res.RowsAffected()
  249. return result.SUCCESS.SetData(result.NewResultChange(affected))
  250. }