mysql.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package guard
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/xstr"
  6. "time"
  7. confm "go-common/app/service/live/xuser/conf"
  8. "go-common/app/service/live/xuser/model"
  9. dhhm "go-common/app/service/live/xuser/model/dhh"
  10. "go-common/library/log"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _guardTable = "ap_user_privilege"
  15. )
  16. var (
  17. // add guard info
  18. _addGuardInfo = "REPLACE INTO `%s` (`uid`,`target_id`,`privilege_type`,`start_time`,`expired_time`) VALUES(?,?,?,?,?);"
  19. // get guard info
  20. _getGuardInfo = "SELECT `id`,`uid`,`target_id`,`privilege_type`,`start_time`,`expired_time` FROM `%s` WHERE `uid`=? AND `expired_time`>=? ORDER BY `privilege_type` ASC;"
  21. // get guard info
  22. _getGuardInfo2 = "SELECT `id`,`uid`,`target_id`,`privilege_type`,`start_time`,`expired_time` FROM `%s` WHERE `uid`=? AND `target_id`=? AND `expired_time`>=? ORDER BY `privilege_type` ASC;"
  23. // update guard info
  24. _updGuardInfo = "UPDATE `%s` SET `expired_time`=date_add(expired_time, interval ? day) WHERE `uid`=? AND `target_id`=? AND `expired_time`>=? AND `privilege_type`%s?"
  25. // upsert guard info
  26. _upsertGuardInfo = "INSERT INTO `%s` (`uid`,`target_id`,`privilege_type`,`start_time`,`expired_time`) VALUES(?,?,?,?,?) ON DUPLICATE KEY UPDATE `start_time`=?,`expired_time`=?;"
  27. // 查询大航海信息
  28. _selUID = "SELECT id,uid,target_id,privilege_type,start_time,expired_time,ctime,utime FROM ap_user_privilege where uid IN (%s) AND expired_time >= '%s' "
  29. _selAnchorUID = "SELECT id,uid,target_id,privilege_type,start_time,expired_time,ctime,utime FROM ap_user_privilege where target_id IN (%s) AND expired_time >= '%s' "
  30. _errorDBLogPrefix = "xuser.dahanghai.dao.mysql|"
  31. )
  32. // GetByUIDs 批量查询
  33. func (d *GuardDao) GetByUIDs(c context.Context, uids []int64) (dhhs []*dhhm.DHHDB, err error) {
  34. reqStartTime := confm.RecordTimeCost()
  35. dhhs = make([]*dhhm.DHHDB, 0)
  36. tm := time.Now()
  37. timeNow := tm.Format("2006-1-2 15:04:05")
  38. rows, err1 := d.db.Query(c, fmt.Sprintf(_selUID, xstr.JoinInts(uids), timeNow))
  39. if err1 != nil {
  40. reqAfterTime := confm.RecordTimeCost()
  41. err = err1
  42. log.Error(_errorDBLogPrefix+confm.GetFromDHHDBError+"|GetByUIDs err: %v|cost:%dms", err, reqAfterTime-reqStartTime)
  43. return
  44. }
  45. for rows.Next() {
  46. ele := &dhhm.DHHDB{}
  47. if err = rows.Scan(&ele.ID, &ele.UID, &ele.TargetId, &ele.PrivilegeType, &ele.StartTime, &ele.ExpiredTime, &ele.Ctime, &ele.Utime); err != nil {
  48. log.Error(_errorDBLogPrefix+confm.ScanFromDHHDBError+"|GetByUIDs rows.Scan err: %v", err)
  49. return
  50. }
  51. dhhs = append(dhhs, ele)
  52. }
  53. return
  54. }
  55. // GetByUIDsWithMap 批量查询
  56. func (d *GuardDao) GetByUIDsWithMap(c context.Context, uids []int64) (dhhs map[int64][]*dhhm.DHHDB, err error) {
  57. reqStartTime := confm.RecordTimeCost()
  58. dhhs = make(map[int64][]*dhhm.DHHDB)
  59. tm := time.Now()
  60. timeNow := tm.Format("2006-1-2 15:04:05")
  61. rows, err1 := d.db.Query(c, fmt.Sprintf(_selUID, xstr.JoinInts(uids), timeNow))
  62. if err1 != nil {
  63. reqAfterTime := confm.RecordTimeCost()
  64. err = err1
  65. log.Error(_errorDBLogPrefix+confm.GetFromDHHDBError+"|GetByUIDs err: %v|cost:%dms", err, reqAfterTime-reqStartTime)
  66. return
  67. }
  68. for rows.Next() {
  69. ele := &dhhm.DHHDB{}
  70. if err = rows.Scan(&ele.ID, &ele.UID, &ele.TargetId, &ele.PrivilegeType, &ele.StartTime, &ele.ExpiredTime, &ele.Ctime, &ele.Utime); err != nil {
  71. log.Error(_errorDBLogPrefix+confm.ScanFromDHHDBError+"|GetByUIDs rows.Scan err: %v", err)
  72. return
  73. }
  74. if _, exist := dhhs[ele.UID]; !exist {
  75. dhhs[ele.UID] = make([]*dhhm.DHHDB, 0)
  76. }
  77. dhhs[ele.UID] = append(dhhs[ele.UID], ele)
  78. }
  79. return
  80. }
  81. // GetByAnchorUIDs 批量查询
  82. func (d *GuardDao) GetByAnchorUIDs(c context.Context, uids []int64) (dhhs []*dhhm.DHHDB, err error) {
  83. reqStartTime := confm.RecordTimeCost()
  84. dhhs = make([]*dhhm.DHHDB, 0)
  85. tm := time.Now()
  86. timeNow := tm.Format("2006-1-2 15:04:05")
  87. rows, err1 := d.db.Query(c, fmt.Sprintf(_selAnchorUID, xstr.JoinInts(uids), timeNow))
  88. if err1 != nil {
  89. reqAfterTime := confm.RecordTimeCost()
  90. err = err1
  91. log.Error(_errorDBLogPrefix+confm.GetFromDHHDBError+"|GetByUIDs err: %v|cost:%dms", err, reqAfterTime-reqStartTime)
  92. return
  93. }
  94. for rows.Next() {
  95. ele := &dhhm.DHHDB{}
  96. if err = rows.Scan(&ele.ID, &ele.UID, &ele.TargetId, &ele.PrivilegeType, &ele.StartTime, &ele.ExpiredTime, &ele.Ctime, &ele.Utime); err != nil {
  97. log.Error(_errorDBLogPrefix+confm.ScanFromDHHDBError+"|GetByUIDs rows.Scan err: %v", err)
  98. return
  99. }
  100. dhhs = append(dhhs, ele)
  101. }
  102. return
  103. }
  104. // GetGuardByUID get guard info by uid
  105. func (d *GuardDao) GetGuardByUID(ctx context.Context, uid int64) (info []*model.GuardInfo, err error) {
  106. sql := fmt.Sprintf(_getGuardInfo, _guardTable)
  107. rows, err := d.db.Query(ctx, sql, uid, time.Now().Format("2006-01-02 15:04:05"))
  108. if err != nil {
  109. log.Error("[dao.guard.mysql|GetGuardByUID] get user guard record error(%v), uid(%d)", err, uid)
  110. return nil, err
  111. }
  112. defer rows.Close()
  113. for rows.Next() {
  114. var inf model.GuardInfo
  115. err = rows.Scan(&inf.Id, &inf.Uid, &inf.TargetId, &inf.PrivilegeType, &inf.StartTime, &inf.ExpiredTime)
  116. if err != nil {
  117. log.Error("[dao.guard.mysql|GetGuardByUID] scan user guard record error(%v), uid(%d)", err, uid)
  118. return nil, err
  119. }
  120. info = append(info, &inf)
  121. }
  122. return
  123. }
  124. // GetGuardByUIDRuid get guard info by uid and ruid
  125. func (d *GuardDao) GetGuardByUIDRuid(ctx context.Context, uid int64, ruid int64) (info []*model.GuardInfo, err error) {
  126. sql := fmt.Sprintf(_getGuardInfo2, _guardTable)
  127. rows, err := d.db.Query(ctx, sql, uid, ruid, time.Now().Format("2006-01-02 15:04:05"))
  128. if err != nil {
  129. log.Error("[dao.guard.mysql|GetGuardByUIDRuid] get user guard record error(%v), uid(%d), ruid(%d)", err, uid, ruid)
  130. return nil, err
  131. }
  132. defer rows.Close()
  133. for rows.Next() {
  134. var inf model.GuardInfo
  135. err = rows.Scan(&inf.Id, &inf.Uid, &inf.TargetId, &inf.PrivilegeType, &inf.StartTime, &inf.ExpiredTime)
  136. if err != nil {
  137. log.Error("[dao.guard.mysql|GetGuardByUIDRuid] scan user guard record error(%v), uid(%d), ruid(%d)", err, uid, ruid)
  138. return nil, err
  139. }
  140. info = append(info, &inf)
  141. }
  142. return
  143. }
  144. // AddGuard insert guard
  145. func (d *GuardDao) AddGuard(ctx context.Context, req *model.GuardBuy) (err error) {
  146. sql := fmt.Sprintf(_addGuardInfo, _guardTable)
  147. now := time.Now()
  148. endTime := time.Date(now.Year(), now.Month(), now.Day(), int(23), int(59), int(59), int(999), time.Local).AddDate(0, 0, req.Num*30)
  149. res, err := d.db.Exec(ctx, sql, req.Uid, req.Ruid, req.GuardLevel, now.Format("2006-01-02 15:04:05"), endTime.Format("2006-01-02 15:04:05"))
  150. if err != nil {
  151. // unique key exists error
  152. log.Error("[dao.guard.mysql|AddGuard] add user guard record error(%v), req(%v)", err, req)
  153. return
  154. }
  155. if _, err = res.LastInsertId(); err != nil {
  156. err = errors.WithStack(err)
  157. log.Error("[dao.guard.mysql|AddGuard] get last insert id error(%v), req(%v)", err, req)
  158. }
  159. return
  160. }
  161. // UpdateGuard update guard info
  162. func (d *GuardDao) UpdateGuard(ctx context.Context, req *model.GuardBuy, cond string) (err error) {
  163. sql := fmt.Sprintf(_updGuardInfo, _guardTable, cond)
  164. _, err = d.db.Exec(ctx, sql, req.Num*30, req.Uid, req.Ruid, time.Now().Format("2006-01-02 15:04:05"), req.GuardLevel)
  165. if err != nil {
  166. log.Error("[dao.guard.mysql|UpdateGuard] update user guard record error(%v), req(%v)", err, req)
  167. return
  168. }
  169. return
  170. }
  171. // UpsertGuard upsert guard info
  172. func (d *GuardDao) UpsertGuard(ctx context.Context, req *model.GuardBuy, expiredTime string) (err error) {
  173. sql := fmt.Sprintf(_upsertGuardInfo, _guardTable)
  174. now := time.Now()
  175. _, err = d.db.Exec(ctx, sql, req.Uid, req.Ruid, req.GuardLevel, now.Format("2006-01-02 15:04:05"), expiredTime, now.Format("2006-01-02 15:04:05"), expiredTime)
  176. if err != nil {
  177. log.Error("[dao.guard.mysql|UpsertGuard] upsert user guard record error(%v), req(%v)", err, req)
  178. return
  179. }
  180. return
  181. }