emoji.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package dao
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "go-common/app/admin/main/reply/model"
  7. xsql "go-common/library/database/sql"
  8. )
  9. const (
  10. //state 0-上线 1-下线
  11. _selEmojiSQL = "SELECT id,package_id,name,url,sort,state,remark from emoji order by package_id,sort"
  12. _selEmojiByPidSQL = "SELECT id,package_id,name,url,sort,state,remark from emoji where package_id=? order by sort"
  13. _selEmojiByNameSQL = "SELECT id from emoji where name=? "
  14. _insertEmojiSQL = "INSERT INTO emoji (package_id, name, url, sort, state, remark, ctime, mtime ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
  15. _upEmojiSortSQL = "UPDATE emoji SET sort=?, mtime=? where id=?"
  16. _upEmojiStateSQL = "UPDATE emoji SET state=?, mtime=? WHERE id=?"
  17. _upEmojiSQL = "UPDATE emoji SET name=?, remark=?, url=?, mtime=? WHERE id=?"
  18. _delEmojiByIDSQL = "DELETE from emoji where id=?"
  19. _delEmojiByPidSQL = "DELETE from emoji where package_id=?"
  20. )
  21. // EmojiList get all emoji
  22. func (d *Dao) EmojiList(c context.Context) (emojis []*model.Emoji, err error) {
  23. rows, err := d.db.Query(c, _selEmojiSQL)
  24. if err != nil {
  25. return
  26. }
  27. defer rows.Close()
  28. for rows.Next() {
  29. emo := &model.Emoji{}
  30. if err = rows.Scan(&emo.ID, &emo.PackageID, &emo.Name, &emo.URL, &emo.Sort, &emo.State, &emo.Remark); err != nil {
  31. return
  32. }
  33. emojis = append(emojis, emo)
  34. }
  35. err = rows.Err()
  36. return
  37. }
  38. // EmojiListByPid get emoji by package_id
  39. func (d *Dao) EmojiListByPid(c context.Context, pid int64) (emojis []*model.Emoji, err error) {
  40. rows, err := d.db.Query(c, _selEmojiByPidSQL, pid)
  41. if err != nil {
  42. return
  43. }
  44. defer rows.Close()
  45. for rows.Next() {
  46. emo := &model.Emoji{}
  47. if err = rows.Scan(&emo.ID, &emo.PackageID, &emo.Name, &emo.URL, &emo.Sort, &emo.State, &emo.Remark); err != nil {
  48. return
  49. }
  50. emojis = append(emojis, emo)
  51. }
  52. err = rows.Err()
  53. return
  54. }
  55. // EmojiByName get emoji by name
  56. func (d *Dao) EmojiByName(c context.Context, name string) (emojis []*model.Emoji, err error) {
  57. rows, err := d.db.Query(c, _selEmojiByNameSQL, name)
  58. if err != nil {
  59. return
  60. }
  61. defer rows.Close()
  62. for rows.Next() {
  63. emo := &model.Emoji{}
  64. if err = rows.Scan(&emo.ID); err != nil {
  65. return
  66. }
  67. emojis = append(emojis, emo)
  68. }
  69. err = rows.Err()
  70. return
  71. }
  72. // CreateEmoji insert a emoji into db
  73. func (d *Dao) CreateEmoji(c context.Context, pid int64, name string, url string, sort int32, state int32, remark string) (id int64, err error) {
  74. result, err := d.db.Exec(c, _insertEmojiSQL, pid, name, url, sort, state, remark, time.Now(), time.Now())
  75. if err != nil {
  76. return
  77. }
  78. return result.LastInsertId()
  79. }
  80. // UpEmojiSort udpate emoji sort
  81. func (d *Dao) UpEmojiSort(tx *xsql.Tx, ids string) (err error) {
  82. idx := strings.Split(ids, ",")
  83. for sort, id := range idx {
  84. _, err = tx.Exec(_upEmojiSortSQL, sort, time.Now(), id)
  85. if err != nil {
  86. return
  87. }
  88. }
  89. return nil
  90. }
  91. // UpEmojiStateByID update emoji state
  92. func (d *Dao) UpEmojiStateByID(c context.Context, state int32, id int64) (idx int64, err error) {
  93. result, err := d.db.Exec(c, _upEmojiStateSQL, state, time.Now(), id)
  94. if err != nil {
  95. return
  96. }
  97. return result.RowsAffected()
  98. }
  99. // UpEmoji update emoji name and remark by id
  100. func (d *Dao) UpEmoji(c context.Context, name string, remark string, url string, id int64) (idx int64, err error) {
  101. result, err := d.db.Exec(c, _upEmojiSQL, name, remark, url, time.Now(), id)
  102. if err != nil {
  103. return
  104. }
  105. return result.RowsAffected()
  106. }
  107. // DelEmojiByID delete emoji by id
  108. func (d *Dao) DelEmojiByID(c context.Context, id int64) (idx int64, err error) {
  109. result, err := d.db.Exec(c, _delEmojiByIDSQL, id)
  110. if err != nil {
  111. return
  112. }
  113. return result.RowsAffected()
  114. }
  115. // DelEmojiByPid delete emoji by package_id
  116. func (d *Dao) DelEmojiByPid(c context.Context, pid int64) (idx int64, err error) {
  117. result, err := d.db.Exec(c, _delEmojiByPidSQL, pid)
  118. if err != nil {
  119. return
  120. }
  121. return result.RowsAffected()
  122. }