WaybillPriceModelsImport.php 10 KB

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