OwnerPriceDirectLogisticDetailImport.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Imports;
  3. use App\CarType;
  4. use App\OwnerPriceDirectLogistic;
  5. use App\OwnerPriceDirectLogisticCar;
  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 OwnerPriceDirectLogisticDetailImport implements ToCollection,WithHeadingRow
  15. {
  16. protected $model;
  17. public function __construct(OwnerPriceDirectLogistic $model)
  18. {
  19. $this->model = $model;
  20. }
  21. /**
  22. * @param Collection $collection
  23. * @return bool
  24. */
  25. public function collection(Collection $collection)
  26. {
  27. if (!$this->model){
  28. Cache::put("directLogistic",["success"=>false, "data"=>"不存在父级"],86400);
  29. return false;
  30. }
  31. $row = $collection->first();
  32. $additional = "续费";
  33. foreach ($row as $key => $str){
  34. if (mb_strpos($key,$additional) !== false){
  35. $row["续费"] = $str;
  36. $additional = $key;
  37. break;
  38. }
  39. }
  40. $header = [
  41. "车型","起步费","续费"
  42. ];
  43. foreach ($header as $str){
  44. if (!isset($row[$str])){
  45. Cache::put("directLogistic",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
  46. return false;
  47. }
  48. }
  49. if ($row)
  50. //车型map
  51. $map = [];
  52. $carTypes = CarType::query()->get();
  53. foreach ($carTypes as $carType){
  54. $map[$carType->name] = $carType->id;
  55. }
  56. //已存在的计费
  57. $existDetails = [];
  58. foreach ($this->model->details as $detail){
  59. $existDetails[$detail->car_type_id] = $detail->id;
  60. }
  61. //生成列表内的重复条目
  62. $existInsert = [];
  63. //导入的数据整理,存在更新
  64. $id = $this->model->id;
  65. $errors = [];
  66. $insert = [];
  67. $update = [["id","base_fee","additional_fee"]];
  68. $date = date('Y-m-d H:i:s');
  69. foreach ($collection as $index => $item){
  70. /* 数据校验 */
  71. if (!$item["车型"]){
  72. $errors[] = "第“".($index+2)."”行车型为空";
  73. continue;
  74. }else{
  75. if (!isset($map[$item["车型"]])){
  76. $errors[] = "第“".($index+2)."”行未知车型";
  77. continue;
  78. }
  79. $item["车型"] = $map[$item["车型"]];
  80. }
  81. if (!$item["起步费"] || !is_numeric($item["起步费"]) || $item["起步费"] <= 0){
  82. $errors[] = "第“".($index+2)."”行非法起步费";
  83. continue;
  84. }
  85. if (!$item[$additional] || !is_numeric($item[$additional]) || $item[$additional] <= 0){
  86. $errors[] = "第“".($index+2)."”行非法续费";
  87. continue;
  88. }
  89. if (isset($existInsert[$item["车型"]])){
  90. $errors[] = "第“".($index+2)."”行与第“".$existInsert[$item["车型"]]."”行重复";
  91. continue;
  92. }
  93. if (isset($existDetails[$item["车型"]])){
  94. $update[] = [
  95. "id" => $existDetails[$item["车型"]],
  96. "base_fee" => $item["起步费"],
  97. "additional_fee" => $item[$additional],
  98. "updated_at" => $date,
  99. ];
  100. continue;
  101. }
  102. $insert[] = [
  103. "owner_price_direct_logistic_id" => $id,
  104. "car_type_id" => $item["车型"],
  105. "base_fee" => $item["起步费"],
  106. "additional_fee" => $item[$additional],
  107. "created_at" => $date,
  108. ];
  109. $existInsert[$item["车型"]] = $index+2;
  110. }
  111. if (count($update) > 1){
  112. app(BatchUpdateService::class)->batchUpdate("owner_price_direct_logistic_cars",$update);
  113. LogService::log(__METHOD__,"直发车计费导入修改",json_encode($update));
  114. }
  115. if (count($insert) > 0){
  116. OwnerPriceDirectLogisticCar::query()->insert($insert);
  117. LogService::log(__METHOD__,"直发车计费导入录入",json_encode($insert));
  118. }
  119. $this->model->load(["details"=>function($query){$query->with("carType");}]);
  120. Cache::put("directLogistic",["success"=>true,"data"=>$this->model->details,"errors"=>$errors],86400);
  121. return true;
  122. }
  123. }