emoji_package.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. _selEmojiPackageSQL = "SELECT id,name,url,remark,sort,state from emoji_package order by sort"
  11. _addEmojiPackageSQL = "INSERT INTO emoji_package (name, url, sort, state, remark, ctime, mtime ) VALUES (?, ?, ?, ?, ?, ?, ?)"
  12. _upEmojiPackageSQL = "UPDATE emoji_package SET `name`=?, url=?, remark=?, state=?, mtime=? WHERE id=?"
  13. _upEmojiPackageSortSQL = "UPDATE emoji_package SET sort=?, mtime=? where id=?"
  14. _delEmojiPackageSQL = "DELETE from emoji_package where id=?"
  15. )
  16. // EmojiPackageList get all emoji_package
  17. func (d *Dao) EmojiPackageList(c context.Context) (packs []*model.EmojiPackage, err error) {
  18. rows, err := d.db.Query(c, _selEmojiPackageSQL)
  19. if err != nil {
  20. return
  21. }
  22. defer rows.Close()
  23. for rows.Next() {
  24. pack := &model.EmojiPackage{}
  25. if err = rows.Scan(&pack.ID, &pack.Name, &pack.URL, &pack.Remark, &pack.Sort, &pack.State); err != nil {
  26. return
  27. }
  28. packs = append(packs, pack)
  29. }
  30. err = rows.Err()
  31. return
  32. }
  33. // CreateEmojiPackage insert a emoji_package into db
  34. func (d *Dao) CreateEmojiPackage(c context.Context, name string, url string, sort int32, remark string, state int32) (id int64, err error) {
  35. result, err := d.db.Exec(c, _addEmojiPackageSQL, name, url, sort, state, remark, time.Now(), time.Now())
  36. if err != nil {
  37. return
  38. }
  39. return result.LastInsertId()
  40. }
  41. // UpEmojiPackageSort udpate emojipack sort
  42. func (d *Dao) UpEmojiPackageSort(tx *xsql.Tx, ids string) (err error) {
  43. idx := strings.Split(ids, ",")
  44. for sort, id := range idx {
  45. _, err = tx.Exec(_upEmojiPackageSortSQL, sort, time.Now(), id)
  46. if err != nil {
  47. return
  48. }
  49. }
  50. return nil
  51. }
  52. //UpEmojiPackage update emojipack by id
  53. func (d *Dao) UpEmojiPackage(c context.Context, name string, url string, remark string, state int32, id int64) (idx int64, err error) {
  54. result, err := d.db.Exec(c, _upEmojiPackageSQL, name, url, remark, state, time.Now(), id)
  55. if err != nil {
  56. return
  57. }
  58. return result.RowsAffected()
  59. }
  60. // DelEmojiPackage delete emoji_package by id
  61. func (d *Dao) DelEmojiPackage(c context.Context, id int64) (idx int64, err error) {
  62. result, err := d.db.Exec(c, _delEmojiPackageSQL, id)
  63. if err != nil {
  64. return
  65. }
  66. return result.RowsAffected()
  67. }