SortingController.php 8.6 KB

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