WaybillPriceModelsImport.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Imports;
  3. use App\Logistic;
  4. use App\WaybillPriceModel;
  5. use App\City;
  6. use App\Events\WaybillPriceModelEvent;
  7. use App\Province;
  8. use App\Unit;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Cache;
  11. use Maatwebsite\Excel\Concerns\ToCollection;
  12. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  13. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  14. HeadingRowFormatter::default('none');
  15. class WaybillPriceModelsImport implements ToCollection,WithHeadingRow
  16. {
  17. protected $isOverride;
  18. public function __construct($isOverride)
  19. {
  20. if ($isOverride==1){
  21. $this->isOverride=true;
  22. }
  23. }
  24. /**
  25. * @param Collection $collection
  26. */
  27. public function Collection(Collection $collection)
  28. {
  29. $endIs=false;
  30. $cityIs=true;
  31. if (isset($collection->toArray()[0]['承运商'])&&isset($collection->toArray()[0]['计数单位'])&&isset($collection->toArray()[0]['省份'])
  32. &&isset($collection->toArray()[0]['单价'])){
  33. $endIs=true;
  34. }else Cache::put('error','请检查您第一行标题是否存在承运商,计数单位,省份,单价,市,计数区间,起步费,最低计数',86400);
  35. $exception=[];
  36. $sum=2;
  37. if ($endIs) {
  38. foreach ($collection as $row) {
  39. if ($row['承运商'] && $row['计数单位'] && $row['省份'] && $row['单价']) {
  40. if (strstr($row['省份'], '省')){$row['省份']=str_replace('省','',$row['省份']);};
  41. $logistic = Logistic::query()->where('name', $row['承运商'])->first();
  42. $unit = Unit::query()->where('name', $row['计数单位'])->first();
  43. $province = Province::query()->where('name', $row['省份'])->first();
  44. $city = City::query()->where('name', $row['市'])->first();
  45. if ($logistic && $unit && $province) {
  46. $billing = WaybillPriceModel::query()->where('logistic_id', $logistic->id);
  47. $billing = $billing->where('unit_id', $unit->id);
  48. $billing = $billing->where('province_id', $province->id);
  49. if ($row['市']) {
  50. $billing = $billing->where('city_id', $city->id);
  51. }
  52. if ($row['计数区间'] && !strstr($row['计数区间'], '∞')) {
  53. $str = explode('-', $row['计数区间']);
  54. if (count($str) == 2) {
  55. $billing = $billing->where('range_min', $str[0]);
  56. $billing = $billing->where('range_max', $str[1]);
  57. }
  58. }
  59. $billing = $billing->first();
  60. if (!$billing) {
  61. if (isset($waybillPriceModel['city_id'])){
  62. $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND city_id IS NULL',[$logistic->id,$province->id])->first();
  63. if ($waybillPriceModelProvince){
  64. $cityIs=false;
  65. array_push($exception, ['第' . $sum . '行数据已存在省份模型,无需录入城市模型']);
  66. }
  67. }else{
  68. $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND city_id IS NOT NULL',[$logistic->id,$province->id])->first();
  69. if ($waybillPriceModelProvince){
  70. $cityIs=false;
  71. array_push($exception, ['第' . $sum . '行数据已存在城市模型,无法录入省份模型']);
  72. }
  73. }
  74. if ($cityIs){
  75. $waybillPriceModel = ['logistic_id' => $logistic->id, 'unit_id' => $unit->id, 'province_id' => $province->id, 'unit_price' => $row['单价']];
  76. if ($row['计数区间']) {
  77. $str = explode('-', $row['计数区间']);
  78. if (preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[0]) >= 0
  79. && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[1]) > 0
  80. && $str[0] < $str[1] && count($str) == 2
  81. ) {
  82. $waybillPriceModel = array_merge($waybillPriceModel, ['range_min' => $str[0], 'range_max' => $str[1]]);
  83. } else {
  84. array_push($exception, ['第' . $sum . '行数据计数区间数据有误,应为:最小值-最大值']);
  85. }
  86. }
  87. if ($row['市']) {
  88. if ($city) {
  89. $waybillPriceModel = array_merge($waybillPriceModel, ['city_id' => $city->id]);
  90. }
  91. }
  92. if ($row['起步费'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['起步费']) > 0) {
  93. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => $row['起步费']]);
  94. } else {
  95. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => 0]);
  96. array_push($exception, ['第' . $sum . '行数据起步费为空,或为负,已默认设为0']);
  97. }
  98. if ($row['最低计数'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['最低计数']) > 0) {
  99. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => $row['最低计数']]);
  100. } else {
  101. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => 0]);
  102. array_push($exception, ['第' . $sum . '行数据最低计数为空,或为负,已默认设为0']);
  103. }
  104. $waybillPriceModel=WaybillPriceModel::query()->create($waybillPriceModel);
  105. /** @var WaybillPriceModel $waybillPriceModel */
  106. event(new WaybillPriceModelEvent($waybillPriceModel));
  107. }
  108. } else {
  109. if ($this->isOverride) {
  110. $waybillPriceModel = ['logistic_id' => $logistic->id, 'unit_id' => $unit->id, 'province_id' => $province->id, 'unit_price' => $row['单价']];
  111. if ($row['计数区间']&& !strstr($row['计数区间'], '∞')) {
  112. $str = explode('-', $row['计数区间']);
  113. if (preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[0]) > 0
  114. && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[1]) > 0
  115. && $str[0] < $str[1] && count($str) == 2
  116. ) {
  117. $waybillPriceModel = array_merge($waybillPriceModel, ['range_min' => $str[0], 'range_max' => $str[1]]);
  118. } else {
  119. array_push($exception, ['第' . $sum . '行数据计数区间数据有误,应为:最小值-最大值']);
  120. }
  121. }
  122. if ($row['市']) {
  123. if ($city) {
  124. $waybillPriceModel = array_merge($waybillPriceModel, ['city_id' => $city->id]);
  125. }
  126. }
  127. if ($row['起步费'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['起步费']) > 0) {
  128. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => $row['起步费']]);
  129. } else {
  130. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => 0]);
  131. array_push($exception, ['第' . $sum . '行数据起步费为空,或为负,已默认设为0']);
  132. }
  133. if ($row['最低计数'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['最低计数']) > 0) {
  134. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => $row['最低计数']]);
  135. } else {
  136. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => 0]);
  137. array_push($exception, ['第' . $sum . '行数据最低计数为空,或为负,已默认设为0']);
  138. }
  139. $billing->fill($waybillPriceModel);
  140. if ($billing->save()) {
  141. /** @var WaybillPriceModel $billing */
  142. event(new WaybillPriceModelEvent($billing));
  143. array_push($exception, ['第' . $sum . '行数据已覆盖原计费模型']);
  144. }
  145. } else {
  146. array_push($exception, ['第' . $sum . '行数据在计费模型中已存在']);
  147. }
  148. }
  149. } else {
  150. array_push($exception, ['第' . $sum . '行数据承运商,单位,省在基础数据中未定义']);
  151. }
  152. } else {
  153. array_push($exception, ['第' . $sum . '行数据必填项为空']);
  154. }
  155. $sum++;
  156. }
  157. Cache::put('exception', $exception, 86400);
  158. }
  159. }
  160. }