<?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 {

            $userCommon = new User();
            $rs = $userCommon->handle('get_business_list_tmp', ['where' => [
                ['addtime', '>=', date('Y-m-d H:i:s', time() - 5 * 60)],
            ]]);

            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', 'companyNo');

                $insert = [];
                foreach ($rs['data'] as $item) {
                    if (!isset($exists[$item['companyNo']])) {
                        $insert[] = [
                            'companyNo' => $item['companyNo'],
                            'company_name' => $item['company'],
                            'company_address' => $item['addr'],
                            'company_license' => $item['inv_code'],
                            'bank_name' => $item['inv_bank'],
                            'bankNo' => $item['inv_bankNo'],
                            'contector' => $item['inv_legaler'],
                            'mobile' => $item['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' => '',
                        ];
                    }
                }

                if ($insert) {
                    $res = Db::connect('mysql_cxinv')
                        ->table('cfp_company_info')
                        ->insertAll($insert);
                    $output->writeln('同步业务公司数据成功,共' . $res);
                }

            }

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

        }

    }

}