ImportService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services;
  3. use App\Models\ImportPost;
  4. use Cache;
  5. class ImportService
  6. {
  7. const CACHE_KEY = 'pf:import-service:';
  8. public static function getId($userId, $year, $month, $day)
  9. {
  10. if($userId > 999999) {
  11. return;
  12. }
  13. if($year < 9 || $year > 23) {
  14. return;
  15. }
  16. if($month < 1 || $month > 12) {
  17. return;
  18. }
  19. if($day < 1 || $day > 31) {
  20. return;
  21. }
  22. $start = 1;
  23. $key = self::CACHE_KEY . 'getIdRange:incr:byUserId:' . $userId . ':y-' . $year . ':m-' . $month . ':d-' . $day;
  24. $incr = Cache::increment($key, random_int(5, 19));
  25. if($incr > 999) {
  26. $daysInMonth = now()->parse($day . '-' . $month . '-' . $year)->daysInMonth;
  27. if($month == 12) {
  28. $year = $year + 1;
  29. $month = 1;
  30. $day = 0;
  31. }
  32. if($day + 1 >= $daysInMonth) {
  33. $day = 1;
  34. $month = $month + 1;
  35. } else {
  36. $day = $day + 1;
  37. }
  38. return self::getId($userId, $year, $month, $day);
  39. }
  40. $uid = str_pad($userId, 6, 0, STR_PAD_LEFT);
  41. $year = str_pad($year, 2, 0, STR_PAD_LEFT);
  42. $month = str_pad($month, 2, 0, STR_PAD_LEFT);
  43. $day = str_pad($day, 2, 0, STR_PAD_LEFT);
  44. $zone = $year . $month . $day . str_pad($incr, 3, 0, STR_PAD_LEFT);
  45. return [
  46. 'id' => $start . $uid . $zone,
  47. 'year' => $year,
  48. 'month' => $month,
  49. 'day' => $day,
  50. 'incr' => $incr,
  51. ];
  52. }
  53. public static function getPostCount($profileId, $refresh = false)
  54. {
  55. $key = self::CACHE_KEY . 'totalPostCountByProfileId:' . $profileId;
  56. if($refresh) {
  57. Cache::forget($key);
  58. }
  59. return intval(Cache::remember($key, 21600, function() use($profileId) {
  60. return ImportPost::whereProfileId($profileId)->whereSkipMissingMedia(false)->count();
  61. }));
  62. }
  63. public static function getAttempts($profileId)
  64. {
  65. $key = self::CACHE_KEY . 'attemptsByProfileId:' . $profileId;
  66. return intval(Cache::remember($key, 21600, function() use($profileId) {
  67. return ImportPost::whereProfileId($profileId)
  68. ->whereSkipMissingMedia(false)
  69. ->get()
  70. ->groupBy(function($item) {
  71. return $item->created_at->format('Y-m-d');
  72. })
  73. ->count();
  74. }));
  75. }
  76. public static function clearAttempts($profileId)
  77. {
  78. $key = self::CACHE_KEY . 'attemptsByProfileId:' . $profileId;
  79. return Cache::forget($key);
  80. }
  81. public static function getImportedFiles($profileId, $refresh = false)
  82. {
  83. $key = self::CACHE_KEY . 'importedPostsByProfileId:' . $profileId;
  84. if($refresh) {
  85. Cache::forget($key);
  86. }
  87. return Cache::remember($key, 21600, function() use($profileId) {
  88. return ImportPost::whereProfileId($profileId)
  89. ->get()
  90. ->filter(function($ip) {
  91. return StatusService::get($ip->status_id) == null;
  92. })
  93. ->map(function($ip) {
  94. return collect($ip->media)->map(function($m) { return $m['uri']; });
  95. })->values()->flatten();
  96. });
  97. }
  98. public static function clearImportedFiles($profileId)
  99. {
  100. $key = self::CACHE_KEY . 'importedPostsByProfileId:' . $profileId;
  101. return Cache::forget($key);
  102. }
  103. }