SocialMessageListImport.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Models\HootsuiteHistory;
  4. use App\Http\Models\HootsuiteUser;
  5. use App\Http\Models\Site;
  6. use Illuminate\Support\Collection;
  7. use Maatwebsite\Excel\Concerns\Importable;
  8. use Maatwebsite\Excel\Concerns\ToCollection;
  9. class SocialMessageListImport implements ToCollection
  10. {
  11. use Importable;
  12. public function collection(Collection $collections)
  13. {
  14. $updateList = [];
  15. foreach ($collections as $key => $collection) {
  16. if ($key < 1) {
  17. continue;
  18. }
  19. $siteId = Site::query()->where('domain', trim($collection[0]))->value('id');
  20. if (!empty($siteId)) {
  21. $socialIds = [];
  22. $faceBookResult = strstr($collection[1], 'facebook');
  23. if (!empty($faceBookResult)) {
  24. $result = $this->getSocialId($siteId, 'FACEBOOKPAGE');
  25. if (!empty($result)) {
  26. $socialIds[] = $result;
  27. }
  28. }
  29. $linKeDinResult = strstr($collection[1], 'linkedin');
  30. if (!empty($linKeDinResult)) {
  31. $result = $this->getSocialId($siteId, 'LINKEDINCOMPANY');
  32. if (!empty($result)) {
  33. $socialIds[] = $result;
  34. }
  35. }
  36. $twitterResult = strstr($collection[1], 'twitter');
  37. if (!empty($twitterResult)) {
  38. $result = $this->getSocialId($siteId, 'TWITTER');
  39. if (!empty($result)) {
  40. $socialIds[] = $result;
  41. }
  42. }
  43. $pinResult = strstr($collection[1], 'pin') ?? 0;
  44. if (!empty($pinResult)) {
  45. $result = $this->getSocialId($siteId, 'PINTEREST');
  46. if (!empty($result)) {
  47. $socialIds[] = $result;
  48. }
  49. }
  50. $t = $collection[3];
  51. $n = intval(($t - 25569) * 3600 * 24);
  52. $date = gmdate('Y-m-d H:i:s', $n);
  53. $lens = mb_strlen($collection[2]);
  54. if ($lens > 1000) {
  55. $content = mb_substr($collection[2], 0, 997 - $lens) . '...';
  56. } else {
  57. $content = $collection[2];
  58. }
  59. $array = [
  60. 'site_id' => $siteId,
  61. 'remote_content_id' => 0,
  62. 'content' => $content ?? $collection[2],
  63. 'url' => $collection[1],
  64. 'publish_at' => $date,
  65. 'social_ids' => \GuzzleHttp\json_encode($socialIds),
  66. 'type' => 1,
  67. 'created_at' => date('Y-m-d H:i:s'),
  68. ];
  69. $updateList[] = $array;
  70. }
  71. }
  72. HootsuiteHistory::query()->insert($updateList);
  73. }
  74. public function getSocialId($siteId, $type)
  75. {
  76. $hootSuiteUser = HootsuiteUser::query()->where(['site_id' => $siteId])->first();
  77. $socialProfiles = $hootSuiteUser->social_profiles ?? [];
  78. foreach ($socialProfiles as $kk => $vv) {
  79. if ($vv['type'] == $type) {
  80. return $vv['id'];
  81. }
  82. }
  83. return 0;
  84. }
  85. }