LogisticNumberFeatureController.php 5.3 KB

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