123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace app\command;
- use app\admin\common\User;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Exception;
- use think\facade\Cache;
- use think\facade\Db;
- class TempHandleBusinessData extends Command
- {
- private $key = 'temp_handle_business_data_lock_key';
- protected function configure()
- {
- // 指令配置
- $this->setName('TempHandleBusinessData')
- ->setDescription('将业务公司数据同步到结算平台');
- }
- protected function execute(Input $input, Output $output)
- {
- $tmp = Cache::store('redis')->handler()->get($this->key);
- if ($tmp) return '';//不执行
- Cache::store('redis')->handler()->set($this->key, 1, 5 * 60);
- try {
- //临时同步供应商数据
- $temp = Db::connect('mysql_sys')
- ->name('supplier')
- ->alias('a')
- ->field('a.*,b.contactor,b.position,b.mobile')
- ->leftJoin('supplier_contact b', 'b.code=a.code')
- ->where('a.is_del', 0)
- ->where('a.updatetime', '>=', date('Y-m-d H:i:s', time() - 60))
- ->select()
- ->toArray();
- if (!empty($temp)) {
- $a = Db::connect('mysql_cxinv')
- ->table('cfp_supplier_info')
- ->whereIn('code', array_column($temp, 'code'))
- ->column('id,updatetime', 'code');
- $ins_supplier = [];
- foreach ($temp as $val) {
- if (!isset($a[$val['code']])) {
- $ins_supplier[] = [
- 'name' => $val['name'],
- 'code' => $val['code'],
- 'source' => $val['source'],
- 'contector' => $val['contactor'],
- 'post' => $val['position'],
- 'type' => $val['type'],
- 'nature' => $val['supplier_type'],
- 'corporation' => $val['legaler'],
- 'registertime' => $val['registertime'],
- 'capital' => $val['capital'],
- 'supplier_type' => $val['supplier_type'],
- 'pay_method' => $val['pay_type'],
- 'paydays' => $val['pay_day'],
- 'mobile' => $val['mobile'],
- 'address' => $val['addr'],
- 'return_ticket' => $val['ticket_type'],
- 'delivery' => $val['delivery_way'],
- 'status' => $val['status'],
- 'addtime' => $val['addtime'],
- 'updatetime' => $val['updatetime'],
- 'registercode' => $val['registercode'],
- 'level' => $val['level'],
- 'product_category' => $val['product_category'],
- 'license_img' => $val['license_img'],
- ];
- }else{
- if($val["updatetime"]==$a[$val['code']]['updatetime']) continue;
- $supp_upda=[
- 'id' =>$a[$val['code']]['id'],
- 'name' => $val['name'],
- 'code' => $val['code'],
- 'source' => $val['source'],
- 'contector' => $val['contactor'],
- 'post' => $val['position'],
- 'type' => $val['type'],
- 'nature' => $val['supplier_type'],
- 'corporation' => $val['legaler'],
- 'registertime' => $val['registertime'],
- 'capital' => $val['capital'],
- 'supplier_type' => $val['supplier_type'],
- 'pay_method' => $val['pay_type'],
- 'paydays' => $val['pay_day'],
- 'mobile' => $val['mobile'],
- 'address' => $val['addr'],
- 'return_ticket' => $val['ticket_type'],
- 'delivery' => $val['delivery_way'],
- 'status' => $val['status'],
- 'updatetime' => $val['updatetime'],
- 'registercode' => $val['registercode'],
- 'level' => $val['level'],
- 'product_category' => $val['product_category'],
- 'license_img' => $val['license_img'],
- //以下字段填空字符串,需要用户在结算平台补充值
- ];
- $else = Db::connect('mysql_cxinv')
- ->table('cfp_supplier_info')
- ->save($supp_upda);
- if($else ==false) throw new Exception("供应商公司{$val['code']}更新失败");
- }
- }
- if ($ins_supplier) Db::connect('mysql_cxinv')->table('cfp_supplier_info')->insertAll($ins_supplier);
- }
- $userCommon = new User();
- $rs = $userCommon->handle('get_business_list_tmp', ['where' => [
- ['a.updatetime', '>=', date('Y-m-d H:i:s', time() -60)],
- ]]);
- // $output->writeln('---***---');
- // $output->writeln(json_encode($rs,JSON_UNESCAPED_UNICODE));
- // $output->writeln('---***---');
- if (isset($rs['code']) && $rs['code'] == 0 && !empty($rs['data'])) {
- $all_code = array_column($rs['data'], 'companyNo');
- //业务公司
- $exists = Db::connect('mysql_cxinv')
- ->table('cfp_company_info')
- ->whereIn('companyNo', $all_code)
- ->column('id,updatetime', 'companyNo');
- //供应商
- $gys_exists = Db::connect('mysql_cxinv')
- ->table('cfp_supplier_info')
- ->whereIn('code', array_column($rs['data'], 'supplierNo'))
- ->column('*', 'code');
- $insert = $gys_insert = $customer_ins = [];
- foreach ($rs['data'] as $item) {
- if (!isset($exists[$item['companyNo']])) {
- $insert[] = [
- 'companyNo' => $item['companyNo'],
- 'company_name' => $item['company'],
- 'company_address' => $item['inv_addr'],
- 'company_license' => $item['inv_code'],
- 'bank_name' => $item['inv_bank'],
- 'bankNo' => $item['inv_bankNo'],
- 'contector' => $item['inv_legaler'],
- 'mobile' => $item['invoice_mobile'],
- 'company_img' => $item['license_img'],
- 'status' => 0,//默认禁用,需要在结算平台启用
- 'is_del' => $item['is_del'],
- 'addtime' => $item['addtime'],
- 'updatetime' => $item['updatetime'],
- 'invoice_title' => $item['invoice_title'],
- //以下字段填空字符串,需要用户在结算平台补充值
- 'input_ticket' => '',
- 'out_ticket' => '',
- 'voider' => '',
- 'payee' => '',
- 'drawer' => '',
- 'reviewer' => '',
- 'ownerPlace' => '',
- 'denomination' => '',
- 'invoiceType' => '',
- ];
- $gys_insert[] = array_merge(
- $gys_exists[$item['supplierNo']],
- [
- 'id' => null,
- 'code' => $item['companyNo'],
- 'type' => '企业',
- 'addtime' => date('Y-m-d H:i:s'),
- 'updatetime' => date('Y-m-d H:i:s'),
- ]
- );
- $customer_ins[]=[
- 'companyNo'=>$item['companyNo'],
- 'companyName'=>$item['company'],
- 'parent'=>'业务公司',
- 'addtime'=>date('Y-m-d H:i:s'),
- 'updatetime'=>date('Y-m-d H:i:s'),
- ];
- }else{
- if($item["updatetime"]==$exists[$item['companyNo']]['updatetime']) continue;
- $upda=[
- 'id' => $exists[$item['companyNo']]['id'],
- 'company_name' => $item['company'],
- 'company_address' => $item['inv_addr'],
- 'company_license' => $item['inv_code'],
- 'bank_name' => $item['inv_bank'],
- 'bankNo' => $item['inv_bankNo'],
- 'contector' => $item['inv_legaler'],
- 'mobile' => $item['invoice_mobile'],
- 'company_img' => $item['license_img'],
- 'is_del' => $item['is_del'],
- 'updatetime' => $item['updatetime'],
- 'invoice_title' => $item['invoice_title'],
- //以下字段填空字符串,需要用户在结算平台补充值
- ];
- $else = Db::connect('mysql_cxinv')
- ->table('cfp_company_info')
- ->save($upda);
- if($else ==false) throw new Exception("业务公司{$item['companyNo']}更新失败");
- }
- }
- if ($insert) {
- $res = Db::connect('mysql_cxinv')
- ->table('cfp_company_info')
- ->insertAll($insert);
- $output->writeln('同步业务公司数据成功,共' . $res);
- }
- if ($gys_insert) {
- Db::connect('mysql_cxinv')
- ->table('cfp_supplier_info')
- ->insertAll($gys_insert);
- }
- if ($customer_ins) {
- Db::connect('mysql_cxinv')
- ->table('cfp_customer_info')
- ->insertAll($customer_ins);
- }
- }
- Cache::store('redis')->handler()->set($this->key, 0);
- } catch (Exception $exception) {
- Cache::store('redis')->handler()->set($this->key, 0);
- $output->writeln('脚本出错,' . $exception->getMessage() . '|' . $exception->getFile() . ':' . $exception->getLine());
- }
- }
- }
|