mysql_draft.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "time"
  8. artmdl "go-common/app/interface/openplatform/article/model"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. xtime "go-common/library/time"
  13. )
  14. const (
  15. _addArticleDraftSQL = "REPLACE INTO article_draft_%s (id,category_id,title,summary,banner_url,template_id,mid,reprint,image_urls,tags,content, dynamic_intro, origin_image_urls, list_id, media_id, spoiler) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  16. _articleDraftSQL = "SELECT id,category_id,title,summary,banner_url,template_id,mid,reprint,image_urls,tags,content,mtime,dynamic_intro,origin_image_urls, list_id, media_id, spoiler FROM article_draft_%s WHERE id=? AND deleted_time=0"
  17. _checkDraftSQL = "SELECT deleted_time FROM article_draft_%s WHERE id=?"
  18. _deleteArticleDraftSQL = "UPDATE article_draft_%s SET deleted_time=? WHERE id=?"
  19. _upperDraftsSQL = "SELECT id,category_id,title,summary,template_id,reprint,image_urls,tags,mtime,dynamic_intro,origin_image_urls, list_id FROM article_draft_%s WHERE mid=? AND deleted_time=0 " +
  20. "ORDER BY mtime DESC LIMIT ?,?"
  21. _countUpperDraftSQL = "SELECT COUNT(*) FROM article_draft_%s WHERE mid=? AND deleted_time=0"
  22. )
  23. // ArtDraft get draft by article_id
  24. func (d *Dao) ArtDraft(c context.Context, mid, aid int64) (res *artmdl.Draft, err error) {
  25. var (
  26. row *xsql.Row
  27. tags string
  28. imageURLs, originImageURLs string
  29. category = &artmdl.Category{}
  30. author = &artmdl.Author{}
  31. meta = &artmdl.Meta{Media: &artmdl.Media{}}
  32. mtime time.Time
  33. sqlStr = fmt.Sprintf(_articleDraftSQL, d.hit(mid))
  34. )
  35. res = &artmdl.Draft{Article: &artmdl.Article{}}
  36. row = d.articleDB.QueryRow(c, sqlStr, aid)
  37. if err = row.Scan(&meta.ID, &category.ID, &meta.Title, &meta.Summary, &meta.BannerURL, &meta.TemplateID, &author.Mid, &meta.Reprint, &imageURLs, &tags, &res.Content, &mtime, &meta.Dynamic, &originImageURLs, &res.ListID, &meta.Media.MediaID, &meta.Media.Spoiler); err != nil {
  38. if err == sql.ErrNoRows {
  39. res = nil
  40. err = nil
  41. return
  42. }
  43. PromError("db:读取草稿")
  44. log.Error("ArtDraft.row.Scan() error(%d,%d,%v)", mid, aid, err)
  45. return
  46. }
  47. meta.Category = category
  48. meta.Author = author
  49. meta.Mtime = xtime.Time(mtime.Unix())
  50. if imageURLs == "" {
  51. meta.ImageURLs = []string{}
  52. } else {
  53. meta.ImageURLs = strings.Split(imageURLs, ",")
  54. }
  55. if originImageURLs == "" {
  56. meta.OriginImageURLs = []string{}
  57. } else {
  58. meta.OriginImageURLs = strings.Split(originImageURLs, ",")
  59. }
  60. if tags == "" {
  61. res.Tags = []string{}
  62. } else {
  63. res.Tags = strings.Split(tags, ",")
  64. }
  65. res.Meta = meta
  66. return
  67. }
  68. // UpperDrafts batch get draft by mid.
  69. func (d *Dao) UpperDrafts(c context.Context, mid int64, start, ps int) (res []*artmdl.Draft, err error) {
  70. var (
  71. rows *xsql.Rows
  72. sqlStr = fmt.Sprintf(_upperDraftsSQL, d.hit(mid))
  73. )
  74. if rows, err = d.articleDB.Query(c, sqlStr, mid, start, ps); err != nil {
  75. PromError("db:读取草稿")
  76. log.Error("d.articleDB.Query(%d,%d,%d) error(%+v)", mid, start, ps, err)
  77. return
  78. }
  79. defer rows.Close()
  80. for rows.Next() {
  81. var (
  82. tags string
  83. imageURLs, originImageURLs string
  84. category = &artmdl.Category{}
  85. author = &artmdl.Author{}
  86. art = &artmdl.Draft{Article: &artmdl.Article{}}
  87. meta = &artmdl.Meta{}
  88. mtime time.Time
  89. listID int64
  90. )
  91. if err = rows.Scan(&meta.ID, &category.ID, &meta.Title, &meta.Summary, &meta.TemplateID, &meta.Reprint, &imageURLs, &tags, &mtime, &meta.Dynamic, &originImageURLs, &listID); err != nil {
  92. log.Error("UpperDrafts.row.Scan() error(%d,%d,%d,%v)", mid, start, ps, err)
  93. return
  94. }
  95. meta.Category = category
  96. meta.Author = author
  97. meta.Mtime = xtime.Time(mtime.Unix())
  98. if imageURLs == "" {
  99. meta.ImageURLs = []string{}
  100. } else {
  101. meta.ImageURLs = strings.Split(imageURLs, ",")
  102. }
  103. if originImageURLs == "" {
  104. meta.OriginImageURLs = []string{}
  105. } else {
  106. meta.OriginImageURLs = strings.Split(originImageURLs, ",")
  107. }
  108. if tags == "" {
  109. art.Tags = []string{}
  110. } else {
  111. art.Tags = strings.Split(tags, ",")
  112. }
  113. art.Meta = meta
  114. art.ListID = listID
  115. res = append(res, art)
  116. }
  117. err = rows.Err()
  118. promErrorCheck(err)
  119. return
  120. }
  121. // AddArtDraft add article draft .
  122. func (d *Dao) AddArtDraft(c context.Context, a *artmdl.Draft) (id int64, err error) {
  123. var (
  124. deleted bool
  125. res sql.Result
  126. tags = strings.Join(a.Tags, ",")
  127. imageUrls = strings.Join(a.ImageURLs, ",")
  128. originImageUrls = strings.Join(a.OriginImageURLs, ",")
  129. sqlStr = fmt.Sprintf(_addArticleDraftSQL, d.hit(a.Author.Mid))
  130. )
  131. if a.ID > 0 {
  132. if deleted, err = d.IsDraftDeleted(c, a.Author.Mid, a.ID); err != nil {
  133. return
  134. } else if deleted {
  135. err = ecode.ArtCreationDraftDeleted
  136. return
  137. }
  138. }
  139. if res, err = d.articleDB.Exec(c, sqlStr, a.ID, a.Category.ID, a.Title, a.Summary, a.BannerURL, a.TemplateID, a.Author.Mid, a.Reprint, imageUrls, tags, a.Content, a.Dynamic, originImageUrls, a.ListID, a.Media.MediaID, a.Media.Spoiler); err != nil {
  140. PromError("db:新增或更新草稿")
  141. log.Error("d.articleDB.Exec(%+v) error(%+v)", a, err)
  142. return
  143. }
  144. if id, err = res.LastInsertId(); err != nil {
  145. PromError("db:获取新增草稿ID")
  146. log.Error("res.LastInsertId() error(%+v)", err)
  147. }
  148. return
  149. }
  150. // IsDraftDeleted judges is draft has been deleted.
  151. func (d *Dao) IsDraftDeleted(c context.Context, mid, aid int64) (deleted bool, err error) {
  152. var (
  153. dt int
  154. sqlStr = fmt.Sprintf(_checkDraftSQL, d.hit(mid))
  155. )
  156. if err = d.articleDB.QueryRow(c, sqlStr, aid).Scan(&dt); err != nil {
  157. if err == sql.ErrNoRows {
  158. err = nil
  159. return
  160. }
  161. PromError("db:判断草稿是否被删除")
  162. log.Error("d.articleDB.QueryRow(%d,%d) error(%+v)", mid, aid, err)
  163. return
  164. }
  165. if dt > 0 {
  166. deleted = true
  167. }
  168. return
  169. }
  170. // TxDeleteArticleDraft deletes article draft via transaction.
  171. func (d *Dao) TxDeleteArticleDraft(c context.Context, tx *xsql.Tx, mid, aid int64) (err error) {
  172. var (
  173. now = time.Now().Unix()
  174. sqlStr = fmt.Sprintf(_deleteArticleDraftSQL, d.hit(mid))
  175. )
  176. if _, err = tx.Exec(sqlStr, now, aid); err != nil {
  177. PromError("db:删除草稿")
  178. log.Error("TxDeleteArticleDraft.Exec(%d,%d) error(%+v)", mid, aid, err)
  179. }
  180. return
  181. }
  182. // DelArtDraft deletes article draft.
  183. func (d *Dao) DelArtDraft(c context.Context, mid, aid int64) (err error) {
  184. var (
  185. now = time.Now().Unix()
  186. sqlStr = fmt.Sprintf(_deleteArticleDraftSQL, d.hit(mid))
  187. )
  188. if _, err = d.articleDB.Exec(c, sqlStr, now, aid); err != nil {
  189. PromError("db:删除草稿")
  190. log.Error("d.articleDB.Exec(%d,%d) error(%+v)", mid, aid, err)
  191. }
  192. return
  193. }
  194. // CountUpperDraft count upper's draft
  195. func (d *Dao) CountUpperDraft(c context.Context, mid int64) (count int, err error) {
  196. var sqlStr = fmt.Sprintf(_countUpperDraftSQL, d.hit(mid))
  197. if err = d.articleDB.QueryRow(c, sqlStr, mid).Scan(&count); err != nil {
  198. if err == sql.ErrNoRows {
  199. err = nil
  200. return
  201. }
  202. PromError("db:读取草稿计数")
  203. log.Error("CountUpperDraft.row.Scan() error(%d,%v)", mid, err)
  204. }
  205. return
  206. }