DoorDeviceJob.php 9.1 KB

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