PostRepository.php 26 KB

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