member.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/job/main/member/model"
  7. smodel "go-common/app/service/main/member/model"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _shard = 100
  12. _SelBaseInfo = "SELECT mid,name,sex,face,sign,rank,birthday,ctime,mtime FROM user_base_%02d WHERE mid=?"
  13. _SetOfficial = "INSERT INTO user_official (mid,role,title,description) values (?,?,?,?) ON DUPLICATE KEY UPDATE role=VALUES(role),title=VALUES(title),description=VALUES(description)"
  14. _SetBaseInfo = "INSERT INTO user_base_%02d(mid,name,sex,face,sign,rank) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE name=?,sex=?,face=?,sign=?,rank=?"
  15. _SetName = "INSERT INTO user_base_%02d(mid,name) VALUES (?,?) ON DUPLICATE KEY UPDATE name=?"
  16. _SetSign = "INSERT INTO user_base_%02d(mid,sign) VALUES (?,?) ON DUPLICATE KEY UPDATE sign=?"
  17. _InitBase = "INSERT IGNORE INTO user_base_%02d(mid) VALUES (?)"
  18. _SetFace = "UPDATE user_base_%02d SET face=? WHERE mid=?"
  19. _SelMoral = "SELECT mid,moral,added,deducted,last_recover_date from user_moral where mid = ?"
  20. _recoverMoral = `update user_moral set moral=moral+?,added=added+?, last_recover_date=? where mid = ? and last_recover_date != ? and moral < 7000 `
  21. _auditQueuingFace = `UPDATE user_property_review SET state=?, operator='system', remark=? WHERE property=1 AND state=10 AND is_monitor=0 AND new=?`
  22. )
  23. // hit get table suffix
  24. func hit(id int64) int64 {
  25. return id % _shard
  26. }
  27. // BaseInfo info of user.
  28. //用于检查数据,不需要拼url
  29. func (d *Dao) BaseInfo(c context.Context, mid int64) (r *model.BaseInfo, err error) {
  30. r = &model.BaseInfo{}
  31. row := d.db.QueryRow(c, fmt.Sprintf(_SelBaseInfo, hit(mid)), mid)
  32. if err = row.Scan(&r.Mid, &r.Name, &r.Sex, &r.Face, &r.Sign, &r.Rank, &r.Birthday, &r.CTime, &r.MTime); err != nil {
  33. r = nil
  34. if err == sql.ErrNoRows {
  35. err = nil
  36. return
  37. }
  38. log.Error("row.Scan() error(%v) mid(%v)", err, mid)
  39. }
  40. return
  41. }
  42. // SelMoral select moral by mid from db
  43. func (d *Dao) SelMoral(c context.Context, mid int64) (moral *smodel.Moral, err error) {
  44. moral = &smodel.Moral{}
  45. row := d.db.QueryRow(c, _SelMoral, mid)
  46. if err = row.Scan(&moral.Mid, &moral.Moral, &moral.Added, &moral.Deducted, &moral.LastRecoverDate); err != nil {
  47. if err == sql.ErrNoRows {
  48. moral = nil
  49. err = nil
  50. return
  51. }
  52. log.Error(" SelMoral row.Scan() error(%v) mid(%v)", err, mid)
  53. }
  54. return
  55. }
  56. // RecoverMoral set moral by mid from db
  57. func (d *Dao) RecoverMoral(c context.Context, mid, delta, added int64, lastRecoverDate string) (rowsAffected int64, err error) {
  58. var res sql.Result
  59. if res, err = d.db.Exec(c, _recoverMoral, delta, added, lastRecoverDate, mid, lastRecoverDate); err != nil {
  60. log.Error(" RecoverMoral d.db.Exec() error(%v) mid(%v)", err, mid)
  61. return
  62. }
  63. return res.RowsAffected()
  64. }
  65. // SetSign set sign.
  66. func (d *Dao) SetSign(c context.Context, mid int64, sign string) (err error) {
  67. if _, err = d.db.Exec(c, fmt.Sprintf(_SetSign, hit(mid)), mid, sign, sign); err != nil {
  68. log.Error("setSign: tx.Exec(%d, %v) error(%v)", mid, sign, err)
  69. return
  70. }
  71. return
  72. }
  73. // SetOfficial set official.
  74. func (d *Dao) SetOfficial(c context.Context, mid int64, role int8, title string, desc string) error {
  75. if _, err := d.db.Exec(c, _SetOfficial, mid, role, title, desc); err != nil {
  76. log.Error("_SetOfficial: tx.Exec(%d,%d,%s,%s) error(%v)", mid, role, title, desc, err)
  77. return err
  78. }
  79. return nil
  80. }
  81. // SetName set name.
  82. func (d *Dao) SetName(c context.Context, mid int64, name string) (err error) {
  83. if len(name) <= 0 {
  84. log.Error("SetName: mid(%d) len(name)<=0 ", mid)
  85. return
  86. }
  87. if _, err = d.db.Exec(c, fmt.Sprintf(_SetName, hit(mid)), mid, name, name); err != nil {
  88. log.Error("setName: tx.Exec(%d, %v) error(%v)", mid, name, err)
  89. return
  90. }
  91. return
  92. }
  93. // SetBaseInfo set base info of user.
  94. func (d *Dao) SetBaseInfo(c context.Context, r *model.BaseInfo) (err error) {
  95. if len(r.Name) <= 0 {
  96. log.Error("SetBaseInfo: mid(%d) len(r.Name)<=0 BaseInfo(%v)", r.Mid, r)
  97. return
  98. }
  99. if _, err = d.db.Exec(c, fmt.Sprintf(_SetBaseInfo, hit(r.Mid)), r.Mid, r.Name, r.Sex, r.Face, r.Sign, r.Rank, r.Name, r.Sex, r.Face, r.Sign, r.Rank); err != nil {
  100. log.Error("SetBaseInfo: db.Exec(%d, %v) error(%v)", r.Mid, r, err)
  101. }
  102. return
  103. }
  104. // SetFace set face.
  105. func (d *Dao) SetFace(c context.Context, mid int64, face string) (err error) {
  106. if _, err = d.db.Exec(c, fmt.Sprintf(_SetFace, hit(mid)), face, mid); err != nil {
  107. log.Error("SetFace: tx.Exec(%v,%v) error(%v)", face, mid, err)
  108. }
  109. return
  110. }
  111. // InitBase init base info.
  112. func (d *Dao) InitBase(c context.Context, mid int64) (err error) {
  113. if _, err = d.db.Exec(c, fmt.Sprintf(_InitBase, hit(mid)), mid); err != nil {
  114. log.Error("InitBase: tx.Exec(%d) error(%v)", mid, err)
  115. }
  116. return
  117. }
  118. // AuditQueuingFace auditQueuingFace.
  119. func (d *Dao) AuditQueuingFace(c context.Context, face string, remark string, state int8) error {
  120. _, err := d.db.Exec(c, _auditQueuingFace, state, remark, face)
  121. if err != nil {
  122. log.Error("Failed to audit queuing face: %s, remark: %s, state: %d: %+v", face, remark, state, err)
  123. return err
  124. }
  125. return nil
  126. }