SortingController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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\OrderBin;
  9. use App\OrderCommodity;
  10. use App\Services\LogService;
  11. use App\Services\OracleDOCOrderHeaderService;
  12. use App\Services\OrderCommodityService;
  13. use App\Services\OrderService;
  14. use App\SortingStation;
  15. use App\User;
  16. use App\UserToken;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\Hash;
  20. use Illuminate\Support\Facades\Validator;
  21. class SortingController extends Controller
  22. {
  23. function login(Request $request){
  24. $name = $request->input('name');
  25. $password = $request->input('password');
  26. $station_id = $request->input('station_id');
  27. $errors=$this->loginValidator($request->all())->errors();
  28. if(count($errors)>0){
  29. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  30. return response()->json(['result'=>'failure','fail_info'=>'error','errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  31. }
  32. $user=User::query()->where('name',$name)->first();
  33. if(!$user||!Hash::check($password, $user['password'])){
  34. return ['result'=>'failure','fail_info'=>'认证错误'];
  35. }
  36. $station = SortingStation::findOrCreate($station_id);
  37. $station->login();
  38. return ['result'=>'success','token'=>$user->token()];
  39. }
  40. protected function loginValidator(array $data)
  41. {
  42. return Validator::make($data, [
  43. 'name' => ['required', 'string', 'max:191'],
  44. 'password' => ['required', 'string', 'max:191'],
  45. 'station_id' => ['required', 'string', 'max:191'],
  46. ],[
  47. 'required' => ':attribute 不能为空',
  48. ],[
  49. 'name' => '用户名',
  50. 'password' => '密码',
  51. 'station_id' => '设备ID',
  52. ]);
  53. }
  54. function process(Request $request){
  55. $token = trim($request->input('token'));
  56. $station_id = $request->input('station_id');
  57. $batch_id = $request->input('batch_id');
  58. $errors=$this->processValidator($request->all())->errors();
  59. if(count($errors)>0){
  60. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  61. return response()->json(['result'=>'failure','fail_info'=>'error','errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  62. }
  63. // if(!UserToken::getUser($token)){
  64. // return ['result'=>'unauthority','fail_info'=>'无效令牌或令牌过期'];
  65. // }
  66. // 同步orderCommodity
  67. $this->syncOrder($batch_id);
  68. /** @var Batch $batch */
  69. $batch=Batch::query()->where('code',$batch_id)->orderBy('id','desc')->first();
  70. $data=[
  71. 'result'=>'success',
  72. 'station_id'=>$station_id,
  73. 'batch_id'=>$batch_id,
  74. 'orders'=>[]
  75. ];
  76. $ordersSorted=$batch->orders()->get()->sortBy(function(Order $order){
  77. return $order->bin()->first()['number'];
  78. });
  79. $ordersSorted->each(function(Order $order)use(&$data,$request){
  80. if($order['status']=='取消')return;
  81. $orderData=[
  82. 'order_id'=>$order['code'],
  83. 'owner'=>$order->owner()->first()['code'],
  84. 'status'=>$order['status']=='未处理'?'available':$order['status'],
  85. 'created_at'=>$order['created_at']->toDateTimeString(),
  86. 'bin'=>(function()use($order){
  87. $bin=$order->bin()->first()['number']??'';
  88. if(!$bin){
  89. $bin=OracleDOCWaveDetails::query()->where('orderno', 'SO201230003574')->get('seqno')->first()['seqno']??'';
  90. LogService::log(__METHOD__,__FUNCTION__,'bin缺失补查:'.$bin.'. order:'.$order->toJson());
  91. return $bin;
  92. }
  93. return $bin;
  94. })(),
  95. 'barcodes'=>[]
  96. ];
  97. $order->orderCommodities()->each(function(OrderCommodity $orderCommodity)use(&$orderData,$request){
  98. $commodity=$orderCommodity->commodity()->first();
  99. if(!$commodity){
  100. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '播种位数据准备出错,找不到订单对应的Commodity id的对象'.$orderCommodity['commodity_id'].',是否表数据在波次生成后丢失?'.json_encode($request->all()));
  101. return;
  102. }
  103. $barcodeStr=$commodity->barcodes()->get()->map(function(CommodityBarcode $barcode){
  104. return $barcode['code'];
  105. })->filter(function($code){
  106. return $code&&(!preg_match('/[\x{4e00}-\x{9fa5}]/u',$code));
  107. })->join(',');
  108. $orderData['barcodes'][]=[
  109. 'id'=>$orderCommodity['id']??'',
  110. 'barcode_id'=>$barcodeStr??'',
  111. 'name'=>$commodity['name']??'',
  112. 'sku'=>$commodity['sku']??'',
  113. 'amount'=>$orderCommodity['amount']??'',
  114. 'location'=>$orderCommodity['location']??'',
  115. ];
  116. });
  117. $data['orders'][]=$orderData;
  118. });
  119. $sendToWms=(new \App\Http\Controllers\api\thirdPart\flux\SortingController())->informBinAssignment($batch);
  120. if(!$sendToWms){
  121. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '播种位发送给WMS错误:'.json_encode($request->all()));
  122. return response()->json(['result'=>'failure','fail_info'=>'播种位发送给WMS错误,请联系管理员检查错误'])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  123. }
  124. $station = SortingStation::findOrCreate($station_id);
  125. $station->setProcessingBatch($batch);
  126. return $data;
  127. }
  128. protected function processValidator(array $data)
  129. {
  130. return Validator::make($data, [
  131. 'token' => ['required', 'string', 'max:191'],
  132. 'station_id' => ['required', 'string', 'max:191'],
  133. 'batch_id' => ['required', 'string', 'max:191','exists:batches,code'],
  134. ],[
  135. 'required' => ':attribute 不能为空',
  136. 'exists' => ':attribute 不存在',
  137. ],[
  138. 'station_id' => '设备ID',
  139. 'batch_id' => '波次号',
  140. ]);
  141. }
  142. function done(Request $request){
  143. $token = $request->input('token');
  144. $station_id = $request->input('station_id');
  145. $batch_id = $request->input('batch_id');
  146. app('LogService')->log(__METHOD__, __FUNCTION__.'_request', '浩创的完成请求:'.json_encode($request->all()));
  147. $errors=$this->doneValidator($request->all())->errors();
  148. $failInfo='';
  149. foreach ($errors as $error){$failInfo.=$error[0].'; ';}
  150. if(count($errors)>0){
  151. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, json_encode($request->all()).'|'.json_encode($errors));
  152. return response()->json(['result'=>'failure','fail_info'=>$failInfo,'errors'=>$errors])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  153. }
  154. if(!UserToken::getUser($token)){
  155. return ['result'=>'unauthority','fail_info'=>'无效令牌或令牌过期'];
  156. }
  157. $batch=Batch::query()->where('code',$batch_id)->first();
  158. if($batch->status=='已处理'){
  159. app('LogService')->log(__METHOD__,'alert_'.__FUNCTION__,$batch['code'].'重复发送,波次已处理');
  160. return ['result'=>'failure','fail_info'=>$batch['code'].'重复发送,波次已处理'];
  161. }
  162. $sendToWms=(new \App\Http\Controllers\api\thirdPart\flux\SortingController())->informBatchFinished($batch);
  163. if(!$sendToWms){
  164. app('LogService')->log(__METHOD__, 'error' . __FUNCTION__, '发送给WMS错误:'.json_encode($request->all()));
  165. return response()->json(['result'=>'failure','fail_info'=>'发送给WMS错误,请联系管理员检查错误'])->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  166. }
  167. $batch->setProcessed();
  168. $station = SortingStation::query()->where('name',$station_id)->first();
  169. $station->clearProcessingBatch();
  170. return ['result'=>'success','batch_id'=>$batch_id];
  171. }
  172. protected function doneValidator(array $data)
  173. {
  174. return Validator::make($data, [
  175. 'token' => ['required', 'string', 'max:191'],
  176. 'station_id' => ['required', 'string', 'max:191','exists:sorting_stations,name'],
  177. 'batch_id' => ['required', 'string', 'max:191','exists:batches,code'],
  178. ],[
  179. 'required' => ':attribute 不能为空',
  180. 'exists' => ':attribute 不存在',
  181. ],[
  182. 'station_id' => '设备ID',
  183. 'batch_id' => '波次号',
  184. ]);
  185. }
  186. public function syncOrder($code)
  187. {
  188. $orderHeaders = app(OracleDOCOrderHeaderService::class)->getQuery()->where('DOC_Order_Header.WaveNo',$code)->get();
  189. app(OrderService::class)->syncOrderByWMSOrderHeaders($orderHeaders);
  190. app(OrderCommodityService::class)->syncOrderCommodity($orderHeaders);
  191. $this->syncOrderBin($code);
  192. }
  193. public function syncOrderBin($code)
  194. {
  195. $wave = DB::connection("oracle")->selectOne(DB::raw("select * from DOC_WAVE_HEADER where WAVENO = ?"),[$code]);
  196. if (!$wave) return;
  197. $owner = app("OwnerService")->codeGetOwner($wave->customerid);
  198. $obj = [
  199. "wms_status" => $this->wms_status($wave),
  200. "wms_type"=>$wave->descr,
  201. "created_at"=>date("Y-m-d H:i:s"),
  202. "wms_created_at"=>$wave->addtime,
  203. "updated_at"=>$wave->edittime,
  204. "owner_id"=>$owner->id,
  205. ];
  206. $batch = Batch::query()->where("code",$code)->first();
  207. if (!$batch){
  208. $obj["code"] = $code;
  209. $batch = Batch::query()->create($obj);
  210. }else{
  211. Batch::query()->where("code",$code)->update($obj);
  212. }
  213. $order_nos = array_column(DB::connection("oracle")->select(DB::raw("select orderno from DOC_WAVE_DETAILS where WAVENO = ?"),[$code]),"orderno");
  214. Order::query()->whereIn("code",$order_nos)->update(["batch_id"=>$batch->id]);
  215. Order::query()->with(["batch","bin"])->whereIn("code",$order_nos)->get()->each(function ($order){
  216. if (!$order->bin){
  217. $bin = DB::connection("oracle")->selectOne(DB::raw("select seqno from DOC_WAVE_DETAILS where waveno = ? and orderno = ?"),[$order->batch->code,$order->code]);
  218. if ($bin){
  219. OrderBin::query()->create([
  220. 'order_id' => $order->id,
  221. 'number' => $bin->seqno,
  222. ]);
  223. }
  224. }
  225. });
  226. }
  227. /**
  228. * @param $wave
  229. * @return string
  230. */
  231. private function wms_status($wave): string
  232. {
  233. switch ($wave->wavestatus) {
  234. case 00:
  235. $wms_status = '创建';
  236. break;
  237. case 40:
  238. $wms_status = '部分收货';
  239. break;
  240. case 90:
  241. $wms_status = '取消';
  242. break;
  243. case 99:
  244. $wms_status = '完成';
  245. break;
  246. case 62:
  247. $wms_status = '部分装箱';
  248. break;
  249. default:
  250. $wms_status = (string)$wave->wavestatus;
  251. }
  252. return $wms_status;
  253. }
  254. }