SocialStatisticsController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Logics\Admin\SocialLogic;
  4. use App\Http\Models\HootsuiteHistory;
  5. use App\Http\Models\HootsuiteUser;
  6. use App\Http\Models\PrSocial;
  7. use App\Http\Models\Site;
  8. use App\Http\Models\Social;
  9. use Facebook\Facebook;
  10. use GuzzleHttp\Client;
  11. use Illuminate\Http\Request;
  12. use App\Http\Controllers\Controller;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. use Laravel\Socialite\Facades\Socialite;
  16. class SocialStatisticsController extends Controller
  17. {
  18. public function socialStatistics($siteId)
  19. {
  20. $site = Site::query()->where('id', $siteId)->first();
  21. $startDate = HootsuiteHistory::query()->where('site_id', $siteId)->orderBy('publish_at')->value('publish_at') ?? date('Y-m-01 00:00:00', strtotime('first day of -1 month'));
  22. $ym = date('Y-m-01 00:00:00', strtotime($startDate));
  23. $ym2 = date('Y-m-t 23:59:59', strtotime('first day of -1 month'));
  24. $this->getSitePublishStatus($siteId, $ym, $ym2);
  25. $date1 = date('Y-m', strtotime('first day of -3 month'));
  26. $date2 = date('Y-m', strtotime('first day of -2 month'));
  27. $date3 = date('Y-m', strtotime('first day of -1 month'));
  28. $twitterCountList = $this->getTwitterCountList($siteId, $ym, $ym2, $date1, $date2, $date3);
  29. $facebookCountList = $this->getFacebookCountList($siteId, $ym, $ym2, $date1, $date2, $date3);
  30. $linkedInCountList = $this->getLinkedInCountList($siteId, $ym, $ym2, $date1, $date2, $date3);
  31. $pinCountList = $this->getPinCountList($siteId, $ym, $ym2, $date1, $date2, $date3);
  32. $instagramCountList = $this->getInstagramCount($siteId, $ym, $ym2, $date1, $date2, $date3);
  33. $youtubeCountList = $this->getYoutubeCountList($siteId, $ym, $ym2, $date1, $date2, $date3);
  34. $dataList = [
  35. [
  36. 'type' => 'twitter',
  37. 'data' => [
  38. 'data_list' => $twitterCountList,
  39. 'hide' => 0,
  40. ]
  41. ],
  42. [
  43. 'type' => 'facebook',
  44. 'data' => [
  45. 'data_list' => $facebookCountList,
  46. 'hide' => 0,
  47. ]
  48. ],
  49. [
  50. 'type' => 'linkedIn',
  51. 'data' => [
  52. 'data_list' => $linkedInCountList,
  53. 'hide' => 0,
  54. ]
  55. ],
  56. [
  57. 'type' => 'pin',
  58. 'data' => [
  59. 'data_list' => $pinCountList,
  60. 'hide' => 0,
  61. ]
  62. ],
  63. [
  64. 'type' => 'ins',
  65. 'data' => [
  66. 'data_list' => $instagramCountList,
  67. 'hide' => 0,
  68. ]
  69. ],
  70. [
  71. 'type' => 'youtube',
  72. 'data' => [
  73. 'data_list' => $youtubeCountList,
  74. 'hide' => 0,
  75. ]
  76. ],
  77. ];
  78. $lastMonth = date('Ym', strtotime('first day of -1 month'));
  79. foreach ($dataList as $item) {
  80. if (PrSocial::query()->where(['site_id' => $site->id, 'ym' => $lastMonth])->where(['type' => $item['type']])->exists()) {
  81. PrSocial::query()->where(['site_id' => $site->id, 'ym' => $lastMonth])->where(['type' => $item['type']])->update([
  82. 'data_list' => json_encode($item['data']['data_list']),
  83. 'hide' => $item['data']['hide'],
  84. ]);
  85. } else {
  86. PrSocial::query()->create([
  87. 'ym' => $lastMonth,
  88. 'type' => $item['type'],
  89. 'site_id' => $site->id,
  90. 'old_id' => $site->old_id,
  91. 'data_list' => $item['data']['data_list'],
  92. 'hide' => $item['data']['hide']
  93. ]);
  94. }
  95. }
  96. $this->saveNextSocial($site, $dataList);
  97. return response()->json(['message' => '操作成功']);
  98. }
  99. public function getSitePublishStatus($siteId, $ym, $ym2)
  100. {
  101. $list = DB::table('hootsuite_history')
  102. ->where('site_id', $siteId)
  103. ->select('id', 'site_id', 'url', 'publish_at', 'social_ids')
  104. ->whereBetween('publish_at', [$ym, [$ym2]])
  105. ->get();
  106. foreach ($list as $key => $value) {
  107. $socialIdList = json_decode($value->social_ids);
  108. if (empty($socialIdList)) {
  109. $twitter = strstr($value->url, 'twitter');
  110. if (!empty($twitter)) {
  111. $value->is_twitter = 1;
  112. }
  113. $facebook = strstr($value->url, 'facebook');
  114. if (!empty($facebook)) {
  115. $value->is_facebook = 1;
  116. }
  117. $linkEdin = strstr($value->url, 'linkedin');
  118. if (!empty($linkEdin)) {
  119. $value->is_linkedIn = 1;
  120. }
  121. $pin = strstr($value->url, 'pin');
  122. if (!empty($pin)) {
  123. $value->is_pin = 1;
  124. }
  125. $youtube = strstr($value->url, 'youtube');
  126. if (!empty($youtube)) {
  127. $value->is_youtube = 1;
  128. }
  129. $instagram = strstr($value->url, 'instagram');
  130. if (!empty($instagram)) {
  131. $value->is_instagram = 1;
  132. }
  133. } else {
  134. $result = $this->getSocialId($value->site_id, $socialIdList);
  135. foreach ($result as $kk => $vv) {
  136. if ($vv == 'TWITTER') {
  137. $value->is_twitter = 1;
  138. }
  139. if ($vv == 'FACEBOOKPAGE') {
  140. $value->is_facebook = 1;
  141. }
  142. if ($vv == 'LINKEDINCOMPANY') {
  143. $value->is_linkedIn = 1;
  144. }
  145. if ($vv == 'PINTEREST') {
  146. $value->is_pin = 1;
  147. }
  148. }
  149. }
  150. $update = [];
  151. if (!empty($value->is_twitter)) {
  152. $update['is_twitter'] = $value->is_twitter ?? 0;
  153. }
  154. if (!empty($value->is_facebook)) {
  155. $update['is_facebook'] = $value->is_facebook ?? 0;
  156. }
  157. if (!empty($value->is_linkedIn)) {
  158. $update['is_linkedIn'] = $value->is_linkedIn ?? 0;
  159. }
  160. if (!empty($value->is_pin)) {
  161. $update['is_pin'] = $value->is_pin ?? 0;
  162. }
  163. if (!empty($value->is_youtube)) {
  164. $update['is_youtube'] = $value->is_youtube ?? 0;
  165. }
  166. if (!empty($value->is_instagram)) {
  167. $update['is_instagram'] = $value->is_instagram ?? 0;
  168. }
  169. if (!empty($update)) {
  170. DB::table('hootsuite_history')->where('id', $value->id)->update($update);
  171. }
  172. }
  173. }
  174. public function getSocialId($siteId, $socialIdsList)
  175. {
  176. $array = [];
  177. $hootSuiteUser = HootsuiteUser::query()->where(['site_id' => $siteId])->first();
  178. $socialProfiles = $hootSuiteUser->social_profiles ?? [];
  179. foreach ($socialProfiles as $key => $value) {
  180. foreach ($socialIdsList as $kk => $vv) {
  181. if ($value['id'] == $vv) {
  182. $array[] = $value['type'];
  183. }
  184. }
  185. }
  186. return $array;
  187. }
  188. public function getTwitterCountList($siteId, $ym, $ym2, $date1, $date2, $date3)
  189. {
  190. $list1 = DB::table('hootsuite_history')
  191. ->where('site_id', $siteId)
  192. ->where('is_twitter', 1)
  193. ->whereBetween('publish_at', [$ym, $ym2])
  194. ->selectRaw('DATE_FORMAT(publish_at,"%Y-%m") as date, count(*) as twitter_count,site_id')
  195. ->groupBy('date', 'site_id')->get()->toArray();
  196. $twitterCount = [null, null, null];
  197. if (empty($list1)) {
  198. return $twitterCount;
  199. }
  200. $dateList = $this->getDateList($ym);
  201. foreach ($dateList as $key => $value) {
  202. $sum = 0;
  203. foreach ($list1 as $kk => $vv) {
  204. if (strtotime($value['date']) >= strtotime($vv->date)) {
  205. $sum += $vv->twitter_count;
  206. }
  207. $dateList[$key]['sum'] = $sum;
  208. }
  209. }
  210. foreach ($dateList as $key => $value) {
  211. if ($value['date'] == $date1) {
  212. $twitterCount[2] = $value['sum'];
  213. }
  214. if ($value['date'] == $date2) {
  215. $twitterCount[1] = $value['sum'];
  216. }
  217. if ($value['date'] == $date3) {
  218. $twitterCount[0] = $value['sum'];
  219. }
  220. }
  221. return $twitterCount;
  222. }
  223. public function getFacebookCountList($siteId, $ym, $ym2, $date1, $date2, $date3)
  224. {
  225. $list2 = DB::table('hootsuite_history')
  226. ->where('site_id', $siteId)
  227. ->where('is_facebook', 1)
  228. ->whereBetween('publish_at', [$ym, $ym2])
  229. ->selectRaw('DATE_FORMAT(publish_at,"%Y-%m") as date, count(*) as facebook_count,site_id')
  230. ->groupBy('date', 'site_id')->get()->toArray();
  231. $facebookCount = [null, null, null];
  232. if (empty($list2)) {
  233. return $facebookCount;
  234. }
  235. $dateList = $this->getDateList($ym);
  236. foreach ($dateList as $key => $value) {
  237. $sum = 0;
  238. foreach ($list2 as $kk => $vv) {
  239. if (strtotime($value['date']) >= strtotime($vv->date)) {
  240. $sum += $vv->facebook_count;
  241. }
  242. $dateList[$key]['sum'] = $sum;
  243. }
  244. }
  245. foreach ($dateList as $key => $value) {
  246. if ($value['date'] == $date1) {
  247. $facebookCount[2] = $value['sum'];
  248. }
  249. if ($value['date'] == $date2) {
  250. $facebookCount[1] = $value['sum'];
  251. }
  252. if ($value['date'] == $date3) {
  253. $facebookCount[0] = $value['sum'];
  254. }
  255. }
  256. return $facebookCount;
  257. }
  258. public function getLinkedInCountList($siteId, $ym, $ym2, $date1, $date2, $date3)
  259. {
  260. $list3 = DB::table('hootsuite_history')
  261. ->where('site_id', $siteId)
  262. ->where('is_linkedIn', 1)
  263. ->whereBetween('publish_at', [$ym, $ym2])
  264. ->selectRaw('DATE_FORMAT(publish_at,"%Y-%m") as date, count(*) as linkedIn_count,site_id')
  265. ->groupBy('date', 'site_id')->get()->toArray();
  266. $linkedInCount = [null, null, null];
  267. if (empty($list3)) {
  268. return $linkedInCount;
  269. }
  270. $dateList = $this->getDateList($ym);
  271. foreach ($dateList as $key => $value) {
  272. $sum = 0;
  273. foreach ($list3 as $kk => $vv) {
  274. if (strtotime($value['date']) >= strtotime($vv->date)) {
  275. $sum += $vv->linkedIn_count;
  276. }
  277. $dateList[$key]['sum'] = $sum;
  278. }
  279. }
  280. foreach ($dateList as $key => $value) {
  281. if ($value['date'] == $date1) {
  282. $linkedInCount[2] = $value['sum'];
  283. }
  284. if ($value['date'] == $date2) {
  285. $linkedInCount[1] = $value['sum'];
  286. }
  287. if ($value['date'] == $date3) {
  288. $linkedInCount[0] = $value['sum'];
  289. }
  290. }
  291. return $linkedInCount;
  292. }
  293. public function getPinCountList($siteId, $ym, $ym2, $date1, $date2, $date3)
  294. {
  295. $list4 = DB::table('hootsuite_history')
  296. ->where('site_id', $siteId)
  297. ->where('is_pin', 1)
  298. ->whereBetween('publish_at', [$ym, $ym2])
  299. ->selectRaw('DATE_FORMAT(publish_at,"%Y-%m") as date, count(*) as pin_count,site_id')
  300. ->groupBy('date', 'site_id')->get()->toArray();
  301. $pinCount = [null, null, null];
  302. if (empty($list4)) {
  303. return $pinCount;
  304. }
  305. $dateList = $this->getDateList($ym);
  306. foreach ($dateList as $key => $value) {
  307. $sum = 0;
  308. foreach ($list4 as $kk => $vv) {
  309. if (strtotime($value['date']) >= strtotime($vv->date)) {
  310. $sum += $vv->pin_count;
  311. }
  312. $dateList[$key]['sum'] = $sum;
  313. }
  314. }
  315. foreach ($dateList as $key => $value) {
  316. if ($value['date'] == $date1) {
  317. $pinCount[2] = $value['sum'];
  318. }
  319. if ($value['date'] == $date2) {
  320. $pinCount[1] = $value['sum'];
  321. }
  322. if ($value['date'] == $date3) {
  323. $pinCount[0] = $value['sum'];
  324. }
  325. }
  326. return $pinCount;
  327. }
  328. public function getYoutubeCountList($siteId, $ym, $ym2, $date1, $date2, $date3)
  329. {
  330. $list5 = DB::table('hootsuite_history')
  331. ->where('site_id', $siteId)
  332. ->where('is_youtube', 1)
  333. ->whereBetween('publish_at', [$ym, $ym2])
  334. ->selectRaw('DATE_FORMAT(publish_at,"%Y-%m") as date, count(*) as youtube_count,site_id')
  335. ->groupBy('date', 'site_id')->get()->toArray();
  336. $youtubeCount = [null, null, null];
  337. if (empty($list5)) {
  338. return $youtubeCount;
  339. }
  340. $dateList = $this->getDateList($ym);
  341. foreach ($dateList as $key => $value) {
  342. $sum = 0;
  343. foreach ($list5 as $kk => $vv) {
  344. if (strtotime($value['date']) >= strtotime($vv->date)) {
  345. $sum += $vv->youtube_count;
  346. }
  347. $dateList[$key]['sum'] = $sum;
  348. }
  349. }
  350. foreach ($dateList as $key => $value) {
  351. if ($value['date'] == $date1) {
  352. $youtubeCount[2] = $value['sum'];
  353. }
  354. if ($value['date'] == $date2) {
  355. $youtubeCount[1] = $value['sum'];
  356. }
  357. if ($value['date'] == $date3) {
  358. $youtubeCount[0] = $value['sum'];
  359. }
  360. }
  361. return $youtubeCount;
  362. }
  363. public function getInstagramCount($siteId, $ym, $ym2, $date1, $date2, $date3)
  364. {
  365. $list6 = DB::table('hootsuite_history')
  366. ->where('site_id', $siteId)
  367. ->where('is_instagram', 1)
  368. ->whereBetween('publish_at', [$ym, $ym2])
  369. ->selectRaw('DATE_FORMAT(publish_at,"%Y-%m") as date, count(*) as instagram_count,site_id')
  370. ->groupBy('date', 'site_id')->get()->toArray();
  371. $instagramCount = [null, null, null];
  372. if (empty($list6)) {
  373. return $instagramCount;
  374. }
  375. $dateList = $this->getDateList($ym);
  376. foreach ($dateList as $key => $value) {
  377. $sum = 0;
  378. foreach ($list6 as $kk => $vv) {
  379. if (strtotime($value['date']) >= strtotime($vv->date)) {
  380. $sum += $vv->instagram_count;
  381. }
  382. $dateList[$key]['sum'] = $sum;
  383. }
  384. }
  385. foreach ($dateList as $key => $value) {
  386. if ($value['date'] == $date1) {
  387. $instagramCount[2] = $value['sum'];
  388. }
  389. if ($value['date'] == $date2) {
  390. $instagramCount[1] = $value['sum'];
  391. }
  392. if ($value['date'] == $date3) {
  393. $instagramCount[0] = $value['sum'];
  394. }
  395. }
  396. return $instagramCount;
  397. }
  398. //提前保存下个月的
  399. private function saveNextSocial($site, $dataList)
  400. {
  401. $ym = date('Ym');
  402. foreach ($dataList as $item) {
  403. $data = PrSocial::query()->where(['site_id' => $site->id, 'ym' => $ym, 'type' => $item['type']])->first();
  404. $item['data']['data_list'][0] = null;
  405. if ($data) {
  406. $data->data_list = $item['data']['data_list'];
  407. $data->hide = $item['data']['hide'];
  408. $data->save();
  409. } else {
  410. PrSocial::query()->create([
  411. 'ym' => $ym,
  412. 'type' => $item['type'],
  413. 'site_id' => $site->id,
  414. 'old_id' => $site->old_id,
  415. 'data_list' => $item['data']['data_list'],
  416. 'hide' => $item['data']['hide']
  417. ]);
  418. }
  419. }
  420. }
  421. public function getDateList($ym)
  422. {
  423. $startTime = strtotime(date("Y-m", strtotime("-1 months", strtotime($ym))));
  424. $endTime = strtotime(date('Y-m', strtotime("first day of -1 month")));
  425. $dateList = [];
  426. $i = 0;
  427. while ($startTime < $endTime) {
  428. $startTime = strtotime('+1 Month', $startTime);
  429. $i++;
  430. $dateList[$i]['date'] = date('Y-m', $startTime);
  431. }
  432. return $dateList;
  433. }
  434. }