diff --git a/app/Core/AbstractExport.php b/app/Core/AbstractExport.php index bf258dbc..27a170f7 100644 --- a/app/Core/AbstractExport.php +++ b/app/Core/AbstractExport.php @@ -26,6 +26,7 @@ abstract class AbstractExport implements WithEvents, WithTitle, ShouldAutoSize \App\Domains\Virtual\Exports\FlowPoolExportDetailExport::class => '流量池明细', \App\Domains\Virtual\Exports\OrderCardExport::class => '订单卡清单', \App\Domains\Virtual\Exports\OrderExport::class => '订单导出', + \App\Domains\Virtual\Exports\PropertyExport::class => '属性导出', \App\Domains\Stats\Exports\CompanyCountExport::class => '企业统计', \App\Domains\Stats\Exports\OrderExport::class => '订单统计', \App\Domains\Stats\Exports\OrderDetailExport::class => '订单明细', diff --git a/app/Domains/Virtual/Exports/PropertyExport.php b/app/Domains/Virtual/Exports/PropertyExport.php new file mode 100644 index 00000000..401f01ac --- /dev/null +++ b/app/Domains/Virtual/Exports/PropertyExport.php @@ -0,0 +1,108 @@ +conditions = $conditions; + $this->settings = app(PropertySettingRepository::class)->getAll(); + parent::__construct(); + } + + public function collection() + { + return app(PropertyService::class)->index($this->conditions); + } + + public function headings(): array + { + $provinces = $this->settings['province']; + + $headings = [ + '*企业ID', + '企业名称', + '*套餐ID', + '套餐名称', + '月流量', + '销售数量', + '*公司类型', + '*产品类型', + '套餐类型', + '平台/API', + '*车辆类型', + '商用车分类', + '*客户类型', + ]; + + foreach ($provinces as $province) { + array_push($headings, $province); + } + + return $headings; + } + + /** + * @param mixed $row + * + * @return mixed + */ + public function rows($rows) + { + $provinces = $this->settings['province']; + + $array = []; + + foreach ($rows as $key => $value) { + $item = [ + $value['company_id'], + $value['company_name'], + $value['package_id'], + $value['package_name'], + strval($value['flows'] ?: 0), + strval($value['counts']), + $value['company'], + $value['product'], + $value['package'], + $value['platform'], + $value['vehicle'], + $value['commercial_vehicle'], + $value['customer'], + ]; + + foreach ($provinces as $province) { + $item[$province] = strval($value['province'][$province] ?: 0); + } + + $array[] = $item; + } + + return $array; + } + + /** + * @return array + */ + public function columnFormats(): array + { + return [ + 'A' => NumberFormat::FORMAT_NUMBER, + 'C' => NumberFormat::FORMAT_NUMBER, + 'E' => NumberFormat::FORMAT_NUMBER, + 'F' => NumberFormat::FORMAT_NUMBER, + ]; + } +} diff --git a/app/Domains/Virtual/Http/Controllers/PropertyController.php b/app/Domains/Virtual/Http/Controllers/PropertyController.php index d3601046..879ea54f 100644 --- a/app/Domains/Virtual/Http/Controllers/PropertyController.php +++ b/app/Domains/Virtual/Http/Controllers/PropertyController.php @@ -3,7 +3,12 @@ namespace App\Domains\Virtual\Http\Controllers; use App\Core\Controller; use Illuminate\Http\Request; +use App\Domains\Export\Services\ExportService; +use App\Domains\Export\Services\ImportService; +use App\Domains\Virtual\Exports\PropertyExport; use App\Domains\Virtual\Services\PropertyService; +use App\Domains\Virtual\Repositories\PropertySettingRepository; +use App\Exceptions\InvalidArgumentException; class PropertyController extends Controller { @@ -68,7 +73,16 @@ class PropertyController extends Controller */ public function export() { - // + $conditions = $this->request->all(); + + try { + $export = new PropertyExport($conditions); + $url = ExportService::store($export, 'public'); + } catch (\Exception $e) { + throw $e; + } + + return res($url, '导出成功', 201); } /** @@ -78,6 +92,67 @@ class PropertyController extends Controller */ public function import() { - // + $file = $this->request->file('file'); + + $settings = app(PropertySettingRepository::class)->getAll(); + $provinces = $settings['province']; + + $data = ImportService::load($file); + + $array = []; + + $errors = []; + + $columns = [ + 'company_id', + 'package_id', + 'product', + 'vehicle', + 'company', + 'customer', + ]; + + foreach ($data as $i => $item) { + $row = $i + 1; + + foreach ($item as $v => $value) { + if (strpos($v, '*') !== false) { + $item[trim($v, '*')] = $value; + } + } + + if (empty($item['企业id']) || empty($item['套餐id'])) { + throw new InvalidArgumentException("行 {$row} #:无效行"); + } + + $value = [ + 'company_id' => $item['企业id'], + 'package_id' => $item['套餐id'], + 'product' => $item['产品类型'], + 'vehicle' => $item['车辆类型'], + 'commercial_vehicle' => $item['商用车分类'], + 'company' => $item['公司类型'], + 'platform' => $item['平台/api'], + 'customer' => $item['客户类型'], + ]; + + foreach ($provinces as $province) { + $value['province'][$province] = !empty($item[$province]) ? $item[$province] : 0; + $value['province'][$province] = sprintf("%.2f", $value['province'][$province]); + } + + foreach ($columns as $column) { + if (empty($value[$column])) { + $filed = PropertyService::$property_types[$column]; + throw new InvalidArgumentException("行 {$row} #:字段 {$filed} 不能为空"); + } + } + + $array[] = $value; + } + + $this->propertyService->store($array); + + return res(null, '导入成功'); } } diff --git a/app/Domains/Virtual/Services/PropertyService.php b/app/Domains/Virtual/Services/PropertyService.php index 31474c15..a7a70954 100644 --- a/app/Domains/Virtual/Services/PropertyService.php +++ b/app/Domains/Virtual/Services/PropertyService.php @@ -103,11 +103,6 @@ class PropertyService extends Service { $settings = $this->propertySettingRepository->getAll(); - if (isset($values['product'])) { - } - - - foreach ($values as $key => $value) { if (!in_array($key, array_keys(self::$property_types))) { throw new NotExistException("分类{$key}不存在"); diff --git a/database/seeds/PermissionSeeder.php b/database/seeds/PermissionSeeder.php index b0d3b301..d73c30f8 100644 --- a/database/seeds/PermissionSeeder.php +++ b/database/seeds/PermissionSeeder.php @@ -155,7 +155,8 @@ class PermissionSeeder extends Seeder 'children' => [ ['name' => 'virtual.properties.create', 'title' => '设置', 'description' => 'create', 'type' => 1], ['name' => 'virtual.properties.update', 'title' => '修改', 'description' => 'update', 'type' => 1], - ['name' => 'virtual.properties.output', 'title' => '导出', 'description' => 'output', 'type' => 1], + ['name' => 'virtual.properties.export', 'title' => '导出', 'description' => 'output', 'type' => 1], + ['name' => 'virtual.properties.import', 'title' => '导入', 'description' => 'input', 'type' => 1], ], ], ], diff --git a/frontend/src/api/virtual/properties.js b/frontend/src/api/virtual/properties.js index 050b445d..efacd417 100644 --- a/frontend/src/api/virtual/properties.js +++ b/frontend/src/api/virtual/properties.js @@ -58,6 +58,16 @@ export function exportExcel(data) { * @param {[type]} data [description] * @return {[type]} [description] */ -export function importExcel(data) { - return service.post('api/virtual/properties/import', data); +export function importExcel(file) { + let config = { + headers: { + 'Content-Type': 'multipart/form-data' + } + }; + + let params = new FormData(); + + params.append('file', file); + + return service.post('api/virtual/properties/import', params, config); } diff --git a/frontend/src/views/virtual/properties/index.vue b/frontend/src/views/virtual/properties/index.vue index 10265203..617285e0 100644 --- a/frontend/src/views/virtual/properties/index.vue +++ b/frontend/src/views/virtual/properties/index.vue @@ -10,7 +10,7 @@
{{message}}
\n选择图标
\n\n{{message}}
\n选择图标
\n\n{{message}}
\n选择图标
\n\n{{CONFIG.title}}
\n \n{{CONFIG.title}}
\n \n{{CONFIG.title}}
\n \n