PostRepository.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Post;
  9. use App\Models\Behavior;
  10. use App\Models\CategoryTopic;
  11. use App\Models\Post;
  12. use App\Models\PostComment;
  13. use App\Models\PostData;
  14. use App\Models\PostImgs;
  15. use App\Models\PostLog;
  16. use App\Models\PostStatistics;
  17. use App\Models\Topic;
  18. use App\Service\RabbitMqUtil;
  19. use App\Traits\PostTrait;
  20. use App\Traits\UserTrait;
  21. use Illuminate\Database\QueryException;
  22. use Dingo\Api\Http\Response;
  23. use Illuminate\Support\Carbon;
  24. use Illuminate\Support\Facades\DB;
  25. use Illuminate\Support\Facades\Log;
  26. use Illuminate\Support\Facades\Redis;
  27. use Symfony\Component\HttpKernel\Exception\HttpException;
  28. use Tymon\JWTAuth\Facades\JWTAuth;
  29. use League\Csv\Writer;
  30. use League\Csv\CannotInsertRecord;
  31. class PostRepository
  32. {
  33. use PostTrait;
  34. use UserTrait;
  35. public function __construct(Post $post,
  36. PostData $postData,
  37. PostComment $postComment,
  38. PostImgs $postImgs,
  39. PostLog $postLog,
  40. RabbitMqUtil $rabbitMqUtil,
  41. Behavior $behavior,
  42. CategoryTopic $categoryTopic,
  43. PostStatistics $postStatistics,
  44. Topic $topic)
  45. {
  46. $this->post = $post;
  47. $this->postData = $postData;
  48. $this->postComment = $postComment;
  49. $this->postImgs = $postImgs;
  50. $this->postLog = $postLog;
  51. $this->rabbitMqUtil = $rabbitMqUtil;
  52. $this->behavior = $behavior;
  53. $this->categoryTopic = $categoryTopic;
  54. $this->topic = $topic;
  55. $this->postStatistics = $postStatistics;
  56. }
  57. /**
  58. * 发布内容
  59. */
  60. public function create($request)
  61. {
  62. //验证小号
  63. $userInfo = $this->getUserInfo($request['uid']);
  64. if(!$userInfo || !$userInfo['data'] || $userInfo['data']['type'] != 1){
  65. return Response::create([
  66. 'message' => '所选小号信息有误',
  67. 'status_code' => 500
  68. ]);
  69. }
  70. //验证话题
  71. $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
  72. $topicCount = count($topicIdsArray);
  73. if ($topicCount == 0 || $topicCount > 5) {
  74. return Response::create([
  75. 'message' => '所选话题必须1-5个',
  76. 'status_code' => 500
  77. ]);
  78. }
  79. $topicIds = implode(',', $topicIdsArray);
  80. //验证内容字数
  81. $html = strip_tags($request['content']);
  82. if(mb_strlen($html,'UTF8') > 1000){
  83. return Response::create([
  84. 'message' => '所传内容不能超过1000字',
  85. 'status_code' => 500
  86. ]);
  87. }
  88. $data = [
  89. 'uid' => $request['uid'],
  90. 'username' => '暂无',
  91. 'mobile' => '13720758488',
  92. 'avatar' => 'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ep3asJn8emiat1MnPdviaPNroWY3f65y5ezkTAk2qtibv7ea9Ht9R2ahxr9bicY1DIj5vN5FibpDOwXegg/132',
  93. 'type' => $request['type'],
  94. 'img' => $request['img'],
  95. 'video' => $request['video'] ?? '',
  96. 'topic_ids' => $topicIds,
  97. 'title' => $request['title'] ?? '',
  98. 'content' => $request['content'],
  99. 'location' => $request['location'] ?? '',
  100. 'is_suggest' => $request['is_suggest'],
  101. 'is_hide' => 0
  102. ];
  103. $date = date('Y-m-d H:i:s');
  104. DB::beginTransaction();
  105. try {
  106. $post = $this->post->create($data);
  107. $this->postData->create([
  108. 'post_id' => $post->id,
  109. 'pv' => 0,
  110. 'pv_real' => 0,
  111. 'dislike_count' => 0,
  112. 'praise_count' => 0,
  113. 'praise_real_count' => 0,
  114. 'share_count' => 0,
  115. 'share_real_count' => 0,
  116. 'comment_count' => 0,
  117. 'collect_count' => 0,
  118. 'collect_real_count' => 0,
  119. 'available_bean' => $this->availableBean(),
  120. 'will_collect_bean' => rand(100, 200),
  121. 'collect_bean' => 0,
  122. 'weight' => 0
  123. ]);
  124. if (!empty($request['imgs']) && $request['type'] == 'image') {
  125. $imgData = [];
  126. foreach ($request['imgs'] as $img) {
  127. $imgData[] = [
  128. 'post_id' => $post->id,
  129. 'img' => $img,
  130. 'created_at' => $date,
  131. 'updated_at' => $date
  132. ];
  133. }
  134. $this->postImgs->insert($imgData);
  135. }
  136. DB::commit();
  137. Redis::zadd('post_trigger_type', 0, $post->id);
  138. foreach ($topicIdsArray as $id) {
  139. Redis::zincrby('topic.user_uid' . $request['uid'], 1, $id);
  140. }
  141. $virus = $this->behavior->where('behavior_identification', 'publish')->first();
  142. if($virus){
  143. if($post->title){
  144. $desc = $post->title;
  145. }else{
  146. $desc = subtext(strip_tags($post->content), 20);
  147. }
  148. $this->rabbitMqUtil->push('virus_add', [
  149. 'behavior_id' => $virus->virus_behavior_id,
  150. 'behavior_flag' => 'publish',
  151. 'post_id' => $post->id,
  152. 'post_desc' => $desc,
  153. 'post_cover' => $post->img,
  154. 'target_id' => $post->uid,
  155. 'action_id' => $post->id,
  156. ]);
  157. }
  158. return Response::create();
  159. } catch (QueryException $exception) {
  160. DB::rollBack();
  161. Log::debug('发布内容:' . $exception->getMessage());
  162. return Response::create([
  163. 'message' => '发布失败,请重试',
  164. 'error' => $exception->getMessage(),
  165. 'status_code' => 500
  166. ]);
  167. }
  168. }
  169. /**
  170. * 增加数据
  171. */
  172. public function addData($request)
  173. {
  174. $token = JWTAuth::decode(JWTAuth::getToken());
  175. if (!$token || $token['type'] != 1) {
  176. return Response::create([
  177. 'message' => '获取登陆信息失败',
  178. 'status_code' => 500
  179. ]);
  180. }
  181. $uid = $token['user']->id;
  182. $username = $token['user']->username;
  183. //验证小号数量
  184. $postData = $this->postData->where('post_id', $request['post_id'])->first();
  185. if (!$postData) {
  186. return Response::create([
  187. 'message' => '获取内容失败',
  188. 'status_code' => 500
  189. ]);
  190. }
  191. if ($request['add_pv'] == 0 && $request['add_praise_count'] == 0 && $request['add_collect_count'] == 0 && $request['add_share_count'] == 0) {
  192. return Response::create([
  193. 'message' => '增加数据不能同时为0',
  194. 'status_code' => 500
  195. ]);
  196. }
  197. $content = [
  198. 'add_pv' => 0,
  199. 'add_praise_count' => 0,
  200. 'add_collect_count' => 0,
  201. 'add_share_count' => 0,
  202. ];
  203. if ($request['add_pv']) {
  204. $postData->pv += $request['add_pv'];
  205. $content['add_pv'] = $request['add_pv'];
  206. }
  207. if ($request['add_praise_count']) {
  208. $postData->praise_count += $request['add_praise_count'];
  209. $content['add_praise_count'] = $request['add_praise_count'];
  210. }
  211. if ($request['add_collect_count']) {
  212. $postData->collect_count += $request['add_collect_count'];
  213. $content['add_collect_count'] = $request['add_collect_count'];
  214. }
  215. if ($request['add_share_count']) {
  216. $postData->share_count += $request['add_share_count'];
  217. $content['add_share_count'] = $request['add_share_count'];
  218. }
  219. DB::beginTransaction();
  220. try {
  221. $postData->save();
  222. $this->postLog->create([
  223. 'post_id' => $request['post_id'],
  224. 'uid' => $uid,
  225. 'username' => $username,
  226. 'log_type' => 'add_data',
  227. 'content' => json_encode($content)
  228. ]);
  229. DB::commit();
  230. return Response::create();
  231. } catch (QueryException $exception) {
  232. DB::rollBack();
  233. Log::debug('内容增加数据:' . $request['post_id'] . $exception->getMessage());
  234. return Response::create([
  235. 'message' => '增加数据失败,请重试',
  236. 'error' => $exception->getMessage(),
  237. 'status_code' => 500
  238. ]);
  239. }
  240. }
  241. /**
  242. * 评论&回复
  243. */
  244. public function comment($request)
  245. {
  246. //验证小号
  247. $userInfo = $this->getUserInfo($request['uid']);
  248. Log::debug('评论&回复小号' . json_encode($userInfo));
  249. if (!$userInfo || $userInfo['type'] != 1) {
  250. return Response::create([
  251. 'message' => '所选小号信息有误',
  252. 'status_code' => 500
  253. ]);
  254. }
  255. $post = $this->post->find($request['post_id']);
  256. if (!$post) {
  257. return Response::create([
  258. 'message' => '获取内容失败',
  259. 'status_code' => 500
  260. ]);
  261. }
  262. $data = [
  263. 'uid' => $request['uid'],
  264. 'post_id' => $request['post_id'],
  265. 'parent_id' => 0,
  266. 'username' => $userInfo['username'],
  267. 'reply_uid' => 0,
  268. 'reply_username' => '',
  269. 'avatar' => $userInfo['avatar'],
  270. 'content' => $request['content'],
  271. 'is_delete' => 0,
  272. ];
  273. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  274. $comment = $this->postComment->find($request['parent_id']);
  275. if (!$comment) {
  276. return Response::create([
  277. 'message' => '获取评论信息失败',
  278. 'status_code' => 500
  279. ]);
  280. }
  281. if ($comment->parent_id) {
  282. return Response::create([
  283. 'message' => '只能回复评论',
  284. 'status_code' => 500
  285. ]);
  286. }
  287. $data['parent_id'] = $request['parent_id'];
  288. $data['reply_uid'] = $comment->uid;
  289. $data['reply_username'] = $comment->username;
  290. }
  291. DB::beginTransaction();
  292. try {
  293. $this->postComment->create($data);
  294. $post->data->comment_count += 1;
  295. $post->data->save();
  296. DB::commit();
  297. return Response::create();
  298. } catch (QueryException $exception) {
  299. DB::rollBack();
  300. Log::debug('评论内容:' . $request['post_id'] . $exception->getMessage());
  301. return Response::create([
  302. 'message' => '评论失败,请重试',
  303. 'error' => $exception->getMessage(),
  304. 'status_code' => 500
  305. ]);
  306. }
  307. }
  308. /**
  309. * 内容列表
  310. */
  311. public function lists($request)
  312. {
  313. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  314. $where = [];
  315. if (isset($request['content'])) {
  316. $where[] = ['content', 'like', "%{$request['content']}%"];
  317. }
  318. if (isset($request['is_suggest'])) {
  319. $where[] = ['is_suggest', $request['is_suggest']];
  320. }
  321. if (isset($request['type'])) {
  322. $where[] = ['type', $request['type']];
  323. }
  324. if (isset($request['uid'])) {
  325. $where[] = ['uid', $request['uid']];
  326. }
  327. $sort = 'post.id';
  328. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  329. $sort = $request['sort'];
  330. }
  331. $post = $this->post;
  332. if (isset($request['waste']) && $request['waste'] == 1) {
  333. $post = $post->onlyTrashed();
  334. }
  335. return $post
  336. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  337. ->select('post.*')
  338. ->where($where)
  339. ->where(function ($query) use ($request) {
  340. if (isset($request['keyword'])) {
  341. $query->where('uid', '=', $request['keyword'])
  342. ->orWhere('username', 'like', "%{$request['keyword']}%")
  343. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  344. }
  345. })
  346. ->where(function ($query) use ($request) {
  347. if (isset($request['created_at'])) {
  348. $time = explode('_', $request['created_at']);
  349. $query->whereBetween('post.created_at', $time);
  350. }
  351. })
  352. ->where(function ($query) use ($request) {
  353. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  354. $ids = [];
  355. if (isset($request['category_ids'])) {
  356. $categoryIds = explode('_', $request['category_ids']);
  357. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  358. }
  359. if (isset($request['topic_ids'])) {
  360. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  361. }
  362. foreach ($ids as $key => $id) {
  363. if ($key == 0) {
  364. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  365. } else {
  366. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  367. }
  368. }
  369. }
  370. })
  371. ->orderBy($sort, 'desc')
  372. ->paginate($perPage);
  373. }
  374. /**
  375. * 内容详情
  376. */
  377. public function detail($request)
  378. {
  379. return $this->post->withTrashed()->find($request['id']);
  380. }
  381. /**
  382. * 评论列表
  383. */
  384. public function commentList($request)
  385. {
  386. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  387. $where = [];
  388. if (isset($request['post_id'])) {
  389. $where[] = ['post_id', $request['post_id']];
  390. }
  391. if (isset($request['uid'])) {
  392. $where[] = ['uid', $request['uid']];
  393. }
  394. return $this->postComment
  395. ->where($where)
  396. ->orderBy('id', 'desc')
  397. ->paginate($perPage);
  398. }
  399. /**
  400. * 推荐内容
  401. */
  402. public function suggest($request)
  403. {
  404. $post = $this->post->where('id', $request['id'])->first();
  405. if (!$post) {
  406. return Response::create([
  407. 'message' => '获取内容信息失败',
  408. 'status_code' => 500
  409. ]);
  410. }
  411. if ($post->is_suggest == 1) {
  412. $post->is_suggest = 0;
  413. } else {
  414. $post->is_suggest = 1;
  415. }
  416. DB::beginTransaction();
  417. try {
  418. $post->save();
  419. DB::commit();
  420. return Response::create();
  421. } catch (QueryException $exception) {
  422. DB::rollBack();
  423. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  424. return Response::create([
  425. 'message' => '操作失败,请重试',
  426. 'error' => $exception->getMessage(),
  427. 'status_code' => 500
  428. ]);
  429. }
  430. }
  431. /**
  432. * 删除内容
  433. */
  434. public function delete($request)
  435. {
  436. $post = $this->post->where('id', $request['id'])->first();
  437. if (!$post) {
  438. return Response::create([
  439. 'message' => '获取内容信息失败',
  440. 'status_code' => 500
  441. ]);
  442. }
  443. DB::beginTransaction();
  444. try {
  445. $post->delete();
  446. DB::commit();
  447. return Response::create();
  448. } catch (QueryException $exception) {
  449. DB::rollBack();
  450. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  451. return Response::create([
  452. 'message' => '操作失败,请重试',
  453. 'error' => $exception->getMessage(),
  454. 'status_code' => 500
  455. ]);
  456. }
  457. }
  458. /**
  459. * 复原内容
  460. */
  461. public function restore($request)
  462. {
  463. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  464. if (!$post) {
  465. return Response::create([
  466. 'message' => '获取内容信息失败',
  467. 'status_code' => 500
  468. ]);
  469. }
  470. DB::beginTransaction();
  471. try {
  472. $post->restore();
  473. DB::commit();
  474. return Response::create();
  475. } catch (QueryException $exception) {
  476. DB::rollBack();
  477. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  478. return Response::create([
  479. 'message' => '操作失败,请重试',
  480. 'error' => $exception->getMessage(),
  481. 'status_code' => 500
  482. ]);
  483. }
  484. }
  485. /**
  486. * 删除评论
  487. */
  488. public function commentDelete($request)
  489. {
  490. $comment = $this->postComment->find($request['id']);
  491. if (!$comment) {
  492. return Response::create([
  493. 'message' => '获取评论信息失败',
  494. 'status_code' => 500
  495. ]);
  496. }
  497. if ($comment->is_delete == 1) {
  498. return Response::create([
  499. 'message' => '该评论已经删除',
  500. 'status_code' => 500
  501. ]);
  502. }
  503. DB::beginTransaction();
  504. try {
  505. $comment->is_delete = 1;
  506. $comment->save();
  507. DB::commit();
  508. return Response::create();
  509. } catch (QueryException $exception) {
  510. DB::rollBack();
  511. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  512. return Response::create([
  513. 'message' => '操作失败,请重试',
  514. 'error' => $exception->getMessage(),
  515. 'status_code' => 500
  516. ]);
  517. }
  518. }
  519. /**
  520. * 隐藏内容
  521. */
  522. public function hide($request)
  523. {
  524. $post = $this->post->where('id', $request['id'])->first();
  525. if (!$post) {
  526. return Response::create([
  527. 'message' => '获取内容信息失败',
  528. 'status_code' => 500
  529. ]);
  530. }
  531. if ($post->is_hide == 1) {
  532. $post->is_hide = 0;
  533. } else {
  534. $post->is_hide = 1;
  535. }
  536. DB::beginTransaction();
  537. try {
  538. $post->save();
  539. DB::commit();
  540. return Response::create();
  541. } catch (QueryException $exception) {
  542. DB::rollBack();
  543. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  544. return Response::create([
  545. 'message' => '操作失败,请重试',
  546. 'error' => $exception->getMessage(),
  547. 'status_code' => 500
  548. ]);
  549. }
  550. }
  551. /**
  552. * 日志列表
  553. */
  554. public function log($request)
  555. {
  556. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  557. $where = [];
  558. if (isset($request['log_type'])) {
  559. $where[] = ['log_type', $request['log_type']];
  560. }
  561. return $this->postLog
  562. ->where($where)
  563. ->where(function ($query) use ($request) {
  564. if (isset($request['created_at'])) {
  565. $time = explode('_', $request['created_at']);
  566. $query->whereBetween('created_at', $time);
  567. }
  568. })
  569. ->orderBy('id', 'desc')
  570. ->paginate($perPage);
  571. }
  572. public function download($filePath, $type, $request)
  573. {
  574. try {
  575. set_time_limit(0);
  576. if (!ini_get("auto_detect_line_endings")) {
  577. ini_set("auto_detect_line_endings", '1');
  578. }
  579. // 文件路径
  580. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  581. // 设置标题
  582. if ($type == 'post') {
  583. $title = [
  584. '内容', date('Y年m月d日')
  585. ];
  586. } else {
  587. $title = [
  588. '回收站内容', date('Y年m月d日')
  589. ];
  590. }
  591. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  592. $writer->insertone($title);
  593. // 内容
  594. if ($type == 'post') {
  595. $header = [
  596. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  597. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  598. '真实收藏数', '总收藏数', '评论数'
  599. ];
  600. } else {
  601. $header = [
  602. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  603. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  604. ];
  605. }
  606. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  607. // $writer->setOutputBOM(Reader::BOM_UTF8);
  608. $writer->insertone($header);
  609. $where = [];
  610. if (isset($request['content'])) {
  611. $where[] = ['content', 'like', "%{$request['content']}%"];
  612. }
  613. if (isset($request['is_suggest'])) {
  614. $where[] = ['is_suggest', $request['is_suggest']];
  615. }
  616. if (isset($request['type'])) {
  617. $where[] = ['type', $request['type']];
  618. }
  619. $sort = 'post.id';
  620. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  621. $sort = $request['sort'];
  622. }
  623. $post = $this->post;
  624. if ($type == 'post_waste') {
  625. $post = $post->onlyTrashed();
  626. }
  627. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  628. ->select('post.*')
  629. ->where($where)
  630. ->where(function ($query) use ($request) {
  631. if (isset($request['keyword'])) {
  632. $query->where('uid', '=', $request['keyword'])
  633. ->orWhere('username', 'like', "%{$request['keyword']}%")
  634. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  635. }
  636. })
  637. ->where(function ($query) use ($request) {
  638. if (isset($request['created_at'])) {
  639. $time = explode('_', $request['created_at']);
  640. $query->whereBetween('post.created_at', $time);
  641. }
  642. })
  643. ->where(function ($query) use ($request) {
  644. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  645. $ids = [];
  646. if (isset($request['category_ids'])) {
  647. $categoryIds = explode('_', $request['category_ids']);
  648. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  649. }
  650. if (isset($request['topic_ids'])) {
  651. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  652. }
  653. Log::debug('话题ids:' . json_encode($ids));
  654. foreach ($ids as $key => $id) {
  655. if ($key == 0) {
  656. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  657. } else {
  658. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  659. }
  660. }
  661. }
  662. })
  663. ->orderBy($sort, 'desc')
  664. ->chunk(1, function ($posts) use ($writer, $type) {
  665. $data = [];
  666. foreach ($posts as $post) {
  667. if ($type == 'post') {
  668. $tmp = [
  669. $post->id,
  670. Carbon::parse($post->created_at)->toDateTimeString(),
  671. $post->username,
  672. $post->location,
  673. implode(' ', $post->topic()->toArray()),
  674. subtext($post->content, 20),
  675. $post->data->pv_real,
  676. $post->data->pv,
  677. $post->data->praise_real_count,
  678. $post->data->praise_count,
  679. $post->data->share_real_count,
  680. $post->data->share_count,
  681. $post->data->collect_real_count,
  682. $post->data->collect_count,
  683. $post->data->comment_count
  684. ];
  685. } else {
  686. $tmp = [
  687. $post->id,
  688. Carbon::parse($post->created_at)->toDateTimeString(),
  689. $post->username,
  690. Carbon::parse($post->created_at)->toDateTimeString(),
  691. subtext($post->content, 20),
  692. $post->data->pv_real,
  693. $post->data->praise_real_count,
  694. $post->data->share_real_count,
  695. $post->data->collect_real_count,
  696. $post->data->comment_count
  697. ];
  698. }
  699. foreach ($tmp as $key => $value) {
  700. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  701. }
  702. $data[] = $tmp;
  703. }
  704. $writer->insertAll($data);
  705. });
  706. Log::channel('download')->info('内容导出成功!');
  707. } catch (CannotInsertRecord $e) {
  708. $e->getRecord();
  709. Log::channel('download')->info('内容导出失败!');
  710. }
  711. }
  712. /**
  713. * 统计社区内容
  714. * @param $start
  715. * @param $end
  716. * @return array
  717. */
  718. public function statistics($start, $end)
  719. {
  720. $result = $this->postStatistics
  721. ->where('created_at', '>=', $start)
  722. ->where('created_at', '<=', $end)
  723. ->get()->toArray();
  724. $stimestamp = strtotime($start);
  725. $etimestamp = strtotime($end);
  726. $days = ($etimestamp - $stimestamp) / 86400;
  727. $date = array();
  728. for ($i = 0; $i < $days; $i++) {
  729. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  730. }
  731. $totalRead = 0;
  732. $totalPost = 0;
  733. $totalShare = 0;
  734. $totalLike = 0;
  735. $totalCollect = 0;
  736. $totalComment = 0;
  737. $info = [];
  738. foreach ($date as $key => $value) {
  739. $info[$value] = [
  740. 'read' => 0,
  741. 'post' => 0,
  742. 'share' => 0,
  743. 'like' => 0,
  744. 'collect' => 0,
  745. 'comment' => 0,
  746. ];
  747. foreach ($result as $row) {
  748. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  749. $info[$value]['read'] = $row['read_count'];
  750. $info[$value]['post'] = $row['post_count'];
  751. $info[$value]['share'] = $row['share_count'];
  752. $info[$value]['like'] = $row['like_count'];
  753. $info[$value]['collect'] = $row['collect_count'];
  754. $info[$value]['comment'] = $row['comment_count'];
  755. $totalRead += $row['read_count'];
  756. $totalPost += $row['post_count'];
  757. $totalShare += $row['share_count'];
  758. $totalLike += $row['like_count'];
  759. $totalCollect += $row['collect_count'];
  760. $totalComment += $row['comment_count'];
  761. }
  762. }
  763. }
  764. $info['data']['total_read'] = $totalRead;
  765. $info['data']['total_post'] = $totalPost;
  766. $info['data']['total_share'] = $totalShare;
  767. $info['data']['total_like'] = $totalLike;
  768. $info['data']['total_collect'] = $totalCollect;
  769. $info['data']['total_comment'] = $totalComment;
  770. return $info;
  771. }
  772. }