SortingController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. return;
  95. }
  96. $barcodeStr=$commodity->barcodes()->get()->map(function(CommodityBarcode $barcode){
  97. return $barcode['code'];
  98. })->filter(function($code){
  99. return $code&&(!preg_match('/[\x{4e00}-\x{9fa5}]/u',$code));
  100. })->join(',');
  101. $orderData['barcodes'][]=[
  102. 'id'=>$orderCommodity['id']??'',
  103. 'barcode_id'=>$barcodeStr??'',
  104. 'name'=>$commodity['name']??'',
  105. 'sku'=>$commodity['sku']??'',
  106. 'amount'=>$orderCommodity['amount']??'',
  107. 'location'=>$orderCommodity['location']??'',
  108. ];
  109. });
  110. $data['orders'][]=$orderData;
  111. });
  112. $sendToWms=(new \App\Http\Controllers\api\thirdPart\flux\SortingController())->informBinAssignment($batch);
  113. if(!$sendToWms){
  114. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '播种位发送给WMS错误:'.json_encode($request->all()));
  115. return response()->json(['result'=>'failure','fail_info'=>'播种位发送给WMS错误,请联系管理员检查错误'])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  116. }
  117. $station = SortingStation::findOrCreate($station_id);
  118. $station->setProcessingBatch($batch);
  119. return $data;
  120. }
  121. protected function processValidator(array $data)
  122. {
  123. return Validator::make($data, [
  124. 'token' => ['required', 'string', 'max:191'],
  125. 'station_id' => ['required', 'string', 'max:191'],
  126. 'batch_id' => ['required', 'string', 'max:191','exists:batches,code'],
  127. ],[
  128. 'required' => ':attribute 不能为空',
  129. 'exists' => ':attribute 不存在',
  130. ],[
  131. 'station_id' => '设备ID',
  132. 'batch_id' => '波次号',
  133. ]);
  134. }
  135. function done(Request $request){
  136. $token = $request->input('token');
  137. $station_id = $request->input('station_id');
  138. $batch_id = $request->input('batch_id');
  139. app('LogService')->log(__METHOD__, __FUNCTION__.'_request', '浩创的完成请求:'.json_encode($request->all()));
  140. $errors=$this->doneValidator($request->all())->errors();
  141. $failInfo='';
  142. foreach ($errors as $error){$failInfo.=$error[0].'; ';}
  143. if(count($errors)>0){
  144. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  145. return response()->json(['result'=>'failure','fail_info'=>$failInfo,'errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  146. }
  147. if(!UserToken::getUser($token)){
  148. return ['result'=>'unauthority','fail_info'=>'无效令牌或令牌过期'];
  149. }
  150. $batch=Batch::query()->where('code',$batch_id)->first();
  151. if($batch->status=='已处理'){
  152. app('LogService')->log(__METHOD__,'alert_'.__FUNCTION__,$batch['code'].'重复发送,波次已处理');
  153. return ['result'=>'failure','fail_info'=>$batch['code'].'重复发送,波次已处理'];
  154. }
  155. $sendToWms=(new \App\Http\Controllers\api\thirdPart\flux\SortingController())->informBatchFinished($batch);
  156. if(!$sendToWms){
  157. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '发送给WMS错误:'.json_encode($request->all()));
  158. return response()->json(['result'=>'failure','fail_info'=>'发送给WMS错误,请联系管理员检查错误'])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  159. }
  160. $batch->setProcessed();
  161. $station = SortingStation::query()->where('name',$station_id)->first();
  162. $station->clearProcessingBatch();
  163. return ['result'=>'success','batch_id'=>$batch_id];
  164. }
  165. protected function doneValidator(array $data)
  166. {
  167. return Validator::make($data, [
  168. 'token' => ['required', 'string', 'max:191'],
  169. 'station_id' => ['required', 'string', 'max:191','exists:sorting_stations,name'],
  170. 'batch_id' => ['required', 'string', 'max:191','exists:batches,code'],
  171. ],[
  172. 'required' => ':attribute 不能为空',
  173. 'exists' => ':attribute 不存在',
  174. ],[
  175. 'station_id' => '设备ID',
  176. 'batch_id' => '波次号',
  177. ]);
  178. }
  179. }