OwnerPriceDirectLogisticDetailImport.php 6.1 KB

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