123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Imports;
- use App\Http\Models\HootsuiteHistory;
- use App\Http\Models\HootsuiteUser;
- use App\Http\Models\Site;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\ToCollection;
- class SocialMessageListImport implements ToCollection
- {
- use Importable;
- public function collection(Collection $collections)
- {
- $updateList = [];
- foreach ($collections as $key => $collection) {
- if ($key < 1) {
- continue;
- }
- $siteId = Site::query()->where('domain', trim($collection[0]))->value('id');
- if (!empty($siteId)) {
- $socialIds = [];
- $faceBookResult = strstr($collection[1], 'facebook');
- if (!empty($faceBookResult)) {
- $result = $this->getSocialId($siteId, 'FACEBOOKPAGE');
- if (!empty($result)) {
- $socialIds[] = $result;
- }
- }
- $linKeDinResult = strstr($collection[1], 'linkedin');
- if (!empty($linKeDinResult)) {
- $result = $this->getSocialId($siteId, 'LINKEDINCOMPANY');
- if (!empty($result)) {
- $socialIds[] = $result;
- }
- }
- $twitterResult = strstr($collection[1], 'twitter');
- if (!empty($twitterResult)) {
- $result = $this->getSocialId($siteId, 'TWITTER');
- if (!empty($result)) {
- $socialIds[] = $result;
- }
- }
- $pinResult = strstr($collection[1], 'pin') ?? 0;
- if (!empty($pinResult)) {
- $result = $this->getSocialId($siteId, 'PINTEREST');
- if (!empty($result)) {
- $socialIds[] = $result;
- }
- }
- $t = $collection[3];
- $n = intval(($t - 25569) * 3600 * 24);
- $date = gmdate('Y-m-d H:i:s', $n);
- $lens = mb_strlen($collection[2]);
- if ($lens > 1000) {
- $content = mb_substr($collection[2], 0, 997 - $lens) . '...';
- } else {
- $content = $collection[2];
- }
- $array = [
- 'site_id' => $siteId,
- 'remote_content_id' => 0,
- 'content' => $content ?? $collection[2],
- 'url' => $collection[1],
- 'publish_at' => $date,
- 'social_ids' => \GuzzleHttp\json_encode($socialIds),
- 'type' => 1,
- 'created_at' => date('Y-m-d H:i:s'),
- ];
- $updateList[] = $array;
- }
- }
- HootsuiteHistory::query()->insert($updateList);
- }
- public function getSocialId($siteId, $type)
- {
- $hootSuiteUser = HootsuiteUser::query()->where(['site_id' => $siteId])->first();
- $socialProfiles = $hootSuiteUser->social_profiles ?? [];
- foreach ($socialProfiles as $kk => $vv) {
- if ($vv['type'] == $type) {
- return $vv['id'];
- }
- }
- return 0;
- }
- }
|