credit.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/growup/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. // credit score record
  12. _inCreditRecordSQL = "INSERT INTO credit_score_record (mid,operate_at,operator,reason,deducted,remaining) VALUES(?,?,?,?,?,?)"
  13. _creditRecordsSQL = "SELECT id,mid,operate_at,operator,reason,deducted,remaining,is_deleted FROM credit_score_record WHERE mid=?"
  14. _creditRecordSQL = "SELECT deducted FROM credit_score_record WHERE id=? AND is_deleted=0"
  15. // credit score
  16. _inCreditScoreSQL = "INSERT INTO credit_score (mid) VALUES %s ON DUPLICATE KEY UPDATE mid=VALUES(mid)"
  17. _creditScoreSQL = "SELECT score FROM credit_score WHERE mid=?"
  18. _creditScoresSQL = "SELECT mid,score FROM credit_score WHERE mid IN (%s)"
  19. // update credit score
  20. _upCreditScoreSQL = "UPDATE credit_score SET score=? WHERE mid=?"
  21. _reCreditScoreSQL = "UPDATE credit_score SET score=score+%d WHERE mid=?"
  22. )
  23. // InsertCreditRecord insert credit record
  24. func (d *Dao) InsertCreditRecord(c context.Context, cr *model.CreditRecord) (rows int64, err error) {
  25. res, err := d.rddb.Exec(c, _inCreditRecordSQL, cr.MID, cr.OperateAt, cr.Operator, cr.Reason, cr.Deducted, cr.Remaining)
  26. if err != nil {
  27. log.Error("db.inCreditRecordSQL.Exec(%s) error(%v)", _inCreditRecordSQL, err)
  28. return
  29. }
  30. return res.RowsAffected()
  31. }
  32. // TxInsertCreditRecord tx insert credit record
  33. func (d *Dao) TxInsertCreditRecord(tx *sql.Tx, cr *model.CreditRecord) (rows int64, err error) {
  34. res, err := tx.Exec(_inCreditRecordSQL, cr.MID, cr.OperateAt, cr.Operator, cr.Reason, cr.Deducted, cr.Remaining)
  35. if err != nil {
  36. log.Error("tx.inCreditRecordSQL.Exec(%s) error(%v)", _inCreditRecordSQL, err)
  37. return
  38. }
  39. return res.RowsAffected()
  40. }
  41. // CreditRecords get credit records by mid
  42. func (d *Dao) CreditRecords(c context.Context, mid int64) (crs []*model.CreditRecord, err error) {
  43. rows, err := d.rddb.Query(c, _creditRecordsSQL, mid)
  44. if err != nil {
  45. return
  46. }
  47. defer rows.Close()
  48. for rows.Next() {
  49. cr := &model.CreditRecord{}
  50. err = rows.Scan(&cr.ID, &cr.MID, &cr.OperateAt, &cr.Operator, &cr.Reason, &cr.Deducted, &cr.Remaining, &cr.IsDeleted)
  51. if err != nil {
  52. log.Error("rows Scan error(%v)", err)
  53. return
  54. }
  55. crs = append(crs, cr)
  56. }
  57. return
  58. }
  59. // DeductedScore get deducted credit score from credit_score_record by id
  60. func (d *Dao) DeductedScore(c context.Context, id int64) (deducted int, err error) {
  61. row := d.rddb.QueryRow(c, _creditRecordSQL, id)
  62. if err = row.Scan(&deducted); err != nil {
  63. if err == sql.ErrNoRows {
  64. err = nil
  65. } else {
  66. log.Error("row.Scan error(%v)", err)
  67. }
  68. }
  69. return
  70. }
  71. // InsertCreditScore insert credit score
  72. func (d *Dao) InsertCreditScore(c context.Context, values string) (rows int64, err error) {
  73. res, err := d.rddb.Exec(c, fmt.Sprintf(_inCreditScoreSQL, values))
  74. if err != nil {
  75. log.Error("db.inCreditScoreSQL.Exec(%s) error(%v)", _inCreditScoreSQL, err)
  76. return
  77. }
  78. return res.RowsAffected()
  79. }
  80. // UpdateCreditScore update credit score
  81. func (d *Dao) UpdateCreditScore(c context.Context, mid int64, score int) (rows int64, err error) {
  82. res, err := d.rddb.Exec(c, _upCreditScoreSQL, score, mid)
  83. if err != nil {
  84. log.Error("db.upCreditScoreSQL.Exec(%s) error(%v)", _inCreditScoreSQL, err)
  85. return
  86. }
  87. return res.RowsAffected()
  88. }
  89. // TxUpdateCreditScore update credit score
  90. func (d *Dao) TxUpdateCreditScore(tx *sql.Tx, mid int64, score int) (rows int64, err error) {
  91. res, err := tx.Exec(_upCreditScoreSQL, score, mid)
  92. if err != nil {
  93. log.Error("tx.upCreditScoreSQL.Exec(%s) error(%v)", _inCreditScoreSQL, err)
  94. return
  95. }
  96. return res.RowsAffected()
  97. }
  98. // CreditScore credit score by mid
  99. func (d *Dao) CreditScore(c context.Context, mid int64) (score int, err error) {
  100. row := d.rddb.QueryRow(c, _creditScoreSQL, mid)
  101. if err = row.Scan(&score); err != nil {
  102. if err == sql.ErrNoRows {
  103. err = nil
  104. } else {
  105. log.Error("row.Scan error(%v)", err)
  106. }
  107. }
  108. return
  109. }
  110. // CreditScores scores map[mid]score
  111. func (d *Dao) CreditScores(c context.Context, mids []int64) (scores map[int64]int, err error) {
  112. scores = make(map[int64]int)
  113. rows, err := d.rddb.Query(c, fmt.Sprintf(_creditScoresSQL, xstr.JoinInts(mids)))
  114. if err != nil {
  115. return
  116. }
  117. defer rows.Close()
  118. for rows.Next() {
  119. var mid int64
  120. var score int
  121. err = rows.Scan(&mid, &score)
  122. if err != nil {
  123. log.Error("rows Scan error(%v)", err)
  124. return
  125. }
  126. scores[mid] = score
  127. }
  128. return
  129. }
  130. // TxRecoverCreditScore recover credit score
  131. func (d *Dao) TxRecoverCreditScore(tx *sql.Tx, deducted int, mid int64) (rows int64, err error) {
  132. res, err := tx.Exec(fmt.Sprintf(_reCreditScoreSQL, deducted), mid)
  133. if err != nil {
  134. return
  135. }
  136. return res.RowsAffected()
  137. }