cqp 2 meses atrás
pai
commit
4758401fea

+ 56 - 25
app/Console/Commands/CustomerWriteReport.php

@@ -94,9 +94,9 @@ class CustomerWriteReport extends Command
                         CustomerReport::insert($main);
                         CustomerReportDepart::insert($main_depart);
 
-                        echo '写入中' . PHP_EOL;
+                        echo '200条写入中' . PHP_EOL;
                     } else {
-                        echo '无' . PHP_EOL;
+                        echo '数据写入' . PHP_EOL;
                     }
                 });
             });
@@ -126,33 +126,48 @@ class CustomerWriteReport extends Command
             ->toArray();
 
         // 最新的跟进记录
+        $latestRecordsArray = $detail_insert = [];
         $latestRecords = FollowUpRecord::whereIn('data_id', $customer_id)
             ->where('type', FollowUpRecord::type_one)
             ->where('del_time',0)
-            ->whereIn('id', function ($query) use ($customer_id) {
-                $query->select(DB::raw('MAX(id)'))
-                    ->from('follow_up_record')
-                    ->whereColumn('data_id', 'follow_up_record.data_id')
-                    ->whereIn('data_id', $customer_id)
-                    ->where('type', FollowUpRecord::type_one)
-                    ->where('del_time',0)
-                    ->groupBy('data_id');
-            })
-            ->get();
-        $latestRecordsArray = $latestRecords->keyBy('data_id')
-            ->map(fn($item) => $item->follow_type)
-            ->toArray();
+            ->select('data_id','follow_type','crt_id')
+            ->orderBy('id','desc')
+            ->get()->toArray();
+        foreach ($latestRecords as $value){
+            if(! isset($latestRecordsArray[$value['data_id']])){
+                $latestRecordsArray[$value['data_id']] = [
+                    'follow_type' => $value['follow_type'],
+                    'num' => 1,
+                ];
+            }else{
+                $latestRecordsArray[$value['data_id']]['num'] += 1;
+            }
+
+            $detail_insert[] = [
+                'customer_id' => $value['data_id'],
+                'top_depart_id' => 0,
+                'crt_time' => $time,
+                'type' => CustomerReportDepart::type_three,
+                'man_id' => $value['crt_id'],
+            ];
+        }
 
         $customer_info = CustomerInfo::where('del_time',0)
             ->whereIn('customer_id',$customer_id)
-            ->whereIn('type',[CustomerInfo::type_one])
-            ->select('type','contact_type','contact_info','customer_id')
+            ->whereIn('type',[CustomerInfo::type_one, CustomerInfo::type_two])
+            ->select('type','contact_type','contact_info','customer_id','data_id')
             ->get()->toArray();
-        $customer_info_map = [];
+        $customer_info_map = $customer_info_map2 = [];
         foreach ($customer_info as $value){
-            if(! $value['contact_info']) continue;
-            if(! empty($customer_info_map[$value['customer_id']])) continue;
-            $customer_info_map[$value['customer_id']] = $value['contact_info'];
+            if($value['type'] == CustomerInfo::type_one){
+                if(! $value['contact_info']) continue;
+                if(! empty($customer_info_map[$value['customer_id']])) continue;
+                $customer_info_map[$value['customer_id']] = $value['contact_info'];
+            }else{
+                if(! $value['data_id']) continue;
+                if(! empty($customer_info_map2[$value['customer_id']]) && in_array($value['data_id'], $customer_info_map2[$value['customer_id']])) continue;
+                $customer_info_map2[$value['customer_id']][] = $value['data_id'];
+            }
         }
         $array = array_unique(array_merge_recursive(array_column($data,'customer_from'),array_column($data,'car_type')));
         $basic_map = BasicType::whereIn('id',$array)
@@ -163,7 +178,6 @@ class CustomerWriteReport extends Command
             ->pluck('title','id')
             ->toArray();
 
-        $detail_insert = [];
         foreach ($data as $key => $value){
             $province = $province_code = $city = $city_code = "";
             $is_success = 0;
@@ -198,22 +212,39 @@ class CustomerWriteReport extends Command
                 $detail_insert[] = [
                     'customer_id' => $value['customer_id'],
                     'top_depart_id' => $b,
-                    'crt_time' => $time
+                    'crt_time' => $time,
+                    'type' => CustomerReportDepart::type_one,
+                    'man_id' => 0,
                 ];
             }
+            $customer_info2 = $customer_info_map2[$value['customer_id']] ?? [];
+            if(! empty($customer_info2)){
+                foreach ($customer_info2 as $v_i){
+                    $detail_insert[] = [
+                        'customer_id' => $value['customer_id'],
+                        'top_depart_id' => 0,
+                        'crt_time' => $time,
+                        'type' => CustomerReportDepart::type_two,
+                        'man_id' => $v_i,
+                    ];
+                }
+            }
 
             $type = -1;
+            $r_array = $latestRecordsArray[$value['customer_id']] ?? [];
+            $num = $r_array['num'] ?? 0;
             if(isset($isset_map[$value['customer_id']])){
                 $type = 3;
             }else{
-                if(isset($latestRecordsArray[$value['customer_id']])) $type = $latestRecordsArray[$value['customer_id']];
+                if(! empty($r_array)) $type = $r_array['follow_type'];
             }
 
             $data[$key]['type'] = $type;
+            $data[$key]['follow_num'] = $num;
             $data[$key]['contact_info'] = $customer_info_map[$value['customer_id']] ?? "";
             $data[$key]['customer_from'] = $basic_map[$value['customer_from']] ?? "";
             $data[$key]['car_type'] = $customer_info_map[$value['car_type']] ?? "";
-            $data[$key]['consulting_product_new'] = $basic_all_map[$value['consulting_product_new']] ?? "";
+            $data[$key]['consulting_product_new_title'] = $basic_all_map[$value['consulting_product_new']] ?? "";
             $data[$key]['crt_time'] = $time;
         }
 

+ 36 - 0
app/Http/Controllers/Api/StatisticsController.php

@@ -114,4 +114,40 @@ class StatisticsController extends BaseController
             return $this->json_return(201,$data);
         }
     }
+
+    public function customerReportStepOfFirst(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerReportStepOfFirst($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerReportStepOfSecond(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerReportStepOfSecond($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
+    public function customerReportStepOfThird(Request $request){
+        $service = new StatisticsService();
+        $userData = $request->userData->toArray();
+        list($status,$data) = $service->customerReportStepOfThird($request->all(),$userData);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
 }

+ 12 - 0
app/Model/CustomerReportDepart.php

@@ -8,4 +8,16 @@ class CustomerReportDepart extends UseScopeBaseModel
     const CREATED_AT = 'crt_time';
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
+
+    const type_one = 1;//归属门店
+    const type_two = 2;//客户负责人
+    const type_three = 3;//客户跟进记录创建人
+
+    const type_list = [
+        -1 => '待跟进',
+        0 => '跟进中',
+        1 => '联系不上',
+        2 => '无意向',
+        3 => '已成交',
+    ];
 }

+ 900 - 0
app/Service/StatisticsService.php

@@ -3,10 +3,14 @@
 namespace App\Service;
 
 use App\Model\BasicType;
+use App\Model\BasicTypeAllUse;
 use App\Model\Customer;
+use App\Model\CustomerReport;
+use App\Model\CustomerReportDepart;
 use App\Model\Depart;
 use App\Model\DepartIndex;
 use App\Model\Employee;
+use App\Model\EmployeeDepartPermission;
 use App\Model\InOutRecord;
 use App\Model\InvoiceOrder;
 use App\Model\InvoiceOrderInfo;
@@ -1894,4 +1898,900 @@ class StatisticsService extends Service
 
         return $adjustedData;
     }
+
+    //改装 客户模板 t9改装  不是分配的客户 是所有的 =》 点击省 展示市  只有这个有产品
+    //分社 客户模板 t9改装  不是分配的客户 是所有的 =》 点击省 展示分社
+    //加盟 客户模板 t9分社  不是分配的客户 是所有的 =》 点击省 展示市
+    //销售 不是分配的客户 是所有的 =》 点击分社 展示销售人员(客户的负责人)
+    public function customerReportStepOfFirst($data, $user){
+        if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
+        $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        if(empty($data['type'])) return [false, '请选择报表查看类型'];
+        $type = $data['type'];
+        $result = [];
+        if($type == 1){
+            $type_detail = 1;
+            if(! empty($data['type_detail'])) $type_detail = $data['type_detail'];
+            if($type_detail == 1){
+                $result = $this->customerReportStepOneForProvince($return,$type);
+            }else{
+                $result = $this->customerReportStepOneForProduct($return);
+            }
+        }elseif ($type == 2){
+            $result = $this->customerReportStepOneForProvinceFs($return);
+        }elseif ($type == 3){
+            $result = $this->customerReportStepOneForProvince($return,$type);
+        }elseif ($type == 4){
+            $result = $this->customerReportStepOneForProvinceXs($return);
+        }else{
+            return [false ,'报表查看类型错误'];
+        }
+
+        return [true, ['list' => $result]];
+    }
+
+    private function customerReportStepOneForProduct($time){
+        //基础产品数据
+        $basic_all_data = BasicTypeAllUse::where('del_time',0)
+            ->where('type', BasicTypeAllUse::type_one)
+            ->select('title','id')
+            ->get()->toArray();
+        $basic_all = [];
+        foreach ($basic_all_data as $value){
+            $basic_all[] = [
+                'label' => $value['title'],
+                'value' => $value['id'],
+                'all_num' => 0
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+        $model_type = Customer::Model_type_one;
+        $label = "所有产品";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'all_num' => 0
+        ];
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->where('model_type',$model_type)
+            ->select('consulting_product_new')
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['consulting_product_new']])) {
+                $map[$value['consulting_product_new']] += 1;
+            }else{
+                $map[$value['consulting_product_new']] = 1;
+            }
+            $total['all_num'] += 1;
+        }
+
+        foreach ($basic_all as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $basic_all[$key]['all_num'] = $tmp;
+            }
+        }
+
+        usort($basic_all, function($a, $b) {
+            return $b['all_num'] - $a['all_num'];
+        });
+
+        array_unshift($basic_all, $total);
+
+        return $basic_all;
+    }
+
+    private function customerReportStepOneForProvince($time, $type){
+        //省市数据
+        $addressData = config('address3');
+        if(is_string($addressData)) $addressData = json_decode($addressData, true);
+        $province = [];
+        foreach ($addressData as $value){
+            $province[] = [
+                'label' => $value['label'],
+                'value' => $value['value'],
+                'follow_num' => 0,
+                'all_num' => 0
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+        $model_type = Customer::Model_type_one;
+        if($type == 3) $model_type = Customer::Model_type_three;
+        $label = "所有客资";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'follow_num' => 0,
+            'all_num' => 0
+        ];
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->where('model_type',$model_type)
+            ->where('province_code','<>','')
+            ->select('province_code','follow_num')
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($map[$value['province_code']])) {
+                $tmp = bcadd($value['follow_num'], $map[$value['province_code']]['num_1']);
+                $map[$value['province_code']]['num_1'] = $tmp;
+                $map[$value['province_code']]['num_2'] += 1;
+            }else{
+                $map[$value['province_code']] = [
+                    'num_1' => $value['follow_num'],
+                    'num_2' => 1,
+                ];
+            }
+            $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
+            $total['follow_num'] = $tmp_t;
+            $total['all_num'] += 1;
+        }
+
+        foreach ($province as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $province[$key]['follow_num'] = intval($tmp['num_1']);
+                $province[$key]['all_num'] = $tmp['num_2'];
+            }
+        }
+
+        usort($province, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all_num'] !== $a['all_num']) {
+                return $b['all_num'] - $a['all_num']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['value'] - $b['value']; // value 正序
+        });
+
+        array_unshift($province, $total);
+
+        return $province;
+    }
+
+    private function customerReportStepOneForProvinceFs($time){
+        //省市数据
+        $addressData = config('address3');
+        if(is_string($addressData)) $addressData = json_decode($addressData, true);
+        $province = [];
+        foreach ($addressData as $value){
+            $province[] = [
+                'label' => $value['label'],
+                'value' => $value['value'],
+                'follow_num' => 0,
+                'all_num' => 0
+            ];
+        }
+        //门店归属省
+        $depart = Depart::where('del_time',0)
+            ->where('parent_id',0)
+            ->where('province','<>','')
+            ->select('id','province as province_code')
+            ->get()->toArray();
+        $depart_map = [];
+        foreach ($depart as $value){
+            $depart_map[$value['id']] = $value['province_code'];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+        $model_type = Customer::Model_type_one;
+        $label = "全国分社";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'follow_num' => 0,
+            'all_num' => 0
+        ];
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->where('model_type',$model_type)
+            ->where('province_code','<>','')
+            ->select('province_code','follow_num','customer_id')
+            ->get()->toArray();
+        $detail = CustomerReportDepart::where('del_time',0)
+            ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
+            ->where('type',CustomerReportDepart::type_one)
+            ->select('customer_id','top_depart_id')
+            ->get()->toArray();
+        $detail_map = [];
+        foreach ($detail as $value){
+            $detail_map[$value['customer_id']][] = $value['top_depart_id'];
+        }
+
+        $map = [];
+        foreach ($list as $value) {
+            //客资所属门店
+            $c_belong_id = $detail_map[$value['customer_id']] ?? "";
+            if(empty($c_belong_id)) continue;
+            foreach ($c_belong_id as $id){
+                //客资所属门店所属省
+                $province_code = $depart_map[$id] ?? "";
+                if(empty($province_code)) continue;
+                if(isset($map[$province_code])) {
+                    $tmp = bcadd($value['follow_num'], $map[$province_code]['num_1']);
+                    $map[$province_code]['num_1'] = $tmp;
+                    $map[$province_code]['num_2'] += 1;
+                }else{
+                    $map[$province_code] = [
+                        'num_1' => $value['follow_num'],
+                        'num_2' => 1,
+                    ];
+                }
+                $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
+                $total['follow_num'] = $tmp_t;
+                $total['all_num'] += 1;
+            }
+        }
+
+        foreach ($province as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $province[$key]['follow_num'] = intval($tmp['num_1']);
+                $province[$key]['all_num'] = $tmp['num_2'];
+            }
+        }
+
+        usort($province, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all_num'] !== $a['all_num']) {
+                return $b['all_num'] - $a['all_num']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['value'] - $b['value']; // value 正序
+        });
+
+        array_unshift($province, $total);
+
+        return $province;
+    }
+
+    private function customerReportStepOneForProvinceXs($time){
+        //门店
+        $depart = Depart::where('del_time',0)
+            ->where('parent_id',0)
+            ->select('id','title')
+            ->get()->toArray();
+        $departArray = [];
+        foreach ($depart as $value){
+            $departArray[]= [
+                'label' => $value['title'],
+                'value' => $value['id'],
+                'follow_num' => 0,
+                'all_num' => 0
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+        $label = "所有门店";
+        $total = [
+            'label' => $label,
+            'value' => "",
+            'follow_num' => 0,
+            'all_num' => 0
+        ];
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->select('follow_num','customer_id')
+            ->get()->toArray();
+        $detail = CustomerReportDepart::where('del_time',0)
+            ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
+            ->where('type',CustomerReportDepart::type_one)
+            ->select('customer_id','top_depart_id')
+            ->get()->toArray();
+        $detail_map = [];
+        foreach ($detail as $value){
+            $detail_map[$value['customer_id']][] = $value['top_depart_id'];
+        }
+
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($detail_map[$value['customer_id']])){
+                //客资所属门店
+                $c_belong_id = $detail_map[$value['customer_id']];
+                if(empty($c_belong_id)) continue;
+                foreach ($c_belong_id as $id){
+                    if(isset($map[$id])) {
+                        $tmp = bcadd($value['follow_num'], $map[$id]['num_1']);
+                        $map[$id]['num_1'] = $tmp;
+                        $map[$id]['num_2'] += 1;
+                    }else{
+                        $map[$id] = [
+                            'num_1' => $value['follow_num'],
+                            'num_2' => 1,
+                        ];
+                    }
+                    $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
+                    $total['follow_num'] = $tmp_t;
+                    $total['all_num'] += 1;
+                }
+            }
+        }
+
+        foreach ($departArray as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $departArray[$key]['follow_num'] = intval($tmp['num_1']);
+                $departArray[$key]['all_num'] = $tmp['num_2'];
+            }
+        }
+
+        usort($departArray, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all_num'] !== $a['all_num']) {
+                return $b['all_num'] - $a['all_num']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['value'] - $b['value']; // value 正序
+        });
+
+        array_unshift($departArray, $total);
+
+        return $departArray;
+    }
+
+    public function customerReportStepOfSecond($data, $user){
+        if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
+        $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        if(empty($data['type'])) return [false, '请选择报表查看类型'];
+        if(empty($data['value'])) return [false, 'value不能为空'];
+        $type = $data['type'];
+        if($type == 1){
+            list($list, $total) = $this->customerReportStepSecondForCity($return,$data,$type);
+        }elseif ($type == 2){
+            list($list, $total) = $this->customerReportStepSecondForFs($return, $data);
+        }elseif ($type == 3){
+            list($list, $total) = $this->customerReportStepSecondForCity($return,$data,$type);
+        }elseif ($type == 4){
+            list($list, $total) = $this->customerReportStepSecondForProvinceXs($return,$data);
+        }else{
+            return [false ,'报表查看类型错误'];
+        }
+
+        return [true, ['list' => $list, 'total' => $total]];
+    }
+
+    private function customerReportStepSecondForCity($time, $data, $type){
+        $province_code = $data['value'];
+        //市数据
+        $addressData = config('address3');
+        if(is_string($addressData)) $addressData = json_decode($addressData, true);
+        $city = [];
+        foreach ($addressData as $value){
+            if($value['value'] != $province_code) continue;
+            foreach ($value['children'] as $val){
+                $city[] = [
+                    'label' => $val['label'],
+                    'value' => $val['value'],
+                    'follow_num' => 0,
+                    'all_num' => 0
+                ];
+            }
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+        $model_type = Customer::Model_type_one;
+        if($type == 3) $model_type = Customer::Model_type_three;
+//        $label = "所有客资";
+//        $total = [
+//            'label' => $label,
+//            'value' => "",
+//            'follow_num' => 0,
+//            'all_num' => 0
+//        ];
+
+        $total = [
+            'follow_num' => 0,
+            'all_num' => 0
+        ];
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->where('model_type',$model_type)
+            ->where('province_code', $province_code)
+            ->select('city_code','follow_num')
+            ->get()->toArray();
+
+        $map = [];
+        $first_city = $city[0];
+        foreach ($list as $value) {
+            if(empty($value['city_code'])) $value['city_code'] = $first_city['value'];
+            if(isset($map[$value['city_code']])) {
+                $tmp = bcadd($value['follow_num'], $map[$value['city_code']]['num_1']);
+                $map[$value['city_code']]['num_1'] = $tmp;
+                $map[$value['city_code']]['num_2'] += 1;
+            }else{
+                $map[$value['city_code']] = [
+                    'num_1' => $value['follow_num'],
+                    'num_2' => 1,
+                ];
+            }
+            $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
+            $total['follow_num'] = $tmp_t;
+            $total['all_num'] += 1;
+        }
+
+        foreach ($city as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $city[$key]['follow_num'] = intval($tmp['num_1']);
+                $city[$key]['all_num'] = $tmp['num_2'];
+            }
+        }
+
+        usort($city, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all_num'] !== $a['all_num']) {
+                return $b['all_num'] - $a['all_num']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['value'] - $b['value']; // value 正序
+        });
+
+        return [$city, $total];
+    }
+
+    private function customerReportStepSecondForFs($time, $data){
+        $province_code = $data['value'];
+
+        //省下的所有门店
+        $depart = Depart::where('del_time',0)
+            ->where('parent_id',0)
+            ->where('province', $province_code)
+            ->select('id','title')
+            ->get()->toArray();
+        $depart_id = array_column($depart,'id');
+        $depart_array = [];
+        foreach ($depart as $value){
+            $depart_array[] = [
+                'label' => $value['title'],
+                'value' => $value['id'],
+                'follow_num' => 0,
+                'all_num' => 0
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+        $model_type = Customer::Model_type_one;
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->where('model_type',$model_type)
+            ->where('province','<>','')
+            ->select('province_code','follow_num','customer_id')
+            ->get()->toArray();
+        $detail = CustomerReportDepart::where('del_time',0)
+            ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
+            ->where('type',CustomerReportDepart::type_one)
+            ->whereIn('top_depart_id',$depart_id)
+            ->select('customer_id','top_depart_id')
+            ->get()->toArray();
+        $detail_map = [];
+        foreach ($detail as $value){
+            $detail_map[$value['customer_id']][] = $value['top_depart_id'];
+        }
+
+        $total = [
+            'follow_num' => 0,
+            'all_num' => 0
+        ];
+        $map = [];
+        foreach ($list as $value) {
+            if(isset($detail_map[$value['customer_id']])){
+                //客资所属门店
+                $c_belong_id = $detail_map[$value['customer_id']] ?? [];
+                if(empty($c_belong_id)) continue;
+                foreach ($c_belong_id as $id){
+                    if(isset($map[$id])) {
+                        $tmp = bcadd($value['follow_num'], $map[$id]['num_1']);
+                        $map[$id]['num_1'] = $tmp;
+                        $map[$id]['num_2'] += 1;
+                    }else{
+                        $map[$id] = [
+                            'num_1' => $value['follow_num'],
+                            'num_2' => 1,
+                        ];
+                    }
+                    $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
+                    $total['follow_num'] = $tmp_t;
+                    $total['all_num'] += 1;
+                }
+            }
+        }
+
+        foreach ($depart_array as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $depart_array[$key]['follow_num'] = intval($tmp['num_1']);
+                $depart_array[$key]['all_num'] = $tmp['num_2'];
+            }
+        }
+
+        usort($depart_array, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all_num'] !== $a['all_num']) {
+                return $b['all_num'] - $a['all_num']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['value'] - $b['value']; // value 正序
+        });
+
+        return [$depart_array, $total];
+    }
+
+    private function customerReportStepSecondForProvinceXs($time,$data){
+        $top_depart_id = intval($data['value']);
+
+        //门店下所有部门
+        $all_depart_id = DB::select("
+            WITH RECURSIVE sub_departments AS (
+                SELECT id FROM depart WHERE parent_id = ? AND del_time = 0
+                UNION ALL
+                SELECT d.id FROM depart d
+                INNER JOIN sub_departments s ON d.parent_id = s.id
+                WHERE d.del_time = 0
+            )
+            SELECT id FROM sub_departments
+        ", [$top_depart_id]);
+        $allDepartIds = collect($all_depart_id)->pluck('id')->toArray();
+        $allDepartIds[] = $top_depart_id;
+        //所属于这个门店下的人
+        $man = EmployeeDepartPermission::whereIn('depart_id', $allDepartIds)
+            ->select('employee_id')
+            ->get()->toArray();
+        $man = array_column($man,'employee_id');
+        $employee = Employee::where('del_time',0)
+            ->whereIn('id', $man)
+            ->select('id', 'emp_name', 'number')
+            ->get()->toArray();
+        $employee_map = array_column($employee,null,'id');
+
+        $start = $time[0];
+        $end = $time[1];
+        $total = [
+            'follow_num' => 0,
+            'all_num' => 0
+        ];
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->select('follow_num','customer_id')
+            ->get()->toArray();
+        $detail = CustomerReportDepart::where('del_time',0)
+            ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
+            ->select('customer_id','man_id','type','top_depart_id')
+            ->get()->toArray();
+        $manArray = $man_flag = $detail_array = $man_map = [];
+        foreach ($detail as $value){
+            if($value['type'] == CustomerReportDepart::type_two){
+                if(isset($employee_map[$value['man_id']])){
+                    if(in_array($value['man_id'], $man_flag)) continue;
+                    if(! in_array($value['man_id'], $man)) continue;
+                    $man_flag[] = $value['man_id'];
+                    $e_t = $employee_map[$value['man_id']];
+                    $manArray[] = [
+                        'label' => $e_t['emp_name'],
+                        'value' => $e_t['id'],
+                        'follow_num' => 0,
+                        'all_num' => 0
+                    ];
+                }
+            }elseif ($value['type'] == CustomerReportDepart::type_one && $value['top_depart_id'] == $top_depart_id){
+                if(! in_array($value['customer_id'], $detail_array)) $detail_array[] = $value['customer_id'];
+            }elseif ($value['type'] == CustomerReportDepart::type_three){
+               if(isset($man_map[$value['customer_id']][$value['man_id']])){
+                   $man_map[$value['customer_id']][$value['man_id']] += 1;
+               }else{
+                   $man_map[$value['customer_id']][$value['man_id']] = 1;
+               }
+            }
+        }unset($man_flag);
+
+        $map = [];
+        foreach ($list as $value) {
+            //这个客户是否属于这个门店
+            if(in_array($value['customer_id'], $detail_array)){
+                if(isset($man_map[$value['customer_id']])) {
+                    //客户由谁创建跟进记录
+                    $tmp = $man_map[$value['customer_id']];
+                    foreach ($tmp as $m_id => $val){
+                        if(isset($map[$m_id])) {
+                            $tmp = bcadd($value['follow_num'], $map[$m_id]['num_1']);
+                            $map[$m_id]['num_1'] = $tmp;
+                            $map[$m_id]['num_2'] += 1;
+                        }else{
+                            $map[$m_id] = [
+                                'num_1' => $val,
+                                'num_2' => 1,
+                            ];
+                        }
+                        $tmp_t = bcadd($total['follow_num'], $val);
+                        $total['follow_num'] = $tmp_t;
+                    }
+                }
+                $total['all_num'] += 1;
+            }
+        }
+
+        foreach ($manArray as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $manArray[$key]['follow_num'] = intval($tmp['num_1']);
+                $manArray[$key]['all_num'] = $tmp['num_2'];
+            }
+        }
+
+        usort($manArray, function($a, $b) {
+            // 先比较 all_num
+            if ($b['all_num'] !== $a['all_num']) {
+                return $b['all_num'] - $a['all_num']; // all_num 降序
+            }
+
+            // 如果 all_num 相同,再比较 value
+            return $a['value'] - $b['value']; // value 正序
+        });
+
+        return [$manArray, $total];
+    }
+
+    public function customerReportStepOfThird($data, $user){
+        if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
+        $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
+        if(empty($data['type'])) return [false, '请选择报表查看类型'];
+        if(empty($data['value'])) return [false, 'value不能为空'];
+        if(empty($data['value2'])) return [false, 'value2不能为空'];
+        $type = $data['type'];
+        $result = [];
+        if($type == 1){
+            $result = $this->customerReportStepThirdForDetail($return,$data,$type);
+        }elseif ($type == 2){
+            $result = $this->customerReportStepThirdForFsDetail($return, $data);
+        }elseif ($type == 3){
+            $result = $this->customerReportStepThirdForDetail($return,$data,$type);
+        }elseif ($type == 4){
+            $result = $this->customerReportStepThirdForProvinceXsDetail($return,$data);
+        }else{
+            return [false ,'报表查看类型错误'];
+        }
+
+        return [true, ['list' => $result]];
+    }
+
+    private function customerReportStepThirdForDetail($time, $data, $type){
+        $province_code = $data['value'];
+        $city_code = $data['value2'];
+
+        //市数据
+        $addressData = config('address3');
+        if(is_string($addressData)) $addressData = json_decode($addressData, true);
+        $first_city = "";
+        foreach ($addressData as $value){
+            if($value['value'] != $province_code && ! empty($city)) continue;
+            foreach ($value['children'] as $val){
+                if(! empty($city)) continue;
+                $first_city = $val['value'];
+            }
+        }
+
+        $customer = [];
+        foreach (CustomerReportDepart::type_list as $key => $value){
+            $customer[] = [
+                'label' => $value,
+                'value' => $key,
+                'list' => [],
+                'count' => 0
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+        $model_type = Customer::Model_type_one;
+        if($type == 3) $model_type = Customer::Model_type_three;
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->where('model_type',$model_type)
+            ->where('province_code', $province_code)
+            ->select('city_code','type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
+            ->get()->toArray();
+
+        $map = [];
+        foreach ($list as $value) {
+            if(empty($value['city_code']) && $first_city != $city_code) continue;
+            $tmp = [
+                'customer_id' => $value['customer_id'],
+                'title' => $value['customer_id'],
+                'customer_from' => $value['customer_from'],
+                'car_type' => $value['car_type'],
+                'consulting_product_new_title' => $value['consulting_product_new_title'],
+                'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
+                'contact_info' => $value['contact_info'],
+            ];
+            $map[$value['type']][] = $tmp;
+        }
+
+        foreach ($customer as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $customer[$key]['list'] = $tmp;
+                $customer[$key]['count'] = count($tmp);
+            }
+        }
+
+        return $customer;
+    }
+
+    private function customerReportStepThirdForFsDetail($time, $data){
+        $province_code = $data['value'];
+        $top_depart_id = $data['value2'];
+
+        $customer = [];
+        foreach (CustomerReportDepart::type_list as $key => $value){
+            $customer[] = [
+                'label' => $value,
+                'value' => $key,
+                'list' => [],
+                'count' => 0
+            ];
+        }
+
+        $start = $time[0];
+        $end = $time[1];
+        $model_type = Customer::Model_type_one;
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->where('model_type',$model_type)
+            ->where('province','<>','')
+            ->select('type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
+            ->get()->toArray();
+        $detail = CustomerReportDepart::where('del_time',0)
+            ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
+            ->where('type',CustomerReportDepart::type_one)
+            ->where('top_depart_id', $top_depart_id)
+            ->select('customer_id')
+            ->get()->toArray();
+        $detail_array = array_column($detail,'customer_id');
+
+        $map = [];
+        foreach ($list as $value) {
+            if(in_array($value['customer_id'], $detail_array)){
+                $tmp = [
+                    'customer_id' => $value['customer_id'],
+                    'title' => $value['customer_id'],
+                    'customer_from' => $value['customer_from'],
+                    'car_type' => $value['car_type'],
+                    'consulting_product_new_title' => $value['consulting_product_new_title'],
+                    'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
+                    'contact_info' => $value['contact_info'],
+                ];
+                $map[$value['type']][] = $tmp;
+            }
+        }
+
+        foreach ($customer as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $customer[$key]['list'] = $tmp;
+                $customer[$key]['count'] = count($tmp);
+            }
+        }
+
+        return $customer;
+    }
+
+    private function customerReportStepThirdForProvinceXsDetail($time, $data){
+        $top_depart_id = intval($data['value']);
+        $man_id = $data['value2'];
+
+        $customer = [];
+        foreach (CustomerReportDepart::type_list as $key => $value){
+            $customer[] = [
+                'label' => $value,
+                'value' => $key,
+                'list' => [],
+                'count' => 0
+            ];
+        }
+
+        //门店下所有部门
+        $all_depart_id = DB::select("
+            WITH RECURSIVE sub_departments AS (
+                SELECT id FROM depart WHERE parent_id = ? AND del_time = 0
+                UNION ALL
+                SELECT d.id FROM depart d
+                INNER JOIN sub_departments s ON d.parent_id = s.id
+                WHERE d.del_time = 0
+            )
+            SELECT id FROM sub_departments
+        ", [$top_depart_id]);
+        $allDepartIds = collect($all_depart_id)->pluck('id')->toArray();
+        $allDepartIds[] = $top_depart_id;
+        //所属于这个门店下的人
+        $exists = EmployeeDepartPermission::whereIn('depart_id', $allDepartIds)
+            ->where('employee_id',$man_id)
+            ->exists();
+        if(! $exists) return [];
+
+        $start = $time[0];
+        $end = $time[1];
+
+        $list = CustomerReport::where('del_time',0)
+            ->where('enter_time','>=', $start)
+            ->where('enter_time','<=', $end)
+            ->select('type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
+            ->get()->toArray();
+        $detail = CustomerReportDepart::where('del_time',0)
+            ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
+            ->select('customer_id','man_id','type','top_depart_id')
+            ->get()->toArray();
+        $detail_array = $man_map = [];
+        foreach ($detail as $value){
+            if($value['type'] == CustomerReportDepart::type_two){
+
+            }elseif ($value['type'] == CustomerReportDepart::type_one && $value['top_depart_id'] == $top_depart_id){
+                if(! in_array($value['customer_id'], $detail_array)) $detail_array[] = $value['customer_id'];
+            }elseif ($value['type'] == CustomerReportDepart::type_three){
+                if($value['man_id'] == $man_id) $man_map[$value['customer_id']] = 1;
+            }
+        }
+
+        $map = [];
+        foreach ($list as $value) {
+            //这个客户是否属于当前这个门店
+            if(in_array($value['customer_id'], $detail_array)){
+                if(isset($man_map[$value['customer_id']])) {
+                    $tmp = [
+                        'customer_id' => $value['customer_id'],
+                        'title' => $value['customer_id'],
+                        'customer_from' => $value['customer_from'],
+                        'car_type' => $value['car_type'],
+                        'consulting_product_new_title' => $value['consulting_product_new_title'],
+                        'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
+                        'contact_info' => $value['contact_info'],
+                    ];
+                    $map[$value['type']][] = $tmp;
+                }
+            }
+        }
+
+        foreach ($customer as $key => $value){
+            if(isset($map[$value['value']])){
+                $tmp = $map[$value['value']];
+                $customer[$key]['list'] = $tmp;
+                $customer[$key]['count'] = count($tmp);
+            }
+        }
+
+        return $customer;
+    }
 }

Diferenças do arquivo suprimidas por serem muito extensas
+ 2 - 0
config/address3.php


+ 4 - 0
routes/api.php

@@ -341,6 +341,10 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('statisticsCustomer', 'Api\StatisticsController@statisticsCustomer');
     $route->any('statisticsModelTypeOne', 'Api\StatisticsController@statisticsModelTypeOne');
     $route->any('statisticsModelTypeFour', 'Api\StatisticsController@statisticsModelTypeFour');
+    //客资报表
+    $route->any('customerReportStepOfFirst', 'Api\StatisticsController@customerReportStepOfFirst');
+    $route->any('customerReportStepOfSecond', 'Api\StatisticsController@customerReportStepOfSecond');
+    $route->any('customerReportStepOfThird', 'Api\StatisticsController@customerReportStepOfThird');
 
     //设置开关
     $route->any('productInventorySet', 'Api\ProductInventoryController@productInventorySet');

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff