OwnerPriceDirectLogisticDetailImport.php 6.3 KB

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