SortingController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 process(Request $request){
  48. $token = trim($request->input('token'));
  49. $station_id = $request->input('station_id');
  50. $batch_id = $request->input('batch_id');
  51. $errors=$this->processValidator($request->all())->errors();
  52. if(count($errors)>0){
  53. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  54. return response()->json(['result'=>'failure','fail_info'=>'error','errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  55. }
  56. // if(!UserToken::getUser($token)){
  57. // return ['result'=>'unauthority','fail_info'=>'无效令牌或令牌过期'];
  58. // }
  59. /** @var Batch $batch */
  60. $batch=Batch::query()->where('code',$batch_id)->orderBy('id','desc')->first();
  61. $data=[
  62. 'result'=>'success',
  63. 'station_id'=>$station_id,
  64. 'batch_id'=>$batch_id,
  65. 'orders'=>[]
  66. ];
  67. $ordersSorted=$batch->orders()->get()->sortBy(function(Order $order){
  68. return $order->bin()->first()['number'];
  69. });
  70. $ordersSorted->each(function(Order $order)use(&$data,$request){
  71. if($order['status']=='取消')return;
  72. $orderData=[
  73. 'order_id'=>$order['code'],
  74. 'owner'=>$order->owner()->first()['code'],
  75. 'status'=>$order['status']=='未处理'?'available':$order['status'],
  76. 'created_at'=>$order['created_at']->toDateTimeString(),
  77. 'bin'=>$order->bin()->first()['number'],
  78. 'barcodes'=>[]
  79. ];
  80. $order->orderCommodities()->each(function(OrderCommodity $orderCommodity)use(&$orderData,$request){
  81. $commodity=$orderCommodity->commodity()->first();
  82. if(!$commodity){
  83. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '播种位数据准备出错,找不到订单对应的Commodity id的对象'.$orderCommodity['commodity_id'].',是否表数据在波次生成后丢失?'.json_encode($request->all()));
  84. }
  85. $barcodeStr=$commodity->barcodes()->get()->map(function(CommodityBarcode $barcode){
  86. return $barcode['code'];
  87. })->filter(function($code){
  88. return $code&&(!preg_match('/[\x{4e00}-\x{9fa5}]/u',$code));
  89. })->join(',');
  90. $orderData['barcodes'][]=[
  91. 'id'=>$orderCommodity['id'],
  92. 'barcode_id'=>$barcodeStr,
  93. 'name'=>$commodity['name'],
  94. 'sku'=>$commodity['sku'],
  95. 'amount'=>$orderCommodity['amount'],
  96. 'location'=>'A1-01-C2',
  97. ];
  98. });
  99. $data['orders'][]=$orderData;
  100. });
  101. $sendToWms=(new \App\Http\Controllers\Api\thirdPart\flux\SortingController())->informBinAssignment($batch);
  102. if(!$sendToWms){
  103. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '播种位发送给WMS错误:'.json_encode($request->all()));
  104. return response()->json(['result'=>'failure','fail_info'=>'播种位发送给WMS错误,请联系管理员检查错误'])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  105. }
  106. $station = SortingStation::findOrCreate($station_id);
  107. $station->setProcessingBatch($batch);
  108. return $data;
  109. }
  110. protected function processValidator(array $data)
  111. {
  112. return Validator::make($data, [
  113. 'token' => ['required', 'string', 'max:191'],
  114. 'station_id' => ['required', 'string', 'max:191'],
  115. 'batch_id' => ['required', 'string', 'max:191','exists:batches,code'],
  116. ],[
  117. 'required' => ':attribute 不能为空',
  118. 'exists' => ':attribute 不存在',
  119. ],[
  120. 'station_id' => '设备ID',
  121. 'batch_id' => '波次号',
  122. ]);
  123. }
  124. function done(Request $request){
  125. $token = $request->input('token');
  126. $station_id = $request->input('station_id');
  127. $batch_id = $request->input('batch_id');
  128. $errors=$this->doneValidator($request->all())->errors();
  129. $failInfo='';
  130. foreach ($errors as $error){$failInfo.=$error[0].'; ';}
  131. if(count($errors)>0){
  132. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  133. return response()->json(['result'=>'failure','fail_info'=>$failInfo,'errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  134. }
  135. if(!UserToken::getUser($token)){
  136. return ['result'=>'unauthority','fail_info'=>'无效令牌或令牌过期'];
  137. }
  138. $batch=Batch::query()->where('code',$batch_id)->first();
  139. if($batch->status=='已处理'){
  140. app('LogService')->log(__METHOD__,'alert_'.__FUNCTION__,$batch['code'].'重复发送,波次已处理');
  141. return ['result'=>'failure','fail_info'=>$batch['code'].'重复发送,波次已处理'];
  142. }
  143. $sendToWms=(new \App\Http\Controllers\Api\thirdPart\flux\SortingController())->informBatchFinished($batch);
  144. if(!$sendToWms){
  145. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '发送给WMS错误:'.json_encode($request->all()));
  146. return response()->json(['result'=>'failure','fail_info'=>'发送给WMS错误,请联系管理员检查错误'])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  147. }
  148. $batch->setProcessed();
  149. $station = SortingStation::query()->where('name',$station_id)->first();
  150. $station->clearProcessingBatch();
  151. return ['result'=>'success','batch_id'=>$batch_id];
  152. }
  153. protected function doneValidator(array $data)
  154. {
  155. return Validator::make($data, [
  156. 'token' => ['required', 'string', 'max:191'],
  157. 'station_id' => ['required', 'string', 'max:191','exists:sorting_stations,name'],
  158. 'batch_id' => ['required', 'string', 'max:191','exists:batches,code'],
  159. ],[
  160. 'required' => ':attribute 不能为空',
  161. 'exists' => ':attribute 不存在',
  162. ],[
  163. 'station_id' => '设备ID',
  164. 'batch_id' => '波次号',
  165. ]);
  166. }
  167. }