NewsService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\data\service;
  3. use app\data\model\DataNewsItem;
  4. use app\data\model\DataNewsMark;
  5. use app\data\model\DataNewsXCollect;
  6. use think\admin\Service;
  7. /**
  8. * 文章数据服务
  9. * Class NewsService
  10. * @package app\data\service
  11. */
  12. class NewsService extends Service
  13. {
  14. /**
  15. * 同步文章数据统计
  16. * @param string $code 文章编号
  17. * @param array $total 查询统计
  18. */
  19. public static function syncNewsTotal(string $code, array $total = []): void
  20. {
  21. $query = DataNewsXCollect::mk()->field('type,count(1) count');
  22. foreach ($query->where(['code' => $code, 'status' => 2])->group('type')->cursor() as $item) {
  23. $total[$item['type']] = $item['count'];
  24. }
  25. DataNewsItem::mk()->where(['code' => $code])->update([
  26. 'num_like' => $total[1] ?? 0, 'num_collect' => $total[2] ?? 0, 'num_comment' => $total[4] ?? 0,
  27. ]);
  28. }
  29. /**
  30. * 根据code绑定列表数据
  31. * @param array $list 数据列表
  32. * @return array
  33. */
  34. public static function buildListByUidAndCode(array &$list = []): array
  35. {
  36. if (count($list) > 0) {
  37. /*! 绑定文章内容 */
  38. $colls = 'id,code,name,cover,mark,status,deleted,create_at,num_like,num_read,num_comment,num_collect';
  39. $items = DataNewsItem::mk()->whereIn('code', array_unique(array_column($list, 'code')))->column($colls, 'code');
  40. $marks = DataNewsMark::mk()->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. UserAdminService::buildByUid($list, 'uuid', 'user', $colls);
  46. }
  47. return $list;
  48. }
  49. /**
  50. * 获取列表状态
  51. * @param array $list 数据列表
  52. * @param integer $uuid 用户UID
  53. * @return array
  54. */
  55. public static function buildData(array &$list, int $uuid = 0): array
  56. {
  57. if (count($list) > 0) {
  58. [$code2, $code1] = [[], []];
  59. $marks = DataNewsMark::mk()->where(['status' => 1])->column('name');
  60. if ($uuid > 0) {
  61. $map = [['uuid', '=', $uuid], ['code', 'in', array_unique(array_column($list, 'code'))]];
  62. $code1 = DataNewsXCollect::mk()->where($map)->where(['type' => 1])->column('code');
  63. $code2 = DataNewsXCollect::mk()->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. }