SortingController.php 9.2 KB

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