StorageController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. namespace App\Http\Controllers\api\thirdPart\haiq;
  3. use App\Components\AsyncResponse;
  4. use App\Http\Requests\ForeignHaiRobotic_taskUpdateRequest;
  5. use App\MaterialBox;
  6. use App\Services\ForeignHaiRoboticsService;
  7. use App\Services\LogService;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Http;
  10. use Ramsey\Uuid\Uuid;
  11. class StorageController
  12. {
  13. use AsyncResponse;
  14. protected $post;
  15. /** @var ForeignHaiRoboticsService $service */
  16. private $service;
  17. public function __construct()
  18. {
  19. $this->service=app('ForeignHaiRoboticsService');
  20. $stockInfo = [
  21. "skuCode" => "TEST-BSSKU",//商品编码 sku
  22. "qty" => 9,//数量
  23. ];
  24. $bin = [
  25. "taskCode" => "TEST-BS2010100001",//任务编号 全局唯一
  26. "binCode" => "TEST-BIN01",//料箱编码
  27. "fromLocCode" => "HAIB1-02-01",//源库位编码
  28. "toLocCode" => "HAIB1-02-01",//目标库位编码 出库填多个,表示这些库位都可以支持
  29. "sequence" => -1,//出库顺序 -1表示没有顺序,只有移库出库时需要指定顺序,其他可为-1
  30. "stockInfo" => [$stockInfo],//商品信息
  31. ];
  32. /*$this->request = [[
  33. "groupCode" => 1,//组号/波次号 一组任务需要一起完成,再开始下一组任务;没有组任务的限制默认传-1或空
  34. "taskMode" => 3,//任务模式 值 1 (输送线入库)值 2 (输送线出库)值 3(货架到缓存货架)值4(货架到流利货架)
  35. "priority" => 99,//优先级 1-2147483647 1最低
  36. "sequenceFlag" => -1,//是否需要有序 1:需要有序 0:不需要有序
  37. "bins" => [$bin],//可执行货箱任务
  38. ]];*///IDE0005714,IDE0004970,IDE0005676,IDE0005665
  39. $this->post = [[
  40. "taskMode" => 1,
  41. "bins"=>[
  42. [
  43. "binCode" => "IDE0005713",
  44. "taskCode" =>UUID::uuid4().'|'.microtime(true),
  45. "fromLocCode" => "BIN-IN1",
  46. "toLocCode" => "",
  47. ],
  48. ],
  49. "groupCode" => 'g'.microtime(true),
  50. "priority" => 20,
  51. "sequenceFlag" => 1,
  52. ]];
  53. $this->post2 = [[
  54. "taskMode" => 2,
  55. "bins"=>[
  56. [
  57. "taskCode" =>Uuid::uuid4().'|'.microtime(true),
  58. "binCode" => "IDE0005132",
  59. "fromLocCode" => "",
  60. "toLocCode" => "BIN-OUT1",
  61. ],
  62. [
  63. "taskCode" =>Uuid::uuid4().'|'.microtime(true),
  64. "binCode" => "IDE0005152",
  65. "fromLocCode" => "",
  66. "toLocCode" => "BIN-OUT1",
  67. ],
  68. [
  69. "taskCode" =>Uuid::uuid4().'|'.microtime(true),
  70. "binCode" => "IDE0000159",
  71. "fromLocCode" => "",
  72. "toLocCode" => "BIN-OUT1",
  73. ],
  74. [
  75. "taskCode" =>Uuid::uuid4().'|'.microtime(true),
  76. "binCode" => "IDE0004951",
  77. "fromLocCode" => "",
  78. "toLocCode" => "BIN-OUT1",
  79. ],
  80. ],
  81. "groupCode" => 'g'.microtime(true),
  82. "priority" => 20,
  83. "sequenceFlag" => 1,
  84. ]];
  85. $this->moveBinIn = [[
  86. "taskMode" => 6,
  87. "bins"=>[[
  88. "taskCode" =>'t1'.microtime(true),
  89. "binCode" => "IDE0005173",
  90. "fromLocCode" => "HAIB1-02-03", //B1是面对架货右手,从右往左数列,第三个数字从下往上数行
  91. "toLocCode" => "",
  92. ]
  93. // ,[
  94. // "taskCode" =>'t2'.microtime(true),
  95. // "binCode" => "IDE0004973",
  96. // "fromLocCode" => "HAIB1-02-03", //B1是面对架货右手,从右往左数列,第三个数字从下往上数行
  97. // "toLocCode" => "",
  98. // ]
  99. // ,[
  100. // "taskCode" =>'t3'.microtime(true),
  101. // "binCode" => "IDE0005716",
  102. // "fromLocCode" => "HAIB1-03-02", //B1是面对架货右手,从右往左数列,第三个数字从下往上数行
  103. // "toLocCode" => "",
  104. // ],
  105. ],
  106. "groupCode" => 'g'.microtime(true),
  107. "priority" => 20,
  108. "sequenceFlag" => 1,
  109. ]];
  110. $this->moveBinIn2 = [[
  111. "taskMode" => 3,
  112. "bins"=>[
  113. [
  114. "taskCode" =>'t1'.microtime(true),
  115. "binCode" => "IDE0005716",
  116. "fromLocCode" => "", //B1是面对架货右手,从右往左数列,第三个数字从下往上数行
  117. "toLocCode" => "HAIB1-02-03",
  118. ],
  119. // [
  120. // "taskCode" =>'t2'.microtime(true),
  121. // "binCode" => "IDE0004973",
  122. // "fromLocCode" => "", //B1是面对架货右手,从右往左数列,第三个数字从下往上数行
  123. // "toLocCode" => "HAIB1-02-03",
  124. // ],[
  125. // "taskCode" =>'t3'.microtime(true),
  126. // "binCode" => "IDE0005716",
  127. // "fromLocCode" => "", //B1是面对架货右手,从右往左数列,第三个数字从下往上数行
  128. // "toLocCode" => "HAIB1-03-02",
  129. // ],
  130. ],
  131. "groupCode" => 'g'.microtime(true),
  132. "priority" => 20,
  133. "sequenceFlag" => 1,
  134. ]];
  135. $this->move = [[
  136. "taskMode" => 3,
  137. "bins"=>[[
  138. "taskCode" =>'t'.Uuid::uuid4(),
  139. "binCode" => "IDE0005678",
  140. "fromLocCode" => "",
  141. "toLocCode" => "HAIB1-03-01",
  142. ],[
  143. "taskCode" =>'t'.Uuid::uuid4(),
  144. "binCode" => "IDE0004975",
  145. "fromLocCode" => "",
  146. "toLocCode" => "HAIB1-03-02",
  147. ],],
  148. "groupCode" => 'g'.microtime(true),
  149. "priority" => 20,
  150. "sequenceFlag" => 1,
  151. ]];//128,129.127
  152. $this->move2 = [[
  153. "taskMode" => 6,
  154. "bins"=>[
  155. [
  156. "taskCode" =>'t'.Uuid::uuid4(),
  157. "binCode" => "IDE0005711",
  158. "fromLocCode" => "HAIB1-01-02", //B1是面对架货右手,从右往左数列,第三个数字从下往上数行
  159. "toLocCode" => "",
  160. ],
  161. [
  162. "taskCode" =>'t'.Uuid::uuid4(),
  163. "binCode" => "IDE0005680",
  164. "fromLocCode" => "HAIB1-02-02", //B1是面对架货右手,从右往左数列,第三个数字从下往上数行
  165. "toLocCode" => "",
  166. ],
  167. ],
  168. "groupCode" => 'g'.microtime(true),
  169. "priority" => 20,
  170. "sequenceFlag" => 1,
  171. ]];
  172. $this->move3 = [[
  173. "taskMode" => 6,
  174. "bins"=>[[
  175. "taskCode" =>"testm11",
  176. "binCode" => "IDE0000128",
  177. "fromLocCode" => "HAIB2-01-01",
  178. "toLocCode" => "",
  179. ],[
  180. "taskCode" =>"testm21",
  181. "binCode" => "IDE000129",
  182. "fromLocCode" => "HAIB2-02-01",
  183. "toLocCode" => "",
  184. ],[
  185. "taskCode" =>"testm31",
  186. "binCode" => "IDE0000127",
  187. "fromLocCode" => "HAIB2-03-01",
  188. "toLocCode" => "",
  189. ],],
  190. "groupCode" => 'WMm2',
  191. "priority" => 20,
  192. "sequenceFlag" => 1,
  193. ]];
  194. $this->lightOn = [
  195. "areaCode"=> "1004",
  196. "PTLAction"=> 1, //1是开,0是关
  197. "PTLSettings"=> [
  198. "color"=> 2, //灯颜色,0红,1绿,2绿,3黄
  199. "frequency"=> 0 //频率 0是不动,最大是3
  200. ],
  201. "displayInfo"=> [
  202. "detail01"=> "detail01",
  203. "detail02"=> "detail02",
  204. "detail03"=> "detail03",
  205. "qrCode"=> "qrCode",
  206. "qty00"=> "11",
  207. "qty01"=> 1,
  208. "qty02"=> 2,
  209. "title"=> "title",
  210. "uomDesc01"=> "uo",
  211. "uomDesc02"=> "uo"
  212. ],
  213. "locCode"=> "HAIB2-02-03"//灯条口,B1\B2=设备编号,中间号码代表从右往左数的列,右边号码时从下往上数
  214. ];
  215. $this->lightOff = [
  216. "PTLAction"=> 0,
  217. "locCode"=> "HAIB2-02-02"
  218. ];
  219. }
  220. public function relocate(Request $request,$post){
  221. $response = Http::post(config('api.haiq.storage.moveBin'),$post);
  222. if (!$response->ok()){
  223. app('LogService')->log(__METHOD__,"haiq-请求失败,路径异常","REQUEST:".json_encode($this->post)." | RESPONSE:".$response);
  224. return ['success'=>false,"data"=>$response->body()];
  225. }
  226. if (($response["code"] ?? false) && $response["code"] != 200){
  227. app('LogService')->log(__METHOD__,"haiq-料箱出库失败","REQUEST:".json_encode($this->post)." | RESPONSE:".$response);
  228. return ['success'=>false,"data"=>$response["errMsg"]];
  229. }
  230. if (($response["status"] ?? false) && $response["status"] == 500){
  231. app('LogService')->log(__METHOD__,"haiq-料箱出库失败","REQUEST:".json_encode($this->post)." | RESPONSE:".$response);
  232. return ['success'=>false,"data"=>$response["msg"]];
  233. }
  234. app('LogService')->log(__METHOD__,"haiq-料箱出库成功","REQUEST:".json_encode($this->post)." | RESPONSE:".$response);
  235. return ["success"=>true,'data'=>$response->body()];
  236. }
  237. public function light(Request $request,$post){
  238. $response = Http::post(config('api.haiq.storage.light'),$post);
  239. return $response->body();
  240. }
  241. public function moveBinOut(Request $request){
  242. return $this->relocate($request,$this->post);
  243. }
  244. public function postBin1(Request $request){
  245. return $this->relocate($request,$this->post);
  246. }
  247. public function postBin2(Request $request){
  248. return $this->relocate($request,$this->post2);
  249. }
  250. public function moveBinIn(Request $request){
  251. return $this->relocate($request,$this->moveBinIn);
  252. }
  253. public function moveBinIn2(Request $request){
  254. return $this->relocate($request,$this->moveBinIn2);
  255. }
  256. public function moveBinM(Request $request){
  257. return $this->relocate($request,$this->move);
  258. }
  259. public function moveBinM2(Request $request){
  260. return $this->relocate($request,$this->move2);
  261. }
  262. public function lightOn(Request $request){
  263. return $this->light($request,$this->lightOn);
  264. }
  265. public function lightOff(Request $request){
  266. return $this->light($request,$this->lightOff);
  267. }
  268. /**
  269. * 海柔通知:任务状态变更
  270. *
  271. * @param ForeignHaiRobotic_taskUpdateRequest $request
  272. * @return array
  273. */
  274. public function taskUpdate(ForeignHaiRobotic_taskUpdateRequest $request):array
  275. {
  276. LogService::log('海柔请求','taskUpdate', json_encode($request->all()));
  277. try{
  278. $taskCode = explode('_',$request['taskCode']);
  279. if (count($taskCode)>1 && (integer)$taskCode[0]) {
  280. $this->service->taskUpdate($taskCode[0],$request['updateEventType']
  281. ,$request['status'],$request['binCode']
  282. );
  283. }
  284. return ['code'=>200,'errMsg'=>'','data'=>$request->all()];
  285. }catch (\Exception $e){
  286. return ['code'=>$e->getCode(),'errMsg'=>$e->getMessage(),'data'=>$request->all()];
  287. }
  288. }
  289. public function exception(Request $request){
  290. $this->service->excepted('海柔异常','',json_encode($request));
  291. return ['code'=>200,'errMsg'=>'','data'=>$request->all()];
  292. }
  293. public function takeOutToULine(Request $request){
  294. $codesTxt=$request->input('codes');
  295. $codes=preg_split('/[\s,.,。]/',$codesTxt);
  296. if(!$codes||count($codes)==0)return ['result'=>'请输入料箱号'];
  297. $post = [
  298. [
  299. "taskMode" => 2,
  300. "bins"=>[
  301. ],
  302. "groupCode" => 'g'.microtime(true),
  303. "priority" => 20,
  304. "sequenceFlag" => 1,
  305. ]
  306. ];
  307. foreach ($codes as &$code){
  308. if(!trim($code))continue;
  309. $code=str_replace(['ide','IDE'],'',$code);
  310. $code='IDE'.str_pad($code,7,'0',STR_PAD_LEFT);
  311. $post[0]['bins'][]=[
  312. "taskCode" =>Uuid::uuid4().'|'.microtime(true),
  313. "binCode" => $code,
  314. "fromLocCode" => "",
  315. "toLocCode" => "BIN-OUT1",
  316. ];
  317. }
  318. $response = Http::post(config('api.haiq.storage.moveBin'),$post);
  319. $responseBody = $response->body();
  320. $result = ['status'=>'失败'];
  321. LogService::log(__CLASS__,__METHOD__,$responseBody);
  322. if(strstr($responseBody,'200')){
  323. $responseJson=json_decode($responseBody,true);
  324. $result = ['status'=>'成功'];
  325. $responseBody = '取料箱成功:' . implode(',', $codes);
  326. $data0part = $responseJson['data'][0]??[];
  327. if(($data0part['code']??'')==10007
  328. && count($data0part['datas']??[])>0){
  329. $responseBody = '以下料箱不在库内,无法取出:' . implode(',', $data0part['datas']??[]).
  330. '| 成功取出料箱:'.implode(',', array_diff($codes, $data0part['datas']??[]));
  331. $result = ['status'=>'部分成功'];
  332. }
  333. }
  334. if(strstr($responseBody,'成功')){
  335. MaterialBox::query()->whereIn('code',$codes)->update(['status'=>'在出库中']);
  336. }
  337. $result['result']=$responseBody;
  338. return $result;
  339. }
  340. /**
  341. * 填充空箱
  342. */
  343. public function paddingEmptyBox()
  344. {
  345. $amount = \request("amount") ?: 1;
  346. $blacklist = [];
  347. $boxes = "";
  348. for ($i=0;$i<$amount;$i++){
  349. $box = app("MaterialBoxService")->getAnEmptyBoxSortedByOwner(null,$blacklist);
  350. if (!$box)break;
  351. $boxes .= $box->code.",";
  352. $blacklist[] = $box->id;
  353. }
  354. $this->success(rtrim($boxes,","));
  355. }
  356. }