ExpressImport.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Imports;
  3. use App\Components\AsyncResponse;
  4. use App\OwnerPriceExpress;
  5. use App\OwnerPriceExpressProvince;
  6. use App\Province;
  7. use App\Services\common\BatchUpdateService;
  8. use App\Services\LogService;
  9. use Maatwebsite\Excel\Concerns\ToArray;
  10. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  11. class ExpressImport implements WithMultipleSheets,ToArray
  12. {
  13. use AsyncResponse;
  14. public function sheets(): array
  15. {
  16. return [
  17. 0 => $this,
  18. ];
  19. }
  20. /** @var OwnerPriceExpress|\stdClass $express */
  21. protected $express;
  22. /** @var array $errors */
  23. private $errors = [];
  24. public function __construct(OwnerPriceExpress $express = null)
  25. {
  26. $this->express = $express;
  27. }
  28. public function array(array $array)
  29. {
  30. if (isset($array[0][0]) && $array[0][0]=='省')array_splice($array,0,1);
  31. $array = $this->filter($array);
  32. if (!$this->express)$this->success(["data"=>$array,"errors"=>$this->errors]);
  33. if (!$this->express->operation)$this->express = app("OwnerPriceExpressService")->copy($this->express);
  34. //已存在的计费
  35. $existDetails = [];
  36. foreach ($this->express->details as $detail){
  37. $existDetails[$detail->province_id] = $detail->id;
  38. }
  39. //导入的数据整理,存在更新
  40. $id = $this->express->id;
  41. $insert = [];
  42. $update = [["id","initial_weight_price","additional_weight_price","updated_at"]];
  43. $date = date('Y-m-d H:i:s');
  44. foreach ($array as $index => $item){
  45. if (isset($existDetails[$item[0]])){
  46. $update[] = [
  47. "id" => $existDetails[$item[0]],
  48. "initial_weight_price" => $item[1],
  49. "additional_weight_price" => $item[2],
  50. "updated_at" => $date,
  51. ];
  52. continue;
  53. }
  54. $insert[] = [
  55. "owner_price_express_id" => $id,
  56. "province_id" => $item[0],
  57. "initial_weight_price" => $item[1],
  58. "additional_weight_price" => $item[2],
  59. "created_at" => $date,
  60. ];
  61. }
  62. if (count($update) > 1){
  63. app(BatchUpdateService::class)->batchUpdate("owner_price_express_provinces",$update);
  64. LogService::log(__METHOD__,"快递计费导入修改",json_encode($update));
  65. }
  66. if (count($insert) > 0){
  67. OwnerPriceExpressProvince::query()->insert($insert);
  68. LogService::log(__METHOD__,"快递计费导入录入",json_encode($insert));
  69. }
  70. $this->express->load("details.province");
  71. $this->success(["data"=>$this->express->details,"errors"=>$this->errors]);
  72. }
  73. public function filter(array $array)
  74. {
  75. $result = [];
  76. //省份map
  77. $map = [];
  78. $provinces = Province::query()->get();
  79. foreach ($provinces as $province){
  80. $map[mb_substr($province->name,0,2)] = $province->id;
  81. }
  82. foreach ($array as &$item){
  83. if (count($item)<3 || (!$item[0] && !$item[1] && !$item[2]))continue;
  84. $province = mb_substr($item[0],0,2);
  85. if (!isset($map[$province])){
  86. $this->errors[] = "“".$province."”此省份未被记录";
  87. continue;
  88. }else $item[0] = $map[$province];
  89. $sign = false;
  90. for($i=1;$i<count($item);$i++){
  91. if (($item[$i] && !is_numeric($item[$i])) || ($item[$i]!==null && $item[$i]<=0)){
  92. $this->errors[] = "“".$province."”非法价格";
  93. $sign = true;
  94. break;
  95. }
  96. if ($item[$i]===null && $i>3)break;
  97. }
  98. if ($sign)continue;
  99. $result[] = $item;
  100. }
  101. return $result;
  102. }
  103. }