ExpressImport.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Imports;
  3. use App\OwnerPriceExpress;
  4. use App\OwnerPriceExpressProvince;
  5. use App\Province;
  6. use App\Services\common\BatchUpdateService;
  7. use App\Services\LogService;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  12. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  13. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  14. HeadingRowFormatter::default('none');
  15. class ExpressImport implements ToCollection,WithHeadingRow,WithMultipleSheets
  16. {
  17. public function sheets(): array
  18. {
  19. return [
  20. 0 => $this,
  21. ];
  22. }
  23. protected $express;
  24. public function __construct(OwnerPriceExpress $express = null)
  25. {
  26. $this->express = $express;
  27. }
  28. /**
  29. * @param Collection $collection
  30. * @return bool
  31. */
  32. public function collection(Collection $collection)
  33. {
  34. $row = $collection->first();
  35. $header = [
  36. "省","首重价格","续重价格"
  37. ];
  38. foreach ($header as $str){
  39. if (!isset($row[$str])){
  40. Cache::put("express",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
  41. return false;
  42. }
  43. }
  44. //省份map
  45. $map = [];
  46. $provinces = Province::query()->get();
  47. foreach ($provinces as $province){
  48. $map[mb_substr($province->name,0,2)] = $province->id;
  49. }
  50. if (!$this->express){
  51. return $this->readonly($collection,$map);
  52. }
  53. //已存在的计费
  54. $existDetails = [];
  55. foreach ($this->express->details as $detail){
  56. $existDetails[$detail->province_id] = $detail->id;
  57. }
  58. //导入的数据整理,存在更新
  59. $id = $this->express->id;
  60. $errors = [];
  61. $insert = [];
  62. $update = [["id","initial_weight_price","additional_weight_price","updated_at"]];
  63. $date = date('Y-m-d H:i:s');
  64. foreach ($collection as $index => $item){
  65. $province = mb_substr($item["省"],0,2);
  66. if (!isset($map[$province])){
  67. $errors[] = "第“".($index+2)."”行未知省份";
  68. continue;
  69. }
  70. if (!is_numeric($item["首重价格"]) || $item["首重价格"] <= 0){
  71. $errors[] = "第“".($index+2)."”行非法首重价格";
  72. continue;
  73. }
  74. if (!is_numeric($item["续重价格"]) || $item["续重价格"] <= 0){
  75. $errors[] = "第“".($index+2)."”行非法续重价格";
  76. continue;
  77. }
  78. if (isset($existDetails[$map[$province]])){
  79. $update[] = [
  80. "id" => $existDetails[$map[$province]],
  81. "initial_weight_price" => $item["首重价格"],
  82. "additional_weight_price" => $item["续重价格"],
  83. "updated_at" => $date,
  84. ];
  85. continue;
  86. }
  87. $insert[] = [
  88. "owner_price_express_id" => $id,
  89. "province_id" => $map[$province],
  90. "initial_weight_price" => $item["首重价格"],
  91. "additional_weight_price" => $item["续重价格"],
  92. "created_at" => $date,
  93. ];
  94. }
  95. if (count($update) > 1){
  96. app(BatchUpdateService::class)->batchUpdate("owner_price_express_provinces",$update);
  97. LogService::log(__METHOD__,"快递计费导入修改",json_encode($update));
  98. }
  99. if (count($insert) > 0){
  100. OwnerPriceExpressProvince::query()->insert($insert);
  101. LogService::log(__METHOD__,"快递计费导入录入",json_encode($insert));
  102. }
  103. $this->express->load(["details"=>function($query){$query->with("province");}]);
  104. Cache::put("express",["success"=>true,"data"=>$this->express->details,"errors"=>$errors],86400);
  105. return true;
  106. }
  107. private function readonly(Collection $collection, array $map)
  108. {
  109. $errors = [];
  110. $data = [];
  111. foreach ($collection as $index => $item){
  112. $province = mb_substr($item["省"],0,2);
  113. if (!isset($map[$province])){
  114. $errors[] = "第“".($index+2)."”行未知省份";
  115. continue;
  116. }
  117. if (!is_numeric($item["首重价格"]) || $item["首重价格"] <= 0){
  118. $errors[] = "第“".($index+2)."”行非法首重价格";
  119. continue;
  120. }
  121. if (!is_numeric($item["续重价格"]) || $item["续重价格"] <= 0){
  122. $errors[] = "第“".($index+2)."”行非法续重价格";
  123. continue;
  124. }
  125. $data[] = [
  126. "province_id" => $map[$province],
  127. "initial_weight_price" => $item["首重价格"],
  128. "additional_weight_price" => $item["续重价格"],
  129. ];
  130. }
  131. Cache::put("express",["success"=>true,"data"=>$data,"errors"=>$errors],86400);
  132. return true;
  133. }
  134. }