WaybillPriceModelsImport.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Imports;
  3. use App\WaybillPriceModel;
  4. use App\Carrier;
  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{
  35. Cache::put('error','请检查您第一行标题是否存在承运商,计数单位,省份,单价,市,计数区间,起步费,最低计数',86400);
  36. $endIs=false;
  37. }
  38. $exception=[];
  39. $sum=2;
  40. if ($endIs) {
  41. foreach ($collection as $row) {
  42. if ($row['承运商'] && $row['计数单位'] && $row['省份'] && $row['单价']) {
  43. if (strstr($row['省份'], '省')){$row['省份']=str_replace('省','',$row['省份']);};
  44. $carrier = Carrier::where('name', $row['承运商'])->first();
  45. $unit = Unit::where('name', $row['计数单位'])->first();
  46. $province = Province::where('name', $row['省份'])->first();
  47. $city = City::where('name', $row['市'])->first();
  48. if ($carrier && $unit && $province) {
  49. $billing = WaybillPriceModel::where('carrier_id', $carrier->id);
  50. $billing = $billing->where('unit_id', $unit->id);
  51. $billing = $billing->where('province_id', $province->id);
  52. if ($row['市']) {
  53. $billing = $billing->where('city_id', $city->id);
  54. }
  55. if ($row['计数区间'] && !strstr($row['计数区间'], '∞')) {
  56. $str = explode('-', $row['计数区间']);
  57. if (count($str) == 2) {
  58. $billing = $billing->where('range_min', $str[0]);
  59. $billing = $billing->where('range_max', $str[1]);
  60. }
  61. }
  62. $billing = $billing->first();
  63. if (!$billing) {
  64. if (isset($waybillPriceModel['city_id'])){
  65. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND city_id IS NULL',[$carrier->id,$province->id])->first();
  66. if ($waybillPriceModelProvince){
  67. $cityIs=false;
  68. array_push($exception, ['第' . $sum . '行数据已存在省份模型,无需录入城市模型']);
  69. }
  70. }else{
  71. $waybillPriceModelProvince=WaybillPriceModel::whereRaw('carrier_id = ? AND province_id = ? AND city_id IS NOT NULL',[$carrier->id,$province->id])->first();
  72. if ($waybillPriceModelProvince){
  73. $cityIs=false;
  74. array_push($exception, ['第' . $sum . '行数据已存在城市模型,无法录入省份模型']);
  75. }
  76. }
  77. if ($cityIs){
  78. $waybillPriceModel = ['carrier_id' => $carrier->id, 'unit_id' => $unit->id, 'province_id' => $province->id, 'unit_price' => $row['单价']];
  79. if ($row['计数区间']) {
  80. $str = explode('-', $row['计数区间']);
  81. if (preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[0]) >= 0
  82. && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[1]) > 0
  83. && $str[0] < $str[1] && count($str) == 2
  84. ) {
  85. $waybillPriceModel = array_merge($waybillPriceModel, ['range_min' => $str[0], 'range_max' => $str[1]]);
  86. } else {
  87. array_push($exception, ['第' . $sum . '行数据计数区间数据有误,应为:最小值-最大值']);
  88. }
  89. }
  90. if ($row['市']) {
  91. if ($city) {
  92. $waybillPriceModel = array_merge($waybillPriceModel, ['city_id' => $city->id]);
  93. }
  94. }
  95. if ($row['起步费'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['起步费']) > 0) {
  96. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => $row['起步费']]);
  97. } else {
  98. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => 0]);
  99. array_push($exception, ['第' . $sum . '行数据起步费为空,或为负,已默认设为0']);
  100. }
  101. if ($row['最低计数'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['最低计数']) > 0) {
  102. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => $row['最低计数']]);
  103. } else {
  104. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => 0]);
  105. array_push($exception, ['第' . $sum . '行数据最低计数为空,或为负,已默认设为0']);
  106. }
  107. $waybillPriceModel=WaybillPriceModel::create($waybillPriceModel);
  108. event(new WaybillPriceModelEvent($waybillPriceModel));
  109. }
  110. } else {
  111. if ($this->isOverride) {
  112. $waybillPriceModel = ['carrier_id' => $carrier->id, 'unit_id' => $unit->id, 'province_id' => $province->id, 'unit_price' => $row['单价']];
  113. if ($row['计数区间']&& !strstr($row['计数区间'], '∞')) {
  114. $str = explode('-', $row['计数区间']);
  115. if (preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[0]) > 0
  116. && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $str[1]) > 0
  117. && $str[0] < $str[1] && count($str) == 2
  118. ) {
  119. $waybillPriceModel = array_merge($waybillPriceModel, ['range_min' => $str[0], 'range_max' => $str[1]]);
  120. } else {
  121. array_push($exception, ['第' . $sum . '行数据计数区间数据有误,应为:最小值-最大值']);
  122. }
  123. }
  124. if ($row['市']) {
  125. if ($city) {
  126. $waybillPriceModel = array_merge($waybillPriceModel, ['city_id' => $city->id]);
  127. }
  128. }
  129. if ($row['起步费'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['起步费']) > 0) {
  130. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => $row['起步费']]);
  131. } else {
  132. $waybillPriceModel = array_merge($waybillPriceModel, ['base_fee' => 0]);
  133. array_push($exception, ['第' . $sum . '行数据起步费为空,或为负,已默认设为0']);
  134. }
  135. if ($row['最低计数'] && preg_match('/^[1-9]\d*\,\d*|[1-9]\d*$/', $row['最低计数']) > 0) {
  136. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => $row['最低计数']]);
  137. } else {
  138. $waybillPriceModel = array_merge($waybillPriceModel, ['initial_weight' => 0]);
  139. array_push($exception, ['第' . $sum . '行数据最低计数为空,或为负,已默认设为0']);
  140. }
  141. $billing->fill($waybillPriceModel);
  142. if ($billing->save()) {
  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. }