OwnerPriceDirectLogisticDetailImport.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //只读情况下
  59. if (!$this->model){
  60. return $this->readonly($collection, $map, $additional);
  61. }
  62. //拷贝数据时切换对象
  63. if (!$this->model->operation){
  64. $this->model = app("OwnerPriceDirectLogisticService")->copy($this->model);
  65. }
  66. //已存在的计费
  67. $existDetails = [];
  68. foreach ($this->model->details as $detail){
  69. $existDetails[$detail->car_type_id] = $detail->id;
  70. }
  71. //生成列表内的重复条目
  72. $existInsert = [];
  73. //导入的数据整理,存在更新
  74. $id = $this->model->id;
  75. $errors = [];
  76. $insert = [];
  77. $update = [["id","base_fee","additional_fee"]];
  78. $date = date('Y-m-d H:i:s');
  79. foreach ($collection as $index => $item){
  80. /* 数据校验 */
  81. if (!$item["车型"]){
  82. $errors[] = "第“".($index+2)."”行车型为空";
  83. continue;
  84. }else{
  85. if (!isset($map[$item["车型"]])){
  86. $errors[] = "第“".($index+2)."”行未知车型";
  87. continue;
  88. }
  89. $item["车型"] = $map[$item["车型"]];
  90. }
  91. if (!$item["起步费"] || !is_numeric($item["起步费"]) || $item["起步费"] <= 0){
  92. $errors[] = "第“".($index+2)."”行非法起步费";
  93. continue;
  94. }
  95. if (!$item[$additional] || !is_numeric($item[$additional]) || $item[$additional] <= 0){
  96. $errors[] = "第“".($index+2)."”行非法续费";
  97. continue;
  98. }
  99. if (isset($existInsert[$item["车型"]])){
  100. $errors[] = "第“".($index+2)."”行与第“".$existInsert[$item["车型"]]."”行重复";
  101. continue;
  102. }
  103. if (isset($existDetails[$item["车型"]])){
  104. $update[] = [
  105. "id" => $existDetails[$item["车型"]],
  106. "base_fee" => $item["起步费"],
  107. "additional_fee" => $item[$additional],
  108. "updated_at" => $date,
  109. ];
  110. continue;
  111. }
  112. $insert[] = [
  113. "owner_price_direct_logistic_id" => $id,
  114. "car_type_id" => $item["车型"],
  115. "base_fee" => $item["起步费"],
  116. "additional_fee" => $item[$additional],
  117. "created_at" => $date,
  118. ];
  119. $existInsert[$item["车型"]] = $index+2;
  120. }
  121. if (count($update) > 1){
  122. app(BatchUpdateService::class)->batchUpdate("owner_price_direct_logistic_cars",$update);
  123. LogService::log(__METHOD__,"直发车计费导入修改",json_encode($update));
  124. }
  125. if (count($insert) > 0){
  126. OwnerPriceDirectLogisticCar::query()->insert($insert);
  127. LogService::log(__METHOD__,"直发车计费导入录入",json_encode($insert));
  128. }
  129. $this->model->load(["details"=>function($query){$query->with("carType");}]);
  130. Cache::put("directLogistic",["success"=>true,"data"=>$this->model->details,"errors"=>$errors],86400);
  131. return true;
  132. }
  133. private function readonly(Collection $collection, array $map, $additional)
  134. {
  135. $errors = [];
  136. $data = [];
  137. $existInsert = [];
  138. foreach ($collection as $index => $item){
  139. /* 数据校验 */
  140. if (!$item["车型"]){
  141. $errors[] = "第“".($index+2)."”行车型为空";
  142. continue;
  143. }else{
  144. if (!isset($map[$item["车型"]])){
  145. $errors[] = "第“".($index+2)."”行未知车型";
  146. continue;
  147. }
  148. $item["车型"] = $map[$item["车型"]];
  149. }
  150. if (!$item["起步费"] || !is_numeric($item["起步费"]) || $item["起步费"] <= 0){
  151. $errors[] = "第“".($index+2)."”行非法起步费";
  152. continue;
  153. }
  154. if (!$item[$additional] || !is_numeric($item[$additional]) || $item[$additional] <= 0){
  155. $errors[] = "第“".($index+2)."”行非法续费";
  156. continue;
  157. }
  158. if (isset($existInsert[$item["车型"]])){
  159. $errors[] = "第“".($index+2)."”行与第“".$existInsert[$item["车型"]]."”行重复";
  160. continue;
  161. }
  162. $data[] = [
  163. "car_type_id" => $item["车型"],
  164. "base_fee" => $item["起步费"],
  165. "additional_fee" => $item[$additional],
  166. ];
  167. $existInsert[$item["车型"]] = $index+2;
  168. }
  169. Cache::put("directLogistic",["success"=>true,"data"=>$data,"errors"=>$errors],86400);
  170. return true;
  171. }
  172. }