OwnerPriceLogisticDetailImport.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Imports;
  3. use App\City;
  4. use App\OwnerPriceLogisticDetail;
  5. use App\Province;
  6. use App\Services\common\BatchUpdateService;
  7. use App\Services\LogService;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  12. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  13. HeadingRowFormatter::default('none');
  14. class OwnerPriceLogisticDetailImport implements ToCollection,WithHeadingRow
  15. {
  16. protected $logistic;
  17. public function __construct($logistic)
  18. {
  19. $this->logistic = $logistic;
  20. }
  21. /**
  22. * @param Collection $collection
  23. * @return bool
  24. */
  25. public function collection(Collection $collection)
  26. {
  27. $row = $collection->first();
  28. $header = [
  29. "计数单位","计数区间","省份","市","单价","送货费","起始计费","起始计数","费率"
  30. ];
  31. foreach ($header as $str){
  32. if (!isset($row[$str])){
  33. Cache::put("logistic",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
  34. return false;
  35. }
  36. }
  37. //省份map
  38. $map = [];
  39. $provinces = Province::query()->get();
  40. foreach ($provinces as $province){
  41. $map[$province->name] = $province->id;
  42. }
  43. //市map
  44. $cityMap = [];
  45. $cityMappingProvince = [];
  46. $cities = City::query()->get();
  47. foreach ($cities as $city){
  48. $cityMap[$city->name] = $city->id;
  49. $cityMappingProvince[$city->id] = $city->province_id;
  50. }
  51. if (!$this->logistic)
  52. return $this->readonly($collection, $map, $cityMap, $cityMappingProvince);
  53. if (!$this->logistic->operation){
  54. $this->logistic = app("OwnerPriceLogisticService")->copy($this->express);
  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. Cache::put("logistic",["success"=>false, "data"=>"第“".($index+2)."”行单位不符合"],86400);
  91. return false;
  92. }
  93. if (!$item["计数区间"] || array_search($item["计数区间"],$range[strtoupper($item["计数单位"])]) === false){
  94. Cache::put("logistic",["success"=>false, "data"=>"第“".($index+2)."”行区间不符合"],86400);
  95. return false;
  96. }
  97. if (!isset($cityMap[$item["市"]])){
  98. $errors[] = "第“".($index+2)."”行未知城市";
  99. continue;
  100. }
  101. if (!$item["单价"] || !is_numeric($item["单价"]) || $item["单价"] <= 0){
  102. $errors[] = "第“".($index+2)."”行非法单价";
  103. continue;
  104. }
  105. $numeric = ["送货费","起始计费","起始计数","费率"];
  106. $sign = false;
  107. foreach ($numeric as $column){
  108. if ($item[$column] && (!is_numeric($item[$column]) || $item[$column] <= 0)){
  109. $errors[] = "第“".($index+2)."”行非法".$column;
  110. $sign = true;
  111. break;
  112. }
  113. }
  114. if ($sign)continue;
  115. /* 数据转换及存在校验 */
  116. if ($cityMappingProvince[$cityMap[$item["市"]]] != $map[$item["省份"]]){
  117. $errors[] = "第“".($index+2)."”行城市不属于该省份";
  118. continue;
  119. }
  120. $item["省份"] = $map[$item["省份"]];
  121. $item["市"] = $cityMap[$item["市"]];
  122. $item["计数单位"] = strtoupper($item["计数单位"]) == $unit ? $this->logistic->unit_id : $this->logistic->other_unit_id;
  123. $key = $item["计数单位"]."_".$item["计数区间"]."_".$item["省份"]."_".$item["市"];
  124. if (isset($existInsert[$key])){
  125. $errors[] = "第“".($index+2)."”行与第“".$existInsert[$key]."”行重复";
  126. continue;
  127. }
  128. if (isset($existDetails[$key])){
  129. $update[] = [
  130. "id" => $existDetails[$key],
  131. "unit_price" => $item["单价"],
  132. "delivery_fee" => $item["送货费"],
  133. "initial_fee" => $item["起始计费"],
  134. "initial_amount" => $item["起始计数"],
  135. "rate" => $item["费率"],
  136. "updated_at" => $date,
  137. ];
  138. continue;
  139. }
  140. $insert[] = [
  141. "owner_price_logistic_id" => $id,
  142. "unit_id" => $item["计数单位"],
  143. "range" => $item["计数区间"],
  144. "province_id" => $item["省份"],
  145. "city_id" => $item["市"],
  146. "unit_price" => $item["单价"],
  147. "delivery_fee" => $item["送货费"],
  148. "initial_fee" => $item["起始计费"],
  149. "initial_amount" => $item["起始计数"],
  150. "rate" => $item["费率"],
  151. "created_at" => $date,
  152. ];
  153. $existInsert[$key] = $index+2;
  154. }
  155. if (count($update) > 1){
  156. app(BatchUpdateService::class)->batchUpdate("owner_price_logistic_details",$update);
  157. LogService::log(__METHOD__,"物流计费导入修改",json_encode($update));
  158. }
  159. if (count($insert) > 0){
  160. OwnerPriceLogisticDetail::query()->insert($insert);
  161. LogService::log(__METHOD__,"物流计费导入录入",json_encode($insert));
  162. }
  163. $this->logistic->load(["details"=>function($query){$query->with(["province","unit","city"]);}]);
  164. Cache::put("logistic",["success"=>true,"data"=>$this->logistic->details,"errors"=>$errors],86400);
  165. return true;
  166. }
  167. public function readonly(Collection $collection, array $map, array $cityMap, array $cityMappingProvince)
  168. {
  169. //生成列表内的重复条目
  170. $existInsert = [];
  171. //符合数据
  172. $data = [];
  173. //错误信息
  174. $errors = [];
  175. foreach ($collection as $index => $item){
  176. /* 数据校验 */
  177. if (!$item["省份"]){
  178. $errors[] = "第“".($index+2)."”行省份为空";
  179. continue;
  180. }else{
  181. if ((!isset($map[$item["省份"]]) && !isset($map[mb_substr($item["省份"], 0,-1)]))){
  182. $errors[] = "第“".($index+2)."”行未知省份";
  183. continue;
  184. }
  185. if (isset($map[mb_substr($item["省份"], 0,-1)]))$item["省份"] = mb_substr($item["省份"], 0,-1);
  186. }
  187. if (!isset($cityMap[$item["市"]])){
  188. $errors[] = "第“".($index+2)."”行未知城市";
  189. continue;
  190. }
  191. if (!$item["单价"] || !is_numeric($item["单价"]) || $item["单价"] <= 0){
  192. $errors[] = "第“".($index+2)."”行非法单价";
  193. continue;
  194. }
  195. $numeric = ["送货费","起始计费","起始计数","费率"];
  196. $sign = false;
  197. foreach ($numeric as $column){
  198. if ($item[$column] && (!is_numeric($item[$column]) || $item[$column] <= 0)){
  199. $errors[] = "第“".($index+2)."”行非法".$column;
  200. $sign = true;
  201. break;
  202. }
  203. }
  204. if ($sign)continue;
  205. /* 数据转换及存在校验 */
  206. if ($cityMappingProvince[$cityMap[$item["市"]]] != $map[$item["省份"]]){
  207. $errors[] = "第“".($index+2)."”行城市不属于该省份";
  208. continue;
  209. }
  210. $item["省份"] = $map[$item["省份"]];
  211. $item["市"] = $cityMap[$item["市"]];
  212. $key = $item["计数单位"]."_".$item["计数区间"]."_".$item["省份"]."_".$item["市"];
  213. if (isset($existInsert[$key])){
  214. $errors[] = "第“".($index+2)."”行与第“".$existInsert[$key]."”行重复";
  215. continue;
  216. }
  217. $data[] = [
  218. "unit_id" => $item["计数单位"],
  219. "range" => $item["计数区间"],
  220. "province_id" => $item["省份"],
  221. "city_id" => $item["市"],
  222. "unit_price" => $item["单价"],
  223. "delivery_fee" => $item["送货费"],
  224. "initial_fee" => $item["起始计费"],
  225. "initial_amount" => $item["起始计数"],
  226. "rate" => $item["费率"],
  227. ];
  228. $existInsert[$key] = $index+2;
  229. }
  230. Cache::put("logistic",["success"=>true,"data"=>$data,"errors"=>$errors],86400);
  231. return true;
  232. }
  233. }