WaybillPriceModelsImport.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if ($city) $billing = $billing->where('city_id', $city->id);
  51. else $billing->whereNull("city_id");
  52. }
  53. if ($row['计数区间'] && !strstr($row['计数区间'], '∞')) {
  54. $str = explode('-', $row['计数区间']);
  55. if (count($str) == 2) {
  56. $billing = $billing->where('range_min', $str[0]);
  57. $billing = $billing->where('range_max', $str[1]);
  58. }
  59. }
  60. $billing = $billing->first();
  61. if (!$billing) {
  62. if ($row['市']){
  63. $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND city_id IS NULL',[$logistic->id,$province->id])->first();
  64. if ($waybillPriceModelProvince){
  65. $cityIs=false;
  66. array_push($exception, ['第' . $sum . '行数据已存在省份模型,无需录入城市模型']);
  67. }
  68. }else{
  69. $waybillPriceModelProvince=WaybillPriceModel::query()->whereRaw('logistic_id = ? AND province_id = ? AND city_id IS NOT NULL',[$logistic->id,$province->id])->first();
  70. if ($waybillPriceModelProvince){
  71. $cityIs=false;
  72. array_push($exception, ['第' . $sum . '行数据已存在城市模型,无法录入省份模型']);
  73. }
  74. }
  75. if ($cityIs){
  76. $waybillPriceModel = ['logistic_id' => $logistic->id, 'unit_id' => $unit->id, 'province_id' => $province->id, 'unit_price' => $row['单价']];
  77. if ($row['计数区间']) {
  78. $str = explode('-', $row['计数区间']);
  79. if (preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[0]) >= 0
  80. && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[1]) > 0
  81. && $str[0] < $str[1] && count($str) == 2
  82. ) {
  83. $waybillPriceModel = array_merge($waybillPriceModel, ['range_min' => $str[0], 'range_max' => $str[1]]);
  84. } else {
  85. array_push($exception, ['第' . $sum . '行数据计数区间数据有误,应为:最小值-最大值']);
  86. }
  87. }
  88. if ($row['市']) {
  89. if ($city) {
  90. $waybillPriceModel = array_merge($waybillPriceModel, ['city_id' => $city->id]);
  91. }
  92. }
  93. if ($row['起步费'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['起步费']) > 0) {
  94. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => $row['起步费']]);
  95. } else {
  96. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => 0]);
  97. array_push($exception, ['第' . $sum . '行数据起步费为空,或为负,已默认设为0']);
  98. }
  99. if ($row['最低计数'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['最低计数']) > 0) {
  100. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => $row['最低计数']]);
  101. } else {
  102. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => 0]);
  103. array_push($exception, ['第' . $sum . '行数据最低计数为空,或为负,已默认设为0']);
  104. }
  105. $waybillPriceModel=WaybillPriceModel::query()->create($waybillPriceModel);
  106. /** @var WaybillPriceModel $waybillPriceModel */
  107. event(new WaybillPriceModelEvent($waybillPriceModel));
  108. }
  109. } else {
  110. if ($this->isOverride) {
  111. $waybillPriceModel = ['logistic_id' => $logistic->id, 'unit_id' => $unit->id, 'province_id' => $province->id, 'unit_price' => $row['单价']];
  112. if ($row['计数区间']&& !strstr($row['计数区间'], '∞')) {
  113. $str = explode('-', $row['计数区间']);
  114. if (preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[0]) > 0
  115. && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[1]) > 0
  116. && $str[0] < $str[1] && count($str) == 2
  117. ) {
  118. $waybillPriceModel = array_merge($waybillPriceModel, ['range_min' => $str[0], 'range_max' => $str[1]]);
  119. } else {
  120. array_push($exception, ['第' . $sum . '行数据计数区间数据有误,应为:最小值-最大值']);
  121. }
  122. }
  123. if ($row['市']) {
  124. if ($city) {
  125. $waybillPriceModel = array_merge($waybillPriceModel, ['city_id' => $city->id]);
  126. }
  127. }
  128. if ($row['起步费'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['起步费']) > 0) {
  129. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => $row['起步费']]);
  130. } else {
  131. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => 0]);
  132. array_push($exception, ['第' . $sum . '行数据起步费为空,或为负,已默认设为0']);
  133. }
  134. if ($row['最低计数'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['最低计数']) > 0) {
  135. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => $row['最低计数']]);
  136. } else {
  137. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => 0]);
  138. array_push($exception, ['第' . $sum . '行数据最低计数为空,或为负,已默认设为0']);
  139. }
  140. $billing->fill($waybillPriceModel);
  141. if ($billing->save()) {
  142. /** @var WaybillPriceModel $billing */
  143. event(new WaybillPriceModelEvent($billing));
  144. array_push($exception, ['第' . $sum . '行数据已覆盖原计费模型']);
  145. }
  146. } else {
  147. array_push($exception, ['第' . $sum . '行数据在计费模型中已存在']);
  148. }
  149. }
  150. } else {
  151. array_push($exception, ['第' . $sum . '行数据承运商,单位,省在基础数据中未定义']);
  152. }
  153. } else {
  154. array_push($exception, ['第' . $sum . '行数据必填项为空']);
  155. }
  156. $sum++;
  157. }
  158. Cache::put('exception', $exception, 86400);
  159. }
  160. }
  161. }