ArticleImport.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Logics\Admin\SiteSyncLogic;
  4. use App\Http\Models\Article;
  5. use App\Http\Models\Server;
  6. use App\Http\Models\Site;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Validation\Rule;
  11. use Maatwebsite\Excel\Concerns\Importable;
  12. use Maatwebsite\Excel\Concerns\ToCollection;
  13. use Maatwebsite\Excel\Concerns\WithMappedCells;
  14. use Maatwebsite\Excel\Concerns\WithValidation;
  15. class ArticleImport implements ToCollection
  16. {
  17. use Importable;
  18. public function __construct()
  19. {
  20. }
  21. const oldStatusMap = [
  22. 0 => '待翻译',
  23. 1 => '翻译中',
  24. 2 => '待审核',
  25. 3 => '审核失败',
  26. 4 => '审核通过'
  27. ];
  28. const oldMap = [
  29. 'success_time' => '审核时间',
  30. 'sync_time' => '同步时间',
  31. 'submit_time' => '提交时间',
  32. 'expire_time' => '过期时间',
  33. 'create_time' => '创建时间',
  34. 'word_num' => '已翻词数',
  35. ];
  36. const FIELD_MAP = [
  37. 0 => 'domain',
  38. 6 => 'title',
  39. 7 => 'content',
  40. 8 => 'new_title',
  41. 9 => 'new_content',
  42. 10 => 'status',
  43. 11 => 'tags',
  44. 14 => 'word_num',
  45. 15 => 'sync_time',
  46. 16 => 'success_time',
  47. 17 => 'submit_time',
  48. 18 => 'expire_time',
  49. 19 => 'create_time'
  50. ];
  51. const STATUS_MAP = [
  52. 0 => 1,
  53. 1 => 2,
  54. 2 => 3,
  55. 3 => 4,
  56. 4 => 5
  57. ];
  58. public function collection(Collection $collections)
  59. {
  60. //
  61. $allSites = Site::query()->select(['id', 'domain'])->get();
  62. //
  63. $all = collect([]);
  64. $noSiteList = [];
  65. foreach ($collections as $key => $collection) {
  66. if ($key < 1) {
  67. continue;
  68. }
  69. $temp = $this->fieldMap($collection);
  70. if (empty($temp['sync_time'])) {
  71. continue;
  72. }
  73. $hasSite = $allSites->firstWhere('domain', $temp['domain']);
  74. if ($hasSite) {
  75. $createdAt = $temp['create_time'] ? date('Y-m-d H:i:s', $temp['create_time']) : null;
  76. $syncAt = $temp['sync_time'] ? date('Y-m-d H:i:s', $temp['sync_time']) : null;
  77. $auditAt = $temp['success_time'] ? date('Y-m-d H:i:s', $temp['success_time']) : null;
  78. //[{"anchor": null, "cn_keyword": "膜分离", "en_keyword": null}]
  79. $inserted = [
  80. 'is_export' => 1,
  81. 'translator_id' => 8,//杜佳
  82. 'site_id' => $hasSite->id,
  83. 'group' => json_encode([
  84. [
  85. 'anchor' => null,
  86. 'cn_keyword' => null,
  87. 'en_keyword' => $temp['tags']
  88. ]
  89. ]),
  90. 'title' => $temp['title'],
  91. 'content' => $temp['content'],
  92. 'translate_title' => $temp['new_title'],
  93. 'translate_content' => $temp['new_content'],
  94. 'translate_words' => $temp['word_num'],
  95. 'sync_at' => $syncAt,
  96. 'audit_at' => $auditAt,
  97. 'created_at' => $createdAt,
  98. 'status' => self::STATUS_MAP[$temp['status']] ?? 99 //已结算,
  99. ];
  100. $all->push(collect($inserted));
  101. } else {
  102. $noSiteList[] = $temp['domain'];
  103. }
  104. }
  105. pre_dump($noSiteList);
  106. exit('dsd');
  107. $allItems = $all->chunk(2000)->toArray();
  108. foreach ($allItems as $item) {
  109. // dd($item);
  110. Article::query()->insert($item);
  111. }
  112. }
  113. public function fieldMap($collect): array
  114. {
  115. $result = [];
  116. foreach ($collect as $inx => $val) {
  117. if (empty(self::FIELD_MAP[$inx])) continue;
  118. if (in_array(self::FIELD_MAP[$inx], ['status', 'word_num', 'sync_time', 'success_time', 'submit_time', 'expire_time', 'create_time'])) {
  119. $result[self::FIELD_MAP[$inx]] = intval($val);
  120. } else {
  121. $result[self::FIELD_MAP[$inx]] = $val;
  122. }
  123. }
  124. return $result;
  125. }
  126. }