PostRepository.php 28 KB

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