SortingController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Http\Controllers\Api\thirdPart\haochuang;
  3. use App\Batch;
  4. use App\CommodityBarcode;
  5. use App\Http\Controllers\Controller;
  6. use App\Order;
  7. use App\OrderCommodity;
  8. use App\SortingStation;
  9. use App\User;
  10. use App\UserToken;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Hash;
  13. use Illuminate\Support\Facades\Validator;
  14. class SortingController extends Controller
  15. {
  16. function login(Request $request){
  17. $name = $request->input('name');
  18. $password = $request->input('password');
  19. $station_id = $request->input('station_id');
  20. $errors=$this->loginValidator($request->all())->errors();
  21. if(count($errors)>0){
  22. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  23. return response()->json(['result'=>'failure','fail_info'=>'error','errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  24. }
  25. $user=User::query()->where('name',$name)->first();
  26. if(!$user||!Hash::check($password, $user['password'])){
  27. return ['result'=>'failure','fail_info'=>'认证错误'];
  28. }
  29. $station = SortingStation::findOrCreate($station_id);
  30. $station->login();
  31. return ['result'=>'success','token'=>$user->token()];
  32. }
  33. protected function loginValidator(array $data)
  34. {
  35. return Validator::make($data, [
  36. 'name' => ['required', 'string', 'max:191'],
  37. 'password' => ['required', 'string', 'max:191'],
  38. 'station_id' => ['required', 'string', 'max:191'],
  39. ],[
  40. 'required' => ':attribute 不能为空',
  41. ],[
  42. 'name' => '用户名',
  43. 'password' => '密码',
  44. 'station_id' => '设备ID',
  45. ]);
  46. }
  47. function scanCommodity(Request $request){
  48. return ['result'=>'success','data'=>json_encode($request->all())];
  49. }
  50. function process(Request $request){
  51. $token = trim($request->input('token'));
  52. $station_id = $request->input('station_id');
  53. $batch_id = $request->input('batch_id');
  54. $errors=$this->processValidator($request->all())->errors();
  55. if(count($errors)>0){
  56. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  57. return response()->json(['result'=>'failure','fail_info'=>'error','errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  58. }
  59. // if(!UserToken::getUser($token)){
  60. // return ['result'=>'unauthority','fail_info'=>'无效令牌或令牌过期'];
  61. // }
  62. /** @var Batch $batch */
  63. $batch=Batch::query()->where('code',$batch_id)->orderBy('id','desc')->first();
  64. $data=[
  65. 'result'=>'success',
  66. 'station_id'=>$station_id,
  67. 'batch_id'=>$batch_id,
  68. 'orders'=>[]
  69. ];
  70. $ordersSorted=$batch->orders()->get()->sortBy(function(Order $order){
  71. return $order->bin()->first()['number'];
  72. });
  73. $ordersSorted->each(function(Order $order)use(&$data,$request){
  74. if($order['status']=='取消')return;
  75. $orderData=[
  76. 'order_id'=>$order['code'],
  77. 'owner'=>$order->owner()->first()['code'],
  78. 'status'=>$order['status']=='未处理'?'available':$order['status'],
  79. 'created_at'=>$order['created_at']->toDateTimeString(),
  80. 'bin'=>$order->bin()->first()['number'],
  81. 'barcodes'=>[]
  82. ];
  83. $order->orderCommodities()->each(function(OrderCommodity $orderCommodity)use(&$orderData,$request){
  84. $commodity=$orderCommodity->commodity()->first();
  85. if(!$commodity){
  86. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '播种位数据准备出错,找不到订单对应的Commodity id的对象'.$orderCommodity['commodity_id'].',是否表数据在波次生成后丢失?'.json_encode($request->all()));
  87. }
  88. $barcodeStr=$commodity->barcodes()->get()->map(function(CommodityBarcode $barcode){
  89. return $barcode['code'];
  90. })->filter(function($code){
  91. return $code&&(!preg_match('/[\x{4e00}-\x{9fa5}]/u',$code));
  92. })->join(',');
  93. $orderData['barcodes'][]=[
  94. 'id'=>$orderCommodity['id']??'',
  95. 'barcode_id'=>$barcodeStr??'',
  96. 'name'=>$commodity['name']??'',
  97. 'sku'=>$commodity['sku']??'',
  98. 'amount'=>$orderCommodity['amount']??'',
  99. 'location'=>$orderCommodity['location']??'',
  100. ];
  101. });
  102. $data['orders'][]=$orderData;
  103. });
  104. $sendToWms=(new \App\Http\Controllers\Api\thirdPart\flux\SortingController())->informBinAssignment($batch);
  105. if(!$sendToWms){
  106. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '播种位发送给WMS错误:'.json_encode($request->all()));
  107. return response()->json(['result'=>'failure','fail_info'=>'播种位发送给WMS错误,请联系管理员检查错误'])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  108. }
  109. $station = SortingStation::findOrCreate($station_id);
  110. $station->setProcessingBatch($batch);
  111. return $data;
  112. }
  113. protected function processValidator(array $data)
  114. {
  115. return Validator::make($data, [
  116. 'token' => ['required', 'string', 'max:191'],
  117. 'station_id' => ['required', 'string', 'max:191'],
  118. 'batch_id' => ['required', 'string', 'max:191','exists:batches,code'],
  119. ],[
  120. 'required' => ':attribute 不能为空',
  121. 'exists' => ':attribute 不存在',
  122. ],[
  123. 'station_id' => '设备ID',
  124. 'batch_id' => '波次号',
  125. ]);
  126. }
  127. function done(Request $request){
  128. $token = $request->input('token');
  129. $station_id = $request->input('station_id');
  130. $batch_id = $request->input('batch_id');
  131. app('LogService')->log(__METHOD__, __FUNCTION__.'_request', '浩创的完成请求:'.json_encode($request->all()));
  132. $errors=$this->doneValidator($request->all())->errors();
  133. $failInfo='';
  134. foreach ($errors as $error){$failInfo.=$error[0].'; ';}
  135. if(count($errors)>0){
  136. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  137. return response()->json(['result'=>'failure','fail_info'=>$failInfo,'errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  138. }
  139. if(!UserToken::getUser($token)){
  140. return ['result'=>'unauthority','fail_info'=>'无效令牌或令牌过期'];
  141. }
  142. $batch=Batch::query()->where('code',$batch_id)->first();
  143. if($batch->status=='已处理'){
  144. app('LogService')->log(__METHOD__,'alert_'.__FUNCTION__,$batch['code'].'重复发送,波次已处理');
  145. return ['result'=>'failure','fail_info'=>$batch['code'].'重复发送,波次已处理'];
  146. }
  147. $sendToWms=(new \App\Http\Controllers\Api\thirdPart\flux\SortingController())->informBatchFinished($batch);
  148. if(!$sendToWms){
  149. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '发送给WMS错误:'.json_encode($request->all()));
  150. return response()->json(['result'=>'failure','fail_info'=>'发送给WMS错误,请联系管理员检查错误'])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  151. }
  152. $batch->setProcessed();
  153. $station = SortingStation::query()->where('name',$station_id)->first();
  154. $station->clearProcessingBatch();
  155. return ['result'=>'success','batch_id'=>$batch_id];
  156. }
  157. protected function doneValidator(array $data)
  158. {
  159. return Validator::make($data, [
  160. 'token' => ['required', 'string', 'max:191'],
  161. 'station_id' => ['required', 'string', 'max:191','exists:sorting_stations,name'],
  162. 'batch_id' => ['required', 'string', 'max:191','exists:batches,code'],
  163. ],[
  164. 'required' => ':attribute 不能为空',
  165. 'exists' => ':attribute 不存在',
  166. ],[
  167. 'station_id' => '设备ID',
  168. 'batch_id' => '波次号',
  169. ]);
  170. }
  171. }