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()); } } }