|
@@ -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;
|
|
|
+ }
|
|
|
}
|