123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App\Service;
- use App\Exports\TableHeadExport;
- use App\Import\Import;
- use App\Import\ImportAll;
- use App\Model\BasicType;
- use App\Model\Customer;
- use App\Model\CustomerInfo;
- use App\Model\Depart;
- use App\Model\Employee;
- use App\Model\Product;
- use App\Model\ProductCategory;
- use App\Model\ProductPriceDetail;
- use App\Model\SalesOrder;
- use App\Model\SalesOrderInfo;
- use App\Model\SalesOrderProductInfo;
- use Illuminate\Support\Facades\DB;
- use Maatwebsite\Excel\Facades\Excel;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- class ImportService extends Service
- {
- public static $type = [
- 'product', //产品
- 'customer', //客户
- 'salesOnline', //线上订单
- ];
- public function getTableTitleXls($data,$user){
- if(empty($data['type'])) return [false,'缺少类型'];
- if(! in_array($data['type'],self::$type)) return [false,'类型不存在'];
- //获取配置文件
- $fuc = $data['type'];
- list($status,$msg,$filename) = $this->$fuc($data,$user);
- if(!$status) return [false, $msg];
- $headers = array_column($msg,'value');
- Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
- return [true, ['file' => $filename]];
- }
- private function customer($data,$user){
- //生成下载文件
- $filename = "客户模板_" . time() . '.' . 'xlsx';
- //获取配置文件
- $config = "excel.customerTable";
- $config_array = config($config) ?? [];
- if(empty($config_array)) return [false, '配置文件不存在',''];
- return [true, $config_array,$filename];
- }
- private function product($data,$user){
- //获取配置文件
- $config = "excel.productTable";
- $config_array = config($config) ?? [];
- if(empty($config_array)) return [false, '配置文件不存在', ''];
- $result = (new BasicTypeService())->getMyBasicList($user, 22);
- if(! empty($result)){
- foreach ($result as $value){
- $config_array[] = [
- 'key' => 'table_id.' . $value['id'],
- 'value' => $value['title'],
- ];
- }
- }
- //生成下载文件
- $filename = "产品模板_" . time() . '.' . 'xlsx';
- return [true, $config_array,$filename];
- }
- private function salesOnline($data,$user){
- //生成下载文件
- $filename = "线上订单模板_" . time() . '.' . 'xlsx';
- //获取配置文件
- $config = "excel.salesOnlineTable";
- $config_array = config($config) ?? [];
- if(empty($config_array)) return [false, '配置文件不存在',''];
- return [true, $config_array,$filename];
- }
- //导入入口
- public function importAll($data,$user){
- // //不超时
- // ini_set('max_execution_time', 0);
- // //内存设置
- // ini_set('memory_limit', -1);
- // $reader = IOFactory::createReader('Xlsx');
- // $reader->setReadDataOnly(true); // 只读取有数据的单元格
- // $spreadsheet = $reader->load($data['file']);
- // dd($spreadsheet);
- // // 创建一个Reader对象
- // $reader = IOFactory::createReader('Xlsx'); // 根据你的文件格式选择合适的reader
- //
- //// 加载Excel文件
- // $spreadsheet = $reader->load($data['file']);
- //
- //// 获取第一个工作表
- // $worksheet = $spreadsheet->getActiveSheet();
- //
- //// 获取总行数
- // $totalRows = $worksheet->getHighestRow();dd($totalRows);
- if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
- if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
- if(empty($data['file'])) return [false,'导入文件不能为空'];
- try {
- $import = new ImportAll();
- //设置导入人id
- $import->setCrt($user['id']);
- $import->setUser($user);
- $import->setType($data['type']);
- //导入
- \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
- if($import->getMsg()) return [false, $import->getMsg()];
- }catch (\Throwable $exception) {
- return [false, $exception->getMessage() . ' (Code: ' . $exception->getCode() . ', Line: ' . $exception->getLine() . ')'];
- }
- return [true, ''];
- }
- }
|