cqpCow 11 months ago
parent
commit
f28dc5cc49
1 changed files with 78 additions and 8 deletions
  1. 78 8
      app/Service/ImportService.php

+ 78 - 8
app/Service/ImportService.php

@@ -7,6 +7,7 @@ use App\Import\Import;
 use App\Import\ImportAll;
 use App\Model\BasicType;
 use App\Model\Customer;
+use App\Model\CustomerInfo;
 use App\Model\Employee;
 use App\Model\Product;
 use App\Model\ProductCategory;
@@ -216,7 +217,7 @@ class ImportService extends Service
         if(empty($array)) return [false, '导入数据不能为空'];
 
         //第一次表格数据校验 非空  已经过滤数据
-        $array_clean = [];
+        $array_clean = $contact_info = [];
         foreach ($array as $key => $value){
             $rowData = array_filter($value);
             if (empty($rowData)) {
@@ -232,6 +233,11 @@ class ImportService extends Service
                 if(in_array($value[1],$array_clean)) return [false, '客户名称不能重复'];
                 $array_clean[] = $value[1];
                 $array[$key] = $value;
+
+                if(! empty($value[13])){
+                    if(in_array($value[13],$contact_info)) return [false, '联系方式内容不能重复'];
+                    $contact_info[] = $value[13];
+                }
             }
         }unset($array_clean);
         if(empty($array)) return [false, '导入数据不能为空'];
@@ -245,7 +251,7 @@ class ImportService extends Service
 
         $model = BasicType::TopClear($user,[]);
         $basic = $model->where('del_time',0)
-            ->whereIn('type',[1,2,3,5,9,10])
+            ->whereIn('type',[1,2,3,4,5,9,10])
             ->select('id','title','type')
             ->get()
             ->toArray();
@@ -257,6 +263,8 @@ class ImportService extends Service
                 $basic_list[3][$value['title']] = $value['id'];
             }elseif ($value['type'] == 3){
                 $basic_list[4][$value['title']] = $value['id'];
+            }elseif ($value['type'] == 4){
+                $basic_list[12][$value['title']] = $value['id'];
             }elseif ($value['type'] == 5){
                 $basic_list[7][$value['title']] = $value['id'];
             }elseif ($value['type'] == 9){
@@ -273,12 +281,24 @@ class ImportService extends Service
             ->toArray();
 
         $emp = Employee::where('del_time',0)
-            ->whereIn('emp_name',array_unique(array_merge_recursive(array_column($array,'11'),array_column($array,'12'))))
-            ->pluck('id','emp_name')
+            ->whereIn('number',array_unique(array_column($array,'14')))
+            ->pluck('id','number')
             ->toArray();
 
+        $top_depart_id = $user['depart_top'][0] ?? [];
+        $top_depart_id = $top_depart_id['depart_id'] ?? 0;
+        $contact_info_array = CustomerInfo::from('customer_info as a')
+            ->join('customer as b','b.id','a.customer_id')
+            ->where('a.del_time',0)
+            ->where('b.del_time',0)
+            ->where('b.top_depart_id',$top_depart_id)
+            ->whereIn('a.contact_info', $contact_info)
+            ->select('a.contact_info')->get()->toArray();
+        $contact_info_array = array_column($contact_info_array,'contact_info');
+
         $time = time();
         $insert = [];
+        $insert_detail = $insert_detail2 = [];
         foreach ($array as $value){
             $tmp = [
                 'model_type' => '',
@@ -291,6 +311,7 @@ class ImportService extends Service
                 'intention_product' => '',
                 'progress_stage' => '',
                 'state_type' => '',
+                'address2' => '',
                 'mark' => '',
                 'depart_id' => $head,
                 'top_depart_id' => $head,
@@ -301,6 +322,7 @@ class ImportService extends Service
             $tmp['model_type'] = $value['0'];
             $tmp['consulting_product'] = $value['5'];
             $tmp['mark'] = $value['10'];
+            $tmp['address2'] = $value['11'];
             if(! empty($customer[$value['1']])) return [false, '客户:' . $value['1'] . '已存在'];
             $tmp['title'] = $value['1'];
             if($value['2']){
@@ -331,21 +353,69 @@ class ImportService extends Service
                 if(empty($basic_list[9][$value['9']])) return [false, '车型:' . $value['9'] . '不存在'];
                 $tmp['car_type'] = $basic_list[9][$value['9']];
             }
-//            if($value['11']){
-//                if(empty($emp[$value['11']])) return [false, '负责人:' . $value['11'] . '不存在'];
-//                $tmp['emp_one'] = $emp[$value['11']];
-//            }
+            $contact_id = 0;
+            if($value['12']){
+                if(empty($basic_list[12][$value['12']])) return [false, '联系方式类型:' . $value['12'] . '不存在'];
+                $contact_id = $basic_list[12][$value['12']];
+            }
+            if($value['13'] && in_array($value['13'],$contact_info_array)) return [false, '联系方式内容:' . $value['13'] . '已存在'];
+            $man = 0;
+            if($value['14']){
+                if(empty($emp[$value['14']])) return [false, '负责人:' . $value['14'] . '不存在'];
+                $man = $emp[$value['14']];
+            }
 //            if($value['12']){
 //                if(empty($emp[$value['12']])) return [false, '协同人:' . $value['12'] . '不存在'];
 //                $tmp['emp_two'] = $emp[$value['12']];
 //            }
 
             $insert[] = $tmp;
+
+            $insert_detail[] = [
+                'customer_id' => 0,
+                'contact_type' => $contact_id,
+                'contact_info' => $value['13'],
+                'crt_time' => $time,
+                'type' => CustomerInfo::type_one
+            ];
+            $insert_detail2[] = [
+                'customer_id' => 0,
+                'data_id' => $man,
+                'crt_time' => $time,
+                'type' => CustomerInfo::type_two
+            ];
         }
 
         try{
             DB::beginTransaction();
             if(! empty($insert)) Customer::insert($insert);
+
+            //获取上一次所有id
+            $last_insert_id = Customer::where('crt_time',$time)
+                ->where('crt_time',$time)
+                ->where('depart_id',$head)
+                ->where('top_depart_id',$head)
+                ->where('crt_id',$user['id'])
+                ->select('id')->get()->toArray();
+            $last_insert_id = array_column($last_insert_id,'id');
+
+            //组织数据 写入与主表的关联id
+            $insert_detail_1 = [];
+            foreach ($insert_detail as $key => $value){
+                if(empty($value['contact_type']) && empty($value['contact_info'])) continue;
+                $value['customer_id'] = $last_insert_id[$key];
+                $insert_detail_1[] = $value;
+            }unset($insert_detail);
+            $insert_detail_2 = [];
+            foreach ($insert_detail2 as $key => $value){
+                if(empty($value['data_id'])) continue;
+                $value['customer_id'] = $last_insert_id[$key];
+                $insert_detail_2[] = $value;
+            }unset($insert_detail2);
+
+            if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
+            if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
+
             DB::commit();
         }catch (\Exception $e){
             DB::rollBack();