DoorDeviceJob.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace App\Jobs;
  3. use App\Service\ClearDataService;
  4. use App\Service\InOutOptionService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Support\Facades\Redis;
  11. use Symfony\Component\Console\Output\ConsoleOutput;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class DoorDeviceJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $data;
  17. protected $type;
  18. protected $site;
  19. protected $device_id;
  20. /**
  21. * Create a new job instance
  22. *
  23. * @return void
  24. */
  25. public function __construct($data,$type,$site,$device_id)
  26. {
  27. $this->data = $data;
  28. $this->type = $type;
  29. $this->site = $site ?? '91451322MA5P9JNKXA';
  30. $this->device_id = $device_id;
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. try{
  40. $epc = $this->data;
  41. $type = $this->type;//1 入库 2 出库
  42. $order_number = [];
  43. foreach ($epc as $value){
  44. $str = @hex2bin($value);
  45. $str = ltrim($str, "\x00");// 代0字符串
  46. $str = str_replace(chr(26), '', $str); //过滤CRTL-Z字符
  47. if(! empty($str) && substr($str, 0, 2) === "BZ"){
  48. if(! in_array($str, $order_number)) $order_number[] = $str; // 十六进制字符串转回原来字符串
  49. }
  50. // $order_number[] = hex2bin($value); //十六进制字符串转回 原来字符串
  51. }
  52. file_put_contents('record_door_result.txt',date("Y-m-d H:i:s",time())."原数据:".json_encode($epc) . "解析后:" . json_encode($order_number) .PHP_EOL.'start'.PHP_EOL,8);
  53. if(empty($order_number)) {
  54. file_put_contents('record_door_result.txt',date("Y-m-d H:i:s",time()).'start_end'.PHP_EOL,8);
  55. return;
  56. }
  57. if($type == 1){
  58. //入库
  59. $this->productionReceipt($order_number);
  60. }else{
  61. //出库
  62. //获取包装单产品
  63. $dispatchList = $this->getDispatchList($order_number);
  64. if(empty($dispatchList) || empty($dispatchList['data'])) {
  65. file_put_contents('record_door_result.txt',date("Y-m-d H:i:s",time()).'getlist_end'.PHP_EOL,8);
  66. return;
  67. }
  68. $this->completionOrders($dispatchList['data'],$type);
  69. }
  70. //输出信息 测试
  71. $this->echoMessage(new ConsoleOutput());
  72. }catch (\Exception $exception){
  73. file_put_contents('record_door_error.txt',date("Y-m-d H:i:s",time()).json_encode($this->data) . PHP_EOL.$exception->getMessage().PHP_EOL,8);
  74. }
  75. }
  76. public function productionReceipt($data){
  77. list($status,$token) = ClearDataService::getTokenCs();
  78. if(! $status) return;
  79. $site = $this->site;
  80. $url = "http://121.36.142.167:7774/jbl/api/module-data/box_orders/box_orders/diy/production_receipt";
  81. $post = [
  82. 'defective_order_no_list' => $data,
  83. ];
  84. $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:{$site}"];
  85. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post));
  86. $curl = curl_init();
  87. curl_setopt_array($curl, array(
  88. CURLOPT_URL => $url,
  89. CURLOPT_RETURNTRANSFER => true,
  90. CURLOPT_ENCODING => '',
  91. CURLOPT_MAXREDIRS => 10,
  92. CURLOPT_TIMEOUT => 0,
  93. CURLOPT_FOLLOWLOCATION => true,
  94. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  95. CURLOPT_CUSTOMREQUEST => 'POST',
  96. CURLOPT_POSTFIELDS => $json,
  97. CURLOPT_HTTPHEADER => $header,
  98. ));
  99. $response = curl_exec($curl);
  100. curl_close($curl);
  101. file_put_contents('record_door_result.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.'in_opt'.PHP_EOL,8);
  102. $return = json_decode($response,true);
  103. if(! empty($return['data'])){
  104. $expire_time = 10;
  105. $key = $this->device_id . InOutOptionService::OrderKeyQueueIn;
  106. Redis::set($key, json_encode($return['data']));
  107. Redis::expire($key, $expire_time);
  108. }
  109. }
  110. public function getDispatchList($data){
  111. if(empty($data)) return [];
  112. list($status,$token) = ClearDataService::getTokenCs();
  113. if(! $status) return [];
  114. $site = $this->site;
  115. $url = "http://121.36.142.167:7774/jbl/api/module-data/box_orders/box_orders/diy/defective_order_no_list";
  116. $post = [
  117. 'defective_order_no_list' => $data,
  118. ];
  119. $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:{$site}"];
  120. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post));
  121. $curl = curl_init();
  122. curl_setopt_array($curl, array(
  123. CURLOPT_URL => $url,
  124. CURLOPT_RETURNTRANSFER => true,
  125. CURLOPT_ENCODING => '',
  126. CURLOPT_MAXREDIRS => 10,
  127. CURLOPT_TIMEOUT => 0,
  128. CURLOPT_FOLLOWLOCATION => true,
  129. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  130. CURLOPT_CUSTOMREQUEST => 'POST',
  131. CURLOPT_POSTFIELDS => $json,
  132. CURLOPT_HTTPHEADER => $header,
  133. ));
  134. $response = curl_exec($curl);
  135. curl_close($curl);
  136. file_put_contents('record_door_result.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.'getlist'.PHP_EOL,8);
  137. return json_decode($response,true);
  138. }
  139. public function completionOrders($data,$type){
  140. list($status,$token) = ClearDataService::getTokenCs();
  141. if(! $status) return;
  142. $site = $this->site;
  143. $device_id = $this->device_id;
  144. //组织数据
  145. $main_dtl = $orderNo = [];
  146. if($type == 1){
  147. $opt = InOutOptionService::OrderKeyQueueIn;
  148. $url = 'http://121.36.142.167:7774/jbl/api/module-data/production_receipt/production_receipt';
  149. $dynamicFormId = "473758926009479168";
  150. $showModelId = "473761325902147584";
  151. foreach ($data as $value){
  152. if(empty($value['box_orders']['delivery_status'])){
  153. $main_dtl[] = [
  154. "product_code"=> $value['brand_code'],
  155. "product_title"=> $value['product_title'],
  156. "product_size"=> $value['product_size'],
  157. "color"=> $value['color'],
  158. "color_two"=> $value['color_two'],
  159. "product_unit_title"=> $value['product_unit_title'],
  160. "product_unit"=> $value['product_unit'],
  161. "color_code"=> $value['color_code'],
  162. "color_code_two"=> $value['color_code_two'],
  163. "in_num"=> $value['box_num'],
  164. ];
  165. if(! in_array($value['box_orders']['defective_order_no'], $orderNo)) $orderNo[] = $value['box_orders']['defective_order_no'];
  166. }
  167. }
  168. $datas = [
  169. 'production_receipt' => [
  170. 'production_receipt_no' => null,
  171. 'in_out_type' => 'RK007',
  172. 'box_title' => 'WH05001',
  173. 'in_time' => gmdate("Y-m-d\TH:i:s.000\Z"),
  174. 'status' => "NOT_APPROVED",
  175. ],
  176. 'production_receipt_dtl' => $main_dtl
  177. ];
  178. }else{
  179. $opt = InOutOptionService::OrderKeyQueueOut;
  180. $url = 'http://121.36.142.167:7774/jbl/api/module-data/picking_out/picking_out';
  181. $dynamicFormId = "473763313217908736";
  182. $showModelId = "473771977253269504";
  183. foreach ($data as $value){
  184. if(empty($value['box_orders']['delivery_status'])){
  185. $main_dtl[] = [
  186. "product_code"=> $value['brand_code'],
  187. "product_title"=> $value['product_title'],
  188. "product_size"=> $value['product_size'],
  189. "color"=> $value['color'],
  190. "color_two"=> $value['color_two'],
  191. "product_unit_title"=> $value['product_unit_title'],
  192. "product_unit"=> $value['product_unit'],
  193. "color_code"=> $value['color_code'],
  194. "color_code_two"=> $value['color_code_two'],
  195. "in_num"=> $value['box_num'],
  196. ];
  197. if(! in_array($value['box_orders']['defective_order_no'], $orderNo)) $orderNo[] = $value['box_orders']['defective_order_no'];
  198. }
  199. }
  200. $datas = [
  201. 'picking_out' => [
  202. 'picking_out_no' => null,
  203. 'in_out_type' => 'CK010',
  204. 'box_title' => 'WH05001',
  205. 'out_time' => gmdate("Y-m-d\TH:i:s.000\Z"),
  206. 'status' => "NOT_APPROVED",
  207. ],
  208. 'picking_out_product' => $main_dtl
  209. ];
  210. }
  211. if(! empty($main_dtl)){
  212. //有产品
  213. $post = [
  214. "bizTypeEk" => "LOWCODE",
  215. "bizId" => -1,
  216. "data" => $datas,
  217. "dynamicFormId" => $dynamicFormId,
  218. "showModelId" => $showModelId
  219. ];
  220. }
  221. //组织数据------
  222. file_put_contents('record_door_result.txt',date('Y-m-d H:i:s'). PHP_EOL . json_encode($post) .PHP_EOL.'post'.PHP_EOL,8);
  223. $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:{$site}"];
  224. $curl = curl_init();
  225. curl_setopt_array($curl, array(
  226. CURLOPT_URL => $url,
  227. CURLOPT_RETURNTRANSFER => true,
  228. CURLOPT_ENCODING => '',
  229. CURLOPT_MAXREDIRS => 10,
  230. CURLOPT_TIMEOUT => 0,
  231. CURLOPT_FOLLOWLOCATION => true,
  232. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  233. CURLOPT_CUSTOMREQUEST => 'POST',
  234. CURLOPT_POSTFIELDS => json_encode($post),
  235. CURLOPT_HTTPHEADER => $header,
  236. ));
  237. $response = curl_exec($curl);
  238. curl_close($curl);
  239. $result = json_decode($response,true);
  240. if(! empty($result['createdDate'])) {
  241. $expire_time = 10;
  242. $key = $device_id . $opt;
  243. Redis::set($key, json_encode($orderNo));
  244. Redis::expire($key, $expire_time);
  245. }
  246. file_put_contents('record_door_result.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.'create'.PHP_EOL,8);
  247. }
  248. protected function echoMessage(OutputInterface $output)
  249. {
  250. $output->writeln($this->data);
  251. }
  252. }