TempHandleBusinessData.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\command;
  3. use app\admin\common\User;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\Exception;
  8. use think\facade\Cache;
  9. use think\facade\Db;
  10. class TempHandleBusinessData extends Command
  11. {
  12. private $key = 'temp_handle_business_data_lock_key';
  13. protected function configure()
  14. {
  15. // 指令配置
  16. $this->setName('TempHandleBusinessData')
  17. ->setDescription('将业务公司数据同步到结算平台');
  18. }
  19. protected function execute(Input $input, Output $output)
  20. {
  21. $tmp = Cache::store('redis')->handler()->get($this->key);
  22. if ($tmp) return '';//不执行
  23. Cache::store('redis')->handler()->set($this->key, 1, 5 * 60);
  24. try {
  25. //临时同步供应商数据
  26. $temp = Db::connect('mysql_sys')
  27. ->name('supplier')
  28. ->alias('a')
  29. ->field('a.*,b.contactor,b.position,b.mobile')
  30. ->leftJoin('supplier_contact b', 'b.code=a.code')
  31. ->where('a.is_del', 0)
  32. ->where('a.addtime', '>=', date('Y-m-d H:i:s', time() - 5 * 60))
  33. ->select()
  34. ->toArray();
  35. if (!empty($temp)) {
  36. $a = Db::connect('mysql_cxinv')
  37. ->table('cfp_supplier_info')
  38. ->whereIn('code', array_column($temp, 'code'))
  39. ->column('code', 'code');
  40. $ins_supplier = [];
  41. foreach ($temp as $val) {
  42. if (!isset($a[$val['code']])) {
  43. $ins_supplier[] = [
  44. 'name' => $val['name'],
  45. 'code' => $val['code'],
  46. 'source' => $val['source'],
  47. 'contector' => $val['contactor'],
  48. 'post' => $val['position'],
  49. 'type' => $val['type'],
  50. 'nature' => $val['supplier_type'],
  51. 'corporation' => $val['legaler'],
  52. 'registertime' => $val['registertime'],
  53. 'capital' => $val['capital'],
  54. 'supplier_type' => $val['supplier_type'],
  55. 'pay_method' => $val['pay_type'],
  56. 'paydays' => $val['pay_day'],
  57. 'mobile' => $val['mobile'],
  58. 'address' => $val['addr'],
  59. 'return_ticket' => $val['ticket_type'],
  60. 'delivery' => $val['delivery_way'],
  61. 'status' => $val['status'],
  62. 'addtime' => $val['addtime'],
  63. 'updatetime' => $val['updatetime'],
  64. 'registercode' => $val['registercode'],
  65. 'level' => $val['level'],
  66. 'product_category' => $val['product_category'],
  67. 'license_img' => $val['license_img'],
  68. ];
  69. }
  70. }
  71. if ($ins_supplier) Db::connect('mysql_cxinv')->table('cfp_supplier_info')->insertAll($ins_supplier);
  72. }
  73. $userCommon = new User();
  74. $rs = $userCommon->handle('get_business_list_tmp', ['where' => [
  75. ['a.addtime', '>=', date('Y-m-d H:i:s', time() - 5 * 60)],
  76. ]]);
  77. // $output->writeln('---***---');
  78. // $output->writeln(json_encode($rs,JSON_UNESCAPED_UNICODE));
  79. // $output->writeln('---***---');
  80. if (isset($rs['code']) && $rs['code'] == 0 && !empty($rs['data'])) {
  81. $all_code = array_column($rs['data'], 'companyNo');
  82. //业务公司
  83. $exists = Db::connect('mysql_cxinv')
  84. ->table('cfp_company_info')
  85. ->whereIn('companyNo', $all_code)
  86. ->column('id', 'companyNo');
  87. //供应商
  88. $gys_exists = Db::connect('mysql_cxinv')
  89. ->table('cfp_supplier_info')
  90. ->whereIn('code', array_column($rs['data'], 'supplierNo'))
  91. ->column('*', 'code');
  92. $insert = $gys_insert = [];
  93. foreach ($rs['data'] as $item) {
  94. if (!isset($exists[$item['companyNo']])) {
  95. $insert[] = [
  96. 'companyNo' => $item['companyNo'],
  97. 'company_name' => $item['company'],
  98. 'company_address' => $item['addr'],
  99. 'company_license' => $item['inv_code'],
  100. 'bank_name' => $item['inv_bank'],
  101. 'bankNo' => $item['inv_bankNo'],
  102. 'contector' => $item['inv_legaler'],
  103. 'mobile' => $item['mobile'],
  104. 'company_img' => $item['license_img'],
  105. 'status' => 0,//默认禁用,需要在结算平台启用
  106. 'is_del' => $item['is_del'],
  107. 'addtime' => $item['addtime'],
  108. 'updatetime' => $item['updatetime'],
  109. 'invoice_title' => $item['invoice_title'],
  110. //以下字段填空字符串,需要用户在结算平台补充值
  111. 'input_ticket' => '',
  112. 'out_ticket' => '',
  113. 'voider' => '',
  114. 'payee' => '',
  115. 'drawer' => '',
  116. 'reviewer' => '',
  117. 'ownerPlace' => '',
  118. 'denomination' => '',
  119. 'invoiceType' => '',
  120. ];
  121. $gys_insert[] = array_merge(
  122. $gys_exists[$item['supplierNo']],
  123. [
  124. 'id' => null,
  125. 'code' => $item['companyNo'],
  126. 'type' => '企业',
  127. 'addtime' => date('Y-m-d H:i:s'),
  128. 'updatetime' => date('Y-m-d H:i:s'),
  129. ]
  130. );
  131. }
  132. }
  133. if ($insert) {
  134. $res = Db::connect('mysql_cxinv')
  135. ->table('cfp_company_info')
  136. ->insertAll($insert);
  137. $output->writeln('同步业务公司数据成功,共' . $res);
  138. }
  139. if ($gys_insert) {
  140. Db::connect('mysql_cxinv')
  141. ->table('cfp_supplier_info')
  142. ->insertAll($gys_insert);
  143. }
  144. }
  145. Cache::store('redis')->handler()->set($this->key, 0);
  146. } catch (Exception $exception) {
  147. Cache::store('redis')->handler()->set($this->key, 0);
  148. $output->writeln('脚本出错,' . $exception->getMessage() . '|' . $exception->getFile() . ':' . $exception->getLine());
  149. }
  150. }
  151. }