ExpressImport.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Imports\HeadingRowFormatter;
  13. HeadingRowFormatter::default('none');
  14. class ExpressImport implements ToCollection,WithHeadingRow
  15. {
  16. protected $express;
  17. public function __construct(OwnerPriceExpress $express = null)
  18. {
  19. $this->express = $express;
  20. }
  21. /**
  22. * @param Collection $collection
  23. * @return bool
  24. */
  25. public function collection(Collection $collection)
  26. {
  27. if (!$this->express){
  28. Cache::put("express",["success"=>false, "data"=>"不存在父级"],86400);
  29. return false;
  30. }
  31. $row = $collection->first();
  32. $header = [
  33. "省","首重价格","续重价格"
  34. ];
  35. foreach ($header as $str){
  36. if (!isset($row[$str])){
  37. Cache::put("express",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
  38. return false;
  39. }
  40. }
  41. //省份map
  42. $map = [];
  43. $provinces = Province::query()->get();
  44. foreach ($provinces as $province){
  45. $map[$province->name] = $province->id;
  46. }
  47. //已存在的计费
  48. $existDetails = [];
  49. foreach ($this->express->details as $detail){
  50. $existDetails[$detail->province_id] = $detail->id;
  51. }
  52. //导入的数据整理,存在更新
  53. $id = $this->express->id;
  54. $errors = [];
  55. $insert = [];
  56. $update = [["id","initial_weight_price","additional_weight_price","updated_at"]];
  57. $date = date('Y-m-d H:i:s');
  58. foreach ($collection as $index => $item){
  59. if (!isset($map[$item["省"]])){
  60. $errors[] = "第“".($index+2)."”行未知省份";
  61. continue;
  62. }
  63. if (!is_numeric($item["首重价格"]) || $item["首重价格"] <= 0){
  64. $errors[] = "第“".($index+2)."”行非法首重价格";
  65. continue;
  66. }
  67. if (!is_numeric($item["续重价格"]) || $item["续重价格"] <= 0){
  68. $errors[] = "第“".($index+2)."”行非法续重价格";
  69. continue;
  70. }
  71. if (isset($existDetails[$map[$item["省"]]])){
  72. $update[] = [
  73. "id" => $existDetails[$map[$item["省"]]],
  74. "initial_weight_price" => $item["首重价格"],
  75. "additional_weight_price" => $item["续重价格"],
  76. "updated_at" => $date,
  77. ];
  78. continue;
  79. }
  80. $insert[] = [
  81. "owner_price_express_id" => $id,
  82. "province_id" => $map[$item["省"]],
  83. "initial_weight_price" => $item["首重价格"],
  84. "additional_weight_price" => $item["续重价格"],
  85. "created_at" => $date,
  86. ];
  87. }
  88. if (count($update) > 1){
  89. app(BatchUpdateService::class)->batchUpdate("owner_price_express_provinces",$update);
  90. LogService::log(__METHOD__,"快递计费导入修改",json_encode($update));
  91. }
  92. if (count($insert) > 0){
  93. OwnerPriceExpressProvince::query()->insert($insert);
  94. LogService::log(__METHOD__,"快递计费导入录入",json_encode($insert));
  95. }
  96. $this->express->load(["details"=>function($query){$query->with("province");}]);
  97. Cache::put("express",["success"=>true,"data"=>$this->express->details,"errors"=>$errors],86400);
  98. return true;
  99. }
  100. }