PostRepository.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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['data'] || $userInfo['data']['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['data']['username'],
  267. 'reply_uid' => 0,
  268. 'reply_username' => '',
  269. 'avatar' => $userInfo['data']['avatar'],
  270. 'content' => $request['content'],
  271. 'is_delete' => 0,
  272. ];
  273. $parentCommentContent = '';
  274. $parentCommentUid = 0;
  275. $parentCommentTime = '';
  276. if (isset($request['parent_id']) && $request['parent_id'] != 0) {
  277. $comment = $this->postComment->find($request['parent_id']);
  278. if (!$comment) {
  279. return Response::create([
  280. 'message' => '获取评论信息失败',
  281. 'status_code' => 500
  282. ]);
  283. }
  284. if ($comment->parent_id) {
  285. return Response::create([
  286. 'message' => '只能回复评论',
  287. 'status_code' => 500
  288. ]);
  289. }
  290. $data['parent_id'] = $request['parent_id'];
  291. $data['reply_uid'] = $comment->uid;
  292. $data['reply_username'] = $comment->username;
  293. $parentCommentContent = $comment->content;
  294. $parentCommentUid = $comment->uid;
  295. $parentCommentTime = Carbon::parse($comment->created_at)->toDateTimeString();
  296. }
  297. DB::beginTransaction();
  298. try {
  299. $comment = $this->postComment->create($data);
  300. $post->data->comment_count += 1;
  301. $post->data->save();
  302. DB::commit();
  303. $virus = $this->behavior->where('behavior_identification', 'comment')->first();
  304. if($virus){
  305. if($post->title){
  306. $desc = $post->title;
  307. }else{
  308. $desc = subtext(strip_tags($post->content), 20);
  309. }
  310. $this->rabbitMqUtil->push('virus_add', [
  311. 'behavior_id' => $virus->virus_behavior_id,
  312. 'behavior_flag' => 'comment',
  313. 'post_id' => $post->id,
  314. 'post_author_uid' => $post->uid,
  315. 'post_desc' => $desc,
  316. 'post_cover' => $post->img,
  317. 'comment_id' => $comment->id,
  318. 'comment_content' => $comment->content,
  319. 'parent_comment_id' => $comment->parent_id,
  320. 'parent_comment_content' => $parentCommentContent,
  321. 'parent_comment_uid' => $parentCommentUid,
  322. 'parent_comment_time' => $parentCommentTime,
  323. 'reply_uid' => $comment->reply_uid,
  324. 'reply_username' => $comment->reply_username,
  325. 'target_id' => $comment->uid,
  326. 'action_id' => $post->id,
  327. ]);
  328. }
  329. return Response::create();
  330. } catch (QueryException $exception) {
  331. DB::rollBack();
  332. Log::debug('评论内容:' . $request['post_id'] . $exception->getMessage());
  333. return Response::create([
  334. 'message' => '评论失败,请重试',
  335. 'error' => $exception->getMessage(),
  336. 'status_code' => 500
  337. ]);
  338. }
  339. }
  340. /**
  341. * 内容列表
  342. */
  343. public function lists($request)
  344. {
  345. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  346. $where = [];
  347. if (isset($request['content'])) {
  348. $where[] = ['content', 'like', "%{$request['content']}%"];
  349. }
  350. if (isset($request['is_suggest'])) {
  351. $where[] = ['is_suggest', $request['is_suggest']];
  352. }
  353. if (isset($request['type'])) {
  354. $where[] = ['type', $request['type']];
  355. }
  356. if (isset($request['uid'])) {
  357. $where[] = ['uid', $request['uid']];
  358. }
  359. $sort = 'post.id';
  360. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  361. $sort = $request['sort'];
  362. }
  363. $post = $this->post;
  364. if (isset($request['waste']) && $request['waste'] == 1) {
  365. $post = $post->onlyTrashed();
  366. }
  367. return $post
  368. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  369. ->select('post.*')
  370. ->where($where)
  371. ->where(function ($query) use ($request) {
  372. if (isset($request['keyword'])) {
  373. $query->where('uid', '=', $request['keyword'])
  374. ->orWhere('username', 'like', "%{$request['keyword']}%")
  375. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  376. }
  377. })
  378. ->where(function ($query) use ($request) {
  379. if (isset($request['created_at'])) {
  380. $time = explode('_', $request['created_at']);
  381. $query->whereBetween('post.created_at', $time);
  382. }
  383. })
  384. ->where(function ($query) use ($request) {
  385. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  386. $ids = [];
  387. if (isset($request['category_ids'])) {
  388. $categoryIds = explode('_', $request['category_ids']);
  389. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  390. }
  391. if (isset($request['topic_ids'])) {
  392. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  393. }
  394. foreach ($ids as $key => $id) {
  395. if ($key == 0) {
  396. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  397. } else {
  398. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  399. }
  400. }
  401. }
  402. })
  403. ->orderBy($sort, 'desc')
  404. ->paginate($perPage);
  405. }
  406. /**
  407. * 内容详情
  408. */
  409. public function detail($request)
  410. {
  411. return $this->post->withTrashed()->find($request['id']);
  412. }
  413. /**
  414. * 评论列表
  415. */
  416. public function commentList($request)
  417. {
  418. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  419. $where = [];
  420. if (isset($request['post_id'])) {
  421. $where[] = ['post_id', $request['post_id']];
  422. }
  423. if (isset($request['uid'])) {
  424. $where[] = ['uid', $request['uid']];
  425. }
  426. return $this->postComment
  427. ->where($where)
  428. ->orderBy('id', 'desc')
  429. ->paginate($perPage);
  430. }
  431. /**
  432. * 推荐内容
  433. */
  434. public function suggest($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. if ($post->is_suggest == 1) {
  444. $post->is_suggest = 0;
  445. } else {
  446. $post->is_suggest = 1;
  447. }
  448. DB::beginTransaction();
  449. try {
  450. $post->save();
  451. DB::commit();
  452. return Response::create();
  453. } catch (QueryException $exception) {
  454. DB::rollBack();
  455. Log::debug('推荐内容:' . $request['id'] . $exception->getMessage());
  456. return Response::create([
  457. 'message' => '操作失败,请重试',
  458. 'error' => $exception->getMessage(),
  459. 'status_code' => 500
  460. ]);
  461. }
  462. }
  463. /**
  464. * 删除内容
  465. */
  466. public function delete($request)
  467. {
  468. $post = $this->post->where('id', $request['id'])->first();
  469. if (!$post) {
  470. return Response::create([
  471. 'message' => '获取内容信息失败',
  472. 'status_code' => 500
  473. ]);
  474. }
  475. DB::beginTransaction();
  476. try {
  477. $post->delete();
  478. DB::commit();
  479. return Response::create();
  480. } catch (QueryException $exception) {
  481. DB::rollBack();
  482. Log::debug('删除内容:' . $request['id'] . $exception->getMessage());
  483. return Response::create([
  484. 'message' => '操作失败,请重试',
  485. 'error' => $exception->getMessage(),
  486. 'status_code' => 500
  487. ]);
  488. }
  489. }
  490. /**
  491. * 复原内容
  492. */
  493. public function restore($request)
  494. {
  495. $post = $this->post->withTrashed()->where('id', $request['id'])->first();
  496. if (!$post) {
  497. return Response::create([
  498. 'message' => '获取内容信息失败',
  499. 'status_code' => 500
  500. ]);
  501. }
  502. DB::beginTransaction();
  503. try {
  504. $post->restore();
  505. DB::commit();
  506. return Response::create();
  507. } catch (QueryException $exception) {
  508. DB::rollBack();
  509. Log::debug('复原内容:' . $request['id'] . $exception->getMessage());
  510. return Response::create([
  511. 'message' => '操作失败,请重试',
  512. 'error' => $exception->getMessage(),
  513. 'status_code' => 500
  514. ]);
  515. }
  516. }
  517. /**
  518. * 删除评论
  519. */
  520. public function commentDelete($request)
  521. {
  522. $comment = $this->postComment->find($request['id']);
  523. if (!$comment) {
  524. return Response::create([
  525. 'message' => '获取评论信息失败',
  526. 'status_code' => 500
  527. ]);
  528. }
  529. if ($comment->is_delete == 1) {
  530. return Response::create([
  531. 'message' => '该评论已经删除',
  532. 'status_code' => 500
  533. ]);
  534. }
  535. DB::beginTransaction();
  536. try {
  537. $comment->is_delete = 1;
  538. $comment->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 hide($request)
  555. {
  556. $post = $this->post->where('id', $request['id'])->first();
  557. if (!$post) {
  558. return Response::create([
  559. 'message' => '获取内容信息失败',
  560. 'status_code' => 500
  561. ]);
  562. }
  563. if ($post->is_hide == 1) {
  564. $post->is_hide = 0;
  565. } else {
  566. $post->is_hide = 1;
  567. }
  568. DB::beginTransaction();
  569. try {
  570. $post->save();
  571. DB::commit();
  572. return Response::create();
  573. } catch (QueryException $exception) {
  574. DB::rollBack();
  575. Log::debug('隐藏内容:' . $request['id'] . $exception->getMessage());
  576. return Response::create([
  577. 'message' => '操作失败,请重试',
  578. 'error' => $exception->getMessage(),
  579. 'status_code' => 500
  580. ]);
  581. }
  582. }
  583. /**
  584. * 日志列表
  585. */
  586. public function log($request)
  587. {
  588. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  589. $where = [];
  590. if (isset($request['log_type'])) {
  591. $where[] = ['log_type', $request['log_type']];
  592. }
  593. return $this->postLog
  594. ->where($where)
  595. ->where(function ($query) use ($request) {
  596. if (isset($request['created_at'])) {
  597. $time = explode('_', $request['created_at']);
  598. $query->whereBetween('created_at', $time);
  599. }
  600. })
  601. ->orderBy('id', 'desc')
  602. ->paginate($perPage);
  603. }
  604. public function download($filePath, $type, $request)
  605. {
  606. try {
  607. set_time_limit(0);
  608. if (!ini_get("auto_detect_line_endings")) {
  609. ini_set("auto_detect_line_endings", '1');
  610. }
  611. // 文件路径
  612. $writer = Writer::createFromPath(public_path($filePath), 'w+');
  613. // 设置标题
  614. if ($type == 'post') {
  615. $title = [
  616. '内容', date('Y年m月d日')
  617. ];
  618. } else {
  619. $title = [
  620. '回收站内容', date('Y年m月d日')
  621. ];
  622. }
  623. $title = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($title, true) . ';'));
  624. $writer->insertone($title);
  625. // 内容
  626. if ($type == 'post') {
  627. $header = [
  628. '内容ID', '发布时间', '用户昵称', '城市', '内容标签', '内容前20个字',
  629. '真实浏览量', '总浏览量', '真实点赞数', '总赞数', '真实分享数', '总分享数',
  630. '真实收藏数', '总收藏数', '评论数'
  631. ];
  632. } else {
  633. $header = [
  634. '内容ID', '发布时间', '用户昵称', '内容标签', '内容前20个字',
  635. '真实浏览量', '真实点赞数', '真实点赞数', '真实分享数', '真实收藏数', '评论数'
  636. ];
  637. }
  638. $header = eval('return ' . iconv('utf-8', 'gbk//IGNORE', var_export($header, true) . ';'));
  639. // $writer->setOutputBOM(Reader::BOM_UTF8);
  640. $writer->insertone($header);
  641. $where = [];
  642. if (isset($request['content'])) {
  643. $where[] = ['content', 'like', "%{$request['content']}%"];
  644. }
  645. if (isset($request['is_suggest'])) {
  646. $where[] = ['is_suggest', $request['is_suggest']];
  647. }
  648. if (isset($request['type'])) {
  649. $where[] = ['type', $request['type']];
  650. }
  651. $sort = 'post.id';
  652. if (isset($request['sort']) && in_array($request['sort'], ['praise_count', 'share_count', 'pv', 'comment_count', 'create_bean'])) {
  653. $sort = $request['sort'];
  654. }
  655. $post = $this->post;
  656. if ($type == 'post_waste') {
  657. $post = $post->onlyTrashed();
  658. }
  659. $post->join('post_data', 'post_data.post_id', '=', 'post.id')
  660. ->select('post.*')
  661. ->where($where)
  662. ->where(function ($query) use ($request) {
  663. if (isset($request['keyword'])) {
  664. $query->where('uid', '=', $request['keyword'])
  665. ->orWhere('username', 'like', "%{$request['keyword']}%")
  666. ->orWhere('mobile', 'like', "%{$request['keyword']}%");
  667. }
  668. })
  669. ->where(function ($query) use ($request) {
  670. if (isset($request['created_at'])) {
  671. $time = explode('_', $request['created_at']);
  672. $query->whereBetween('post.created_at', $time);
  673. }
  674. })
  675. ->where(function ($query) use ($request) {
  676. if (isset($request['category_ids']) || isset($request['topic_ids'])) {
  677. $ids = [];
  678. if (isset($request['category_ids'])) {
  679. $categoryIds = explode('_', $request['category_ids']);
  680. $ids = $this->categoryTopic->whereIn('category_id', $categoryIds)->pluck('topic_id')->toArray();
  681. }
  682. if (isset($request['topic_ids'])) {
  683. $ids = array_merge($ids, explode('_', $request['topic_ids']));
  684. }
  685. Log::debug('话题ids:' . json_encode($ids));
  686. foreach ($ids as $key => $id) {
  687. if ($key == 0) {
  688. $query = $query->whereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  689. } else {
  690. $query = $query->orWhereRaw('FIND_IN_SET(' . $id . ',topic_ids)');
  691. }
  692. }
  693. }
  694. })
  695. ->orderBy($sort, 'desc')
  696. ->chunk(1, function ($posts) use ($writer, $type) {
  697. $data = [];
  698. foreach ($posts as $post) {
  699. if ($type == 'post') {
  700. $tmp = [
  701. $post->id,
  702. Carbon::parse($post->created_at)->toDateTimeString(),
  703. $post->username,
  704. $post->location,
  705. implode(' ', $post->topic()->toArray()),
  706. subtext($post->content, 20),
  707. $post->data->pv_real,
  708. $post->data->pv,
  709. $post->data->praise_real_count,
  710. $post->data->praise_count,
  711. $post->data->share_real_count,
  712. $post->data->share_count,
  713. $post->data->collect_real_count,
  714. $post->data->collect_count,
  715. $post->data->comment_count
  716. ];
  717. } else {
  718. $tmp = [
  719. $post->id,
  720. Carbon::parse($post->created_at)->toDateTimeString(),
  721. $post->username,
  722. Carbon::parse($post->created_at)->toDateTimeString(),
  723. subtext($post->content, 20),
  724. $post->data->pv_real,
  725. $post->data->praise_real_count,
  726. $post->data->share_real_count,
  727. $post->data->collect_real_count,
  728. $post->data->comment_count
  729. ];
  730. }
  731. foreach ($tmp as $key => $value) {
  732. $tmp[$key] = iconv('utf-8', 'gbk//IGNORE', $value);
  733. }
  734. $data[] = $tmp;
  735. }
  736. $writer->insertAll($data);
  737. });
  738. Log::channel('download')->info('内容导出成功!');
  739. } catch (CannotInsertRecord $e) {
  740. $e->getRecord();
  741. Log::channel('download')->info('内容导出失败!');
  742. }
  743. }
  744. /**
  745. * 统计社区内容
  746. * @param $start
  747. * @param $end
  748. * @return array
  749. */
  750. public function statistics($start, $end)
  751. {
  752. $result = $this->postStatistics
  753. ->where('created_at', '>=', $start)
  754. ->where('created_at', '<=', $end)
  755. ->get()->toArray();
  756. $stimestamp = strtotime($start);
  757. $etimestamp = strtotime($end);
  758. $days = ($etimestamp - $stimestamp) / 86400;
  759. $date = array();
  760. for ($i = 0; $i < $days; $i++) {
  761. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  762. }
  763. $totalRead = 0;
  764. $totalPost = 0;
  765. $totalShare = 0;
  766. $totalLike = 0;
  767. $totalCollect = 0;
  768. $totalComment = 0;
  769. $info = [];
  770. foreach ($date as $key => $value) {
  771. $info[$value] = [
  772. 'read' => 0,
  773. 'post' => 0,
  774. 'share' => 0,
  775. 'like' => 0,
  776. 'collect' => 0,
  777. 'comment' => 0,
  778. ];
  779. foreach ($result as $row) {
  780. if ($value == date('Y-m-d', strtotime($row['created_at']))) {
  781. $info[$value]['read'] = $row['read_count'];
  782. $info[$value]['post'] = $row['post_count'];
  783. $info[$value]['share'] = $row['share_count'];
  784. $info[$value]['like'] = $row['like_count'];
  785. $info[$value]['collect'] = $row['collect_count'];
  786. $info[$value]['comment'] = $row['comment_count'];
  787. $totalRead += $row['read_count'];
  788. $totalPost += $row['post_count'];
  789. $totalShare += $row['share_count'];
  790. $totalLike += $row['like_count'];
  791. $totalCollect += $row['collect_count'];
  792. $totalComment += $row['comment_count'];
  793. }
  794. }
  795. }
  796. $info['data']['total_read'] = $totalRead;
  797. $info['data']['total_post'] = $totalPost;
  798. $info['data']['total_share'] = $totalShare;
  799. $info['data']['total_like'] = $totalLike;
  800. $info['data']['total_collect'] = $totalCollect;
  801. $info['data']['total_comment'] = $totalComment;
  802. return $info;
  803. }
  804. }