From 269bd7168e941aed4b5cca478eb5de47674e4a8b Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 30 Jul 2025 15:15:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E6=B5=B7=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command/ImportCustomerCommand.php | 167 ++++++++++++ app/crm/service/CustomerService.php | 9 + composer.json | 3 +- composer.lock | 372 +++++++++++++++++++++++++- 4 files changed, 549 insertions(+), 2 deletions(-) create mode 100644 app/command/ImportCustomerCommand.php diff --git a/app/command/ImportCustomerCommand.php b/app/command/ImportCustomerCommand.php new file mode 100644 index 0000000..8e79eda --- /dev/null +++ b/app/command/ImportCustomerCommand.php @@ -0,0 +1,167 @@ +addArgument('file', InputArgument::REQUIRED, 'Excel文件路径') +// ->addOption('user-id', 'u', InputOption::VALUE_REQUIRED, '用户ID', 0) +// ->addOption('channel-id', 'c', InputOption::VALUE_REQUIRED, '渠道ID', 5) +// ->addOption('corp-abbr', 'a', InputOption::VALUE_REQUIRED, '公司缩写', 2); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { +// $filePath = $input->getArgument('file'); +// $userId = $input->getOption('user-id'); +// $channelId = $input->getOption('channel-id'); +// $corpAbbr = $input->getOption('corp-abbr'); + $filePath = 'C:\Users\16961\Downloads\中国制造 2023-2025-询盘导出.xls'; + $userId = 0; + $channelId = 5; + $corpAbbr = 2; + + // 检查文件是否存在 + if (!file_exists($filePath)) { + $output->writeln("文件不存在: {$filePath}"); + return self::FAILURE; + } + + try { + $output->writeln("开始导入文件: {$filePath}"); + + // 加载Excel文件 + $output->writeln("正在加载Excel文件..."); + $spreadsheet = IOFactory::load($filePath); + $worksheet = $spreadsheet->getActiveSheet(); + $output->writeln("Excel文件加载成功"); + + // 获取数据范围 + $highestRow = $worksheet->getHighestRow(); + $highestColumn = $worksheet->getHighestColumn(); + + $output->writeln("文件包含 {$highestRow} 行数据"); + + // 获取所有国家数据,用于匹配 + $output->writeln("正在获取国家数据..."); + $countries = Country::select('id', 'cn_addr', 'en_addr')->get()->keyBy(function($item) { + return strtolower(trim($item->cn_addr)); + }); + $output->writeln("获取到 " . $countries->count() . " 个国家数据"); + + $successCount = 0; + $errorCount = 0; + $errors = []; + + // 从第二行开始读取数据(第一行是标题) + $output->writeln("开始读取Excel数据..."); + for ($row = 2; $row <= $highestRow; $row++) { + try { + $output->writeln("正在处理第 {$row} 行..."); + + // 读取各列数据 - 使用坐标方法 + try { + $countryName = trim($worksheet->getCell('B' . $row)->getValue() ?? ''); + $customerName = trim($worksheet->getCell('C' . $row)->getValue() ?? ''); + $inquiryProduct = trim($worksheet->getCell('E' . $row)->getValue() ?? ''); + } catch (\Exception $cellError) { + $output->writeln("读取第{$row}行单元格时出错: " . $cellError->getMessage() . ""); + continue; + } + + $output->writeln("读取数据: 国家={$countryName}, 客户={$customerName}, 产品={$inquiryProduct}"); + + // 跳过空行 + if (empty($customerName)) { + continue; + } + + // 查找国家ID + $countryId = 0; + if (!empty($countryName)) { + $countryKey = strtolower($countryName); + if ($countries->has($countryKey)) { + $countryId = $countries->get($countryKey)->id; + } else { + // 尝试英文名称匹配 + $countryByEn = $countries->first(function($item) use ($countryName) { + return strtolower(trim($item->en_addr)) === strtolower($countryName); + }); + if ($countryByEn) { + $countryId = $countryByEn->id; + } + } + } + + // 检查是否已存在相同客户名 +// $existingCustomer = Customer::where('contacts', $customerName)->first(); +// if ($existingCustomer) { +// $output->writeln("跳过已存在的客户: {$customerName}"); +// continue; +// } + + // 创建客户记录 + $customerData = [ + 'user_id' => $userId, + 'corp_abbr' => $corpAbbr, + 'channel_id' => $channelId, + 'country_id' => $countryId, + 'contacts' => $customerName, + 'desc_collect' => $inquiryProduct, + 'grade' => 0, // 默认等级 + 'magnitude' => 1, // 默认商机 + 'type' => 1, // 默认类型 + 'sex' => 0, // 默认性别 + 'enter_user_id' => $userId, + ]; + + $customer = Customer::create($customerData); + $successCount++; + + $output->writeln("成功导入客户: {$customerName} (ID: {$customer->id})"); + + } catch (\Exception $e) { + $errorCount++; + $errorMsg = "第{$row}行导入失败: " . $e->getMessage(); + $errorMsg .= "\n文件: " . $e->getFile() . " 行: " . $e->getLine(); + $errorMsg .= "\n堆栈: " . $e->getTraceAsString(); + $errors[] = $errorMsg; + $output->writeln("{$errorMsg}"); + } + } + + $output->writeln("\n导入完成!"); + $output->writeln("成功导入: {$successCount} 条记录"); + $output->writeln("失败: {$errorCount} 条记录"); + + if (!empty($errors)) { + $output->writeln("\n错误详情:"); + foreach ($errors as $error) { + $output->writeln("{$error}"); + } + } + + return self::SUCCESS; + + } catch (\Exception $e) { + $output->writeln("导入过程中发生错误: " . $e->getMessage() . ""); + $output->writeln("文件: " . $e->getFile() . " 行: " . $e->getLine() . ""); + $output->writeln("堆栈: " . $e->getTraceAsString() . ""); + return self::FAILURE; + } + } +} \ No newline at end of file diff --git a/app/crm/service/CustomerService.php b/app/crm/service/CustomerService.php index c6518aa..3cffb7d 100644 --- a/app/crm/service/CustomerService.php +++ b/app/crm/service/CustomerService.php @@ -2,6 +2,7 @@ namespace app\crm\service; +use app\common\model\Corp; use app\common\model\User; use app\crm\logic\CustomerLogic; use app\crm\model\CarType; @@ -83,6 +84,14 @@ class CustomerService $query->onlyTrashed(); } if ($seas == 'seas') { + $corpId = Corp::where('abbr', $params['corp_abbr'])->first()->value('id'); + if($corpId == 1){ + $query->whereIn('channel_id', [1,2,3,4,5,6,7,8]); + } elseif($corpId == 3) { + $query->whereIn('channel_id', [9,10,11]); + } else{ + $query->whereIn('channel_id', [0]); + } $query->withTrashed(); } diff --git a/composer.json b/composer.json index 64b3e58..ec04c9f 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,8 @@ "webman/redis-queue": "^2.1", "symfony/translation": "^7.3", "symfony/filesystem": "^7.3", - "workerman/crontab": "^1.0" + "workerman/crontab": "^1.0", + "phpoffice/phpspreadsheet": "^4.5" }, "suggest": { "ext-event": "For better performance. " diff --git a/composer.lock b/composer.lock index 7f84b75..d88df56 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8ef9d9a07b2be477345002a7623ea951", + "content-hash": "471a31011b58e1e514bdb9c8963055ad", "packages": [ { "name": "aliyuncs/oss-sdk-php", @@ -186,6 +186,85 @@ ], "time": "2024-02-09T16:56:22+00:00" }, + { + "name": "composer/pcre", + "version": "3.3.2", + "source": { + "type": "git", + "url": "https://github.com/composer/pcre.git", + "reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/pcre/zipball/b2bed4734f0cc156ee1fe9c0da2550420d99a21e", + "reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e", + "shasum": "" + }, + "require": { + "php": "^7.4 || ^8.0" + }, + "conflict": { + "phpstan/phpstan": "<1.11.10" + }, + "require-dev": { + "phpstan/phpstan": "^1.12 || ^2", + "phpstan/phpstan-strict-rules": "^1 || ^2", + "phpunit/phpunit": "^8 || ^9" + }, + "type": "library", + "extra": { + "phpstan": { + "includes": [ + "extension.neon" + ] + }, + "branch-alias": { + "dev-main": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Composer\\Pcre\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + } + ], + "description": "PCRE wrapping library that offers type-safe preg_* replacements.", + "keywords": [ + "PCRE", + "preg", + "regex", + "regular expression" + ], + "support": { + "issues": "https://github.com/composer/pcre/issues", + "source": "https://github.com/composer/pcre/tree/3.3.2" + }, + "funding": [ + { + "url": "https://packagist.com", + "type": "custom" + }, + { + "url": "https://github.com/composer", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/composer/composer", + "type": "tidelift" + } + ], + "time": "2024-11-12T16:29:46+00:00" + }, { "name": "defuse/php-encryption", "version": "v2.4.0", @@ -2524,6 +2603,191 @@ ], "time": "2024-12-08T08:18:47+00:00" }, + { + "name": "maennchen/zipstream-php", + "version": "3.1.2", + "source": { + "type": "git", + "url": "https://github.com/maennchen/ZipStream-PHP.git", + "reference": "aeadcf5c412332eb426c0f9b4485f6accba2a99f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/maennchen/ZipStream-PHP/zipball/aeadcf5c412332eb426c0f9b4485f6accba2a99f", + "reference": "aeadcf5c412332eb426c0f9b4485f6accba2a99f", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "ext-zlib": "*", + "php-64bit": "^8.2" + }, + "require-dev": { + "brianium/paratest": "^7.7", + "ext-zip": "*", + "friendsofphp/php-cs-fixer": "^3.16", + "guzzlehttp/guzzle": "^7.5", + "mikey179/vfsstream": "^1.6", + "php-coveralls/php-coveralls": "^2.5", + "phpunit/phpunit": "^11.0", + "vimeo/psalm": "^6.0" + }, + "suggest": { + "guzzlehttp/psr7": "^2.4", + "psr/http-message": "^2.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "ZipStream\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paul Duncan", + "email": "pabs@pablotron.org" + }, + { + "name": "Jonatan Männchen", + "email": "jonatan@maennchen.ch" + }, + { + "name": "Jesse Donat", + "email": "donatj@gmail.com" + }, + { + "name": "András Kolesár", + "email": "kolesar@kolesar.hu" + } + ], + "description": "ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.", + "keywords": [ + "stream", + "zip" + ], + "support": { + "issues": "https://github.com/maennchen/ZipStream-PHP/issues", + "source": "https://github.com/maennchen/ZipStream-PHP/tree/3.1.2" + }, + "funding": [ + { + "url": "https://github.com/maennchen", + "type": "github" + } + ], + "time": "2025-01-27T12:07:53+00:00" + }, + { + "name": "markbaker/complex", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/MarkBaker/PHPComplex.git", + "reference": "95c56caa1cf5c766ad6d65b6344b807c1e8405b9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/MarkBaker/PHPComplex/zipball/95c56caa1cf5c766ad6d65b6344b807c1e8405b9", + "reference": "95c56caa1cf5c766ad6d65b6344b807c1e8405b9", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "dev-master", + "phpcompatibility/php-compatibility": "^9.3", + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0", + "squizlabs/php_codesniffer": "^3.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "Complex\\": "classes/src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mark Baker", + "email": "mark@lange.demon.co.uk" + } + ], + "description": "PHP Class for working with complex numbers", + "homepage": "https://github.com/MarkBaker/PHPComplex", + "keywords": [ + "complex", + "mathematics" + ], + "support": { + "issues": "https://github.com/MarkBaker/PHPComplex/issues", + "source": "https://github.com/MarkBaker/PHPComplex/tree/3.0.2" + }, + "time": "2022-12-06T16:21:08+00:00" + }, + { + "name": "markbaker/matrix", + "version": "3.0.1", + "source": { + "type": "git", + "url": "https://github.com/MarkBaker/PHPMatrix.git", + "reference": "728434227fe21be27ff6d86621a1b13107a2562c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/MarkBaker/PHPMatrix/zipball/728434227fe21be27ff6d86621a1b13107a2562c", + "reference": "728434227fe21be27ff6d86621a1b13107a2562c", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "dev-master", + "phpcompatibility/php-compatibility": "^9.3", + "phpdocumentor/phpdocumentor": "2.*", + "phploc/phploc": "^4.0", + "phpmd/phpmd": "2.*", + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0", + "sebastian/phpcpd": "^4.0", + "squizlabs/php_codesniffer": "^3.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "Matrix\\": "classes/src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mark Baker", + "email": "mark@demon-angel.eu" + } + ], + "description": "PHP Class for working with matrices", + "homepage": "https://github.com/MarkBaker/PHPMatrix", + "keywords": [ + "mathematics", + "matrix", + "vector" + ], + "support": { + "issues": "https://github.com/MarkBaker/PHPMatrix/issues", + "source": "https://github.com/MarkBaker/PHPMatrix/tree/3.0.1" + }, + "time": "2022-12-02T22:17:43+00:00" + }, { "name": "monolog/monolog", "version": "2.10.0", @@ -2831,6 +3095,112 @@ }, "time": "2020-10-15T08:29:30+00:00" }, + { + "name": "phpoffice/phpspreadsheet", + "version": "4.5.0", + "source": { + "type": "git", + "url": "https://github.com/PHPOffice/PhpSpreadsheet.git", + "reference": "2ea9786632e6fac1aee601b6e426bcc723d8ce13" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPOffice/PhpSpreadsheet/zipball/2ea9786632e6fac1aee601b6e426bcc723d8ce13", + "reference": "2ea9786632e6fac1aee601b6e426bcc723d8ce13", + "shasum": "" + }, + "require": { + "composer/pcre": "^1||^2||^3", + "ext-ctype": "*", + "ext-dom": "*", + "ext-fileinfo": "*", + "ext-gd": "*", + "ext-iconv": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-simplexml": "*", + "ext-xml": "*", + "ext-xmlreader": "*", + "ext-xmlwriter": "*", + "ext-zip": "*", + "ext-zlib": "*", + "maennchen/zipstream-php": "^2.1 || ^3.0", + "markbaker/complex": "^3.0", + "markbaker/matrix": "^3.0", + "php": "^8.1", + "psr/http-client": "^1.0", + "psr/http-factory": "^1.0", + "psr/simple-cache": "^1.0 || ^2.0 || ^3.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "dev-main", + "dompdf/dompdf": "^2.0 || ^3.0", + "friendsofphp/php-cs-fixer": "^3.2", + "mitoteam/jpgraph": "^10.3", + "mpdf/mpdf": "^8.1.1", + "phpcompatibility/php-compatibility": "^9.3", + "phpstan/phpstan": "^1.1 || ^2.0", + "phpstan/phpstan-deprecation-rules": "^1.0 || ^2.0", + "phpstan/phpstan-phpunit": "^1.0 || ^2.0", + "phpunit/phpunit": "^10.5", + "squizlabs/php_codesniffer": "^3.7", + "tecnickcom/tcpdf": "^6.5" + }, + "suggest": { + "dompdf/dompdf": "Option for rendering PDF with PDF Writer", + "ext-intl": "PHP Internationalization Functions", + "mitoteam/jpgraph": "Option for rendering charts, or including charts with PDF or HTML Writers", + "mpdf/mpdf": "Option for rendering PDF with PDF Writer", + "tecnickcom/tcpdf": "Option for rendering PDF with PDF Writer" + }, + "type": "library", + "autoload": { + "psr-4": { + "PhpOffice\\PhpSpreadsheet\\": "src/PhpSpreadsheet" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Maarten Balliauw", + "homepage": "https://blog.maartenballiauw.be" + }, + { + "name": "Mark Baker", + "homepage": "https://markbakeruk.net" + }, + { + "name": "Franck Lefevre", + "homepage": "https://rootslabs.net" + }, + { + "name": "Erik Tilt" + }, + { + "name": "Adrien Crivelli" + } + ], + "description": "PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine", + "homepage": "https://github.com/PHPOffice/PhpSpreadsheet", + "keywords": [ + "OpenXML", + "excel", + "gnumeric", + "ods", + "php", + "spreadsheet", + "xls", + "xlsx" + ], + "support": { + "issues": "https://github.com/PHPOffice/PhpSpreadsheet/issues", + "source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/4.5.0" + }, + "time": "2025-07-24T05:15:59+00:00" + }, { "name": "phpoption/phpoption", "version": "1.9.3",