OwnerPriceLogisticDetailImport.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Imports;
  3. use App\City;
  4. use App\OwnerPriceLogistic;
  5. use App\OwnerPriceLogisticDetail;
  6. use App\Province;
  7. use App\Services\common\BatchUpdateService;
  8. use App\Services\LogService;
  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 OwnerPriceLogisticDetailImport implements ToCollection,WithHeadingRow
  16. {
  17. protected $logistic;
  18. public function __construct(OwnerPriceLogistic $logistic)
  19. {
  20. $this->logistic = $logistic;
  21. }
  22. /**
  23. * @param Collection $collection
  24. * @return bool
  25. */
  26. public function collection(Collection $collection)
  27. {
  28. if (!$this->logistic){
  29. Cache::put("logistic",["success"=>false, "data"=>"不存在父级"],86400);
  30. return false;
  31. }
  32. $row = $collection->first();
  33. $header = [
  34. "计数单位","计数区间","省份","市","单价","送货费","起始计费","起始计数","费率"
  35. ];
  36. foreach ($header as $str){
  37. if (!isset($row[$str])){
  38. Cache::put("logistic",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
  39. return false;
  40. }
  41. }
  42. //省份map
  43. $map = [];
  44. $provinces = Province::query()->get();
  45. foreach ($provinces as $province){
  46. $map[$province->name] = $province->id;
  47. }
  48. //市map
  49. $cityMap = [];
  50. $cityMappingProvince = [];
  51. $cities = City::query()->get();
  52. foreach ($cities as $city){
  53. $cityMap[$city->name] = $city->id;
  54. $cityMappingProvince[$city->id] = $city->province_id;
  55. }
  56. //对比单位
  57. $unit = $this->logistic->unit ? strtoupper($this->logistic->unit->name) : '';
  58. $otherUnit = $this->logistic->otherUnit ? strtoupper($this->logistic->otherUnit->name) : '';
  59. //对比区间
  60. $range = [
  61. $unit => explode(",",$this->logistic->unit_range),
  62. $otherUnit => explode(",",$this->logistic->other_unit_range),
  63. ];
  64. //已存在的计费
  65. $existDetails = [];
  66. foreach ($this->logistic->details as $detail){
  67. $existDetails[$detail->unit_id.'_'.$detail->range."_".$detail->province_id."_".$detail->city_id] = $detail->id;
  68. }
  69. //生成列表内的重复条目
  70. $existInsert = [];
  71. //导入的数据整理,存在更新
  72. $id = $this->logistic->id;
  73. $errors = [];
  74. $insert = [];
  75. $update = [["id","unit_price","delivery_fee","initial_fee","initial_amount","rate","updated_at"]];
  76. $date = date('Y-m-d H:i:s');
  77. foreach ($collection as $index => $item){
  78. /* 数据校验 */
  79. if (!$item["省份"]){
  80. $errors[] = "第“".($index+2)."”行省份为空";
  81. continue;
  82. }else{
  83. if ((!isset($map[$item["省份"]]) && !isset($map[mb_substr($item["省份"], 0,-1)]))){
  84. $errors[] = "第“".($index+2)."”行未知省份";
  85. continue;
  86. }
  87. if (isset($map[mb_substr($item["省份"], 0,-1)]))$item["省份"] = mb_substr($item["省份"], 0,-1);
  88. }
  89. if (!$item["计数单位"] || (strtoupper($item["计数单位"]) != $unit && strtoupper($item["计数单位"]) != $otherUnit)){
  90. //$errors[] = "第“".($index+2)."”行单位不符合";
  91. //continue;
  92. Cache::put("logistic",["success"=>false, "data"=>"第“".($index+2)."”行单位不符合"],86400);
  93. return false;
  94. }
  95. if (!$item["计数区间"] || array_search($item["计数区间"],$range[strtoupper($item["计数单位"])]) === false){
  96. //$errors[] = "第“".($index+2)."”行非法首重价格";
  97. //continue;
  98. Cache::put("logistic",["success"=>false, "data"=>"第“".($index+2)."”行区间不符合"],86400);
  99. return false;
  100. }
  101. if (!isset($cityMap[$item["市"]])){
  102. $errors[] = "第“".($index+2)."”行未知城市";
  103. continue;
  104. }
  105. if (!$item["单价"] || !is_numeric($item["单价"]) || $item["单价"] <= 0){
  106. $errors[] = "第“".($index+2)."”行非法单价";
  107. continue;
  108. }
  109. $numeric = ["送货费","起始计费","起始计数","费率"];
  110. $sign = false;
  111. foreach ($numeric as $column){
  112. if ($item[$column] && (!is_numeric($item[$column]) || $item[$column] <= 0)){
  113. $errors[] = "第“".($index+2)."”行非法".$column;
  114. $sign = true;
  115. break;
  116. }
  117. }
  118. if ($sign)continue;
  119. /* 数据转换及存在校验 */
  120. if ($cityMappingProvince[$cityMap[$item["市"]]] != $map[$item["省份"]]){
  121. $errors[] = "第“".($index+2)."”行城市不属于该省份";
  122. continue;
  123. }
  124. $item["省份"] = $map[$item["省份"]];
  125. $item["计数单位"] = strtoupper($item["计数单位"]) == $unit ? $this->logistic->unit_id : $this->logistic->other_unit_id;
  126. $item["市"] = $cityMap[$item["市"]];
  127. $key = $item["计数单位"]."_".$item["计数区间"]."_".$item["省份"]."_".$item["市"];
  128. if (isset($existInsert[$key])){
  129. $errors[] = "第“".($index+2)."”行与第“".$existInsert[$key]."”行重复";
  130. continue;
  131. }
  132. if (isset($existDetails[$key])){
  133. $update[] = [
  134. "id" => $existDetails[$key],
  135. "unit_price" => $item["单价"],
  136. "delivery_fee" => $item["送货费"],
  137. "initial_fee" => $item["起始计费"],
  138. "initial_amount" => $item["起始计数"],
  139. "rate" => $item["费率"],
  140. "updated_at" => $date,
  141. ];
  142. continue;
  143. }
  144. $insert[] = [
  145. "owner_price_logistic_id" => $id,
  146. "unit_id" => $item["计数单位"],
  147. "range" => $item["计数区间"],
  148. "province_id" => $item["省份"],
  149. "city_id" => $item["市"],
  150. "unit_price" => $item["单价"],
  151. "delivery_fee" => $item["送货费"],
  152. "initial_fee" => $item["起始计费"],
  153. "initial_amount" => $item["起始计数"],
  154. "rate" => $item["费率"],
  155. "created_at" => $date,
  156. ];
  157. $existInsert[$key] = $index+2;
  158. }
  159. if (count($update) > 1){
  160. app(BatchUpdateService::class)->batchUpdate("owner_price_logistic_details",$update);
  161. LogService::log(__METHOD__,"物流计费导入修改",json_encode($update));
  162. }
  163. if (count($insert) > 0){
  164. OwnerPriceLogisticDetail::query()->insert($insert);
  165. LogService::log(__METHOD__,"物流计费导入录入",json_encode($insert));
  166. }
  167. $this->logistic->load(["details"=>function($query){$query->with(["province","unit","city"]);}]);
  168. Cache::put("logistic",["success"=>true,"data"=>$this->logistic->details,"errors"=>$errors],86400);
  169. return true;
  170. }
  171. }