LogisticNumberFeatureController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $logistic=$this->getLogisticByFeatures($logisticNumber);
  44. if(!$logistic){
  45. return ['success'=>'false',];
  46. }
  47. return ['success'=>'true','logistic'=>$logistic];
  48. }
  49. private function getLogisticByFeatures(string $logisticNumber)
  50. {
  51. $numberLength=strlen($logisticNumber);
  52. $char0 = mb_strcut($logisticNumber, 0, 1);
  53. $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
  54. (select logistic_id from logistic_number_features where name='length' and value='$numberLength') feLen) and
  55. (name='char0' and value='$char0')) as tableA")
  56. )->get();
  57. $targetsIds=$logistic_items->map(function($id){return $id->logistic_id;})->unique()->toArray();
  58. for ($i=1;$i<LogisticNumberFeature::$featureConsideringLength;$i++){
  59. if(count($targetsIds)<=1)break;
  60. $paramName='char'.$i;
  61. $$paramName = mb_strcut($logisticNumber, 0, 2);
  62. $logistic_itemsTemp=LogisticNumberFeature::select(['logistic_id','updated_at'])->where('name',$paramName)
  63. ->where('value',$$paramName)->whereIn('logistic_id',$targetsIds)->get();
  64. if($logistic_itemsTemp->isNotEmpty()){
  65. $targetsIds=$logistic_itemsTemp->map(function($id){return $id->logistic_id;})->unique()->toArray();
  66. $logistic_items=$logistic_itemsTemp;
  67. }
  68. }
  69. if(count($targetsIds)==0)return null;
  70. $result_logistic_id=$targetsIds[0];
  71. if(count($targetsIds)>1){
  72. $finalTarget=$logistic_items->reduce(function ($carry,$item){
  73. if($carry){
  74. if($carry->updated_at > $item->updated_at){
  75. return $carry;
  76. }
  77. return $item;
  78. }
  79. return $item;
  80. });
  81. $result_logistic_id=$finalTarget->logistic_id;
  82. }
  83. return Logistic::find($result_logistic_id);
  84. }
  85. public function createFeatures(string $logisticNumber,int $logisticId)
  86. {
  87. $logisticNumber = trim($logisticNumber);
  88. $featureConsideringLength=LogisticNumberFeature::$featureConsideringLength;
  89. $numberLength=strlen($logisticNumber);
  90. if($numberLength<$featureConsideringLength)
  91. $featureConsideringLength=$numberLength;
  92. if(!$logisticNumber){
  93. $this->log(__METHOD__, 'error', '创建退货快递单号特征时,单号传入了空值');
  94. return null;
  95. };
  96. $featuresCreated=[];
  97. $feature=$this->createFeature($logisticId,'length',$numberLength,100);
  98. if($feature)$featuresCreated[]=$feature;
  99. for($i=0;$i<$featureConsideringLength;$i++){
  100. $chars=mb_strcut($logisticNumber,0,$i+1);
  101. $feature=$this->createFeature($logisticId,"char$i",$chars,99-$i);
  102. if($feature)$featuresCreated[]=$feature;
  103. }
  104. return $featuresCreated;
  105. }
  106. private function createFeature(int $logisticId,string $featureName, string $value,int $weight)
  107. {
  108. if(!$value){
  109. $this->log(__METHOD__, 'error', "创建退货快递单号特征{$featureName}时,特征值为空");
  110. return null;
  111. };
  112. $feature=LogisticNumberFeature::where('logistic_id',$logisticId)
  113. ->where('name',$featureName)->where('value',$value)->first();
  114. if($feature){
  115. $feature->touch();
  116. }else{
  117. $feature=new LogisticNumberFeature(['logistic_id'=>$logisticId,'name'=>$featureName,'value'=>$value,'weight'=>$weight]);
  118. $feature->save();
  119. return $feature;
  120. }
  121. return null;
  122. }
  123. static public function loadRecentRejectedsToFeatures($days,$limit){
  124. $rejecteds=RejectedBill::where('created_at','>',Carbon::today()->subDays($days))->limit($limit)->get();
  125. $featureController=new LogisticNumberFeatureController();
  126. $rejecteds->each(function ($rejected)use($featureController){
  127. if($rejected['logistic_number_return'])
  128. $featureController->createFeatures($rejected['logistic_number_return'],$rejected['id_logistic_return']);
  129. });
  130. }
  131. }