question_bank.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "strconv"
  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. _QusBankInfo = "SELECT qb_id ,qb_name,cd_time,max_retry_time,is_deleted FROM question_bank where qb_id= ? and is_deleted= 0 "
  14. _addQusBank = "insert into question_bank(qb_id ,qb_name,cd_time,max_retry_time,is_deleted) values(?,?,?,?,?)"
  15. _qusBanklist = "SELECT qb_id ,qb_name,cd_time,max_retry_time,is_deleted FROM question_bank where is_deleted= 0 and total_cnt < ? and qb_id IN(%s) order by id desc "
  16. _qusBankcnt = "SELECT COUNT(*) FROM question_bank where is_deleted= 0 "
  17. _delQusBank = "update question_bank set is_deleted = ? where qb_id =? "
  18. _searchQusBank = "SELECT qb_id ,qb_name,cd_time,max_retry_time,is_deleted FROM question_bank where is_deleted= 0 and qb_name LIKE ? "
  19. _updateQusBank = "update question_bank set qb_name = ? ,max_retry_time=? ,cd_time = ? where qb_id =? "
  20. _qusBanklistByids = "SELECT qb_id, qb_name, cd_time, max_retry_time, is_deleted FROM question_bank WHERE is_deleted = 0 and qb_id IN(%s)"
  21. _getStaticQusBankList = "SELECT id, qb_id,qb_name,cd_time,max_retry_time,is_deleted,total_cnt ,easy_cnt,normal_cnt,hard_cnt from question_bank where is_deleted =0 "
  22. _getQbID = "SELECT qb_id FROM question_bank where id= ? and is_deleted= 0"
  23. _getQusBankByID = "select a.qb_id,a.qb_name,a.cd_time,a.max_retry_time , a.is_deleted, count(if(b.is_deleted = 0,true,null)) total " +
  24. "from question_bank a left join question b on a.qb_id = b.question_bank_id WHERE a.qb_id IN(%s) GROUP BY a.qb_id HAVING a.is_deleted = 0 "
  25. _getQusBankCnt = "SELECT sum(case when difficulty = 1 then 1 else 0 end ) 'easy', sum(case when difficulty = 2 then 1 else 0 end ) 'normal'," +
  26. "sum(case when difficulty = 3 then 1 else 0 end ) 'hard',count(id) 'total' from question WHERE is_deleted =0 and `question_bank_id`= ?"
  27. _updateQusBankCnt = "update question_bank set easy_cnt = ? ,normal_cnt=? ,hard_cnt = ?,total_cnt = ? where qb_id =? "
  28. )
  29. // GetQusBankInfo info
  30. func (d *Dao) GetQusBankInfo(c context.Context, qbid int64) (oi *model.QuestionBank, err error) {
  31. oi = &model.QuestionBank{}
  32. row := d.db.QueryRow(c, _QusBankInfo, qbid)
  33. if err = row.Scan(&oi.QsBId, &oi.QBName, &oi.CdTime, &oi.MaxRetryTime, &oi.IsDeleted); err != nil {
  34. if err == sql.ErrNoRows {
  35. oi = nil
  36. err = nil
  37. } else {
  38. log.Error("row.Scan error(%v)", err)
  39. }
  40. }
  41. return
  42. }
  43. // InsertQusBank add
  44. func (d *Dao) InsertQusBank(c context.Context, oi *model.QuestionBank) (lastID int64, err error) {
  45. res, err := d.db.Exec(c, _addQusBank, oi.QsBId, oi.QBName, oi.CdTime, oi.MaxRetryTime, oi.IsDeleted)
  46. if err != nil {
  47. log.Error("[dao.question|GetQusBankList] d.db.Query err: %v", err)
  48. return
  49. }
  50. lastID, err = res.LastInsertId()
  51. return
  52. }
  53. // StatisticsQusBank 统计bank
  54. func (d *Dao) StatisticsQusBank(c context.Context, offset int, limitnum int, name string) (res []*model.QusBankSt, err error) {
  55. res = make([]*model.QusBankSt, 0)
  56. var rows *sql.Rows
  57. var _sql string
  58. if name == "" {
  59. _sql = _getStaticQusBankList + " order by id desc limit ? ,? "
  60. rows, err = d.db.Query(c, _sql, offset, limitnum)
  61. } else {
  62. _sql = _getStaticQusBankList + " and qb_name LIKE ? order by id desc limit ? ,? "
  63. name = "%" + name + "%"
  64. rows, err = d.db.Query(c, _sql, name, offset, limitnum)
  65. }
  66. if err != nil {
  67. log.Error("[dao.question|GetQusBankList] d.db.Query err: %v %d,%d", err, offset, limitnum)
  68. return
  69. }
  70. defer rows.Close()
  71. for rows.Next() {
  72. oi := &model.QusBankSt{}
  73. if err = rows.Scan(&oi.ID, &oi.QsBId, &oi.QBName, &oi.CdTime, &oi.MaxRetryTime, &oi.IsDeleted, &oi.TotalCnt, &oi.EasyCnt, &oi.NormalCnt, &oi.HardCnt); err != nil {
  74. log.Error("[dao.question|GetOrder] rows.Scan err: %v", res)
  75. return
  76. }
  77. res = append(res, oi)
  78. }
  79. return
  80. }
  81. // GetQusBankList list
  82. func (d *Dao) GetQusBankList(c context.Context, cnt int, ids []int64) (res []*model.QuestionBank, err error) {
  83. res = make([]*model.QuestionBank, 0)
  84. rows, err := d.db.Query(c, fmt.Sprintf(_qusBanklist, xstr.JoinInts([]int64(ids))), cnt)
  85. if err != nil {
  86. log.Error("[dao.question|GetQusBankList] d.db.Query err: %v %d,%d", err)
  87. return
  88. }
  89. defer rows.Close()
  90. for rows.Next() {
  91. oi := &model.QuestionBank{}
  92. if err = rows.Scan(&oi.QsBId, &oi.QBName, &oi.CdTime, &oi.MaxRetryTime, &oi.IsDeleted); err != nil {
  93. log.Error("[dao.question|GetOrder] rows.Scan err: %v", res)
  94. return
  95. }
  96. res = append(res, oi)
  97. }
  98. return
  99. }
  100. // DelQusBank del
  101. func (d *Dao) DelQusBank(c context.Context, qbID int64, status int8) (affect int64, err error) {
  102. res, err := d.db.Exec(c, _delQusBank, status, qbID)
  103. if err != nil {
  104. log.Error("d.UpdateQusBank(qbid:%d, dmid:%d) error(%v)", qbID, status, err)
  105. return
  106. }
  107. s, e := res.RowsAffected()
  108. log.Error("d.UpdateQusBank(qbxxxxxid:%d, dxxxxxmid:%d) error(%v)", qbID, s, e)
  109. return res.RowsAffected()
  110. }
  111. // UpdateQusBank update
  112. func (d *Dao) UpdateQusBank(c context.Context, qbID int64, name string, trytime int64, cdtime int64) (affect int64, err error) {
  113. res, err := d.db.Exec(c, _updateQusBank, name, trytime, cdtime, qbID)
  114. if err != nil {
  115. log.Error("d.UpdateQusBank(qbid:%d, dmid:%d) error(%v)", qbID, trytime, err)
  116. return
  117. }
  118. return res.RowsAffected()
  119. }
  120. // GetQusBankCount cnt
  121. func (d *Dao) GetQusBankCount(c context.Context, name string) (total int64, err error) {
  122. var cntSQL string
  123. if name == "" {
  124. cntSQL = _qusBankcnt
  125. err = d.db.QueryRow(c, cntSQL).Scan(&total)
  126. } else {
  127. cntSQL = _qusBankcnt + " and qb_name LIKE ? "
  128. name = "%" + name + "%"
  129. err = d.db.QueryRow(c, cntSQL, name).Scan(&total)
  130. }
  131. if err != nil {
  132. log.Error("d.GetQusBankCount error(%v)", err)
  133. return
  134. }
  135. return
  136. }
  137. // BankSearch search
  138. func (d *Dao) BankSearch(c context.Context, name string) (res []*model.QuestionBank, err error) {
  139. res = make([]*model.QuestionBank, 0)
  140. rows, err := d.db.Query(c, _searchQusBank, "%"+name+"%")
  141. if err != nil {
  142. log.Error("[dao.question|BankSearch] d.db.Query err: %v", err)
  143. return
  144. }
  145. defer rows.Close()
  146. for rows.Next() {
  147. oi := &model.QuestionBank{}
  148. if err = rows.Scan(&oi.QsBId, &oi.QBName, &oi.CdTime, &oi.MaxRetryTime, &oi.IsDeleted); err != nil {
  149. log.Error("[dao.question|GetOrder] rows.Scan err: %v", res)
  150. return
  151. }
  152. res = append(res, oi)
  153. }
  154. return
  155. }
  156. // GetQusBankListByIds ids
  157. func (d *Dao) GetQusBankListByIds(c context.Context, ids []int64) (res []*model.QuestionBank, err error) {
  158. res = make([]*model.QuestionBank, 0)
  159. rows, err := d.db.Query(c, fmt.Sprintf(_qusBanklistByids, xstr.JoinInts([]int64(ids))))
  160. if err != nil {
  161. log.Error("[dao.question|GetQusBankListByIds] d.db.Query err: %v", err)
  162. return
  163. }
  164. defer rows.Close()
  165. for rows.Next() {
  166. oi := &model.QuestionBank{}
  167. if err = rows.Scan(&oi.QsBId, &oi.QBName, &oi.CdTime, &oi.MaxRetryTime, &oi.IsDeleted); err != nil {
  168. log.Error("[dao.question|GetOrder] rows.Scan err: %v", res)
  169. return
  170. }
  171. res = append(res, oi)
  172. }
  173. return
  174. }
  175. // GetQBId id
  176. func (d *Dao) GetQBId(c context.Context, id int64) (qbID int64, err error) {
  177. err = d.db.QueryRow(c, _getQbID, id).Scan(&qbID)
  178. if err != nil {
  179. log.Error("d.GetQBId error(%v)", err.Error())
  180. return
  181. }
  182. return
  183. }
  184. // GetBankInfoByQBid byids
  185. func (d *Dao) GetBankInfoByQBid(c context.Context, qbID map[int64]int64) (res []*model.QusBankSt, err error) {
  186. rows, err := d.db.Query(c, fmt.Sprintf(_getQusBankByID, d.CoverStr(qbID)))
  187. fmt.Println(fmt.Sprintf(_getQusBankByID, d.CoverStr(qbID)))
  188. if err != nil {
  189. log.Error(fmt.Sprintf("d.mysql.Query(%s) error(%+v)", _getQusBankByID, err.Error()))
  190. return
  191. }
  192. defer rows.Close()
  193. for rows.Next() {
  194. oi := &model.QusBankSt{}
  195. if err = rows.Scan(&oi.QsBId, &oi.QBName, &oi.CdTime, &oi.MaxRetryTime, &oi.IsDeleted, &oi.TotalCnt); err != nil {
  196. log.Error(fmt.Sprintf("d.mysql.Query(%s) error(%+v)", _getQusBankByID, err.Error()))
  197. return
  198. }
  199. res = append(res, oi)
  200. }
  201. return
  202. }
  203. // CoverStr str
  204. func (d *Dao) CoverStr(strs map[int64]int64) string {
  205. var buf = bytes.NewBuffer(nil)
  206. for _, str := range strs {
  207. buf.WriteString("'")
  208. buf.WriteString(strconv.FormatInt(str, 10))
  209. buf.WriteString("'")
  210. buf.WriteString(",")
  211. }
  212. buf.Truncate(buf.Len() - 1)
  213. return buf.String()
  214. }
  215. // UpdateQsBankCnt 更新数量
  216. func (d *Dao) UpdateQsBankCnt(c context.Context, qid int64) (effid int64, err error) {
  217. oi := &model.QusBankCnt{}
  218. err = d.db.QueryRow(c, _getQusBankCnt, qid).Scan(&oi.EasyCnt, &oi.NormalCnt, &oi.HardCnt, &oi.TotalCnt)
  219. if err != nil {
  220. log.Error("d.UpdateQsBankCnt error(%v)", err.Error())
  221. return
  222. }
  223. res, err := d.db.Exec(c, _updateQusBankCnt, &oi.EasyCnt, &oi.NormalCnt, &oi.HardCnt, &oi.TotalCnt, qid)
  224. if err != nil {
  225. log.Error("d._updateQusBankCnt(qbid:%d, dmid:%d) error(%v)", err)
  226. return
  227. }
  228. return res.RowsAffected()
  229. }