template.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package template
  2. import (
  3. "context"
  4. "go-common/library/ecode"
  5. "time"
  6. "go-common/app/interface/main/creative/model/template"
  7. "go-common/library/log"
  8. timex "go-common/library/time"
  9. )
  10. // Templates get templates for archive.
  11. func (s *Service) Templates(c context.Context, mid int64) (tps []*template.Template, err error) {
  12. if tps, err = s.tpl.Templates(c, mid); err != nil {
  13. log.Error("s.tem.Templates(%d) error(%v)", mid, err)
  14. return
  15. }
  16. return
  17. }
  18. // AddTemplate add template for archive.
  19. func (s *Service) AddTemplate(c context.Context, mid int64, typeid int16, cp, name, title, tag, content string, now time.Time) (err error) {
  20. var (
  21. tpl *template.Template
  22. count int64
  23. )
  24. if count, err = s.tpl.Count(c, mid); err != nil {
  25. log.Error("s.tpl.Count(%d) error(%v)", count, err)
  26. return
  27. }
  28. if count >= 5 {
  29. log.Error("mid(%d) upper limit(%d)", mid, count)
  30. err = ecode.CreativeTemplateOverMax
  31. return
  32. }
  33. tpl = &template.Template{
  34. Name: name,
  35. Title: title,
  36. Tag: tag,
  37. Content: content,
  38. TypeID: typeid,
  39. Copyright: template.Copyright(cp),
  40. State: int8(template.StateNormal),
  41. CTime: timex.Time(now.Unix()),
  42. MTime: timex.Time(now.Unix()),
  43. }
  44. _, err = s.tpl.AddTemplate(c, mid, tpl)
  45. return
  46. }
  47. // UpdateTemplate update template for archive.
  48. func (s *Service) UpdateTemplate(c context.Context, id, mid int64, typeid int16, cp, name, title, tag, content string, now time.Time) (err error) {
  49. var (
  50. t, tpl *template.Template
  51. )
  52. if t, err = s.tpl.Template(c, id, mid); err != nil {
  53. log.Error("s.tpl.Template id(%d) mid(%d) error(%v)", id, mid, err)
  54. return
  55. }
  56. if t == nil {
  57. err = ecode.NothingFound
  58. return
  59. }
  60. if t.State != 0 {
  61. err = ecode.CreativeTemplateDeleted
  62. return
  63. }
  64. tpl = &template.Template{
  65. ID: id,
  66. Name: name,
  67. Title: title,
  68. Tag: tag,
  69. Content: content,
  70. TypeID: typeid,
  71. Copyright: template.Copyright(cp),
  72. MTime: timex.Time(now.Unix()),
  73. }
  74. _, err = s.tpl.UpTemplate(c, mid, tpl)
  75. return
  76. }
  77. // DelTemplate delete template.
  78. func (s *Service) DelTemplate(c context.Context, id, mid int64, now time.Time) (err error) {
  79. var (
  80. t, tpl *template.Template
  81. )
  82. if t, err = s.tpl.Template(c, id, mid); err != nil {
  83. log.Error("s.tpl.Template id(%d) mid(%d) error(%v)", id, mid, err)
  84. return
  85. }
  86. if t == nil {
  87. err = ecode.NothingFound
  88. return
  89. }
  90. if t.State != 0 {
  91. err = ecode.CreativeTemplateDeleted
  92. return
  93. }
  94. tpl = &template.Template{
  95. ID: id,
  96. State: int8(template.StateDel),
  97. MTime: timex.Time(now.Unix()),
  98. }
  99. _, err = s.tpl.DelTemplate(c, mid, tpl)
  100. return
  101. }