question_bank_bind.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/app/service/openplatform/anti-fraud/model"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. _addQuestionBankBind = "INSERT INTO question_bank_bind (target_item, target_item_type, bank_id, use_in_time, source, is_deleted) VALUES %s"
  13. _getQuestionBankBindWithDeletedNew = " SELECT a.target_item,a.target_item_type,a.bank_id,a.use_in_time,b.id,b.qb_name" +
  14. " FROM question_bank_bind as a join question_bank as b" +
  15. " ON a.bank_id = b.qb_id" +
  16. " where a.target_item IN(%s) and a.target_item_type = ? and a.source = ?"
  17. _getQuestionBankBind = _getQuestionBankBindWithDeletedNew + " AND a.is_deleted = 0"
  18. _updateQuestionBankBind = "UPDATE question_bank_bind set bank_id = ?, use_in_time = ?, is_deleted = 0" +
  19. " WHERE source = ?" +
  20. " AND target_item_type = ?" +
  21. " AND target_item IN(%s)"
  22. _deleteQuestionBankBind = "UPDATE question_bank_bind SET is_deleted = 1 WHERE target_item IN(%s)" +
  23. " AND target_item_type = ?" +
  24. " AND source = ?"
  25. _getBindItem = "SELECT id, target_item, target_item_type, bank_id, use_in_time, source, is_deleted, ctime, mtime FROM question_bank_bind WHERE bank_id = ? AND is_deleted = 0 limit ?,?"
  26. _countBindItem = "SELECT COUNT(1) as count FROM question_bank_bind WHERE bank_id = ? AND is_deleted = 0"
  27. )
  28. // AddBankBind add
  29. func (d *Dao) AddBankBind(c context.Context, update []model.ArgQuestionBankBind, insert []model.ArgQuestionBankBind) (err error) {
  30. lenUpdate := len(update)
  31. lenInsert := len(insert)
  32. if lenUpdate == 0 && lenInsert == 0 {
  33. return
  34. }
  35. updateItems := map[int64]*model.ArgQuestionBankBindToDb{}
  36. insertItems := map[int64]*model.ArgQuestionBankBindToDb{}
  37. for _, v := range update {
  38. if _, ok := updateItems[v.QsBId]; !ok {
  39. updateItems[v.QsBId] = &model.ArgQuestionBankBindToDb{
  40. QsBId: v.QsBId,
  41. Source: v.Source,
  42. TargetItemType: v.TargetItemType,
  43. UseInTime: v.UseInTime,
  44. TargetItems: []string{v.TargetItems},
  45. }
  46. } else {
  47. updateItems[v.QsBId].TargetItems = append(updateItems[v.QsBId].TargetItems, v.TargetItems)
  48. }
  49. d.DelTargetItemBindCache(c, v.TargetItems)
  50. }
  51. for _, v := range insert {
  52. if _, ok := insertItems[v.QsBId]; !ok {
  53. insertItems[v.QsBId] = &model.ArgQuestionBankBindToDb{
  54. QsBId: v.QsBId,
  55. Source: v.Source,
  56. TargetItemType: v.TargetItemType,
  57. UseInTime: v.UseInTime,
  58. TargetItems: []string{v.TargetItems},
  59. }
  60. } else {
  61. insertItems[v.QsBId].TargetItems = append(insertItems[v.QsBId].TargetItems, v.TargetItems)
  62. }
  63. }
  64. tx, err := d.db.Begin(c)
  65. if err != nil {
  66. log.Error("d.AddBankBind(%v) error(%v)", err)
  67. return
  68. }
  69. defer func() {
  70. if err != nil {
  71. if err = tx.Rollback(); err != nil {
  72. log.Error("tx.Rollback() error(%v)", err)
  73. }
  74. return
  75. }
  76. if err = tx.Commit(); err != nil {
  77. log.Error("tx.Commit() error(%v)", err)
  78. return
  79. }
  80. }()
  81. if len(updateItems) > 0 {
  82. for _, v := range updateItems {
  83. sql := fmt.Sprintf(_updateQuestionBankBind, `"`+strings.Join(v.TargetItems, `","`)+`"`)
  84. _, err = tx.Exec(sql, v.QsBId, v.UseInTime, v.Source, v.TargetItemType)
  85. if err != nil {
  86. log.Error("d.AddBankBind(%v, %v) error(%v)", update, insert, err)
  87. return
  88. }
  89. }
  90. }
  91. if len(insertItems) > 0 {
  92. for _, v := range insertItems {
  93. lenInsert = len(v.TargetItems)
  94. if lenInsert > 0 {
  95. placeholder := strings.Trim(strings.Repeat("(?, ?, ?, ?, ?, ?),", lenInsert), ",")
  96. values := make([]interface{}, 0)
  97. for _, ins := range v.TargetItems {
  98. values = append(values, ins, v.TargetItemType, v.QsBId, v.UseInTime, v.Source, 0)
  99. }
  100. _, err = tx.Exec(fmt.Sprintf(_addQuestionBankBind, placeholder), values...)
  101. if err != nil {
  102. log.Error("d.AddBankBind() tx.Exec(%s) error(%v)", fmt.Sprintf(_addQuestionBankBind, values), err)
  103. return
  104. }
  105. }
  106. }
  107. }
  108. return
  109. }
  110. // GetBankBind 查询绑定关系
  111. func (d *Dao) GetBankBind(c context.Context, source, targetItemType int8, targetItem []string, withDeleted bool) (list []*model.QuestionBankBind, err error) {
  112. list = make([]*model.QuestionBankBind, 0)
  113. if len(targetItem) == 0 {
  114. return
  115. }
  116. sql := _getQuestionBankBind
  117. if withDeleted {
  118. sql = _getQuestionBankBindWithDeletedNew
  119. }
  120. sql = fmt.Sprintf(sql, `"`+strings.Join(targetItem, `","`)+`"`)
  121. rows, err := d.db.Query(c, sql, targetItemType, source)
  122. if err != nil {
  123. log.Error("d.GetBankBind(%v, %v, %v) db.Query() error(%v)", source, targetItemType, targetItem, err)
  124. return
  125. }
  126. defer rows.Close()
  127. for rows.Next() {
  128. tmp := &model.QuestionBankBind{QuestionBank: new(model.QuestionBank)}
  129. err = rows.Scan(&tmp.TargetItem, &tmp.TargetItemType, &tmp.QsBId, &tmp.UseInTime, &tmp.ID, &tmp.QuestionBank.QBName)
  130. if err != nil {
  131. return
  132. }
  133. list = append(list, tmp)
  134. }
  135. return
  136. }
  137. // GetBindBank 获取已绑定的题库信息
  138. func (d *Dao) GetBindBank(c context.Context, source, targetItemType int8, targetItem []string) (binds []*model.QuestionBankBind, err error) {
  139. // list = make([]*model.QuestionBankBind, 0)
  140. binds, err = d.GetBankBind(c, source, targetItemType, targetItem, false)
  141. if len(binds) == 0 {
  142. return
  143. }
  144. var bankIds []int64
  145. for _, bind := range binds {
  146. bankIds = append(bankIds, bind.QsBId)
  147. }
  148. banks, err := d.GetQusBankListByIds(c, bankIds)
  149. if err != nil {
  150. return
  151. }
  152. for _, bind := range binds {
  153. for _, bank := range banks {
  154. if bind.QsBId == bank.QsBId {
  155. bind.QuestionBank = bank
  156. break
  157. }
  158. }
  159. }
  160. return
  161. }
  162. // CountBindItem cnt
  163. func (d *Dao) CountBindItem(c context.Context, bankID int64) (count int64, err error) {
  164. if err = d.db.QueryRow(c, _countBindItem, bankID).Scan(&count); err != nil {
  165. log.Error("d.CountBindItem(%d) error(%v)", bankID, err)
  166. }
  167. return
  168. }
  169. // QuestionBankUnbind 解绑
  170. func (d *Dao) QuestionBankUnbind(c context.Context, delIds []int64, targetType int8, source int8) (err error) {
  171. if len(delIds) > 0 {
  172. sql := fmt.Sprintf(_deleteQuestionBankBind, xstr.JoinInts(delIds))
  173. _, err = d.db.Exec(c, sql, targetType, source)
  174. if err != nil {
  175. log.Error("d.QuestionBankUnbind() tx.Exec(%s) error(%v)", fmt.Sprintf(_deleteQuestionBankBind, xstr.JoinInts(delIds)), err)
  176. return
  177. }
  178. }
  179. for _, delID := range delIds {
  180. d.DelTargetItemBindCache(c, strconv.FormatInt(delID, 10))
  181. }
  182. return
  183. }
  184. // GetBindItem itm
  185. func (d *Dao) GetBindItem(c context.Context, bankID int64, page, pageSize int) (list []*model.QuestionBankBind, total int64, err error) {
  186. list = make([]*model.QuestionBankBind, 0)
  187. rows, err := d.db.Query(c, _getBindItem, bankID, (page-1)*pageSize, pageSize)
  188. if err != nil {
  189. log.Error("d.GetBindItem(%d) error(%v)", bankID, err)
  190. return
  191. }
  192. defer rows.Close()
  193. for rows.Next() {
  194. tmp := &model.QuestionBankBind{}
  195. err = rows.Scan(&tmp.ID, &tmp.TargetItem, &tmp.TargetItemType, &tmp.QsBId, &tmp.UseInTime, &tmp.Source, &tmp.IsDeleted, &tmp.Ctime, &tmp.Mtime)
  196. if err != nil {
  197. return
  198. }
  199. list = append(list, tmp)
  200. }
  201. total, err = d.CountBindItem(c, bankID)
  202. if err != nil {
  203. log.Error("d.GetBindItem(%d, %d, %d) error(%v)", bankID, page, pageSize, err)
  204. return
  205. }
  206. return
  207. }