jury.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. model "go-common/app/interface/main/credit/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _updateJurySQL = "INSERT INTO blocked_jury(mid,status,expired) VALUES(?,?,?) ON DUPLICATE KEY UPDATE status=?,expired=?"
  14. _updateVoteTotalSQL = "UPDATE blocked_jury SET vote_total=vote_total+1 WHERE mid=?"
  15. _getJurySQL = "SELECT id,mid,status,expired,invalid_reason,vote_total,vote_radio,vote_right,vote_total,black FROM blocked_jury WHERE mid=?"
  16. _juryInfosSQL = "SELECT mid,status,expired,invalid_reason,vote_total,vote_radio,vote_right,total,black FROM blocked_jury WHERE mid IN (%s)"
  17. )
  18. //JuryApply user jury apply.
  19. func (d *Dao) JuryApply(c context.Context, mid int64, expired time.Time) (err error) {
  20. if _, err = d.db.Exec(c, _updateJurySQL, mid, 1, expired, 1, expired); err != nil {
  21. log.Error("JuryApply: db.Exec(%d,%d,%v) error(%v)", mid, 1, expired, err)
  22. }
  23. return
  24. }
  25. // AddUserVoteTotal add user vote total.
  26. func (d *Dao) AddUserVoteTotal(c context.Context, mid int64) (err error) {
  27. if _, err = d.db.Exec(c, _updateVoteTotalSQL, mid); err != nil {
  28. log.Error("AddUserVoteTotal: db.Exec(%d) error(%v)", mid, err)
  29. }
  30. return
  31. }
  32. // JuryInfo get user's jury info
  33. func (d *Dao) JuryInfo(c context.Context, mid int64) (r *model.BlockedJury, err error) {
  34. row := d.db.QueryRow(c, _getJurySQL, mid)
  35. r = &model.BlockedJury{}
  36. if err = row.Scan(&r.ID, &r.MID, &r.Status, &r.Expired, &r.InvalidReason, &r.VoteTotal, &r.VoteRadio, &r.VoteRight, &r.CaseTotal, &r.Black); err != nil {
  37. if err == sql.ErrNoRows {
  38. err = nil
  39. r = nil
  40. }
  41. }
  42. return
  43. }
  44. // JuryInfos get user's applying jury info
  45. func (d *Dao) JuryInfos(c context.Context, mids []int64) (mbj map[int64]*model.BlockedJury, err error) {
  46. rows, err := d.db.Query(c, fmt.Sprintf(_juryInfosSQL, xstr.JoinInts(mids)))
  47. if err != nil {
  48. err = errors.Wrap(err, "JuryInfos")
  49. return
  50. }
  51. mbj = make(map[int64]*model.BlockedJury, len(mids))
  52. defer rows.Close()
  53. for rows.Next() {
  54. bj := new(model.BlockedJury)
  55. if err = rows.Scan(&bj.MID, &bj.Status, &bj.Expired, &bj.InvalidReason, &bj.VoteTotal, &bj.VoteRadio, &bj.VoteRight, &bj.CaseTotal, &bj.Black); err != nil {
  56. if err == sql.ErrNoRows {
  57. err = nil
  58. return
  59. }
  60. err = errors.Wrap(err, "JuryInfos")
  61. return
  62. }
  63. mbj[bj.MID] = bj
  64. }
  65. err = rows.Err()
  66. return
  67. }