cqp 1 月之前
父節點
當前提交
62541b4d56
共有 3 個文件被更改,包括 145 次插入2 次删除
  1. 40 1
      app/Service/ExportFileService.php
  2. 62 1
      app/Service/ImportService.php
  3. 43 0
      config/excel/freightTable.php

+ 40 - 1
app/Service/ExportFileService.php

@@ -3,6 +3,7 @@
 namespace App\Service;
 
 use App\Exports\ExportOrder;
+use App\Model\Freight;
 use App\Model\Product;
 use Illuminate\Support\Facades\DB;
 use Maatwebsite\Excel\Facades\Excel;
@@ -41,7 +42,7 @@ class ExportFileService extends Service
 
     protected static $fuc_name = [
         self::type_one => '存货',
-        self::type_two => '派单合同',
+        self::type_two => '运费设置',
         self::type_three => '收付款单',
         self::type_four => '施工单',
         self::type_five => '采购单',
@@ -124,6 +125,39 @@ class ExportFileService extends Service
         return $this->saveExportData($return,$header);
     }
 
+    public function two($ergs,$user){
+        $id = $ergs['id'];
+
+        // 导出数据
+        $return = [];
+
+        DB::table('freight')
+            ->whereIn('id', $id)
+            ->select(Freight::$field)
+            ->orderBy('id','desc')
+            ->chunkById(500,function ($data) use(&$return){
+                $data = Collect($data)->map(function ($object) {
+                    return (array)$object;
+                })->toArray();
+
+                foreach ($data as $value) {
+                    $return[] = [
+                        0 => $value['area'],
+                        1 => $value['region'],
+                        2 => $value['kilometer'],
+                        3 => $value['min_freight_fee'],
+                        4 => $value['one_and_five'],
+                        5 => $value['greater_than_five'],
+                        6 => $value['company'],
+                    ];
+                }
+            });
+
+        $header = ['所属区域','地区','公里数','最低运费','1-5吨','5吨以上','物流公司'];
+
+        return $this->saveExportData($return,$header);
+    }
+
     public function getListForSearch($ergs, $user){
         $data = $ergs['order_search'];
         $id = [];
@@ -132,6 +166,11 @@ class ExportFileService extends Service
             $model = $service->productCommon($data, $user, ['id']);
             $return = $this->limitData($model,'',$data);
             $id = array_column($return,'id');
+        }elseif ($ergs['type'] == self::type_two){
+            $service = new FreightService();
+            $model = $service->freightCommon($data, $user, ['id']);
+            $return = $this->limitData($model,'',$data);
+            $id = array_column($return,'id');
         }
 
         return $id;

+ 62 - 1
app/Service/ImportService.php

@@ -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'];

+ 43 - 0
config/excel/freightTable.php

@@ -0,0 +1,43 @@
+<?php
+return [
+    [
+        'key' =>'area',
+        'value' => '所属区域',
+        'required' => true,
+    ],
+    [
+        'key' =>'region',
+        'value' => '地区',
+        'required' => true,
+    ],
+    [
+        'key' =>'kilometer',
+        'value' => '公里数',
+        'required' => true,
+        'default' => 0,
+    ],
+    [
+        'key' =>'min_freight_fee',
+        'value' => '最低运费',
+        'required' => true,
+        'default' => 0,
+    ],
+    [
+        'key' =>'one_and_five',
+        'value' => '1-5吨',
+        'required' => true,
+        'default' => 0,
+    ],
+    [
+        'key' =>'greater_than_five',
+        'value' => '5吨以上',
+        'required' => true,
+        'default' => 0,
+    ],
+    [
+        'key' =>'company',
+        'value' => '物流公司',
+        'required' => false,
+        'default' => '',
+    ],
+];