OwnerPriceDirectLogisticDetailImport.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. //车型map
  50. $map = [];
  51. $carTypes = CarType::query()->get();
  52. foreach ($carTypes as $carType){
  53. $map[$carType->name] = $carType->id;
  54. }
  55. //已存在的计费
  56. $existDetails = [];
  57. foreach ($this->model->details as $detail){
  58. $existDetails[$detail->car_type_id] = $detail->id;
  59. }
  60. //生成列表内的重复条目
  61. $existInsert = [];
  62. //导入的数据整理,存在更新
  63. $id = $this->model->id;
  64. $errors = [];
  65. $insert = [];
  66. $update = [["id","base_fee","additional_fee"]];
  67. $date = date('Y-m-d H:i:s');
  68. foreach ($collection as $index => $item){
  69. /* 数据校验 */
  70. if (!$item["车型"]){
  71. $errors[] = "第“".($index+2)."”行车型为空";
  72. continue;
  73. }else{
  74. if (!isset($map[$item["车型"]])){
  75. $errors[] = "第“".($index+2)."”行未知车型";
  76. continue;
  77. }
  78. $item["车型"] = $map[$item["车型"]];
  79. }
  80. if (!$item["起步费"] || !is_numeric($item["起步费"]) || $item["起步费"] <= 0){
  81. $errors[] = "第“".($index+2)."”行非法起步费";
  82. continue;
  83. }
  84. if (!$item[$additional] || !is_numeric($item[$additional]) || $item[$additional] <= 0){
  85. $errors[] = "第“".($index+2)."”行非法续费";
  86. continue;
  87. }
  88. if (isset($existInsert[$item["车型"]])){
  89. $errors[] = "第“".($index+2)."”行与第“".$existInsert[$item["车型"]]."”行重复";
  90. continue;
  91. }
  92. if (isset($existDetails[$item["车型"]])){
  93. $update[] = [
  94. "id" => $existDetails[$item["车型"]],
  95. "base_fee" => $item["起步费"],
  96. "additional_fee" => $item[$additional],
  97. "updated_at" => $date,
  98. ];
  99. continue;
  100. }
  101. $insert[] = [
  102. "owner_price_direct_logistic_id" => $id,
  103. "car_type_id" => $item["车型"],
  104. "base_fee" => $item["起步费"],
  105. "additional_fee" => $item[$additional],
  106. "created_at" => $date,
  107. ];
  108. $existInsert[$item["车型"]] = $index+2;
  109. }
  110. if (count($update) > 1){
  111. app(BatchUpdateService::class)->batchUpdate("owner_price_direct_logistic_cars",$update);
  112. LogService::log(__METHOD__,"直发车计费导入修改",json_encode($update));
  113. }
  114. if (count($insert) > 0){
  115. OwnerPriceDirectLogisticCar::query()->insert($insert);
  116. LogService::log(__METHOD__,"直发车计费导入录入",json_encode($insert));
  117. }
  118. $this->model->load(["details"=>function($query){$query->with("carType");}]);
  119. Cache::put("directLogistic",["success"=>true,"data"=>$this->model->details,"errors"=>$errors],86400);
  120. return true;
  121. }
  122. }