|
@@ -4,6 +4,7 @@ namespace App\Service;
|
|
|
|
|
|
use App\Exports\TableHeadExport;
|
|
|
use App\Import\ImportAll;
|
|
|
+use App\Model\Freight;
|
|
|
use App\Model\Product;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
@@ -15,6 +16,7 @@ class ImportService extends Service
|
|
|
{
|
|
|
public static $type = [
|
|
|
'product', //存货
|
|
|
+ 'freight', //运费设置
|
|
|
];
|
|
|
|
|
|
public function getTableTitleXls($data,$user){
|
|
@@ -50,6 +52,16 @@ class ImportService extends Service
|
|
|
return [true, [$config_array, $filename]];
|
|
|
}
|
|
|
|
|
|
+ private function freight($data,$user){
|
|
|
+ $config_array = $this->getTableConfig($data['type']);
|
|
|
+ if(empty($config_array)) return [false, ['导入配置表头文件不存在','']];
|
|
|
+
|
|
|
+ //生成下载文件
|
|
|
+ $filename = "运费设置导入模板_" . time() . '.' . 'xlsx';
|
|
|
+
|
|
|
+ return [true, [$config_array, $filename]];
|
|
|
+ }
|
|
|
+
|
|
|
//导入入口
|
|
|
public function importAll($data,$user){
|
|
|
// //不超时
|
|
@@ -167,6 +179,51 @@ class ImportService extends Service
|
|
|
return [true, ''];
|
|
|
}
|
|
|
|
|
|
+ public function freightImport($array, $user, $other_param){
|
|
|
+ $upload = $array[0];
|
|
|
+ list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+ $table_config = $msg;
|
|
|
+
|
|
|
+ // 去除表头
|
|
|
+ unset($array[0]);
|
|
|
+ if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
+
|
|
|
+ list($array, $error) = $this->checkCommon($array, $table_config);
|
|
|
+ if(! empty($error)) return [0, $error];
|
|
|
+ if(empty($array)) return [false, '导入数据不能为空'];
|
|
|
+
|
|
|
+ $time = time();
|
|
|
+ $insert = [];
|
|
|
+ foreach ($array as $value){
|
|
|
+ $tmp = [];
|
|
|
+ foreach ($value as $k => $val){
|
|
|
+ $field = $table_config[$k]['key'];
|
|
|
+ $tmp[$field] = $val;
|
|
|
+ }
|
|
|
+
|
|
|
+ $tmp['crt_id'] = $user['id'];
|
|
|
+ $tmp['crt_time'] = $time;
|
|
|
+
|
|
|
+ //产品主表
|
|
|
+ $insert[] = $tmp;
|
|
|
+ }
|
|
|
+
|
|
|
+ try{
|
|
|
+ Freight::where('del_time',0)
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+ //新增
|
|
|
+ if(! empty($insert)) Freight::insert($insert);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $e){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
//公共校验
|
|
|
private function checkCommon($array, $table_config){
|
|
|
$error = [];
|
|
@@ -211,7 +268,11 @@ class ImportService extends Service
|
|
|
$error[] = $line . $row . "数据不存在";
|
|
|
}else{
|
|
|
$table_tmp = $table_config[$k] ?? [];
|
|
|
- if(! empty($table_tmp['required']) && empty($tmp_v)) $error[] = $line . $table_tmp['value'] . "必填";
|
|
|
+ if(! empty($table_tmp['required'])){
|
|
|
+ if ($tmp_v === '' || ! isset($tmp_v)) {
|
|
|
+ $error[] = $line . $table_tmp['value'] . "必填";
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if(empty($tmp_v) && isset($table_tmp['default'])) $tmp_v = $table_tmp['default'];
|