TempHandleBusinessData.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.updatetime', '>=', date('Y-m-d H:i:s', time() - 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('id,updatetime', '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. }else{
  70. if($val["updatetime"]==$a[$val['code']]['updatetime']) continue;
  71. $supp_upda=[
  72. 'id' =>$a[$val['code']]['id'],
  73. 'name' => $val['name'],
  74. 'code' => $val['code'],
  75. 'source' => $val['source'],
  76. 'contector' => $val['contactor'],
  77. 'post' => $val['position'],
  78. 'type' => $val['type'],
  79. 'nature' => $val['supplier_type'],
  80. 'corporation' => $val['legaler'],
  81. 'registertime' => $val['registertime'],
  82. 'capital' => $val['capital'],
  83. 'supplier_type' => $val['supplier_type'],
  84. 'pay_method' => $val['pay_type'],
  85. 'paydays' => $val['pay_day'],
  86. 'mobile' => $val['mobile'],
  87. 'address' => $val['addr'],
  88. 'return_ticket' => $val['ticket_type'],
  89. 'delivery' => $val['delivery_way'],
  90. 'status' => $val['status'],
  91. 'updatetime' => $val['updatetime'],
  92. 'registercode' => $val['registercode'],
  93. 'level' => $val['level'],
  94. 'product_category' => $val['product_category'],
  95. 'license_img' => $val['license_img'],
  96. //以下字段填空字符串,需要用户在结算平台补充值
  97. ];
  98. $else = Db::connect('mysql_cxinv')
  99. ->table('cfp_supplier_info')
  100. ->save($supp_upda);
  101. if($else ==false) throw new Exception("供应商公司{$val['code']}更新失败");
  102. }
  103. }
  104. if ($ins_supplier) Db::connect('mysql_cxinv')->table('cfp_supplier_info')->insertAll($ins_supplier);
  105. }
  106. $userCommon = new User();
  107. $rs = $userCommon->handle('get_business_list_tmp', ['where' => [
  108. ['a.updatetime', '>=', date('Y-m-d H:i:s', time() -60)],
  109. ]]);
  110. // $output->writeln('---***---');
  111. // $output->writeln(json_encode($rs,JSON_UNESCAPED_UNICODE));
  112. // $output->writeln('---***---');
  113. if (isset($rs['code']) && $rs['code'] == 0 && !empty($rs['data'])) {
  114. $all_code = array_column($rs['data'], 'companyNo');
  115. //业务公司
  116. $exists = Db::connect('mysql_cxinv')
  117. ->table('cfp_company_info')
  118. ->whereIn('companyNo', $all_code)
  119. ->column('id,updatetime', 'companyNo');
  120. //供应商
  121. $gys_exists = Db::connect('mysql_cxinv')
  122. ->table('cfp_supplier_info')
  123. ->whereIn('code', array_column($rs['data'], 'supplierNo'))
  124. ->column('*', 'code');
  125. $insert = $gys_insert = $customer_ins = [];
  126. foreach ($rs['data'] as $item) {
  127. if (!isset($exists[$item['companyNo']])) {
  128. $insert[] = [
  129. 'companyNo' => $item['companyNo'],
  130. 'company_name' => $item['company'],
  131. 'company_address' => $item['inv_addr'],
  132. 'company_license' => $item['inv_code'],
  133. 'bank_name' => $item['inv_bank'],
  134. 'bankNo' => $item['inv_bankNo'],
  135. 'contector' => $item['inv_legaler'],
  136. 'mobile' => $item['invoice_mobile'],
  137. 'company_img' => $item['license_img'],
  138. 'status' => 0,//默认禁用,需要在结算平台启用
  139. 'is_del' => $item['is_del'],
  140. 'addtime' => $item['addtime'],
  141. 'updatetime' => $item['updatetime'],
  142. 'invoice_title' => $item['invoice_title'],
  143. //以下字段填空字符串,需要用户在结算平台补充值
  144. 'input_ticket' => '',
  145. 'out_ticket' => '',
  146. 'voider' => '',
  147. 'payee' => '',
  148. 'drawer' => '',
  149. 'reviewer' => '',
  150. 'ownerPlace' => '',
  151. 'denomination' => '',
  152. 'invoiceType' => '',
  153. ];
  154. $gys_insert[] = array_merge(
  155. $gys_exists[$item['supplierNo']],
  156. [
  157. 'id' => null,
  158. 'code' => $item['companyNo'],
  159. 'type' => '企业',
  160. 'addtime' => date('Y-m-d H:i:s'),
  161. 'updatetime' => date('Y-m-d H:i:s'),
  162. ]
  163. );
  164. $customer_ins[]=[
  165. 'companyNo'=>$item['companyNo'],
  166. 'companyName'=>$item['company'],
  167. 'parent'=>'业务公司',
  168. 'addtime'=>date('Y-m-d H:i:s'),
  169. 'updatetime'=>date('Y-m-d H:i:s'),
  170. ];
  171. }else{
  172. if($item["updatetime"]==$exists[$item['companyNo']]['updatetime']) continue;
  173. $upda=[
  174. 'id' => $exists[$item['companyNo']]['id'],
  175. 'company_name' => $item['company'],
  176. 'company_address' => $item['inv_addr'],
  177. 'company_license' => $item['inv_code'],
  178. 'bank_name' => $item['inv_bank'],
  179. 'bankNo' => $item['inv_bankNo'],
  180. 'contector' => $item['inv_legaler'],
  181. 'mobile' => $item['invoice_mobile'],
  182. 'company_img' => $item['license_img'],
  183. 'is_del' => $item['is_del'],
  184. 'updatetime' => $item['updatetime'],
  185. 'invoice_title' => $item['invoice_title'],
  186. //以下字段填空字符串,需要用户在结算平台补充值
  187. ];
  188. $else = Db::connect('mysql_cxinv')
  189. ->table('cfp_company_info')
  190. ->save($upda);
  191. if($else ==false) throw new Exception("业务公司{$item['companyNo']}更新失败");
  192. }
  193. }
  194. if ($insert) {
  195. $res = Db::connect('mysql_cxinv')
  196. ->table('cfp_company_info')
  197. ->insertAll($insert);
  198. $output->writeln('同步业务公司数据成功,共' . $res);
  199. }
  200. if ($gys_insert) {
  201. Db::connect('mysql_cxinv')
  202. ->table('cfp_supplier_info')
  203. ->insertAll($gys_insert);
  204. }
  205. if ($customer_ins) {
  206. Db::connect('mysql_cxinv')
  207. ->table('cfp_customer_info')
  208. ->insertAll($customer_ins);
  209. }
  210. }
  211. Cache::store('redis')->handler()->set($this->key, 0);
  212. } catch (Exception $exception) {
  213. Cache::store('redis')->handler()->set($this->key, 0);
  214. $output->writeln('脚本出错,' . $exception->getMessage() . '|' . $exception->getFile() . ':' . $exception->getLine());
  215. }
  216. }
  217. }