request = request();
$this->root_id = $this->request->employee->root_id;
$xinjushang = 0;
View::assign('xinjushang', $xinjushang);
}
/**
* 数据获取接口
*
* @param string $metod
* @return void
*/
public function __call($method, $arguments)
{
if (!$this->request->isAjax()) {
if (Str::snake($method) == 'customer' || Str::snake($method) == 'clue') {
//返回组织列表
$org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
View::assign('org', $org);
}
$aes = new Aes('zqxg@screen');
$rootAes = $aes->encrypt($this->root_id);
view::assign('rootId', $rootAes);
return View::fetch(Str::snake($method) . '_data');
}
$method .= 'Data';
if (method_exists($this, $method)) {
if ($method == 'followUpData') {
$rs = $this->followUpData($arguments);
} elseif ($method == 'customerData') {
$rs = $this->customerData($arguments);
} elseif ($method == 'clueData') {
$rs = $this->clueData($arguments);
} elseif ($method == 'editInspectorData') {
$rs = $this->editInspectorData($arguments);
} elseif ($method == 'inspectorsData') {
$rs = $this->inspectorsData($arguments);
} elseif ($method == 'addInspectorData') {
$rs = $this->addInspectorData($arguments);
} elseif ($method == 'houseData') {
$rs = $this->houseData($arguments);
} elseif ($method == 'sourceData') {
$rs = $this->sourceData($arguments);
}
// $rs = $this->$method($arguments);
return json($rs);
}
return json(['code' => 0, 'data' => [], 'count' => 0]);
}
/**
* 客户数据统计
*
* @return string
*/
private function customerData($param)
{
$startCondition = ['addtime', '>', date('Y-m-d H:i:s', 0)];
$endCondition = ['addtime', '<', date('Y-m-d H:i:s')];
$beforeCondition = '';
//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
if (!empty($param['start_date'])) {
$count_dates = explode(' - ', $param['start_date']);
$param['start_date'] = date('Y-m-d', strtotime($count_dates[0]));
$param['end_date'] = date('Y-m-d', strtotime($count_dates[1]));
$startCondition = ['addtime', '>', date('Y-m-d H:i:s', strtotime($param['start_date']))];
$beforeCondition = date('Y-m-d H:i:s', strtotime($param['start_date']));
}
if (!empty($param['end_date'])) {
$endCondition = ['addtime', '<', date('Y-m-d', strtotime($param['end_date'])) . ' 23:59:59'];
}
$param['page'] = !empty($param['page']) ? $param['page'] : 1;
$param['limit'] = !empty($param['limit']) ? $param['limit'] : 10;
// 占位数据
$initdata = ', 0 as customer_num, 0 as unconfirm_num, 0 as deposit_num, 0 as signed_num, 0 as deposit_total_money, 0 as signed_total_money, 0 as shop_num,
0 as measure_num, 0 as clue_num, 0 as effective_num, 0 as meet_num, "0%" as meet_lv, "0%" as deposit_lv, "0%" as signed_lv, 0 as average_val, "0%" as effective_lv, 0 as maika_num';
/** 不同数据内容整理 */
if (empty($param['type']) || $param['type'] == 1) {
$group = 'org_id';
// 部门名称列表获取(只获取没有子部门的部门)
if (!empty($param['select_org_id'])) {
$select_org_path = Org::where('id', $param['select_org_id'])->value('path');
$condition = [['path', 'like', $select_org_path . '%']];
} else {
$condition = [['path', 'like', $this->root_id . '-%']];
}
$org_where = [];
$org_data = Org::where($condition)->select()->toArray();
foreach ($org_data as $item) {
$search = 'false';
foreach ($org_data as $val) {
if ($search == 'false') {
if ($val['pid'] == $item['id']) {
$search = 'true';
}
}
}
if ($search == 'false') $org_where[] = $item['id'];
}
$data = Org::where([['path', 'like', $this->root_id . '-%'], ['id', 'in', $org_where]])->page($param['page'], $param['limit'])->column('id,name as org_name, 0 as employee_num' . $initdata);
// 部门id
$orgIdList = array_column($data, 'id');
// 查询条件
$typeCondition = ['org_id', 'in', $orgIdList];
// 量房,到店,见面数(量房+到店)
$customerVisitData = $this->getCustomerVisitNum('org', $orgIdList, $startCondition, $endCondition, $beforeCondition, $typeCondition);
$count = Org::where([['path', 'like', $this->root_id . '-%'], ['id', 'in', $org_where]])->count();
} else {
$group = 'employee_id';
$orgCondition = [];
if (!empty($param['org_id'])) $orgCondition = ['org_id', '=', $param['org_id']];
// 部门人员获取
$data = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], $orgCondition])->page($param['page'], $param['limit'])->column('id, org_id, name' . $initdata);
// 员工id
$employeeIdList = array_column($data, 'id');
// 查询条件
$typeCondition = ['employee_id', 'in', $employeeIdList];
// 量房,到店,见面数(量房+到店)
$customerVisitData = $this->getCustomerVisitNum('employee', $employeeIdList, $startCondition, $endCondition, $beforeCondition, $typeCondition);
// 员工部门获取
$orgs = Org::where([['path', 'like', $this->root_id . '-%']])->column('name', 'id');
$count = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], $orgCondition])->count();
$typeCondition = ['org_id', '=', $param['org_id']];
}
// 线索获取-转移客户没有转移线索导致数据对不上
$sql = CustomerClue::where([$startCondition, $endCondition, $typeCondition, ['employee_id', 'in', $customerVisitData['eid']]])->group($group)->field("count(id) as clue_num,count(if(state=1, '1', null)) as effective_num, org_id, $group")->buildSql();
$clueData = Db::query($sql);
$clueData = array_combine(array_column($clueData, $group), $clueData);
foreach ($data as &$item) {
// 员工数据,添加部门信息
if (!empty($param['type']) && $param['type'] != 1) {
if (isset($orgs[$item['org_id']])) {
$item['org_name'] = $orgs[$item['org_id']];
} else {
$item['org_name'] = '';
}
}
if (isset($clueData[$item['id']])) {
$item['clue_num'] = $clueData[$item['id']]['clue_num'];
$item['effective_num'] = $clueData[$item['id']]['effective_num'];
if (isset($customerVisitData['data'][$item['id']])) {
$item['effective_num'] += $customerVisitData['data'][$item['id']]['effective_total_number'];
}
}
if (isset($customerVisitData['data'][$item['id']])) {
$item['deposit_total_money'] = $customerVisitData['data'][$item['id']]['deposit_total_money'];
$item['signed_total_money'] = $customerVisitData['data'][$item['id']]['signed_total_money'];
$item['signed_num'] = $customerVisitData['data'][$item['id']]['signed_num'];
$item['deposit_num'] = $customerVisitData['data'][$item['id']]['deposit_num'];
$item['unconfirm_num'] = $customerVisitData['data'][$item['id']]['unconfirm_num'];
$item['shop_num'] = $customerVisitData['data'][$item['id']]['shop_num'];
$item['measure_num'] = $customerVisitData['data'][$item['id']]['measure_num'];
$item['meet_num'] = $customerVisitData['data'][$item['id']]['meet_num'];
$item['maika_num'] = $customerVisitData['data'][$item['id']]['maika_num'];
if (empty($param['type']) || $param['type'] == 1) {
$item['employee_num'] = $customerVisitData['data'][$item['id']]['employee_num'];
}
$item['customer_num'] = $customerVisitData['data'][$item['id']]['customer_num'];
}
// 计算数据
if ($item['signed_num'] != 0) $item['average_val'] = floor($item['signed_total_money'] / $item['signed_num']);
if ($item['customer_num'] !== 0) $item['meet_lv'] = $item['customer_num'] ? round($item['meet_num'] / $item['customer_num'] * 100, 2) . '%' : '0%';
if ($item['meet_num'] !== 0) $item['deposit_lv'] = round($item['deposit_num'] / $item['meet_num'] * 100, 2) . '%';
if ($item['meet_num'] !== 0) $item['signed_lv'] = round($item['signed_num'] / $item['meet_num'] * 100, 2) . '%';
if ($item['customer_num'] != 0) $item['effective_lv'] = round($item['effective_num'] / $item['customer_num'] * 100, 2) . '%';
}
return ['code' => 0, 'data' => $data, 'count' => $count];
}
/**
* 量房到店数获取(用户客户数据统计获取优化)
*/
public function getCustomerVisitNum($type, $ids, $startCondition, $endCondition, $beforeCondition, $typeCondition)
{
$orgId = $ids;
if ($type == 'org') {
// 部门人员获取
$employees = Employee::where([['org_id', 'in', $ids], ['state', '=', '在职'], ['uid', '<>', 0]])->column('org_id', 'id');
// 员工id
$ids = array_keys($employees);
}
$state2 = CustomerVisitLog::changeState('待确认', 'chaos');
$state3 = CustomerVisitLog::changeState('已到访', 'chaos');
$state4 = CustomerVisitLog::changeState('已交定', 'chaos');
$state5 = CustomerVisitLog::changeState('已量房', 'chaos');
$state8 = CustomerVisitLog::changeState('已到场', 'chaos');
$state6 = CustomerVisitLog::changeState('已签单', 'chaos');
$state15 = CustomerVisitLog::changeState('已卖卡', 'chaos');
//查询时间之前的见面数量
$beforeCustomersId = [];
if ($beforeCondition) {
$vis_where = [['state', 'in', array_merge($state3, $state5, $state8)], ['addtime', '<', $beforeCondition]];
$beforeCustomers = Customer::with([
'visitLog' => function ($query) use ($vis_where) {
$query->where($vis_where)->field('customer_id,state');
}
])->where(function ($query) {
$not_sure = Customer::changeState('待确认', 'chaos');
$or1[] = ['crm_res_id', 'null', null];
$or2[] = ['crm_res_id', 'not null', null];
$or2[] = ['state', 'not in', $not_sure];
$query->whereOr([$or1, $or2]);
})->where([$typeCondition])->field('id,employee_id,state')->select()->toArray();
foreach ($beforeCustomers as $v) {
foreach ($v['visitLog'] as $va) {
if (in_array($va['state'], array_merge($state3, $state5, $state8))) {
$beforeCustomersId[] = $v['id'];
}
}
}
}
$vis_where = [$startCondition, $endCondition, ['state', 'in', array_merge($state2, $state3, $state4, $state5, $state6, $state8, $state15)]];
$customers = Customer::with(['visitLog' => function ($query) use ($vis_where) {
$query->where($vis_where)->field('customer_id,addtime,state,money')->order('addtime desc');
}])->where(function ($query) {
$not_sure = Customer::changeState('待确认', 'chaos');
$or1[] = ['crm_res_id', 'null', null];
$or2[] = ['crm_res_id', 'not null', null];
$or2[] = ['state', 'not in', $not_sure];
$query->whereOr([$or1, $or2]);
})->where([$typeCondition])->field('id,employee_id,state')->select();
// 无效客户数
$no_where = [
['employee_id', 'in', $ids],
$startCondition,
$endCondition
];
$no_valid_customer = CustomerInvalidLog::where($no_where)->column('customer_id,org_id,employee_id');
$customerVisitData = [];
$checkDaraIds = [];
$measureMumIds = []; //量房数
$shopNumIds = []; //到店数
$meetNumIds = []; //见面数
$depositNumIds = []; //订金数-签单数
$signedNumIds = []; //签单数-转单数
$unconfirmNumIds = []; //待确认
$maikaNumIds = []; //卖卡
foreach ($customers as $c) {
if (!isset($customerVisitData[$c['employee_id']])) {
$customerVisitData[$c['employee_id']] = ['maika_num' => 0, 'effective_total_number' => 0, 'signed_total_money' => 0, 'deposit_total_money' => 0, 'signed_num' => 0, 'deposit_num' => 0, 'unconfirm_num' => 0, 'shop_num' => 0, 'measure_num' => 0, 'meet_num' => 0, 'customer_num' => 0, 'customer' => []];
}
$state = [];
$signed_total_money = [];
$deposit_total_money = [];
foreach ($c['visitLog'] as $v) {
$state[$v['state']][] = $v;
}
foreach ($state as $k => $v) {
if (in_array($k, $state15) && !in_array($c['id'], $maikaNumIds)) {
$customerVisitData[$c['employee_id']]['maika_num']++;
$measureMumIds[] = $c['id'];
}
if (in_array($k, $state6) && !in_array($c['id'], $signedNumIds)) {
$customerVisitData[$c['employee_id']]['signed_num']++;
$signedNumIds[] = $c['id'];
$signed_total_money[] = $v[0]['money'];
}
if (in_array($k, $state4) && !in_array($c['id'], $depositNumIds)) {
$customerVisitData[$c['employee_id']]['deposit_num']++;
$depositNumIds[] = $c['id'];
$deposit_total_money[] = $v[0]['money'];
}
if (in_array($k, $state2) && !in_array($c['id'], $unconfirmNumIds)) {
$customerVisitData[$c['employee_id']]['unconfirm_num']++;
$unconfirmNumIds[] = $c['id'];
}
if (in_array($k, $state3) && !in_array($c['id'], $shopNumIds)) {
$customerVisitData[$c['employee_id']]['shop_num']++;
$shopNumIds[] = $c['id'];
}
if (in_array($k, $state5) && !in_array($c['id'], $measureMumIds)) {
$customerVisitData[$c['employee_id']]['measure_num']++;
$measureMumIds[] = $c['id'];
}
if (!in_array($c['id'], $beforeCustomersId) && in_array($k, array_merge($state3, $state4, $state5, $state6, $state8)) && !in_array($c['id'], $meetNumIds)) {
$customerVisitData[$c['employee_id']]['meet_num']++;
$meetNumIds[] = $c['id'];
}
if (!in_array($c['id'], $checkDaraIds) && in_array($k, array_merge($state3, $state4, $state5, $state6, $state8, $state15))) {
$customerVisitData[$c['employee_id']]['effective_total_number']++;
$checkDaraIds[] = $c['id'];
}
}
$customerVisitData[$c['employee_id']]['signed_total_money'] += array_sum($signed_total_money);
$customerVisitData[$c['employee_id']]['deposit_total_money'] += array_sum($deposit_total_money);
$customerVisitData[$c['employee_id']]['customer'][] = $c['id'];
}
$no_employee = [];
foreach ($no_valid_customer as $k => $v) {
$no_employee[$v['employee_id']][] = $v['customer_id'];
}
// 赋值无效客户
foreach ($customerVisitData as $k => $v) {
if (isset($no_employee[$k])) {
$customer_ids = array_unique(array_merge($v['customer'], $no_employee[$k]));
$customerVisitData[$k]['customer_num'] = count($customer_ids);
} else {
$customerVisitData[$k]['customer_num'] = count($customerVisitData[$k]['customer']);
}
}
if ($type == 'org') {
$visitData = [];
foreach ($employees as $empId => $org_id) {
if (!isset($visitData[$org_id])) $visitData[$org_id] = ['maika_num' => 0, 'effective_total_number' => 0, 'signed_total_money' => 0, 'deposit_total_money' => 0, 'signed_num' => 0, 'deposit_num' => 0, 'unconfirm_num' => 0, 'shop_num' => 0, 'measure_num' => 0, 'employee_num' => 0, 'meet_num' => 0, 'customer_num' => 0];
if (isset($customerVisitData[$empId])) {
$visitData[$org_id]['maika_num'] += $customerVisitData[$empId]['maika_num'];
$visitData[$org_id]['effective_total_number'] += $customerVisitData[$empId]['effective_total_number'];
$visitData[$org_id]['signed_total_money'] += $customerVisitData[$empId]['signed_total_money'];
$visitData[$org_id]['deposit_total_money'] += $customerVisitData[$empId]['deposit_total_money'];
$visitData[$org_id]['signed_num'] += $customerVisitData[$empId]['signed_num'];
$visitData[$org_id]['deposit_num'] += $customerVisitData[$empId]['deposit_num'];
$visitData[$org_id]['unconfirm_num'] += $customerVisitData[$empId]['unconfirm_num'];
$visitData[$org_id]['shop_num'] += $customerVisitData[$empId]['shop_num'];
$visitData[$org_id]['measure_num'] += $customerVisitData[$empId]['measure_num'];
$visitData[$org_id]['meet_num'] += $customerVisitData[$empId]['meet_num'];
$visitData[$org_id]['customer_num'] += $customerVisitData[$empId]['customer_num'];
}
$visitData[$org_id]['employee_num']++;
}
return ['data' => $visitData, 'eid' => $ids];
}
return ['data' => $customerVisitData, 'eid' => $ids];
}
/**
* 跟进情况统计
*
* @return string
*/
private function followUpData($param)
{
$startCondition = ['addtime', '>', date('Y-m-d H:i:s', 0)];
$endCondition = ['addtime', '<', date('Y-m-d H:i:s')];
if (!empty($param['start_date'])) {
$startCondition = ['addtime', '>', date('Y-m-d H:i:s', strtotime($param['start_date']))];
}
if (!empty($param['end_date'])) {
$endCondition = ['addtime', '<', date('Y-m-d', strtotime($param['end_date'])) . ' 23:59:59'];
}
// 客户等级获取(该项在系统中被写死,如需改变需调查后调整)
if (!empty($param['level'])) {
$level = [$param['level']];
$levelCondition = ['level', '=', $param['level']];
} else {
$level = ['A', 'B', 'C'];
$levelCondition = ['level', 'in', $level];
}
$param['page'] = !empty($param['page']) ? $param['page'] : 1;
$param['limit'] = !empty($param['limit']) ? $param['limit'] : 10;
/** 不同数据内容整理 */
if (empty($param['type']) || $param['type'] == 1) {
$group = 'org_id';
// 部门名称列表获取
$data = Org::where([['path', 'like', $this->root_id . '-%']])->page($param['page'], $param['limit'])->column('id,name as org_name');
// 部门id
$orgIdList = array_column($data, 'id');
// 员工查找
$employees = Employee::where([['org_id', 'in', $orgIdList], ['state', '=', '在职'], ['uid', '<>', 0]])->column('org_id', 'id');
// 员工id
$employeeIdList = array_keys($employees);
// 查询条件
$typeCondition = ['org_id', 'in', $orgIdList];
$count = Org::where([['path', 'like', $this->root_id . '-%']])->count();
} else {
$group = 'employee_id';
$orgCondition = [];
if (!empty($param['org_id'])) $orgCondition = ['org_id', '=', $param['org_id']];
// 部门人员获取
$data = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], $orgCondition])->page($param['page'], $param['limit'])->column('id, org_id, name');
// 员工id
$employeeIdList = array_column($data, 'id');
// 查询条件
$typeCondition = ['employee_id', 'in', $employeeIdList];
// 员工部门获取
$orgs = Org::where([['path', 'like', $this->root_id . '-%']])->column('name', 'id');
foreach ($data as &$c) {
$c['org_name'] = $orgs[$c['org_id']];
}
$count = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], $orgCondition])->count();
}
// 客户数据获取
$customerData = Customer::where([$typeCondition, $startCondition, $endCondition, $levelCondition, ['state', '<>', '无效']])
->column("
id,
$group,
level,
state,
deposit_money,
signed_money
");
// 查询访问记录
$customerVisitData = CustomerVisitLog::where([['employee_id', 'in', $employeeIdList], $startCondition, $endCondition])
->group('customer_id')->column("
count(if(state in ('" . implode('\',\'', CustomerVisitLog::changeState('已到店', 'chaos')) . "'), '1', null)) as shop_num,
count(if(state in ('" . implode('\',\'', CustomerVisitLog::changeState('已量房', 'chaos')) . "'), '1', null)) as measure_num
", 'customer_id');
// 数据整理 结构【分组名ID】【客户类型】{num,confirm_num,shop_num,measure_num,deposit_num,signed_num}
$clearData = [];
foreach ($customerData as $customer) {
if (!isset($clearData[$customer[$group]][$customer['level']]))
$clearData[$customer[$group]][$customer['level']] = [
'level' => $customer['level'],
'num' => 0,
'confirm_num' => 0,
'shop_num' => 0,
'measure_num' => 0,
'deposit_num' => 0,
'signed_num' => 0
];
$clearData[$customer[$group]][$customer['level']]['num'] += 1;
if (in_array($customer['state'], Customer::changeState('待确认', 'chaos')))
$clearData[$customer[$group]][$customer['level']]['confirm_num'] += 1;
if (isset($customerVisitData[$customer['id']])) {
($customerVisitData[$customer['id']]['shop_num'] > 0 || in_array($customer['state'], Customer::changeState('确认到店', 'chaos'))) ?? $clearData[$customer[$group]][$customer['level']]['shop_num'] += 1;
($customerVisitData[$customer['id']]['measure_num'] > 0 || in_array($customer['state'], Customer::changeState('确认量房', 'chaos'))) ?? $clearData[$customer[$group]][$customer['level']]['measure_num'] += 1;
}
if ($customer['deposit_money'] > 0 || in_array($customer['state'], Customer::changeState('交定', 'chaos')))
$clearData[$customer[$group]][$customer['level']]['deposit_num'] += 1;
if ($customer['signed_money'] > 0 || in_array($customer['state'], Customer::changeState('签单', 'chaos')))
$clearData[$customer[$group]][$customer['level']]['signed_num'] += 1;
}
// 数据二次处理
$dealData = [];
foreach ($data as &$i) {
if (isset($i['org_name'])) $i['org_name'] = '' . $i['org_name'] . '';
foreach ($level as $n => $l) {
if (isset($clearData[$i['id']]) && isset($clearData[$i['id']][$l])) {
$item = $clearData[$i['id']][$l];
$item['unshop_num'] = $item['num'] - $item['shop_num'];
} else {
$item = [
'level' => $l,
'num' => 0,
'confirm_num' => 0,
'shop_num' => 0,
'measure_num' => 0,
'deposit_num' => 0,
'signed_num' => 0,
'unshop_num' => 0
];
}
$dealData[] = array_merge($i, $item);
}
}
return ['code' => 0, 'data' => $dealData, 'count' => $count];
}
/**
* 房屋信息统计
*
* @return string
*/
private function houseData()
{
return ['code' => 0, 'data' => [], 'count' => 0];
}
/**
* 客户来源渠道统计
*
* @return string
*/
private function sourceData()
{
return ['code' => 0, 'data' => [], 'count' => 0];
}
/**
* 线索统计
*
* @return string
*/
public function clueData($param)
{
$startCondition = ['addtime', '>', date('Y-m-d H:i:s', 0)];
$endCondition = ['addtime', '<', date('Y-m-d H:i:s')];
//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
if (!empty($param['start_date'])) {
$count_dates = explode(' - ', $param['start_date']);
$param['start_date'] = date('Y-m-d', strtotime($count_dates[0]));
$param['end_date'] = date('Y-m-d', strtotime($count_dates[1]));
$startCondition = ['addtime', '>', date('Y-m-d H:i:s', strtotime($param['start_date']))];
}
if (!empty($param['end_date'])) {
$endCondition = ['addtime', '<', date('Y-m-d', strtotime($param['end_date'])) . ' 23:59:59'];
}
$param['page'] = !empty($param['page']) ? $param['page'] : 1;
$param['limit'] = !empty($param['limit']) ? $param['limit'] : 10;
/** 不同数据内容整理 */
if (empty($param['type']) || $param['type'] == 1) {
// 部门名称列表获取
$org_where = [['path', 'like', $this->root_id . '-%']];
if (!empty($param['org_id'])) {
$org = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
unset($org_where);
$org_where[] = ['path', 'like', $org['path'] . '%'];
}
$data = Org::where($org_where)->page($param['page'], $param['limit'])->column('id,name as org_name');
$count = Org::where($org_where)->count();
foreach ($data as &$i) {
$employee_id = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], ['org_id', '=', $i['id']]])->column('id');
$clueData = [];
if (isset($employee_id) && $employee_id) {
$typeConditionOrg = ['org_id', '=', $i['id']];
$typeConditionEmployee = ['employee_id', 'in', $employee_id];
$clueData = CustomerClue::where([$startCondition, $endCondition, $typeConditionOrg, $typeConditionEmployee])->field("count(*) as num,count(if(state=1,1,null)) as valid_num,count(if(state=2,1,null)) as invalid_num")->find();
}
$i['org_name'] = '' . $i['org_name'] . '';
if (isset($clueData) && $clueData) {
$i['num'] = $clueData['num'];
$i['valid_num'] = $clueData['valid_num'];
$i['invalid_num'] = $clueData['invalid_num'];
} else {
$i['num'] = 0;
$i['valid_num'] = 0;
$i['invalid_num'] = 0;
}
$i['valid_lv'] = $i['num'] == 0 ? '0%' : round($i['valid_num'] / $i['num'], 2) * 100 . '%';
}
} else {
// 部门人员获取
$data = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], ['org_id', '=', $param['org_id']]])->page($param['page'], $param['limit'])->column('id, org_id, name');
// 员工id
$employeeIdList = array_column($data, 'id');
$orgs = Org::where([['path', 'like', $this->root_id . '-%']])->column('name', 'id');
foreach ($data as &$c) {
$c['org_name'] = $orgs[$c['org_id']];
}
$count = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], ['org_id', '=', $param['org_id']]])->count();
//查询部门下的员工保证数据里外一致
$typeConditionOrg = ['org_id', 'in', $param['org_id']];
$typeConditionEmployee = ['employee_id', 'in', $employeeIdList];
$clueData = CustomerClue::where([$startCondition, $endCondition, $typeConditionOrg, $typeConditionEmployee])->field("employee_id,count(id) as num,count(if(state=1,1,null)) as valid_num,count(if(state=2,1,null)) as invalid_num")->group('employee_id')->select()->toArray();
$clueData = array_combine(array_column($clueData, 'employee_id'), $clueData);
// 数据处理
foreach ($data as &$i) {
if (isset($clueData[$i['id']])) {
$i['num'] = $clueData[$i['id']]['num'];
$i['valid_num'] = $clueData[$i['id']]['valid_num'];
$i['invalid_num'] = $clueData[$i['id']]['invalid_num'];
} else {
$i['num'] = 0;
$i['valid_num'] = 0;
$i['invalid_num'] = 0;
}
$i['valid_lv'] = $i['num'] == 0 ? '0%' : round($i['valid_num'] / $i['num'], 2) * 100 . '%';
}
}
return ['code' => 0, 'data' => $data, 'count' => $count];
}
//客户扩展字段转换
public function emp_extfield_change($customerList)
{
$chmod = new CustomerLogic;
$list = CustomerPortraitField::with(['select'])->where([['root_id', '=', $this->root_id], ['pid', '<>', 0]])->select()->toArray();
foreach ($customerList as $key => $val) {
$new = '';
if (!empty($val['ext']) && $val['ext'] != 'null') {
//$extdata=get_object_vars($val['ext']);
$extdata = json_decode($val['ext'], true);
if (isset($extdata['ext1'])) {
$new = $chmod->old_data_saves($val, $list);
} else {
$new = $chmod->new_data_saves($val, $extdata, $list);
}
} else {
$new = $chmod->no_ext_saves($val, $list);
}
$new = array_combine(array_column($new, 'keyname'), array_column($new, 'valname'));
//$customerList[$key]['newext']=$new;
$customerList[$key]['ext'] = $new;
}
return $customerList;
}
/**
* 客户跟进数据统计
*/
public function customer_statistics($page, $limit, $getTotal = false)
{
$param = $this->request->only(['sdate' => '', 'edate' => '', 'org_id' => 0, 'add_wechat_time']);
// 获取部门id
$root_id = request()->employee->root_id;
$orgs = Org::where([['path', 'like', $root_id . '-%']])->column('name,id,pid,level,path', 'id');
// 部门领导获取
$leaders = Employee::with('org')->where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['uid', '<>', 0]])->select();
// 领导结构修改
$leadersData = [];
foreach ($leaders as $l) {
if (isset($leadersData[$l['org_id']])) {
$leadersData[$l['org_id']]['leaders'][] = $l['name'];
$leadersData[$l['org_id']]['leaders_ids'][] = $l['id'];
} else {
$leadersData[$l['org_id']] = [
'org_name' => $l['org']['name'],
'level' => $l['org']['level'],
'path' => $l['org']['path'],
'leaders_ids' => [
$l['id']
],
'leaders' => [
$l['name']
]
];
}
}
// 补充没领导的部门
foreach ($orgs as $k => $item) {
if (!isset($leadersData[$k])) {
$leadersData[$k] = [
'org_name' => $item['name'],
'level' => $item['level'],
'path' => $item['path'],
'leaders' => [],
'leaders_ids' => []
];
}
}
// 查询最近访问的客户
$where = [];
if ($param['org_id']) {
//查询本部门下面是否有部门
$childorg = orgSubIds($param['org_id']);
$where[] = ['org_id|customer_org_id', 'in', $childorg];
// $where[] = ['org_id', '=', $param['org_id']];
} else {
$where[] = ['org_id', 'in', array_keys($orgs)];
}
if ($param['sdate']) {
$param['sdate'] = date('Y-m-d', strtotime($param['sdate']));
$where[] = ['addtime', '>=', $param['sdate'] . ' 00:00:00'];
}
if ($param['edate']) {
$param['edate'] = date('Y-m-d', strtotime($param['edate']));
$where[] = ['addtime', '<=', $param['edate'] . ' 23:59:59'];
}
// 查询排除的部门
$sheji = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '设计部']])->value('id');
$xiaoshou = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '市场部']])->value('id');
$fanweinei = Org::whereOr([
[['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $sheji . '-%']],
[['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $xiaoshou . '-%']],
])->column('id');
$searchEmployeeIdList = Employee::where([['org_id', 'in', $fanweinei]])->column('id');
//如果有部门筛选把员工控制在本部门
if ($param['org_id']) $searchEmployeeIdList = Employee::where([['org_id', 'in', $childorg]])->column('id');
//$where[] = ['employee_id', 'in', $searchEmployeeIdList];
$where[] = ['customer_employee_id', 'in', $searchEmployeeIdList];
//2022-10-31辛居尚重11月1号开始正式使用
$where[] = ['addtime', '>', date('2022-10-31 23:59:59')];
if (!empty($param['add_wechat_time'])) {
$newtime = explode(' - ', $param['add_wechat_time']);
$sdate = strtotime($newtime[0]);
$edate = strtotime($newtime[1]);
$wechat_count = [];
$wechat_count = Customer::where([['ext', 'not null', ''], ['ext', '<>', ''], ['add_wechat_time', '>=', date('Y-m-d', $sdate)], ['add_wechat_time', '<=', date('Y-m-d', $edate)]])->column('id');
$cids = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
$wechat_count = array_intersect($cids, $wechat_count);
$where[] = ['customer_id', 'in', $wechat_count];
}
$total = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date")->where($where)->group('customer_id,date')->count();
$searchObject = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date, max(addtime) as addtime, SUM(number_of_visitors) as number_of_visitors, SUM(measure_room_img_type='量房图片') as measure_type1, SUM(measure_room_img_type='量房报告图片') as measure_type2, SUM(measure_room_img_type='客户需求表') as measure_type3,GROUP_CONCAT(aid) as aid_list")->where($where)->order('date desc,addtime desc')->group('customer_id,date');
if ($getTotal == false) $searchObject->page($page, $limit);
$lastVisitCustomerList = $searchObject->select();
// 获取客户信息
$customerIdList = array_unique(array_column(empty($lastVisitCustomerList) ? [] : $lastVisitCustomerList->toArray(), 'customer_id'));
$customerList = Customer::with(['employee', 'designer'])->where([['id', 'in', $customerIdList]])->field('id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList1 = CustomerRecycle::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList2 = CustomerDropPool::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList = array_merge($customerList->toArray(), $customerList1->toArray(), $customerList2->toArray());
$customerList = array_combine(array_column($customerList, 'id'), $customerList);
$customerList = $this->emp_extfield_change($customerList);
// 查询爆品产品
$packageId = implode(',', array_column($customerList, 'package_id'));
$package = CustomerPackage::where([['id', 'in', $packageId]])->column('total_price', 'id');
// 获取公司名称
$companyName = Company::where([['root_id', '=', $this->root_id]])->value('company_name');
// 公司负责人获取
// $companyLeader = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['org_id', '=', $this->root_id], ['uid', '<>', 0]])->column('name');
// 来源
$sourseIdList = array_column($customerList, 'source_id');
$sourseList = CustomerSource::where([['id', 'in', $sourseIdList]])->column('source', 'id');
$num = ($page - 1) * $limit + 1;
// 处理数据
$data = [];
foreach ($lastVisitCustomerList as $c) {
$item = [
'key' => $num,
'customer_id' => $c['customer_id'],
];
if (!isset($customerList[$c['customer_id']])) {
$data[] = $item;
$num++;
continue;
}
list($Y, $m, $d) = explode('-', $c['date']);
$leader_arr = ['941' => '罗镐', '1847' => '刘有凤']; //馨居尚,菡萏怡景,部门负责人
$item = [
'key' => $num,
'customer_name' => $customerList[$c['customer_id']]['name'],
'date' => $Y . '年' . $m . '月' . $d . '日',
'addtime' => $c['addtime'],
'company_name' => $companyName,
'leader' => !empty($leader_arr[$this->root_id]) ? $leader_arr[$this->root_id] : '罗镐',
'community_name' => $customerList[$c['customer_id']]['community_name'],
// 'measure_type1' => $c['measure_type1'] ? '√' : '',
// 'measure_type2' => $c['measure_type2'] ? '√' : '',
// 'measure_type3' => $c['measure_type3'] ? '√' : '',
'measure_room_store' => $this->measureRoomStore($c['date'], $c['customer_id']),
'number_of_visitors' => $c['number_of_visitors'] == 0 ? '' : $c['number_of_visitors'],
'area' => $c['area'],
//'ext39' => $customerList[$c['customer_id']]['ext']['房屋位置'],
'ext39' => $customerList[$c['customer_id']]['ext']['house_location'],
//'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['谈单时长']) ? $customerList[$c['customer_id']]['ext']['谈单时长'] : 0,
'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['talking_about_single_time']) ? $customerList[$c['customer_id']]['ext']['talking_about_single_time'] : 0,
'source' => isset($sourseList[$customerList[$c['customer_id']]['source_id']]) ? $sourseList[$customerList[$c['customer_id']]['source_id']] : '',
'square' => $customerList[$c['customer_id']]['square'],
//'ext42' => $customerList[$c['customer_id']]['ext']['门牌单元号']
'ext42' => $customerList[$c['customer_id']]['ext']['unit_number']
];
//量房图片类型
$where_img_type = [];
$where_img_type[] = ['addtime', 'like', '%' . $c['date'] . '%'];
$where_img_type[] = ['customer_id', '=', $c['customer_id']];
$where_img_type[] = CustomerVisitLog::changeState(['state', '=', '确认量房']);
$str = CustomerVisitLog::where($where_img_type)->column('measure_room_img_type');
$str = $str ? implode(',', $str) : '';
$item['measure_type2'] = ($str && (strpos($str, '量房图片') !== false || strpos($str, '量房报告图片') !== false || strpos($str, '量房验房报告') !== false)) ? '√' : '';
$item['measure_type3'] = ($str && strpos($str, '客户需求图片') !== false) ? '√' : '';
// 计算交房相差月份
$houseDeliveryTime = strtotime($customerList[$c['customer_id']]['house_delivery_time']);
$month = (date('Y', $houseDeliveryTime) - date('Y')) * 12 + date('m', $houseDeliveryTime) - date('m');
$type = '0';
// if ($customerList[$c['customer_id']]['ext']['加微类型'] == '常规加微') {
// $type = '1';
// } elseif ($customerList[$c['customer_id']]['ext']['加微类型'] == '社群加微') {
// $type = '2';
// }
$item['add_wechat_time'] = $customerList[$c['customer_id']]['ext']['add_wechat_time'];
if ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '常规加微') {
$type = '1';
} elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '社群加微') {
$type = '2';
} elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '历史加微') {
$type = '3';
}
if ($type !== '0') {
$item['house_type' . $type] = in_array($customerList[$c['customer_id']]['house_type'], ['现房', '二手房']) ? '√' : '';
if ($item['house_type' . $type] != '√' && !empty($customerList[$c['customer_id']]['house_delivery_time'])) {
$item['delivery_month' . $type . '_1'] = $month <= 3 ? '√' : '';
$item['delivery_month' . $type . '_2'] = $month > 3 && $month <= 6 ? '√' : '';
$item['delivery_month' . $type . '_3'] = $month > 6 && $month <= 12 ? '√' : '';
$item['delivery_month' . $type . '_4'] = $month > 12 ? '√' : '';
} else {
$item['delivery_month' . $type . '_1'] = '';
$item['delivery_month' . $type . '_2'] = '';
$item['delivery_month' . $type . '_3'] = '';
$item['delivery_month' . $type . '_4'] = '';
}
}
// 设计师
if (!empty($customerList[$c['customer_id']]['designer'])) {
$designerOrgId = $customerList[$c['customer_id']]['designer']['org_id'];
$item['designer_name'] = $customerList[$c['customer_id']]['designer']['name'];
$item['designer_org'] = $orgs[$designerOrgId]['name'];
$item['designer_leader'] = isset($leadersData[$designerOrgId]) ? implode(',', $leadersData[$designerOrgId]['leaders']) : '';
$path = $leadersData[$designerOrgId]['path'];
$pathList = array_filter(explode('-', $path));
foreach ($pathList as $od) {
if (strstr($leadersData[$od]['org_name'], '部') !== false && $leadersData[$od]['org_name'] != '设计部') {
$level = 3;
} else {
$level = $leadersData[$od]['level'];
}
if (isset($item['employee_org_level' . $level])) $level++;
$item['designer_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
$item['designer_org_level' . $level] = $leadersData[$od]['org_name'];
}
} else {
$item['designer_name'] = $item['designer_org'] = $item['designer_leader'] = '';
}
// 员工
if (!empty($customerList[$c['customer_id']]['employee'])) {
$employeeOrgId = $customerList[$c['customer_id']]['employee']['org_id'];
$item['employee_name'] = $customerList[$c['customer_id']]['employee']['name'];
$item['employee_org'] = $orgs[$employeeOrgId]['name'];
$item['employee_leader'] = implode(',', $leadersData[$employeeOrgId]['leaders']);
$path = $leadersData[$employeeOrgId]['path'];
$pathList = array_filter(explode('-', $path));
foreach ($pathList as $od) {
if (strstr($leadersData[$od]['org_name'], '区中区') !== false) {
$level = 3;
} elseif (strstr($leadersData[$od]['org_name'], '中心') !== false) {
$level = 4;
} elseif (strstr($leadersData[$od]['org_name'], '区') !== false) {
$level = 2;
} else {
$level = $leadersData[$od]['level'];
}
if (isset($item['employee_org_level' . $level])) $level++;
// $level = $leadersData[$od]['level'];
$item['employee_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
$item['employee_org_level' . $level] = $leadersData[$od]['org_name'];
}
// 查询直属领导是否跟踪过
// 获取直属领导id
$leaderIdList = $leadersData[$employeeOrgId]['leaders_ids'];
$query = [];
$query[] = ['employee_id', 'in', $leaderIdList];
$query[] = ['addtime', 'like', $c['date'] . '%'];
$query[] = ['customer_id', '=', $c['customer_id']];
$item['leader_visit'] = !CustomerVisitLog::where($query)->findOrEmpty()->isEmpty() && ($item['measure_type2'] == '√' || $item['measure_type3'] == '√') ? "√" : "";
} else {
$item['employee_name'] = $item['employee_org'] = $item['employee_leader'] = '';
$item['leader_visit'] = '';
}
// 量房验房
$item['measure_type1'] = ($item['leader_visit'] == '√' || $item['measure_type2'] == '√' || $item['measure_type3'] == '√') ? '√' : '';
// 手机号
$phoneArr = array_filter([$customerList[$c['customer_id']]['phone'], $customerList[$c['customer_id']]['phone1'], $customerList[$c['customer_id']]['phone2']]);
$item['phone'] = substr_replace(array_shift($phoneArr), '******', 3, 6);
// 建群情况
//$jianqun = explode(',', $customerList[$c['customer_id']]['ext']['建群情况']);
$jianqun = explode(',', $customerList[$c['customer_id']]['ext']['group_building']);
$item['ext35_1'] = in_array('1对1小群', $jianqun) || in_array('一对一小群', $jianqun) ? '√' : '';
$item['ext35_2'] = in_array('临时大群', $jianqun) ? '√' : '';
$item['ext35_3'] = in_array('社群', $jianqun) ? '√' : '';
// 直播
//$zhibo = explode(',', $customerList[$c['customer_id']]['ext']['直播情况']);
$zhibo = explode(',', $customerList[$c['customer_id']]['ext']['live_broadcast']);
$item['ext36_1'] = in_array('一对一直播', $zhibo) || in_array('1对1业务直播', $zhibo) ? '√' : '';
$item['ext36_2'] = in_array('1对1设计直播', $zhibo) ? '√' : '';
$item['ext36_3'] = in_array('一对多直播', $zhibo) ? '√' : '';
// $item['ext37_1'] = $item['ext36_1'] == '√' ? $customerList[$c['customer_id']]['ext']['1对1业务人员'] : '';
// $item['ext37_2'] = $item['ext36_2'] == '√' ? $customerList[$c['customer_id']]['ext']['1对1设计人员'] : '';
// $item['ext37_3'] = $item['ext36_3'] == '√' ? $customerList[$c['customer_id']]['ext']['直播人员'] : '';
$item['ext37_1'] = $item['ext36_1'] == '√' ? $customerList[$c['customer_id']]['ext']['live_broadcast_business'] : '';
$item['ext37_2'] = $item['ext36_2'] == '√' ? $customerList[$c['customer_id']]['ext']['live_broadcast_design'] : '';
$item['ext37_3'] = $item['ext36_3'] == '√' ? $customerList[$c['customer_id']]['ext']['live_broadcast_personnel'] : '';
// 签单金额
$condition = [];
$condition[] = ['addtime', 'like', $c['date'] . '%'];
$condition[] = ['customer_id', '=', $c['customer_id']];
$condition1 = $condition;
$condition2 = $condition;
$condition1[] = CustomerVisitLog::changeState(['state', '=', '已交定']);
$condition2[] = CustomerVisitLog::changeState(['state', '=', '已签单']);
$item['signature_money'] = CustomerVisitLog::where($condition2)->order('addtime desc')->value('money');
$deposit = CustomerVisitLog::where($condition1)->order('addtime desc')->field('money,deposit_mode')->find();
if (!empty($deposit)) {
$deposit['deposit_mode'] !== 0 ? $item['deposit_money1'] = $deposit['money'] : $item['deposit_money2'] = $deposit['money'];
// 爆品定金
if ($customerList[$c['customer_id']]['package_id']) {
$packageIdList = explode(',', $customerList[$c['customer_id']]['package_id']);
$totalMoney = 0;
foreach ($packageIdList as $pid) {
$totalMoney += $package[$pid];
}
if ($totalMoney !== 0) {
$item['package'] = $deposit['money'] / $totalMoney * 100 >= 10 ? '√' : '';
}
}
}
// 预计装修时间 -> 高意向到店客户
if (!empty($customerList[$c['customer_id']]['plan_deco_time'])) {
$planDecoTime = strtotime($customerList[$c['customer_id']]['plan_deco_time']);
$item['plan_month_day'] = ($planDecoTime - time()) / 86400 < 30 ? '√' : '';
}
//活动见面类型
$query_actmet = [];
$query_actmet[] = ['addtime', 'like', $c['date'] . '%'];
$query_actmet[] = ['customer_id', '=', $c['customer_id']];
$query_actmet[] = ['actmeet_type', '<>', 0];
$query_actmet[] = CustomerVisitLog::changeState(['state', '=', '已到场']);
$actmet = CustomerVisitLog::where($query_actmet)->order('addtime desc')->value('actmeet_type');
if (!empty($actmet)) {
$item['actmeet_type1'] = ($actmet == 1) ? '√' : '';
$item['actmeet_type2'] = ($actmet == 2) ? '√' : '';
$item['actmeet_type3'] = ($actmet == 3) ? '√' : '';
} else {
$item['actmeet_type1'] = '';
$item['actmeet_type2'] = '';
$item['actmeet_type3'] = '';
}
// if (!empty($customerList[$c['customer_id']]['plan_deco_time'])) {
// $planDecoTime = strtotime($customerList[$c['customer_id']]['plan_deco_time']);
// $month = (date('Y') - date('Y', $planDecoTime)) * 12 + date('m') - date('m', $planDecoTime);
// $item['plan_month1'] = $month <= 3 ? '√' : 0;
// $item['plan_month2'] = $month > 3 && $month <= 6 ? '√' : 0;
// $item['plan_month3'] = $month > 6 ? '√' : 0;
// } else {
// $item['plan_month1'] = '';
// $item['plan_month2'] = '';
// $item['plan_month3'] = '';
// }
// 到店次数
$where = [];
$where[] = ['addtime', '<', $c['date'] . ' 23:59:59'];
$where[] = ['customer_id', '=', $c['customer_id']];
$state1 = CustomerVisitLog::changeState('确定到店', 'chaos');
$where[] = ['state', 'in', $state1];
$count = CustomerVisitLog::where($where)->count();
$item['visit_number1'] = $count == 1 ? '√' : '';
$item['visit_number2'] = $count == 2 ? '√' : '';
$item['visit_number3'] = $count >= 3 ? '√' : '';
// 外场活动名
$aidList = array_unique(explode(',', $c['aid_list']));
$huodong = ModelActivity::where([['id', 'in', $aidList], ['cate', '=', 1]])->column('title,address');
$huodongList = [];
foreach ($huodong as $h) {
if ($h['address']) {
$huodongList[] = $h['title'] . '(' . $h['address'] . ')';
} else {
$huodongList[] = $h['title'];
}
}
$daochang = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['addtime', 'between', [$c['date'] . ' 00:00:00', $c['date'] . ' 23:59:59']], ['state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')]])->count();
$item['outfield_activities'] = $daochang ? implode(',', $huodongList) : '';
// 外场活动转内场新客户 活动前未到店以及后续状态,参加活动后,第一次到店显示
$item['outfield_infield'] = $this->getIsNewCusomerVisitAfterActivity($c['date'], $c['customer_id']);
$data[] = $item;
$num++;
}
// if (!empty($param['add_wechat_time'])) {
// foreach ($data as $key => $val) {
// if (!empty($val['add_wechat_time'])) {
// $xin[] = $val;
// }
// }
// $newtime = explode(' - ', $param['add_wechat_time']);
// $startwetime = str_replace('-', '/', $newtime[0]);
// $endwetime = str_replace('-', '/', $newtime[1]);
// foreach ($xin as $key => $val) {
// if ($val['add_wechat_time'] >= $startwetime && $val['add_wechat_time'] <= $endwetime) {
// $tian[] = $val;
// }
// }
// $total = count($tian);
// if ($getTotal == true) {
// $data = $tian;
// } else {
// $data = array_slice($tian, ($param['page'] - 1) * $param['limit'], $param['limit']);
// }
// }
return json(['code' => 0, 'data' => $data, 'count' => $total, 'page' => ceil($total / $limit)]);
}
/**
* 统计部报表数据统计
*/
public function new_customer_statistics($page, $limit, $getTotal = false)
{
$param = $this->request->only(['sdate' => '', 'edate' => '', 'org_id' => 0, 'add_wechat_time']);
// 获取部门id
$root_id = request()->employee->root_id;
$orgs = Org::where([['path', 'like', $root_id . '-%']])->column('name,id,pid,level,path', 'id');
// 部门领导获取
$leaders = Employee::with('org')->where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['uid', '<>', 0]])->select();
// 领导结构修改
$leadersData = [];
foreach ($leaders as $l) {
if (isset($leadersData[$l['org_id']])) {
$leadersData[$l['org_id']]['leaders'][] = $l['name'];
$leadersData[$l['org_id']]['leaders_ids'][] = $l['id'];
} else {
$leadersData[$l['org_id']] = [
'org_name' => $l['org']['name'],
'level' => $l['org']['level'],
'path' => $l['org']['path'],
'leaders_ids' => [
$l['id']
],
'leaders' => [
$l['name']
]
];
}
}
// 补充没领导的部门
foreach ($orgs as $k => $item) {
if (!isset($leadersData[$k])) {
$leadersData[$k] = [
'org_name' => $item['name'],
'level' => $item['level'],
'path' => $item['path'],
'leaders' => [],
'leaders_ids' => []
];
}
}
// 查询最近访问的客户
$where = [];
if ($param['org_id']) {
//查询本部门下面是否有部门
$childorg = orgSubIds($param['org_id']);
$where[] = ['org_id|customer_org_id', 'in', $childorg];
// $where[] = ['org_id', '=', $param['org_id']];
} else {
$where[] = ['org_id', 'in', array_keys($orgs)];
}
if ($param['sdate']) {
$param['sdate'] = date('Y-m-d', strtotime($param['sdate']));
$where[] = ['addtime', '>=', $param['sdate'] . ' 00:00:00'];
}
if ($param['edate']) {
$param['edate'] = date('Y-m-d', strtotime($param['edate']));
$where[] = ['addtime', '<=', $param['edate'] . ' 23:59:59'];
}
// 查询排除的部门
$sheji = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '设计部']])->value('id');
$xiaoshou = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '市场部']])->value('id');
$fanweinei = Org::whereOr([
[['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $sheji . '-%']],
[['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $xiaoshou . '-%']],
])->column('id');
$searchEmployeeIdList = Employee::where([['org_id', 'in', $fanweinei]])->column('id');
//如果有部门筛选把员工控制在本部门
if ($param['org_id']) $searchEmployeeIdList = Employee::where([['org_id', 'in', $childorg]])->column('id');
//$where[] = ['employee_id', 'in', $searchEmployeeIdList];
$where[] = ['customer_employee_id', 'in', $searchEmployeeIdList];
//2022-10-31辛居尚重11月1号开始正式使用
$where[] = ['addtime', '>', date('2022-10-31 23:59:59')];
//新增加查询记录到前一天的24点
$sel_endtime = date('Y-m-d');
if (empty($param['edate']) && empty($param['sdate'])) $where[] = ['addtime', '<', date('Y-m-d', strtotime("$sel_endtime -1 days")).' 23:59:59'];
if (!empty($param['add_wechat_time'])) {
$newtime = explode(' - ', $param['add_wechat_time']);
$sdate = strtotime($newtime[0]);
$edate = strtotime($newtime[1]);
$wechat_count = [];
$wechat_count = Customer::where([['ext', 'not null', ''], ['ext', '<>', ''], ['add_wechat_time', '>=', date('Y-m-d', $sdate)], ['add_wechat_time', '<=', date('Y-m-d', $edate)]])->column('id');
$cids = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
$wechat_count = array_intersect($cids, $wechat_count);
$where[] = ['customer_id', 'in', $wechat_count];
}
//去掉不需要的跟进记录
$state1 = CustomerVisitLog::changeState('已到店', 'chaos');
$state2 = CustomerVisitLog::changeState('已到场', 'chaos');
$state3 = CustomerVisitLog::changeState('已量房', 'chaos');
$state4 = CustomerVisitLog::changeState('已交定', 'chaos');
$state5 = CustomerVisitLog::changeState('已签单', 'chaos');
$where[] = ['state', 'in', array_merge($state1, $state2, $state3, $state4, $state5)];
$total = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date")->where($where)->group('customer_id,date')->count();
$searchObject = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date, max(addtime) as addtime, SUM(number_of_visitors) as number_of_visitors, SUM(measure_room_img_type='量房图片') as measure_type1, SUM(measure_room_img_type='量房报告图片') as measure_type2, SUM(measure_room_img_type='客户需求表') as measure_type3,GROUP_CONCAT(aid) as aid_list")->where($where)->order('date desc,addtime desc')->group('customer_id,date');
if ($getTotal == false) $searchObject->page($page, $limit);
$lastVisitCustomerList = $searchObject->select();
// 获取客户信息
$customerIdList = array_unique(array_column(empty($lastVisitCustomerList) ? [] : $lastVisitCustomerList->toArray(), 'customer_id'));
$customerList = Customer::with(['employee', 'designer'])->where([['id', 'in', $customerIdList]])->field('id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList1 = CustomerRecycle::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList2 = CustomerDropPool::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList = array_merge($customerList->toArray(), $customerList1->toArray(), $customerList2->toArray());
$customerList = array_combine(array_column($customerList, 'id'), $customerList);
$customerList = $this->emp_extfield_change($customerList);
// 查询爆品产品
$packageId = implode(',', array_column($customerList, 'package_id'));
$package = CustomerPackage::where([['id', 'in', $packageId]])->column('total_price', 'id');
// 获取公司名称
$companyName = Company::where([['root_id', '=', $this->root_id]])->value('company_name');
// 公司负责人获取
// $companyLeader = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['org_id', '=', $this->root_id], ['uid', '<>', 0]])->column('name');
// 来源
$sourseIdList = array_column($customerList, 'source_id');
$sourseList = CustomerSource::where([['id', 'in', $sourseIdList]])->column('source', 'id');
$num = ($page - 1) * $limit + 1;
// 处理数据
$data = [];
foreach ($lastVisitCustomerList as $c) {
$item = [
'key' => $num,
'customer_id' => $c['customer_id'],
];
if (!isset($customerList[$c['customer_id']])) {
$data[] = $item;
$num++;
continue;
}
list($Y, $m, $d) = explode('-', $c['date']);
$leader_arr = ['941' => '罗镐', '1847' => '刘有凤']; //馨居尚,菡萏怡景,部门负责人
$item = [
'key' => $num,
'customer_name' => $customerList[$c['customer_id']]['name'],
'date' => $Y . '年' . $m . '月' . $d . '日',
'addtime' => $c['addtime'],
'company_name' => $companyName,
'leader' => !empty($leader_arr[$this->root_id]) ? $leader_arr[$this->root_id] : '罗镐',
'community_name' => $customerList[$c['customer_id']]['community_name'],
// 'measure_type1' => $c['measure_type1'] ? '√' : '',
// 'measure_type2' => $c['measure_type2'] ? '√' : '',
// 'measure_type3' => $c['measure_type3'] ? '√' : '',
'measure_room_store' => $this->measureRoomStore($c['date'], $c['customer_id']),
'number_of_visitors' => $c['number_of_visitors'] == 0 ? '' : $c['number_of_visitors'],
'area' => $c['area'],
//'ext39' => $customerList[$c['customer_id']]['ext']['房屋位置'],
'ext39' => $customerList[$c['customer_id']]['ext']['house_location'],
//'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['谈单时长']) ? $customerList[$c['customer_id']]['ext']['谈单时长'] : 0,
'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['talking_about_single_time']) ? $customerList[$c['customer_id']]['ext']['talking_about_single_time'] : 0,
'source' => isset($sourseList[$customerList[$c['customer_id']]['source_id']]) ? $sourseList[$customerList[$c['customer_id']]['source_id']] : '',
'square' => $customerList[$c['customer_id']]['square'],
//'ext42' => $customerList[$c['customer_id']]['ext']['门牌单元号']
'ext42' => $customerList[$c['customer_id']]['ext']['unit_number']
];
//量房图片类型
$where_img_type = [];
$where_img_type[] = ['addtime', 'like', '%' . $c['date'] . '%'];
$where_img_type[] = ['customer_id', '=', $c['customer_id']];
$where_img_type[] = CustomerVisitLog::changeState(['state', '=', '确认量房']);
$str = CustomerVisitLog::where($where_img_type)->order('addtime desc')->column('measure_room_img_type', 'addtime');
$measure_type2 = $measure_type3 = '';
foreach ($str as $k => $v) {
if ($v && (strpos($v, '量房图片') !== false || strpos($v, '量房报告图片') !== false || strpos($v, '量房验房报告') !== false)) {
$measure_type2 = $k;
break;
}
if ($v && (strpos($v, '客户需求图片') !== false)) {
$measure_type3 = $k;
break;
}
}
$item['measure_type2'] = $measure_type2;
$item['measure_type3'] = $measure_type3;
$lastmeasure_time = CustomerVisitlog::where($where_img_type)->order('addtime desc')->value('addtime');
// $str = $str ? implode(',', $str) : '';
// $item['measure_type2'] = ($str && (strpos($str, '量房图片') !== false || strpos($str, '量房报告图片') !== false || strpos($str, '量房验房报告') !== false)) ? '√' : '';
// $item['measure_type3'] = ($str && strpos($str, '客户需求图片') !== false) ? '√' : '';
// 计算交房相差月份
$houseDeliveryTime = strtotime($customerList[$c['customer_id']]['house_delivery_time']);
$month = (date('Y', $houseDeliveryTime) - date('Y')) * 12 + date('m', $houseDeliveryTime) - date('m');
$type = '0';
// if ($customerList[$c['customer_id']]['ext']['加微类型'] == '常规加微') {
// $type = '1';
// } elseif ($customerList[$c['customer_id']]['ext']['加微类型'] == '社群加微') {
// $type = '2';
// }
$item['add_wechat_time'] = $customerList[$c['customer_id']]['ext']['add_wechat_time'];
if ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '常规加微') {
$type = '1';
} elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '社群加微') {
$type = '2';
} elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '历史加微') {
$type = '3';
}
if ($type !== '0') {
$item['house_type' . $type] = in_array($customerList[$c['customer_id']]['house_type'], ['现房', '二手房']) ? '√' : '';
if ($item['house_type' . $type] != '√' && !empty($customerList[$c['customer_id']]['house_delivery_time'])) {
$item['delivery_month' . $type . '_1'] = $month <= 3 ? '√' : '';
$item['delivery_month' . $type . '_2'] = $month > 3 && $month <= 6 ? '√' : '';
$item['delivery_month' . $type . '_3'] = $month > 6 && $month <= 12 ? '√' : '';
$item['delivery_month' . $type . '_4'] = $month > 12 ? '√' : '';
} else {
$item['delivery_month' . $type . '_1'] = '';
$item['delivery_month' . $type . '_2'] = '';
$item['delivery_month' . $type . '_3'] = '';
$item['delivery_month' . $type . '_4'] = '';
}
}
// 设计师
if (!empty($customerList[$c['customer_id']]['designer'])) {
$designerOrgId = $customerList[$c['customer_id']]['designer']['org_id'];
$item['designer_name'] = $customerList[$c['customer_id']]['designer']['name'];
$item['designer_org'] = $orgs[$designerOrgId]['name'];
$item['designer_leader'] = isset($leadersData[$designerOrgId]) ? implode(',', $leadersData[$designerOrgId]['leaders']) : '';
$path = $leadersData[$designerOrgId]['path'];
$pathList = array_filter(explode('-', $path));
foreach ($pathList as $od) {
if (strstr($leadersData[$od]['org_name'], '部') !== false && $leadersData[$od]['org_name'] != '设计部') {
$level = 3;
} else {
$level = $leadersData[$od]['level'];
}
if (isset($item['employee_org_level' . $level])) $level++;
$item['designer_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
$item['designer_org_level' . $level] = $leadersData[$od]['org_name'];
}
} else {
$item['designer_name'] = $item['designer_org'] = $item['designer_leader'] = '';
}
// 员工
if (!empty($customerList[$c['customer_id']]['employee'])) {
$employeeOrgId = $customerList[$c['customer_id']]['employee']['org_id'];
$item['employee_name'] = $customerList[$c['customer_id']]['employee']['name'];
$item['employee_org'] = $orgs[$employeeOrgId]['name'];
$item['employee_leader'] = implode(',', $leadersData[$employeeOrgId]['leaders']);
$path = $leadersData[$employeeOrgId]['path'];
$pathList = array_filter(explode('-', $path));
foreach ($pathList as $od) {
if (strstr($leadersData[$od]['org_name'], '区中区') !== false) {
$level = 3;
} elseif (strstr($leadersData[$od]['org_name'], '中心') !== false) {
$level = 4;
} elseif (strstr($leadersData[$od]['org_name'], '区') !== false) {
$level = 2;
} else {
$level = $leadersData[$od]['level'];
}
if (isset($item['employee_org_level' . $level])) $level++;
// $level = $leadersData[$od]['level'];
$item['employee_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
$item['employee_org_level' . $level] = $leadersData[$od]['org_name'];
}
// 查询直属领导是否跟踪过
// 获取直属领导id
$leaderIdList = $leadersData[$employeeOrgId]['leaders_ids'];
$query = [];
$query[] = ['employee_id', 'in', $leaderIdList];
$query[] = ['addtime', 'like', $c['date'] . '%'];
$query[] = ['customer_id', '=', $c['customer_id']];
$leader_visit_time = CustomerVisitLog::where($query)->order('addtime desc')->value('addtime');
$item['leader_visit'] = !empty($leader_visit_time) && ($item['measure_type2'] || $item['measure_type3']) ? $leader_visit_time : "";
//$item['leader_visit'] = !CustomerVisitLog::where($query)->findOrEmpty()->isEmpty() && ($item['measure_type2'] == '√' || $item['measure_type3'] == '√') ? "√" : "";
} else {
$item['employee_name'] = $item['employee_org'] = $item['employee_leader'] = '';
$item['leader_visit'] = '';
}
// 量房验房
//$item['measure_type1'] = ($item['leader_visit'] == '√' || $item['measure_type2'] == '√' || $item['measure_type3'] == '√') ? $lastmeasure_time : '';
$item['measure_type1'] = ($item['leader_visit'] || $item['measure_type2'] || $item['measure_type3']) ? $lastmeasure_time : '';
// 手机号
$phoneArr = array_filter([$customerList[$c['customer_id']]['phone'], $customerList[$c['customer_id']]['phone1'], $customerList[$c['customer_id']]['phone2']]);
$item['phone'] = substr_replace(array_shift($phoneArr), '******', 3, 6);
// 建群情况
//$jianqun = explode(',', $customerList[$c['customer_id']]['ext']['建群情况']);
$jianqun = explode(',', $customerList[$c['customer_id']]['ext']['group_building']);
//建群时间
$jianqun_time = !empty($customerList[$c['customer_id']]['ext']['group_building_date']) ? $customerList[$c['customer_id']]['ext']['group_building_date'] : '';
if (empty($jianqun_time)) {
$jianqun_time = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['save_portrait_field', 'like', '%group_building%']])->value('addtime');
}
$item['ext35_1'] = (in_array('1对1小群', $jianqun) || in_array('一对一小群', $jianqun)) && $jianqun_time ? $jianqun_time : '';
$item['ext35_2'] = in_array('临时大群', $jianqun) && $jianqun_time ? $jianqun_time : '';
$item['ext35_3'] = in_array('社群', $jianqun) && $jianqun_time ? $jianqun_time : '';
// 直播
//$zhibo = explode(',', $customerList[$c['customer_id']]['ext']['直播情况']);
$zhibo = explode(',', $customerList[$c['customer_id']]['ext']['live_broadcast']);
$zhibo_time = !empty($customerList[$c['customer_id']]['ext']['live_broadcast_date']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_date'] : '';
if (empty($zhibo_time)) {
$zhibo_time = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['save_portrait_field', 'like', '%live_broadcast%']])->value('addtime');
}
$item['ext36_1'] = (in_array('一对一直播', $zhibo) || in_array('1对1业务直播', $zhibo)) && $zhibo_time ? $zhibo_time : '';
$item['ext36_2'] = in_array('1对1设计直播', $zhibo) && $zhibo_time ? $zhibo_time : '';
$item['ext36_3'] = in_array('一对多直播', $zhibo) && $zhibo_time ? $zhibo_time : '';
$item['ext37_1'] = !empty($item['ext36_1']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_business'] : '';
$item['ext37_2'] = !empty($item['ext36_2']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_design'] : '';
$item['ext37_3'] = !empty($item['ext36_3']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_personnel'] : '';
// 签单金额
$condition = [];
// $condition[] = ['addtime', 'like', $c['date'] . '%'];
$condition[] = ['customer_id', '=', $c['customer_id']];
$condition1 = $condition;
$condition2 = $condition;
$condition1[] = CustomerVisitLog::changeState(['state', '=', '已交定']);
$condition2[] = CustomerVisitLog::changeState(['state', '=', '已签单']);
$item['signature_money'] = CustomerVisitLog::where($condition2)->order('addtime desc')->value('money');
$deposit = CustomerVisitLog::where($condition1)->order('addtime desc')->field('money,deposit_mode')->find();
if (!empty($deposit)) {
$deposit['deposit_mode'] !== 0 ? $item['deposit_money1'] = $deposit['money'] : $item['deposit_money2'] = $deposit['money'];
// 爆品定金
if ($customerList[$c['customer_id']]['package_id']) {
$packageIdList = explode(',', $customerList[$c['customer_id']]['package_id']);
$totalMoney = 0;
foreach ($packageIdList as $pid) {
$totalMoney += $package[$pid];
}
if ($totalMoney !== 0) {
$item['package'] = $deposit['money'] / $totalMoney * 100 >= 10 ? '√' : '';
}
}
}
// 预计装修时间 -> 高意向到店客户
if (!empty($customerList[$c['customer_id']]['plan_deco_time'])) {
$planDecoTime = strtotime($customerList[$c['customer_id']]['plan_deco_time']);
$item['plan_month_day'] = ($planDecoTime - time()) / 86400 < 30 ? $customerList[$c['customer_id']]['plan_deco_time'] : '';
}
//活动见面类型
$query_actmet = [];
$query_actmet[] = ['addtime', 'like', $c['date'] . '%'];
$query_actmet[] = ['customer_id', '=', $c['customer_id']];
$query_actmet[] = ['actmeet_type', '<>', 0];
$query_actmet[] = CustomerVisitLog::changeState(['state', '=', '已到场']);
$actmet = CustomerVisitLog::where($query_actmet)->order('addtime desc')->value('addtime,actmeet_type');
if (!empty($actmet)) {
$item['actmeet_type1'] = !empty($actmet['actmeet_type']) && $actmet['actmeet_type'] == 1 ? $actmet['addtime'] : '';
$item['actmeet_type2'] = !empty($actmet['actmeet_type']) && $actmet['actmeet_type'] == 2 ? $actmet['addtime'] : '';
$item['actmeet_type3'] = !empty($actmet['actmeet_type']) && $actmet['actmeet_type'] == 3 ? $actmet['addtime'] : '';
} else {
$item['actmeet_type1'] = '';
$item['actmeet_type2'] = '';
$item['actmeet_type3'] = '';
}
//查询谈单时长
$query_order_time = [];
$query_order_time[] = ['addtime', 'like', $c['date'] . '%'];
$query_order_time[] = ['customer_id', '=', $c['customer_id']];
$query_order_time[] = CustomerVisitLog::changeState(['state', '=', '已到店']);
$order_time = CustomerVisitLog::where($query_order_time)->sum('talking_order_time');
$item['order_time'] = $order_time;
//新的判断网谈登记,茶楼见面
// $query_actmet1 = [];
// $query_actmet1[] = ['addtime', 'like', $c['date'] . '%'];
// $query_actmet1[] = ['customer_id', '=', $c['customer_id']];
// //$query_actmet1[] = ['save_portrait_field','not null',''];
// //$query_actmet1[] = ['save_portrait_field','like','online_meet_checked%'];
// $query_actmet1[] = ['save_portrait_field', 'like', ['%online_meet_checked%', '%teahouse_meet%'], 'OR'];
// $query_actmet1[] = CustomerVisitLog::changeState(['state', '=', '未到访']);
// $actmet_data = CustomerVisitLog::where($query_actmet1)->column('save_portrait_field', 'id');
// $online_meet_checked = $teahouse_meet = '';
// if ($actmet_data) {
// foreach ($actmet_data as $m => $p) {
// $save_field_list = json_decode($p, true);
// foreach ($save_field_list as $k => $v) {
// if ($v['keyname'] == 'online_meet_checked') $online_meet_checked = $v['value'];
// if ($v['keyname'] == 'teahouse_meet') $teahouse_meet = $v['value'];
// }
// }
// }
// $item['actmeet_type2'] = $teahouse_meet ? $teahouse_meet : '';
// $item['actmeet_type3'] = $online_meet_checked ? $online_meet_checked : '';
//查询定金客户转到店
$item['djzdd'] = $this->isStateToShop($c['date'], $c['customer_id'], '已交定', $customerList[$c['customer_id']]['ext']);
//查询量房客户转到店
$item['lfzdd'] = $this->isStateToShop($c['date'], $c['customer_id'], '已量房', $customerList[$c['customer_id']]['ext']);
//查询外场活动转到店
$item['wczdd'] = $this->isStateToShop($c['date'], $c['customer_id'], '已到场', $customerList[$c['customer_id']]['ext']);
//新到店次数
$where = [];
$starttime = date('Y-m', strtotime($c['date'])) . '-01 00:00:00';
$where[] = ['addtime', '<', $c['date'] . ' 23:59:59'];
$where[] = ['addtime', '>', $starttime];
$where[] = ['customer_id', '=', $c['customer_id']];
$state1 = CustomerVisitLog::changeState('确定到店', 'chaos');
$where[] = ['state', 'in', $state1];
$list = CustomerVisitLog::where($where)->order('addtime desc')->column('addtime,id');
$newlist = [];
if (!empty($list)) {
foreach ($list as $k => $v) {
$list[$k]['newtime'] = date('Y-m-d', strtotime($v['addtime']));
}
foreach ($list as $k => $v) {
$newlist[$v['newtime']][] = $v;
}
}
$count = count($newlist);
$daodian = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['addtime', 'between', [$c['date'] . ' 00:00:00', $c['date'] . ' 23:59:59']], ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]])->order('addtime desc')->value('addtime');
$item['visit_number1'] = $count == 1 && $daodian ? end($newlist)[0]['addtime'] : '';
//$item['visit_number2'] = $count == 2 ? array_pop($newlist)[0]['addtime'] : '';
$item['visit_number2'] = $count == 2 && $daodian ? current($newlist)[0]['addtime'] : '';
$item['visit_number3'] = $count >= 3 && $daodian ? '√' : '';
// 外场活动名
$aidList = array_unique(explode(',', $c['aid_list']));
$huodong = ModelActivity::where([['id', 'in', $aidList], ['cate', '=', 1]])->column('title,address');
$huodongList = [];
foreach ($huodong as $h) {
if ($h['address']) {
$huodongList[] = $h['title'] . '(' . $h['address'] . ')';
} else {
$huodongList[] = $h['title'];
}
}
$daochang = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['addtime', 'between', [$c['date'] . ' 00:00:00', $c['date'] . ' 23:59:59']], ['state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')]])->count();
$item['outfield_activities'] = $daochang ? implode(',', $huodongList) : '';
// 外场活动转内场新客户 活动前未到店以及后续状态,参加活动后,第一次到店显示
//$item['outfield_infield'] = $this->getIsNewCusomerVisitAfterActivity($c['date'], $c['customer_id']);
$item['outfield_infield'] = $daodian && empty($item['djzdd']) && empty($item['lfzdd']) && empty($item['wczdd']) ? $daodian : '';
$data[] = $item;
$num++;
}
return json(['code' => 0, 'data' => $data, 'count' => $total, 'page' => ceil($total / $limit)]);
}
/**
* 查询是否是定金,量房,外场客户转到店
*/
public function isStateToShop($date, $customerId, $state, $cusext)
{
$where = [];
$where[] = ['addtime', '<', $date . ' 23:59:59'];
$where[] = ['customer_id', '=', $customerId];
$state1 = CustomerVisitLog::changeState('已到店', 'chaos');
$where[] = ['state', 'in', $state1];
// 查询日期必须到店
$had = CustomerVisitLog::where($where)->order('addtime asc')->column('addtime');
// 是否多次到店或未到店,当天到店多次算一次
if (empty($had)) return '';
foreach ($had as $k => $v) {
if ($v < $date . ' 00:00:00') return '';
}
//$lastTime = strtotime($had[0]);
$lastTime = strtotime(end($had));
// 判断到店日期是否是所选日期
if (date('Y-m-d', $lastTime) != $date) return '';
// 判断是否参加过活动
$state_time = CustomerVisitLog::where([
['addtime', '<', $date . ' 23:59:59'],
['state', 'in', CustomerVisitLog::changeState($state, 'chaos')],
['customer_id', '=', $customerId]
])->order('addtime asc')->value('addtime');
if (empty($state_time)) return '';
//if (in_array($state, ['已量房', '已交定']) && empty($state_time)) return '';
//如果已到场时间为空再查询网谈登记时间跟茶楼见面时间,如果两个都有值取最早时间的值
// if ($state == '已到场' && empty($state_time)) {
// if (isset($cusext['online_meet_checked']) && isset($cusext['teahouse_meet'])) $state_time = $cusext['online_meet_checked'] > isset($cusext['teahouse_meet']) ? isset($cusext['teahouse_meet']) : isset($cusext['online_meet_checked']);
// if (isset($cusext['online_meet_checked'])) $state_time = $cusext['online_meet_checked'];
// if (isset($cusext['teahouse_meet'])) $state_time = $cusext['teahouse_meet'];
// if (empty($state_time)) return '';
// }
if (strtotime($state_time) < $lastTime) return date('Y-m-d H:i:s', $lastTime);
return '';
}
/**
* 获取客户跟进数据表表头
*/
public function new_table_data()
{
// 领导获取
$orgLeader = Employee::where([['root_id', '=', $this->root_id], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id, org_id, name');
$orgDealLeader = [];
foreach ($orgLeader as $leader) {
$orgDealLeader[$leader['org_id']][] = $leader['name'];
}
// 查询设计师部门
$designerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 2]])->order('level desc')->column('level, id, name');
// 部门处理
$dealDesignerOrg = [];
foreach ($designerOrg as $org) {
$dealDesignerOrg[$org['level']]['name'][] = $org['name'];
isset($dealDesignerOrg[$org['level']]['leader']) ?: $dealDesignerOrg[$org['level']]['leader'] = [];
if (isset($orgDealLeader[$org['id']]))
$dealDesignerOrg[$org['level']]['leader'] = array_merge($dealDesignerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
}
// 剔除末枝部门
$del = false;
foreach ($dealDesignerOrg as $k => $item) {
if (!$del) {
unset($dealDesignerOrg[$k]);
$del = true;
}
}
// 查询销售部门
$sellerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 1]])->order('level desc')->column('level, id, name');
// 部门处理
$dealSellerOrg = [];
foreach ($sellerOrg as $org) {
$dealSellerOrg[$org['level']]['name'][] = $org['name'];
isset($dealSellerOrg[$org['level']]['leader']) ?: $dealSellerOrg[$org['level']]['leader'] = [];
if (isset($orgDealLeader[$org['id']]))
$dealSellerOrg[$org['level']]['leader'] = array_merge($dealSellerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
}
// 剔除末枝部门
$del = false;
foreach ($dealSellerOrg as $k => $item) {
if (!$del) {
unset($dealSellerOrg[$k]);
$del = true;
}
}
$header = $this->getNewVisitLogHeaders();
view::assign('header1', $header['header1']);
view::assign('header2', $header['header2']);
$org = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['org_type', '=', 1]])->field('id,name')->select()->toArray();
View::assign('org', $org);
$row = [];
foreach ($header['header1'] as $item) {
if (isset($item['child'])) {
foreach ($item['child'] as $c) {
$row[] = $c;
}
} else {
$row[] = $item['field'];
}
}
view::assign('row', json_encode($row));
return View::fetch();
}
/**
* 获取线上跟踪客户记录表头
*/
private function getNewVisitLogHeaders()
{
// 第二行头部
$header2 = [];
// 设计师部门插入头部
$header2[] = ["field" => 'customer_name', "title" => '客户名字', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'phone', "title" => '客户电话', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext39', "title" => '省市区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'community_name', "title" => '楼盘', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext42', "title" => '门牌单元号', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'square', "title" => '面积', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_name', "title" => '业务员', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org', "title" => '业务部门', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader', "title" => '业务经理', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level4', "title" => '业务中心', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level4', "title" => '业务总监', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level3', "title" => '区中区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level3', "title" => '区中区副总', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level2', "title" => '大区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level2', "title" => '大区副总', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_name', "title" => '设计师', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_org', "title" => '设计部门', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader', "title" => '设计经理', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_org_level2', "title" => '设计中心', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader_level2', "title" => '设计总监', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader_level1', "title" => '设计副总', "align" => 'center', "class" => "bgEEECE1"];
// 加微
$header2[] = ["field" => 'house_type1', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'house_type2', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'house_type3', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
// 建群
$header2[] = ["field" => 'ext35_1', "title" => '1对1小群', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext35_2', "title" => '临时大群', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext35_3', "title" => '社群', "align" => 'center', "class" => "bgEEECE1"];
// 直播
$header2[] = ["field" => 'ext36_1', "title" => '1对1业务直播', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext37_1', "title" => '业务员', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext36_2', "title" => '1对1设计直播', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext37_2', "title" => '设计师', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext36_3', "title" => '一对多直播', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext37_3', "title" => '直播人员', "align" => 'center', "class" => "bgF2DCDB"];
// 量房验房
$header2[] = ["field" => 'measure_type2', "title" => '量房验房报告', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'measure_type3', "title" => '客户需求表', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'leader_visit', "title" => '经理是否回访', "align" => 'center', "class" => "bgEEECE1"];
// 到店
$header2[] = ["field" => 'outfield_activities', "title" => '外场活动到店', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'outfield_infield', "title" => '内场直接到店', "align" => 'center', "class" => "bgEEECE1"];
//见面方式
$header2[] = ["field" => 'actmeet_type1', "title" => '外场活动', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'actmeet_type2', "title" => '茶楼见面', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'actmeet_type3', "title" => '网谈登记', "align" => 'center', "class" => "bgEEECE1"];
// 管理到店数据
$header2[] = ["field" => 'deposit_money2', "title" => '线上定金', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'deposit_money1', "title" => '线下定金', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'djzdd', "title" => '定金客户转到店', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'package', "title" => '爆品定金(10%以上)', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'measure_type1', "title" => '量房验房', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'lfzdd', "title" => '量房客户转到店', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'visit_number1', "title" => '一次到店
(内场到店)', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'visit_number2', "title" => '二次到店', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'visit_number3', "title" => '三次及以上到店', "align" => 'center', "class" => "bgEEECE1"];
// 插入销售部门
$employeeChild = ['employee_name', 'employee_org', 'employee_leader', 'employee_org_level4', 'employee_leader_level4', 'employee_org_level3', 'employee_leader_level3', 'employee_org_level2', 'employee_leader_level2'];
// 设计部门
$designerChild = ['designer_name', 'designer_org', 'designer_leader', 'designer_org_level2', 'designer_leader_level2', 'designer_leader_level1'];
// 第一行头部
$header1 = [];
$header1[] = ["type" => 'key', "field" => 'key', "title" => '序号', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => 'date', "title" => '日期', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['customer_name', 'phone', 'ext39', 'community_name', 'ext42', 'square'], "title" => '客户信息 ', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
$header1[] = ["field" => 'company_name', "title" => '公司', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
$header1[] = ["field" => 'leader', "title" => '负责人', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
$header1[] = ["field" => "", "child" => $employeeChild, "title" => '营销', "align" => 'center', "colspan" => 9, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => $designerChild, "title" => '设计', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['house_type1', 'delivery_month1_1', 'delivery_month1_2', 'delivery_month1_3', 'delivery_month1_4'], "title" => '常规加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "", "child" => ['house_type2', 'delivery_month2_1', 'delivery_month2_2', 'delivery_month2_3', 'delivery_month2_4'], "title" => '社群加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "", "child" => ['house_type3', 'delivery_month3_1', 'delivery_month3_2', 'delivery_month3_3', 'delivery_month3_4'], "title" => '历史加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "add_wechat_time", "title" => '加微时间', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => "plan_month_day", "title" => '高意向到店客户
(1个月内装修)', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['ext35_1', 'ext35_2', 'ext35_3'], "title" => '建群', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['ext36_1', 'ext37_1', 'ext36_2', 'ext37_2', 'ext36_3', 'ext37_3'], "title" => '直播', "align" => 'center', "colspan" => 6, "class" => "bgF2DCDB"];
$header1[] = ["field" => "", "child" => ['measure_type2', 'measure_type3', 'leader_visit'], "title" => '量房验房', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
// $header1[] = ["field" => "yylf", "title" => '预约量房', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
// $header1[] = ["field" => "yydd", "title" => '预约到店', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
// $header1[] = ["field" => "yydc", "title" => '预约到场', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => '', "child" => ['outfield_activities', 'outfield_infield'], "title" => '到店', "align" => 'center', "colspan" => 2, "class" => "bgFDE9D9"];
$header1[] = ["field" => '', "child" => ['actmeet_type1', 'actmeet_type2', 'actmeet_type3'], "title" => '见面方式', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
$header1[] = ["field" => '', "child" => ['deposit_money2', 'deposit_money1', 'djzdd', 'package', 'measure_type1', 'lfzdd', 'visit_number1', 'visit_number2', 'visit_number3'], "title" => '管理到店数据', "align" => 'center', "colspan" => 9, "class" => "bgFDE9D9"];
$header1[] = ["field" => 'number_of_visitors', "title" => '到店人数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
$header1[] = ["field" => 'signature_money', "title" => '合同价', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
$header1[] = ["field" => 'wczdd', "title" => '外场转内场到店', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
$header1[] = ["field" => 'order_time', "title" => '谈单时长', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
return ['header1' => $header1, 'header2' => $header2];
}
/**
* 总经办报表
*/
public function officeStatistics($page, $limit)
{
$param = $this->request->only(['sdate' => '', 'edate' => '', 'org_id' => 0, 'add_wechat_time']);
// 获取部门id
$root_id = request()->employee->root_id;
$orgs = Org::where([['path', 'like', $root_id . '-%']])->column('name,id,pid,level,path', 'id');
// 部门领导获取
$leaders = Employee::with('org')->where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['uid', '<>', 0]])->select();
// 领导结构修改
$leadersData = [];
foreach ($leaders as $l) {
if (isset($leadersData[$l['org_id']])) {
$leadersData[$l['org_id']]['leaders'][] = $l['name'];
$leadersData[$l['org_id']]['leaders_ids'][] = $l['id'];
} else {
$leadersData[$l['org_id']] = [
'org_name' => $l['org']['name'],
'level' => $l['org']['level'],
'path' => $l['org']['path'],
'leaders_ids' => [
$l['id']
],
'leaders' => [
$l['name']
]
];
}
}
// 补充没领导的部门
foreach ($orgs as $k => $item) {
if (!isset($leadersData[$k])) {
$leadersData[$k] = [
'org_name' => $item['name'],
'level' => $item['level'],
'path' => $item['path'],
'leaders' => [],
'leaders_ids' => []
];
}
}
// 查询最近访问的客户
$where = [];
if ($param['org_id']) {
//查询本部门下面是否有部门
$childorg = orgSubIds($param['org_id']);
$where[] = ['org_id|customer_org_id', 'in', $childorg];
// $where[] = ['org_id', '=', $param['org_id']];
} else {
$where[] = ['org_id', 'in', array_keys($orgs)];
}
if ($param['sdate']) {
$param['sdate'] = date('Y-m-d', strtotime($param['sdate']));
$where[] = ['addtime', '>=', $param['sdate'] . ' 00:00:00'];
}
if ($param['edate']) {
$param['edate'] = date('Y-m-d', strtotime($param['edate']));
$where[] = ['addtime', '<=', $param['edate'] . ' 23:59:59'];
}
// 查询排除的部门
$sheji = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '设计部']])->value('id');
$xiaoshou = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '市场部']])->value('id');
$fanweinei = Org::whereOr([
[['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $sheji . '-%']],
[['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $xiaoshou . '-%']],
])->column('id');
$searchEmployeeIdList = Employee::where([['org_id', 'in', $fanweinei]])->column('id');
//如果有部门筛选把员工控制在本部门
if ($param['org_id']) $searchEmployeeIdList = Employee::where([['org_id', 'in', $childorg]])->column('id');
//$where[] = ['employee_id', 'in', $searchEmployeeIdList];
$where[] = ['customer_employee_id', 'in', $searchEmployeeIdList];
//2022-10-31辛居尚重11月1号开始正式使用
$where[] = ['addtime', '>', date('2022-10-31 23:59:59')];
//新增加查询记录到前一天的24点
$sel_endtime = date('Y-m-d');
//if (empty($param['edate']) && empty($param['sdate'])) $where[] = ['addtime', '<', date('Y-m-d', strtotime("$sel_endtime -1 days")).' 23:59:59'];
if (!empty($param['add_wechat_time'])) {
$newtime = explode(' - ', $param['add_wechat_time']);
$sdate = strtotime($newtime[0]);
$edate = strtotime($newtime[1]);
$wechat_count = [];
$wechat_count = Customer::where([['ext', 'not null', ''], ['ext', '<>', ''], ['add_wechat_time', '>=', date('Y-m-d', $sdate)], ['add_wechat_time', '<=', date('Y-m-d', $edate)]])->column('id');
$cids = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
$wechat_count = array_intersect($cids, $wechat_count);
$where[] = ['customer_id', 'in', $wechat_count];
}
//只查询客户的扩展字段更新跟量房的记录
$where1[] = ['save_portrait_field', 'like', ['%add_wechat_time%', '%add_wechat_type%', '%group_building"%', '%live_broadcast%'], 'OR'];
$where1[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')];
$total = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date")->where($where)
->where(function ($query) use ($where1) {
$query->whereOr($where1);
})->group('customer_id,date')->count();
$lastVisitCustomerList = CustomerVisitLog::field("customer_id,state,save_portrait_field,DATE_FORMAT(addtime,'%Y-%m-%d') as date, max(addtime) as addtime")->where($where)
->where(function ($query) use ($where1) {
$query->whereOr($where1);
})->order('date desc,addtime desc')->group('customer_id,date')->page($page, $limit)->select();
// 获取客户信息
$customerIdList = array_unique(array_column(empty($lastVisitCustomerList) ? [] : $lastVisitCustomerList->toArray(), 'customer_id'));
$customerList = Customer::with(['employee', 'designer'])->where([['id', 'in', $customerIdList]])->field('id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList1 = CustomerRecycle::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList2 = CustomerDropPool::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
$customerList = array_merge($customerList->toArray(), $customerList1->toArray(), $customerList2->toArray());
$customerList = array_combine(array_column($customerList, 'id'), $customerList);
$customerList = $this->emp_extfield_change($customerList);
// 获取公司名称
$companyName = Company::where([['root_id', '=', $this->root_id]])->value('company_name');
$num = ($page - 1) * $limit + 1;
// 处理数据
$data = [];
foreach ($lastVisitCustomerList as $c) {
$item = [
'key' => $num,
'customer_id' => $c['customer_id'],
];
if (!isset($customerList[$c['customer_id']])) {
$data[] = $item;
$num++;
continue;
}
list($Y, $m, $d) = explode('-', $c['date']);
$leader_arr = ['941' => '罗镐', '1847' => '刘有凤']; //馨居尚,菡萏怡景,部门负责人
$item = [
'key' => $num,
'customer_name' => $customerList[$c['customer_id']]['name'],
'date' => $Y . '年' . $m . '月' . $d . '日',
'addtime' => $c['addtime'],
'company_name' => $companyName,
'leader' => !empty($leader_arr[$this->root_id]) ? $leader_arr[$this->root_id] : '罗镐',
'community_name' => $customerList[$c['customer_id']]['community_name'],
'measure_room_store' => $this->measureRoomStore($c['date'], $c['customer_id']),
'number_of_visitors' => $c['number_of_visitors'] == 0 ? '' : $c['number_of_visitors'],
'area' => $c['area'],
'ext39' => $customerList[$c['customer_id']]['ext']['house_location'],
'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['talking_about_single_time']) ? $customerList[$c['customer_id']]['ext']['talking_about_single_time'] : 0,
'source' => isset($sourseList[$customerList[$c['customer_id']]['source_id']]) ? $sourseList[$customerList[$c['customer_id']]['source_id']] : '',
'square' => $customerList[$c['customer_id']]['square'],
'ext42' => $customerList[$c['customer_id']]['ext']['unit_number']
];
//查询当天所有的修改扩展字段的记录
$savelist = CustomerVisitLog::where([['addtime', 'between', [$c['date'] . ' 00:00:00', $c['date'] . ' 23:59:59']], ['save_portrait_field', 'like', ['%add_wechat_time%', '%add_wechat_type%', '%group_building"%', '%live_broadcast%'], 'OR'], ['customer_id', '=', $c['customer_id']]])->column('save_portrait_field');
$day_savefild = [];
if (!empty($savelist)) {
foreach ($savelist as $k => $v) {
$day_savefild = array_merge(json_decode($v, true), $day_savefild);
}
$day_savefild = array_unique(array_column($day_savefild, 'keyname'));
}
$item['day_savefild'] = $day_savefild;
// 计算交房相差月份
$houseDeliveryTime = strtotime($customerList[$c['customer_id']]['house_delivery_time']);
$month = (date('Y', $houseDeliveryTime) - date('Y')) * 12 + date('m', $houseDeliveryTime) - date('m');
$type = '0';
$item['add_wechat_time'] = in_array('add_wechat_time', $day_savefild) ? $customerList[$c['customer_id']]['ext']['add_wechat_time'] : '';
if ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '常规加微') {
$type = '1';
} elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '社群加微') {
$type = '2';
} elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '历史加微') {
$type = '3';
}
if ($type !== '0' && in_array('add_wechat_type', $day_savefild)) {
$item['house_type' . $type] = in_array($customerList[$c['customer_id']]['house_type'], ['现房', '二手房']) ? '√' : '';
if ($item['house_type' . $type] != '√' && !empty($customerList[$c['customer_id']]['house_delivery_time'])) {
$item['delivery_month' . $type . '_1'] = $month <= 3 ? '√' : '';
$item['delivery_month' . $type . '_2'] = $month > 3 && $month <= 6 ? '√' : '';
$item['delivery_month' . $type . '_3'] = $month > 6 && $month <= 12 ? '√' : '';
$item['delivery_month' . $type . '_4'] = $month > 12 ? '√' : '';
} else {
$item['delivery_month' . $type . '_1'] = '';
$item['delivery_month' . $type . '_2'] = '';
$item['delivery_month' . $type . '_3'] = '';
$item['delivery_month' . $type . '_4'] = '';
}
}
// 设计师
if (!empty($customerList[$c['customer_id']]['designer'])) {
$designerOrgId = $customerList[$c['customer_id']]['designer']['org_id'];
$item['designer_name'] = $customerList[$c['customer_id']]['designer']['name'];
$item['designer_org'] = $orgs[$designerOrgId]['name'];
$item['designer_leader'] = isset($leadersData[$designerOrgId]) ? implode(',', $leadersData[$designerOrgId]['leaders']) : '';
$path = $leadersData[$designerOrgId]['path'];
$pathList = array_filter(explode('-', $path));
foreach ($pathList as $od) {
if (strstr($leadersData[$od]['org_name'], '部') !== false && $leadersData[$od]['org_name'] != '设计部') {
$level = 3;
} else {
$level = $leadersData[$od]['level'];
}
if (isset($item['employee_org_level' . $level])) $level++;
$item['designer_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
$item['designer_org_level' . $level] = $leadersData[$od]['org_name'];
}
} else {
$item['designer_name'] = $item['designer_org'] = $item['designer_leader'] = '';
}
// 员工
if (!empty($customerList[$c['customer_id']]['employee'])) {
$employeeOrgId = $customerList[$c['customer_id']]['employee']['org_id'];
$item['employee_name'] = $customerList[$c['customer_id']]['employee']['name'];
$item['employee_org'] = $orgs[$employeeOrgId]['name'];
$item['employee_leader'] = implode(',', $leadersData[$employeeOrgId]['leaders']);
$path = $leadersData[$employeeOrgId]['path'];
$pathList = array_filter(explode('-', $path));
foreach ($pathList as $od) {
if (strstr($leadersData[$od]['org_name'], '区中区') !== false) {
$level = 3;
} elseif (strstr($leadersData[$od]['org_name'], '中心') !== false) {
$level = 4;
} elseif (strstr($leadersData[$od]['org_name'], '区') !== false) {
$level = 2;
} else {
$level = $leadersData[$od]['level'];
}
if (isset($item['employee_org_level' . $level])) $level++;
$item['employee_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
$item['employee_org_level' . $level] = $leadersData[$od]['org_name'];
}
// 查询直属领导是否跟踪过
// 获取直属领导id
$leaderIdList = $leadersData[$employeeOrgId]['leaders_ids'];
} else {
$item['employee_name'] = $item['employee_org'] = $item['employee_leader'] = '';
$item['leader_visit'] = '';
}
// 量房验房
$item['measure_type1'] = in_array($c['state'], [8, '已量房', '确定量房', '确认量房', '8']) ? '√' : '';
// 手机号
$phoneArr = array_filter([$customerList[$c['customer_id']]['phone'], $customerList[$c['customer_id']]['phone1'], $customerList[$c['customer_id']]['phone2']]);
$item['phone'] = substr_replace(array_shift($phoneArr), '******', 3, 6);
// 建群情况
$jianqun = in_array('group_building', $day_savefild) ? explode(',', $customerList[$c['customer_id']]['ext']['group_building']) : [];
$item['ext35_1'] = (in_array('1对1小群', $jianqun) || in_array('一对一小群', $jianqun)) ? '√' : '';
$item['ext35_2'] = in_array('临时大群', $jianqun) ? '√' : '';
$item['ext35_3'] = in_array('社群', $jianqun) ? '√' : '';
// 直播
$zhibo = in_array('live_broadcast', $day_savefild) ? explode(',', $customerList[$c['customer_id']]['ext']['live_broadcast']) : [];
$item['ext36_1'] = (in_array('一对一直播', $zhibo) || in_array('1对1业务直播', $zhibo)) ? '√' : '';
$item['ext36_2'] = in_array('1对1设计直播', $zhibo) ? '√' : '';
$item['ext36_3'] = in_array('一对多直播', $zhibo) ? '√' : '';
$item['ext37_1'] = !empty($item['ext36_1']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_business'] : '';
$item['ext37_2'] = !empty($item['ext36_2']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_design'] : '';
$item['ext37_3'] = !empty($item['ext36_3']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_personnel'] : '';
$data[] = $item;
$num++;
}
return json(['code' => 0, 'data' => $data, 'count' => $total, 'page' => ceil($total / $limit)]);
}
/**
* 总经办报表表格
*/
public function office_table_data()
{
// 领导获取
$orgLeader = Employee::where([['root_id', '=', $this->root_id], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id, org_id, name');
$orgDealLeader = [];
foreach ($orgLeader as $leader) {
$orgDealLeader[$leader['org_id']][] = $leader['name'];
}
// 查询设计师部门
$designerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 2]])->order('level desc')->column('level, id, name');
// 部门处理
$dealDesignerOrg = [];
foreach ($designerOrg as $org) {
$dealDesignerOrg[$org['level']]['name'][] = $org['name'];
isset($dealDesignerOrg[$org['level']]['leader']) ?: $dealDesignerOrg[$org['level']]['leader'] = [];
if (isset($orgDealLeader[$org['id']]))
$dealDesignerOrg[$org['level']]['leader'] = array_merge($dealDesignerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
}
// 剔除末枝部门
$del = false;
foreach ($dealDesignerOrg as $k => $item) {
if (!$del) {
unset($dealDesignerOrg[$k]);
$del = true;
}
}
// 查询销售部门
$sellerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 1]])->order('level desc')->column('level, id, name');
// 部门处理
$dealSellerOrg = [];
foreach ($sellerOrg as $org) {
$dealSellerOrg[$org['level']]['name'][] = $org['name'];
isset($dealSellerOrg[$org['level']]['leader']) ?: $dealSellerOrg[$org['level']]['leader'] = [];
if (isset($orgDealLeader[$org['id']]))
$dealSellerOrg[$org['level']]['leader'] = array_merge($dealSellerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
}
// 剔除末枝部门
$del = false;
foreach ($dealSellerOrg as $k => $item) {
if (!$del) {
unset($dealSellerOrg[$k]);
$del = true;
}
}
$header = $this->getOfficeHeaders();
view::assign('header1', $header['header1']);
view::assign('header2', $header['header2']);
$org = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['org_type', '=', 1]])->field('id,name')->select()->toArray();
View::assign('org', $org);
$row = [];
foreach ($header['header1'] as $item) {
if (isset($item['child'])) {
foreach ($item['child'] as $c) {
$row[] = $c;
}
} else {
$row[] = $item['field'];
}
}
view::assign('row', json_encode($row));
return View::fetch();
}
/**
* 获取线上跟踪客户记录表头
*/
private function getOfficeHeaders()
{
// 第二行头部
$header2 = [];
// 设计师部门插入头部
$header2[] = ["field" => 'customer_name', "title" => '客户名字', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'phone', "title" => '客户电话', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext39', "title" => '省市区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'community_name', "title" => '楼盘', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext42', "title" => '门牌单元号', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'square', "title" => '面积', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_name', "title" => '业务员', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org', "title" => '业务部门', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader', "title" => '业务经理', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level4', "title" => '业务中心', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level4', "title" => '业务总监', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level3', "title" => '区中区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level3', "title" => '区中区副总', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level2', "title" => '大区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level2', "title" => '大区副总', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_name', "title" => '设计师', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_org', "title" => '设计部门', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader', "title" => '设计经理', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_org_level2', "title" => '设计中心', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader_level2', "title" => '设计总监', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader_level1', "title" => '设计副总', "align" => 'center', "class" => "bgEEECE1"];
// 加微
$header2[] = ["field" => 'house_type1', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'house_type2', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'house_type3', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
// 建群
$header2[] = ["field" => 'ext35_1', "title" => '1对1小群', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext35_2', "title" => '临时大群', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext35_3', "title" => '社群', "align" => 'center', "class" => "bgEEECE1"];
// 直播
$header2[] = ["field" => 'ext36_1', "title" => '1对1业务直播', "align" => 'center', "class" => "bgF2DCDB"];
//$header2[] = ["field" => 'ext37_1', "title" => '业务员', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext36_2', "title" => '1对1设计直播', "align" => 'center', "class" => "bgF2DCDB"];
//$header2[] = ["field" => 'ext37_2', "title" => '设计师', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext36_3', "title" => '一对多直播', "align" => 'center', "class" => "bgF2DCDB"];
//$header2[] = ["field" => 'ext37_3', "title" => '直播人员', "align" => 'center', "class" => "bgF2DCDB"];
// 插入销售部门
$employeeChild = ['employee_name', 'employee_org', 'employee_leader', 'employee_org_level4', 'employee_leader_level4', 'employee_org_level3', 'employee_leader_level3', 'employee_org_level2', 'employee_leader_level2'];
// 设计部门
$designerChild = ['designer_name', 'designer_org', 'designer_leader', 'designer_org_level2', 'designer_leader_level2', 'designer_leader_level1'];
// 第一行头部
$header1 = [];
$header1[] = ["type" => 'key', "field" => 'key', "title" => '序号', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => 'date', "title" => '日期', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['customer_name', 'phone', 'ext39', 'community_name', 'ext42', 'square'], "title" => '客户信息 ', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
$header1[] = ["field" => 'company_name', "title" => '公司', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
$header1[] = ["field" => 'leader', "title" => '负责人', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
$header1[] = ["field" => "", "child" => $employeeChild, "title" => '营销', "align" => 'center', "colspan" => 9, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => $designerChild, "title" => '设计', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['house_type1', 'delivery_month1_1', 'delivery_month1_2', 'delivery_month1_3', 'delivery_month1_4'], "title" => '常规加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "", "child" => ['house_type2', 'delivery_month2_1', 'delivery_month2_2', 'delivery_month2_3', 'delivery_month2_4'], "title" => '社群加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "", "child" => ['house_type3', 'delivery_month3_1', 'delivery_month3_2', 'delivery_month3_3', 'delivery_month3_4'], "title" => '历史加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "add_wechat_time", "title" => '加微时间', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['ext35_1', 'ext35_2', 'ext35_3'], "title" => '建群', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['ext36_1', 'ext36_2', 'ext36_3'], "title" => '直播', "align" => 'center', "colspan" => 3, "class" => "bgF2DCDB"];
$header1[] = ["field" => "measure_type1", "title" => '量房验房', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
return ['header1' => $header1, 'header2' => $header2];
}
/**
* 检测客户是否是外场转内场新客户
*/
public function getIsNewCusomerVisitAfterActivity($date, $customerId)
{
$where = [];
$where[] = ['addtime', '<', $date . ' 23:59:59'];
$where[] = ['customer_id', '=', $customerId];
$state1 = CustomerVisitLog::changeState('已到店', 'chaos');
$where[] = ['state', 'in', $state1];
// 查询日期必须到店
$had = CustomerVisitLog::where($where)->order('addtime desc')->column('addtime, aid');
// 是否多次到店或未到店
if (count($had) != 1) return '';
$lastTime = strtotime($had[0]['addtime']);
// 判断到店日期是否是所选日期
if (date('Y-m-d', $lastTime) != $date) return '';
// 判断是否参加过活动
$aidExit = CustomerVisitLog::where([
['addtime', '<', $date . ' 23:59:59'],
['customer_id', '=', $customerId]
])->order('addtime desc')->column('aid');
if (empty($aidExit)) return '';
$nei = ModelActivity::where([['id', 'in', $aidExit], ['cate', '=', 1]])->count();
if ($nei) return '√';
return '';
}
/**
* 量房转到店
*/
public function measureRoomStore($date, $customer_id)
{
$where = [];
//单日内先量房,在到店
$where[] = ['addtime', 'like', '%' . $date . '%'];
$where[] = ['customer_id', '=', $customer_id];
$state1 = CustomerVisitLog::changeState('确认量房', 'chaos');
$state2 = CustomerVisitLog::changeState('确认到店', 'chaos');
$where[] = ['state', 'in', array_merge($state1, $state2)];
$arr = CustomerVisitLog::where($where)->order('id asc')->column('id,state');
$measure = $store = 0;
foreach ($arr as $k => $v) {
if ($measure == 0 && in_array($v['state'], $state1)) {
$measure = $v['id'];
}
if ($store == 0 && in_array($v['state'], $state2)) {
$store = $v['id'];
}
}
return ($measure > 0 && $store > 0 && $measure < $store) ? '√' : '';
}
/**
* 获取客户跟进数据表表头
*/
public function table()
{
// 领导获取
$orgLeader = Employee::where([['root_id', '=', $this->root_id], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id, org_id, name');
$orgDealLeader = [];
foreach ($orgLeader as $leader) {
$orgDealLeader[$leader['org_id']][] = $leader['name'];
}
// 查询设计师部门
$designerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 2]])->order('level desc')->column('level, id, name');
// 部门处理
$dealDesignerOrg = [];
foreach ($designerOrg as $org) {
$dealDesignerOrg[$org['level']]['name'][] = $org['name'];
isset($dealDesignerOrg[$org['level']]['leader']) ?: $dealDesignerOrg[$org['level']]['leader'] = [];
if (isset($orgDealLeader[$org['id']]))
$dealDesignerOrg[$org['level']]['leader'] = array_merge($dealDesignerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
}
// 剔除末枝部门
$del = false;
foreach ($dealDesignerOrg as $k => $item) {
if (!$del) {
unset($dealDesignerOrg[$k]);
$del = true;
}
}
// 查询销售部门
$sellerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 1]])->order('level desc')->column('level, id, name');
// 部门处理
$dealSellerOrg = [];
foreach ($sellerOrg as $org) {
$dealSellerOrg[$org['level']]['name'][] = $org['name'];
isset($dealSellerOrg[$org['level']]['leader']) ?: $dealSellerOrg[$org['level']]['leader'] = [];
if (isset($orgDealLeader[$org['id']]))
$dealSellerOrg[$org['level']]['leader'] = array_merge($dealSellerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
}
// 剔除末枝部门
$del = false;
foreach ($dealSellerOrg as $k => $item) {
if (!$del) {
unset($dealSellerOrg[$k]);
$del = true;
}
}
// // 设计师部门数量
// $designerCount = count($dealDesignerOrg) * 2 + 3;
// $sellerCount = count($dealSellerOrg) * 2 + 3;
// View::assign('dealDesignerOrg', $dealDesignerOrg);
// View::assign('designerCount', $designerCount);
// View::assign('dealSellerOrg', $dealSellerOrg);
// View::assign('sellerCount', $sellerCount);
$header = $this->getVisitLogHeaders();
view::assign('header1', $header['header1']);
view::assign('header2', $header['header2']);
$org = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['org_type', '=', 1]])->field('id,name')->select()->toArray();
View::assign('org', $org);
$row = [];
foreach ($header['header1'] as $item) {
if (isset($item['child'])) {
foreach ($item['child'] as $c) {
$row[] = $c;
}
} else {
$row[] = $item['field'];
}
}
view::assign('row', json_encode($row));
return View::fetch('table_data');
}
private function tree($data, $pid = 0)
{
$new_arr = [];
foreach ($data as $k => $v) {
if ($v['pid'] == $pid) {
$children = $this->tree($data, $v['id']);
$v['children'] = $children;
$new_arr[] = $v;
}
}
return $new_arr;
}
/*
* 人员树
*/
public function get_person()
{
$ids = input('id', '');
$root_id = request()->employee->root_id;
//树形
$where = [
['path', 'like', $root_id . '-%'],
['status', '=', 1]
];
$allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray();
$tree = $this->tree($allnodes, 0);
return json($tree);
}
/**
* 获取线上跟踪客户记录表头
*/
private function getVisitLogHeaders()
{
// 第二行头部
$header2 = [];
// 设计师部门插入头部
$header2[] = ["field" => 'customer_name', "title" => '客户名字', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'phone', "title" => '客户电话', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext39', "title" => '省市区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'community_name', "title" => '楼盘', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext42', "title" => '门牌单元号', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'square', "title" => '面积', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_name', "title" => '业务员', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org', "title" => '业务部门', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader', "title" => '业务经理', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level4', "title" => '业务中心', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level4', "title" => '业务总监', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level3', "title" => '区中区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level3', "title" => '区中区副总', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_org_level2', "title" => '大区', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'employee_leader_level2', "title" => '大区副总', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_name', "title" => '设计师', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_org', "title" => '设计部门', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader', "title" => '设计经理', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_org_level2', "title" => '设计中心', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader_level2', "title" => '设计总监', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'designer_leader_level1', "title" => '设计副总', "align" => 'center', "class" => "bgEEECE1"];
// 加微
$header2[] = ["field" => 'house_type1', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month1_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'house_type2', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month2_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'house_type3', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
$header2[] = ["field" => 'delivery_month3_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
// 建群
$header2[] = ["field" => 'ext35_1', "title" => '1对1小群', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext35_2', "title" => '临时大群', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'ext35_3', "title" => '社群', "align" => 'center', "class" => "bgEEECE1"];
// 直播
$header2[] = ["field" => 'ext36_1', "title" => '1对1业务直播', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext37_1', "title" => '业务员', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext36_2', "title" => '1对1设计直播', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext37_2', "title" => '设计师', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext36_3', "title" => '一对多直播', "align" => 'center', "class" => "bgF2DCDB"];
$header2[] = ["field" => 'ext37_3', "title" => '直播人员', "align" => 'center', "class" => "bgF2DCDB"];
// 量房验房
$header2[] = ["field" => 'measure_type2', "title" => '量房验房报告', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'measure_type3', "title" => '客户需求表', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'leader_visit', "title" => '经理是否回访', "align" => 'center', "class" => "bgEEECE1"];
// 到店
$header2[] = ["field" => 'outfield_activities', "title" => '外场活动到店', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'outfield_infield', "title" => '内场直接到店', "align" => 'center', "class" => "bgEEECE1"];
//见面方式
$header2[] = ["field" => 'actmeet_type1', "title" => '外场活动', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'actmeet_type2', "title" => '茶楼见面', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'actmeet_type3', "title" => '网谈登记', "align" => 'center', "class" => "bgEEECE1"];
// 管理到店数据
$header2[] = ["field" => 'deposit_money2', "title" => '线上定金', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'deposit_money1', "title" => '线下定金', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'package', "title" => '爆品定金(10%以上)', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'measure_type1', "title" => '量房验房', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'visit_number1', "title" => '一次到店
(内场到店)', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'visit_number2', "title" => '二次到店', "align" => 'center', "class" => "bgEEECE1"];
$header2[] = ["field" => 'visit_number3', "title" => '三次及以上到店', "align" => 'center', "class" => "bgEEECE1"];
// 插入销售部门
$employeeChild = ['employee_name', 'employee_org', 'employee_leader', 'employee_org_level4', 'employee_leader_level4', 'employee_org_level3', 'employee_leader_level3', 'employee_org_level2', 'employee_leader_level2'];
// 设计部门
$designerChild = ['designer_name', 'designer_org', 'designer_leader', 'designer_org_level2', 'designer_leader_level2', 'designer_leader_level1'];
// 第一行头部
$header1 = [];
$header1[] = ["type" => 'key', "field" => 'key', "title" => '序号', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => 'date', "title" => '日期', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['customer_name', 'phone', 'ext39', 'community_name', 'ext42', 'square'], "title" => '客户信息 ', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
$header1[] = ["field" => 'company_name', "title" => '公司', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
$header1[] = ["field" => 'leader', "title" => '负责人', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
$header1[] = ["field" => "", "child" => $employeeChild, "title" => '营销', "align" => 'center', "colspan" => 9, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => $designerChild, "title" => '设计', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['house_type1', 'delivery_month1_1', 'delivery_month1_2', 'delivery_month1_3', 'delivery_month1_4'], "title" => '常规加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "", "child" => ['house_type2', 'delivery_month2_1', 'delivery_month2_2', 'delivery_month2_3', 'delivery_month2_4'], "title" => '社群加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "", "child" => ['house_type3', 'delivery_month3_1', 'delivery_month3_2', 'delivery_month3_3', 'delivery_month3_4'], "title" => '历史加微
(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
$header1[] = ["field" => "add_wechat_time", "title" => '加微时间', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => "plan_month_day", "title" => '高意向到店客户
(1个月内装修)', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['ext35_1', 'ext35_2', 'ext35_3'], "title" => '建群', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
$header1[] = ["field" => "", "child" => ['ext36_1', 'ext37_1', 'ext36_2', 'ext37_2', 'ext36_3', 'ext37_3'], "title" => '直播', "align" => 'center', "colspan" => 6, "class" => "bgF2DCDB"];
$header1[] = ["field" => "", "child" => ['measure_type2', 'measure_type3', 'leader_visit'], "title" => '量房验房', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
$header1[] = ["field" => '', "child" => ['outfield_activities', 'visit_number1'], "title" => '到店', "align" => 'center', "colspan" => 2, "class" => "bgFDE9D9"];
$header1[] = ["field" => '', "child" => ['actmeet_type1', 'actmeet_type2', 'actmeet_type3'], "title" => '见面方式', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
$header1[] = ["field" => '', "child" => ['deposit_money2', 'deposit_money1', 'package', 'measure_type1', 'visit_number1', 'visit_number2', 'visit_number3'], "title" => '管理到店数据', "align" => 'center', "colspan" => 7, "class" => "bgFDE9D9"];
$header1[] = ["field" => 'number_of_visitors', "title" => '到店人数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
$header1[] = ["field" => 'signature_money', "title" => '合同价', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
return ['header1' => $header1, 'header2' => $header2];
}
/**
* 渠道统计
* 昨日,近一周,近一月
*/
public function channelStatistics()
{
return View::fetch();
}
/**
* 渠道统计
* 昨日,近一周,近一月
*/
public function channelStatisticsList()
{
$request = request();
$root_id = $request->employee->root_id;
$team_orgs = orgSubIds($request->employee->org_id);
$param = Request()->only(['page' => 1, 'limit' => 10, 'start_date', 'keyword']);
$startDateTime = date('Y-m-d H:i:s', 0);
$endDateTime = date('Y-m-d H:i:s', time());
if (!empty($param['start_date'])) {
$count_dates = explode(' - ', $param['start_date']);
$startDateTime = date('Y-m-d H:i:s', strtotime($count_dates[0]));
$endDateTime = date('Y-m-d H:i:s', strtotime($count_dates[1]) + 86400);
}
$soud_where[] = ['root_id', '=', $root_id];
setCondition($param, ['keyword', 'source'], ['like', '%VALUE%'], $soud_where);
$org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
if (!empty($param['source_id'])) $soud_where[] = ['id', 'in', explode(',', $param['source_id'])];
$soud_list = CustomerSource::field('id,source as name')->where($soud_where)->select()->toArray();
$where[] = ['addtime', 'between', [$startDateTime, $endDateTime]];
if (!empty($param['employee_id'])) $org_employee = [$param['employee_id']];
$where[] = ['employee_id', 'in', $org_employee];
$exten_where[] = ['root_id', '=', $root_id];
$exten_where[] = ['addtime', 'between', [$startDateTime, $endDateTime]];
if (!empty($param['employee_id'])) $exten_where[] = ['id', '=', 0];
$exten_list = CustomerExtension::with(['source' => function ($query) {
$query->field('id,source');
}])->where($exten_where)->select()->toArray();
$t = ['number' => 0, 'clue_count' => 0, 'valid_count' => 0, 'meet_count' => 0, 'delivery_count' => 0, 'order_count' => 0, 'maika_count' => 0, 'order_money' => 0, 'meet_grawth' => '0%', 'valid_grawth' => '0%', 'delivery_grawth' => '0%', 'order_grawth' => '0%'];
$state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
$state2 = CustomerVisitLog::changeState('已交定', 'chaos');
$state3 = CustomerVisitLog::changeState('已签单', 'chaos');
$state5 = Customer::changeState('无效', 'chaos');
$state6 = Customer::changeState('待确认', 'chaos');
$state7 = CustomerVisitLog::changeState('已量房', 'chaos');
$state8 = CustomerVisitLog::changeState('已到场', 'chaos');
$state9 = CustomerVisitLog::changeState('已卖卡', 'chaos');
$ismeet = [];
foreach ($soud_list as $key => $val) {
$soud_list[$key]['employee_id'] = !empty($param['employee_id']) ? $param['employee_id'] : '';
$exten_money = $exten_show = $exten_click = $clue_cont = $valid = $meet = $deposit = $sign = $basic_money = $maika_count = 0;
$validids = $cusids = [];
$customer_list = Customer::where($where)->where([['source_id', '=', $val['id']], ['state', 'not in', $state6]])->column('state', 'id');
$clue_ids = Customer::where($where)->where([['source_id', '=', $val['id']]])->column('id');
$clue_cont = count($clue_ids);
foreach ($customer_list as $k => $v) {
if (!in_array($v, $state5)) {
$cusids[] = $k;
$valid++;
}
}
unset($customer_list);
$validids = $cusids;
//查询无效的客户
$invalidcus = CustomerInvalidLog::where([['employee_id', 'in', $org_employee], ['root_id', '=', $root_id], ['source_id', '=', $val['id']], ['cus_addtime', 'between', [$startDateTime, $endDateTime]]])->column('customer_id,employee_id,status');
foreach ($invalidcus as $k => $v) {
if (!in_array($v['customer_id'], $validids)) {
if ($v['status'] == 1) {
$valid++;
$validids[] = $v['customer_id'];
}
}
if (!in_array($v['customer_id'], $clue_ids)) {
$clue_cont++;
$clue_ids[] = $v['customer_id'];
}
}
unset($clue_ids, $validids, $cusids);
foreach ($exten_list as $k => $v) {
if ($v['source_id'] == $val['id']) {
$exten_money += $v['money'];
$exten_show += $v['show'];
$exten_click += $v['click'];
}
}
$visitlog_list = CustomerVisitLog::hasWhere('customer', ['source_id' => $val['id']])
->where([['CustomerVisitLog.customer_employee_id', 'in', $org_employee], ['CustomerVisitLog.state', '<>', 'null'], ['CustomerVisitLog.confirm_date', 'between', [$startDateTime, $endDateTime]]])
->order('CustomerVisitLog.addtime desc')->column('CustomerVisitLog.id,CustomerVisitLog.customer_id,CustomerVisitLog.state,CustomerVisitLog.money');
$metids = [];
foreach ($visitlog_list as $k => $v) {
if (in_array($v['state'], array_merge($state1, $state7, $state8))) $metids[] = $v['customer_id'];
}
$yesids = !empty(array_unique($metids)) ? $this->selBeforelog(array_unique($metids), $startDateTime) : [];
$depositIds = []; //交定
$signIds = []; //签单
$maikaIds = []; //卖卡
foreach ($visitlog_list as $k => $v) {
if (in_array($v['state'], array_merge($state1, $state7, $state8)) && !in_array($v['customer_id'], $ismeet) && in_array($v['customer_id'], $yesids)) {
$meet++;
$ismeet[] = $v['customer_id'];
}
if (in_array($v['state'], $state2) && !in_array($v['customer_id'], $depositIds)) {
$deposit++;
$depositIds[] = $v['customer_id'];
}
if (in_array($v['state'], $state3) && !in_array($v['customer_id'], $signIds)) {
$sign++;
$basic_money += $v['money'];
$signIds[] = $v['customer_id'];
}
if (in_array($v['state'], $state9) && !in_array($v['customer_id'], $maikaIds)) {
$maika_count++;
$maikaIds[] = $v['customer_id'];
}
}
unset($visitlog_list);
$soud_list[$key]['source'] = $val['name'];
$soud_list[$key]['clue_count'] = $clue_cont;
$soud_list[$key]['valid_count'] = $valid;
$soud_list[$key]['meet_count'] = $meet;
$soud_list[$key]['delivery_count'] = $deposit;
$soud_list[$key]['order_count'] = $sign;
$soud_list[$key]['maika_count'] = $maika_count;
$soud_list[$key]['order_money'] = $basic_money;
$soud_list[$key]['meet_grawth'] = !empty($meet) && !empty($clue_cont) ? round($meet / $clue_cont * 100, 2) . '%' : '0%';
$soud_list[$key]['valid_grawth'] = $valid && $clue_cont ? round($valid / $clue_cont * 100, 2) . '%' : '0%';
$soud_list[$key]['delivery_grawth'] = !empty($deposit) && !empty($meet) ? round($deposit / $meet * 100, 2) . '%' : '0%';
$soud_list[$key]['order_grawth'] = !empty($sign) && !empty($meet) ? round($sign / $meet * 100, 2) . '%' : '0%';
//总数
$t['number']++;
$t['clue_count'] += $clue_cont;
$t['valid_count'] += $valid;
$t['meet_count'] += $meet;
$t['delivery_count'] += $deposit;
$t['order_count'] += $sign;
$t['maika_count'] += $maika_count;
$t['order_money'] += $basic_money;
}
$t['meet_grawth'] = ($t['clue_count'] == 0 || $t['meet_count'] == 0) ? '0%' : round($t['meet_count'] / $t['clue_count'] * 100, 2) . '%';
$t['valid_grawth'] = ($t['valid_count'] == 0 || $t['clue_count'] == 0) ? '0%' : round($t['valid_count'] / $t['clue_count'] * 100, 2) . '%';
$t['delivery_grawth'] = ($t['delivery_count'] == 0 || $t['meet_count'] == 0) ? '0%' : round($t['delivery_count'] / $t['meet_count'] * 100, 2) . '%';
$t['order_grawth'] = ($t['order_count'] == 0 || $t['meet_count'] == 0) ? '0%' : round($t['order_count'] / $t['meet_count'] * 100, 2) . '%';
return json(['code' => 0, 'data' => $soud_list, 'header' => $t, 'count' => 0, 'msg' => '获取成功']);
}
public function selBeforelog($metids, $firstday)
{
$statday = '2021-12-01 00:00:00';
$state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
$state2 = CustomerVisitLog::changeState('已到场', 'chaos');
$state3 = CustomerVisitLog::changeState('已量房', 'chaos');
$q_where[] = ['customer_id', 'in', $metids];
$q_where[] = ['state', 'in', array_merge($state1, $state2, $state3)];
$q_where[] = ['addtime', 'between', [$statday, $firstday]];
$qianvslog = CustomerVisitLog::where($q_where)->group('customer_id')->column('customer_id');
$yesids = array_diff($metids, $qianvslog);
return $yesids;
}
/**
* 渠道统计合计
*/
public function channelStatisticsTotal()
{
//合计
$param = Request()->only(['start_date' => '', 'end_date' => '', 'keyword' => '']);
$root_id = request()->employee->root_id;
$startDateTime = date('Y-m-d H:i:s', 0);
$endDateTime = date('Y-m-d H:i:s', time());
if (!empty($param['start_date'])) {
$count_dates = explode(' - ', $param['start_date']);
$startDateTime = date('Y-m-d H:i:s', strtotime($count_dates[0]));
$endDateTime = date('Y-m-d H:i:s', strtotime($count_dates[1]) + 86400);
}
//渠道列表
$where[] = ['root_id', '=', $root_id];
if ($param['keyword']) {
$where[] = ['source', 'like', '%' . $param['keyword'] . '%'];
}
//有效量,确认到场,确认量房,确认到店,交定签单
$state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
$state2 = CustomerVisitLog::changeState('确认量房', 'chaos');
$state3 = CustomerVisitLog::changeState('确认到场', 'chaos');
$state4 = CustomerVisitLog::changeState('交定', 'chaos');
$state5 = CustomerVisitLog::changeState('签单', 'chaos');
$state6 = CustomerVisitLog::changeState('待确认', 'chaos');
$state7 = CustomerVisitLog::changeState('无效', 'chaos');
$state8 = CustomerVisitLog::changeState('已卖卡', 'chaos');
// 见面状态
$make_state0 = array_merge($state1, $state2, $state3);
$make_state1 = array_merge($state1, $state2, $state3, $state4, $state5);
$make_state2 = array_merge($state1, $state2, $state3, $state4, $state5, $state8);
//员工id
$eids = Employee::where([['root_id', '=', $root_id], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
//渠道ID
$sourceId = CustomerSource::where($where)->column('id');
//查询搜索日期之前的见面量
$beforeId = [];
if (!empty($param['start_date'])) {
$vis_where = [['state', 'in', $make_state2], ['addtime', '<', $startDateTime]];
$beforeList = Customer::with(['visitLog' => function ($query) use ($vis_where) {
$query->where($vis_where)->field('customer_id,state,money');
}])
->where([['source_id', 'in', $sourceId], ['employee_id', 'in', $eids]])
->where(function ($query) {
$not_sure = Customer::changeState('待确认', 'chaos');
$or1[] = ['crm_res_id', 'null', null];
$or2[] = ['crm_res_id', 'not null', null];
$or2[] = ['state', 'not in', $not_sure];
$query->whereOr([$or1, $or2]);
})->field('id,source_id')->select()->toArray();
foreach ($beforeList as $v) {
if ($v['visitLog']) $beforeId[] = $v['id'];
}
}
$vis_where = [['state', 'in', $make_state2], ['addtime', '>=', $startDateTime], ['addtime', '<', $endDateTime]];
$customerList = Customer::with(['visitLog' => function ($query) use ($vis_where) {
$query->where($vis_where)->field('customer_id,state,money');
}])
->where([['source_id', 'in', $sourceId], ['employee_id', 'in', $eids]])
->where(function ($query) {
$not_sure = Customer::changeState('待确认', 'chaos');
$or1[] = ['crm_res_id', 'null', null];
$or2[] = ['crm_res_id', 'not null', null];
$or2[] = ['state', 'not in', $not_sure];
$query->whereOr([$or1, $or2]);
})->field('id,source_id')->select()->toArray();
$data['source_count'] = count($sourceId); //渠道总数
$data['clue_count'] = 0; //线索总数
$data['valid_count'] = 0; //有效线索数量
$data['meet_count'] = 0; //见面数量
$data['delivery_count'] = 0; //交定数量
$data['order_count'] = 0; //签单数量
$data['maika_count'] = 0; //卖卡数量
$tmp_valid_count = $tmp_meet_count = $tmp_delivery_count = $tmp_order_count = $tmp_maika_count = [];
foreach ($customerList as $v) {
$data['clue_count'] += 1; //线索量
$tmp_valid_count[$v['source_id']][] = 0; //有效量
$tmp_meet_count[$v['source_id']][] = 0; //见面
$tmp_delivery_count[$v['source_id']][] = 0; //交定
$tmp_order_count[$v['source_id']][] = 0; //签单量
$tmp_maika_count[$v['source_id']][] = 0; //卖卡
foreach ($v['visitLog'] as $tmp) {
if (!in_array($v['id'], $tmp_valid_count[$v['source_id']]) && in_array($tmp['state'], $make_state2)) {
$tmp_valid_count[$v['source_id']][] = $v['id']; //有效量
$data['valid_count'] += 1;
}
if (!in_array($v['id'], $tmp_meet_count[$v['source_id']]) && in_array($tmp['state'], $make_state0) && !in_array($v['id'], $beforeId)) {
$tmp_meet_count[$v['source_id']][] = $v['id']; //见面
$data['meet_count'] += 1;
}
if (!in_array($v['id'], $tmp_delivery_count[$v['source_id']]) && in_array($tmp['state'], $state4)) {
$tmp_delivery_count[$v['source_id']][] = $v['id']; //交定
$data['delivery_count'] += 1;
}
if (!in_array($v['id'], $tmp_order_count[$v['source_id']]) && in_array($tmp['state'], $state5)) {
$tmp_order_count[$v['source_id']][] = $v['id']; //签单量
$data['order_count'] += 1;
}
if (!in_array($v['id'], $tmp_maika_count) && in_array($tmp['state'], $state8)) {
$tmp_maika_count[$v['source_id']][] = $v['id']; //卖卡
$data['maika_count'] += 1;
}
}
}
$data['valid_grawth'] = $data['clue_count'] == 0 ? '0%' : round($data['valid_count'] / $data['clue_count'] * 100, 2) . '%'; //有效率
if ($data['valid_count'] > 0) {
$data['meet_grawth'] = round($data['meet_count'] / $data['valid_count'] * 100, 2) . '%';
} else {
$data['meet_grawth'] = '0%';
}
if ($data['meet_count'] > 0) {
$data['delivery_grawth'] = round($data['delivery_count'] / $data['meet_count'] * 100, 2) . '%';
$data['order_grawth'] = round($data['order_count'] / $data['meet_count'] * 100, 2) . '%';
} else {
$data['delivery_grawth'] = '0%';
$data['order_grawth'] = '0%';
}
return json(['code' => 0, 'data' => $data, 'msg' => '操作成功']);
}
/**
* 线上巡店查询结果导出
*/
public function export($m)
{
$typeList = [
'newStatisticsExport' => ['headerMethod' => 'getNewVisitLogHeaders', 'dataMethod' => 'new_customer_statistics'],
'officeStatisticsExport' => ['headerMethod' => 'getOfficeHeaders', 'dataMethod' => 'officeStatistics'],
'statisticsExport' => ['headerMethod' => 'getVisitLogHeaders', 'dataMethod' => 'customer_statistics'],
];
if (!isset($typeList[$m])) {
json(['code' => 1, 'msg' => '类型不存在'])->send();
return;
}
$param = Request::post();
$filename = uniqid();
$log = [
'root_id' => request()->employee->root_id,
'file' => $filename . '.csv',
'type' => $m,
'employee_id' => request()->employee->id,
'search' => json_encode($param)
];
$exportLog = ExportLog::create($log);
json(['code' => 0, 'msg' => '导出中,请稍后在记录中查看'])->send();
$headerMethod = $typeList[$m]['headerMethod'];
$dataMethod = $typeList[$m]['dataMethod'];
$header = $this->$headerMethod();
$row = [];
$title = [];
foreach ($header['header1'] as $item) {
if (isset($item['colspan'])) {
for ($s = 0; $s < $item['colspan']; $s++) {
$h2 = array_shift($header['header2']);
$title[] = strip_tags($h2['title']);
}
} else {
$title[] = strip_tags($item['title']);
}
if (isset($item['child'])) {
foreach ($item['child'] as $k) {
$row[] = $k;
}
} else {
$row[] = $item['field'];
}
}
touch('../download/' . $filename . '.csv');
$fp = fopen('../download/' . $filename . '.csv', 'a');
fputcsv($fp, $title);
$n = 2000;
$p = 1;
do {
$data = $this->$dataMethod($p, $n);
$data = $data->getData();
foreach ($data['data'] as $rowData) {
$r = [];
foreach ($row as $field) {
$r[] = isset($rowData[$field]) ? $rowData[$field] : '';
}
fputcsv($fp, $r);
}
$p++;
} while ($n * ($p - 1) < $data['count']);
fclose($fp);
// 文件上传
$rs = ossUpload($m . '/' . $filename . '.csv', './download/' . $filename . '.csv');
if ($rs) {
unlink('../download/' . $filename . '.csv');
}
$exportLog->save(['state' => 1]);
}
/**
* 线上巡店查询结果导出
*/
public function exportList()
{
$param = request()->param();
$methodtype = 'statisticsExport';
if (!empty($param['methodtype'])) $methodtype = $param['methodtype'];
if (!in_array($methodtype, ['statisticsExport', 'newStatisticsExport', 'officeStatisticsExport'])) return json(['code' => 1, 'msg' => '方法参数错误']);
if (!request()->isAjax()) {
View::assign('methodtype', $methodtype);
return View::fetch();
}
$count = AsyncTask::where([['root_id', '=', request()->employee->root_id], ['method', '=', $methodtype]])->count();
$data = AsyncTask::field('params, addtime,state,complete_time,result')
->where([['root_id', '=', request()->employee->root_id], ['method', '=', $methodtype]])
->order('id desc')
->page($param['page'], $param['limit'])
->select();
if (!empty($data)) $data = $data->toArray();
$org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->column('name', 'id');
foreach ($data as &$item) {
if (isset($item['params']['sdate']) && $item['params']['sdate'] != '') $item['params']['sdate'] = date('Y-m-d', strtotime($item['params']['sdate']));
if (isset($item['params']['edate']) && $item['params']['edate'] != '') $item['params']['edate'] = date('Y-m-d', strtotime($item['params']['edate']));
if (isset($item['params']['org_id']) && isset($org[$item['params']['org_id']])) $item['params']['org_id'] = $org[$item['params']['org_id']];
}
return json(['code' => 0, 'count' => $count, 'data' => $data]);
}
/**
* daochu文件下载
*/
public function downloadXls($path, $methodtype)
{
if (in_array($methodtype, ['statisticsExport', 'newStatisticsExport', 'officeStatisticsExport', 'behaviorStatisticsData', 'highBehaviorStatisticsData', 'manageStatisticsData', 'customer_list_data', 'generalStatisticsData'])) {
$had = ExportLog::where([['root_id', '=', request()->employee->root_id], ['type', '=', $methodtype], ['file', '=', $path], ['state', '=', 1]])->find();
$floder = 'statisticsExport/';
if (in_array($methodtype, ['newStatisticsExport'])) $floder = $methodtype . '/';
} else {
return json(['code' => 1, 'msg' => '方法参数错误']);
}
$ossExit = ossFileExist($floder . $path);
if (empty($had) || (!file_exists('../download/' . $path) && !$ossExit)) return View::display('', ['msg' => '文件不存在或数据处理中,请稍后再试']);
// 检测文件
if ($ossExit) {
$path = 'https://' . config('app.ali_oss_bindurl') . '/' . $floder . $path;
return redirect($path);
}
return download('../download/' . $path, $path);
}
/**
* 客户管理数据统计
*/
public function customer_list()
{
$root_id = request()->employee->root_id;
$org = Org::where([['path', 'like', $root_id . '-%'], ['org_type', '=', 1]])->field('id,name')->select()->toArray();
View::assign('org', $org);
//渠道来源下拉
$source = CustomerSource::where('root_id', $root_id)->column('id,source');
View::assign('source', $source);
return View::fetch();
}
/**
* 客户统计列表数据
*/
public function customer_list_data($page = 1, $limit = 20)
{
$root_id = request()->employee->root_id;
$source_id = input('source_id', 0);
$employee_name = input('employee_name', '', 'trim');
$customer_type = input('customer_type', '', 'intval');
switch ($customer_type) {
case 1:
$where[] = ['crm_res_id', '>', 0];
$where[] = ['remark', '<>', '公海获取'];
break;
case 2:
$where[] = ['remark', '=', '活动报名建档'];
break;
case 3:
$where[] = ['agents_id', '>', 0];
break;
case 4:
$where[] = ['crm_res_id', 'NULL', null];
$where[] = ['remark', '<>', '活动报名建档'];
$where[] = ['remark', '<>', '公海获取'];
$where[] = ['agents_id', 'NULL', null];
break;
case 5:
$where[] = ['remark', '=', '公海获取'];
break;
default:
break;
}
if ($employee_name) {
$employee_ids = Employee::where([['name', 'like', '%' . $employee_name . '%'], ['root_id', '=', $root_id]])->column('id');
$where[] = ['employee_id', 'in', $employee_ids];
}
$start_date = input('start_date', '', 'trim');
$end_date = input('end_date', '', 'trim');
if ($start_date) {
$count_dates = explode(' - ', $start_date);
$start_date = $count_dates[0];
$end_date = $count_dates[1];
} else {
$start_date = $end_date = '';
}
if ($start_date && $end_date) {
$where[] = ['addtime', 'between', [$start_date . ' 00:00:00', $end_date . ' 23:59:59']];
} elseif ($start_date) {
$where[] = ['addtime', '>', $start_date . ' 00:00:00'];
} elseif ($end_date) {
$where[] = ['addtime', '<', $end_date . ' 23:59:59'];
}
$org_id = input('select_org_id', '', 'trim');
$org_ids = orgSubIds($root_id);
if ($org_id) {
$org_id = explode(',', $org_id);
$sel_org_ids = [];
foreach ($org_id as $k => $v) {
if (in_array($v, $org_ids)) {
$sel_org_ids[] = $v;
}
}
$where[] = ['org_id', 'in', $sel_org_ids];
} else {
$where[] = ['org_id', 'in', $org_ids];
}
if ($source_id) $where[] = ['source_id', '=', $source_id];
$where[] = ['died', '<>', 2];
$where[] = ['employee_id', 'not null', ''];
$where[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
//2023-02-26 只统计有效客户 和CRM客户管理-店面客户管理保持一致
$state11 = Customer::changeState('待确认', 'chaos');
$state22 = Customer::changeState('无效', 'chaos');
$model = Customer::where($where)->where(function ($query) {
$state11 = Customer::changeState('待确认', 'chaos');
$state22 = Customer::changeState('无效', 'chaos');
$or1[] = ['crm_res_id', 'null', null];
$or1[] = ['state', 'not in', array_merge($state11, $state22)];
$or2[] = ['crm_res_id', '>', 0];
$or2[] = ['state', 'not in', $state22];
$query->whereOr([$or1, $or2]);
});
$count = $model->count();
$list = $model->with(['employee', 'org', 'designer', 'source'])->page($page, $limit)->order('addtime desc')->select()->each(function ($item) {
if ($item['remark'] == '公海获取') {
$customer_type = '公海获取';
} elseif ($item['agents_id']) {
$customer_type = '装修推荐管';
} elseif ($item['remark'] == '活动报名建档') {
$customer_type = '活动报名';
} elseif ($item['crm_res_id']) {
$customer_type = '资源库';
} else {
$customer_type = '自建';
}
$item['customer_type'] = $customer_type;
if (empty($item['name'])) {
$item['name'] = '未知';
}
if (empty($item['community_name'])) {
$item['community_name'] = '未知';
}
if (empty($item['house_type'])) {
$item['house_type'] = '未知';
}
if (empty($item['square'])) {
$item['square'] = '未知';
}
$item['phone'] = substr_replace($item['phone'], '******', 3, 6);
$item['signed_money_data'] = $item->getData('signed_money');
// 指派客户经理及设计师
$assigned_personal_manager_ids = [];
if ($item['assigned_personnel']) {
$assigned_personal = explode(',', $item['assigned_personnel']);
$assigned_personal_list = Employee::with('org')->where('id', 'in', $assigned_personal)->select()->toArray();
$assigned_personal_designer = [];
$assigned_personal_manager = [];
$assigned_personal_designer_org = [];
foreach ($assigned_personal_list as $k => $v) {
if ($v['org']['org_type'] == 2) {
$assigned_personal_designer[] = $v['name'];
$assigned_personal_designer_org[] = $v['org']['name'];
} else {
$assigned_personal_manager[] = $v['name'];
$assigned_personal_manager_ids[] = $v['id'];
}
}
$item['assigned_personal_manager'] = $assigned_personal_manager ? implode('、', $assigned_personal_manager) : '无';
$item['assigned_personal_designer'] = $assigned_personal_designer ? implode('、', $assigned_personal_designer) : '无';
$item['assigned_personal_designer_org'] = $assigned_personal_designer_org ? implode('、', $assigned_personal_designer_org) : '无';
} else {
$item['assigned_personal_manager'] = '无';
$item['assigned_personal_designer'] = '无';
$item['assigned_personal_designer_org'] = '无';
}
// 未跟踪天数
$last_visit_log = CustomerVisitLog::where('customer_id', '=', $item['id'])->findOrEmpty();
if (!$last_visit_log->isEmpty()) {
$time_long = time() - strtotime($last_visit_log['addtime']);
} else {
$time_long = time() - strtotime($item['addtime']);
}
$no_visit_day = ceil($time_long / (24 * 3600));
$item['no_visit_day'] = $no_visit_day;
// 是否加微
$ext = json_decode($item['ext'], true);
$item['wechat'] = '否'; // 是否加微
$item['add_wechat_type'] = '无'; // 加微类型
$item['add_wechat_time'] = '无'; // 加微时间
$item['plan_issuing_date'] = '无'; //出方案时间
$item['drawing_date'] = '无'; // 出图时间
$item['group_building_date'] = '无'; // 建群时间
if (!empty($ext)) {
foreach ($ext as $v) {
if (isset($v['keyname']) && $v['keyname'] == 'wechat' && !empty($v['value'])) {
$item['wechat'] = CustomerPortraitFieldSelect::where('id', $v['value'])->value('name');
} elseif (isset($v['keyname']) && $v['keyname'] == 'add_wechat_time' && !empty($v['value'])) {
$item['wechat'] = '是';
} elseif (isset($v['keyname']) && $v['keyname'] == 'add_wechat_type' && !empty($v['value'])) {
$item['wechat'] = '是';
}
if (isset($v['keyname']) && $v['keyname'] == 'add_wechat_time' && !empty($v['value'])) {
$item['add_wechat_time'] = $v['value'];
}
if (isset($v['keyname']) && $v['keyname'] == 'add_wechat_type' && !empty($v['value'])) {
$item['add_wechat_type'] = CustomerPortraitFieldSelect::where('id', $v['value'])->value('name');
}
if (isset($v['keyname']) && $v['keyname'] == 'plan_issuing_date' && !empty($v['value'])) {
$item['plan_issuing_date'] = $v['value'];
}
if (isset($v['keyname']) && $v['keyname'] == 'drawing_date' && !empty($v['value'])) {
$item['drawing_date'] = $v['value'];
}
if (isset($v['keyname']) && $v['keyname'] == 'group_building_date' && !empty($v['value'])) {
$item['group_building_date'] = $v['value'];
}
}
}
$lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
$lf_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $lf_state]])->order('addtime asc')->select()->toArray();
if (!empty($lf_list)) {
$item['is_liangfang'] = '是';
$item['liangfang_date'] = $lf_list[0]['addtime'];
$item['once_liangfang_days'] = ceil((strtotime($lf_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600));
} else {
$item['is_liangfang'] = '否';
$item['liangfang_date'] = '无';
$item['once_liangfang_days'] = '无';
}
// 到店
$dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
$dd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $dd_state]])->order('addtime asc')->select()->toArray();
$item['once_daodian'] = '否';
$item['daodian_date'] = '无';
$item['once_daodian_days'] = '无';
$item['once_daodian_shop'] = '无';
$item['twice_daodian_date'] = '无'; //二次到店时间
$item['twice_daodian_days'] = '无'; // 二次到店周期 二次到店时间 - 一次到店时间
$item['twice_daodian_shop'] = '无';
if (!empty($dd_list)) {
$item['once_daodian'] = '是';
$item['daodian_date'] = $dd_list[0]['addtime'];
$item['once_daodian_days'] = ceil((strtotime($dd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600));
if (!empty($dd_list[0]['shop_id'])) {
$shop_name = Shop::where('id', $dd_list[0]['shop_id'])->findOrEmpty();
if ($shop_name->isEmpty()) {
$item['once_daodian_shop'] = '未知';
} else {
$item['once_daodian_shop'] = $shop_name['name'];
}
}
// 二次到店
if (count($dd_list) > 1) {
$item['twice_daodian_date'] = $dd_list[1]['addtime'];
$item['twice_daodian_days'] = ceil((strtotime($dd_list[1]['addtime']) - strtotime($dd_list[0]['addtime'])) / (24 * 3600));
$shop_name = Shop::where('id', $dd_list[1]['shop_id'])->findOrEmpty();
if ($shop_name->isEmpty()) {
$item['twice_daodian_shop'] = '未知';
} else {
$item['twice_daodian_shop'] = $shop_name['name'];
}
}
}
// 量房 - 到店 周期
if (!empty($lf_list) && !empty($dd_list)) {
if ($lf_list[0]['addtime'] > $dd_list[0]['addtime']) {
$item['liangfang_daodian_days'] = ceil((strtotime($lf_list[0]['addtime']) - strtotime($dd_list[0]['addtime'])) / (24 * 3600));
} else {
$item['liangfang_daodian_days'] = ceil((strtotime($dd_list[0]['addtime']) - strtotime($lf_list[0]['addtime'])) / (24 * 3600));
}
} else {
$item['liangfang_daodian_days'] = '无';
}
// 交定(签单)
$jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
$jd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $jd_state]])->order('addtime asc')->select()->toArray();
if (!empty($jd_list)) {
$item['is_deposit'] = '是';
$item['deposit_date'] = $jd_list[0]['addtime'];
$item['deposit_days'] = ceil((strtotime($jd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600));
} else {
$item['is_deposit'] = '否';
$item['deposit_date'] = '无';
$item['deposit_days'] = '无';
}
// 签单(转单)
$qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
$qd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $qd_state]])->order('addtime asc')->select()->toArray();
if (!empty($qd_list)) {
$item['sign_date'] = $qd_list[0]['addtime'];
$item['sign_days'] = ceil((strtotime($qd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600));
} else {
$item['sign_date'] = '无';
$item['sign_days'] = '无';
}
// 交定转单周期
if (!empty($jd_list) && !empty($qd_list)) {
if ($jd_list[0]['addtime'] > $qd_list[0]['addtime']) {
$item['deposit_sign_days'] = ceil((strtotime($jd_list[0]['addtime']) - strtotime($qd_list[0]['addtime'])) / (24 * 3600));
} else {
$item['deposit_sign_days'] = ceil((strtotime($qd_list[0]['addtime']) - strtotime($jd_list[0]['addtime'])) / (24 * 3600));
}
} else {
$item['deposit_sign_days'] = '无';
}
// 一次到店未签单时间
$first_visit = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $dd_state]])->order('addtime asc')->findOrEmpty();
$item['first_visit_no_sign'] = '无';
if (!$first_visit->isEmpty()) {
$first_visit_no_sign = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['addtime', '>', $first_visit['addtime']]])->order('addtime asc')->findOrEmpty();
if (!$first_visit_no_sign->isEmpty()) {
if (!in_array($first_visit_no_sign['state'], $qd_state)) {
$item['first_visit_no_sign'] = $first_visit['addtime'];
}
}
}
// 客户经理回访
if (!empty($assigned_personal_manager_ids)) {
$manager_next_visit = CustomerVisitLog::where([['employee_id', 'in', $assigned_personal_manager_ids], ['next_contact_date', 'not null', ''], ['customer_id', '=', $item['id']]])->field('customer_id,next_contact_date,addtime,employee_id')->select()->toArray();
$manager_visit_times = 0;
$manager_visit_days = 0;
foreach ($manager_next_visit as $k => $v) {
unset($visit_where);
$visit_where[] = ['addtime', 'between', [$v['next_contact_date'] . ' 00:00:00', $v['next_contact_date'] . ' 23:59:59']];
$visit_where[] = ['customer_id', '=', $v['customer_id']];
$visit_where[] = ['employee_id', '=', $v['employee_id']];
$visit_next_find = CustomerVisitLog::where($visit_where)->findOrEmpty();
if (!$visit_next_find->isEmpty()) {
$manager_visit_times++;
$next_days = ceil((strtotime($visit_next_find['addtime']) - strtotime($v['next_contact_date'])) / (24 * 3600));
$manager_visit_days += $next_days;
}
}
$item['manager_visit_times'] = $manager_visit_times;
$item['manager_visit_cycle'] = $manager_visit_times ? round($manager_visit_days / $manager_visit_times, 1) : '无';
} else {
$item['manager_visit_times'] = 0;
$item['manager_visit_cycle'] = '无';
}
// 经理回访
$employee_next_visit = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['next_contact_date', 'not null', ''], ['employee_id', '=', $item['employee_id']]])->field('customer_id,next_contact_date,employee_id')->select()->toArray();
$employee_visit_times = 0;
$employee_visit_days = 0;
foreach ($employee_next_visit as $k => $v) {
unset($visit_where);
$visit_where[] = ['addtime', 'between', [$v['next_contact_date'] . ' 00:00:00', $v['next_contact_date'] . ' 23:59:59']];
$visit_where[] = ['customer_id', '=', $v['customer_id']];
$visit_where[] = ['employee_id', '=', $v['employee_id']];
$visit_next_find = CustomerVisitLog::where($visit_where)->findOrEmpty();
if (!$visit_next_find->isEmpty()) {
$employee_visit_times++;
$next_days = ceil((strtotime($visit_next_find['addtime']) - strtotime($v['next_contact_date'])) / (24 * 3600));
$employee_visit_days += $next_days;
}
}
$item['employee_visit_times'] = $employee_visit_times;
$item['employee_visit_cycle'] = $employee_visit_times ? round($employee_visit_days / $employee_visit_times, 1) : '无';
$wx_state = Customer::changeState('无效', 'chaos');
if (in_array($item['state'], $wx_state)) {
$item['invalid'] = '是';
} else {
$item['invalid'] = '否';
}
if ($item['died'] == 2) {
$item['died'] = '是';
} else {
$item['died'] = '否';
}
// 卖卡时间
$maika_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
$maika_visit = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $maika_state]])->order('addtime desc')->findOrEmpty();
$item['maika_time'] = '无';
if (!$maika_visit->isEmpty()) {
$item['maika_time'] = $maika_visit['addtime'];
}
})->toArray();
$clue_where[] = ['name', '=', 'clueTag'];
$clue_where[] = ['root_id', '=', request()->employee->root_id];
$find = Setting::where($clue_where)->findOrEmpty();
if ($find->isEmpty()) {
$clue_tag = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
} else {
$clue_tag = array_filter(explode(',', $find['content']));
}
foreach ($list as $k => $v) {
$list[$k]['house_delivery_time'] = $v['house_delivery_time'] ? $v['house_delivery_time'] : '未知';
$list[$k]['employee_name'] = $v['employee']['name'] ?? '无';
$list[$k]['source_name'] = $v['source']['source'] ?? '未知';
if ($v['invalid'] == '否') {
$list[$k]['invalid_remark'] = '';
} else {
$wx_state = CustomerVisitLog::changeState('无效', 'chaos');
$invalid_log = CustomerVisitLog::where([['customer_id', '=', $v['id']], ['state', 'in', $wx_state]])->order('addtime desc')->findOrEmpty();
if (!$invalid_log->isEmpty() && !empty($invalid_log['remark']) && in_array($invalid_log['remark'], $clue_tag)) {
$list[$k]['invalid_remark'] = $invalid_log['remark'];
} else {
$list[$k]['invalid_remark'] = '其他';
}
}
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'page' => ceil($count / $limit)]);
}
/**
* 客户统计列表导出
*/
public function customer_list_export()
{
if (!request()->isAjax()) {
$header = [
["field" => 'name', "title" => '客户姓名', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'phone', "title" => '手机号', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'community_name', "title" => '小 区', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'house_type', "title" => '房屋类型', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'house_delivery_time', "title" => '交房时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'square', "title" => '面积', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'org_name', "title" => '部门', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'employee_name', "title" => '业务员', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'addtime', "title" => '添加时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'source_name', "title" => '来源', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'assigned_personal_manager', "title" => '指派客户经理', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'assigned_personal_designer_org', "title" => '指派设计部', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'assigned_personal_designer', "title" => '指派设计师', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'no_visit_day', "title" => '未跟踪天数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'wechat', "title" => '是否加微信', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'add_wechat_type', "title" => '加微类型', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'add_wechat_time', "title" => '加微时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'group_building_date', "title" => '建群时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'is_liangfang', "title" => '是否量房', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'liangfang_date', "title" => '量房时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'once_liangfang_days', "title" => '量房周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'plan_issuing_date', "title" => '计划出初步方案时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'once_daodian', "title" => '是否一次到店', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'daodian_date', "title" => '一次到店时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'once_daodian_shop', "title" => '一次所到店面', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'once_daodian_days', "title" => '一次到店周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'liangfang_daodian_days', "title" => '量房到店周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'twice_daodian_date', "title" => '二次到店时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'twice_daodian_shop', "title" => '二次所到店面', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'twice_daodian_days', "title" => '二次到店周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'is_deposit', "title" => '是否交定/签单', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'deposit_date', "title" => '交定/签单时间', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'drawing_date', "title" => '出图时间', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'sign_date', "title" => '转单时间', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'sign_days', "title" => '交定转单周期', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'signed_money_data', "title" => '合同金额(元)', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'first_visit_no_sign', "title" => '一次到店未签单时间', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'manager_visit_times', "title" => '客户经理回访次数', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'manager_visit_cycle', "title" => '客户经理回访周期', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'employee_visit_times', "title" => '经理回访次数', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'employee_visit_cycle', "title" => '经理回访平均周期', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'invalid', "title" => '无效客户', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'invalid_remark', "title" => '无效原因', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
["field" => 'died', "title" => '是否死单', " align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"]
];
view::assign('row', json_encode($header));
view::assign('header', $header);
return View::fetch();
} else {
return $this->customer_list_data();
}
}
/**
* 总统计页面
*/
public function generalStatistics()
{
$root_id = request()->employee->root_id;
//房屋状态是否开启
$house_status_where = [
['root_id', '=', $root_id],
['keyname', '=', 'house_status'],
['status', '=', 0]
];
$house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
$field = [];
if (!$house_status_per->isEmpty()) {
$field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
$fields = [];
foreach ($field as $v) {
$fields[] = [
'field' => 'house_status_' . $v['id'],
'title' => $v['name'],
'align' => 'center',
'width' => 100,
'rowspan' => 2
];
}
$field = $fields;
}
View::assign('field', json_encode($field));
//无效客资
$invalid_customer = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
$arr3 = [];
if ($invalid_customer) {
$arr3 = explode(',', $invalid_customer);
} else {
$arr3 = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
}
$field4 = [];
foreach ($arr3 as $k3 => $v3) {
$field4[] = [
'field' => 'invalid_customer_' . $k3,
'title' => $v3,
'align' => 'center',
'width' => 100,
'rowspan' => 1
];
}
View::assign('field4', json_encode($field4));
//返回组织列表
$org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
View::assign('org', $org);
$type = input('type', '', 'intval');
View::assign('type', 2);
$org_id = input('org_id', '', 'intval');
View::assign('org_id', $org_id);
return View::fetch();
}
/**
* 平均周期
*/
public function avgCycle($cus, $vis)
{
$state1 = CustomerVisitLog::changeState('已交定', 'chaos');
$state2 = CustomerVisitLog::changeState('已量房', 'chaos');
$state3 = CustomerVisitLog::changeState('已到店', 'chaos');
$dep_time = [];
$room_time = [];
$store_time = [];
$next_date = [];
foreach ($vis as $v) {
//交定日期
if (in_array($v['state'], $state1)) {
$dep_time[$v['customer_id']] = $v['addtime'];
} elseif (in_array($v['state'], $state2)) {
$room_time[$v['customer_id']] = $v['addtime'];
} elseif (in_array($v['state'], $state3)) {
$store_time[$v['customer_id']] = $v['addtime'];
}
//平均回访次数
if (!isset($next_date[$v['customer_id']]['addtime'])) {
$next_date[$v['customer_id']]['addtime'] = [];
}
if (!isset($next_date[$v['customer_id']]['next_date'])) {
$next_date[$v['customer_id']]['next_date'] = [];
}
$next_date[$v['customer_id']]['addtime'][] = date('Y-m-d', strtotime($v['addtime']));
$next_date[$v['customer_id']]['next_date'][] = date('Y-m-d', strtotime($v['next_contact_date']));
}
$dep_cycle = $room_cycle = $room_store_cycle = $next = [];
foreach ($cus as $v2) {
if (isset($dep_time[$v2['id']])) {
$dep_cycle[] = strtotime($dep_time[$v2['id']]) - strtotime($v2['addtime']);
}
if (isset($room_time[$v2['id']])) {
$room_cycle[] = strtotime($room_time[$v2['id']]) - strtotime($v2['addtime']);
}
if (isset($room_time[$v2['id']]) && isset($store_time[$v2['id']])) {
$room_store_cycle[] = abs(strtotime($room_time[$v2['id']]) - strtotime($store_time[$v2['id']]));
}
if (isset($next_date[$v2['id']]['addtime']) && $next_date[$v2['id']]['next_date']) {
$next[] = count(array_filter(array_intersect($next_date[$v2['id']]['addtime'], $next_date[$v2['id']]['next_date'])));
}
}
//平均交定周期
$data['dep_cycle'] = empty($dep_cycle) ? 0 : ceil(count($dep_cycle) / array_sum($dep_cycle));
//平均量房周期
$data['room_cycle'] = empty($room_cycle) ? 0 : ceil(count($room_cycle) / array_sum($room_cycle));
//平均量房到店周期
$data['room_store_cycle'] = empty($room_store_cycle) ? 0 : ceil(count($room_store_cycle) / array_sum($room_store_cycle));
//平均回访次数
$data['next'] = !array_sum($next) ? 0 : ceil(count($next) / array_sum($next));
return $data;
}
/**
* 获取见面客户
* @param $search_list 要判断有多少见面的log记录
* @param $before_list 之前见面的客户id集合
*/
public function visitDeal($search_list, $before_list)
{
// 见面状态
$state2 = CustomerVisitLog::changeState('已到场', 'chaos');
$state3 = CustomerVisitLog::changeState('已量房', 'chaos');
$state4 = CustomerVisitLog::changeState('已到店', 'chaos');
$state5 = CustomerVisitLog::changeState('已交定', 'chaos');
$state6 = CustomerVisitLog::changeState('已签单', 'chaos');
$visit_state = array_merge($state2, $state3, $state4, $state5, $state6);
$search_ids = [];
foreach ($search_list as $k => $v) {
if (in_array($v['state'], $visit_state)) {
$search_ids[] = $v['customer_id'];
}
}
$visit_ids = array_values(array_unique(array_diff($search_ids, $before_list)));
return $visit_ids;
}
/**
* 总统计数据
*/
public function generalStatisticsData($page = 1, $limit = 10)
{
$param = request()->only(['date' => '', 'keyword' => '', 'org_id' => '', 'start_date' => '', 'end_date' => '', 'type' => '', 'source_id' => '']);
$root_id = request()->employee->root_id;
//日期搜索//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
if ($param['start_date']) {
$count_dates = explode(' - ', $param['start_date']);
$param['start_date'] = date('Y-m-d 00:00:00', strtotime($count_dates[0]));
$param['end_date'] = date('Y-m-d 23:59:59', strtotime($count_dates[1]));
} else {
$param['start_date'] = '2010-01-01 00:00:00';
$param['end_date'] = date('Y-m-d H:i:s', time());
}
$date_query = [['addtime', 'between', [$param['start_date'], $param['end_date']]]];
$date_query1 = [['share_time', 'between', [$param['start_date'], $param['end_date']]]];
//加微时间筛选
$sdate = strtotime($param['start_date']);
$edate = strtotime($param['end_date']);
$where = [
['root_id', '=', $root_id],
['uid', '>', 0],
['state', '=', '在职']
];
if ($param['keyword']) {
$where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
}
$org_id = $param['org_id'] ? $param['org_id'] : $root_id;
$sub_orgs = orgSubIds($org_id);
if ($param['org_id']) {
$o = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
$orgIds = Org::where([['path', 'like', $o['path'] . '%']])->column('id');
$where[] = ['org_id', 'in', $orgIds];
} else {
$where[] = ['org_id', 'in', $sub_orgs];
}
$list = Employee::with(['org' => function ($query) {
$query->field('id, name');
}])->where($where)->field('id,org_id,name')->page($page, $limit)->select()->toArray();
$count = Employee::where($where)->count();
$page_employee = array_column($list, 'id');
$state2 = CustomerVisitLog::changeState('已到场', 'chaos');
$state3 = CustomerVisitLog::changeState('已量房', 'chaos');
$state4 = CustomerVisitLog::changeState('已到店', 'chaos');
$state5 = CustomerVisitLog::changeState('已交定', 'chaos');
$state6 = CustomerVisitLog::changeState('已签单', 'chaos');
$state7 = CustomerVisitLog::changeState('已卖卡', 'chaos');
$wuxiao_log_state = CustomerVisitLog::changeState('无效', 'chaos');
$c_where[] = ['Customer.employee_id', 'in', $page_employee];
$c_where[] = ['CustomerVisitLog.confirm_date', 'between', [$param['start_date'], $param['end_date']]];
$c_where[] = ['Customer.state', 'NOT NULL', null];
$add_where[] = ['employee_id', 'in', $page_employee];
$add_where[] = ['addtime|employee_time', 'between', [$param['start_date'], $param['end_date']]];
$add_where[] = ['state', 'NOT NULL', null];
$db_where[] = ['Customer.employee_id', 'in', $page_employee];
$db_where[] = ['Customer.state', 'NOT NULL', null];
if ($param['source_id']) {
$c_where[] = ['Customer.source_id', '=', $param['source_id']];
$db_where[] = ['Customer.source_id', '=', $param['source_id']];
$jiav_where[] = ['source_id', '=', $param['source_id']];
$j_w_where[] = ['source_id', '=', $param['source_id']];
$add_where[] = ['source_id', '=', $param['source_id']];
}
// 加微客户查询
// 先查搜索时间段内部门客户的加微数据,再判断哪些客户是page_employee中员工的客户,再判断其中哪些是已经置为无效的客户,再把这些置为无效的客户拼接到page_empployee员工身上。
$page_employee_org = array_unique(array_column($list, 'org_id'));
$jiav_where[] = ['org_id', 'in', $page_employee_org];
$jiav_where[] = ['add_wechat_time', 'between', [$param['start_date'], $param['end_date']]];
$jiav_list = Customer::where($jiav_where)->column('id,employee_id,state');
$db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
if ($date_query) {
$db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$param['start_date'], $param['end_date']]];
}
// $customer_list = Customer::where($c_where)->column('id,employee_id,state,ext,is_resource,source_id,addtime,signed_money,house_type,square,house_status,remark,crm_res_id,agents_id,died,assign_type,employee_time');
// 搜索时间段内的跟踪客户
$customer_list = Db::view('Customer', 'id,employee_id,state,ext,is_resource,source_id,addtime,signed_money,house_type,square,house_status,remark,crm_res_id,agents_id,died,assign_type,employee_time')
->view('CustomerVisitLog', [], 'Customer.id=CustomerVisitLog.customer_id')
->where($c_where)
->group('Customer.id')
->select()
->toArray();
// 搜索时间段内添加的客户
$add_customer = Customer::where($add_where)->column('id,employee_id,crm_res_id,addtime,employee_time');
$add_cus_employee = [];
$fenpei_cus_employee = [];
foreach ($add_customer as $k => $v) {
if (empty($v['crm_res_id'])) {
if ($v['addtime'] > $param['start_date'] && $v['addtime'] < $param['end_date']) {
$add_cus_employee[$v['employee_id']][] = $v;
}
} else {
if ((!empty($v['employee_time']) && $v['employee_time'] > $param['start_date'] && $v['employee_time'] < $param['end_date'])) {
$fenpei_cus_employee[$v['employee_id']][] = $v;
}
}
}
// 搜索时间段内的跟踪记录
$vislog_list = Db::view('CustomerVisitLog', 'id,customer_id,state,addtime,next_contact_date,remark,employee_id,confirm_date')
->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
->where($db_where)
->select()
->toArray();
$visit_state = array_merge($state2, $state3, $state4, $state5, $state6);
$be_where[] = ['Customer.employee_id', 'in', $page_employee];
$be_where[] = ['Customer.state', 'NOT NULL', null];
$be_where[] = ['CustomerVisitLog.confirm_date', '<', $param['start_date']];
$be_where[] = ['CustomerVisitLog.state', 'in', $visit_state];
// 搜索时间段外的到访记录
$before_log = Db::view('CustomerVisitLog', 'id,customer_id')
->view('Customer', 'employee_id', 'Customer.id=CustomerVisitLog.customer_id')
->where($be_where)
->select()
->toArray();
// 员工见过面的客户
$employee_visit = [];
foreach ($before_log as $k => $v) {
$employee_visit[$v['employee_id']][] = $v['customer_id'];
}
// 处理加微数据
$jiav_employee = []; // 正常的客户
$jiav_employee_wuxiao = []; // 无效的客户
foreach ($jiav_list as $k => $v) {
if (in_array($v['employee_id'], $page_employee) && !in_array($v['state'], Customer::changeState('无效', 'chaos', true))) {
$jiav_employee[$v['employee_id']][] = $v['id'];
} elseif (in_array($v['state'], Customer::changeState('无效', 'chaos', true))) {
$jiav_employee_wuxiao[] = $v['id'];
}
}
// 去查询这些置为无效的加微客户原先都是谁的
$j_w_where[] = ['customer_id', 'in', $jiav_employee_wuxiao];
$j_w_where[] = ['employee_id', 'in', $page_employee];
$jiav_wuxiao_log = CustomerInvalidLog::where($j_w_where)->column('customer_id,employee_id');
$jiav_wuxiao_log_deal = [];
foreach ($jiav_wuxiao_log as $v) {
$jiav_wuxiao_log_deal[$v['employee_id']][] = $v['customer_id'];
}
foreach ($jiav_employee as $k => $v) {
if (isset($jiav_wuxiao_log_deal[$k])) {
$jiav_employee[$k] = array_unique(array_merge($v, $jiav_wuxiao_log_deal[$k]));
}
}
$customer_employee = []; // 员工下的客户
$customer_ids_employee = []; // 员工下的客户id
$customer_ids_key = []; // 客户所属员工
foreach ($customer_list as $k => $v) {
$customer_employee[$v['employee_id']][] = $v;
$customer_ids_employee[$v['employee_id']][] = $v['id'];
$customer_ids_key[$v['id']] = $v['employee_id'];
}
//2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H
$no_where = [
['employee_id', 'in', array_column($list, 'id')],
['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')],
['addtime', 'between', [$param['start_date'], $param['end_date']]]
];
$no_valid_customer = CustomerVisitLog::where($no_where)->column('id,customer_id,state,addtime,next_contact_date,remark,employee_id,confirm_date');
$vislog_list = array_merge($vislog_list, $no_valid_customer);
$visit_log_employee = []; //员工跟进记录
foreach ($vislog_list as $k => $v) {
$have_employee = $customer_ids_key[$v['customer_id']] ?? 0;
if ($have_employee) {
$v_customer_ids = $customer_ids_employee[$have_employee] ?? [];
// 只保留还是员工客户的跟踪记录,或者是员工置为无效的客户记录
if (!in_array($v['customer_id'], $v_customer_ids) && !in_array($v['state'], $wuxiao_log_state, true)) {
unset($vislog_list[$k]);
continue;
}
$visit_log_employee[$have_employee][] = $v;
}
}
$vislog_list = array_values($vislog_list);
foreach ($list as $k => $v) {
$list[$k]['visit_log'] = $visit_log_employee[$v['id']] ?? [];
$list[$k]['customer'] = $customer_employee[$v['id']] ?? [];
}
$customer['sign_customer'] = []; //签单客户
$customer['measuring_room_customer'] = []; //量房客户
$customer['maika_customer'] = []; //卖卡客户
$customer['measuring_room_time'] = []; //量房时间
$customer['to_the_store'] = []; //到店客户id
$customer['to_the_store_time'] = []; //到店时间
$customer['deposit_customer'] = []; //交定客户
$customer['deposit_date'] = []; // 交定时间
foreach ($vislog_list as $v) {
if (in_array($v['state'], $state6, true)) { //签单客户id
$customer['sign_customer'][] = $v['customer_id'];
} elseif (in_array($v['state'], $state3, true)) { //量房客户id
$customer['measuring_room_customer'][] = $v['customer_id'];
//量房时间 按照时间升序排列
if (empty($customer['measuring_room_time'][$v['customer_id']])) {
$customer['measuring_room_time'][$v['customer_id']] = [];
$customer['measuring_room_time'][$v['customer_id']][] = $v['confirm_date'];
} else {
$customer['measuring_room_time'][$v['customer_id']][] = $v['confirm_date'];
}
} elseif (in_array($v['state'], $state4, true)) { //到店客户id
$customer['to_the_store'][] = $v['customer_id'];
//到店时间 按照时间升序排列
if (empty($customer['to_the_store_time'][$v['customer_id']])) {
$customer['to_the_store_time'][$v['customer_id']] = [];
$customer['to_the_store_time'][$v['customer_id']][] = $v['confirm_date'];
} else {
$customer['to_the_store_time'][$v['customer_id']][] = $v['confirm_date'];
}
} elseif (in_array($v['state'], $state5, true)) { //交定客户
$customer['deposit_customer'][] = $v['customer_id'];
//交定时间 按照时间升序排列
if (empty($customer['deposit_date'][$v['customer_id']])) {
$customer['deposit_date'][$v['customer_id']] = [];
$customer['deposit_date'][$v['customer_id']][] = $v['confirm_date'];
} else {
$customer['deposit_date'][$v['customer_id']][] = $v['confirm_date'];
}
} elseif (in_array($v['state'], $state7, true)) { //卖卡客户
$customer['maika_customer'][] = $v['customer_id'];
}
}
// 有跟进客户
$log_customer_id = []; // 某个员工下的跟踪客户id数组
//智慧屏讲解次数
$zhihui = [];
// 无效
$wuxiao_visit_log = [];
foreach ($vislog_list as $k => $v) {
if (in_array($v['state'], $wuxiao_log_state, true)) {
$wuxiao_visit_log[] = $v;
}
$have_employee = $customer_ids_key[$v['customer_id']] ?? 0;
if ($have_employee) {
if (strpos($v['remark'], '讲解智慧屏##') !== false) {
if (empty($zhihui[$have_employee])) {
$zhihui[$have_employee] = 1;
} else {
$zhihui[$have_employee] += 1;
}
}
}
if (in_array($v['state'], $wuxiao_log_state, true)) {
continue;
}
if (in_array($v['remark'], ['客户再分配', '资源库分配', '客户转移'], true) || (strpos($v['remark'], '业务员') !== false && strpos($v['remark'], '对客户') !== false && strpos($v['remark'], '进行报备') !== false)) {
continue;
}
if ($have_employee) {
if (isset($log_customer_id[$have_employee])) {
$log_customer_id[$have_employee][] = $v['customer_id'];
} else {
$log_customer_id[$have_employee] = [];
$log_customer_id[$have_employee][] = $v['customer_id'];
}
}
}
foreach ($log_customer_id as $k => $v) {
$log_customer_id[$k] = array_values(array_unique($v));
}
// 外呼数据
$outCallLog = OutCallLog::where([['root_id', '=', $root_id], ['employee_id', 'in', $page_employee]])->where($date_query)->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,employee_id")->group('employee_id')->select()->toArray();
$outCallLog = array_combine(array_column($outCallLog, 'employee_id'), $outCallLog);
// 手机打电话次数获取
$outCallMbLog = OutCallMbLog::where([
['root_id', '=', $root_id], ['employee_id', 'in', $page_employee]
])->where($date_query)->field("count(id) as phone_count,employee_id")->group('employee_id')->select()->toArray();
$outCallMbLog = array_combine(array_column($outCallMbLog, 'employee_id'), $outCallMbLog);
// 房屋状态设置项
//房屋状态是否开启
$house_status_where = [
['root_id', '=', $root_id],
['keyname', '=', 'house_status'],
['status', '=', 0]
];
$house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
$house_status_field = [];
if (!$house_status_per->isEmpty()) {
$house_status_field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
}
// 户型是否开启
$housetype_where = [
['root_id', '=', $root_id],
['keyname', '=', 'housetype_arrow'],
['status', '=', 0]
];
$housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
$housetype_arrow = [];
if (!$housetype_per->isEmpty()) {
$housetype_arrow = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
}
//分享内容次数
$share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
$share = ShareLog::where([['employee_id', 'in', $page_employee], ['type', 'in', $share_where]])->where($date_query1)->field('concat(employee_id,type,data_id) as str,employee_id')->select()->toArray();
$shares = [];
foreach ($share as $v) {
if (empty($shares[$v['employee_id']])) {
$shares[$v['employee_id']] = 1;
} else {
$shares[$v['employee_id']] += 1;
}
}
// 培训资料是否看了 课程是否学习过
$look = TrainCourseView::where([['employee_id', 'in', $page_employee], ['time', '>', 0]])->where($date_query)->group('employee_id')->column('employee_id');
//考核次数 通过次数
$don = TrainDoneLog::where([['employee_id', 'in', $page_employee]])->where($date_query)->column('employee_id,done_percent');
$don1 = $don2 = [];
foreach ($don as $don_k => $don_v) {
if (empty($don1[$don_v['employee_id']])) {
$don1[$don_v['employee_id']] = 1;
} else {
$don1[$don_v['employee_id']] += 1;
}
if ($don_v['done_percent'] == 100) {
if (empty($don2[$don_v['employee_id']])) {
$don2[$don_v['employee_id']] = 1;
} else {
$don2[$don_v['employee_id']] += 1;
}
}
}
//无效客资
$invalid = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
$invalid = $invalid ? explode(',', $invalid) : ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
$invalid_customer = [];
// 按客户来源搜索处理
$wuxiao_customer_id = array_values(array_unique(array_column($wuxiao_visit_log, 'customer_id')));
$no_valid_customers = []; //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H 康
if ($param['source_id']) {
$wuxiao_source_customer_id = Customer::where([['id', 'in', $wuxiao_customer_id], ['source_id', '=', $param['source_id']]])->column('id');
foreach ($wuxiao_visit_log as $k => $v) {
if (in_array($v['customer_id'], $wuxiao_source_customer_id)) {
$remark = '';
if (in_array($v['remark'], $invalid)) {
$remark = $v['remark'];
} else {
foreach ($invalid as $i_k => $i_v) {
if (strpos($v['remark'], $i_v) !== false) {
$remark = $i_v;
break;
}
}
}
if (isset($invalid_customer[$v['employee_id']])) {
$invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
} else {
$invalid_customer[$v['employee_id']] = [];
$invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
}
if (isset($no_valid_customers[$v['employee_id']])) {
$no_valid_customers[$v['employee_id']][] = $v['customer_id'];
} else {
$no_valid_customers[$v['employee_id']] = [];
$no_valid_customers[$v['employee_id']][] = $v['customer_id'];
}
}
}
} else {
foreach ($wuxiao_visit_log as $k => $v) {
$remark = '';
if (in_array($v['remark'], $invalid)) {
$remark = $v['remark'];
} else {
foreach ($invalid as $i_k => $i_v) {
if (strpos($v['remark'], $i_v) !== false) {
$remark = $i_v;
break;
}
}
}
if (isset($invalid_customer[$v['employee_id']])) {
$invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
} else {
$invalid_customer[$v['employee_id']] = [];
$invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
}
if (isset($no_valid_customers[$v['employee_id']])) {
$no_valid_customers[$v['employee_id']][] = $v['customer_id'];
} else {
$no_valid_customers[$v['employee_id']] = [];
$no_valid_customers[$v['employee_id']][] = $v['customer_id'];
}
}
}
// 无效客资来自资源分配
$wuxiao_customer_res_id = Customer::where([['id', 'in', $wuxiao_customer_id], ['crm_res_id', '>', 0]])->column('id');
$wuxiao_customer_res_employee = [];
// 无效客资来自自建
$wuxiao_customer_build_employee = [];
foreach ($invalid_customer as $k => $v) {
$customer_id_v = array_column($v, 'customer_id');
foreach ($customer_id_v as $vv) {
if (in_array($vv, $wuxiao_customer_res_id)) {
$wuxiao_customer_res_employee[$k][] = $vv;
} else {
$wuxiao_customer_build_employee[$k][] = $vv;
}
}
}
$f_invalid = array_flip($invalid);
//预约记录
$sub_where = [
['CustomersSubscribe.state', '=', 0],
['CustomersSubscribe.type', 'in', [1, 3]],
['CustomersSubscribe.employee_id', 'in', $page_employee]
];
if ($date_query) $sub_where[] = ['CustomersSubscribe.addtime', 'between', [$param['start_date'], $param['end_date']]]; //日期条件
$sub1 = $sub2 = []; //1到店 2量房
$subscribe = Db::view('CustomersSubscribe')
->view('Customer', 'id', 'CustomersSubscribe.customer_id=Customer.id')
->where($sub_where)
->column('CustomersSubscribe.type,CustomersSubscribe.employee_id');
foreach ($subscribe as $v) {
$v['type'] == 1 ? @$sub1[$v['employee_id']] += 1 : @$sub2[$v['employee_id']] += 1;
}
foreach ($list as $k => $v) {
//部门名称
$list[$k]['org_name'] = $v['org']['name'];
//预计到店数
$list[$k]['yuji_store'] = isset($sub1[$v['id']]) ? $sub1[$v['id']] : 0;
//客户id 所有的客户ID 包括资源,自创,无效等
$all_customer_id = array_column($v['customer'], 'id');
//数据处理
$customer_data = $this->dealCustomerData($v['customer'], $sdate, $edate, $customer['measuring_room_time'], $customer['to_the_store_time'], $customer['deposit_date']);
$cycle_data = $this->avgCycle($v['customer'], $v['visit_log']);
$v_employee_visit = $employee_visit[$v['id']] ?? [];
$visit_data = $this->visitDeal($v['visit_log'], $v_employee_visit);
// 分配资源数量 包含已置入公海的
$resource_wuxiao_ids = [];
if (isset($wuxiao_customer_res_employee[$v['id']])) {
$resource_wuxiao_ids = array_values(array_unique($wuxiao_customer_res_employee[$v['id']]));
}
// 由于置为无效之后没有了获取客户时间(employee_time) 所以资源不统计无效
// $list[$k]['resource_count'] = count(array_unique(array_merge($customer_data['is_resource'], $resource_wuxiao_ids)));
$resource_count = $fenpei_cus_employee[$v['id']] ?? [];
$list[$k]['resource_count'] = count($resource_count);
if (isset($outCallLog[$v['id']])) {
// 拨打电话数量
$list[$k]['connecting_count'] = $outCallLog[$v['id']]['phone_count'];
// 接通数量
$list[$k]['connecting_capacity'] = $outCallLog[$v['id']]['on_phone_count'];
// 接通率
$list[$k]['connecting_capacity_grawth'] = $outCallLog[$v['id']]['phone_count'] == 0 ? '0%' : round($outCallLog[$v['id']]['on_phone_count'] / $outCallLog[$v['id']]['phone_count'] * 100, 1) . '%';
// 未接通数量
$list[$k]['connecting_no'] = $outCallLog[$v['id']]['off_phone_count'];
// 总通话时长(分)
$list[$k]['phone_time'] = round($outCallLog[$v['id']]['phone_time'] / 60, 1);
// 平均通话时长(秒)
$list[$k]['average_duration'] = $outCallLog[$v['id']]['on_phone_count'] == 0 ? 0 : round($outCallLog[$v['id']]['phone_time'] / $outCallLog[$v['id']]['on_phone_count'], 0);
} else {
$list[$k]['connecting_count'] = $list[$k]['connecting_capacity'] = $list[$k]['connecting_no'] = $list[$k]['phone_time'] = $list[$k]['average_duration'] = 0;
$list[$k]['connecting_capacity_grawth'] = '0%';
}
// 手机通话
if (isset($outCallMbLog[$v['id']])) $list[$k]['connecting_count'] += $outCallMbLog[$v['id']]['phone_count'];
// 客户总量
$customer_count = $customer_data['customer_count'];
$list[$k]['following_up_count'] = count($customer_count);
// 加微客户数量
$v_wechat_count = $jiav_employee[$v['id']] ?? [];
$list[$k]['add_wechat_count'] = count($v_wechat_count);
// 待确认客户数
$list[$k]['not_sure_count'] = count($customer_data['not_sure_count']);
// 有效客户数
if (isset($no_valid_customers[$v['id']])) {
$list[$k]['valid_count'] = count(array_unique(array_merge($customer_data['valid_count'], $no_valid_customers[$v['id']])));
} else {
$list[$k]['valid_count'] = count($customer_data['valid_count']);
}
//有效未加微
$list[$k]['valid_no_wechat'] = count(array_diff($customer_data['valid_count'], $customer_data['wechat_count']));
// 报备客户数
//$reported_diff = array_diff($all_customer_id, $customer_data['is_resource']); //现有客户除去资源库来的
// $reported_quantity = isset($wuxiao_customer_build_employee[$v['id']]) ? array_unique(array_merge($reported_diff, $wuxiao_customer_build_employee[$v['id']])) : $reported_diff;
$reported_diff = $add_cus_employee[$v['id']] ?? [];
$list[$k]['reported_quantity'] = count($reported_diff);
// 加微率
$list[$k]['add_wechat_count_grawth'] = count($customer_count) == 0 ? '0%' : round(count($v_wechat_count) / count($customer_count) * 100, 2) . '%';
// 无效客户数
foreach ($invalid as $invalid_k => $invalid_v) {
$list[$k]['invalid_customer_' . $invalid_k] = [];
}
if (isset($invalid_customer[$v['id']])) {
$invalid_customer_all = array_unique(array_column($invalid_customer[$v['id']], 'customer_id'));
$list[$k]['invalid_customer_count'] = count($invalid_customer_all);
if ($invalid) {
foreach ($invalid_customer[$v['id']] as $key => $value) {
if ($value['remark']) {
$key = $f_invalid[$value['remark']];
$list[$k]['invalid_customer_' . $key][] = $value['customer_id'];
}
}
}
} else {
$invalid_customer_all = [];
$list[$k]['invalid_customer_count'] = 0;
}
foreach ($invalid as $invalid_k => $invalid_v) {
$list[$k]['invalid_customer_' . $invalid_k] = count(array_unique($list[$k]['invalid_customer_' . $invalid_k]));
}
// 线索总量
$v_customer = !empty($v['customer']) ? array_column($v['customer'], 'id') : [];
$all_customer = array_unique(array_merge($v_customer, $invalid_customer_all));
$list[$k]['all_customer_count'] = count($all_customer);
// 有效率(%)
$list[$k]['valid_grawth'] = count($all_customer) == 0 ? '0%' : round(count($customer_data['valid_count']) / count($all_customer) * 100, 2) . '%';
// 未跟进客户数
if (isset($log_customer_id[$v['id']])) {
$following_up_count = $log_customer_id[$v['id']];
} else {
$following_up_count = [];
}
$list[$k]['not_following_up_count'] = count($customer_count) - count(array_intersect($customer_count, $following_up_count)); // 总的减去有跟进的
// 意向分派客户数
$list[$k]['yixiang_assigned_count'] = count($customer_data['yixiang_assigned_count']);
// 到店分派客户数
$list[$k]['daodian_assigned_count'] = count($customer_data['daodian_assigned_count']);
// 量房分配客户数
$list[$k]['measuring_assigned_count'] = count($customer_data['liangfang_assigned_count']);
// 智慧屏讲解次数
$list[$k]['number_of_explanations'] = isset($zhihui[$v['id']]) ? $zhihui[$v['id']] : 0;
// 量房客户数
$measuring_room_customer = array_intersect($all_customer_id, $customer['measuring_room_customer']); //量房客户ID
$to_the_store_ids = array_intersect($all_customer_id, $customer['to_the_store']); // 到店客户ID
$list[$k]['measuring_room_customer'] = count($measuring_room_customer);
// 量房未到店
$measuring_room_customer_to_store = array_intersect($measuring_room_customer, $to_the_store_ids); // 量房 和 到店
$measuring_room_customer_no_to_store = array_diff($measuring_room_customer, $measuring_room_customer_to_store);
$list[$k]['measuring_room_customer_no_to_store'] = count($measuring_room_customer_no_to_store);
// 量房已到店
$list[$k]['measuring_room_customer_to_store'] = count($measuring_room_customer_to_store);
//平均量房到店周期
$list[$k]['avg_room_store_zhouqi'] = $cycle_data['room_store_cycle'];
// 平均量房周期(天)
$list[$k]['measuring_room_zhouqi'] = $cycle_data['room_cycle'];
// 量房率
$list[$k]['measuring_room_grawth'] = count($customer_count) == 0 ? '0%' : round(count($measuring_room_customer) / count($customer_count) * 100, 2) . '%';
// 到店数 次数
$to_the_store = 0;
foreach ($customer['to_the_store'] as $kk => $vv) {
if (in_array($vv, $all_customer_id)) {
$to_the_store++;
}
}
//有户型图的客户
$list[$k]['drawing_date_count'] = count($customer_data['drawing_date']);
$list[$k]['to_the_store'] = $to_the_store;
// 一次到店数
$list[$k]['to_the_store_1'] = $customer_data['strtore_count_0'];
// 二次到店
$list[$k]['to_the_store_2'] = $customer_data['strtore_count_1'];
// 二次到店比率
$list[$k]['strtore_2v1'] = $customer_data['strtore_count_0'] == 0 ? '0%' : round($customer_data['strtore_count_1'] / $customer_data['strtore_count_0'] * 100, 2) . '%';
// 三次及以上到店
$list[$k]['to_the_store_3'] = $customer_data['strtore_count_2'];
// 平均到店周期(天)
$list[$k]['strtore_avg_days_1'] = ceil($customer_data['strtore_0'] / 86400); // 一次
$list[$k]['strtore_avg_days_2'] = ceil($customer_data['strtore_1'] / 86400); // 二次
$list[$k]['strtore_avg_days_3'] = ceil($customer_data['strtore_2'] / 86400); // 三次
//一次 二次 三次 到店率
$list[$k]['strtore_count_1_grawth'] = count($customer_count) == 0 ? '0%' : round($customer_data['strtore_count_0'] / count($customer_count) * 100, 2) . '%';
$list[$k]['strtore_count_2_grawth'] = count($customer_count) == 0 ? '0%' : round($customer_data['strtore_count_1'] / count($customer_count) * 100, 2) . '%';
$list[$k]['strtore_count_3_grawth'] = count($customer_count) == 0 ? '0%' : round($customer_data['strtore_count_2'] / count($customer_count) * 100, 2) . '%';
// 到店客户数
$list[$k]['strtore_customer_count'] = count($to_the_store_ids);
// 到店率
$list[$k]['strtore_customer_grawth'] = count($customer_count) == 0 ? '0%' : round(count($to_the_store_ids) / count($customer_count) * 100, 2) . '%';
// 签单客户数量
$deposit_count_ids = array_intersect($all_customer_id, $customer['deposit_customer']);
$list[$k]['deposit_count'] = count($deposit_count_ids);
// 签单率
$list[$k]['deposit_avg'] = count($visit_data) == 0 ? '0%' : round(count($deposit_count_ids) / count($visit_data) * 100, 2) . '%';
// 平均签单周期
$list[$k]['deposit_avg_days'] = $cycle_data['dep_cycle'];
// 转单客户数量
$sign_customer_ids = array_intersect($all_customer_id, $customer['sign_customer']);
$list[$k]['sign_count'] = count($sign_customer_ids);
//一次 二次 三次 到店交定个数
$list[$k]['strtore_deposit_count_0'] = count(array_unique(array_intersect($customer['deposit_customer'], $customer_data['strtore_customer_0'])));
$list[$k]['strtore_deposit_count_1'] = count(array_unique(array_intersect($customer['deposit_customer'], $customer_data['strtore_customer_1'])));
$list[$k]['strtore_deposit_count_2'] = count(array_unique(array_intersect($customer['deposit_customer'], $customer_data['strtore_customer_2'])));
//一次 二次 三次 到店交定率 数量/有效
$list[$k]['strtore_deposit_grawth_0'] = count($customer_count) == 0 ? '0%' : round($list[$k]['strtore_deposit_count_0'] / count($customer_count) * 100, 2) . '%';
$list[$k]['strtore_deposit_grawth_1'] = count($customer_count) == 0 ? '0%' : round($list[$k]['strtore_deposit_count_1'] / count($customer_count) * 100, 2) . '%';
$list[$k]['strtore_deposit_grawth_2'] = count($customer_count) == 0 ? '0%' : round($list[$k]['strtore_deposit_count_2'] / count($customer_count) * 100, 2) . '%';
// 卖卡客户数量
$maika_customer_ids = array_intersect($all_customer_id, $customer['maika_customer']);
$list[$k]['maika_count'] = count($maika_customer_ids);
// 转单率
$list[$k]['sign_count_grawth'] = count($visit_data) == 0 ? '0%' : round(count($sign_customer_ids) / count($visit_data) * 100, 2) . '%';
// 平均跟进次数
$list[$k]['avg_visit_count'] = $cycle_data['next'];
// 平均合同金额(元)
$list[$k]['signed_money'] = $customer_data['signed_money'];
// 合同总金额
$list[$k]['signed_money_all'] = $customer_data['signed_money_all'];
// 面积
$list[$k]['square'] = $customer_data['square'];
$list[$k]['square_0_80'] = $customer_data['square_0_80'];
$list[$k]['square_80_100'] = $customer_data['square_80_100'];
$list[$k]['square_100_120'] = $customer_data['square_100_120'];
$list[$k]['square_120_200'] = $customer_data['square_120_200'];
$list[$k]['square_200_500'] = $customer_data['square_200_500'];
$list[$k]['square_500'] = $customer_data['square_500'];
// 房屋状态
$list[$k]['house_status'] = 0;
if ($house_status_field) {
$house_status = $this->getHouseStatus($customer_data['house_status'], $house_status_field);
$list[$k] = array_merge($list[$k], $house_status);
$list[$k]['house_status'] = array_sum($house_status);
}
//房屋户型 及 对应的转单率
if ($housetype_arrow) {
$housetype_arrow_data = $this->getHousetypeArrow($customer_data['housetype_arrow'], $housetype_arrow, $sign_customer_ids);
$list[$k] = array_merge($list[$k], $housetype_arrow_data);
}
// 房屋类型
$list[$k]['house_type'] = $customer_data['existing_homes_count'] + $customer_data['forward_housing_count'];
$list[$k]['existing_homes_count'] = $customer_data['existing_homes_count'];
$list[$k]['forward_housing_count'] = $customer_data['forward_housing_count'];
// 调用内容数量
// 分享内容次数
$list[$k]['shares_count'] = isset($shares[$v['id']]) ? $shares[$v['id']] : 0;
// 考核次数
$list[$k]['assessment_times'] = isset($don1[$v['id']]) ? $don1[$v['id']] : 0;
// 通过次数
$list[$k]['number_of_passes'] = isset($don2[$v['id']]) ? $don2[$v['id']] : 0;
//培训资料是否看了 课程是否学习过
$list[$k]['look_data'] = in_array($v['id'], $look) ? '是' : '否';
unset($list[$k]['customer']);
unset($list[$k]['visit_log']);
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
}
/**
* 处理客户数据
* $list 业务员的客户数组
* $sdate 开始时间筛选
* $edate 结束时间筛选
* $measuring_room_time 客户量房时间 计算平均量房时间
* $to_the_store_time 客户到店时间 计算平均到店时间
* $deposit_customer 客户交定时间 计算平均交定周期
*/
static function dealCustomerData($list, $sdate, $edate, $measuring_room_time, $to_the_store_time, $deposit_customer)
{
$time = time();
$data['not_sure_count'] = []; // 待确认客户id
$data['customer_count'] = []; // 客户量 除去资源、无效、死单
$data['valid_count'] = []; // 有效客户 除去资源、无效、死单、待确认 (客户量的基础上减去待确认的)
$data['yixiang_assigned_count'] = []; // 意向分配客户数
$data['daodian_assigned_count'] = []; // 到店分配客户数
$data['liangfang_assigned_count'] = []; // 量房分配客户数
$data['wechat_count'] = []; //加微客户id
$data['is_resource'] = []; //资源库分配客户id
$data['drawing_date'] = []; //有户型图的客户
$row['room_cycle_0'] = $row['room_cycle_1'] = $row['room_cycle_2'] = []; //一次 二次 三次量房周期
$row['strtore_0'] = $row['strtore_1'] = $row['strtore_2'] = []; //一次 二次 三次到店周期
$row['deposit_0'] = $row['deposit_1'] = $row['deposit_2'] = []; //一次 二次 三次交定周期
$data['room_cycle_customer_0'] = $data['room_cycle_customer_1'] = $data['room_cycle_customer_2'] = []; //一次 两次 三次量房客户
$data['strtore_customer_0'] = $data['strtore_customer_1'] = $data['strtore_customer_2'] = []; //一次 两次 三次到店客户
$data['deposit_customer_0'] = $data['deposit_customer_1'] = $data['deposit_customer_2'] = []; //一次 两次 三次交定客户
$row['signed_money'] = []; //合同金额
$house_type = []; //房屋类型
$housetype_arrow = []; // 房屋户型
$square = []; //房屋面积
$data['house_status'] = []; //房屋状态
//报备客户
$data['reported_quantity'] = 0;
//死单客户
$data['died'] = 0;
$not_sure_state = Customer::changeState('待确认', 'chaos');
$invalid_state = Customer::changeState('无效', 'chaos');
$invalid_not_sure_state = array_values(array_merge($invalid_state, $not_sure_state));
foreach ($list as $v) {
// 待确认客户
if (empty($v['crm_res_id']) && in_array($v['state'], $not_sure_state, true)) {
$data['not_sure_count'][] = $v['id'];
}
//客户添加时间
$addtime = strtotime($v['addtime']);
// 客户数量
if (intval($v['died']) != 2) {
if ((empty($v['crm_res_id']) && !in_array($v['state'], $invalid_state, true)) || (!empty($v['crm_res_id']) && !in_array($v['state'], $invalid_not_sure_state, true))) {
$data['customer_count'][] = $v['id'];
$square[] = $v['square']; //面积
}
}
// 有效客户 一部分数据只是有效时候才做统计
if (intval($v['died']) != 2 && !in_array($v['state'], $invalid_not_sure_state, true)) {
$data['valid_count'][] = $v['id'];
// 房屋类型
$house_type[] = $v['house_type'];
$row['signed_money'][] = $v['signed_money']; //合同金额
$data['house_status'][] = $v['house_status'];
// 房屋户型
$ext = json_decode($v['ext'], true);
if (!empty($ext) && is_array($ext)) {
$ext = array_filter($ext);
foreach ($ext as $e) {
if (isset($e['keyname']) && $e['keyname'] == 'housetype_arrow' && !empty($e['value'])) {
$housetype_arrow[$v['id']] = $e['value'];
}
}
}
}
// 意向分派客户数
if (!empty($v['assign_type']) && $v['assign_type'] == 'yixiang') {
$data['yixiang_assigned_count'][] = $v['id'];
}
// 到店分派客户数
if (!empty($v['assign_type']) && $v['assign_type'] == 'daodian') {
$data['daodian_assigned_count'][] = $v['id'];
}
// 量房分派客户数
if (!empty($v['assign_type']) && $v['assign_type'] == 'liangfang') {
$data['liangfang_assigned_count'][] = $v['id'];
}
//资源库分配数量
if ($v['crm_res_id'] && strtotime($v['employee_time']) >= $sdate && strtotime($v['employee_time']) <= $edate) {
$data['is_resource'][] = $v['id'];
}
//加微客户,有户型图客户
$v2 = $v['ext'];
$ext = json_decode($v2, true);
if (!empty($ext) && is_array($ext)) {
$ext = array_filter($ext);
foreach ($ext as $e) {
if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value'])) {
$data['wechat_count'][] = $v['id'];
} elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) <= $time) {
$data['drawing_date'][] = $v['id'];
}
}
}
//一次 二次 三次量房周期
if (isset($measuring_room_time[$v['id']])) {
$count = count($measuring_room_time[$v['id']]);
$start = $count >= 3 ? 2 : $count - 1;
$measuring_room_cycle = [];
$measuring_room_cycle[$start] = $measuring_room_time[$v['id']][$start];
// $measuring_room_cycle = array_slice($measuring_room_time[$v['id']],0, 3);
foreach ($measuring_room_cycle as $cycle_key => $cycle_item) {
$cycle_addtime = strtotime($cycle_item);
if ($addtime < $cycle_addtime) $row['room_cycle_' . $cycle_key][] = $cycle_addtime - $addtime;
$data['room_cycle_customer_' . $cycle_key][] = $v['id'];
}
}
//一次 二次 三次到店周期
if (isset($to_the_store_time[$v['id']])) {
$count = count($to_the_store_time[$v['id']]);
$start = $count >= 3 ? 2 : $count - 1;
$to_the_store_times = [];
$to_the_store_times[$start] = $to_the_store_time[$v['id']][$start];
// $to_the_store_times = array_slice($to_the_store_time[$v['id']],0, 3);
foreach ($to_the_store_times as $store_key => $store_item) {
$store_addtime = strtotime($store_item);
if ($addtime < $store_addtime) $row['strtore_' . $store_key][] = $store_addtime - $addtime;
$data['strtore_customer_' . $store_key][] = $v['id'];
}
}
//一次 二次 三次交定周期
if (isset($deposit_customer[$v['id']])) {
$count = count($deposit_customer[$v['id']]);
$start = $count >= 3 ? 2 : $count - 1;
$deposit_customer_time = [];
$deposit_customer_time[$start] = $deposit_customer[$v['id']][$start];
// $deposit_customer_time = array_slice($deposit_customer[$v['id']],0, 3);
foreach ($deposit_customer_time as $deposit_key => $deposit_item) {
$deposit_addtime = strtotime($deposit_item);
if ($addtime < $deposit_item) $row['deposit_' . $deposit_key][] = $deposit_addtime - $addtime;
$data['deposit_customer_' . $deposit_key][] = $v['id'];
}
}
//死单
if (intval($v['died']) == 2) $data['died'] += 1;
}
$data['housetype_arrow'] = $housetype_arrow;
foreach ([0, 1, 2] as $range) {
//平均一次,二次,三次量房周期
$data['room_cycle_' . $range] = $row['room_cycle_' . $range] ? ceil(array_sum($row['room_cycle_' . $range]) / count($row['room_cycle_' . $range])) : 0;
//一次,二次,三次 量房人数
$data['room_cycle_count_' . $range] = count($data['room_cycle_customer_' . $range]);
//平均到店周期
$data['strtore_' . $range] = $row['strtore_' . $range] ? ceil(array_sum($row['strtore_' . $range]) / count($row['strtore_' . $range])) : 0;
//一次 二次 三次 到店人数
$data['strtore_count_' . $range] = count($data['strtore_customer_' . $range]);
//平均交定周期
$data['deposit_' . $range] = $row['deposit_' . $range] ? ceil(array_sum($row['deposit_' . $range]) / count($row['deposit_' . $range])) : 0;
//一次 二次 三次 交定人数
$data['deposit_count_' . $range] = count($data['deposit_customer_' . $range]);
}
//平均合同金额
$signed_money = array_filter($row['signed_money']);
$data['signed_money'] = empty($signed_money) ? 0 : round(array_sum($signed_money) / count($signed_money), 2);
$data['signed_money_all'] = array_sum($signed_money);
//房屋类型
$data['existing_homes_count'] = 0; //现房数量
$data['forward_housing_count'] = 0; //期房数量
foreach ($house_type as $house_type_item) {
if (strpos($house_type_item, '现房') !== false) {
$data['existing_homes_count'] += 1;
} elseif (strpos($house_type_item, '期房') !== false) {
$data['forward_housing_count'] += 1;
}
}
//房屋面积
$data['square'] = $data['square_0_80'] = $data['square_80_100'] = $data['square_100_120'] = $data['square_120_200'] = $data['square_200_500'] = $data['square_500'] = 0;
foreach ($square as $square_item) {
if (!$square_item) continue;
$data['square'] += 1;
if ($square_item <= 80) {
$data['square_0_80'] += 1;
} elseif ($square_item <= 100) {
$data['square_80_100'] += 1;
} elseif ($square_item <= 120) {
$data['square_100_120'] += 1;
} elseif ($square_item <= 200) {
$data['square_120_200'] += 1;
} elseif ($square_item <= 500) {
$data['square_200_500'] += 1;
} else {
$data['square_500'] += 1;
}
}
return $data;
}
/**
* 弹框页面
*/
public function generalOpen()
{
$root_id = request()->employee->root_id;
$eid = input('eid', '', 'intval');
View::assign('eid', $eid);
$type = input('type', '');
$start_date = input('start_date', '');
$end_date = input('end_date', '');
if ($start_date) {
$ls_date = explode(' - ', $start_date);
if (count($ls_date) == 2) {
$start_date = $ls_date[0];
$end_date = $ls_date[1];
}
}
if ($type == 'assign') {
//待指派
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话']
];
$url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
} elseif ($type == 'share') {
//调用内容
$field = [
['field' => 'share_time', 'title' => '分享时间'],
['field' => 'title', 'title' => '分享内容']
];
$url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
} elseif ($type == 'yuji_store') {
//预计到店
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'date', 'title' => '预约时间']
];
$url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
} elseif ($type == 'number_of_explanations') {
//智慧屏讲解客户列表
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话'],
['field' => 'addtime', 'title' => '讲解时间']
];
$url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
} elseif ($type == 'invalid') {
//无效客资
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话']
];
$url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date, 'key' => input('key', '')]);
} else {
//加微客户
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话']
];
$url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
}
View::assign('fields', json_encode($field));
View::assign('url', $url);
return View::fetch('add_wechat');
}
/**
* 弹框列表
*/
public function generalOpenData()
{
$param = Request::only(['page' => 1, 'limit' => 10, 'eid' => 0, 'type' => '', 'start_date' => '', 'end_date' => '']);
$page = $param['page'];
$limit = $param['limit'];
$root_id = request()->employee->root_id;
$where = [
['employee_id', '=', $param['eid']]
];
$sdate = strtotime('2015-01-01');
$edate = time();
if ($param['start_date'] && $param['end_date']) {
$sdate = strtotime($param['start_date']);
$edate = strtotime($param['end_date']);
}
$start_date = date('Y-m-d 00:00:00', $sdate);
$end_date = date('Y-m-d 23:59:59', $edate);
if ($param['type'] == 'wechat') {
$where[] = ['add_wechat_time', 'between', [$start_date, $end_date]];
$wuxiao_state = Customer::changeState('无效', 'chaos');
$w_where[] = ['add_wechat_time', 'between', [$start_date, $end_date]];
$w_where[] = ['employee_id', 'null', null];
$w_where[] = ['state', 'in', $wuxiao_state];
$ids_wuxiao = Customer::where($w_where)->column('id'); //这是加微的,但是置为无效的客户
$j_w_where[] = ['customer_id', 'in', $ids_wuxiao];
$j_w_where[] = ['employee_id', '=', $param['eid']];
// 无效的加微客户id
$jiav_wuxiao_log = CustomerInvalidLog::where($j_w_where)->column('customer_id');
// 所属员工的加微客户id
$customer_ids = Customer::where($where)->column('id');
$ids = array_unique(array_merge($customer_ids, $jiav_wuxiao_log));
$where1[] = ['id', 'in', $ids];
$where2[] = ['customer_id', 'in', $ids];
$data = $this->selCustomerValid($where1, $where2, $page, $limit, []);
$list = $data['list'];
$count = $data['count'];
} elseif ($param['type'] == 'drawing') {
$field = 'drawing_date';
$vis_where[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where[] = ['Customer.ext', 'like', '%' . $field . '%'];
$vis_where[] = ['Customer.died', '<>', 2];
$vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$ext = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where)
->group('Customer.id')
->column('Customer.id,Customer.ext');
$cid = [];
foreach ($ext as $v) {
$json = json_decode($v['ext'], true);
foreach ($json as $v2) {
if (!isset($v2['keyname'])) continue;
if ($v2['keyname'] == $field && !empty($v2['value']) && strtotime($v2['value']) >= $sdate && strtotime($v2['value']) <= $edate) {
$cid[] = $v['id'];
}
}
}
$where[] = ['id', 'in', $cid];
$count = Customer::where($where)->count();
$list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
} elseif ($param['type'] == 'share') {
//调用内容
$share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
$wheres = [
['employee_id', '=', $param['eid']],
['type', 'in', $share_where],
['share_time', 'between', [$start_date, $end_date]]
];
$model = ShareLog::with('content')->where($wheres)->order('id desc');
$count = $model->count();
$list = $model->page($page, $limit)->select()->toArray();
$arr = [
'Activity' => '#活动#',
'Article' => '#图文素材#',
'Building' => '#楼盘进度#',
'Card' => '#名片#',
'CompanyStrength' => '#公司实力#',
'Construction' => '#在施工地#',
'CustomerJsAll' => '',
'MaterialCase' => '#装修案例#',
'MaterialEvidence' => '#口碑见证#',
'ToolAll' => '#谈单工具#',
'Video' => '#视频#',
'Weiwang' => '#个人微网#'
];
foreach ($list as $k => $v) {
$type = isset($arr[$v['type']]) ? $arr[$v['type']] : '';
$field = $v['type'] == 'Building' ? 'name' : 'title';
if ($v['content'] && isset($v['content'][$field])) {
$list[$k]['title'] = $type . '《' . $v['content'][$field] . '》';
} else {
$list[$k]['title'] = '暂无';
}
}
} elseif ($param['type'] == 'resource') { //分配
$where[] = ['crm_res_id', 'not null', null];
$where[] = ['died', '<>', 2];
$where[] = ['employee_time', 'between', [$start_date, $end_date]];
$where[] = ['employee_id', '=', $param['eid']];
$ids = Customer::where($where)->column('id');
// 无效客资来自资源分配
$i_where[] = ['employee_id', '=', $param['eid']];
$i_where[] = ['cus_employee_time', 'between', [$start_date, $end_date]];
$wuxiao_ids = CustomerInvalidLog::where($i_where)->column('customer_id');
$ids = array_merge($ids, $wuxiao_ids);
$where1[] = ['id', 'in', $ids];
$where1[] = ['crm_res_id', '>', 0];
$where2[] = ['customer_id', 'in', $ids];
$where2[] = ['crm_res_id', '>', 0];
$data = $this->selCustomerValid($where1, $where2, $page, $limit, []);
$list = $data['list'];
$count = $data['count'];
} elseif ($param['type'] == 'reported_quantity') {
// 报备客户数
$where[] = ['crm_res_id', 'null', null];
$where[] = ['addtime', 'between', [$start_date, $end_date]];
$where[] = ['employee_id', '=', $param['eid']];
$where[] = ['state', 'not null', null];
$count = Customer::where($where)->count();
$list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
/* $ids = Customer::where($where)->column('id');
// 无效客资
$i_where[] = ['employee_id', '=', $param['eid']];
$i_where[] = ['cus_addtime', 'between', [$start_date, $end_date]];
$i_where[] = ['is_resource', '=', 0];
$wuxiao_ids = CustomerInvalidLog::where($i_where)->column('customer_id');
$ids = array_merge($ids, $wuxiao_ids);
$where1[] = ['id', 'in', $ids];
$where1[] = ['crm_res_id', 'null', null];
$where2[] = ['customer_id', 'in', $ids];
$where2[] = ['crm_res_id', 'null', null];
$data = $this->selCustomerValid($where1, $where2, $page, $limit, []);
$list = $data['list'];
$count = $data['count']; */
} elseif ($param['type'] == 'foolow') { //在跟进
//客户总数
$vis_where[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where[] = ['Customer.died', '<>', 2];
$vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$ids = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where)
->where(function ($query) {
$or1[] = ['Customer.crm_res_id', 'NULL', null];
$or1[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')];
$or2[] = ['Customer.crm_res_id', 'NOT NULL', null];
$or2[] = ['Customer.state', 'not in', array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos'))];
$query->whereOr([$or1, $or2]);
})
->group('CustomerVisitLog.customer_id')
->column('CustomerVisitLog.customer_id');
$where[] = ['id', 'in', $ids];
$count = Customer::where($where)->count();
$list = Customer::where($where)->field('id,name,phone')->page($page, $limit)->select()->toArray();
} elseif (in_array($param['type'], ['dep', 'sign_count', 'room'])) {
//量房,到店,签单,转单客户数
$all_state_type = ['room' => '已量房', 'dep' => '已交定', 'sign_count' => '已签单', 'invalid_customer_count' => '无效'];
if (empty($all_state_type[$param['type']])) return json(['code' => 1, 'data' => '', 'count' => 0, 'msg' => '参数错误']);
$db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
switch ($param['type']) {
case 'room':
$state = CustomerVisitLog::changeState('已量房', 'chaos');
break;
case 'dep':
$state = CustomerVisitLog::changeState('已交定', 'chaos');
break;
case 'sign_count':
$state = CustomerVisitLog::changeState('已签单', 'chaos');
break;
break;
default:
$state = '';
break;
}
if ($state) {
$db_where[] = ['CustomerVisitLog.state', 'in', $state];
}
$db_where[] = ['Customer.employee_id', '=', $param['eid']];
$db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
$cids = Db::view('Customer', 'id')
->view('CustomerVisitLog', [], 'Customer.id=CustomerVisitLog.customer_id')
->where($db_where)
->group('Customer.id')
->column('Customer.id');
$count = Customer::where([['id', 'in', $cids]])->count();
$list = Customer::where([['id', 'in', $cids]])->field('name,phone')->page($page, $limit)->select()->toArray();
} elseif ($param['type'] == 'valid') {
//有效客户数量
$state1 = Customer::changeState('待确认', 'chaos');
$state2 = Customer::changeState('无效', 'chaos');
$vis_where[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where[] = ['Customer.state', 'not in', array_merge($state1, $state2)];
$vis_where[] = ['Customer.died', '<>', 2];
$vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$ids = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where)
->group('CustomerVisitLog.customer_id')
->column('CustomerVisitLog.customer_id');
//2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H
$no_where[] = ['employee_id', '=', $param['eid']];
$no_where[] = ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')];
$no_where[] = ['addtime', 'between', [$start_date, $end_date]];
$no_valid_customer = CustomerVisitLog::where($no_where)->group('customer_id')->column('customer_id');
$ids = array_unique(array_merge($ids, $no_valid_customer));
$where1[] = ['id', 'in', $ids];
$where2[] = ['customer_id', 'in', $ids];
$data = $this->selCustomerValid($where1, $where2, $page, $limit, []);
$list = $data['list'];
$count = $data['count'];
} elseif ($param['type'] == 'valid_no_wechat') {
//有效客户数量
$state1 = Customer::changeState('待确认', 'chaos');
$state2 = Customer::changeState('无效', 'chaos');
$vis_where[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where[] = ['Customer.state', 'not in', array_merge($state1, $state2)];
$vis_where[] = ['Customer.died', '<>', 2];
$vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$list = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where)
->group('Customer.id')
->column('Customer.id,Customer.ext');
$cid = [];
foreach ($list as $k => $v) {
if ($v['ext']) {
$json = json_decode($v['ext'], true);
$i = 0;
foreach ($json as $v2) {
if (!isset($v2['keyname'])) continue;
if ($v2['keyname'] == 'add_wechat_time' && !empty($v2['value'])) {
$i++;
continue;
}
}
if ($i == 0) $cid[] = $v['id'];
} else {
$cid[] = $v['id'];
}
}
$count = Customer::where([['id', 'in', $cid]])->count();
$list = Customer::where([['id', 'in', $cid]])->field('name,phone')->page($page, $limit)->select()->toArray();
} elseif ($param['type'] == 'room_no_store') {
//量房未到店
$state3 = CustomerVisitLog::changeState('已量房', 'chaos');
$state4 = CustomerVisitLog::changeState('已到店', 'chaos');
$db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$db_where[] = ['Customer.employee_id', '=', $param['eid']];
$db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
$db_where1 = $db_where;
$db_where1[] = ['CustomerVisitLog.state', 'in', $state3];
$cids1 = Db::view('Customer')
->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
->where($db_where1)
->group('Customer.id')
->column('Customer.id');
$db_where2 = $db_where;
$db_where2[] = ['CustomerVisitLog.state', 'in', $state4];
$cids2 = Db::view('Customer')
->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
->where($db_where2)
->group('Customer.id')
->column('Customer.id');
$cids = array_diff($cids1, $cids2);
$count = Customer::where([['id', 'in', $cids]])->count();
$list = Customer::where([['id', 'in', $cids]])->field('name,phone')->page($page, $limit)->select()->toArray();
} elseif ($param['type'] == 'yuji_store') {
//预计到店
$sub_where[] = ['CustomersSubscribe.employee_id', '=', $param['eid']];
$sub_where[] = ['CustomersSubscribe.type', '=', 1];
$sub_where[] = ['CustomersSubscribe.state', '=', 0];
$sub_where[] = ['CustomersSubscribe.addtime', 'between', [$start_date, $end_date]];
$count = Db::view('CustomersSubscribe')
->view('Customer', 'id', 'CustomersSubscribe.customer_id=Customer.id')
->where($sub_where)->count();
$list = Db::view('CustomersSubscribe')
->view('Customer', 'id', 'CustomersSubscribe.customer_id=Customer.id')
->where($sub_where)
->page($page, $limit)
->column('CustomersSubscribe.customer_id,CustomersSubscribe.subscribe_date date,customer.name');
} elseif (in_array($param['type'], ['strtore_count_0', 'strtore_count_1', 'strtore_count_2'])) {
$arr = ['strtore_count_0' => 'count(CustomerVisitLog.id)=1', 'strtore_count_1' => 'count(CustomerVisitLog.id)=2', 'strtore_count_2' => 'count(CustomerVisitLog.id)>=3'];
$state3 = CustomerVisitLog::changeState('已到店', 'chaos');
$vis_where[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$vis_where[] = ['CustomerVisitLog.state', 'in', $state3];
$vislog_list = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where)
->where([['CustomerVisitLog.is_merge', '=', 0]])
->group('CustomerVisitLog.customer_id')
->having($arr[$param['type']])
->column('CustomerVisitLog.customer_id');
$where[] = ['id', 'in', $vislog_list];
$count = Customer::where($where)->count();
$list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
} elseif (in_array($param['type'], ['strtore_deposit_count_0', 'strtore_deposit_count_1', 'strtore_deposit_count_2'])) {
//到店签单
$arr = ['strtore_deposit_count_0' => 'count(CustomerVisitLog.id)=1', 'strtore_deposit_count_1' => 'count(CustomerVisitLog.id)=2', 'strtore_deposit_count_2' => 'count(CustomerVisitLog.id)>=3'];
$state3 = CustomerVisitLog::changeState('已到店', 'chaos');
$vis_where1[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where1[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$vis_where1[] = ['CustomerVisitLog.state', 'in', $state3];
$customer_ids = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where1)
->where([['CustomerVisitLog.is_merge', '=', 0]])
->group('CustomerVisitLog.customer_id')
->having($arr[$param['type']])
->column('CustomerVisitLog.customer_id');
$state2 = CustomerVisitLog::changeState('已交定', 'chaos');
$vis_where2[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where2[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$vis_where2[] = ['CustomerVisitLog.state', 'in', $state2];
$customer_ids_ding = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where2)
->where([['CustomerVisitLog.is_merge', '=', 0]])
->group('CustomerVisitLog.customer_id')
->column('CustomerVisitLog.customer_id');
$where[] = ['id', 'in', array_intersect($customer_ids, $customer_ids_ding)];
$count = Customer::where($where)->count();
$list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
} elseif (in_array($param['type'], ['square_0_80', 'square_80_100', 'square_100_120', 'square_120_200', 'square_200_500', 'square_500'])) {
$arr = [
'square_0_80' => [0, 80],
'square_80_100' => [80, 100],
'square_100_120' => [101, 120],
'square_120_200' => [121, 200],
'square_200_500' => [201, 500],
'square_500' => [501, 999999],
];
$vis_where[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where[] = ['Customer.died', '<>', 2];
$vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$square = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where)
->where(function ($query) {
$or1[] = ['Customer.crm_res_id', 'NULL', null];
$or1[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')];
$or2[] = ['Customer.crm_res_id', 'NOT NULL', null];
$or2[] = ['Customer.state', 'not in', array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos'))];
$query->whereOr([$or1, $or2]);
})
->group('Customer.id')
->column('Customer.id,Customer.square');
$cid = [];
foreach ($square as $v) {
if ($v['square'] && $v['square'] > $arr[$param['type']][0] && $v['square'] <= $arr[$param['type']][1]) $cid[] = $v['id'];
}
$where[] = ['id', 'in', $cid];
$count = Customer::where($where)->count();
$list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
} elseif ($param['type'] == 'existing_homes_count' || $param['type'] == 'forward_housing_count') {
//现房 期房客户
$field = $param['type'] == 'existing_homes_count' ? '现房' : '期房';
$vis_where[] = ['Customer.employee_id', '=', $param['eid']];
$vis_where[] = ['Customer.died', '<>', 2];
$vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
$vis_where[] = ['Customer.house_type', 'like', '%' . $field . '%'];
$vis_where[] = ['state', 'not in', array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos'))];
$ids = Db::view('CustomerVisitLog', 'customer_id')
->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
->where($vis_where)
->group('Customer.id')
->column('Customer.id');
$where[] = ['id', 'in', $ids];
$model = Customer::where($where)->field('name,phone');
$count = $model->count();
$list = $model->page($page, $limit)->select()->toArray();
} elseif ($param['type'] == 'number_of_explanations') {
$db_where[] = ['CustomerVisitLog.remark', 'like', '%讲解智慧屏##%'];
$db_where[] = ['CustomerVisitLog.addtime', 'between', [$start_date, $end_date]];
$db_where[] = ['Customer.employee_id', '=', $param['eid']];
$db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
$model = Db::view('Customer', ['name', 'phone'])
->view('CustomerVisitLog', 'customer_id,addtime', 'Customer.id=CustomerVisitLog.customer_id')
->where($db_where);
$count = $model->count();
$list = $model->page($page, $limit)->select()->toArray();
$aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
foreach ($list as $k => $v) {
$list[$k]['phone'] = $aec->decrypt($v['phone']);
}
} elseif ($param['type'] == 'invalid') {
$key = input('key', '');
if ($key === '') {
return json(['code' => 0, 'data' => [], 'count' => [], 'msg' => '获取成功']);
}
$setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'clueTag']])->value('content');
if (empty($setting)) return json(['code' => 0, 'data' => [], 'count' => [], 'msg' => '获取成功']);
$setting = explode(',', $setting)[$key];
$where[] = ['employee_id', '=', $param['eid']];
$where[] = ['confirm_date', 'between', [$start_date, $end_date]];
$where[] = ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')];
$where[] = ['remark', 'like', '%' . $setting . '%'];
$customer_ids = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
$count = Customer::where([['id', 'in', $customer_ids]])->count();
$list = Customer::where([['id', 'in', $customer_ids]])->field('name,phone')->page($page, $limit)->select()->toArray();
}
if ($param['type'] == 'share') {
foreach ($list as $k => $v) {
$type = isset($arr[$v['type']]) ? $arr[$v['type']] : '';
$field = $v['type'] == 'Building' ? 'name' : 'title';
if ($v['content'] && isset($v['content'][$field])) {
$list[$k]['title'] = $type . '《' . $v['content'][$field] . '》';
} else {
$list[$k]['title'] = '暂无';
}
}
} elseif ($param['type'] == 'yuji_store') {
/* foreach ($list as $k => $v) {
$list[$k]['name'] = isset($customer_ids[$v['customer_id']]) ? $customer_ids[$v['customer_id']] : '未知';
} */
} else {
foreach ($list as $k => $v) {
$list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
$list[$k]['name'] = empty($v['name']) ? '未知' : $v['name'];
}
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
}
/**
* 有效客户联查(包含回收站)
*/
public function selCustomerValid($condition, $con_union, $page, $limit, $order)
{
$prefix = env('database.prefix');
$model = Db::table($prefix . 'customer')
->field('id,designer_id,employee_id,name,community_name,phone,state,addtime,phone1,phone2,remark,ext,deposit_money,signed_money,org_id')
->where($condition)
->union(function ($query) use ($con_union) {
$prefix = env('database.prefix');
$query->field('customer_id id,designer_id,employee_id,name,community_name,phone,state,addtime,phone1,phone2,remark,ext,deposit_money,signed_money,org_id')->where($con_union)->table($prefix . 'customer_recycle');
});
$count = count($model->select()->toArray());
$list = $model->page($page, $limit)->order($order)->select()->toArray();
$aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
foreach ($list as $k => $v) {
$list[$k]['state'] = Customer::changeState($v['state'], 'state');
$phone = $aec->decrypt($v['phone']);
$phone1 = !empty($v['phone1']) ? $aec->decrypt($v['phone1']) : '';
$phone2 = !empty($v['phone2']) ? $aec->decrypt($v['phone2']) : '';
$list[$k]['phone'] = substr_replace($phone, '******', 3, 6);
$list[$k]['phone1'] = $phone1 ? substr_replace($phone1, '******', 3, 6) : '';
$list[$k]['phone2'] = $phone2 ? substr_replace($phone2, '******', 3, 6) : '';
$employee = Employee::find($v['employee_id']);
$list[$k]['employee_name'] = '';
if (!empty($employee)) {
$list[$k]['employee_name'] = $employee['name'];
}
$designer = Employee::find($v['designer_id']);
$list[$k]['designer_name'] = '';
if (!empty($designer)) {
$list[$k]['designer_name'] = $designer['name'];
}
$list[$k]['org_name'] = '';
if ($v['org_id']) {
$org = Org::where('id', '=', $v['org_id'])->find();
if (!empty($org)) {
$list[$k]['org_name'] = $org['name'];
}
}
}
$data = ['count' => $count, 'list' => $list];
return $data;
}
/**
* 房屋状态
*/
private function getHouseStatus($house_status, $field)
{
$data = [];
$name = array_column($field, 'name');
$house = [];
foreach ($house_status as $v) {
@$house[$v] += 1;
}
foreach ($field as $k2 => $v2) {
$data['house_status_' . $v2['id']] = isset($house[$v2['name']]) ? $house[$v2['name']] : 0;
}
return $data;
}
/**
* 房屋户型
*/
private function getHousetypeArrow($housetype, $field, $sign_ids)
{
$data = [];
$house = [];
foreach ($housetype as $k => $v) {
@$house[$v][] = $k; // $v 是户型id,$k是客户id
}
// 每个户型的成交率
foreach ($field as $k => $v) {
$v_customer = $house[$v['id']] ?? [];
$v_sign = array_intersect($v_customer, $sign_ids);
$data['housetype_arrow_lv_' . $v['id']] = count($v_customer) > 0 ? round(count($v_sign) / count($v_customer) * 100, 2) . '%' : '0%';
}
// 每个户型的客户数
foreach ($field as $k => $v) {
$data['housetype_arrow_' . $v['id']] = isset($house[$v['id']]) ? count($house[$v['id']]) : 0;
}
return $data;
}
/**
* 加微数量
* $wechat 业务员的客户数组
* $sdate 开始时间筛选
* $edate 结束时间筛选
* $measuring_room_time 客户量房时间 计算平均量房时间
* $to_the_store_time 客户到店时间 计算平均到店时间
* $deposit_customer 客户交定时间 计算平均交定周期
*/
private function getWechatCount($wechat, $sdate, $edate, $measuring_room_time, $to_the_store_time, $deposit_customer)
{
$time = time();
$data['wechat_count'] = []; //加微客户id
$data['is_resource'] = []; //资源库分配客户id
$data['drawing_date'] = []; //有户型图的客户
$row['room_cycle_0'] = $row['room_cycle_1'] = $row['room_cycle_2'] = []; //一次 二次 三次量房周期
$row['strtore_0'] = $row['strtore_1'] = $row['strtore_2'] = []; //一次 二次 三次到店周期
$row['deposit_0'] = $row['deposit_1'] = $row['deposit_2'] = []; //一次 二次 三次交定周期
$data['room_cycle_customer_0'] = $data['room_cycle_customer_1'] = $data['room_cycle_customer_2'] = []; //一次 两次 三次量房客户
$data['strtore_customer_0'] = $data['strtore_customer_1'] = $data['strtore_customer_2'] = []; //一次 两次 三次到店客户
$data['deposit_customer_0'] = $data['deposit_customer_1'] = $data['deposit_customer_2'] = []; //一次 两次 三次交定客户
$row['signed_money'] = []; //合同金额
$house_type = []; //房屋类型
$square = []; //房屋面积
$data['house_status'] = []; //房屋状态
//报备客户
$data['reported_quantity'] = 0;
//死单客户
$data['dead_order'] = 0;
foreach ($wechat as $v) {
//客户添加时间
$addtime = strtotime($v['addtime']);
//资源库分配数量
if ($v['crm_res_id']) $data['is_resource'][] = $v['id'];
$row['signed_money'][] = $v['signed_money']; //合同金额
$house_type[] = $v['house_type'];
$square[] = $v['square'];
$data['house_status'][] = $v['house_status'];
//加微客户,有户型图客户
$v2 = $v['ext'];
$ext = json_decode($v2, true);
if (!empty($ext) && is_array($ext)) {
$ext = array_filter($ext);
foreach ($ext as $e) {
if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) {
$data['wechat_count'][] = $v['id'];
} elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) <= $time) {
$data['drawing_date'][] = $v['id'];
}
}
}
//一次 二次 三次量房周期
if (isset($measuring_room_time[$v['id']])) {
$count = count($measuring_room_time[$v['id']]);
$start = $count >= 3 ? 2 : $count - 1;
$measuring_room_cycle = [];
$measuring_room_cycle[$start] = $measuring_room_time[$v['id']][$start];
// $measuring_room_cycle = array_slice($measuring_room_time[$v['id']],0, 3);
foreach ($measuring_room_cycle as $cycle_key => $cycle_item) {
$cycle_addtime = strtotime($cycle_item);
if ($addtime < $cycle_addtime) $row['room_cycle_' . $cycle_key][] = $cycle_addtime - $addtime;
$data['room_cycle_customer_' . $cycle_key][] = $v['id'];
}
}
//一次 二次 三次到店周期
if (isset($to_the_store_time[$v['id']])) {
$count = count($to_the_store_time[$v['id']]);
$start = $count >= 3 ? 2 : $count - 1;
$to_the_store_times = [];
$to_the_store_times[$start] = $to_the_store_time[$v['id']][$start];
// $to_the_store_times = array_slice($to_the_store_time[$v['id']],0, 3);
foreach ($to_the_store_times as $store_key => $store_item) {
$store_addtime = strtotime($store_item);
if ($addtime < $store_addtime) $row['strtore_' . $store_key][] = $store_addtime - $addtime;
$data['strtore_customer_' . $store_key][] = $v['id'];
}
}
//一次 二次 三次交定周期
if (isset($deposit_customer[$v['id']])) {
$count = count($deposit_customer[$v['id']]);
$start = $count >= 3 ? 2 : $count - 1;
$deposit_customer_time = [];
$deposit_customer_time[$start] = $deposit_customer[$v['id']][$start];
// $deposit_customer_time = array_slice($deposit_customer[$v['id']],0, 3);
foreach ($deposit_customer_time as $deposit_key => $deposit_item) {
$deposit_addtime = strtotime($deposit_item);
if ($addtime < $deposit_item) $row['deposit_' . $deposit_key][] = $deposit_addtime - $addtime;
$data['deposit_customer_' . $deposit_key][] = $v['id'];
}
}
//报备客户
// if () {
// # code...
// }
//死单
if ($v['dead_order'] == 2) $data['dead_order'] += 1;
}
foreach ([0, 1, 2] as $range) {
//平均一次,二次,三次量房周期
$data['room_cycle_' . $range] = $row['room_cycle_' . $range] ? ceil(array_sum($row['room_cycle_' . $range]) / count($row['room_cycle_' . $range])) : 0;
//一次,二次,三次 量房人数
$data['room_cycle_count_' . $range] = count($row['room_cycle_' . $range]);
//平均到店周期
$data['strtore_' . $range] = $row['strtore_' . $range] ? ceil(array_sum($row['strtore_' . $range]) / count($row['strtore_' . $range])) : 0;
//一次 二次 三次 到店人数
$data['strtore_count_' . $range] = count($row['strtore_' . $range]);
//平均交定周期
$data['deposit_' . $range] = $row['deposit_' . $range] ? ceil(array_sum($row['deposit_' . $range]) / count($row['deposit_' . $range])) : 0;
//一次 二次 三次 交定人数
$data['deposit_count_' . $range] = count($row['deposit_' . $range]);
}
//平均合同金额
$signed_money = array_filter($row['signed_money']);
$data['signed_money'] = empty($signed_money) ? 0 : round(array_sum($signed_money) / count($signed_money), 2);
//房屋类型
$data['existing_homes_count'] = 0; //现房数量
$data['forward_housing_count'] = 0; //期房数量
foreach ($house_type as $house_type_item) {
if (strpos($house_type_item, '现房') !== false) {
$data['existing_homes_count'] += 1;
} elseif (strpos($house_type_item, '期房') !== false) {
$data['forward_housing_count'] += 1;
}
}
//房屋面积
$data['square_0_80'] = $data['square_80_100'] = $data['square_100_120'] = $data['square_120_200'] = $data['square_200_500'] = $data['square_500'] = 0;
foreach ($square as $square_item) {
if (!$square_item) continue;
if ($square_item <= 80) {
$data['square_0_80'] += 1;
} elseif ($square_item <= 100) {
$data['square_80_100'] += 1;
} elseif ($square_item <= 120) {
$data['square_100_120'] += 1;
} elseif ($square_item <= 200) {
$data['square_120_200'] += 1;
} elseif ($square_item <= 500) {
$data['square_200_500'] += 1;
} else {
$data['square_500'] += 1;
}
}
return $data;
}
/**
* 行为统计页面
*/
public function behaviorStatistics()
{
//返回组织列表
$org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
View::assign('org', $org);
return View::fetch();
}
/**
* 行为统计接口
* 日期截止到 2022011-20
*/
public function behaviorStatisticsData($page = 1, $limit = 10)
{
$param = Request::only(['page' => 1, 'start_date' => '', 'end_date' => '', 'keyword' => '', 'org_id' => '']);
$root_id = request()->employee->root_id;
if ($param['start_date']) {
$count_dates = explode(' - ', $param['start_date']);
$start_date = strtotime($count_dates[0]);
$end_date = strtotime($count_dates[1]);
} else {
$start_date = strtotime('2022-11-20');
$end_date = strtotime(date('Y-m-d'));
}
//登录记录
$login_where = [
['root_id', '=', $root_id],
['content', '=', '小程序登录'],
['addtime', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]]
];
$login = OperateLog::where($login_where)->field("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
$login = array_column($login, 'indexx');
if ($param['keyword']) {
$where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
}
$where[] = ['root_id', '=', $root_id];
$where[] = ['uid', '>', 0];
$where[] = ['state', 'in', ['在职', '离职']];
if (!empty($param['org_id'])) {
//部门筛选
$org = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
$orgIds = Org::where([['path', 'like', $org['path'] . '%']])->column('id');
$where[] = ['org_id', 'in', $orgIds];
}
$employees = Employee::where($where)->column('id,name,org_id,addtime,state,termination_time');
$day = ($end_date - $start_date) / 86400;
$list = [];
for ($i = 0; $i <= $day; $i++) {
$date = date('Y-m-d', $end_date - $i * 86400);
$time = strtotime($date) + 86399;
foreach ($employees as $k => $v) {
$addtime = strtotime($v['addtime']);
$now_date = date('Y-m-d', strtotime($date));
$indexx = $v['id'] . '@' . $now_date;
$termination_time = strtotime(date('Y-m-d 23:59:59', strtotime($v['termination_time'])));
// if ($time >= $addtime && in_array($indexx,$login)) {
$status_state = $v['state'] == '离职' ? ($termination_time >= $time ? true : false) : true;
if ($time >= $addtime && $status_state) {
$j = $v;
$j['x_date'] = date('Y年m月d日', strtotime($date));
$j['date'] = $now_date;
$j['index'] = $indexx;
$list[] = $j;
}
}
}
$count = count($list);
$list = array_slice($list, $limit * ($page - 1), $limit);
// $dates = array_unique(array_column($list,'date'));
$eids = array_unique(array_column($list, 'id'));
$query1 = ['employee_id', 'in', $eids];
$query2 = ['share_time', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]];
$query3 = ['addtime', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]];
$custoemr_where = [
['employee_id', 'in', $eids]
];
$customer = Customer::where($custoemr_where)->column('id,ext,employee_id,house_delivery_time');
//加微客户 预计转施工时间
$add_wechat_date = [];
$house_delivery_time = [];
foreach ($customer as $v1) {
$v2 = $v1['ext'];
$ext = json_decode($v2, true);
if (!empty($ext) && is_array($ext)) {
$ext = array_filter($ext);
foreach ($ext as $e) {
if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value'])) {
@$add_wechat_date[$v1['employee_id'] . '@' . date('Y-m-d', strtotime($e['value']))] += 1;
} elseif (isset($e['keyname']) && $e['keyname'] == 'will_transfer_to_construction_date' && !empty($e['value'])) {
@$house_delivery_time[$v1['employee_id'] . '@' . date('Y-m-d', strtotime($e['value']))] += 1;
}
}
}
}
//调用内容数量,分享内容次数
$share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
$share = ShareLog::where([$query1, $query2, ['type', 'in', $share_where]])->field("id,concat(employee_id,'@',DATE_FORMAT(share_time,'%Y-%m-%d')) as indexx,concat(employee_id,type,data_id,DATE_FORMAT(share_time,'%Y-%m-%d')) as str,employee_id")->select()->toArray();
$shares = [];
foreach ($share as $share_v) {
@$shares[$share_v['indexx']] += 1;
}
///培训资料是否看了 课程是否学习过
$look = TrainCourseView::where([$query1, $query3, ['time', '>', 0]])->column("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
$look = array_unique(array_column($look, 'indexx'));
//考核次数 通过次数
$don = ExamEmpResult::with(['paperInfo' => function ($query) {
$query->field('id,base_score')->bind(['base_score' => 'base_score']);
}])->where([$query1, $query3, ['state', '=', 1]])->field("id,paper_id,final_score,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->select()->toArray();
$don1 = $don2 = []; //是否有考核,考核是否通过
foreach ($don as $vv) {
@$don1[$vv['indexx']] += 1; //考核次数
if ($vv['final_score'] >= $vv['base_score']) @$don2[$vv['indexx']] += 1; //考核通过次数
}
//客户跟进数量
$vislog = CustomerVisitLog::where([
['employee_id|customer_employee_id', 'in', $eids], $query3
])->column("state,IF(employee_id,employee_id,customer_employee_id) as employee,DATE_FORMAT(addtime,'%Y-%m-%d') as date,customer_id,concat(IF(employee_id,employee_id,customer_employee_id),'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
$log1 = $log2 = []; //跟进次数 , 跟进客户数
$state1 = CustomerVisitLog::changeState('已量房', 'chaos');
$state2 = CustomerVisitLog::changeState('已到店', 'chaos');
$state3 = CustomerVisitLog::changeState('已交定', 'chaos');
$state4 = CustomerVisitLog::changeState('已签单', 'chaos');
$state_count1 = $state_count2 = $state_count3 = $state_count4 = [];
foreach ($vislog as $ke => $va) {
@$log1[$va['indexx']] += 1;
@$log2[$va['indexx']][] = $va['customer_id'];
if (in_array($va['state'], $state1)) {
@$state_count1[$va['indexx']][] = $va['customer_id'];
} elseif (in_array($va['state'], $state2)) {
@$state_count2[$va['indexx']][] = $va['customer_id'];
} elseif (in_array($va['state'], $state3)) {
@$state_count3[$va['indexx']][] = $va['customer_id'];
} elseif (in_array($va['state'], $state4)) {
@$state_count4[$va['indexx']][] = $va['customer_id'];
}
}
//预约记录
$query4 = [
['state', '=', 0],
['type', 'in', [1, 3]],
['employee_id', 'in', $eids],
['subscribe_date', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]]
];
$sub1 = $sub2 = []; //1到店 2量房
$subscribe = CustomersSubscribe::where($query4)->column("employee_id,customer_id,subscribe_date,type,concat(employee_id,'@',DATE_FORMAT(subscribe_date,'%Y-%m-%d')) as indexx");
foreach ($subscribe as $v4) {
$v4['type'] == 1 ? @$sub1[$v4['indexx']] += 1 : @$sub2[$v4['indexx']] += 1;
}
//话术浏览次数
$talk_view = TalkskillViewLog::where([$query1, $query3])->column("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
$talk_count = [];
foreach ($talk_view as $t_item) {
@$talk_count[$t_item['indexx']] += 1;
}
//智慧屏讲解次数
$zhihuis = CustomerVisitLog::where([['employee_id|customer_employee_id', 'in', $eids], $query3, ['remark', 'like', '%讲解智慧屏##%']])
->field("IF(employee_id,employee_id,customer_employee_id) as employee,DATE_FORMAT(addtime,'%Y-%m-%d') as date,customer_id,concat(IF(employee_id,employee_id,customer_employee_id),'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->select()->toArray();
$zhihui = [];
foreach ($zhihuis as $v) {
@$zhihui[$v['indexx']] += 1;
}
// 外呼系统统计
$outCallLog = OutCallLog::where([['employee_id', 'in', $eids]])->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
$outCallLog = array_combine(array_column($outCallLog, 'indexx'), $outCallLog);
// 手机打电话次数获取
$outCallMbLog = OutCallMbLog::where([
['employee_id', 'in', $eids]
])->field("count(id) as phone_count,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
$outCallMbLog = array_combine(array_column($outCallMbLog, 'indexx'), $outCallMbLog);
$org = Org::where([['id', 'in', array_column($list, 'org_id')]])->column('name', 'id');
foreach ($list as $key => $val) {
//部门名称
$list[$key]['org_name'] = isset($org[$val['org_id']]) ? $org[$val['org_id']] : '无';
//电话量
$list[$key]['phone_count'] = 0;
//接通量
$list[$key]['on_phone_count'] = 0;
//未接通
$list[$key]['off_phone_count'] = 0;
//平均通话时长
$list[$key]['avg_phone_time'] = 0;
if (isset($outCallLog[$val['index']])) {
//电话量
$list[$key]['phone_count'] = $outCallLog[$val['index']]['phone_count'];
//接通量
$list[$key]['on_phone_count'] = $outCallLog[$val['index']]['on_phone_count'];
//未接通
$list[$key]['off_phone_count'] = $outCallLog[$val['index']]['off_phone_count'];
//平均通话时长
$list[$key]['avg_phone_time'] = $list[$key]['on_phone_count'] == 0 ? 0 : round($outCallLog[$val['index']]['phone_time'] / $list[$key]['on_phone_count'], 2);
}
if (isset($outCallMbLog[$val['index']])) {
$list[$key]['phone_count'] += $outCallMbLog[$val['index']]['phone_count'];
}
//加微数量
$list[$key]['add_wechat_count'] = isset($add_wechat_date[$val['index']]) ? $add_wechat_date[$val['index']] : 0;
//分享内容数量
$list[$key]['share_count'] = isset($shares[$val['index']]) ? $shares[$val['index']] : 0;
//智慧屏讲解次数
$list[$key]['explain_count'] = isset($zhihui[$val['index']]) ? $zhihui[$val['index']] : 0;
//话术浏览次数
$list[$key]['talkskill_view_count'] = isset($talk_count[$val['index']]) ? $talk_count[$val['index']] : 0;
//培训是否看
$list[$key]['is_look'] = in_array($val['index'], $look) ? '是' : '否';
//跟进次数
$list[$key]['log_count'] = isset($log1[$val['index']]) ? $log1[$val['index']] : 0;
//跟进客户数量
$list[$key]['log_customer_count'] = isset($log2[$val['index']]) ? count(array_unique($log2[$val['index']])) : 0;
//量房客户数
$list[$key]['measuring_room_customer_count'] = isset($state_count1[$val['index']]) ? count(array_unique($state_count1[$val['index']])) : 0;
//到店客户数
$list[$key]['sto_customer_count'] = isset($state_count2[$val['index']]) ? count(array_unique($state_count2[$val['index']])) : 0;
//交定
$list[$key]['deposit_customer_count'] = isset($state_count3[$val['index']]) ? count(array_unique($state_count3[$val['index']])) : 0;
//签单
$list[$key]['sign_customer_count'] = isset($state_count4[$val['index']]) ? count(array_unique($state_count4[$val['index']])) : 0;
//预计转施工客户数量
$list[$key]['house_delivery_count'] = isset($house_delivery_time[$val['index']]) ? $house_delivery_time[$val['index']] : 0;
//预计到店数
$list[$key]['subscribe_store_count'] = isset($sub1[$val['index']]) ? $sub1[$val['index']] : 0;
//预计量房数量
$list[$key]['subscribe_room_count'] = isset($sub2[$val['index']]) ? $sub2[$val['index']] : 0;
//是否有考核
$list[$key]['is_exam'] = isset($don1[$val['index']]) ? '是' : '否';
//考核是否通过
$list[$key]['is_exam_ok'] = isset($don2[$val['index']]) ? '是' : '否';
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功', 'page' => ceil($count / $limit)]);
}
/**
* 行为统计数据导出
*/
public function behaviorStatisticsDataExport()
{
$root_id = request()->employee->root_id;
$type = input('type', '');
if (!request()->isAjax()) {
$header2 = $header3 = [];
if ($type == 'high') {
//高层统计
$header1 = [
["field" => 'x_date', "title" => '时 间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'name', "title" => '员工姓名', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'org_name', "title" => '所属部门', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'position', "title" => '职位', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'phone_count', "title" => '电话数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'on_phone_count', "title" => '接通数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'off_phone_count', "title" => '未接通', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'avg_phone_time', "title" => '平均通话时长', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'add_wechat_count', "title" => '加微数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'share_count', "title" => '调用内容数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'talkskill_view_count', "title" => '浏览话术次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'is_look', "title" => '看培训次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'look_time', "title" => '看培训时长(S)', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'is_exam', "title" => '考核次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'is_exam_ok', "title" => '考核通过次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'log_customer_count', "title" => '客户跟进数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'log_count', "title" => '总回访次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'log_time', "title" => '平均回访时长', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'log_time1', "title" => '平均回访周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'explain_count', "title" => '智慧屏讲解次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'explain_count1', "title" => '每个客户讲解次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'measuring_room_customer_count', "title" => '量房客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'store_1', "title" => '一次到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'store_1_grawth', "title" => '一次到店率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'store_2', "title" => '二次到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'store_2_grawth', "title" => '二次到店率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'store_3', "title" => '三次及以上到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'store_3_grawth', "title" => '三次及以上到店率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'store_1_dep', "title" => '一次到店签单客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'store_1_dep_grawth', "title" => '一次到店签单率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'deposit_customer_count', "title" => '总签单数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'deposit_customer_grawth', "title" => '总签单率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"]
];
view::assign('row', json_encode($header1));
} elseif ($type == 'manager') {
$header1 = [
["field" => 'name', "title" => '员工名称', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'customer_count', "title" => '客户数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'return_visit_count', "title" => '待回访客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'no_assigned_personnel', "title" => '待指派客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'no_visit_3', "title" => '3天内未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'no_visit_7', "title" => '7天内未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'no_visit_15', "title" => '15天内未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'no_visit_30', "title" => '一个月内未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'no_visit_31', "title" => '一个月以上未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'valid_count', "title" => '有效客户', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'valid_grawth', "title" => '有效率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'phone_count', "title" => '电话数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'on_phone_count', "title" => '接通数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'on_phone_grawth', "title" => '接通率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'off_phone_count', "title" => '未接通', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'avg_phone_time', "title" => '平均通话时长', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'add_wechat_count', "title" => '加微数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'add_wechat_grawth', "title" => '加微率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'room', "title" => '量房客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'room_grawth', "title" => '量房率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store1', "title" => '一次到店客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store1_grawth', "title" => '一次到店率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store2', "title" => '二次到店客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store2_grawth', "title" => '二次到店率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store3', "title" => '三次及以上到店客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store3_grawth', "title" => '三次及以上到店率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store1_dep', "title" => '一次到店签单客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store1_dep_grawth', "title" => '一次到店签单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store2_dep', "title" => '二次到店签单数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store2_dep_grawth', "title" => '二次到店签单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store3_dep', "title" => '三次及以上到店签单数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'store3_dep_grawth', "title" => '三次及以上到店签单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'dep', "title" => '总签单数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'dep_grawth', "title" => '总签单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'drawing_date', "title" => '出方案客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'sign_count', "title" => '转单客户', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'sign_grawth', "title" => '转单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => '', "child" => ['square_0_80', 'square_80_100', 'square_100_120', 'square_120_200', 'square_200_500', 'square_500'], "title" => '面积分布', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9", 'colspan' => 6],
["field" => 'existing_homes_count', "title" => '现房数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
["field" => 'forward_housing_count', "title" => '期房数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
];
//房屋状态是否开启
$house_status_where = [
['root_id', '=', $root_id],
['keyname', '=', 'house_status'],
['status', '=', 0]
];
$house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
$field = [];
if (!$house_status_per->isEmpty()) {
$field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('id,name');
foreach ($field as $val) {
$header1[] = ["field" => 'house_status_' . $val['id'], "title" => $val['name'], "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
}
}
$header2 = [
["field" => 'square_0_80', "title" => '80以下', "align" => 'center', "class" => "bgFDE9D9"],
["field" => 'square_80_100', "title" => '81-100', "align" => 'center', "class" => "bgFDE9D9"],
["field" => 'square_100_120', "title" => '101-120', "align" => 'center', "class" => "bgFDE9D9"],
["field" => 'square_120_200', "title" => '121-200', "align" => 'center', "class" => "bgFDE9D9"],
["field" => 'square_200_500', "title" => '201-500', "align" => 'center', "class" => "bgFDE9D9"],
["field" => 'square_500', "title" => '500以上', "align" => 'center', "class" => "bgFDE9D9"]
];
$header = $header1;
foreach ($header as $k => $v) {
if (isset($v['child'])) {
array_splice($header, $k, 1, $header2);
}
}
view::assign('row', json_encode($header));
} elseif ($type == 'general') {
$header1 = [
['field' => 'name', 'title' => '业务员名称', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'org_name', 'title' => '业务员所在部门', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'connecting_capacity', 'title' => '接通多少', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'connecting_capacity_grawth', 'title' => '接通率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'average_duration', 'title' => '平均通话时长', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'add_wechat_count', 'title' => '加微数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'add_wechat_count_grawth', 'title' => '微信通过率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'shares_count', 'title' => '调用内容数量', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'number_of_explanations', 'title' => '智慧屏讲解次数', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'look_data', 'title' => '培训资料是否看了', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'assessment_times', 'title' => '考核次数', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'number_of_passes', 'title' => '通过次数', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'resource_count', 'title' => '分配数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'following_up_count', 'title' => '在跟进数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'deposit_count', 'title' => '签单数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'reported_quantity', 'title' => '报备数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'valid_count', 'title' => '有效数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'valid_grawth', 'title' => '有效率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'valid_no_wechat', 'title' => '有效未加微', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'measuring_room_customer_no_to_store', 'title' => '量房未到店', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'drawing_date_count', 'title' => '户型图', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'measuring_room_customer', 'title' => '量房数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'measuring_room_zhouqi', 'title' => '平均量房周期', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'measuring_room_grawth', 'title' => '量房率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'yuji_store', 'title' => '预计到店数', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'to_the_store_1', 'title' => '一次到店', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'to_the_store_2', 'title' => '二次到店', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'to_the_store_3', 'title' => '三次及以上到店', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_avg_days_1', 'title' => '平均一次到店周期', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_avg_days_2', 'title' => '平均二次到店周期', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_avg_days_3', 'title' => '平均三次及以上到店周期', 'align' => 'center', 'width' => 200, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_count_1_grawth', 'title' => '一次到店率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_count_2_grawth', 'title' => '二次到店率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_count_3_grawth', 'title' => '三次及以上到店率', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'avg_room_store_zhouqi', 'title' => '平均量房到店周期', 'align' => 'center', 'width' => 170, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_deposit_count_0', 'title' => '一次到店签单数量', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_deposit_count_1', 'title' => '二次到店签单数量', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'strtore_deposit_count_2', 'title' => '三次到及以上店签单数量', 'align' => 'center', 'width' => 200, 'rowspan' => 1, 'class' => 'bgFDE9D9', 'sort' => 38],
['field' => 'strtore_deposit_grawth_0', 'title' => '一次到店签单率', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9', 'sort' => 39],
['field' => 'strtore_deposit_grawth_1', 'title' => '二次到店签单率', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9', 'sort' => 40],
['field' => 'strtore_deposit_grawth_2', 'title' => '三次到及以上店签单率', 'align' => 'center', 'width' => 180, 'rowspan' => 1, 'class' => 'bgFDE9D9', 'sort' => 41],
['field' => 'deposit_avg_days', 'title' => '平均签单周期', 'align' => 'center', 'width' => 135, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'sign_count', 'title' => '转单数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'sign_count_grawth', 'title' => '转单率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'avg_visit_count', 'title' => '平均回访次数', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'signed_money', 'title' => '平均合同金额', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'square_0_80', 'title' => '80以下', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'square_80_100', 'title' => '81-100', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'square_100_120', 'title' => '101-120', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'square_120_200', 'title' => '121-200', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'square_200_500', 'title' => '201-500', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'square_500', 'title' => '500以上', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'existing_homes_count', 'title' => '现房数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'forward_housing_count', 'title' => '期房数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
['field' => 'maika_count', 'title' => '卖卡数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
];
//网销端已增加type值到10,如增加type请往10后面加
$root_id = request()->employee->root_id;
//房屋状态是否开启
$house_status_where = [
['root_id', '=', $root_id],
['keyname', '=', 'house_status'],
['status', '=', 0]
];
$house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
$house_field = [];
if (!$house_status_per->isEmpty()) {
$field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
$fields = [];
foreach ($field as $v) {
$fields[] = [
'field' => 'house_status_' . $v['id'],
'title' => $v['name'],
'align' => 'center',
'width' => 100,
'rowspan' => 1,
'class' => 'bgFDE9D9'
];
}
$house_field = $fields;
}
$header1 = array_merge($header1, $house_field);
// 户型
$housetype_where = [
['root_id', '=', $root_id],
['keyname', '=', 'housetype_arrow'],
['status', '=', 0]
];
$housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
$housetype_field = [];
$housetype_done_field = [];
if (!$housetype_per->isEmpty()) {
$field = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
$fields = [];
$fields_done = [];
foreach ($field as $v) {
$fields[] = [
'field' => 'housetype_arrow_' . $v['id'],
'title' => $v['name'],
'align' => 'center',
'width' => 100,
'rowspan' => 1,
'class' => 'bgFDE9D9'
];
$fields_done[] = [
'field' => 'housetype_arrow_lv_' . $v['id'],
'title' => $v['name'] . '成交率',
'align' => 'center',
'width' => 100,
'rowspan' => 1,
'class' => 'bgFDE9D9'
];
}
$housetype_field = $fields;
$housetype_done_field = $fields_done;
}
$header1 = array_merge($header1, $housetype_field);
$header1 = array_merge($header1, $housetype_done_field);
//无效客资
$invalid_customer = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
if ($invalid_customer) {
$invalid_arr = explode(',', $invalid_customer);
} else {
$invalid_arr = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
}
$invalid_field = [];
foreach ($invalid_arr as $k => $v) {
$invalid_field[] = [
'field' => 'invalid_customer_' . $k,
'title' => $v,
'align' => 'center',
'width' => 100,
'rowspan' => 1,
'class' => 'bgFDE9D9'
];
}
$new_type_5_3 = [];
foreach ($header1 as $k => $v) {
$new_type_5_3[$k] = $v;
if ($v['field'] == 'signed_money') {
$new_type_5_3 = array_merge($new_type_5_3, $invalid_field);
}
}
$header1 = $new_type_5_3;
view::assign('row', json_encode($header1));
} else {
// 行为统计
$header1 = [
["field" => 'x_date', "title" => '时 间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'name', "title" => '员工姓名', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'org_name', "title" => '所属部门', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'phone_count', "title" => '电话量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'on_phone_count', "title" => '接通量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'off_phone_count', "title" => '未接通', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'avg_phone_time', "title" => '平均通话时长', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'add_wechat_count', "title" => '加微数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'share_count', "title" => '调用内容数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'explain_count', "title" => '智慧屏讲解次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'talkskill_view_count', "title" => '浏览话术次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'is_look', "title" => '培训是否看了', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'is_exam', "title" => '是否有考核', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'is_exam_ok', "title" => '考核是否通过', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'log_customer_count', "title" => '客户跟进数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'log_count', "title" => '总回访次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'subscribe_room_count', "title" => '预计量房客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'subscribe_store_count', "title" => '预计到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'measuring_room_customer_count', "title" => '量房客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'sto_customer_count', "title" => '到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'deposit_customer_count', "title" => '交定/签单客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'house_delivery_count', "title" => '预计转施工客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
["field" => 'sign_customer_count', "title" => '转单客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"]
];
view::assign('row', json_encode($header1));
}
View::assign('header1', $header1);
View::assign('header2', $header2);
View::assign('header3', $header3);
return View::fetch();
}
if ($type == 'high') {
return $this->highBehaviorStatisticsData();
} elseif ($type == 'manager') {
return $this->manageStatisticsData();
} elseif ($type == 'general') {
return $this->generalStatisticsData();
} else {
return $this->behaviorStatisticsData();
}
}
/**
* 分享内容页面
*/
public function behaviorStatisticsShareLog()
{
$root_id = request()->employee->root_id;
$index = input('index', '');
View::assign('index', $index);
return View::fetch();
}
/**
* 分享内容列表接口
*/
public function behaviorStatisticsShareLogData($page = 1, $limit = 10)
{
$param = Request::only(['page' => 1, 'index' => '']);
$root_id = request()->employee->root_id;
$arr = explode('@', $param['index']);
$share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
$where = [
['employee_id', '=', $arr[0]],
['share_time', 'like', '%' . $arr[1] . '%'],
['type', 'in', $share_where]
];
$list = ShareLog::with('content')->where($where)->page($page, $limit)->order('id desc')->select()->toArray();
$count = ShareLog::where($where)->count();
$arr = [
'Activity' => '#活动#',
'Article' => '#图文素材#',
'Building' => '#楼盘进度#',
'Card' => '#名片#',
'CompanyStrength' => '#公司实力#',
'Construction' => '#在施工地#',
'CustomerJsAll' => '',
'MaterialCase' => '#装修案例#',
'MaterialEvidence' => '#口碑见证#',
'ToolAll' => '#谈单工具#',
'Video' => '#视频#',
'Weiwang' => '#个人微网#'
];
foreach ($list as $k => $v) {
$type = isset($arr[$v['type']]) ? $arr[$v['type']] : '';
if ($v['content']) {
$field = $v['type'] == 'Building' ? 'name' : 'title';
$list[$k]['title'] = $type . '《' . $v['content'][$field] . '》';
} else {
$list[$k]['title'] = '';
}
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
}
/**
* 高层行为统计页面
*/
public function highBehaviorStatistics()
{
//返回组织列表
$org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
View::assign('org', $org);
return View::fetch();
}
/**
* 高层行为统计接口
* 日期截止到 2022011-20
*/
public function highBehaviorStatisticsData($page = 1, $limit = 10)
{
$param = Request::only(['page' => 1, 'start_date' => '', 'end_date' => '', 'keyword' => '', 'org_id' => '']);
$root_id = request()->employee->root_id;
if ($param['start_date']) {
$count_dates = explode(' - ', $param['start_date']);
$start_date = strtotime($count_dates[0]);
$end_date = strtotime($count_dates[1]);
} else {
$start_date = strtotime('2022-11-20');
$end_date = strtotime(date('Y-m-d'));
}
if ($param['keyword']) $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
if ($param['org_id']) {
$o = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
$orgIds = Org::where([['path', 'like', $o['path'] . '%']])->column('id');
$where[] = ['org_id', 'in', $orgIds];
}
$where[] = ['root_id', '=', $root_id];
$where[] = ['uid', '>', 0];
$where[] = ['state', 'in', ['在职', '离职']];
$eids = Employee::where($where)->column('id');
$e_count = count($eids);
//每页开始的位置
$start = ($page - 1) * $limit;
$end = $start + $limit - 1;
//统计日期总天数
$day = ($end_date - $start_date) / 86400;
//数据总量
$count = ($day + 1) * $e_count;
$ids = [];
for ($i = 0; $i <= $day; $i++) {
$j = $i * $e_count;
if (($j + $e_count) < $start) continue;
$e = false;
foreach ($eids as $k => $v) {
$g = $j + $k;
if ($g >= $start && $g <= $end) {
$date = date('Y-m-d', $end_date - ($i * 86400));
$ids[] = ['id' => $v, 'index' => $v . '@' . $date, 'g' => $g];
} elseif ($j > $end) {
$e = true;
break;
}
}
if ($e) break;
}
$lists = array_column($ids, 'index');
$where[] = ['id', 'in', array_column($ids, 'id')];
$employees = Employee::with(['customer' => function ($query) {
$query->field("id,employee_id,DATE_FORMAT(addtime,'%Y-%m-%d') as addtime");
}])->where($where)->field('id,name,org_id,addtime,position,state,termination_time')->select()->toArray();
$em_dates = [];
foreach ($employees as $emp_key => $emp_item) {
$em_dates[$emp_item['id']] = $emp_item;
}
$list = [];
foreach ($lists as $list_key => $list_item) {
$arr = explode('@', $list_item);
$j = $em_dates[$arr[0]];
$j['x_date'] = date('Y年m月d日', strtotime($arr[1]));
$j['date'] = $arr[1];
$j['index'] = $list_item;
$list[] = $j;
}
$eids = array_unique(array_column($list, 'id'));
$query1 = ['employee_id', 'in', $eids];
$query2 = ['share_time', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]];
$query3 = ['addtime', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]];
$custoemr_where = [
['employee_id', 'in', $eids]
];
$customer = Customer::where($custoemr_where)->column("id,ext,employee_id,house_delivery_time,DATE_FORMAT(addtime,'%Y-%m-%d') as addtime");
//加微客户 预计转施工时间
$add_wechat_date = [];
$house_delivery_time = [];
$e_cids = [];
foreach ($customer as $v1) {
$v2 = $v1['ext'];
$ext = json_decode($v2, true);
if (!empty($ext) && is_array($ext)) {
$ext = array_filter($ext);
foreach ($ext as $e) {
if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value'])) {
@$add_wechat_date[$v1['employee_id'] . '@' . date('Y-m-d', strtotime($e['value']))] += 1;
} elseif (isset($e['keyname']) && $e['keyname'] == 'will_transfer_to_construction_date' && !empty($e['value'])) {
@$house_delivery_time[$v1['employee_id'] . '@' . date('Y-m-d', strtotime($e['value']))] += 1;
}
}
}
$e_cids[$v1['employee_id']][] = $v1['id'];
}
//客户添加日期截至到列表日期的天数
$vis = CustomerVisitLog::where([['customer_id', 'in', array_column($customer, 'id')], ['customer_employee_id', 'in', $eids]])->field("DATE_FORMAT(addtime,'%Y-%m-%d') as addtime,concat(customer_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) indexx,customer_id,customer_employee_id")->group('indexx')->order('id asc')->select()->toArray();
$viss = [];
foreach ($vis as $vis_k => $vis_v) {
$viss[$vis_v['customer_id']][] = strtotime($vis_v['addtime']);
}
foreach ($list as $list_key => $list_val) {
$customers = $list_val['customer'];
foreach ($customers as $customers_key => $customers_val) {
//列表日期
$customers[$customers_key]['list_date'] = $list_val['date'];
$start = strtotime($list_val['date']);
//客户添加日期截至到列表日期的天数
$customers[$customers_key]['date_diff'] = ceil(($start - strtotime($customers_val['addtime']) + 1) / 86400);
$customers[$customers_key]['visit_count'] = 0;
//从客户添加日期截至列表日期的客户的回访次数
if (isset($viss[$customers_val['id']])) {
$flip = array_flip($viss[$customers_val['id']]);
foreach ($flip as $flip_k => $flip_v) {
if ($flip_k > $start) {
break;
}
$customers[$customers_key]['visit_count'] += 1;
}
}
//客户回访周期
$customers[$customers_key]['visit_cycle'] = $customers[$customers_key]['visit_count'] == 0 ? 0 : ceil($customers[$customers_key]['date_diff'] / $customers[$customers_key]['visit_count']);
}
$cycle = array_filter(array_column($customers, 'visit_cycle'));
$list[$list_key]['log_time1'] = $cycle ? ceil(array_sum($cycle) / count($cycle)) : 0;
unset($list[$list_key]['customer']);
}
//每人截止到日期的回访次数 和 天数
$dates = array_column($list, 'date'); //要查询的所有日期
//调用内容数量,分享内容次数
$share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
$share = ShareLog::where([$query1, $query2, ['type', 'in', $share_where]])->field("id,concat(employee_id,'@',DATE_FORMAT(share_time,'%Y-%m-%d')) as indexx,concat(employee_id,type,data_id,DATE_FORMAT(share_time,'%Y-%m-%d')) as str,employee_id")->select()->toArray();
$shares = [];
foreach ($share as $share_v) {
@$shares[$share_v['indexx']] += 1;
}
///培训资料是否看了 课程是否学习过
$look = TrainCourseView::where([$query1, $query3, ['time', '>', 0]])->column("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx,time");
$looks = [];
$look_time = [];
foreach ($look as $look_item) {
@$looks[$look_item['indexx']] += 1;
@$look_time[$look_item['indexx']] += $look_item['time'];
}
//考核次数 通过次数
$don = ExamEmpResult::with(['paperInfo' => function ($query) {
$query->field('id,base_score')->bind(['base_score' => 'base_score']);
}])->where([$query1, $query3, ['state', '=', 1]])->field("id,paper_id,final_score,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->select()->toArray();
$don1 = $don2 = []; //是否有考核,考核是否通过
foreach ($don as $vv) {
@$don1[$vv['indexx']] += 1; //考核次数
if ($vv['final_score'] >= $vv['base_score']) @$don2[$vv['indexx']] += 1; //考核通过次数
}
//客户跟进数量
$vislog = CustomerVisitLog::where([
['employee_id|customer_employee_id', 'in', $eids], $query3
])->order('id asc')->column("state,IF(employee_id,employee_id,customer_employee_id) as employee,DATE_FORMAT(addtime,'%Y-%m-%d') as date,customer_id,concat(IF(employee_id,employee_id,customer_employee_id),'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
$log1 = $log2 = []; //跟进次数 , 跟进客户数
$state1 = CustomerVisitLog::changeState('已量房', 'chaos');
$state2 = CustomerVisitLog::changeState('已到店', 'chaos');
$state3 = CustomerVisitLog::changeState('已交定', 'chaos');
$state4 = CustomerVisitLog::changeState('已签单', 'chaos');
$state_count1 = $state_count2 = $state_count3 = $state_count4 = $store = [];
foreach ($vislog as $ke => $va) {
@$log1[$va['indexx']] += 1;
@$log2[$va['indexx']][] = $va['customer_id'];
if (in_array($va['state'], $state1)) {
@$state_count1[$va['indexx']][] = $va['customer_id'];
} elseif (in_array($va['state'], $state2)) {
@$state_count2[$va['indexx']][] = $va['customer_id'];
@$store[$va['indexx']][$va['customer_id']][] = $va['addtime'];
} elseif (in_array($va['state'], $state3)) {
@$state_count3[$va['indexx']][] = $va['customer_id'];
} elseif (in_array($va['state'], $state4)) {
@$state_count4[$va['indexx']][] = $va['customer_id'];
}
}
//一次到店,2次到店 ,3次及以上到店
$s1 = $cus1 = $s2 = $cus2 = $s3 = $cus3 = [];
foreach ($store as $store_key => $store_item) {
foreach ($store_item as $store_item_key => $store_item_item) {
if (count($store_item_item) == 1) {
@$s1[$store_key] += 1;
$cus1[] = $store_item_key;
} elseif (count($store_item_item) == 2) {
@$s2[$store_key] += 1;
$cus2[] = $store_item_key;
} elseif (count($store_item_item) >= 3) {
@$s3[$store_key] += 1;
$cus3[] = $store_item_key;
}
}
}
//预约记录
$query4 = [
['state', '=', 0],
['type', 'in', [1, 3]],
['employee_id', 'in', $eids],
['subscribe_date', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]]
];
$sub1 = $sub2 = []; //1到店 2量房
$subscribe = CustomersSubscribe::where($query4)->column("employee_id,customer_id,subscribe_date,type,concat(employee_id,'@',DATE_FORMAT(subscribe_date,'%Y-%m-%d')) as indexx");
foreach ($subscribe as $v4) {
$v4['type'] == 1 ? @$sub1[$v4['indexx']] += 1 : @$sub2[$v4['indexx']] += 1;
}
//话术浏览次数
$talk_view = TalkskillViewLog::where([$query1, $query3])->column("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
$talk_count = [];
foreach ($talk_view as $t_item) {
@$talk_count[$t_item['indexx']] += 1;
}
//智慧屏讲解次数
$zhihuis = CustomerVisitLog::where([['employee_id|customer_employee_id', 'in', $eids], $query3, ['remark', 'like', '%讲解智慧屏##%']])
->field("IF(employee_id,employee_id,customer_employee_id) as employee,DATE_FORMAT(addtime,'%Y-%m-%d') as date,customer_id,concat(IF(employee_id,employee_id,customer_employee_id),'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->select()->toArray();
$zhihui = [];
foreach ($zhihuis as $v) {
@$zhihui[$v['indexx']] += 1;
}
// 外呼系统统计
$outCallLog = OutCallLog::where([['employee_id', 'in', $eids]])->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
$outCallLog = array_combine(array_column($outCallLog, 'indexx'), $outCallLog);
// 手机打电话次数获取
$outCallMbLog = OutCallMbLog::where([
['employee_id', 'in', $eids]
])->field("count(id) as phone_count,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
$outCallMbLog = array_combine(array_column($outCallMbLog, 'indexx'), $outCallMbLog);
$org = Org::where([['id', 'in', array_column($list, 'org_id')]])->column('name', 'id');
foreach ($list as $key => $val) {
//部门名称
$list[$key]['org_name'] = isset($org[$val['org_id']]) ? $org[$val['org_id']] : '无';
//职位
// $list[$key]['position'] = $val['position'];
//电话量
$list[$key]['phone_count'] = 0;
//接通量
$list[$key]['on_phone_count'] = 0;
//未接通
$list[$key]['off_phone_count'] = 0;
//平均通话时长
$list[$key]['avg_phone_time'] = 0;
if (isset($outCallLog[$val['index']])) {
//电话量
$list[$key]['phone_count'] = $outCallLog[$val['index']]['phone_count'];
//接通量
$list[$key]['on_phone_count'] = $outCallLog[$val['index']]['on_phone_count'];
//未接通
$list[$key]['off_phone_count'] = $outCallLog[$val['index']]['off_phone_count'];
//平均通话时长
$list[$key]['avg_phone_time'] = $list[$key]['on_phone_count'] == 0 ? 0 : round($outCallLog[$val['index']]['phone_time'] / $list[$key]['on_phone_count'], 2);
}
if (isset($outCallMbLog[$val['index']])) {
//电话量
$list[$key]['phone_count'] += $outCallMbLog[$val['index']]['phone_count'];
}
//加微数量
$list[$key]['add_wechat_count'] = isset($add_wechat_date[$val['index']]) ? $add_wechat_date[$val['index']] : 0;
//分享内容数量
$list[$key]['share_count'] = isset($shares[$val['index']]) ? $shares[$val['index']] : 0;
//智慧屏讲解次数
$list[$key]['explain_count'] = isset($zhihui[$val['index']]) ? $zhihui[$val['index']] : 0;
//每个客户讲解次数
$list[$key]['explain_count1'] = $list[$key]['add_wechat_count'] == 0 ? $list[$key]['explain_count'] : ceil($list[$key]['explain_count'] / $list[$key]['add_wechat_count']);
//话术浏览次数
$list[$key]['talkskill_view_count'] = isset($talk_count[$val['index']]) ? $talk_count[$val['index']] : 0;
//培训观看次数
$list[$key]['is_look'] = isset($looks[$val['index']]) ? $looks[$val['index']] : 0;
//培训观看时长
$list[$key]['look_time'] = isset($look_time[$val['index']]) ? $look_time[$val['index']] : 0;
//跟进次数
$list[$key]['log_count'] = isset($log1[$val['index']]) ? $log1[$val['index']] : 0;
//平均回访时长
$list[$key]['log_time'] = 0;
//一次到店客户数
$list[$key]['store_1'] = isset($s1[$val['index']]) ? $s1[$val['index']] : 0;
//二次到店客户数
$list[$key]['store_2'] = isset($s2[$val['index']]) ? $s2[$val['index']] : 0;
//三次到店客户数
$list[$key]['store_3'] = isset($s3[$val['index']]) ? $s3[$val['index']] : 0;
//跟进客户数量
$list[$key]['log_customer_count'] = isset($log2[$val['index']]) ? count(array_unique($log2[$val['index']])) : 0;
//量房客户数
$list[$key]['measuring_room_customer_count'] = isset($state_count1[$val['index']]) ? count(array_unique($state_count1[$val['index']])) : 0;
//到店客户数
$list[$key]['sto_customer_count'] = isset($state_count2[$val['index']]) ? count(array_unique($state_count2[$val['index']])) : 0;
//一次到店率
$list[$key]['store_1_grawth'] = $list[$key]['sto_customer_count'] == 0 ? '0%' : round($list[$key]['store_1'] / $list[$key]['sto_customer_count'] * 100, 2) . '%';
//二次到店率
$list[$key]['store_2_grawth'] = $list[$key]['sto_customer_count'] == 0 ? '0%' : round($list[$key]['store_2'] / $list[$key]['sto_customer_count'] * 100, 2) . '%';
//三次及以上到店率
$list[$key]['store_3_grawth'] = $list[$key]['sto_customer_count'] == 0 ? '0%' : round($list[$key]['store_3'] / $list[$key]['sto_customer_count'] * 100, 2) . '%';
//交定
$list[$key]['deposit_customer_count'] = isset($state_count3[$val['index']]) ? count(array_unique($state_count3[$val['index']])) : 0;
// 一次到店交定客户数量
$list[$key]['store_1_dep'] = $list[$key]['deposit_customer_count'] == 0 ? 0 : count(array_intersect($state_count3[$val['index']], $cus1));
//一次到店交定率
$list[$key]['store_1_dep_grawth'] = $list[$key]['sto_customer_count'] == 0 ? '0%' : round($list[$key]['store_1_dep'] / $list[$key]['sto_customer_count'] * 100, 2) . '%';
//交定率
$list[$key]['deposit_customer_grawth'] = $list[$key]['log_customer_count'] == 0 ? '0%' : round($list[$key]['deposit_customer_count'] / $list[$key]['log_customer_count'] * 100, 2) . '%';
//签单
$list[$key]['sign_customer_count'] = isset($state_count4[$val['index']]) ? count(array_unique($state_count4[$val['index']])) : 0;
//预计转施工客户数量
$list[$key]['house_delivery_count'] = isset($house_delivery_time[$val['index']]) ? $house_delivery_time[$val['index']] : 0;
//预计到店数
$list[$key]['subscribe_store_count'] = isset($sub1[$val['index']]) ? $sub1[$val['index']] : 0;
//预计量房数量
$list[$key]['subscribe_room_count'] = isset($sub2[$val['index']]) ? $sub2[$val['index']] : 0;
//考核次数
$list[$key]['is_exam'] = isset($don1[$val['index']]) ? $don1[$val['index']] : 0;
//考核通过次数
$list[$key]['is_exam_ok'] = isset($don2[$val['index']]) ? $don2[$val['index']] : 0;
//回访次数 从客户报备开始截至到日期的回访次数,一天按一次计算
//平均回访周期
// $list[$key]['log_time1'] = 0;
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功', 'page' => ceil($count / $limit)]);
}
/**
* 加微客户页面
*/
public function addWechat()
{
$root_id = request()->employee->root_id;
$index = input('index', '');
View::assign('index', $index);
$type = input('type', '');
if ($type == 'customerLog') {
//跟进记录
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'addtime', 'title' => '跟进时间']
];
$url = url("statistics/customerLog", ["index" => $index]);
} elseif ($type == 'customerLogs') {
//跟进记录
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'addtime', 'title' => '回访时间']
];
$url = url("statistics/customerLog", ["index" => $index, 'type' => 'log']);
} elseif ($type == 'room') {
//量房客户
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'addtime', 'title' => '量房时间']
];
$url = url("statistics/customerLog", ["index" => $index, 'type' => 'room']);
} elseif (in_array($type, ['store1', 'store2'])) {
//一次到店两次到店客户
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'addtime', 'title' => '到店时间']
];
$url = url("statistics/customerLog", ["index" => $index, 'type' => $type]);
} elseif ($type == 'store_dep') {
//到店交定客户
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'store_time', 'title' => '到店时间'],
['field' => 'dep_time', 'title' => '签单时间']
];
$url = url("statistics/customerLog", ["index" => $index, 'type' => $type]);
} elseif ($type == 'dep') {
//交定客户
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'addtime', 'title' => '签单时间']
];
$url = url("statistics/customerLog", ["index" => $index, 'type' => $type]);
} elseif ($type == 'explain_count') {
//智慧屏讲解客户列表
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话'],
['field' => 'addtime', 'title' => '讲解时间'],
];
$url = url("statistics/customerLog", ["index" => $index, 'type' => $type]);
} else {
//加微客户
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话'],
['field' => 'date', 'title' => '加微日期'],
];
$url = url("statistics/addWechatList", ["index" => $index]);
}
View::assign('fields', json_encode($field));
View::assign('url', $url);
return View::fetch();
}
/**
* 加微客户列表接口
*/
public function addWechatList()
{
$param = Request::only(['page' => 1, 'limit' => 10, 'index' => '']);
$root_id = request()->employee->root_id;
$arr = explode('@', $param['index']);
$time = $arr[1];
$where = [
['employee_id', '=', $arr[0]],
['add_wechat_time', 'like', $time]
];
$list = Customer::where($where)->field('phone,name,add_wechat_time date')->page($param['page'], $param['limit'])->select()->toArray();
foreach ($list as $k => $v) {
$list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
}
$count = Customer::where($where)->count();
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
}
/**
* 跟进列表接口
*/
public function customerLog()
{
$param = Request::only(['page' => 1, 'limit' => 10, 'index' => '', 'type' => '']);
$root_id = request()->employee->root_id;
$arr = explode('@', $param['index']);
$where = [
['employee_id|customer_employee_id', '=', $arr[0]],
['addtime', 'like', '%' . $arr[1] . '%']
];
if ($param['type'] == 'log') {
// 跟进记录
$field = 'addtime,customer_id,state';
} elseif ($param['type'] == 'room') {
//量房客户
$field = 'max(addtime) addtime,customer_id,state';
$where[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')];
} elseif (in_array($param['type'], ['store1', 'store2'])) {
//一次到店客户 , 二次到店客户
$field = "max(addtime) addtime,customer_id,state";
$where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
} elseif ($param['type'] == 'store_dep') {
//一次到店交定
$field = 'id,name';
} elseif ($param['type'] == 'dep') {
//交定
$where[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
$field = 'max(addtime) addtime,customer_id,state';
} elseif ($param['type'] == 'explain_count') {
//智慧屏讲解客户列表
$where[] = ['remark', 'like', '%讲解智慧屏##%'];
$field = 'id,addtime,customer_id';
} else {
//跟进客户
$field = 'max(addtime) addtime,customer_id,state';
}
if ($param['type'] != 'store_dep' || $param['type'] == 'explain_count') {
$list = CustomerVisitLog::with(['customer' => function ($query) {
$query->field('id,name,phone')->bind(['name' => 'name', 'phone' => 'phone']);
}])->where($where)->field($field);
}
if (in_array($param['type'], ['', 'room', 'dep'])) {
$list = $list->group('customer_id');
} elseif (in_array($param['type'], ['store1', 'store2'])) {
$c = $param['type'] == 'store1' ? 1 : 2;
$list = $list->group('customer_id')->having('count(customer_id)=' . $c);
} elseif ($param['type'] == 'store_dep') {
//一次到店
$store = CustomerVisitLog::where(array_merge($where, [['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]]))->group('customer_id')->having('count(customer_id)=1')->column('addtime', 'customer_id');
//交定
$dep = CustomerVisitLog::where(array_merge($where, [['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]]))->column('addtime', 'customer_id');
$cids = array_intersect(array_keys($store), array_keys($dep));
$list = Customer::where([['id', 'in', $cids]])->field($field);
}
$count = $list->count();
$list = $list->page($param['page'], $param['limit'])->select()->toArray();
if ($param['type'] == 'store_dep') {
foreach ($list as $k => $v) {
$list[$k]['store_time'] = $store[$v['id']];
$list[$k]['dep_time'] = $dep[$v['id']];
}
} elseif ($param['type'] == 'explain_count') {
foreach ($list as $k => $v) {
$list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
$list[$k]['name'] = empty($v['name']) ? '未知' : $v['name'];
}
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
}
/**
* 管理行为统计页面
*/
public function manageStatistics()
{
$root_id = request()->employee->root_id;
//房屋状态是否开启
$house_status_where = [
['root_id', '=', $root_id],
['keyname', '=', 'house_status'],
['status', '=', 0]
];
$house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
$field = [];
if (!$house_status_per->isEmpty()) {
$field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
$fields = [];
foreach ($field as $v) {
$fields[] = [
'field' => 'house_status_' . $v['id'],
'title' => $v['name'],
'align' => 'center',
'width' => 100,
'rowspan' => 2
];
}
$field = $fields;
}
View::assign('field', json_encode($field));
//返回组织列表
$org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
View::assign('org', $org);
return View::fetch();
}
/**
* 管理层数据统计
*/
public function manageStatisticsData($page = 1, $limit = 10)
{
$param = Request::only(['page' => 1, 'start_date' => '', 'end_date' => '', 'keyword' => '', 'org_id' => '']);
//日期搜索//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
if ($param['start_date']) {
$count_dates = explode(' - ', $param['start_date']);
$param['start_date'] = date('Y-m-d 00:00:00', strtotime($count_dates[0]));
$param['end_date'] = date('Y-m-d 23:59:59', strtotime($count_dates[1]));
} else {
$param['start_date'] = $param['end_date'] = '';
}
$query_date = ($param['start_date'] && $param['end_date']) ? [['addtime', 'between', [$param['start_date'], $param['end_date']]]] : [];
$root_id = request()->employee->root_id;
$where = [
['root_id', '=', $root_id],
['uid', '>', 0],
['state', '=', '在职']
// ['is_manager', '=', 1]
];
if ($param['start_date'] && $param['end_date']) {
$sdate = strtotime($param['start_date']);
$edate = strtotime($param['end_date']);
} else {
$edate = time();
$sdate = 972473642;
}
if ($param['keyword']) {
$where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
}
if ($param['org_id']) {
$o = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
$orgIds = Org::where([['path', 'like', $o['path'] . '%']])->column('id');
$where[] = ['org_id', 'in', $orgIds];
}
$employee_date = [$param['start_date'], $param['end_date']];
$list = Employee::with(['customer' => function ($query) use ($employee_date) {
//手动报备查addtime,分配查询employee_time
if ($employee_date[0] && $employee_date[1]) {
$e_where1 = [
['addtime', 'between', [$employee_date[0], $employee_date[1]]],
['crm_res_id', '=', 0]
];
$e_where2 = [
['crm_res_id', '>', 0],
['employee_time', 'between', [$employee_date[0], $employee_date[1]]]
];
$query->whereOr([$e_where1, $e_where2])->field('id,employee_id,ext,is_resource,addtime,signed_money,house_type,square,house_status,return_visit,assigned_personnel');
} else {
$query->field('id,employee_id,ext,is_resource,addtime,signed_money,house_type,square,house_status,return_visit,assigned_personnel');
}
}])->where($where)->field('id,org_id,name')->page($page, $limit)->select()->toArray();
$count = Employee::where($where)->count();
//客户ID
$customer_ids = [];
foreach ($list as $key => $val) {
$cid = array_column($val['customer'], 'id');
$customer_ids = array_merge($customer_ids, $cid);
}
//2023-02-21 有效客户查询
$state1 = Customer::changeState('待确认', 'chaos');
$state2 = Customer::changeState('无效', 'chaos');
$queryv1 = [['state', 'not in', array_merge($state1, $state2)], ['id', 'in', $customer_ids]];
$queryv2 = [['state', 'in', $state1], ['crm_res_id', 'null', null], ['id', 'in', $customer_ids]];
$valid_vislog = Customer::whereOr([$queryv1, $queryv2])->column('id');
//最近跟进时间
$max_addtime = CustomerVisitLog::where([['is_merge', '=', 0], ['customer_id', 'in', $customer_ids]])->group('customer_id')->column('max(addtime) addtime', 'customer_id');
//查询有效数量 //有效性判断 ,, //有效状态 未到访,已到访,确定到场,已量房,交定,签单
$state1 = CustomerVisitLog::changeState('未到访', 'chaos');
$state2 = CustomerVisitLog::changeState('已到访', 'chaos');
$state3 = CustomerVisitLog::changeState('已量房', 'chaos');
$state4 = CustomerVisitLog::changeState('已到店', 'chaos');
$state5 = CustomerVisitLog::changeState('已交定', 'chaos');
$state6 = CustomerVisitLog::changeState('已签单', 'chaos');
$state7 = CustomerVisitLog::changeState('已到场', 'chaos');
$vis_where[] = ['customer_id', 'in', $customer_ids];
$vis_where[] = ['state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)];
if($query_date) $vis_where[] = $query_date[0];
$vislog_list = CustomerVisitLog::where($vis_where)->order('addtime asc')->column('customer_id,state,addtime,next_contact_date');
// $valid_vislog = array_unique(array_column($vislog_list, 'customer_id'));
$rooms = $store = $dep = $sign = [];
foreach ($vislog_list as $vislog_list_item) {
if (in_array($vislog_list_item['state'], $state3)) {
$rooms[] = $vislog_list_item['customer_id'];
} elseif (in_array($vislog_list_item['state'], $state4)) {
@$store[$vislog_list_item['customer_id']][] = $vislog_list_item['addtime'];
} elseif (in_array($vislog_list_item['state'], $state5)) {
@$dep[$vislog_list_item['customer_id']][] = $vislog_list_item['addtime'];
} elseif (in_array($vislog_list_item['state'], $state6)) {
$sign[] = $vislog_list_item['customer_id'];
}
}
$rooms = array_unique($rooms); //量房客户
//到店客户
$store1 = $store2 = $store3 = [];
foreach ($store as $store_key => $store_val) {
if (count($store_val) == 1) {
$store1[] = $store_key;
} elseif (count($store_val) == 2) {
$store2[] = $store_key;
} else {
$store3[] = $store_key;
}
}
//交定客户
$dep1 = $dep2 = $dep3 = [];
foreach ($dep as $dep_key => $dep_item) {
if (count($dep_item) == 1) {
$dep1[] = $dep_key;
} elseif (count($dep_item) == 2) {
$dep2[] = $dep_key;
} else {
$dep3[] = $dep_key;
}
}
//房屋状态是否开启
$house_status_where = [
['root_id', '=', $root_id],
['keyname', '=', 'house_status'],
['status', '=', 0]
];
$house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
$field = [];
if (!$house_status_per->isEmpty()) {
$field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
}
// 电话统计
$outCallLog = OutCallLog::where([['root_id', '=', $root_id]])->where($query_date)->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,employee_id")->group('employee_id')->select()->toArray();
$outCallLog = array_combine(array_column($outCallLog, 'employee_id'), $outCallLog);
// 手机打电话次数获取
$outCallMbLog = OutCallMbLog::where([
['root_id', '=', $root_id]
])->field("count(id) as phone_count,employee_id")->group('employee_id')->select()->toArray();
$outCallMbLog = array_combine(array_column($outCallMbLog, 'employee_id'), $outCallMbLog);
foreach ($list as $k => $v) {
//客户ID
$cids = array_column($v['customer'], 'id');
//客户数量
$list[$k]['customer_count'] = count($cids);
//待回访
$list[$k]['return_visit_count'] = count(array_filter(array_column($v['customer'], 'return_visit')));
//待指派
$assigned_personnel = count(array_filter(array_column($v['customer'], 'assigned_personnel')));
$list[$k]['no_assigned_personnel'] = $list[$k]['customer_count'] - $assigned_personnel;
//三天,七天,十五天,三十天,三十天以上未跟进
$list[$k]['no_visit_3'] = $list[$k]['no_visit_7'] = $list[$k]['no_visit_15'] = $list[$k]['no_visit_30'] = $list[$k]['no_visit_31'] = 0;
foreach ($v['customer'] as $customer_item) {
//最近跟进时间
$visit_time = isset($max_addtime[$customer_item['id']]) ? $max_addtime[$customer_item['id']] : $customer_item['addtime'];
$day = ceil((time() - strtotime($visit_time)) / 86400);
if ($day <= 3 && $day > 1) {
$list[$k]['no_visit_3'] += 1;
} elseif ($day <= 7 && $day > 3) {
$list[$k]['no_visit_7'] += 1;
} elseif ($day < 15 && $day > 7) {
$list[$k]['no_visit_15'] += 1;
} elseif ($day <= 30 && $day > 15) {
$list[$k]['no_visit_30'] += 1;
} elseif ($day > 30) {
$list[$k]['no_visit_31'] += 1;
}
}
//有效客户
$valid = array_intersect($cids, $valid_vislog);
//有效客户数量
$list[$k]['valid_count'] = count($valid);
//有效率
$list[$k]['valid_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['valid_count']);
//电话量
$list[$k]['phone_count'] = 0;
//接通量
$list[$k]['on_phone_count'] = 0;
//接通率
$list[$k]['on_phone_grawth'] = 0;
//未接通
$list[$k]['off_phone_count'] = 0;
//平均通话时长
$list[$k]['avg_phone_time'] = 0;
if (isset($outCallLog[$v['id']])) {
//电话量
$list[$k]['phone_count'] = $outCallLog[$v['id']]['phone_count'];
//接通量
$list[$k]['on_phone_count'] = $outCallLog[$v['id']]['on_phone_count'];
//接通率
$list[$k]['on_phone_grawth'] = $this->grawth($list[$k]['phone_count'], $list[$k]['on_phone_count']);
//未接通
$list[$k]['off_phone_count'] = $outCallLog[$v['id']]['off_phone_count'];
//平均通话时长
$list[$k]['avg_phone_time'] = $list[$k]['on_phone_count'] == 0 ? 0 : round($outCallLog[$v['id']]['phone_time'] / $list[$k]['on_phone_count'], 2);
}
if (isset($outCallMbLog[$v['id']])) {
//电话量
$list[$k]['phone_count'] += $outCallMbLog[$v['id']]['phone_count'];
}
$customer = $this->addWechatCount($v['customer'], $sdate, $edate);
//加微数量
$list[$k]['add_wechat_count'] = count($customer['wechat_count']);
//出方案客户数
$list[$k]['drawing_date'] = count($customer['plan_issuing_date']);
//加微率
$list[$k]['add_wechat_grawth'] = $this->grawth($list[$k]['valid_count'], $list[$k]['add_wechat_count']);
//量房客户数
$list[$k]['room'] = count(array_intersect($rooms, $cids));
//量房率
$list[$k]['room_grawth'] = $this->grawth($list[$k]['valid_count'], $list[$k]['room']);
//到店客户数
$store1 = array_intersect($store1, $cids);
$store2 = array_intersect($store2, $cids);
$store3 = array_intersect($store3, $cids);
$list[$k]['store1'] = count($store1);
$list[$k]['store2'] = count($store2);
$list[$k]['store3'] = count($store3);
$list[$k]['store1_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store1']);
$list[$k]['store2_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store2']);
$list[$k]['store3_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store3']);
//交定客户数
$z_dep = array_intersect(array_keys($dep), $cids);
$dep1 = array_intersect($dep1, $cids);
$dep2 = array_intersect($dep2, $cids);
$dep3 = array_intersect($dep3, $cids);
$list[$k]['dep1'] = count($dep1);
$list[$k]['dep2'] = count($dep2);
$list[$k]['dep3'] = count($dep3);
$list[$k]['dep1_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep1']);
$list[$k]['dep2_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep2']);
$list[$k]['dep3_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep3']);
$list[$k]['dep'] = count($z_dep);
$list[$k]['dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep']);
//一次到店签单客户数
$list[$k]['store1_dep'] = count(array_intersect($z_dep, $store1));
$list[$k]['store1_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store1_dep']);
$list[$k]['store2_dep'] = count(array_intersect($z_dep, $store2));
$list[$k]['store2_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store2_dep']);
$list[$k]['store3_dep'] = count(array_intersect($z_dep, $store3));
$list[$k]['store3_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store3_dep']);
//签单
$list[$k]['sign_count'] = count(array_intersect($cids, $sign));
$list[$k]['sign_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['sign_count']);
//面积
$list[$k]['square_0_80'] = $customer['square_0_80']; //
$list[$k]['square_80_100'] = $customer['square_80_100']; //
$list[$k]['square_100_120'] = $customer['square_100_120']; //
$list[$k]['square_120_200'] = $customer['square_120_200']; //
$list[$k]['square_200_500'] = $customer['square_200_500']; //
$list[$k]['square_500'] = $customer['square_500']; //
//房屋类型
$list[$k]['existing_homes_count'] = $customer['existing_homes_count']; //现房数量
$list[$k]['forward_housing_count'] = $customer['forward_housing_count']; //期房数量
//房屋状态
if ($field) {
$house_status = $this->getHouseStatus($customer['house_status'], $field);
$list[$k] = array_merge($list[$k], $house_status);
}
unset($list[$k]['customer']);
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功', 'page' => ceil($count / $limit)]);
}
/**
* 率
*/
private function grawth($zcount, $count)
{
$res = $zcount == 0 ? '0%' : round($count / $zcount * 100, 2) . '%';
return $res;
}
/**
* 加微数量
*/
private function addWechatCount($wechat, $sdate, $edate)
{
$data['wechat_count'] = [];
$data['plan_issuing_date'] = []; //出方案时间
$square = $house_type = [];
$data['house_status'] = [];
foreach ($wechat as $v) {
$data['house_status'][] = $v['house_status'];
$square[] = $v['square'];
$house_type[] = $v['house_type'];
//客户添加时间
$addtime = strtotime($v['addtime']);
//加微客户,有户型图客户
$v2 = $v['ext'];
$ext = json_decode($v2, true);
if (!empty($ext) && is_array($ext)) {
$ext = array_filter($ext);
foreach ($ext as $e) {
if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) {
$data['wechat_count'][] = $v['id'];
} elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) {
$data['drawing_date'][] = $v['id'];
}
}
}
}
//面积
//房屋面积
$data['square_0_80'] = $data['square_80_100'] = $data['square_100_120'] = $data['square_120_200'] = $data['square_200_500'] = $data['square_500'] = 0;
foreach ($square as $square_item) {
if (!$square_item) continue; //不填不计算
if ($square_item <= 80) {
$data['square_0_80'] += 1;
} elseif ($square_item <= 100) {
$data['square_80_100'] += 1;
} elseif ($square_item <= 120) {
$data['square_100_120'] += 1;
} elseif ($square_item <= 200) {
$data['square_120_200'] += 1;
} elseif ($square_item <= 500) {
$data['square_200_500'] += 1;
} else {
$data['square_500'] += 1;
}
}
//房屋类型
$data['existing_homes_count'] = 0; //现房数量
$data['forward_housing_count'] = 0; //期房数量
foreach ($house_type as $house_type_item) {
if (strpos($house_type_item, '现房') !== false) {
$data['existing_homes_count'] += 1;
} elseif (strpos($house_type_item, '期房') !== false) {
$data['forward_housing_count'] += 1;
}
}
return $data;
}
/**
* 弹框页面
*/
public function manageOpen()
{
$root_id = request()->employee->root_id;
$eid = input('eid',);
View::assign('eid', $eid);
$type = input('type', '');
$start_date = input('start_date', '');
$end_date = input('end_date', '');
if ($type == 'assign') {
//待指派
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话']
];
$url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
} else if (in_array($type, ['v3', 'v7', 'v15', 'v30', 'v31'])) {
//天未指派
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话']
];
$url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
} else if (in_array($type, ['valid', 'wechat', 'room'])) {
//天未指派
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话']
];
$url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
} elseif ($type == 'invalid') {
$key = input('key', '');
//无效客资
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话']
];
$url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date, 'key' => $key]);
} elseif ($type == 'number_of_explanations') {
//智慧屏讲解
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话'],
['field' => 'addtime', 'title' => '讲解时间']
];
$url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
} else {
//待回访客户
$field = [
['field' => 'name', 'title' => '姓名'],
['field' => 'phone', 'title' => '电话']
];
$url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
}
View::assign('fields', json_encode($field));
View::assign('url', $url);
return View::fetch('add_wechat');
}
/**
* 弹框页面数据
*/
public function manageOpenData()
{
$param = Request::only(['page' => 1, 'limit' => 10, 'eid' => 0, 'type' => '', 'start_date' => '', 'end_date' => '']);
$root_id = request()->employee->root_id;
$where = [
['employee_id', '=', $param['eid']]
];
if ($param['start_date'] && $param['end_date']) {
$where[] = ['addtime', 'between', [$param['start_date'], $param['end_date'] . ' 23:59:59']];
}
if ($param['type'] == 'assign') {
//待指派
$where[] = ['assigned_personnel', '=', NULL];
$list = Customer::where($where)->where(function ($query) {
$or1[] = ['crm_res_id', 'null', null];
$or2[] = ['crm_res_id', '>', 0];
$or2[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
$query->whereOr([$or1, $or2]);
})->field('name,phone');
} elseif (in_array($param['type'], ['v3', 'v7', 'v15', 'v30', 'v31'])) {
$arr = ['v3' => [1, 3], 'v7' => [3, 7], 'v15' => [7, 15], 'v30' => [15, 30], 'v31' => [30, 999999]];
//3天未回访
$cids = Customer::where($where)->column('addtime', 'id');
$max_addtime = CustomerVisitLog::where([['is_merge', '=', 0], ['customer_id', 'in', array_keys($cids)]])->group('customer_id')->column('max(addtime) addtime', 'customer_id');
$cid = [];
foreach ($cids as $k => $v) {
//最近跟进时间
$visit_time = isset($max_addtime[$k]) ? $max_addtime[$k] : $v;
$day = ceil((time() - strtotime($visit_time)) / 86400);
if ($day > $arr[$param['type']][0] && $day <= $arr[$param['type']][1]) {
$cid[] = $k;
}
}
if (empty($cid)) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
$where[] = ['id', 'in', $cid];
$list = Customer::where($where)->field('name,phone');
} elseif ($param['type'] == 'valid') {
//有效客户数量
$state1 = Customer::changeState('待确认', 'chaos');
$state2 = Customer::changeState('无效', 'chaos');
$query1 = [['state', 'not in', array_merge($state1, $state2)], ['employee_id', '=', $param['eid']]];
$query2 = [['state', 'in', $state1], ['crm_res_id', 'null', null], ['employee_id', '=', $param['eid']]];
$ids = Customer::whereOr([$query1, $query2])->column('id');
if ($param['start_date'] && $param['end_date']) {
$query1 = [['id', 'in', $ids], ['crm_res_id', 'null', null], ['addtime', 'between', [$param['start_date'], $param['end_date']]]];
$query2 = [['id', 'in', $ids], ['crm_res_id', 'not null', null], ['employee_time', 'between', [$param['start_date'], $param['end_date']]]];
$list = Customer::whereOr([$query1, $query2])->field('name,phone');
} else {
$list = Customer::where([['id', 'in', $ids]])->field('name,phone');
}
} elseif ($param['type'] == 'wechat' || $param['type'] == 'drawing') {
$field = $param['type'] == 'wechat' ? 'add_wechat_time' : 'drawing_date';
//加微客户
$where1[] = ['ext', 'like', '%' . $field . '%'];
$ext = Customer::where(array_merge($where, $where1))->column('id,ext');
$cid = [];
foreach ($ext as $v) {
$json = json_decode($v['ext'], true);
foreach ($json as $v2) {
if ($v2['keyname'] == $field && !empty($v2['value'])) {
$cid[] = $v['id'];
}
}
}
$where[] = ['id', 'in', $cid];
$list = Customer::where($where)->field('name,phone');
} elseif ($param['type'] == 'room') {
//量房客户
$customer_ids = Customer::where($where)->column('id');
$state3 = CustomerVisitLog::changeState('已量房', 'chaos');
$vis_where[] = ['customer_id', 'in', $customer_ids];
$vis_where[] = ['state', 'in', $state3];
$cid = CustomerVisitLog::where($vis_where)->group('customer_id')->column('customer_id');
$where[] = ['id', 'in', $cid];
$list = Customer::where($where)->field('name,phone');
} elseif (in_array($param['type'], ['store1', 'store2', 'store3'])) {
$having = ['store1' => 'count(id)=1', 'store2' => 'count(id)=2', 'store3' => 'count(id)>=3'];
//一次到店客户数
$customer_ids = Customer::where($where)->column('id');
$state = CustomerVisitLog::changeState('已到店', 'chaos');
$vis_where[] = ['customer_id', 'in', $customer_ids];
$vis_where[] = ['state', 'in', $state];
$cid = CustomerVisitLog::where($vis_where)->group('customer_id')->having($having[$param['type']])->column('customer_id');
$where[] = ['id', 'in', $cid];
$list = Customer::where($where)->field('name,phone');
} elseif (in_array($param['type'], ['store1_dep', 'store2_dep', 'store3_dep'])) {
$having = ['store1_dep' => 'count(id)=1', 'store2_dep' => 'count(id)=2', 'store3_dep' => 'count(id)>=3'];
//一次到店客户数
$customer_ids = Customer::where($where)->column('id');
$state = CustomerVisitLog::changeState('已到店', 'chaos');
$state1 = CustomerVisitLog::changeState('已交定', 'chaos');
$vis_where[] = ['customer_id', 'in', $customer_ids];
$vis_where[] = ['state', 'in', $state];
$c1 = CustomerVisitLog::where($vis_where)->group('customer_id')->having($having[$param['type']])->column('customer_id');
//交定
$vis_where1[] = ['customer_id', 'in', $customer_ids];
$vis_where1[] = ['state', 'in', $state1];
$c2 = CustomerVisitLog::where($vis_where1)->group('customer_id')->column('customer_id');
$where[] = ['id', 'in', array_intersect($c1, $c2)];
$list = Customer::where($where)->field('name,phone');
} elseif ($param['type'] == 'dep' || $param['type'] == 'sign_count') {
$state = $param['type'] == 'dep' ? '已交定' : '已签单';
$customer_ids = Customer::where($where)->column('id');
$state1 = CustomerVisitLog::changeState($state, 'chaos');
//交定
$vis_where1[] = ['customer_id', 'in', $customer_ids];
$vis_where1[] = ['state', 'in', $state1];
$c2 = CustomerVisitLog::where($vis_where1)->group('customer_id')->column('customer_id');
$where[] = ['id', 'in', $c2];
$list = Customer::where($where)->field('name,phone');
} elseif (in_array($param['type'], ['square_0_80', 'square_80_100', 'square_100_120', 'square_120_200', 'square_200_500', 'square_500'])) {
$arr = [
'square_0_80' => [0, 80],
'square_80_100' => [81, 100],
'square_100_120' => [101, 120],
'square_120_200' => [121, 200],
'square_200_500' => [201, 500],
'square_500' => [501, 999999],
];
$square = Customer::where($where)->column('square,id');
$cid = [];
foreach ($square as $v) {
if ($v['square'] && $v['square'] > $arr[$param['type']][0] && $v['square'] <= $arr[$param['type']][1]) $cid[] = $v['id'];
}
$where[] = ['id', 'in', $cid];
$list = Customer::where($where)->field('name,phone');
} elseif ($param['type'] == 'existing_homes_count' || $param['type'] == 'forward_housing_count') {
//现房 期房客户
$field = $param['type'] == 'existing_homes_count' ? '现房' : '期房';
$where[] = ['house_type', 'like', '%' . $field . '%'];
$list = Customer::where($where)->field('name,phone');
} elseif ($param['type'] == 'invalid') {
$invalid_customer = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
$arr = $invalid_customer ? explode(',', $invalid_customer) : ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
$field = (int)input('key', '');
if (empty($arr) || !isset($arr[$field])) return json(['code' => 0, 'data' => [], 'count' => [], 'msg' => '获取成功']);
$status = $arr[$field];
$customer_ids = Customer::where($where)->column('id');
$state3 = CustomerVisitLog::where([['remark', 'like', '%' . $status . '%'], ['customer_id', 'in', $customer_ids]])->group('customer_id')->column('customer_id');
$where[] = ['id', 'in', $state3];
$list = Customer::where($where)->field('name,phone');
} elseif ($param['type'] == 'number_of_explanations') {
//智慧屏讲解客户列表
// $list = CustomerVisitLog::with(['custoemr'=>function($query){}])->where([['employee_id|customer_employee_id','=',$param['eid']],['remark','like','%讲解智慧屏##%']])->field('customer_id,addtime')->select()->toArray();
} else {
$where[] = ['return_visit', '=', 1];
$list = Customer::where($where)->field('name,phone');
}
$count = $list->count();
$list = $list->page($param['page'], $param['limit'])->select()->toArray();
foreach ($list as $k => $v) {
$list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
$list[$k]['name'] = empty($v['name']) ? '未知' : $v['name'];
}
return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
}
/**
* 统计展示字段 网销端已增加type值到10,
*/
public function statisticsFields($type)
{
// 行为统计
$type1 = [
'x_date' => ['name' => '时间', 'show' => 1, 'width' => 150, 'sort' => 1],
'name' => ['name' => '员工姓名', 'show' => 1, 'width' => 100, 'sort' => 2],
'org_name' => ['name' => '所属部门', 'show' => 1, 'width' => 100, 'sort' => 3],
'phone_count' => ['name' => '电话量', 'show' => 1, 'width' => 75, 'sort' => 4],
'on_phone_count' => ['name' => '接通量', 'show' => 1, 'width' => 75, 'sort' => 5],
'off_phone_count' => ['name' => '未接通', 'show' => 1, 'width' => 75, 'sort' => 6],
'avg_phone_time' => ['name' => '平均通话时长', 'show' => 1, 'width' => 130, 'sort' => 7],
'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 90, 'sort' => 8],
'share_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 130, 'sort' => 9],
'explain_count' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 150, 'sort' => 10],
'talkskill_view_count' => ['name' => '浏览话术次数', 'show' => 1, 'width' => 130, 'sort' => 11],
'is_look' => ['name' => '培训是否看了', 'show' => 1, 'width' => 120, 'sort' => 12],
'is_exam' => ['name' => '是否有考核', 'show' => 1, 'width' => 110, 'sort' => 13],
'is_exam_ok' => ['name' => '考核是否通过', 'show' => 1, 'width' => 130, 'sort' => 14],
'log_customer_count' => ['name' => '客户跟进数', 'show' => 1, 'width' => 100, 'sort' => 15],
'log_count' => ['name' => '总回访次数', 'show' => 1, 'width' => 100, 'sort' => 16],
'subscribe_room_count' => ['name' => '预计量房客户数', 'show' => 1, 'width' => 150, 'sort' => 17],
'subscribe_store_count' => ['name' => '预计到店客户数', 'show' => 1, 'width' => 150, 'sort' => 18],
'measuring_room_customer_count' => ['name' => '量房客户数', 'show' => 1, 'width' => 120, 'sort' => 19],
'sto_customer_count' => ['name' => '到店客户数', 'show' => 1, 'width' => 120, 'sort' => 20],
'deposit_customer_count' => ['name' => '交定/签单客户数', 'show' => 1, 'width' => 150, 'sort' => 21],
'house_delivery_count' => ['name' => '预计转施工客户数', 'show' => 1, 'width' => 150, 'sort' => 22],
'sign_customer_count' => ['name' => '转单客户数', 'show' => 1, 'width' => 100, 'sort' => 23],
];
// 高层统计
$type2 = [
'x_date' => ['name' => '时间', 'show' => 1, 'width' => 150, 'sort' => 1],
'name' => ['name' => '员工姓名', 'show' => 1, 'width' => 100, 'sort' => 2],
'org_name' => ['name' => '所属部门', 'show' => 1, 'width' => 100, 'sort' => 3],
'position' => ['name' => '职位', 'show' => 1, 'width' => 80, 'sort' => 4],
'phone_count' => ['name' => '电话量', 'show' => 1, 'width' => 75, 'sort' => 5],
'on_phone_count' => ['name' => '接通量', 'show' => 1, 'width' => 75, 'sort' => 6],
'off_phone_count' => ['name' => '未接通', 'show' => 1, 'width' => 75, 'sort' => 7],
'avg_phone_time' => ['name' => '平均通话时长', 'show' => 1, 'width' => 130, 'sort' => 8],
'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 90, 'sort' => 9],
'share_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 130, 'sort' => 10],
'explain_count' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 150, 'sort' => 11],
'explain_count1' => ['name' => '每个客户讲解次数', 'show' => 1, 'width' => 200, 'sort' => 12],
'talkskill_view_count' => ['name' => '浏览话术次数', 'show' => 1, 'width' => 130, 'sort' => 13],
'is_look' => ['name' => '看培训次数', 'show' => 1, 'width' => 120, 'sort' => 14],
'look_time' => ['name' => '看培训时长(S)', 'show' => 1, 'width' => 150, 'sort' => 15],
'is_exam' => ['name' => '是否有考核', 'show' => 1, 'width' => 110, 'sort' => 16],
'is_exam_ok' => ['name' => '考核是否通过', 'show' => 1, 'width' => 130, 'sort' => 17],
'log_customer_count' => ['name' => '客户跟进数', 'show' => 1, 'width' => 100, 'sort' => 18],
'log_count' => ['name' => '总回访次数', 'show' => 1, 'width' => 100, 'sort' => 19],
'log_time' => ['name' => '平均回访时长', 'show' => 1, 'width' => 150, 'sort' => 20],
'log_time1' => ['name' => '平均回访周期', 'show' => 1, 'width' => 150, 'sort' => 21],
'measuring_room_customer_count' => ['name' => '量房客户数', 'show' => 1, 'width' => 120, 'sort' => 22],
'store_1' => ['name' => '一次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 23],
'store_1_grawth' => ['name' => '一次到店率', 'show' => 1, 'width' => 125, 'sort' => 24],
'store_2' => ['name' => '二次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 25],
'store_2_grawth' => ['name' => '二次到店率', 'show' => 1, 'width' => 125, 'sort' => 26],
'store_3' => ['name' => '三次及以上到店客户数', 'show' => 1, 'width' => 250, 'sort' => 27],
'store_3_grawth' => ['name' => '三次及以上到店率', 'show' => 1, 'width' => 200, 'sort' => 28],
'store_1_dep' => ['name' => '一次到店签单客户数', 'show' => 1, 'width' => 225, 'sort' => 29],
'store_1_dep_grawth' => ['name' => '一次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 30],
'deposit_customer_count' => ['name' => '总签单数', 'show' => 1, 'width' => 100, 'sort' => 31],
'deposit_customer_grawth' => ['name' => '总签单率', 'show' => 1, 'width' => 100, 'sort' => 32],
];
// 管理层统计
$type3 = [
'name' => ['name' => '员工姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
'customer_count' => ['name' => '客户数量', 'show' => 1, 'width' => 100, 'sort' => 2],
'return_visit_count' => ['name' => '待回访客户数', 'show' => 1, 'width' => 150, 'sort' => 3],
'no_assigned_personnel' => ['name' => '待指派客户数', 'show' => 1, 'width' => 150, 'sort' => 4],
'no_visit_3' => ['name' => '3天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 5],
'no_visit_7' => ['name' => '7天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 6],
'no_visit_15' => ['name' => '15天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 7],
'no_visit_30' => ['name' => '一个月内未跟进客户数', 'show' => 1, 'width' => 250, 'sort' => 8],
'no_visit_31' => ['name' => '一个月以上未跟进客户数', 'show' => 1, 'width' => 275, 'sort' => 9],
'valid_count' => ['name' => '有效客户', 'show' => 1, 'width' => 100, 'sort' => 10],
'valid_grawth' => ['name' => '有效率', 'show' => 1, 'width' => 75, 'sort' => 11],
'phone_count' => ['name' => '电话数量', 'show' => 1, 'width' => 100, 'sort' => 12],
'on_phone_count' => ['name' => '接通数量', 'show' => 1, 'width' => 100, 'sort' => 13],
'on_phone_grawth' => ['name' => '接通率', 'show' => 1, 'width' => 75, 'sort' => 14],
'off_phone_count' => ['name' => '未接通', 'show' => 1, 'width' => 75, 'sort' => 15],
'avg_phone_time' => ['name' => '平均通话时长', 'show' => 1, 'width' => 150, 'sort' => 16],
'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 100, 'sort' => 17],
'add_wechat_grawth' => ['name' => '加微率', 'show' => 1, 'width' => 75, 'sort' => 18],
'room' => ['name' => '量房客户数', 'show' => 1, 'width' => 125, 'sort' => 19],
'room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 75, 'sort' => 20],
'store1' => ['name' => '一次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 21],
'store1_grawth' => ['name' => '一次到店率', 'show' => 1, 'width' => 125, 'sort' => 22],
'store2' => ['name' => '二次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 23],
'store2_grawth' => ['name' => '二次到店率', 'show' => 1, 'width' => 125, 'sort' => 24],
'store3' => ['name' => '三次及以上到店客户数', 'show' => 1, 'width' => 250, 'sort' => 25],
'store3_grawth' => ['name' => '三次及以上到店率', 'show' => 1, 'width' => 200, 'sort' => 26],
'store1_dep' => ['name' => '一次到店签单客户数', 'show' => 1, 'width' => 225, 'sort' => 27],
'store1_dep_grawth' => ['name' => '一次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 28],
'store2_dep' => ['name' => '二次到店签单数', 'show' => 1, 'width' => 175, 'sort' => 29],
'store2_dep_grawth' => ['name' => '二次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 30],
'store3_dep' => ['name' => '三次及以上到店签单数', 'show' => 1, 'width' => 250, 'sort' => 31],
'store3_dep_grawth' => ['name' => '三次及以上到店签单率', 'show' => 1, 'width' => 250, 'sort' => 32],
'dep' => ['name' => '总签单数', 'show' => 1, 'width' => 100, 'sort' => 33],
'dep_grawth' => ['name' => '总签单率', 'show' => 1, 'width' => 100, 'sort' => 34],
'drawing_date' => ['name' => '出方案客户数', 'show' => 1, 'width' => 150, 'sort' => 35],
'sign_count' => ['name' => '转单客户', 'show' => 1, 'width' => 100, 'sort' => 36],
'sign_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 75, 'sort' => 37],
'area' => ['name' => '面积分布', 'show' => 1, 'width' => 100, 'sort' => 38],
'existing_homes_count' => ['name' => '现房数量', 'show' => 1, 'width' => 100, 'sort' => 40],
'forward_housing_count' => ['name' => '期房数量', 'show' => 1, 'width' => 100, 'sort' => 41],
];
// 客户统计列表
$type4 = [
'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
'phone' => ['name' => '手机号', 'show' => 1, 'width' => 125, 'sort' => 2],
'community_name' => ['name' => '小区', 'show' => 1, 'width' => 150, 'sort' => 3],
'house_type' => ['name' => '房屋类型', 'show' => 1, 'width' => 100, 'sort' => 4],
'house_delivery_time' => ['name' => '交房时间', 'show' => 1, 'width' => 100, 'sort' => 5],
'square' => ['name' => '面积', 'show' => 1, 'width' => 75, 'sort' => 6],
'org_name' => ['name' => '部门', 'show' => 1, 'width' => 120, 'sort' => 7],
'employee_name' => ['name' => '业务员', 'show' => 1, 'width' => 90, 'sort' => 8],
'addtime' => ['name' => '添加时间', 'show' => 1, 'width' => 170, 'sort' => 9],
'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 100, 'sort' => 10],
'assigned_personal_manager' => ['name' => '指派客户经理', 'show' => 1, 'width' => 150, 'sort' => 11],
'assigned_personal_designer_org' => ['name' => '指派设计部', 'show' => 1, 'width' => 125, 'sort' => 12],
'assigned_personal_designer' => ['name' => '指派设计师', 'show' => 1, 'width' => 125, 'sort' => 13],
'no_visit_day' => ['name' => '未跟踪天数', 'show' => 1, 'width' => 125, 'sort' => 14],
'wechat' => ['name' => '是否加微信', 'show' => 1, 'width' => 125, 'sort' => 15],
'add_wechat_type' => ['name' => '加微类型', 'show' => 1, 'width' => 100, 'sort' => 16],
'add_wechat_time' => ['name' => '加微时间', 'show' => 1, 'width' => 120, 'sort' => 17],
'group_building_date' => ['name' => '建群时间', 'show' => 1, 'width' => 250, 'sort' => 18],
'is_liangfang' => ['name' => '是否量房', 'show' => 1, 'width' => 125, 'sort' => 19],
'liangfang_date' => ['name' => '量房时间', 'show' => 1, 'width' => 170, 'sort' => 20],
'once_liangfang_days' => ['name' => '量房周期', 'show' => 1, 'width' => 120, 'sort' => 21],
'plan_issuing_date' => ['name' => '计划出初步方案时间', 'show' => 1, 'width' => 225, 'sort' => 22],
'once_daodian' => ['name' => '是否一次到店', 'show' => 1, 'width' => 150, 'sort' => 23],
'daodian_date' => ['name' => '一次到店时间', 'show' => 1, 'width' => 170, 'sort' => 24],
'once_daodian_shop' => ['name' => '一次所到店面', 'show' => 1, 'width' => 150, 'sort' => 25],
'once_daodian_days' => ['name' => '一次到店周期', 'show' => 1, 'width' => 150, 'sort' => 26],
'liangfang_daodian_days' => ['name' => '量房到店周期', 'show' => 1, 'width' => 150, 'sort' => 27],
'twice_daodian_date' => ['name' => '二次到店时间', 'show' => 1, 'width' => 170, 'sort' => 28],
'twice_daodian_shop' => ['name' => '二次所到店面', 'show' => 1, 'width' => 150, 'sort' => 29],
'twice_daodian_days' => ['name' => '二次到店周期', 'show' => 1, 'width' => 150, 'sort' => 30],
'is_deposit' => ['name' => '是否交定/签单', 'show' => 1, 'width' => 170, 'sort' => 31],
'deposit_date' => ['name' => '交定/签单时间', 'show' => 1, 'width' => 170, 'sort' => 32],
'drawing_date' => ['name' => '出图时间', 'show' => 1, 'width' => 100, 'sort' => 33],
'sign_date' => ['name' => '转单时间', 'show' => 1, 'width' => 170, 'sort' => 34],
'sign_days' => ['name' => '交定转单周期', 'show' => 1, 'width' => 150, 'sort' => 35],
'signed_money_data' => ['name' => '合同金额(元)', 'show' => 1, 'width' => 125, 'sort' => 36],
'first_visit_no_sign' => ['name' => '一次到店未签单时间', 'show' => 1, 'width' => 200, 'sort' => 37],
'manager_visit_times' => ['name' => '客户经理回访次数', 'show' => 1, 'width' => 200, 'sort' => 38],
'manager_visit_cycle' => ['name' => '客户经理回访周期', 'show' => 1, 'width' => 100, 'sort' => 39],
'employee_visit_times' => ['name' => '经理回访次数', 'show' => 1, 'width' => 150, 'sort' => 40],
'employee_visit_cycle' => ['name' => '经理回访平均周期', 'show' => 1, 'width' => 200, 'sort' => 41],
'invalid' => ['name' => '无效客户', 'show' => 1, 'width' => 100, 'sort' => 42],
'invalid_remark' => ['name' => '无效原因', 'show' => 1, 'width' => 100, 'sort' => 43],
'died' => ['name' => '是否死单', 'show' => 1, 'width' => 100, 'sort' => 44],
'customer_type' => ['name' => '客户种类', 'show' => 1, 'width' => 100, 'sort' => 11],
];
// 总统计
// 一级标题
$type5_1 = [
'name' => ['name' => '业务员名称', 'show' => 1, 'width' => 100],
'org_name' => ['name' => '业务员所在部门', 'show' => 1, 'width' => 130],
'behavior' => ['name' => '行为统计', 'show' => 1, 'width' => 100],
'customer' => ['name' => '客户统计', 'show' => 1, 'width' => 100],
'house' => ['name' => '房屋信息统计', 'show' => 1, 'width' => 100],
];
// 二级标题
$type5_2 = [
'name' => ['name' => '业务员名称', 'show' => 1, 'width' => 100, 'parent' => 'name'],
'org_name' => ['name' => '业务员所在部门', 'show' => 1, 'width' => 130, 'parent' => 'org_name'],
'connecting_count' => ['name' => '电话量', 'show' => 1, 'width' => 150, 'parent' => 'behavior'],
'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 100, 'parent' => 'behavior'],
'add_wechat_count_grawth' => ['name' => '微信通过率', 'show' => 1, 'width' => 100, 'parent' => 'behavior'],
'shares_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 130, 'parent' => 'behavior'],
'number_of_explanations' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 150, 'parent' => 'behavior'],
'look_data' => ['name' => '培训资料是否看了', 'show' => 1, 'width' => 150, 'parent' => 'behavior'],
'exam_data' => ['name' => '考核通过情况', 'show' => 1, 'width' => 100, 'parent' => 'behavior'],
'customer_data' => ['name' => '客户总量', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'valid_count' => ['name' => '有效数量', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'valid_grawth' => ['name' => '有效率', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'valid_no_wechat' => ['name' => '有效未加微', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'measuring_room_customer_no_to_store' => ['name' => '量房未到店', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'drawing_date_count' => ['name' => '户型图', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'measuring_room_customer' => ['name' => '量房数量', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'measuring_room_zhouqi' => ['name' => '平均量房周期', 'show' => 1, 'width' => 130, 'parent' => 'customer'],
'measuring_room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'yuji_store' => ['name' => '预计到店数', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'daodian_count' => ['name' => '到店数', 'show' => 1, 'width' => 120, 'parent' => 'customer'],
'daodian_avg' => ['name' => '平均到店周期', 'show' => 1, 'width' => 225, 'parent' => 'customer'],
'daodian_grawth' => ['name' => '到店率', 'show' => 1, 'width' => 150, 'parent' => 'customer'],
'avg_room_store_zhouqi' => ['name' => '平均量房到店周期', 'show' => 1, 'width' => 170, 'parent' => 'customer'],
'daodian_deposit' => ['name' => '到店签单数量', 'show' => 1, 'width' => 150, 'parent' => 'customer'],
'daodian_deposit_grawth' => ['name' => '到店签单率', 'show' => 1, 'width' => 150, 'parent' => 'customer'],
'avg_dep_zhouqi'=> ['name'=> '平均签单周期', 'show'=> 1, 'width'=> 135, 'parent'=> 'customer'],
'sign_count' => ['name' => '转单数量', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'sign_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'avg_visit_count' => ['name' => '平均回访次数', 'show' => 1, 'width' => 130, 'parent' => 'customer'],
'signed_money' => ['name' => '平均合同金额', 'show' => 1, 'width' => 130, 'parent' => 'customer'],
'died_customer' => ['name' => '无效客资', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
'area' => ['name' => '面积分布', 'show' => 1, 'width' => 170, 'parent' => 'house'],
'existing_homes_count' => ['name' => '现房数量', 'show' => 1, 'width' => 100, 'parent' => 'house'],
'forward_housing_count' => ['name' => '期房数量', 'show' => 1, 'width' => 100, 'parent' => 'house'],
];
// 三级标题
$type5_3 = [
'name' => ['name' => '业务员名称', 'show' => 1, 'width' => 100, 'parent' => 'name', 'sort' => 1],
'org_name' => ['name' => '业务员所在部门', 'show' => 1, 'width' => 130, 'parent' => 'org_name', 'sort' => 2],
'connecting_count' => ['name' => '拨打量', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 2],
'connecting_capacity' => ['name' => '接通多少', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 3],
'connecting_capacity_grawth' => ['name' => '接通率', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 4],
'average_duration' => ['name' => '平均通话时长', 'show' => 1, 'width' => 130, 'parent' => 'call_count', 'sort' => 5],
'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 100, 'parent' => 'behavior', 'sort' => 6],
'add_wechat_count_grawth' => ['name' => '微信通过率', 'show' => 1, 'width' => 100, 'parent' => 'behavior', 'sort' => 7],
'shares_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 130, 'parent' => 'behavior', 'sort' => 8],
'number_of_explanations' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 150, 'parent' => 'behavior', 'sort' => 9],
'look_data' => ['name' => '培训资料是否看了', 'show' => 1, 'width' => 150, 'parent' => 'behavior', 'sort' => 10],
'assessment_times' => ['name' => '考核次数', 'show' => 1, 'width' => 100, 'parent' => 'exam_data', 'sort' => 11],
'number_of_passes' => ['name' => '通过次数', 'show' => 1, 'width' => 100, 'parent' => 'exam_data', 'sort' => 12],
'resource_count' => ['name' => '分配数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 13],
'following_up_count' => ['name' => '在跟进数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 14],
'deposit_count' => ['name' => '签单数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 15],
'reported_quantity' => ['name' => '报备数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 16],
'valid_count' => ['name' => '有效数量', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 17],
'valid_grawth' => ['name' => '有效率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 18],
'valid_no_wechat' => ['name' => '有效未加微', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 19],
'measuring_room_customer_no_to_store' => ['name' => '量房未到店', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 20],
'drawing_date_count' => ['name' => '户型图', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 21],
'measuring_room_customer' => ['name' => '量房数量', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 22],
'measuring_room_zhouqi' => ['name' => '平均量房周期', 'show' => 1, 'width' => 130, 'parent' => 'customer', 'sort' => 23],
'measuring_room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 24],
'yuji_store' => ['name' => '预计到店数', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 25],
'to_the_store_1' => ['name' => '一次到店', 'show' => 1, 'width' => 100, 'parent' => 'daodian_count', 'sort' => 26],
'to_the_store_2' => ['name' => '二次到店', 'show' => 1, 'width' => 100, 'parent' => 'daodian_count', 'sort' => 27],
'strtore_2v1' => ['name' => '二访比率', 'show' => 1, 'width' => 100, 'parent' => 'daodian_count', 'sort' => 28],
'to_the_store_3' => ['name' => '三次及以上到店', 'show' => 1, 'width' => 130, 'parent' => 'daodian_count', 'sort' => 28],
'strtore_avg_days_1' => ['name' => '平均一次到店周期', 'show' => 1, 'width' => 150, 'parent' => 'daodian_avg', 'sort' => 29],
'strtore_avg_days_2' => ['name' => '平均二次到店周期', 'show' => 1, 'width' => 150, 'parent' => 'daodian_avg', 'sort' => 30],
'strtore_avg_days_3' => ['name' => '平均三次及以上到店周期', 'show' => 1, 'width' => 200, 'parent' => 'daodian_avg', 'sort' => 31],
'strtore_count_1_grawth' => ['name' => '一次到店率', 'show' => 1, 'width' => 100, 'parent' => 'daodian_grawth', 'sort' => 32],
'strtore_count_2_grawth' => ['name' => '二次到店率', 'show' => 1, 'width' => 100, 'parent' => 'daodian_grawth', 'sort' => 33],
'strtore_count_3_grawth' => ['name' => '三次及以上到店率', 'show' => 1, 'width' => 150, 'parent' => 'daodian_grawth', 'sort' => 34],
'avg_room_store_zhouqi' => ['name' => '平均量房到店周期', 'show' => 1, 'width' => 170, 'parent' => 'customer', 'sort' => 35],
'strtore_deposit_count_0' => ['name' => '一次到店签单数量', 'show' => 1, 'width' => 150, 'parent' => 'daodian_deposit', 'sort' => 36],
'strtore_deposit_count_1' => ['name' => '二次到店签单数量', 'show' => 1, 'width' => 150, 'parent' => 'daodian_deposit', 'sort' => 37],
'strtore_deposit_count_2' => ['name' => '三次到及以上店签单数量', 'show' => 1, 'width' => 200, 'parent' => 'daodian_deposit', 'sort' => 38],
'strtore_deposit_grawth_0' => ['name' => '一次到店签单率', 'show' => 1, 'width' => 130, 'parent' => 'daodian_deposit_grawth', 'sort' => 39],
'strtore_deposit_grawth_1' => ['name' => '二次到店签单率', 'show' => 1, 'width' => 130, 'parent' => 'daodian_deposit_grawth', 'sort' => 40],
'strtore_deposit_grawth_2' => ['name' => '三次到及以上店签单率', 'show' => 1, 'width' => 180, 'parent' => 'daodian_deposit_grawth', 'sort' => 41],
'deposit_avg_days' => ['name' => '平均签单周期', 'show' => 1, 'width' => 135, 'parent' => 'customer', 'sort' => 42],
'sign_count' => ['name' => '转单数量', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 43],
'sign_count_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 44],
'avg_visit_count' => ['name' => '平均回访次数', 'show' => 1, 'width' => 130, 'parent' => 'customer', 'sort' => 45],
'signed_money' => ['name' => '平均合同金额', 'show' => 1, 'width' => 130, 'parent' => 'customer', 'sort' => 46],
'square_0_80' => ['name' => '80以下', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 47],
'square_80_100' => ['name' => '81-100', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 48],
'square_100_120' => ['name' => '101-120', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 49],
'square_120_200' => ['name' => '121-200', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 50],
'square_200_500' => ['name' => '201-500', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 51],
'square_500' => ['name' => '500以上', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 52],
'existing_homes_count' => ['name' => '现房数量', 'show' => 1, 'width' => 100, 'parent' => 'house', 'sort' => 53],
'forward_housing_count' => ['name' => '期房数量', 'show' => 1, 'width' => 100, 'parent' => 'house', 'sort' => 54],
];
//网销端已增加type值到10,如增加type请往10后面加
$root_id = request()->employee->root_id;
//房屋状态是否开启
$house_status_where = [
['root_id', '=', $root_id],
['keyname', '=', 'house_status'],
['status', '=', 0]
];
$house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
$house_field = [];
if (!$house_status_per->isEmpty()) {
$field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
$fields = [];
foreach ($field as $v) {
$fields['house_status_' . $v['id']] = [
'name' => $v['name'],
'show' => 1,
'width' => 100,
'parent' => 'house',
'sort' => 50
];
}
$house_field = $fields;
}
$type5_2 = array_merge($type5_2, $house_field);
$type5_3 = array_merge($type5_3, $house_field);
// 户型
$housetype_where = [
['root_id', '=', $root_id],
['keyname', '=', 'housetype_arrow'],
['status', '=', 0]
];
$housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
$housetype_field = [];
$housetype_done_field = [];
if (!$housetype_per->isEmpty()) {
$field = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
$fields = [];
$fields_done = [];
foreach ($field as $v) {
$fields['housetype_arrow_' . $v['id']] = [
'name' => $v['name'],
'show' => 1,
'width' => 100,
'parent' => 'house',
'sort' => 51
];
$fields_done['housetype_arrow_lv_' . $v['id']] = [
'name' => $v['name'] . '成交率',
'show' => 1,
'width' => 150,
'parent' => 'house',
'sort' => 51
];
}
$housetype_field = $fields;
$housetype_done_field = $fields_done;
}
$type5_2 = array_merge($type5_2, $housetype_field);
$type5_3 = array_merge($type5_3, $housetype_field);
$type5_2 = array_merge($type5_2, $housetype_done_field);
$type5_3 = array_merge($type5_3, $housetype_done_field);
//无效客资
$invalid_customer = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
if ($invalid_customer) {
$invalid_arr = explode(',', $invalid_customer);
} else {
$invalid_arr = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
}
$invalid_field = [];
foreach ($invalid_arr as $k => $v) {
$invalid_field['invalid_customer_' . $k] = [
'name' => $v,
'show' => 1,
'width' => 100,
'parent' => 'died_customer',
'sort' => 55
];
}
$new_type_5_3 = [];
foreach ($type5_3 as $k => $v) {
$new_type_5_3[$k] = $v;
if ($k == 'signed_money') {
$new_type_5_3 = array_merge($new_type_5_3, $invalid_field);
}
}
$type5_3 = $new_type_5_3;
// 管理层户型分布
$new_type_3 = [];
foreach ($type3 as $k => $v) {
$new_type_3[$k] = $v;
if ($k == 'area') {
$new_type_3 = array_merge($new_type_3, $house_field);
}
}
switch ($type) {
case 1: // 行为统计全部字段
return $type1;
break;
case 2: // 高层统计全部字段
return $type2;
break;
case 3: // 管理层统计全部字段
return $type3;
break;
case 4: // 客户统计列表
return $type4;
break;
case 5: // 总统计
return $type5_3;
break;
default:
return [];
break;
}
}
/**
* 统计表字段
*/
public function fieldList($type = 0, $is_org = 0)
{
$field = $this->statisticsFields($type);
$root_id = request()->employee->root_id;
$employee_id = request()->employee->id;
$save_field = StatisticsSetting::where([['root_id', '=', $root_id], ['employee_id', '=', $employee_id], ['type', '=', $type]])->findOrEmpty();
if (!$save_field->isEmpty()) {
$save_content = json_decode($save_field['content'], true);
if (!empty($save_content)) {
foreach ($save_content as $k => $v) {
if (isset($field[$k])) {
$field[$k]['show'] = $v['show'];
$field[$k]['sort'] = $v['sort'];
}
}
array_multisort(array_column($field, 'sort'), SORT_ASC, $field);
}
}
if ($type == 5) {
if ($is_org == 1) {
$field_5['org_name'] = ['name' => '部门名称', 'show' => 1, 'width' => 100, 'sort' => 1];
foreach ($field as $k => $v) {
if ($k == 'name' || $k == 'org_name' || $k == 'look_data') {
unset($field[$k]);
}
}
$field_5 = array_merge($field_5, $field);
$field = $field_5;
} else {
$field_5['name'] = [];
$field_5['org_name'] = [];
foreach ($field as $k => $v) {
if (isset($field_5[$k])) {
$field_5[$k] = $v;
unset($field[$k]);
}
}
$field_5 = array_merge($field_5, $field);
$field = $field_5;
}
}
return json(['code' => 0, 'data' => $field, 'msg' => 'success']);
}
/**
* 统计字段展示设置
*/
public function fieldSetting()
{
$type = input('type', '', 'intval'); // 类型
$content = input('content', '', 'trim'); // 内容
$content = json_decode($content, true);
$default_field = $this->statisticsFields($type);
$root_id = request()->employee->root_id;
$employee_id = request()->employee->id;
$find = StatisticsSetting::where([['root_id', '=', $root_id], ['employee_id', '=', $employee_id], ['type', '=', $type]])->findOrEmpty();
foreach ($content as $k => $v) {
if (isset($v['name']) && isset($v['show']) && isset($v['sort']) && isset($default_field[$v['name']])) {
$default_field[$v['name']]['show'] = $v['show'];
$default_field[$v['name']]['sort'] = $v['sort'];
}
}
$save_content = json_encode($default_field);
if ($find->isEmpty()) {
$result = StatisticsSetting::insert(['root_id' => $root_id, 'employee_id' => $employee_id, 'type' => $type, 'content' => $save_content]);
} else {
$find->content = $save_content;
$find->save();
}
return json(['code' => 0, 'data' => [], 'msg' => '设置成功']);
}
/**
* 导出记录
*/
public function export2($m)
{
$typeList = ['behaviorStatisticsData' => ['type' => 1], 'highBehaviorStatisticsData' => ['type' => 2], 'manageStatisticsData' => ['type' => 3], 'customer_list_data' => ['type' => 4], 'generalStatisticsData' => ['type' => 5, 'is_org' => 2]];
if (!isset($typeList[$m])) {
json(['code' => 1, 'msg' => '类型不存在'])->send();
return;
}
$param = Request::post();
$filename = uniqid();
$log = [
'root_id' => request()->employee->root_id,
'file' => $filename . '.csv',
'type' => $m,
'employee_id' => request()->employee->id,
'search' => json_encode($param)
];
$exportLog = ExportLog::create($log);
json(['code' => 0, 'msg' => '导出中,请稍后在记录中查看'])->send();
$type = $typeList[$m]['type'];
$is_org = isset($typeList[$m]['is_org']) ? $typeList[$m]['is_org'] : 0;
$c = $this->fieldList($type, $is_org);
$c = $c->getData();
$title = $c['data'];
array_multisort(array_column($title, 'sort'), $title);
touch('../download/' . $filename . '.csv');
$fp = fopen('../download/' . $filename . '.csv', 'a');
$t = [];
foreach ($title as $ti) {
$t[] = $ti['name'];
}
fputcsv($fp, $t);
$n = 2000;
$p = 1;
do {
$data = $this->$m($p, $n);
$data = $data->getData();
foreach ($data['data'] as $row) {
$r = [];
foreach ($title as $k => $f) {
$r[] = isset($row[$k]) ? $row[$k] : '';
}
fputcsv($fp, $r);
}
$p++;
} while ($n * ($p - 1) < $data['count']);
fclose($fp);
// 文件上传
$rs = ossUpload('statisticsExport/' . $filename . '.csv', '../download/' . $filename . '.csv');
if ($rs) {
unlink('../download/' . $filename . '.csv');
}
$exportLog->save(['state' => 1]);
}
public function exportList2($type)
{
if (!request()->isAjax()) {
return View::fetch();
}
$where = [
['type', '=', $type],
['root_id', '=', request()->employee->root_id]
];
$data = ExportLog::with(['employee' => function ($query) {
$query->bind(['opt_name']);
}])->field('id,state,employee_id,addtime,file')->where($where)->order('addtime desc')->select();
$count = ExportLog::where($where)->count();
return json(['code' => 0, 'count' => $count, 'data' => $data]);
}
public function export3($m)
{
// $param = Request::get();
// $filename = uniqid();
// $log = [
// 'root_id' => request()->employee->root_id,
// 'file' => $filename . '.csv',
// 'type' => $m,
// 'employee_id' => request()->employee->id,
// 'search' => json_encode($param)
// ];
// $exportLog = ExportLog::create($log);
return json(['code' => 0, 'msg' => '1111'])->send();
$type = $typeList[$m]['type'];
$is_org = isset($typeList[$m]['is_org']) ? $typeList[$m]['is_org'] : 0;
$c = $this->fieldList($type, $is_org);
$c = $c->getData();
$title = $c['data'];
array_multisort(array_column($title, 'sort'), $title);
touch('../download/' . $filename . '.csv');
$fp = fopen('../download/' . $filename . '.csv', 'a');
$t = [];
foreach ($title as $ti) {
$t[] = $ti['name'];
}
fputcsv($fp, $t);
$n = 2000;
$p = 1;
do {
$data = $this->$m($p, $n);
$data = $data->getData();
foreach ($data['data'] as $row) {
$r = [];
foreach ($title as $k => $f) {
$r[] = isset($row[$k]) ? $row[$k] : '';
}
fputcsv($fp, $r);
}
$p++;
} while ($n * ($p - 1) < $data['count']);
fclose($fp);
// 文件上传
$rs = ossUpload('statisticsExport/' . $filename . '.csv', '../download/' . $filename . '.csv');
if ($rs) {
unlink('../download/' . $filename . '.csv');
}
$exportLog->save(['state' => 1]);
}
}