QueueCus.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Cache;
  10. use think\facade\Db;
  11. use think\facade\Log;
  12. class QueueCus extends Command
  13. {
  14. protected function configure()
  15. {
  16. // 指令配置
  17. $this->setName('queuecus')
  18. ->setDescription('the queuecus command');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. // 指令输出
  23. $redis = Cache::store("redis");
  24. while($redis->handler()->llen('createOrderJob1')>0){
  25. $data = $redis->handler()->rpop('createOrderJob1');
  26. $user_info = json_decode($data,true);
  27. $rel =$this->checkJob($user_info);
  28. if($rel){
  29. $this->doData($user_info);
  30. }
  31. $output->writeln('queuecus');
  32. }
  33. }
  34. protected function doData($data){
  35. $result = OcrByInvoice(["url"=>$data['image']]);
  36. $result = json_decode($result,true);
  37. if(isset($result['code'])&&$result['code']==0 ){
  38. $obj = $result['data'];
  39. if(isset($obj['error_code'])){
  40. Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save
  41. (["remark"=>json_encode($obj,JSON_UNESCAPED_UNICODE)]);
  42. $sql ="update cfp_pay_stages set remark=CONCAT(ifnull(remark,''),'识别失败;') where id={$data['payid']}";
  43. Db::query($sql);
  44. $this->failed($data);
  45. return false;
  46. }
  47. try{
  48. // $ist = Db::name("invoice")->where([['code',"=",$object['code']],["number","=",$object['number']]])
  49. // ->find();
  50. // if($ist){
  51. // if($ist['id']!=$data['id']){
  52. // Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(["remark"=>"已存在发票{$object['code']}"]);
  53. // $sql ="update cfp_pay_stages set remark=CONCAT(remark,'已存在发票{$object['code']};') where id={$data['payid']}";
  54. // Db::query($sql);
  55. // $this->failed($data);
  56. // return false;
  57. // }
  58. // }
  59. $object = $result['data']["result"];
  60. $obje=json_encode($object,JSON_UNESCAPED_UNICODE);
  61. Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(["status"=>0,'invoice_info'=>$obje,"remark"=>""]);
  62. $info = Db::name("invoice_info")->where("invid","=",$data['id'])->find();
  63. if(empty($info)){
  64. $info=[];
  65. }
  66. Log::write(Db::name("invoice_img")->getLastSql());
  67. $info['payid']= $data['payid'];
  68. $info['invid']= $data['id'];
  69. $info['type']= $object['type'];
  70. $info['code']= $object['code'];
  71. $info['number']= $object['number'];
  72. $info['check_code']= $object['check_code'];
  73. $info['issue_date']= $object['issue_date'];
  74. $info['buyer_name']= $object['buyer_name'];
  75. $info['buyer_id']= $object['buyer_id'];
  76. $info['buyer_address']= $object['buyer_address'];
  77. $info['buyer_bank']= $object['buyer_bank'];
  78. $info['seller_name']= $object['seller_name'];
  79. $info['seller_id']= $object['seller_id'];
  80. $info['seller_address']= $object['seller_address'];
  81. $info['seller_bank']= $object['seller_bank'];
  82. // $info['subtotal_amount']= str_replace("¥",'',$object['subtotal_amount']);
  83. // $info['subtotal_tax']=str_replace("¥",'',$object['subtotal_tax']);
  84. // $info['total']= str_replace("¥",'',$object['total']);
  85. $info['subtotal_amount']= $object['subtotal_amount'];
  86. $info['subtotal_tax']=$object['subtotal_tax'];
  87. $info['total']= $object['total'];
  88. $info['item_list']= json_encode($object['item_list'],JSON_UNESCAPED_UNICODE);
  89. $info['status']= 0;
  90. $info['change_field']= "";
  91. Db::name("invoice_info")->save($info);
  92. $num =Db::name("invoice_img")->where([['payid',"=", $data['payid']],["is_del","=",0],['status',"<>",0]])
  93. ->count();
  94. if($num<=0){
  95. Db::name("pay_stages")->where("id","=",$data['payid'])->save(["status"=>5,"remark"=>""]);
  96. }
  97. return true;
  98. }catch (\Exception $e){
  99. echo $e->getMessage();
  100. return false;
  101. }
  102. }else{
  103. Log::write(var_export( $result,true));
  104. Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save
  105. (["remark"=>json_encode($result,JSON_UNESCAPED_UNICODE)]);
  106. $sql ="update cfp_pay_stages set remark=CONCAT(remark,'识别失败;') where id={$data['payid']}";
  107. Db::query($sql);
  108. $this->failed($data);
  109. return false;
  110. }
  111. }
  112. private function checkJob($data){
  113. $list= Db::name("invoice_img")->where("id","=",$data['id'])->find();
  114. if($list['status']==0){
  115. return false;
  116. }
  117. return true;
  118. }
  119. public function failed($data){
  120. Db::name("invoice_img")->where([['id',"=",$data['id']],['status',"=",1]])->save(['status'=>2]);
  121. $num =Db::name("invoice_img")->where([['payid',"=", $data['payid']],["is_del","=",0],['status',"=",2]])
  122. ->count();
  123. if($num>0){
  124. Db::name("pay_stages")->where("id","=",$data['payid'])->save(["status"=>9]);
  125. }
  126. }
  127. }