mysql_list.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "sort"
  7. "sync"
  8. "time"
  9. "go-common/app/interface/openplatform/article/model"
  10. xsql "go-common/library/database/sql"
  11. "go-common/library/log"
  12. "go-common/library/sync/errgroup"
  13. xtime "go-common/library/time"
  14. "go-common/library/xstr"
  15. )
  16. const (
  17. _creativeCountArticlesSQL = "SELECT count(*) FROM articles WHERE mid = ? and deleted_time = 0 AND category_id in (%s)"
  18. _creativeListsSQL = "SELECT id, image_url, name, update_time, ctime, summary, publish_time, words FROM lists WHERE deleted_time = 0 AND mid = ?"
  19. _creativeListAddSQL = "INSERT INTO lists(name, image_url, summary, publish_time, words, mid) VALUES(?,?,?,?,?,?)"
  20. _creativeListDelSQL = "update lists SET deleted_time = ? WHERE id = ? AND deleted_time = 0"
  21. _creativeListUpdateSQL = "update lists SET name = ?, image_url = ?, summary = ?, words = ?, publish_time = ? where id = ? and deleted_time = 0"
  22. _creativeListUpdateTimeSQL = "update lists SET update_time = ? where id = ? and update_time < ? and deleted_time = 0"
  23. _creativeCategoryArticlesSQL = "SELECT id, title, publish_time, state FROM articles WHERE mid = ? AND deleted_time = 0"
  24. _creativeListArticlesSQL = "SELECT article_id, position FROM article_lists WHERE list_id = ? AND deleted_time = 0 ORDER BY position ASC"
  25. _creativeListsArticlesSQL = "SELECT article_id, position, list_id FROM article_lists WHERE list_id in (%s) AND deleted_time = 0"
  26. _creativeListAddArticleSQL = "INSERT INTO article_lists(article_id, list_id, position) values(?,?,?) ON DUPLICATE KEY UPDATE deleted_time =0, position=?"
  27. _creativeListDelArticleSQL = "UPDATE article_lists SET deleted_time = ? WHERE article_id = ? and list_id = ? and deleted_time = 0"
  28. _creativeDelArticleListSQL = "UPDATE article_lists SET deleted_time = ? WHERE article_id = ? and deleted_time = 0"
  29. _creativeListDelAllArticleSQL = "UPDATE article_lists SET deleted_time = ? WHERE list_id = ? and deleted_time = 0"
  30. _creativeArticlesSQL = "SELECT id, title, state, publish_time FROM articles WHERE id in (%s) and deleted_time = 0"
  31. _listSQL = "SELECT id, mid, image_url, name, update_time, ctime, summary, words, publish_time FROM lists WHERE deleted_time = 0 AND id = ?"
  32. _listsSQL = "SELECT id, mid, image_url, name, update_time, ctime, summary, words, publish_time FROM lists WHERE deleted_time = 0 AND id in (%s)"
  33. _allListsSQL = "SELECT id, mid, image_url, name, update_time, ctime, summary, words, publish_time FROM lists WHERE deleted_time = 0"
  34. _artslistSQL = "SELECT article_id, list_id FROM article_lists WHERE article_id IN (%s) AND deleted_time = 0"
  35. _allListsExSQL = "SELECT id, mid, image_url, name, update_time, ctime, summary, words, publish_time FROM lists WHERE deleted_time = 0 ORDER BY id DESC LIMIT ?, ?"
  36. )
  37. // CreativeUpLists get article lists
  38. func (d *Dao) CreativeUpLists(c context.Context, mid int64) (res []*model.List, err error) {
  39. rows, err := d.creativeListsStmt.Query(c, mid)
  40. if err != nil {
  41. PromError("db:up主文集")
  42. log.Errorv(c, log.KV("log", "CreativeUplists"), log.KV("error", err))
  43. return
  44. }
  45. defer rows.Close()
  46. for rows.Next() {
  47. var (
  48. t, ctime time.Time
  49. r = &model.List{Mid: mid}
  50. pt int64
  51. )
  52. if err = rows.Scan(&r.ID, &r.ImageURL, &r.Name, &t, &ctime, &r.Summary, &pt, &r.Words); err != nil {
  53. PromError("db:up主文集scan")
  54. log.Error("dao.CreativeUpLists.rows.Scan error(%+v)", err)
  55. return
  56. }
  57. if t.Unix() > 0 {
  58. r.UpdateTime = xtime.Time(t.Unix())
  59. }
  60. r.Ctime = xtime.Time(ctime.Unix())
  61. r.PublishTime = xtime.Time(pt)
  62. res = append(res, r)
  63. }
  64. err = rows.Err()
  65. promErrorCheck(err)
  66. return
  67. }
  68. // RawUpLists .
  69. func (d *Dao) RawUpLists(c context.Context, mid int64) (res []int64, err error) {
  70. lists, err := d.CreativeUpLists(c, mid)
  71. for _, list := range lists {
  72. res = append(res, list.ID)
  73. }
  74. return
  75. }
  76. // CreativeListUpdate update list
  77. func (d *Dao) CreativeListUpdate(c context.Context, id int64, name, imageURL, summary string, publishTime xtime.Time, words int64) (err error) {
  78. if _, err := d.creativeListUpdateStmt.Exec(c, name, imageURL, summary, words, int64(publishTime), id); err != nil {
  79. PromError("db:修改文集")
  80. log.Errorv(c, log.KV("dao.CreativeListUpdate.Exec", id), log.KV("name", name), log.KV("image_url", imageURL), log.KV("error", err), log.KV("summary", summary))
  81. }
  82. return
  83. }
  84. // CreativeListDelAllArticles del list
  85. func (d *Dao) CreativeListDelAllArticles(c context.Context, id int64) (err error) {
  86. if _, err := d.creativeListDelAllArticleStmt.Exec(c, time.Now(), id); err != nil {
  87. PromError("db:删除文集下的文章")
  88. log.Errorv(c, log.KV("dao.CreativeListDelAllArticles.Exec", id), log.KV("error", err))
  89. }
  90. return
  91. }
  92. // CreativeListDel del list
  93. func (d *Dao) CreativeListDel(c context.Context, id int64) (err error) {
  94. if _, err := d.creativeListDelStmt.Exec(c, time.Now().Unix(), id); err != nil {
  95. PromError("db:删除文集")
  96. log.Errorv(c, log.KV("dao.CreativeListDel.Exec", id), log.KV("error", err))
  97. }
  98. return
  99. }
  100. // CreativeListUpdateTime update list time
  101. func (d *Dao) CreativeListUpdateTime(c context.Context, id int64, t time.Time) (err error) {
  102. if _, err := d.creativeListUpdateTimeStmt.Exec(c, t, id, t); err != nil {
  103. PromError("db:修改文集更新时间")
  104. log.Errorv(c, log.KV("dao.CreativeListUpdateTime.Exec", id), log.KV("time", t), log.KV("error", err))
  105. }
  106. return
  107. }
  108. // CreativeListAdd add list
  109. func (d *Dao) CreativeListAdd(c context.Context, mid int64, name, imageURL, summary string, publishTime xtime.Time, words int64) (res int64, err error) {
  110. r, err := d.creativeListAddStmt.Exec(c, name, imageURL, summary, int64(publishTime), words, mid)
  111. if err != nil {
  112. PromError("db:增加文集")
  113. log.Errorv(c, log.KV("dao.CreativeListAdd.Exec", mid), log.KV("name", name), log.KV("image_url", imageURL), log.KV("error", err), log.KV(summary, summary))
  114. return
  115. }
  116. if res, err = r.LastInsertId(); err != nil {
  117. PromError("db:增加文集ID")
  118. log.Errorv(c, log.KV("log", "res.LastInsertId"), log.KV("error", err))
  119. }
  120. return
  121. }
  122. // CreativeCountArticles novel count
  123. func (d *Dao) CreativeCountArticles(c context.Context, mid int64, cids []int64) (res int64, err error) {
  124. s := fmt.Sprintf(_creativeCountArticlesSQL, xstr.JoinInts(cids))
  125. if err = d.articleDB.QueryRow(c, s, mid).Scan(&res); err != nil {
  126. if err == sql.ErrNoRows {
  127. err = nil
  128. return
  129. }
  130. PromError("db:小说计数")
  131. log.Errorv(c, log.KV("log", "dao.CreativeCountArticles"), log.KV("error", err))
  132. }
  133. return
  134. }
  135. // CreativeCategoryArticles can add articles
  136. func (d *Dao) CreativeCategoryArticles(c context.Context, mid int64) (res []*model.ListArtMeta, err error) {
  137. rows, err := d.articleDB.Query(c, _creativeCategoryArticlesSQL, mid)
  138. if err != nil {
  139. PromError("db:up主可被加入文集的文章列表")
  140. log.Errorv(c, log.KV("log", "CreativeCategoryArticles"), log.KV("error", err))
  141. return
  142. }
  143. defer rows.Close()
  144. for rows.Next() {
  145. var (
  146. t int64
  147. m = &model.ListArtMeta{}
  148. )
  149. if err = rows.Scan(&m.ID, &m.Title, &t, &m.State); err != nil {
  150. PromError("db:up主可被加入文集的文章列表scan")
  151. log.Errorv(c, log.KV("log", "CreativeCategoryArticles"), log.KV("error", err))
  152. return
  153. }
  154. m.PublishTime = xtime.Time(t)
  155. res = append(res, m)
  156. }
  157. err = rows.Err()
  158. promErrorCheck(err)
  159. return
  160. }
  161. // CreativeListArticles .
  162. func (d *Dao) CreativeListArticles(c context.Context, listID int64) (res []*model.ListArtMeta, err error) {
  163. rows, err := d.creativeListArticlesStmt.Query(c, listID)
  164. if err != nil {
  165. PromError("db:文集的文章列表")
  166. log.Errorv(c, log.KV("log", "CreativeListArticles"), log.KV("error", err))
  167. return
  168. }
  169. defer rows.Close()
  170. for rows.Next() {
  171. var (
  172. r = &model.ListArtMeta{}
  173. )
  174. if err = rows.Scan(&r.ID, &r.Position); err != nil {
  175. PromError("db:文集的文章列表scan")
  176. log.Errorv(c, log.KV("log", "CreativeListArticles"), log.KV("error", err))
  177. return
  178. }
  179. res = append(res, r)
  180. }
  181. err = rows.Err()
  182. promErrorCheck(err)
  183. return
  184. }
  185. // CreativeListsArticles .
  186. func (d *Dao) CreativeListsArticles(c context.Context, listIDs []int64) (res map[int64][]*model.ListArtMeta, err error) {
  187. if len(listIDs) == 0 {
  188. return
  189. }
  190. s := fmt.Sprintf(_creativeListsArticlesSQL, xstr.JoinInts(listIDs))
  191. rows, err := d.articleDB.Query(c, s)
  192. if err != nil {
  193. PromError("db:多个文集的文章列表")
  194. log.Errorv(c, log.KV("log", "CreativeListsArticles"), log.KV("error", err))
  195. return
  196. }
  197. defer rows.Close()
  198. for rows.Next() {
  199. var (
  200. r = &model.ListArtMeta{}
  201. lid int64
  202. )
  203. if err = rows.Scan(&r.ID, &r.Position, &lid); err != nil {
  204. PromError("db:多个文集的文章列表scan")
  205. log.Errorv(c, log.KV("log", "CreativeListsArticles"), log.KV("error", err))
  206. return
  207. }
  208. if res == nil {
  209. res = make(map[int64][]*model.ListArtMeta)
  210. }
  211. res[lid] = append(res[lid], r)
  212. }
  213. for lid, arts := range res {
  214. sort.Slice(arts, func(i, j int) bool { return arts[i].Position < arts[j].Position })
  215. res[lid] = arts
  216. }
  217. err = rows.Err()
  218. promErrorCheck(err)
  219. return
  220. }
  221. // CreativeArticles get up all state articles
  222. func (d *Dao) CreativeArticles(c context.Context, aids []int64) (res map[int64]*model.ListArtMeta, err error) {
  223. var (
  224. group, errCtx = errgroup.WithContext(c)
  225. mutex = &sync.Mutex{}
  226. )
  227. if len(aids) == 0 {
  228. return
  229. }
  230. res = make(map[int64]*model.ListArtMeta)
  231. keysLen := len(aids)
  232. for i := 0; i < keysLen; i += _mysqlBulkSize {
  233. var keys []int64
  234. if (i + _mysqlBulkSize) > keysLen {
  235. keys = aids[i:]
  236. } else {
  237. keys = aids[i : i+_mysqlBulkSize]
  238. }
  239. group.Go(func() (err error) {
  240. var rows *xsql.Rows
  241. metasSQL := fmt.Sprintf(_creativeArticlesSQL, xstr.JoinInts(keys))
  242. if rows, err = d.articleDB.Query(errCtx, metasSQL); err != nil {
  243. PromError("db:CreativeArticles")
  244. log.Errorv(c, log.KV("log", "CreativeArticles"), log.KV("error", err))
  245. return
  246. }
  247. defer rows.Close()
  248. for rows.Next() {
  249. var (
  250. t int64
  251. a = &model.ListArtMeta{}
  252. )
  253. err = rows.Scan(&a.ID, &a.Title, &a.State, &t)
  254. if err != nil {
  255. return
  256. }
  257. a.PublishTime = xtime.Time(t)
  258. mutex.Lock()
  259. res[a.ID] = a
  260. mutex.Unlock()
  261. }
  262. err = rows.Err()
  263. return err
  264. })
  265. }
  266. if err = group.Wait(); err != nil {
  267. PromError("db:CreativeArticles")
  268. log.Errorv(c, log.KV("error", err))
  269. return
  270. }
  271. if len(res) == 0 {
  272. res = nil
  273. }
  274. return
  275. }
  276. // RawList get list from db
  277. func (d *Dao) RawList(c context.Context, id int64) (res *model.List, err error) {
  278. var (
  279. t, ctime time.Time
  280. pt int64
  281. )
  282. res = &model.List{ID: id}
  283. if err = d.listStmt.QueryRow(c, id).Scan(&res.ID, &res.Mid, &res.ImageURL, &res.Name, &t, &ctime, &res.Summary, &res.Words, &pt); err != nil {
  284. if err == sql.ErrNoRows {
  285. res = nil
  286. err = nil
  287. return
  288. }
  289. PromError("db文集")
  290. log.Errorv(c, log.KV("log", err))
  291. }
  292. if t.Unix() > 0 {
  293. res.UpdateTime = xtime.Time(t.Unix())
  294. }
  295. res.Ctime = xtime.Time(ctime.Unix())
  296. res.PublishTime = xtime.Time(pt)
  297. return
  298. }
  299. // TxAddListArticle tx add list article
  300. func (d *Dao) TxAddListArticle(c context.Context, tx *xsql.Tx, listID int64, aid int64, position int) (err error) {
  301. if _, err = tx.Exec(_creativeListAddArticleSQL, aid, listID, position, position); err != nil {
  302. PromError("db:新增文集文章tx")
  303. log.Error("tx.Exec() error(%+v)", err)
  304. return
  305. }
  306. return
  307. }
  308. // TxDelListArticle tx del list article
  309. func (d *Dao) TxDelListArticle(c context.Context, tx *xsql.Tx, listID int64, aid int64) (err error) {
  310. t := time.Now().Unix()
  311. if _, err = tx.Exec(_creativeListDelArticleSQL, t, aid, listID); err != nil {
  312. PromError("db:删除文集文章tx")
  313. log.Error("tx.Exec() error(%+v)", err)
  314. return
  315. }
  316. return
  317. }
  318. // TxDelArticleList .
  319. func (d *Dao) TxDelArticleList(tx *xsql.Tx, aid int64) (err error) {
  320. t := time.Now().Unix()
  321. if _, err = tx.Exec(_creativeDelArticleListSQL, t, aid); err != nil {
  322. PromError("db:tx删除文集文章")
  323. log.Error("tx.Exec() error(%+v)", err)
  324. return
  325. }
  326. return
  327. }
  328. // RawLists get lists from db
  329. func (d *Dao) RawLists(c context.Context, ids []int64) (res map[int64]*model.List, err error) {
  330. s := fmt.Sprintf(_listsSQL, xstr.JoinInts(ids))
  331. rows, err := d.articleDB.Query(c, s)
  332. if err != nil {
  333. PromError("db:文集列表")
  334. log.Errorv(c, log.KV("log", "Lists"), log.KV("error", err))
  335. return
  336. }
  337. defer rows.Close()
  338. for rows.Next() {
  339. var (
  340. t, ctime time.Time
  341. r = &model.List{}
  342. pt int64
  343. )
  344. if err = rows.Scan(&r.ID, &r.Mid, &r.ImageURL, &r.Name, &t, &ctime, &r.Summary, &r.Words, &pt); err != nil {
  345. PromError("db:文集列表scan")
  346. log.Error("dao.Lists.rows.Scan error(%+v)", err)
  347. return
  348. }
  349. if t.Unix() > 0 {
  350. r.UpdateTime = xtime.Time(t.Unix())
  351. }
  352. r.Ctime = xtime.Time(ctime.Unix())
  353. r.PublishTime = xtime.Time(pt)
  354. if res == nil {
  355. res = make(map[int64]*model.List)
  356. }
  357. res[r.ID] = r
  358. }
  359. err = rows.Err()
  360. promErrorCheck(err)
  361. return
  362. }
  363. // RawAllLists get lists from db
  364. func (d *Dao) RawAllLists(c context.Context) (res []*model.List, err error) {
  365. rows, err := d.allListStmt.Query(c)
  366. if err != nil {
  367. PromError("db:全部文集列表")
  368. log.Errorv(c, log.KV("log", "AllLists"), log.KV("error", err))
  369. return
  370. }
  371. defer rows.Close()
  372. for rows.Next() {
  373. var (
  374. t, ctime time.Time
  375. r = &model.List{}
  376. pt int64
  377. )
  378. if err = rows.Scan(&r.ID, &r.Mid, &r.ImageURL, &r.Name, &t, &ctime, &r.Summary, &r.Words, &pt); err != nil {
  379. PromError("db:全部文集列表scan")
  380. log.Error("dao.AllLists.rows.Scan error(%+v)", err)
  381. return
  382. }
  383. if t.Unix() > 0 {
  384. r.UpdateTime = xtime.Time(t.Unix())
  385. }
  386. r.Ctime = xtime.Time(ctime.Unix())
  387. r.PublishTime = xtime.Time(pt)
  388. res = append(res, r)
  389. }
  390. err = rows.Err()
  391. promErrorCheck(err)
  392. return
  393. }
  394. // RawAllListsEx get lists from db
  395. func (d *Dao) RawAllListsEx(c context.Context, start int, size int) (res []*model.List, err error) {
  396. rows, err := d.articleDB.Query(c, _allListsExSQL, start, size)
  397. if err != nil {
  398. PromError("db:全部文集列表")
  399. log.Errorv(c, log.KV("log", "AllLists"), log.KV("error", err))
  400. return
  401. }
  402. defer rows.Close()
  403. for rows.Next() {
  404. var (
  405. t, ctime time.Time
  406. r = &model.List{}
  407. pt int64
  408. )
  409. if err = rows.Scan(&r.ID, &r.Mid, &r.ImageURL, &r.Name, &t, &ctime, &r.Summary, &r.Words, &pt); err != nil {
  410. PromError("db:全部文集列表scan")
  411. log.Error("dao.AllLists.rows.Scan error(%+v)", err)
  412. return
  413. }
  414. if t.Unix() > 0 {
  415. r.UpdateTime = xtime.Time(t.Unix())
  416. }
  417. r.Ctime = xtime.Time(ctime.Unix())
  418. r.PublishTime = xtime.Time(pt)
  419. res = append(res, r)
  420. }
  421. err = rows.Err()
  422. promErrorCheck(err)
  423. return
  424. }
  425. // RawArtsListID get articles list from db
  426. func (d *Dao) RawArtsListID(c context.Context, aids []int64) (res map[int64]int64, err error) {
  427. s := fmt.Sprintf(_artslistSQL, xstr.JoinInts(aids))
  428. rows, err := d.articleDB.Query(c, s)
  429. if err != nil {
  430. PromError("db:文章所属文集")
  431. log.Errorv(c, log.KV("log", "ArtsList"), log.KV("error", err))
  432. return
  433. }
  434. defer rows.Close()
  435. for rows.Next() {
  436. var (
  437. aid, listID int64
  438. )
  439. if err = rows.Scan(&aid, &listID); err != nil {
  440. PromError("db:文章所属文集scan")
  441. log.Error("dao.ArtsList.rows.Scan error(%+v)", err)
  442. return
  443. }
  444. if res == nil {
  445. res = make(map[int64]int64)
  446. }
  447. res[aid] = listID
  448. }
  449. err = rows.Err()
  450. promErrorCheck(err)
  451. return
  452. }
  453. // AddListArticle add list article
  454. func (d *Dao) AddListArticle(c context.Context, listID int64, aid int64, position int) (err error) {
  455. if _, err = d.creativeListAddArticleStmt.Exec(c, aid, listID, position, position); err != nil {
  456. PromError("db:新增文集文章")
  457. log.Error("d.creativeListAddArticleStmt(list: %v, aid: %v, position: %v) error(%+v)", listID, aid, position, err)
  458. }
  459. return
  460. }
  461. // DelListArticle delete list article
  462. func (d *Dao) DelListArticle(c context.Context, listID int64, aid int64) (err error) {
  463. if _, err = d.creativeListDelArticleStmt.Exec(c, time.Now().Unix(), aid, listID); err != nil {
  464. PromError("db:删除文集文章")
  465. log.Error("d.DelListArticle(list: %v, aid: %v) error(%+v)", listID, aid, err)
  466. }
  467. return
  468. }
  469. // RawListArts .
  470. func (d *Dao) RawListArts(c context.Context, listID int64) (res []*model.ListArtMeta, err error) {
  471. if listID <= 0 {
  472. return
  473. }
  474. arts, err := d.CreativeListArticles(c, listID)
  475. if err != nil {
  476. return
  477. }
  478. var ids []int64
  479. for _, art := range arts {
  480. ids = append(ids, art.ID)
  481. }
  482. metas, err := d.ArticleMetas(c, ids)
  483. if err != nil {
  484. return
  485. }
  486. for _, id := range ids {
  487. if metas[id] != nil {
  488. res = append(res, &model.ListArtMeta{
  489. ID: id,
  490. Title: metas[id].Title,
  491. PublishTime: metas[id].PublishTime,
  492. Words: metas[id].Words,
  493. ImageURLs: metas[id].ImageURLs,
  494. Category: metas[id].Category,
  495. Categories: metas[id].Categories,
  496. Summary: metas[id].Summary,
  497. })
  498. }
  499. }
  500. return
  501. }
  502. // RawListsArts .
  503. func (d *Dao) RawListsArts(c context.Context, ids []int64) (res map[int64][]*model.ListArtMeta, err error) {
  504. if len(ids) == 0 {
  505. return
  506. }
  507. for _, id := range ids {
  508. lists, err := d.RawListArts(c, id)
  509. if err != nil {
  510. return nil, err
  511. }
  512. if res == nil {
  513. res = make(map[int64][]*model.ListArtMeta)
  514. }
  515. res[id] = lists
  516. }
  517. return
  518. }
  519. // ArtsList get article's read list
  520. func (d *Dao) ArtsList(c context.Context, aids []int64) (res map[int64]*model.List, err error) {
  521. if len(aids) == 0 {
  522. return
  523. }
  524. arts, err := d.ArtsListID(c, aids)
  525. if err != nil {
  526. return
  527. }
  528. listsMap := make(map[int64]bool)
  529. for _, list := range arts {
  530. listsMap[list] = true
  531. }
  532. var lids []int64
  533. for l := range listsMap {
  534. lids = append(lids, l)
  535. }
  536. lists, err := d.Lists(c, lids)
  537. if err != nil {
  538. return
  539. }
  540. res = make(map[int64]*model.List)
  541. for aid, lid := range arts {
  542. if lists[lid] != nil {
  543. res[aid] = lists[lid]
  544. }
  545. }
  546. return
  547. }
  548. // ArtList article list
  549. func (d *Dao) ArtList(c context.Context, aid int64) (res *model.List, err error) {
  550. if aid <= 0 {
  551. return
  552. }
  553. lists, err := d.ArtsList(c, []int64{aid})
  554. res = lists[aid]
  555. return
  556. }
  557. // RawListReadCount .
  558. func (d *Dao) RawListReadCount(c context.Context, id int64) (res int64, err error) {
  559. metas, err := d.RawListArts(c, id)
  560. if err != nil {
  561. return
  562. }
  563. var ids []int64
  564. for _, meta := range metas {
  565. if meta.IsNormal() {
  566. ids = append(ids, meta.ID)
  567. }
  568. }
  569. // get stats
  570. stats, err := d.ArticlesStats(c, ids)
  571. if err != nil {
  572. return
  573. }
  574. for _, aid := range ids {
  575. if stats[aid] != nil {
  576. res += stats[aid].View
  577. }
  578. }
  579. return
  580. }