LogisticNumberFeatureController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Logistic;
  4. use App\LogisticNumberFeature;
  5. use App\RejectedBill;
  6. use Carbon\Carbon;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. class LogisticNumberFeatureController extends Controller
  10. {
  11. public function index()
  12. {
  13. //
  14. }
  15. public function create()
  16. {
  17. //
  18. }
  19. public function store(Request $request)
  20. {
  21. //
  22. }
  23. public function show(LogisticNumberFeature $logisticNumberFeature)
  24. {
  25. //
  26. }
  27. public function edit(LogisticNumberFeature $logisticNumberFeature)
  28. {
  29. //
  30. }
  31. public function update(Request $request, LogisticNumberFeature $logisticNumberFeature)
  32. {
  33. //
  34. }
  35. public function destroy(LogisticNumberFeature $logisticNumberFeature)
  36. {
  37. //
  38. }
  39. public function apiComputeLogisticByNumber(Request $request)
  40. {
  41. $logisticNumber=$request->input('logistic_number_return');
  42. if(!$logisticNumber){
  43. return ['success'=>'false',];
  44. }
  45. $logistic=$this->getLogisticByFeatures($logisticNumber);
  46. if(!$logistic){
  47. return ['success'=>'false',];
  48. }
  49. return ['success'=>'true','logistic'=>$logistic];
  50. }
  51. public function getLogisticByFeatures(string $logisticNumber)
  52. {
  53. $numberLength=strlen($logisticNumber);
  54. $char0 = mb_strcut($logisticNumber, 0, 1);
  55. $logistic_items = DB::table(DB::raw("(select logistic_id,updated_at from logistic_number_features as a where logistic_id in (select logistic_id from
  56. (select logistic_id from logistic_number_features where name='length' and value='$numberLength') feLen) and
  57. (name='char0' and value='$char0')) as tableA")
  58. )->get();
  59. $targetsIds=$logistic_items->map(function($id){return $id->logistic_id;})->unique()->toArray();
  60. for ($i=1;$i<LogisticNumberFeature::$featureConsideringLength;$i++){
  61. if(count($targetsIds)<=1)break;
  62. $paramName='char'.$i;
  63. $$paramName = mb_strcut($logisticNumber, 0, 2);
  64. $logistic_itemsTemp=LogisticNumberFeature::select(['logistic_id','updated_at'])->where('name',$paramName)
  65. ->where('value',$$paramName)->whereIn('logistic_id',$targetsIds)->get();
  66. if($logistic_itemsTemp->isNotEmpty()){
  67. $targetsIds=$logistic_itemsTemp->map(function($id){return $id->logistic_id;})->unique()->toArray();
  68. $logistic_items=$logistic_itemsTemp;
  69. }
  70. }
  71. if(count($targetsIds)==0)return null;
  72. $result_logistic_id=$targetsIds[0];
  73. if(count($targetsIds)>1){
  74. $finalTarget=$logistic_items->reduce(function ($carry,$item){
  75. if($carry){
  76. if($carry->updated_at > $item->updated_at){
  77. return $carry;
  78. }
  79. return $item;
  80. }
  81. return $item;
  82. });
  83. $result_logistic_id=$finalTarget->logistic_id;
  84. }
  85. return Logistic::find($result_logistic_id);
  86. }
  87. public function createFeatures(string $logisticNumber,int $logisticId)
  88. {
  89. $logisticNumber = trim($logisticNumber);
  90. $featureConsideringLength=LogisticNumberFeature::$featureConsideringLength;
  91. $numberLength=strlen($logisticNumber);
  92. if($numberLength<$featureConsideringLength)
  93. $featureConsideringLength=$numberLength;
  94. if(!$logisticNumber){
  95. app('LogService')->log(__METHOD__, 'error', '创建退货快递单号特征时,单号传入了空值');
  96. return null;
  97. };
  98. $featuresCreated=[];
  99. $feature=$this->createFeature($logisticId,'length',$numberLength,100);
  100. if($feature)$featuresCreated[]=$feature;
  101. for($i=0;$i<$featureConsideringLength;$i++){
  102. $chars=mb_strcut($logisticNumber,0,$i+1);
  103. $feature=$this->createFeature($logisticId,"char$i",$chars,99-$i);
  104. if($feature)$featuresCreated[]=$feature;
  105. }
  106. return $featuresCreated;
  107. }
  108. private function createFeature(int $logisticId,string $featureName, string $value,int $weight)
  109. {
  110. if(!$value){
  111. app('LogService')->log(__METHOD__, 'error', "创建退货快递单号特征{$featureName}时,特征值为空");
  112. return null;
  113. };
  114. $feature=LogisticNumberFeature::where('logistic_id',$logisticId)
  115. ->where('name',$featureName)->where('value',$value)->first();
  116. if($feature){
  117. $feature->touch();
  118. }else{
  119. $feature=new LogisticNumberFeature(['logistic_id'=>$logisticId,'name'=>$featureName,'value'=>$value,'weight'=>$weight]);
  120. $feature->save();
  121. return $feature;
  122. }
  123. return null;
  124. }
  125. static public function loadRecentRejectedsToFeatures($days,$limit){
  126. $rejecteds=RejectedBill::where('created_at','>',Carbon::today()->subDays($days))->limit($limit)->get();
  127. $featureController=new LogisticNumberFeatureController();
  128. $rejecteds->each(function ($rejected)use($featureController){
  129. if($rejected['logistic_number_return'])
  130. $featureController->createFeatures($rejected['logistic_number_return'],$rejected['id_logistic_return']);
  131. });
  132. }
  133. }