shorturl.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/interface/main/shorturl/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _prefix = "su_"
  11. //short_url
  12. _getSQL = "SELECT id,mid,short_url,long_url,state,ctime,mtime FROM short_url WHERE short_url=?"
  13. _getAllSQL = "SELECT id,mid,short_url,long_url,state,ctime,mtime FROM short_url"
  14. _getLimitSQL = "SELECT id,mid,short_url,long_url,state,ctime,mtime FROM short_url WHERE state=? " // TODO limit page
  15. _shortInSQL = "INSERT IGNORE INTO short_url(mid,short_url,long_url,state,ctime) VALUES(?,?,?,?,?)"
  16. _shortUpSQL = "UPDATE short_url SET mid=?,long_url=? WHERE id=?"
  17. _shortCountSQL = "SELECT COUNT(*) FROM short_url WHERE state=?"
  18. _shortByIDSQL = "SELECT id,mid,short_url,long_url,state,ctime,mtime FROM short_url WHERE id=?"
  19. _updateStateSQL = "UPDATE short_url SET mid=?,state=? WHERE id=?"
  20. )
  21. // Short get short_url
  22. func (d *Dao) Short(ctx context.Context, short string) (res *model.ShortUrl, err error) {
  23. rows := d.db.QueryRow(ctx, _getSQL, short)
  24. res = &model.ShortUrl{}
  25. if err = rows.Scan(&res.ID, &res.Mid, &res.Short, &res.Long, &res.State, &res.CTime, &res.MTime); err != nil {
  26. if err == sql.ErrNoRows {
  27. res = nil
  28. err = nil
  29. } else {
  30. log.Error("rows.Scan error(%v)", err)
  31. }
  32. return
  33. }
  34. return
  35. }
  36. // ShortbyID get short_url by id
  37. func (d *Dao) ShortbyID(ctx context.Context, id int64) (res *model.ShortUrl, err error) {
  38. rows := d.db.QueryRow(ctx, _shortByIDSQL, id)
  39. res = &model.ShortUrl{}
  40. if err = rows.Scan(&res.ID, &res.Mid, &res.Short, &res.Long, &res.State, &res.CTime, &res.MTime); err != nil {
  41. if err == sql.ErrNoRows {
  42. res = nil
  43. err = nil
  44. } else {
  45. log.Error("rows.Scan err (%v)", err)
  46. }
  47. return
  48. }
  49. res.FormatDate()
  50. return
  51. }
  52. // AllShorts get all short_url
  53. func (d *Dao) AllShorts(ctx context.Context) (res []*model.ShortUrl, err error) {
  54. rows, err := d.db.Query(ctx, _getAllSQL)
  55. if err != nil {
  56. log.Error("query error (%v)", err)
  57. return
  58. }
  59. defer rows.Close()
  60. for rows.Next() {
  61. su := &model.ShortUrl{}
  62. if err = rows.Scan(&su.ID, &su.Mid, &su.Short, &su.Long, &su.State, &su.CTime, &su.MTime); err != nil {
  63. log.Error("rows.Scan err (%v)", err)
  64. return
  65. }
  66. su.FormatDate()
  67. res = append(res, su)
  68. }
  69. return
  70. }
  71. // ShortCount get all short_url
  72. func (d *Dao) ShortCount(ctx context.Context, mid int64, long string) (count int, err error) {
  73. countSQL := _shortCountSQL
  74. if mid > 0 {
  75. countSQL = fmt.Sprintf("%s AND mid=%d", countSQL, mid)
  76. }
  77. if long != "" {
  78. countSQL += " AND long_url='" + long + "'"
  79. }
  80. row := d.db.QueryRow(ctx, countSQL, model.StateNormal)
  81. if err = row.Scan(&count); err != nil {
  82. log.Error("row.Scan error(%v)", err)
  83. return
  84. }
  85. return
  86. }
  87. // InShort add short_url
  88. func (d *Dao) InShort(ctx context.Context, su *model.ShortUrl) (id int64, err error) {
  89. res, err := d.db.Exec(ctx, _shortInSQL, su.Mid, su.Short, su.Long, su.State, su.CTime)
  90. if err != nil {
  91. log.Error("tx.Exec() error(%v)", err)
  92. return
  93. }
  94. return res.LastInsertId()
  95. }
  96. // ShortUp add short_url
  97. func (d *Dao) ShortUp(ctx context.Context, id, mid int64, long string) (rows int64, err error) {
  98. res, err := d.db.Exec(ctx, _shortUpSQL, mid, long, id)
  99. if err != nil {
  100. log.Error("tx.Exec error(%v)", err)
  101. return
  102. }
  103. return res.RowsAffected()
  104. }
  105. // UpdateState update state
  106. func (d *Dao) UpdateState(ctx context.Context, id, mid int64, state int) (rows int64, err error) {
  107. _, err = d.db.Exec(ctx, _updateStateSQL, mid, state, id)
  108. if err != nil {
  109. log.Error("d.db.Exec(%s) error(%v)", _updateStateSQL, err)
  110. return
  111. }
  112. return
  113. }
  114. // ShortLimit get short_url list
  115. func (d *Dao) ShortLimit(ctx context.Context, pn, ps int, mid int64, long string) (res []*model.ShortUrl, err error) {
  116. limitSQL := _getLimitSQL
  117. if mid > 0 {
  118. limitSQL = fmt.Sprintf("%s AND mid=%d", limitSQL, mid)
  119. }
  120. if long != "" {
  121. limitSQL += " AND long_url='" + long + "'"
  122. }
  123. limitSQL += " ORDER BY id DESC LIMIT ?,? "
  124. rows, err := d.db.Query(ctx, limitSQL, model.StateNormal, pn, ps)
  125. if err != nil {
  126. log.Error("query error (%v)", err)
  127. return
  128. }
  129. defer rows.Close()
  130. res = []*model.ShortUrl{}
  131. for rows.Next() {
  132. su := &model.ShortUrl{}
  133. if err = rows.Scan(&su.ID, &su.Mid, &su.Short, &su.Long, &su.State, &su.CTime, &su.MTime); err != nil {
  134. log.Error("rows.Scan err (%v)", err)
  135. return
  136. }
  137. su.FormatDate()
  138. res = append(res, su)
  139. }
  140. return
  141. }