PostRepository.php 29 KB

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