tidb.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "sync"
  7. "time"
  8. "go-common/app/service/main/thumbup/model"
  9. sql "go-common/library/database/tidb"
  10. "go-common/library/log"
  11. xtime "go-common/library/time"
  12. "go-common/library/xstr"
  13. "go-common/library/sync/errgroup"
  14. )
  15. const (
  16. _tidbBulkSize = 50
  17. _tidbBusinessesSQL = "SELECT id, name, message_list_type, user_likes_limit, message_likes_limit, enable_originid, user_list_type FROM business WHERE dtime = '0000-00-00 00:00:00'"
  18. _tidbLikeMidSQL = "SELECT type FROM likes WHERE mid=? AND business_id = ? AND origin_id = ? AND message_id = ?"
  19. _tidbUserLikeListSQL = "SELECT message_id, mtime FROM likes WHERE mid = ? AND business_id =? AND type = ? ORDER BY mtime desc LIMIT ? OFFSET ?"
  20. _tidbUserHasLikeSQL = "SELECT message_id, mtime FROM likes WHERE mid = ? AND business_id =? AND type = ? AND message_id in (%s)"
  21. _tidbItemLikeListSQL = "SELECT mid, mtime FROM likes WHERE business_id =? AND origin_id =? AND message_id =? AND type = ? ORDER BY mtime desc LIMIT ? OFFSET ?"
  22. _tidbStatSQL = "SELECT message_id, likes_count, dislikes_count, origin_id, likes_change, dislikes_change FROM counts WHERE business_id = ? AND origin_id = ? AND message_id = ?"
  23. _tidbStatsSQL = "SELECT message_id, likes_count, dislikes_count, likes_change, dislikes_change FROM counts WHERE business_id = ? AND origin_id = 0 AND message_id in (%s)"
  24. _tidbStatsOriginSQL = "SELECT message_id, likes_count, dislikes_count, likes_change, dislikes_change FROM counts WHERE business_id = ? AND origin_id = ?"
  25. _tidbUserLikeCountSQL = "SELECT COUNT(*) FROM likes WHERE business_id =? AND mid = ? AND type = ?"
  26. _tidbUpdateLikeSQL = "INSERT INTO likes (business_id, origin_id, message_id, mid, type, mtime) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE type=?, mtime = ?"
  27. _tidbUpdateCountsSQL = "INSERT INTO counts (business_id, origin_id, message_id, likes_count, dislikes_count, up_mid) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE "
  28. _tidbupdateCountChange = "UPDATE counts SET likes_change = likes_change + ?, dislikes_change = dislikes_change + ? WHERE business_id = ? AND origin_id = ? AND message_id = ?"
  29. _tidbItemHasLikeSQL = "SELECT mid, mtime FROM likes WHERE business_id = ? AND origin_id = ? AND message_id = ? AND type =? AND mid in (%s)"
  30. )
  31. // Businesses get business list
  32. func (d *Dao) Businesses(c context.Context) (res []*model.Business, err error) {
  33. var rows *sql.Rows
  34. if rows, err = d.businessesStmt.Query(c); err != nil {
  35. PromError("tidb:业务查询")
  36. log.Error("tidb.businessesStmt.Query error(%v)", err)
  37. return
  38. }
  39. defer rows.Close()
  40. for rows.Next() {
  41. b := &model.Business{}
  42. if err = rows.Scan(&b.ID, &b.Name, &b.MessageListType, &b.UserLikesLimit, &b.MessageLikesLimit, &b.EnableOriginID, &b.UserListType); err != nil {
  43. PromError("tidb:业务Scan")
  44. log.Error("tidb.rows.Business.Scan error(%v)", err)
  45. return
  46. }
  47. res = append(res, b)
  48. }
  49. err = rows.Err()
  50. return
  51. }
  52. // LikeState get like state
  53. func (d *Dao) LikeState(c context.Context, mid, businessID, originID, messageID int64) (res int8, err error) {
  54. if err = d.likeStateStmt.QueryRow(c, mid, businessID, originID, messageID).Scan(&res); err != nil {
  55. if err == sql.ErrNoRows {
  56. err = nil
  57. return
  58. }
  59. PromError("tidb:LikeByMid")
  60. log.Error("tidbLikeByMid(%d,%d,%d,%d) error(%v)", mid, businessID, originID, messageID, err)
  61. }
  62. return
  63. }
  64. // UserLikeCount count
  65. func (d *Dao) UserLikeCount(c context.Context, businessID, mid int64, typ int8) (res int, err error) {
  66. if err = d.userLikeCountStmt.QueryRow(c, businessID, mid, typ).Scan(&res); err != nil {
  67. PromError("tidb:UserLikeCount")
  68. log.Error("tidbUserLikeCount(%d,%d,%d) error(%v)", businessID, mid, typ, err)
  69. }
  70. return
  71. }
  72. // RawItemLikeList item like list
  73. func (d *Dao) RawItemLikeList(c context.Context, messageID, businessID, originID int64, state int8, start, end int) (res []*model.UserLikeRecord, err error) {
  74. limit := end - start + 1
  75. var rows *sql.Rows
  76. if rows, err = d.itemLikeListStmt.Query(c, businessID, originID, messageID, state, limit, start); err != nil {
  77. PromError("tidb:项目点赞列表")
  78. log.Error("tidb.ItemLikeList.Query error(%v,%v,%v,%v,%v, %v)", businessID, originID, messageID, state, limit, err)
  79. return
  80. }
  81. defer rows.Close()
  82. for rows.Next() {
  83. b := &model.UserLikeRecord{}
  84. var t time.Time
  85. if err = rows.Scan(&b.Mid, &t); err != nil {
  86. PromError("tidb:业务Scan")
  87. log.Error("tidb.rows.Business.Scan error(%v)", err)
  88. return
  89. }
  90. b.Time = xtime.Time(t.Unix())
  91. res = append(res, b)
  92. }
  93. err = rows.Err()
  94. return
  95. }
  96. // RawUserLikeList .
  97. func (d *Dao) RawUserLikeList(c context.Context, mid, businessID int64, state int8, start, end int) (res []*model.ItemLikeRecord, err error) {
  98. limit := end - start
  99. var rows *sql.Rows
  100. if rows, err = d.userLikeListStmt.Query(c, mid, businessID, state, limit, start); err != nil {
  101. PromError("tidb:用户点赞列表")
  102. log.Error("tidb.UserLikeList.Query error(%v,%v,%v,%v,%v)", mid, businessID, state, limit, err)
  103. return
  104. }
  105. defer rows.Close()
  106. for rows.Next() {
  107. b := &model.ItemLikeRecord{}
  108. var t time.Time
  109. if err = rows.Scan(&b.MessageID, &t); err != nil {
  110. PromError("tidb:用户Scan")
  111. log.Error("tidb.rows.Business.Scan error(%v)", err)
  112. return
  113. }
  114. b.Time = xtime.Time(t.Unix())
  115. res = append(res, b)
  116. }
  117. err = rows.Err()
  118. return
  119. }
  120. // MessageStats .
  121. func (d *Dao) MessageStats(c context.Context, businessID int64, ids []int64) (res map[int64]*model.Stats, err error) {
  122. res = make(map[int64]*model.Stats)
  123. var (
  124. group = &errgroup.Group{}
  125. mutex = &sync.Mutex{}
  126. )
  127. keysLen := len(ids)
  128. for i := 0; i < keysLen; i += _tidbBulkSize {
  129. var keys []int64
  130. if (i + _tidbBulkSize) > keysLen {
  131. keys = ids[i:]
  132. } else {
  133. keys = ids[i : i+_tidbBulkSize]
  134. }
  135. group.Go(func() error {
  136. statsSQL := fmt.Sprintf(_tidbStatsSQL, xstr.JoinInts(keys))
  137. rows, e := d.tidb.Query(c, statsSQL, businessID)
  138. if e != nil {
  139. err = e
  140. return nil
  141. }
  142. defer rows.Close()
  143. for rows.Next() {
  144. s := &model.Stats{}
  145. var likeChange, dislikeChange int64
  146. e = rows.Scan(&s.ID, &s.Likes, &s.Dislikes, &likeChange, &dislikeChange)
  147. s.Likes += likeChange
  148. s.Dislikes += dislikeChange
  149. if e != nil {
  150. err = e
  151. continue
  152. }
  153. mutex.Lock()
  154. res[s.ID] = s
  155. mutex.Unlock()
  156. }
  157. return nil
  158. })
  159. }
  160. group.Wait()
  161. if err != nil {
  162. PromError("tidb stats Scan")
  163. log.Error("tidb.rows.Stats.Scan error(%v)", err)
  164. }
  165. if len(res) == 0 {
  166. res = nil
  167. }
  168. return
  169. }
  170. // OriginStats .
  171. func (d *Dao) OriginStats(c context.Context, businessID, originID int64) (res map[int64]*model.Stats, err error) {
  172. var rows *sql.Rows
  173. if rows, err = d.statsOriginStmt.Query(c, businessID, originID); err != nil {
  174. PromError("tidb:计数表")
  175. log.Error("db.Stats.Query error(%v,%v,%v)", businessID, originID, err)
  176. return
  177. }
  178. defer rows.Close()
  179. for rows.Next() {
  180. b := &model.Stats{OriginID: originID}
  181. var likeChange, dislikeChange int64
  182. if err = rows.Scan(&b.ID, &b.Likes, &b.Dislikes, &likeChange, &dislikeChange); err != nil {
  183. PromError("tidb:计数Scan")
  184. log.Error("tidb.rows.Stats.Scan error(%v)", err)
  185. return
  186. }
  187. b.Likes += likeChange
  188. b.Dislikes += dislikeChange
  189. if res == nil {
  190. res = make(map[int64]*model.Stats)
  191. }
  192. res[b.ID] = b
  193. }
  194. err = rows.Err()
  195. return
  196. }
  197. // Stat .
  198. func (d *Dao) Stat(c context.Context, businessID, originID, messageID int64) (res model.Stats, err error) {
  199. var likeChange, dislikeChange int64
  200. if err = d.statStmt.QueryRow(c, businessID, originID, messageID).Scan(&res.ID, &res.Likes, &res.Dislikes, &res.OriginID, &likeChange, &dislikeChange); err != nil {
  201. if err == sql.ErrNoRows {
  202. err = nil
  203. res.ID = messageID
  204. res.OriginID = originID
  205. return
  206. }
  207. PromError("tidb:查询计数表")
  208. log.Error("tidb.Stat.Query error(%v,%v,%v,%v)", businessID, originID, messageID, err)
  209. }
  210. res.Likes += likeChange
  211. res.Dislikes += dislikeChange
  212. return
  213. }
  214. // RawStats stat changes
  215. func (d *Dao) RawStats(c context.Context, businessID, originID, messageID int64) (res model.RawStats, err error) {
  216. if err = d.statStmt.QueryRow(c, businessID, originID, messageID).Scan(&res.ID, &res.Likes, &res.Dislikes, &res.OriginID, &res.LikesChange, &res.DislikesChange); err != nil {
  217. if err == sql.ErrNoRows {
  218. err = nil
  219. res.ID = messageID
  220. res.OriginID = originID
  221. return
  222. }
  223. PromError("tidb:查询change计数表")
  224. log.Error("tidb.StatChanges.Query error(%v,%v,%v,%v)", businessID, originID, messageID, err)
  225. }
  226. return
  227. }
  228. // TxUpdateCounts update like info via transaction.
  229. func (d *Dao) tidbTxUpdateCounts(c context.Context, tx *sql.Tx, businessID, originID, messageID int64, likesCount, dislikesCount, upMid int64) (err error) {
  230. likeSQL := _tidbUpdateCountsSQL
  231. if (likesCount == 0) && (dislikesCount == 0) {
  232. return
  233. }
  234. if (likesCount != 0) && (dislikesCount == 0) {
  235. if likesCount > 0 {
  236. likeSQL += fmt.Sprintf("likes_count = likes_count + %v", likesCount)
  237. } else {
  238. likeSQL += fmt.Sprintf("likes_count = likes_count %v", likesCount)
  239. }
  240. } else if (likesCount == 0) && (dislikesCount != 0) {
  241. if dislikesCount > 0 {
  242. likeSQL += fmt.Sprintf("dislikes_count = dislikes_count + %v", dislikesCount)
  243. } else {
  244. likeSQL += fmt.Sprintf("dislikes_count = dislikes_count %v", dislikesCount)
  245. }
  246. } else {
  247. if likesCount > 0 {
  248. likeSQL += fmt.Sprintf("likes_count = likes_count + %v", likesCount)
  249. } else {
  250. likeSQL += fmt.Sprintf("likes_count = likes_count %v", likesCount)
  251. }
  252. if dislikesCount > 0 {
  253. likeSQL += fmt.Sprintf(", dislikes_count = dislikes_count + %v", dislikesCount)
  254. } else {
  255. likeSQL += fmt.Sprintf(", dislikes_count = dislikes_count %v", dislikesCount)
  256. }
  257. }
  258. if upMid > 0 {
  259. likeSQL += fmt.Sprintf(", up_mid = %d", upMid)
  260. }
  261. if _, err = tx.Exec(likeSQL, businessID, originID, messageID, likesCount, dislikesCount, upMid); err != nil {
  262. PromError("tidb:更新计数表")
  263. log.Error("dao.tidbTxUpdateCounts tx.Exec(%s, %v,%v,%v) error(%v)", likeSQL, businessID, originID, messageID, err)
  264. }
  265. return
  266. }
  267. // tidbTxStat .
  268. func (d *Dao) tidbTxStat(c context.Context, tx *sql.Tx, businessID, originID, messageID int64) (res model.Stats, err error) {
  269. var likeChange, dislikeChange int64
  270. if err = tx.Stmts(d.statStmt).QueryRow(c, businessID, originID, messageID).Scan(&res.ID, &res.Likes, &res.Dislikes, &res.OriginID, &likeChange, &dislikeChange); err != nil {
  271. if err == sql.ErrNoRows {
  272. err = nil
  273. res.ID = messageID
  274. res.OriginID = originID
  275. return
  276. }
  277. PromError("tidb:查询计数表")
  278. log.Error("tidb: Tx db.Stat.Query error(%v,%v,%v,%v)", businessID, originID, messageID, err)
  279. }
  280. res.Likes += likeChange
  281. res.Dislikes += dislikeChange
  282. return
  283. }
  284. // UpdateCount .
  285. func (d *Dao) UpdateCount(c context.Context, businessID, originID, messageID int64, likeChange, dislikeChange int64) (err error) {
  286. if _, err = d.updateCountChangeStmt.Exec(c, likeChange, dislikeChange, businessID, originID, messageID); err != nil {
  287. PromError("tidb:更新change计数表")
  288. log.Error("db.tidbUpdateCountChange.Exec error(%v,%v,%v,%v)", businessID, originID, messageID, err)
  289. }
  290. return
  291. }
  292. // tidbBeginTran begin transaction.
  293. func (d *Dao) tidbBeginTran(c context.Context) (*sql.Tx, error) {
  294. return d.tidb.Begin(c)
  295. }
  296. // UpdateCounts .
  297. func (d *Dao) UpdateCounts(c context.Context, businessID, originID, messageID int64, likeCount, dislikeCount int64, upMid int64) (err error) {
  298. var tx *sql.Tx
  299. if tx, err = d.tidbBeginTran(c); err != nil {
  300. log.Error("tx.BeginTran() error(%v)", err)
  301. return
  302. }
  303. var stat model.Stats
  304. if stat, err = d.tidbTxStat(c, tx, businessID, originID, messageID); err != nil {
  305. if err1 := tx.Rollback(); err1 != nil {
  306. log.Error("tx.Rollback() error(%v)", err1)
  307. }
  308. return
  309. }
  310. if stat.Likes+likeCount < 0 {
  311. likeCount = -stat.Likes
  312. }
  313. if stat.Dislikes+dislikeCount < 0 {
  314. dislikeCount = -stat.Dislikes
  315. }
  316. if err = d.tidbTxUpdateCounts(c, tx, businessID, originID, messageID, likeCount, dislikeCount, upMid); err != nil {
  317. if err1 := tx.Rollback(); err1 != nil {
  318. log.Error("tx.Rollback() error(%v)", err1)
  319. }
  320. return
  321. }
  322. if err = tx.Commit(); err != nil {
  323. PromError("like:更新喜欢计数")
  324. log.Error("tx.Commit() error(%v)", err)
  325. return
  326. }
  327. return
  328. }
  329. // UpdateUpMids .
  330. func (d *Dao) UpdateUpMids(c context.Context, businessID int64, data []*model.UpMidsReq) (rows int64, err error) {
  331. var tx *sql.Tx
  332. if tx, err = d.tidbBeginTran(c); err != nil {
  333. log.Error("UpdateUpMids tx.BeginTran() error(%v)", err)
  334. return
  335. }
  336. var res xsql.Result
  337. for _, u := range data {
  338. if res, err = tx.Exec("UPDATE counts SET up_mid = ? WHERE business_id = ? AND origin_id = ? AND message_id = ?", u.UpMid, businessID, u.OriginID, u.MessageID); err != nil {
  339. if err1 := tx.Rollback(); err1 != nil {
  340. log.Error("UpdateUpMids tx.Rollback() error(%v)", err1)
  341. }
  342. return
  343. }
  344. aff, _ := res.RowsAffected()
  345. rows += aff
  346. }
  347. if err = tx.Commit(); err != nil {
  348. PromError("like:更新UpMids")
  349. log.Error("UpdateUpMids tx.Commit() error(%v)", err)
  350. }
  351. return
  352. }
  353. // ItemHasLike .
  354. func (d *Dao) ItemHasLike(c context.Context, businessID int64, originID, messageID int64, mids []int64, typ int8) (res map[int64]int64, err error) {
  355. var rows *sql.Rows
  356. sqlStr := fmt.Sprintf(_tidbItemHasLikeSQL, xstr.JoinInts(mids))
  357. if rows, err = d.tidb.Query(c, sqlStr, businessID, originID, messageID, typ); err != nil {
  358. PromError("tidb:itemHasLike")
  359. log.Error("tidb.ItemHasLike.Query asc error(%v,%v,%v,%v,%v)", businessID, originID, messageID, typ, err)
  360. return
  361. }
  362. defer rows.Close()
  363. for rows.Next() {
  364. var t time.Time
  365. var mid int64
  366. if res == nil {
  367. res = make(map[int64]int64)
  368. }
  369. if err = rows.Scan(&mid, &t); err != nil {
  370. PromError("tidb:itemHasLikeScan")
  371. log.Error("tidb.rows.itemHasLike.Scan error(%v)", err)
  372. return
  373. }
  374. res[mid] = t.Unix()
  375. }
  376. err = rows.Err()
  377. return
  378. }
  379. // UserHasLike .
  380. func (d *Dao) UserHasLike(c context.Context, businessID, mid int64, messageIDs []int64, typ int8) (res []*model.ItemLikeRecord, err error) {
  381. var rows *sql.Rows
  382. sqlStr := fmt.Sprintf(_tidbUserHasLikeSQL, xstr.JoinInts(messageIDs))
  383. if rows, err = d.tidb.Query(c, sqlStr, mid, businessID, typ); err != nil {
  384. PromError("tidb:userHasLike")
  385. log.Error("tidb.UserHasLike.Query asc error(%v,%v,%v,%v,%v)", mid, businessID, messageIDs, typ, err)
  386. return
  387. }
  388. defer rows.Close()
  389. for rows.Next() {
  390. var t xtime.Time
  391. var messageID int64
  392. if err = rows.Scan(&messageID, &t); err != nil {
  393. PromError("tidb:userHasLikeScan")
  394. log.Error("tidb.rows.userHasLike.Scan error(%v)", err)
  395. return
  396. }
  397. tmp := &model.ItemLikeRecord{
  398. MessageID: messageID,
  399. Time: t,
  400. }
  401. res = append(res, tmp)
  402. }
  403. err = rows.Err()
  404. return
  405. }