question.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "go-common/app/service/openplatform/anti-fraud/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _getQusSQL = "SELECT question_id ,question_type,answer_type,question_name,question_bank_id,difficulty,is_deleted FROM question where question_id=? and is_deleted =0 "
  14. _addQusSQL = "INSERT INTO question(question_id ,question_type,answer_type,question_name,question_bank_id,difficulty) VALUES(?,?,?,?,?,?)"
  15. _getQuslistBywhereSQL = "SELECT question_id ,question_type,answer_type,question_name,question_bank_id,difficulty,is_deleted FROM question where is_deleted = 0 and question_bank_id = ? order by id desc limit ? ,? "
  16. _getAllQusByBankID = "SELECT question_id FROM question WHERE question_bank_id = ? AND is_deleted = 0"
  17. _getQuslistSQL = "SELECT question_id ,question_type,answer_type,question_name,question_bank_id,difficulty,is_deleted FROM question where is_deleted = ? limit ? ,? "
  18. _getQusCntSQL = "SELECT COUNT(*) FROM question where is_deleted = 0 "
  19. _delQusSQL = "UPDATE question set is_deleted = ? where question_id =? "
  20. _updateQusSQL = "UPDATE question set question_type = ? ,answer_type=? ,question_name = ? , question_bank_id = ? ,difficulty = ? where question_id =? and is_deleted =0 "
  21. _addAnswerSQL = "INSERT INTO question_answer(answer_id ,question_id,answer_content,is_correct ) VALUES(?,?,?,?)"
  22. _multyAddAnswerSQL = "INSERT INTO question_answer(answer_id ,question_id,answer_content,is_correct ) VALUES %s "
  23. _updateAnswerSQL = "UPDATE question_answer set answer_content = ? , is_correct =? , is_deleted =0 where answer_id = ? "
  24. _delAnswerSQL = "UPDATE question_answer set is_deleted = 1 where question_id =? "
  25. _getAnswerListSQL = "select answer_id ,question_id, answer_content,is_correct from question_answer where question_id =? and is_deleted =0 "
  26. _addUserAnswerSQL = "INSERT INTO question_user_answer(uid ,question_id,platform,source,answers,is_correct ) VALUES(?,?,?,?,?,?)"
  27. _checkAnswerSQL = "SELECT count(1) from question_answer where is_correct =1 and question_id = ? and answer_id IN(%s) "
  28. _getRandomPicSQL = "SELECT x,y,src from question_verify_pic where id =? "
  29. _getListPicSQL = "SELECT id from question_verify_pic limit ? ,? "
  30. _getPicCntSQL = "SELECT COUNT(*) FROM question_verify_pic "
  31. )
  32. // GetQusInfo info
  33. func (d *Dao) GetQusInfo(c context.Context, qid int64) (oi *model.Question, err error) {
  34. oi = &model.Question{}
  35. row := d.db.QueryRow(c, _getQusSQL, qid)
  36. if err = row.Scan(&oi.QsID, &oi.QsType, &oi.AnswerType, &oi.QsName, &oi.QsBId, &oi.QsDif, &oi.IsDeleted); err != nil {
  37. if err == sql.ErrNoRows {
  38. oi = nil
  39. err = nil
  40. } else {
  41. log.Error("row.Scan error(%v)", err)
  42. }
  43. }
  44. return
  45. }
  46. // InsertQus add
  47. func (d *Dao) InsertQus(c context.Context, oi *model.Question) (lastID int64, err error) {
  48. res, err := d.db.Exec(c, _addQusSQL, oi.QsID, oi.QsType, oi.AnswerType, oi.QsName, oi.QsBId, oi.QsDif)
  49. if err != nil {
  50. log.Error("[dao.question|GetQusList] d.db.Query err: %v", err)
  51. return
  52. }
  53. lastID, err = res.LastInsertId()
  54. return
  55. }
  56. // GetQusList list
  57. func (d *Dao) GetQusList(c context.Context, offset int, limitnum int, qBid int64) (res []*model.Question, err error) {
  58. res = make([]*model.Question, 0)
  59. _sql := _getQuslistSQL
  60. if qBid > 0 {
  61. _sql = _getQuslistBywhereSQL
  62. }
  63. rows, err := d.db.Query(c, _sql, qBid, offset, limitnum)
  64. if err != nil {
  65. log.Error("[dao.question|GetQusList] d.db.Query err: %v %d,%d", err, offset, limitnum)
  66. return
  67. }
  68. defer rows.Close()
  69. for rows.Next() {
  70. oi := &model.Question{}
  71. if err = rows.Scan(&oi.QsID, &oi.QsType, &oi.AnswerType, &oi.QsName, &oi.QsBId, &oi.QsDif, &oi.IsDeleted); err != nil {
  72. log.Error("[dao.question|GetOrder] rows.Scan err: %v", res)
  73. return
  74. }
  75. res = append(res, oi)
  76. }
  77. return
  78. }
  79. // GetQusIds ids
  80. func (d *Dao) GetQusIds(c context.Context, bankID int64) (ids []int64, err error) {
  81. if ids = d.GetBankQuestionsCache(c, bankID); len(ids) > 1 {
  82. return
  83. }
  84. rows, err := d.db.Query(c, _getAllQusByBankID, bankID)
  85. if err != nil {
  86. log.Error("d.GetQusIds(%d) error(%v)", bankID, err)
  87. return
  88. }
  89. defer rows.Close()
  90. for rows.Next() {
  91. var temp int64
  92. if err = rows.Scan(&temp); err != nil {
  93. log.Error("d.GetQusIds(%d) rows.Scan() error(%v)", bankID, err)
  94. return
  95. }
  96. ids = append(ids, temp)
  97. }
  98. d.SetBankQuestionsCache(c, bankID, ids)
  99. return
  100. }
  101. // DelQus del
  102. func (d *Dao) DelQus(c context.Context, qid int64) (affect int64, err error) {
  103. res, err := d.db.Exec(c, _delQusSQL, 1, qid)
  104. if err != nil {
  105. log.Error("d.DelQus(qbid:%d, dmid:%d) error(%v)", qid, 1, err)
  106. return
  107. }
  108. return res.RowsAffected()
  109. }
  110. // UpdateQus update
  111. func (d *Dao) UpdateQus(c context.Context, update *model.ArgUpdateQus, answers []model.Answer) (affect int64, err error) {
  112. res, err := d.db.Exec(c, _updateQusSQL, update.Type, update.AnType, update.Name, update.BId, update.Dif, update.QsID)
  113. if err != nil {
  114. log.Error("d.UpdateQus(qbid:%d, dmid:%d) error(%v)", update.QsID, update.QsID, err)
  115. return
  116. }
  117. return res.RowsAffected()
  118. }
  119. // GetQusCount cnt
  120. func (d *Dao) GetQusCount(c context.Context, bid int64) (total int64, err error) {
  121. var cntSQL string
  122. if bid == 0 {
  123. cntSQL = _getQusCntSQL
  124. err = d.db.QueryRow(c, cntSQL).Scan(&total)
  125. } else {
  126. cntSQL = _getQusCntSQL + "and question_bank_id = ?"
  127. err = d.db.QueryRow(c, cntSQL, bid).Scan(&total)
  128. }
  129. if err != nil {
  130. log.Error("d.GetQusCount error(%v)", err)
  131. return
  132. }
  133. return
  134. }
  135. // InserAnwser add
  136. func (d *Dao) InserAnwser(c context.Context, answer *model.AnswerAdd) (affect int64, err error) {
  137. res, err := d.db.Exec(c, _addAnswerSQL, answer.AnswerID, answer.QsID, answer.AnswerContent, answer.IsCorrect)
  138. if err != nil {
  139. log.Error("d.InserAnwser() error(%v)", err)
  140. return
  141. }
  142. affect, err = res.LastInsertId()
  143. return
  144. }
  145. // MultiAddAnwser add
  146. func (d *Dao) MultiAddAnwser(c context.Context, answers []*model.AnswerAdd) (err error) {
  147. length := len(answers)
  148. if length == 0 {
  149. return
  150. }
  151. values := strings.Trim(strings.Repeat("(?, ?, ?, ?),", length), ",")
  152. args := make([]interface{}, 0)
  153. for _, ins := range answers {
  154. AnswerID := time.Now().UnixNano() / 1e6
  155. time.Sleep(time.Millisecond)
  156. args = append(args, AnswerID, ins.QsID, ins.AnswerContent, ins.IsCorrect)
  157. }
  158. _, err = d.db.Exec(c, fmt.Sprintf(_multyAddAnswerSQL, values), args...)
  159. if err != nil {
  160. log.Error("d.InserAnwser() error(%v)", err)
  161. return
  162. }
  163. return
  164. }
  165. // UpdateAnwser upd
  166. func (d *Dao) UpdateAnwser(c context.Context, answer *model.AnswerAdd) (affect int64, err error) {
  167. res, err := d.db.Exec(c, _updateAnswerSQL, answer.AnswerContent, answer.IsCorrect, answer.AnswerID)
  168. if err != nil {
  169. log.Error("d.UpdateAnwser() error(%v)", err)
  170. return
  171. }
  172. affect, err = res.RowsAffected()
  173. return
  174. }
  175. // DelAnwser del
  176. func (d *Dao) DelAnwser(c context.Context, qusID int64) (affect int64, err error) {
  177. res, err := d.db.Exec(c, _delAnswerSQL, qusID)
  178. if err != nil {
  179. log.Error("d.UpdateAnwser() error(%v)", err)
  180. return
  181. }
  182. affect, err = res.RowsAffected()
  183. return
  184. }
  185. // GetAnswerList list
  186. func (d *Dao) GetAnswerList(c context.Context, qusID int64) (res []*model.Answer, err error) {
  187. res = make([]*model.Answer, 0)
  188. rows, err := d.db.Query(c, _getAnswerListSQL, qusID)
  189. if err != nil {
  190. log.Error("[dao.GetAnswerList] d.db.Query err: %v", err)
  191. return
  192. }
  193. defer rows.Close()
  194. for rows.Next() {
  195. oi := &model.Answer{}
  196. if err = rows.Scan(&oi.AnswerID, &oi.QsID, &oi.AnswerContent, &oi.IsCorrect); err != nil {
  197. log.Error("[dao.question|GetOrder] rows.Scan err: %v", res)
  198. return
  199. }
  200. res = append(res, oi)
  201. }
  202. return
  203. }
  204. // AddUserAnwser add
  205. func (d *Dao) AddUserAnwser(c context.Context, answer *model.ArgCheckAnswer, isCorrect int8) (affect int64, err error) {
  206. ids := xstr.JoinInts(answer.Answers)
  207. res, err := d.db.Exec(c, _addUserAnswerSQL, answer.UID, answer.QsID, answer.Platform, answer.Source, ids, isCorrect)
  208. if err != nil {
  209. log.Error("d.InserAnwser() error(%v)", err)
  210. return
  211. }
  212. affect, err = res.LastInsertId()
  213. return
  214. }
  215. // CheckAnswer check
  216. func (d *Dao) CheckAnswer(c context.Context, qsid int64, ids []int64) (total int, err error) {
  217. err = d.db.QueryRow(c, fmt.Sprintf(_checkAnswerSQL, xstr.JoinInts(ids)), qsid).Scan(&total)
  218. if err != nil {
  219. log.Error("d.GetQusBankCount error(%v)", err)
  220. return
  221. }
  222. return
  223. }
  224. // GetRandomPic get
  225. func (d *Dao) GetRandomPic(c context.Context, id int) (oi *model.QuestBkPic, err error) {
  226. oi = &model.QuestBkPic{}
  227. row := d.db.QueryRow(c, _getRandomPicSQL, id)
  228. if err = row.Scan(&oi.X, &oi.Y, &oi.Src); err != nil {
  229. if err == sql.ErrNoRows {
  230. oi = nil
  231. err = nil
  232. } else {
  233. log.Error("row.Scan error(%v)", err)
  234. }
  235. }
  236. return
  237. }
  238. // GetAllPicIds ids
  239. func (d *Dao) GetAllPicIds(c context.Context, offset int, limitnum int) (ids []int, err error) {
  240. rows, err := d.db.Query(c, _getListPicSQL, offset, limitnum)
  241. if err != nil {
  242. log.Error("[dao.GetAllPicIds] d.db.Query err: %v %d,%d", err, offset, limitnum)
  243. return
  244. }
  245. defer rows.Close()
  246. for rows.Next() {
  247. var oi int
  248. if err = rows.Scan(&oi); err != nil {
  249. log.Error("[dao.question|GetOrder] rows.Scan err: %v", ids)
  250. return
  251. }
  252. ids = append(ids, oi)
  253. }
  254. return
  255. }
  256. // GetPicCount cnt
  257. func (d *Dao) GetPicCount(c context.Context) (total int, err error) {
  258. err = d.db.QueryRow(c, _getPicCntSQL).Scan(&total)
  259. if err != nil {
  260. log.Error("d.GetQusCount error(%v)", err)
  261. return
  262. }
  263. return
  264. }