NewsService.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\data\service;
  3. use think\admin\Service;
  4. /**
  5. * 文章数据服务
  6. * Class NewsService
  7. * @package app\data\service
  8. */
  9. class NewsService extends Service
  10. {
  11. /**
  12. * 同步文章数据统计
  13. * @param string $code 文章编号
  14. * @throws \think\db\exception\DbException
  15. */
  16. public function syncNewsTotal(string $code): void
  17. {
  18. [$map, $total] = [['code' => $code], []];
  19. $query = $this->app->db->name('DataNewsXCollect')->field('count(1) count,type');
  20. $query->where($map)->group('type')->select()->map(function ($item) use (&$total) {
  21. $total[$item['type']] = $item['count'];
  22. });
  23. $this->app->db->name('DataNewsItem')->where(['code' => $code])->update([
  24. 'num_collect' => $total[2] ?? 0, 'num_like' => $total[1] ?? 0,
  25. 'num_comment' => $this->app->db->name('DataNewsXComment')->where($map)->count(),
  26. ]);
  27. }
  28. /**
  29. * 根据code绑定列表数据
  30. * @param array $list 数据列表
  31. * @return array
  32. */
  33. public function buildListByUidAndCode(array &$list = []): array
  34. {
  35. if (count($list) > 0) {
  36. /*! 绑定文章内容 */
  37. $codes = array_unique(array_column($list, 'code'));
  38. $colls = 'id,code,name,cover,mark,status,deleted,create_at,num_like,num_read,num_comment,num_collect';
  39. $items = $this->app->db->name('DataNewsItem')->whereIn('code', $codes)->column($colls, 'code');
  40. $marks = $this->app->db->name('DataNewsMark')->where(['status' => 1])->column('name');
  41. foreach ($items as &$vo) $vo['mark'] = str2arr($vo['mark'] ?: '', ',', $marks);
  42. foreach ($list as &$vo) $vo['record'] = $items[$vo['code']] ?? [];
  43. /*! 绑定用户数据 */
  44. $colls = 'id,phone,nickname,username,headimg,status';
  45. UserService::instance()->buildByUid($list, 'uid', 'user', $colls);
  46. }
  47. return $list;
  48. }
  49. /**
  50. * 获取列表状态
  51. * @param array $list 数据列表
  52. * @param integer $mid 用户UID
  53. * @return array
  54. */
  55. public function buildListState(array &$list, int $mid = 0): array
  56. {
  57. if (count($list) > 0) {
  58. [$code2, $code1, $marks] = [[], [], []];
  59. if ($mid > 0) {
  60. $map = [['uid', '=', $mid], ['code', 'in', array_unique(array_column($list, 'code'))]];
  61. $marks = $this->app->db->name('DataNewsMark')->where(['status' => 1])->column('name');
  62. $code1 = $this->app->db->name('DataNewsXCollect')->where($map)->where(['type' => 1])->column('code');
  63. $code2 = $this->app->db->name('DataNewsXCollect')->where($map)->where(['type' => 2])->column('code');
  64. }
  65. foreach ($list as &$vo) {
  66. $vo['mark'] = str2arr($vo['mark'] ?: '', ',', $marks);
  67. $vo['my_like_state'] = in_array($vo['code'], $code2) ? 1 : 0;
  68. $vo['my_coll_state'] = in_array($vo['code'], $code1) ? 1 : 0;
  69. }
  70. }
  71. return $list;
  72. }
  73. }