DoorDeviceJob.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. $order_number[] = hex2bin($value); //十六进制字符串转回 原来字符串
  45. }
  46. 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);
  47. if(empty($order_number)) return;
  48. //获取包装单产品
  49. $dispatchList = $this->getDispatchList($order_number);
  50. if(empty($dispatchList) || empty($dispatchList['data'])) return;
  51. $this->completionOrders($dispatchList['data'],$type);
  52. //输出信息 测试
  53. $this->echoMessage(new ConsoleOutput());
  54. }catch (\Exception $exception){
  55. 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);
  56. }
  57. }
  58. public function getDispatchList($data){
  59. if(empty($data)) return [];
  60. list($status,$token) = ClearDataService::getTokenCs();
  61. if(! $status) return [];
  62. $site = $this->site;
  63. $url = "http://121.36.142.167:7774/jbl/api/module-data/box_orders/box_orders/diy/defective_order_no_list";
  64. $post = [
  65. 'defective_order_no_list' => $data,
  66. ];
  67. $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:{$site}"];
  68. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post));
  69. $curl = curl_init();
  70. curl_setopt_array($curl, array(
  71. CURLOPT_URL => $url,
  72. CURLOPT_RETURNTRANSFER => true,
  73. CURLOPT_ENCODING => '',
  74. CURLOPT_MAXREDIRS => 10,
  75. CURLOPT_TIMEOUT => 0,
  76. CURLOPT_FOLLOWLOCATION => true,
  77. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  78. CURLOPT_CUSTOMREQUEST => 'POST',
  79. CURLOPT_POSTFIELDS => $json,
  80. CURLOPT_HTTPHEADER => $header,
  81. ));
  82. $response = curl_exec($curl);
  83. curl_close($curl);
  84. file_put_contents('record_door_result.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.'getlist'.PHP_EOL,8);
  85. return json_decode($response,true);
  86. }
  87. public function completionOrders($data,$type){
  88. list($status,$token) = ClearDataService::getTokenCs();
  89. if(! $status) return;
  90. $site = $this->site;
  91. $device_id = $this->device_id;
  92. //组织数据
  93. $main_dtl = $orderNo = [];
  94. if($type == 1){
  95. $opt = InOutOptionService::OrderKeyQueueIn;
  96. $url = 'http://121.36.142.167:7774/jbl/api/module-data/production_receipt/production_receipt';
  97. $dynamicFormId = "473758926009479168";
  98. $showModelId = "473761325902147584";
  99. foreach ($data as $value){
  100. if(empty($value['box_orders']['delivery_status'])){
  101. $main_dtl[] = [
  102. "product_code"=> $value['brand_code'],
  103. "product_title"=> $value['product_title'],
  104. "product_size"=> $value['product_size'],
  105. "color"=> $value['color'],
  106. "color_two"=> $value['color_two'],
  107. "product_unit_title"=> $value['product_unit_title'],
  108. "product_unit"=> $value['product_unit'],
  109. "color_code"=> $value['color_code'],
  110. "color_code_two"=> $value['color_code_two'],
  111. "in_num"=> $value['box_num'],
  112. ];
  113. if(! in_array($value['box_orders']['defective_order_no'], $orderNo)) $orderNo[] = $value['box_orders']['defective_order_no'];
  114. }
  115. }
  116. $datas = [
  117. 'production_receipt' => [
  118. 'production_receipt_no' => null,
  119. 'in_out_type' => 'RK007',
  120. 'box_title' => 'WH05001',
  121. 'in_time' => gmdate("Y-m-d\TH:i:s.000\Z"),
  122. 'status' => "NOT_APPROVED",
  123. ],
  124. 'production_receipt_dtl' => $main_dtl
  125. ];
  126. }else{
  127. $opt = InOutOptionService::OrderKeyQueueOut;
  128. $url = 'http://121.36.142.167:7774/jbl/api/module-data/picking_out/picking_out';
  129. $dynamicFormId = "473763313217908736";
  130. $showModelId = "473771977253269504";
  131. foreach ($data as $value){
  132. if(empty($value['box_orders']['delivery_status'])){
  133. $main_dtl[] = [
  134. "product_code"=> $value['brand_code'],
  135. "product_title"=> $value['product_title'],
  136. "product_size"=> $value['product_size'],
  137. "color"=> $value['color'],
  138. "color_two"=> $value['color_two'],
  139. "product_unit_title"=> $value['product_unit_title'],
  140. "product_unit"=> $value['product_unit'],
  141. "color_code"=> $value['color_code'],
  142. "color_code_two"=> $value['color_code_two'],
  143. "in_num"=> $value['box_num'],
  144. ];
  145. if(! in_array($value['box_orders']['defective_order_no'], $orderNo)) $orderNo[] = $value['box_orders']['defective_order_no'];
  146. }
  147. }
  148. $datas = [
  149. 'picking_out' => [
  150. 'picking_out_no' => null,
  151. 'in_out_type' => 'CK010',
  152. 'box_title' => 'WH05001',
  153. 'out_time' => gmdate("Y-m-d\TH:i:s.000\Z"),
  154. 'status' => "NOT_APPROVED",
  155. ],
  156. 'picking_out_product' => $main_dtl
  157. ];
  158. }
  159. if(! empty($main_dtl)){
  160. //有产品
  161. $post = [
  162. "bizTypeEk" => "LOWCODE",
  163. "bizId" => -1,
  164. "data" => $datas,
  165. "dynamicFormId" => $dynamicFormId,
  166. "showModelId" => $showModelId
  167. ];
  168. }
  169. //组织数据------
  170. 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);
  171. $header = ["Authorization: Bearer {$token}","Content-Type:application/json","Site:{$site}"];
  172. $curl = curl_init();
  173. curl_setopt_array($curl, array(
  174. CURLOPT_URL => $url,
  175. CURLOPT_RETURNTRANSFER => true,
  176. CURLOPT_ENCODING => '',
  177. CURLOPT_MAXREDIRS => 10,
  178. CURLOPT_TIMEOUT => 0,
  179. CURLOPT_FOLLOWLOCATION => true,
  180. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  181. CURLOPT_CUSTOMREQUEST => 'POST',
  182. CURLOPT_POSTFIELDS => json_encode($post),
  183. CURLOPT_HTTPHEADER => $header,
  184. ));
  185. $response = curl_exec($curl);
  186. curl_close($curl);
  187. $result = json_decode($response,true);
  188. if(! empty($result['createdDate'])) {
  189. $expire_time = 6;
  190. $key = $device_id . $opt;
  191. Redis::set($key, json_encode($orderNo));
  192. Redis::expire($key, $expire_time);
  193. }
  194. file_put_contents('record_door_result.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.'create'.PHP_EOL,8);
  195. }
  196. protected function echoMessage(OutputInterface $output)
  197. {
  198. $output->writeln($this->data);
  199. }
  200. }