| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 12 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 70
LogisticNumberFeatureController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 12 | 812.00 | 0.00% covered (danger) | 0.00% | 0 / 70
index | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
create | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
store | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
show | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
edit | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
destroy | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
apiComputeLogisticByNumber | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 5
getLogisticByFeatures | 0.00% covered (danger) | 0.00% | 0 / 1 | 72.00 | 0.00% covered (danger) | 0.00% | 0 / 25
createFeatures | 0.00% covered (danger) | 0.00% | 0 / 1 | 42.00 | 0.00% covered (danger) | 0.00% | 0 / 16
createFeature | 0.00% covered (danger) | 0.00% | 0 / 1 | 12.00 | 0.00% covered (danger) | 0.00% | 0 / 11
loadRecentRejectedsToFeatures | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 6

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