request = request(); $this->employee = $this->request->empcrm; $this->root_id = $this->request->empcrm->root_id; $orgId = $this->request->param('org_id'); if (empty($orgId)) $orgId = $this->employee->org_id; $this->loginEmployeeOrg = Org::where([['id', '=', $orgId], ['path', 'like', $this->root_id . '-%']])->find(); if (empty($this->loginEmployeeOrg)) abort(404, '页面异常'); $this->searchOrg = Org::where([['path', 'like', $this->root_id . '-%']])->column('id'); // 时间设置 $start = '2020-01-01'; $end = date('Y-m-d'); $date = $this->request->param('date'); empty($date) ?: list($start, $end) = explode(' - ', $date); $param['start'] = date('Y-m-d 00:00:00', strtotime($start)); $param['end'] = date('Y-m-d 00:00:00', strtotime($end) + 86400); $this->param = $param; } /** * 渠道统计 */ public function source() { if (!$this->request->isAjax()) { $data = CustomerSource::where([ ['root_id', '=', $this->root_id] ])->column('id as value,source as name'); view::assign('source', $data); View::assign('org', $this->org()); return View::fetch(); } $sourceIdList = $this->request->param('source'); $condition = [['root_id', '=', $this->root_id]]; if (!empty($sourceIdList)) $condition[] = ['id', 'in', $sourceIdList]; // 渠道查询 $data = CustomerSource::where($condition)->column('id,source'); $count = count($data); // 投放记录获取 $this->searchOrg = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%']])->column('id'); $putIn = CustomerExtension::with('source')->where([ ['root_id', '>=', $this->root_id], ['org_id', 'in', $this->searchOrg], ['addtime', '>=', $this->param['start']], ['addtime', '<=', $this->param['end']], ])->group('source_id')->column('sum(money) as money', 'source_id'); // 线索量获取 $clue = $this->sourceClue(); // 飞鱼线索获取 $fishclue = $this->fish('source'); // 有效线索 $valid = $this->sourceValid(); // 见面量 $meet = $this->sourceMeet(); // 定金量 $deposit = $this->sourceDeposit(); // 合同量/合同金额 $sign = $this->customer('sign'); // 数据合并 $this->dealData($data, $putIn, 'money'); $this->dealData($data, $clue, 'clue'); $this->dealData($data, $valid, 'valid'); $this->dealData($data, $meet, 'meet'); $this->dealData($data, $deposit, 'deposit'); $this->dealData($data, $sign, ['sign', 'sign_money']); foreach ($data as &$item) { if (!in_array($item['source'], ['飞鱼线索', '腾讯线索', '欧派线索'])) continue; foreach ($fishclue as $f => $n) { if (($f == 1 || $f == 4) && $item['source'] == '飞鱼线索') { $item['clue'] += $n; continue; } elseif ($f == 2 && $item['source'] == '腾讯线索') { $item['clue'] += $n; break; } elseif ($f == 3 && $item['source'] == '欧派线索') { $item['clue'] += $n; break; } } } $this->dealRowData($data, [ 'clue_money' => ['division' => 'money/clue', '*' => 0, 'islv' => 0], 'valid_money' => ['division' => 'money/valid', '*' => 0, 'islv' => 0], 'valid_percen' => ['division' => 'valid/clue', '*' => 100, 'islv' => 1], 'meet_percen' => ['division' => 'meet/valid', '*' => 100, 'islv' => 1], 'grant' => ['division' => 'meet/clue', '*' => 100, 'islv' => 1], 'meet_money' => ['division' => 'money/meet', '*' => 0, 'islv' => 0], 'deposit_percen' => ['division' => 'deposit/meet', '*' => 100, 'islv' => 1], 'sign_agv_money' => ['division' => 'sign_money/sign', '*' => 0, 'islv' => 0], 'ROI' => ['division' => 'money/sign_money', '*' => 100, 'islv' => 0, 'lv' => 0], ], ['source' => '求和']); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 销售/客服统计 */ public function sale() { View::assign('org', $this->org()); if (!$this->request->isAjax()) return View::fetch(); $this->saleOrg = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%'], ['org_type', '=', 1]])->column('id'); // 获取员工列表和员工数 $data = Employee::where([ ['root_id', '=', $this->root_id], ['org_id', 'in', $this->saleOrg], ['state', '=', '在职'], ['verified', '=', 1], ['uid', '<>', 0] ])->column('id,name'); $count = count($data); if ($count == 0) return json(['code' => 0, 'data' => $data, 'count' => $count]); // 线索量 $clue = $this->employeeClue(); // 飞鱼线索获取 $fishclue = $this->fish('employee'); // 有效量 $valid = $this->employeeValid(); // 待确认 $toBeConfirmed = $this->employeeToBeConfirmed(); // 见面量 $meet = $this->employeeMeet(); // 定金量 $deposit = $this->employeeDeposit(); // 合同量 $sign = $this->customer('employee_sign'); $this->dealData($data, $clue, 'clue'); $this->dealData($data, $valid, 'valid'); $this->dealData($data, $toBeConfirmed, 'to_be_confirmed'); $this->dealData($data, $meet, 'meet'); $this->dealData($data, $deposit, 'deposit'); $this->dealData($data, $sign, ['sign', 'sign_money']); $validState = CustomerStateCheck::where([ ['root_id', '=', $this->root_id], ['org_id', 'in', $this->saleOrg], ['check_state', '=', 1] ])->column('count(*) as num', 'employee_id'); foreach ($data as &$item) { if (isset($fishclue[$item['id']])) $item['clue'] += $fishclue[$item['id']]; $item['valid_check_num'] = $validState[$item['id']] ?? 0; } $this->dealRowData($data, [ 'valid_percen' => ['division' => 'valid/clue', '*' => 100, 'islv' => 1], 'meet_percen' => ['division' => 'meet/valid', '*' => 100, 'islv' => 1], 'grant' => ['division' => 'meet/clue', '*' => 100, 'islv' => 1], 'deposit_percen' => ['division' => 'deposit/meet', '*' => 100, 'islv' => 1] ], ['name' => '求和']); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 设计师统计 */ public function designer() { if (!$this->request->isAjax()) { View::assign('org', $this->org()); return View::fetch(); } $this->designerOrg = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%'], ['org_type', '=', 2]])->column('id'); $data = Employee::where([ ['root_id', '=', $this->root_id], ['org_id', 'in', $this->designerOrg], ['state', '=', '在职'], ['verified', '=', 1], ['uid', '<>', 0] ])->column('id,name'); $this->designerList = array_column($data, 'id'); // 获取员工列表和员工数 $count = count($data); if ($count == 0) return json(['code' => 0, 'data' => $data, 'count' => $count]); // 见面量 $meet = $this->designerMeet(); // 定金量 $deposit = $this->designerDeposit(); // 合同量/业绩总额 $sign = $this->customer('designer_sign'); $this->dealData($data, $meet, 'meet'); $this->dealData($data, $deposit, 'deposit'); $this->dealData($data, $sign, ['sign', 'sign_money']); $this->dealRowData($data, [ 'deposit_percen' => ['division' => 'deposit/meet', '*' => 100, 'islv' => 1] ], ['name' => '求和']); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 设计师部门统计 */ public function designerDepartment() { if (!$this->request->isAjax()) return View::fetch(); $this->designerOrg = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%'], ['org_type', '=', 2]])->column('id'); $data = Employee::where([ ['root_id', '=', $this->root_id], ['org_id', 'in', $this->designerOrg], ['state', '=', '在职'], ['verified', '=', 1], ['uid', '<>', 0] ])->column('id,org_id'); $this->designerList = array_column($data, 'id'); // 获取员工列表和员工数 if (count($data) == 0) return json(['code' => 0, 'data' => $data, 'count' => 0]); // 见面量 $meet = $this->designerMeet(); // 定金量 $deposit = $this->designerDeposit(); // 合同量/业绩总额 $sign = $this->customer('designer_sign'); $this->dealData($data, $meet, 'meet'); $this->dealData($data, $deposit, 'deposit'); $this->dealData($data, $sign, ['sign', 'sign_money']); $departmentData = Org::where([['path', 'like', $this->loginEmployeeOrg->path . '%'], ['org_type', '=', 2]])->column('id,name'); $dealData = []; foreach ($data as $designer) { if (!isset($dealData[$designer['org_id']])) $dealData[$designer['org_id']] = ['meet' => 0, 'deposit' => 0, 'sign' => 0, 'sign_money' => 0]; $dealData[$designer['org_id']]['meet'] += $designer['meet']; $dealData[$designer['org_id']]['deposit'] += $designer['deposit']; $dealData[$designer['org_id']]['sign'] += $designer['sign']; $dealData[$designer['org_id']]['sign_money'] += $designer['sign_money']; } foreach ($departmentData as $k => $depart) { if (isset($dealData[$depart['id']])) { $departmentData[$k]['meet'] = $dealData[$depart['id']]['meet']; $departmentData[$k]['deposit'] = $dealData[$depart['id']]['deposit']; $departmentData[$k]['sign'] = $dealData[$depart['id']]['sign']; $departmentData[$k]['sign_money'] = $dealData[$depart['id']]['sign_money']; } else { $departmentData[$k] = array_merge($depart, ['meet' => 0, 'deposit' => 0, 'sign' => 0, 'sign_money' => 0]); } } $count = count($departmentData); $this->dealRowData($departmentData, [ 'deposit_percen' => ['division' => 'deposit/meet', '*' => 100, 'islv' => 1] ], ['name' => '求和']); return json(['code' => 0, 'data' => $departmentData, 'count' => $count]); } /** * 飞鱼线索统计 */ private function fish($type) { $data = []; switch ($type) { case 'source': $data = FishData::where([ ['org_id', 'in', $this->searchOrg], ['is_allocation', '=', 0], ['create_time_detail', '>=', $this->param['start']], ['create_time_detail', '<', $this->param['end']] ])->group('type')->column('count(id) as num', 'type'); break; case 'employee': $data = FishData::where([ ['org_id', 'in', $this->saleOrg], ['is_allocation', '=', 0], ['create_time_detail', '>=', $this->param['start']], ['create_time_detail', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); break; } return $data; } /** * 投放数据获取 */ private function putin($type, $adid) { $data = []; switch ($type) { case 'money': // 投放金额 $data = FishCampaign::where([ ['advertiser_id', 'in', $adid], ['data_time', '>=', $this->param['start']], ['data_time', '<', $this->param['end']] ])->group('advertiser_id')->column('sum(`cost`) as exten_money', 'advertiser_id'); break; case 'clue': // 线索量 $data = FishData::where([ ['advertiser_id', 'in', $adid], ['create_time', '>=', $this->param['start']], ['create_time', '<', $this->param['end']] ])->column('count(*) as clue_num', 'advertiser_id'); break; case 'valid': // 有效量 $data = Customer::alias('customer')->join('fish_data fish', 'customer.id=fish.customer_id') ->where([ ['fish.advertiser_id', 'in', $adid], ['fish.create_time', '>=', $this->param['start']], ['fish.create_time', '<', $this->param['end']], ['customer.source_id', 'NOTNULL', ''], ['customer.source_id', '<>', 0], ['customer.valid_time', 'NOTNULL', ''] ])->column('count(*) as valid_num', 'fish.advertiser_id'); break; case 'meet': $logQuery = CustomerVisitLog::alias('customer_visit_log')->join('fish_data fish', 'customer_visit_log.customer_id=fish.customer_id') ->where([ ['fish.advertiser_id', 'in', $adid], ['fish.create_time', '>=', $this->param['start']], ['fish.create_time', '<', $this->param['end']], ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]], ])->group('customer_visit_log.customer_id')->fetchSql(true)->column('customer_visit_log.customer_id as id, fish.advertiser_id'); $data = Db::table('(' . $logQuery . ') a') ->group('advertiser_id')->column('count(id) as meet_num', 'advertiser_id'); break; case 'deposit': $logQuery = CustomerVisitLog::alias('customer_visit_log')->join('fish_data fish', 'customer_visit_log.customer_id=fish.customer_id') ->where([ ['fish.advertiser_id', 'in', $adid], ['fish.create_time', '>=', $this->param['start']], ['fish.create_time', '<', $this->param['end']], ['customer_visit_log.state', 'in', [2, 3]], ])->group('customer_visit_log.customer_id')->fetchSql(true)->column('customer_visit_log.customer_id as id, fish.advertiser_id'); $data = Db::table('(' . $logQuery . ') a') ->group('advertiser_id')->column('count(id) as meet_num', 'advertiser_id'); break; case 'sign': $logQuery = CustomerVisitLog::alias('customer_visit_log') ->join('fish_data fish', 'customer_visit_log.customer_id=fish.customer_id') ->join('customer customer', 'customer.id=customer_visit_log.customer_id') ->where([ ['fish.advertiser_id', 'in', $adid], ['fish.create_time', '>=', $this->param['start']], ['fish.create_time', '<', $this->param['end']], ['customer_visit_log.state', 'in', [3]], ])->group('customer_visit_log.customer_id')->fetchSql(true)->column('customer_visit_log.customer_id as id, fish.advertiser_id, customer.signed_money'); $data = Db::table('(' . $logQuery . ') a') ->group('advertiser_id')->column('count(id) as meet_num, sum(signed_money) as signed_money', 'advertiser_id'); break; } return $data; } /** * 线索量(按来源) */ private function sourceClue() { $data = []; $this->merge($data, $this->customer('clue')); // 回收站表线索统计 $this->merge($data, $this->customerRecycle('clue')); // 自动回收表线索统计 $this->merge($data, $this->customerDropPool('clue')); return $data; } /** * 有效量(按来源) */ private function sourceValid() { //客户有效统计 $data = $this->customer('valid'); // 回收站表线索统计 $rdata = $this->customerRecycle('valid'); $this->merge($data, $rdata); // 自动回收表线索统计 $ddata = $this->customerDropPool('valid'); $this->merge($data, $ddata); return $data; } /** * 见面量(按来源) */ private function sourceMeet() { $data = []; $this->merge($data, $this->customer('meet')); $this->merge($data, $this->customerRecycle('meet')); $this->merge($data, $this->customerDropPool('meet')); return $data; } /** * 定金量(按来源) */ private function sourceDeposit() { $data = []; $this->merge($data, $this->customer('deposit')); $this->merge($data, $this->customerRecycle('deposit')); $this->merge($data, $this->customerDropPool('deposit')); return $data; } /** * 线索量(按员工) */ private function employeeClue() { $data = []; // 客户表线索统计 $cdata = $this->customer('employee_clue'); $this->merge($data, $cdata); // 客户无效表(获取无效时,客户所属员工) $iData = $this->customerRecycle('employee_clue'); $this->merge($data, $iData); // 自动回收表线索统计 $ddata = $this->customerDropPool('employee_clue'); $this->merge($data, $ddata); return $data; } /** * 待确认 */ private function employeeToBeConfirmed() { $data = []; // 客户表待确认统计 $this->merge($data, $this->customer("employee_to_be_confirmed")); // 客户回收表待确认统计 $this->merge($data, $this->customerDropPool("employee_to_be_confirmed")); return $data; } /** * 有效量 */ private function employeeValid() { //客户有效统计 $data = $this->customer('employee_valid'); // 回收站表线索统计 $rdata = $this->customerRecycle('employee_valid'); $this->merge($data, $rdata); // 自动回收表线索统计 $ddata = $this->customerDropPool('employee_valid'); $this->merge($data, $ddata); return $data; } /** * 见面量 */ private function employeeMeet() { $data = []; $this->merge($data, $this->customer('employee_meet')); $this->merge($data, $this->customerRecycle('employee_meet')); $this->merge($data, $this->customerDropPool('employee_meet')); return $data; } /** * 定金量 */ private function employeeDeposit() { $data = []; $this->merge($data, $this->customer('employee_deposit')); $this->merge($data, $this->customerRecycle('employee_deposit')); $this->merge($data, $this->customerDropPool('employee_deposit')); return $data; } /** * 见面量(设计师) */ private function designerMeet() { $data = []; $this->merge($data, $this->customer('designer_meet')); $this->merge($data, $this->customerDropPool('designer_meet')); return $data; } /** * 定金量(设计师) */ private function designerDeposit() { $data = []; $this->merge($data, $this->customer('designer_deposit')); $this->merge($data, $this->customerDropPool('designer_deposit')); return $data; } /** * customer表查询 */ private function customer($type) { $data = []; switch ($type) { case 'clue': // 客户表线索统计 $data = Customer::where([ ['org_id', 'in', $this->searchOrg], ['sign_time', '>=', $this->param['start']], ['sign_time', '<', $this->param['end']], ['source_id', 'NOTNULL', ''], ['source_id', '<>', 0] ])->group('source_id')->column('count(id) as num', 'source_id'); break; case 'employee_clue': $data = Customer::where([ ['org_id', 'in', $this->saleOrg], ['employee_id', 'NOTNULL', ''], ['sign_time', '>=', $this->param['start']], ['sign_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); $start = $this->param['start']; $end = $this->param['end']; $saleOrg = $this->saleOrg; $data1 = CustomerInvalidLog::where([['id', 'in', function ($query) use ($start, $end, $saleOrg) { $query->name('customer_invalid_log')->where('customer_id', 'in', function ($query) use ($start, $end, $saleOrg) { $query->name('Customer')->where([ ['org_id', 'in', $saleOrg], ['employee_id', 'NULL', ''], ['sign_time', '>=', $start], ['sign_time', '<', $end] ])->field('id'); })->group('customer_id')->field('max(id)'); }]])->group('employee_id')->column('count(id) as num', 'employee_id'); foreach ($data1 as $k => $n) { if (isset($data[$k])) $data[$k] += $n; else $data[$k] = $n; } break; case 'employee_to_be_confirmed': $data = Customer::where([ ['org_id', 'in', $this->saleOrg], ['employee_id', 'NOTNULL', ''], ['sign_time', '>=', $this->param['start']], ['sign_time', '<', $this->param['end']], ['state', '=', 0] ])->group('employee_id')->column('count(id) as num', 'employee_id'); break; case 'valid': $data = Customer::where([ ['org_id', 'in', $this->searchOrg], ['sign_time', '>=', $this->param['start']], ['sign_time', '<', $this->param['end']], ['source_id', 'NOTNULL', ''], ['source_id', '<>', 0], ['valid_time', 'NOTNULL', ''] ])->group('source_id')->column('count(id) as num', 'source_id'); break; case 'employee_valid': $data = Customer::where([ ['org_id', 'in', $this->saleOrg], ['valid_time', '>=', $this->param['start']], ['valid_time', '<', $this->param['end']], ['employee_id', 'NOTNULL', ''] ])->group('employee_id')->column('count(id) as num', 'employee_id'); $customerIdList = Customer::where([ ['org_id', 'in', $this->saleOrg], ['employee_id', 'NULL', ''], ['valid_time', '>=', $this->param['start']], ['valid_time', '<', $this->param['end']] ])->group('id')->column('id'); $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->group('customer_id')->column('max(id)'); $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'employee_id'); foreach ($data1 as $k => $n) { if (isset($data[$k])) $data[$k] += $n; else $data[$k] = $n; } break; case 'meet': $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]], ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.source_id as source_id, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('source_id')->column('count(id) as num', 'source_id'); break; case "employee_meet": $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]], ['customer.employee_id', 'NOTNULL', ''] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.employee_id as employee_id, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); $logQuery1 = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]], ['customer.employee_id', 'NULL', ''] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, min(customer_visit_log.confirm_date) as first_meet_time'); $customerIdList = Db::table('(' . $logQuery1 . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->fetchSql(true)->column('id'); $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->fetchSql(true)->group('customer_id')->column('max(id)'); $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'employee_id'); foreach ($data1 as $k => $n) { if (isset($data[$k])) $data[$k] += $n; else $data[$k] = $n; } break; case "designer_meet": $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]], ['customer.designer_id', 'NOTNULL', ''], ['customer.designer_id', 'in', $this->designerList] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.designer_id as employee_id, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); $logQuery1 = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['customer_visit_log.state', 'in', [2, 3, 7, 8, 9]], ['customer.employee_id', 'NULL', ''] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, min(customer_visit_log.confirm_date) as first_meet_time'); $customerIdList = Db::table('(' . $logQuery1 . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->fetchSql(true)->column('id'); $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->group('customer_id')->column('max(id)'); $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'designer_id'); foreach ($data1 as $k => $n) { if (isset($data[$k])) $data[$k] += $n; else $data[$k] = $n; } break; case 'deposit': $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['customer_visit_log.state', 'in', [2, 3]], ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.source_id as source_id, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('source_id')->column('count(id) as num', 'source_id'); break; case 'employee_deposit': $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['customer_visit_log.state', 'in', [2, 3]], ['customer.employee_id', 'NOTNULL', ''] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.employee_id as employee_id, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); $logQuery1 = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['customer_visit_log.state', 'in', [2, 3]], ['customer.employee_id', 'NULL', ''] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, min(customer_visit_log.confirm_date) as first_meet_time'); $customerIdList = Db::table('(' . $logQuery1 . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->fetchSql(true)->column('id'); $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->fetchSql(true)->group('customer_id')->column('max(id)'); $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'employee_id'); foreach ($data1 as $k => $n) { if (isset($data[$k])) $data[$k] += $n; else $data[$k] = $n; } break; case 'designer_deposit': $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['customer_visit_log.state', 'in', [2, 3]], ['customer.designer_id', 'NOTNULL', ''], ['customer.designer_id', 'in', $this->designerList] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.designer_id as employee_id, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); $logQuery1 = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['customer_visit_log.state', 'in', [2, 3]], ['customer.employee_id', 'NULL', ''] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, min(customer_visit_log.confirm_date) as first_meet_time'); $customerIdList = Db::table('(' . $logQuery1 . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->fetchSql(true)->column('id'); $lastLogId = CustomerInvalidLog::where([['customer_id', 'in', $customerIdList]])->fetchSql(true)->group('customer_id')->column('max(id)'); $data1 = CustomerInvalidLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'designer_id'); foreach ($data1 as $k => $n) { if (isset($data[$k])) $data[$k] += $n; else $data[$k] = $n; } break; case 'sign': $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['customer_visit_log.state', '=', 3], ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.source_id as source_id, customer.signed_money as signed_money, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('source_id')->column('count(id) as sign, sum(signed_money) as sign_money', 'source_id'); break; case 'employee_sign': $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['customer_visit_log.state', '=', 3], ['customer.employee_id', 'NOTNULL', ''] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.employee_id as employee_id, customer.signed_money as signed_money, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as sign, sum(signed_money) as sign_money', 'employee_id'); break; case 'designer_sign': $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['customer_visit_log.state', '=', 3], ['customer.designer_id', 'NOTNULL', ''], ['customer.designer_id', 'in', $this->designerList] ])->group('customer.id')->fetchSql(true)->column('customer.id as id, customer.designer_id as employee_id, customer.signed_money as signed_money, min(customer_visit_log.confirm_date) as first_meet_time'); $data = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as sign, sum(signed_money) as sign_money', 'employee_id'); break; } return $data; } /** * customer表查询 */ private function customerRecycle($type) { $data = []; switch ($type) { case 'clue': // 客户表线索统计 $data = CustomerRecycle::where([ ['org_id', 'in', $this->searchOrg], ['sign_time', '>=', $this->param['start']], ['sign_time', '<', $this->param['end']], ['source_id', 'NOTNULL', ''], ['source_id', '<>', 0] ])->group("source_id")->column('count(id) as num', "source_id"); break; case 'employee_clue': $customerIdList = Db::name('customer_visit_log') ->alias('log') ->join('customer_recycle customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['log.customer_employee_id', '<>', 0], ['customer.sign_time', '>=', $this->param['start']], ['customer.sign_time', '<', $this->param['end']] ]) ->group('log.customer_id') ->column('log.customer_id'); if (empty($customerIdList)) { $data = []; break; } $lastLogId = CustomerVisitLog::where([['customer_id', 'in', $customerIdList], ['customer_employee_id', '<>', 0]])->group('customer_id')->column('max(id)'); $data = CustomerVisitLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'customer_employee_id'); break; case 'valid': $data = CustomerRecycle::where([ ['org_id', 'in', $this->searchOrg], ['valid_time', '>=', $this->param['start']], ['valid_time', '<', $this->param['end']], ['source_id', 'NOTNULL', ''], ['source_id', '<>', 0] ])->group("source_id")->column('count(id) as num', "source_id"); break; case 'employee_valid': $customerIdList = CustomerRecycle::where([ ['org_id', 'in', $this->saleOrg], ['valid_time', '>=', $this->param['start']], ['valid_time', '<', $this->param['end']] ])->column('customer_id'); if (empty($customerIdList)) { $data = []; break; } $lastLogId = CustomerVisitLog::where([['customer_id', 'in', $customerIdList], ['customer_employee_id', '<>', 0]])->group('customer_id')->column('max(id)'); $data = CustomerVisitLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'customer_employee_id'); break; case 'meet': $recycleQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_recycle customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['log.state', 'in', [2, 3, 7, 8, 9]], ]) ->group('log.customer_id') ->field('log.customer_id as id, customer.source_id as source_id, min(log.confirm_date) as first_meet_time') ->BuildSql(); $data = Db::table($recycleQuery . ' a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('source_id')->column('count(id) as num', 'source_id'); break; case 'employee_meet': $recycleQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_recycle customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['log.state', 'in', [2, 3, 7, 8, 9]], ['log.customer_employee_id', '<>', 0] ]) ->group('log.customer_id') ->fetchSql(true) ->column('log.customer_id as id, min(log.confirm_date) as first_meet_time'); $customerIdList = Db::table('(' . $recycleQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->column('id'); if (empty($customerIdList)) { $data = []; break; } $lastLogId = CustomerVisitLog::where([['customer_id', 'in', $customerIdList], ['customer_employee_id', '<>', 0]])->group('customer_id')->column('max(id)'); $data = CustomerVisitLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'customer_employee_id'); break; case 'deposit': $recycleQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_recycle customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['log.state', 'in', [2, 3]], ]) ->group('log.customer_id') ->field('log.customer_id as id, customer.source_id as source_id, min(log.confirm_date) as first_meet_time') ->BuildSql(); $data = Db::table($recycleQuery . ' a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('source_id')->column('count(id) as num', 'source_id'); break; case 'employee_deposit': $recycleQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_recycle customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['log.state', 'in', [2, 3]], ['log.customer_employee_id', '<>', 0] ]) ->group('log.customer_id') ->fetchSql(true) ->column('log.customer_id as id, min(log.confirm_date) as first_meet_time'); $customerIdList = Db::table('(' . $recycleQuery . ') a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->column('id'); if (empty($customerIdList)) { $data = []; break; } $lastLogId = CustomerVisitLog::where([['customer_id', 'in', $customerIdList], ['customer_employee_id', '<>', 0]])->group('customer_id')->column('max(id)'); $data = CustomerVisitLog::where([['id', 'in', $lastLogId]])->group('employee_id')->column('count(id) as num', 'customer_employee_id'); break; } return $data; } /** * customer表查询 */ private function customerDropPool($type) { $data = []; switch ($type) { case 'clue': // 客户表线索统计 $data = CustomerDropPool::where([ ['org_id', 'in', $this->searchOrg], ['sign_time', '>=', $this->param['start']], ['sign_time', '<', $this->param['end']], ['source_id', 'NOTNULL', ''], ['source_id', '<>', 0] ])->group("source_id")->column('count(id) as num', "source_id"); break; case 'employee_clue': $data = CustomerDropPool::where([ ['org_id', 'in', $this->saleOrg], ['employee_id', 'NOTNULL', ''], ['sign_time', '>=', $this->param['start']], ['sign_time', '<', $this->param['end']] ])->group("employee_id")->column('count(id) as num', "employee_id"); break; case 'employee_to_be_confirmed': $data = CustomerDropPool::where([ ['org_id', 'in', $this->saleOrg], ['employee_id', 'NOTNULL', ''], ['sign_time', '>=', $this->param['start']], ['sign_time', '<', $this->param['end']], ['state', '=', 0] ])->group('employee_id')->column('count(id) as num', 'employee_id'); case 'valid': $data = CustomerDropPool::where([ ['org_id', 'in', $this->searchOrg], ['valid_time', '>=', $this->param['start']], ['valid_time', '<', $this->param['end']], ['source_id', 'NOTNULL', ''], ['source_id', '<>', 0] ])->group("source_id")->column('count(id) as num', "source_id"); break; case 'employee_valid': $data = CustomerDropPool::where([ ['org_id', 'in', $this->saleOrg], ['valid_time', '>=', $this->param['start']], ['valid_time', '<', $this->param['end']], ['employee_id', 'NOTNULL', ''] ])->group("employee_id")->column('count(id) as num', "employee_id"); break; case 'meet': $poolQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['log.state', 'in', [2, 3, 7, 8, 9]], ]) ->group('log.customer_id') ->field('log.customer_id as id, customer.source_id as source_id, min(log.confirm_date) as first_meet_time') ->BuildSql(); $data = Db::table($poolQuery . ' a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('source_id')->column('count(id) as num', 'source_id'); break; case 'employee_meet': $poolQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['log.state', 'in', [2, 3, 7, 8, 9]], ]) ->group('log.customer_id') ->field('log.customer_id as id, customer.employee_id as employee_id, min(log.confirm_date) as first_meet_time') ->BuildSql(); $data = Db::table($poolQuery . ' a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); break; case 'designer_meet': $poolQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['log.state', 'in', [2, 3, 7, 8, 9]], ['customer.designer_id', 'NOTNULL', ''], ['customer.designer_id', 'in', $this->designerList] ]) ->group('log.customer_id') ->field('log.customer_id as id, customer.designer_id as employee_id, min(log.confirm_date) as first_meet_time') ->BuildSql(); $data = Db::table($poolQuery . ' a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); break; case 'deposit': $poolQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['log.state', 'in', [2, 3]], ]) ->group('log.customer_id') ->field('log.customer_id as id, customer.source_id as source_id, min(log.confirm_date) as first_meet_time') ->BuildSql(); $data = Db::table($poolQuery . ' a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('source_id')->column('count(id) as num', 'source_id'); break; case 'employee_deposit': $poolQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->saleOrg], ['log.state', 'in', [2, 3]], ]) ->group('log.customer_id') ->field('log.customer_id as id, customer.employee_id as employee_id, min(log.confirm_date) as first_meet_time') ->BuildSql(); $data = Db::table($poolQuery . ' a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); break; case 'designer_deposit': $poolQuery = Db::name('customer_visit_log') ->alias('log') ->join('customer_drop_pool customer', 'log.customer_id = customer.customer_id') ->where([ ['customer.org_id', 'in', $this->searchOrg], ['log.state', 'in', [2, 3]], ['customer.designer_id', 'NOTNULL', ''], ['customer.designer_id', 'in', $this->designerList] ]) ->group('log.customer_id') ->field('log.customer_id as id, customer.designer_id as employee_id, min(log.confirm_date) as first_meet_time') ->BuildSql(); $data = Db::table($poolQuery . ' a') ->where([ ['first_meet_time', '>=', $this->param['start']], ['first_meet_time', '<', $this->param['end']] ])->group('employee_id')->column('count(id) as num', 'employee_id'); break; } return $data; } /** * 数据合并 */ private function merge(&$data, $mergeData) { foreach ($mergeData as $k => $num) { if (isset($data[$k])) { $data[$k] += $num; } else { $data[$k] = $num; } } } /** * 数据处理 */ private function dealData(&$data, $mergeData, $kname, $searchK = 'id') { foreach ($data as &$d) { if (is_array($kname)) { foreach ($kname as $name) { if (isset($mergeData[$d[$searchK]])) { $d[$name] = floatval($mergeData[$d[$searchK]][$name]); } else { $d[$name] = 0; } } } else { if (isset($mergeData[$d[$searchK]])) { $d[$kname] = floatval($mergeData[$d[$searchK]]); } else { $d[$kname] = 0; } } } } /** * 除法 */ private function division($divisor, $dividend, $lv = true) { if (is_string($divisor)) $divisor = floatval($divisor); if (is_string($dividend)) $dividend = floatval($dividend); if ($dividend == 0) { if ($divisor == 0) return 0; return $lv ? $divisor : 0; } $lv ? $w = 4 : $w = 2; return floatval(round($divisor / $dividend, $w)); } /** * 数据计算 */ private function dealRowData(&$data, $suan, $totalRow) { $excludeKey = array_keys($totalRow); foreach ($data as $row) { foreach ($row as $k => $col) { if (in_array($k, $excludeKey)) continue; if ($k == 'id') continue; if (!isset($totalRow[$k])) $totalRow[$k] = 0; $totalRow[$k] += $col; } } $data[] = $totalRow; foreach ($data as $k => $item) { foreach ($suan as $column => $s) { list($divisor, $dividend) = explode('/', $s['division']); isset($s['lv']) ? $lv = $s['lv'] : $lv = true; $rs = $this->division($item[$divisor], $item[$dividend], $lv); $s['*'] == 0 ?: $rs = $rs * $s['*']; $s['islv'] != 1 ?: $rs = $rs . '%'; $data[$k][$column] = $rs; } } } /** * 部门获取 */ private function org() { $data = Org::where([['id', 'in', $this->searchOrg], ['pid', '<>', 0]])->field('id,name,pid')->order('pid,id')->select(); return $data; } }