template.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package template
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/model/template"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. // insert
  10. _addTplSQL = "INSERT INTO archive_templates (mid,name,title,typeid,tag,copyright,content,state,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?)"
  11. // update
  12. _upTplSQL = "UPDATE archive_templates SET name=?,title=?,typeid=?,tag=?,copyright=?,content=?,mtime=? WHERE id=? AND mid=?"
  13. _delTplSQL = "UPDATE archive_templates SET state=?,mtime=? WHERE id=? AND mid=?"
  14. // select
  15. _getTplSQL = "SELECT id,name,title,typeid,tag,copyright,content,state,ctime,mtime FROM archive_templates WHERE id=? AND mid=?"
  16. _getMutilTplSQL = "SELECT id,name,title,typeid,tag,copyright,content FROM archive_templates WHERE mid=? AND state=0 ORDER BY ctime DESC"
  17. _getCntSQL = "SELECT count(id) FROM archive_templates WHERE mid = ? AND state = 0"
  18. )
  19. // templates get all Template from db.
  20. func (d *Dao) templates(c context.Context, mid int64) (tps []*template.Template, err error) {
  21. tps = make([]*template.Template, 0)
  22. rows, err := d.getMutilTplStmt.Query(c, mid)
  23. if err != nil {
  24. log.Error("mysqlDB.Query error(%v)", err)
  25. return
  26. }
  27. defer rows.Close()
  28. for rows.Next() {
  29. t := &template.Template{}
  30. if err = rows.Scan(&t.ID, &t.Name, &t.Title, &t.TypeID, &t.Tag, &t.Copyright, &t.Content); err != nil {
  31. log.Error("row.Scan error(%v)", err)
  32. return
  33. }
  34. tps = append(tps, t)
  35. }
  36. return
  37. }
  38. // Template get single template.
  39. func (d *Dao) Template(c context.Context, id, mid int64) (t *template.Template, err error) {
  40. row := d.getTplStmt.QueryRow(c, id, mid)
  41. t = &template.Template{}
  42. if err = row.Scan(&t.ID, &t.Name, &t.Title, &t.TypeID, &t.Tag, &t.Copyright, &t.Content, &t.State, &t.CTime, &t.MTime); err != nil {
  43. if err == sql.ErrNoRows {
  44. t = nil
  45. err = nil
  46. } else {
  47. log.Error("row.Scan error(%v)", err)
  48. }
  49. }
  50. return
  51. }
  52. // Templates get all Template
  53. func (d *Dao) Templates(c context.Context, mid int64) (tps []*template.Template, err error) {
  54. // try cache
  55. if tps, _ = d.tplCache(c, mid); tps != nil {
  56. return
  57. }
  58. // from db
  59. if tps, err = d.templates(c, mid); tps != nil {
  60. d.addCache(func() {
  61. d.addTplCache(context.TODO(), mid, tps)
  62. })
  63. }
  64. return
  65. }
  66. // AddTemplate add Template
  67. func (d *Dao) AddTemplate(c context.Context, mid int64, tp *template.Template) (id int64, err error) {
  68. res, err := d.addTplStmt.Exec(c, mid, tp.Name, tp.Title, tp.TypeID, tp.Tag, tp.Copyright, tp.Content, tp.State, tp.CTime, tp.MTime)
  69. if err != nil {
  70. log.Error("d.AddTemplate.Exec(%+v) error(%v)", tp, err)
  71. return
  72. }
  73. if id, err = res.LastInsertId(); err != nil {
  74. log.Error("res.LastInsertId(%d) error(%v)", id, err)
  75. return
  76. }
  77. d.addCache(func() {
  78. d.delTplCache(context.TODO(), mid)
  79. })
  80. return
  81. }
  82. // UpTemplate update Template
  83. func (d *Dao) UpTemplate(c context.Context, mid int64, tp *template.Template) (rows int64, err error) {
  84. res, err := d.upTplStmt.Exec(c, tp.Name, tp.Title, tp.TypeID, tp.Tag, tp.Copyright, tp.Content, tp.MTime, tp.ID, mid)
  85. if err != nil {
  86. log.Error("d.upTplStmt.Exec(%d, %d) error(%v)", mid, tp.ID, err)
  87. return
  88. }
  89. if rows, err = res.RowsAffected(); err != nil {
  90. log.Error("res.RowsAffected rows(%d) error(%v)", rows, err)
  91. return
  92. }
  93. d.addCache(func() {
  94. d.delTplCache(context.TODO(), mid)
  95. })
  96. return
  97. }
  98. // DelTemplate delete Template
  99. func (d *Dao) DelTemplate(c context.Context, mid int64, tp *template.Template) (rows int64, err error) {
  100. res, err := d.delTplStmt.Exec(c, tp.State, tp.MTime, tp.ID, mid)
  101. if err != nil {
  102. log.Error("d.delTplStmt.Exec(%d ) error(%v)", mid, err)
  103. return
  104. }
  105. if rows, err = res.RowsAffected(); err != nil {
  106. log.Error("res.RowsAffected rows(%d) error(%v)", rows, err)
  107. return
  108. }
  109. d.addCache(func() {
  110. d.delTplCache(context.TODO(), mid)
  111. })
  112. return
  113. }
  114. // Count count all state Template
  115. func (d *Dao) Count(c context.Context, mid int64) (count int64, err error) {
  116. row := d.getCntStmt.QueryRow(c, mid)
  117. if err = row.Scan(&count); err != nil {
  118. if err == sql.ErrNoRows {
  119. err = nil
  120. count = 0
  121. } else {
  122. log.Error("row.Scan error(%v)", err)
  123. }
  124. }
  125. return
  126. }