You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
478 lines
20 KiB
478 lines
20 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
// | 官方网址:https://www.niucloud.com
|
|
// +----------------------------------------------------------------------
|
|
// | niucloud团队 版权所有 开源版本可自由商用
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Niucloud Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\api\controller\apiController;
|
|
|
|
use app\model\campus_person_role\CampusPersonRole;
|
|
use app\model\market_performance\MarketPerformance;
|
|
use app\model\order_table\OrderTable;
|
|
use app\model\personnel\PersonnelSummary;
|
|
use app\Request;
|
|
use app\service\api\apiService\CommonService;
|
|
use core\base\BaseApiService;
|
|
use app\model\customer_resources\CustomerResources;
|
|
use think\facade\Db;
|
|
|
|
|
|
use app\model\resource_sharing\ResourceSharing;
|
|
use app\model\six_speed\SixSpeed;
|
|
|
|
/**
|
|
* 统计控制器相关接口
|
|
* Class Personnel
|
|
* @package app\api\controller\apiController
|
|
*/
|
|
class Statistics extends BaseApiService
|
|
{
|
|
|
|
/**
|
|
* 员工端统计(销售)-获取销售首页数据统计
|
|
*/
|
|
public function marketHome(Request $request)
|
|
{
|
|
$customerResources = new CustomerResources();
|
|
$resourceSharing = new ResourceSharing();
|
|
$communication_records = new CommunicationRecords();
|
|
$market_performance = new MarketPerformance();
|
|
$six_speed = new SixSpeed();
|
|
$order = new OrderTable();
|
|
$personnel_id = $request->param('personnel_id','');//员工表id
|
|
$role_key_arr = $request->param('role_key_arr','');//array 角色key
|
|
//字符转数组
|
|
$role_key_arr = explode(',',$role_key_arr);
|
|
if(empty($personnel_id)|| empty($role_key_arr)){
|
|
return fail('缺少参数');
|
|
}
|
|
$firstDayOfMonth = date('m月01日'); // 获取本月第一天
|
|
$lastDayOfMonth = date('m月t日'); // 获取本月最后一天
|
|
$dateRange = "{$firstDayOfMonth}-{$lastDayOfMonth}"; // 拼接日期范围字符串
|
|
|
|
// $role_key_arr = ['sale'];//@todo 调试使用
|
|
|
|
if(in_array('market',$role_key_arr) || in_array('market_manager',$role_key_arr)){
|
|
$yesterdayStart = date('Y-m-d', strtotime('-1 day'));
|
|
$lastMonthStart = date('Y-m', strtotime('-1 month'));
|
|
$customer_list = $customerResources->where(['consultant' => $personnel_id])->select();
|
|
$newTotal = 0;
|
|
$assignedSales = 0;
|
|
$yesterdayNew = 0;
|
|
$todayNew = 0;
|
|
$yjsr = 0;
|
|
$wfpsl = 0;
|
|
$bytc = 0;
|
|
$gdsl = 0;
|
|
$lastNewTotal = 0;
|
|
$lastAssignedSales = 0;
|
|
foreach($customer_list as $item){
|
|
if($item['create_year_month'] == date("Y-m")){
|
|
$newTotal++;
|
|
if($resourceSharing->where(['resource_id' => $item['id']])->find()){
|
|
$assignedSales++;
|
|
}
|
|
}
|
|
|
|
if($item['create_year_month'] == $lastMonthStart){
|
|
$lastNewTotal++;
|
|
if($resourceSharing->where(['resource_id' => $item['id']])->find()){
|
|
$lastAssignedSales++;
|
|
}
|
|
}
|
|
|
|
if($item['create_date'] == $yesterdayStart){
|
|
$yesterdayNew++;
|
|
}
|
|
|
|
if($item['create_date'] == date("Y-m-d")){
|
|
$todayNew++;
|
|
}
|
|
|
|
if(!$resourceSharing->where(['resource_id' => $item['id']])->find()){
|
|
$wfpsl++;
|
|
}
|
|
|
|
if($order->where(['resource_id' => $item['id'],'order_type' => '1'])->find()){
|
|
if($six_speed->where(['resource_id' => $item['id'],'is_closed' => 1])->find()){
|
|
$gdsl++;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
$yjsr = $market_performance->where([
|
|
'performance_date' => date('Y-m-d'),
|
|
'personnel_id' => $personnel_id
|
|
])->Sum('performance_amount');
|
|
|
|
|
|
$bytc = $market_performance
|
|
->where(['personnel_id' => $personnel_id])
|
|
->whereTime('performance_date', 'month')
|
|
->sum('performance_amount');
|
|
|
|
//市场人员统计数据(地推拉人头的)
|
|
$data = [
|
|
'date_range'=>$dateRange,//拼接日期范围字符串
|
|
'role_type'=>'market_type',//角色类型|market_type=市场,sale_type=销售
|
|
//本月
|
|
'month' => [
|
|
'new_total' => $newTotal,//拉新总人数
|
|
'new_total_rate' => min(100, round($newTotal / 1000 * 100, 2)),//拉新总人数比例(不超过100%)
|
|
'assigned_sales' => $assignedSales,//已分配给销售的人数
|
|
'assigned_sales_rate' => $newTotal ? min(100, round($assignedSales / $newTotal * 100, 2)) : 0,//已分配给销售的人数比例(不超过100%)
|
|
'yesterday_new' => $yesterdayNew,//昨日拉新
|
|
'yesterday_new_rate' => $newTotal ? min(100, round($yesterdayNew / $newTotal * 100, 2)) : 0,//昨日拉新比例(不超过100%)
|
|
'today_new' => $todayNew,//今日拉新
|
|
'today_new_rate' => $newTotal ? min(100, round($todayNew / $newTotal * 100, 2)) : 0,//今日拉新比例(不超过100%)
|
|
|
|
],
|
|
//上月
|
|
'last_month' => [
|
|
'new_total' => $lastNewTotal,//拉新总人数
|
|
'new_total_rate' => min(100, round($lastNewTotal / 800 * 100, 2)),//拉新总人数比例(不超过100%)
|
|
'assigned_sales' => $lastAssignedSales,//已分配给销售的人数
|
|
'assigned_sales_rate' => $lastNewTotal ? min(100, round($lastAssignedSales / $lastNewTotal * 100, 2)) : 0,//已分配给销售的人数比例(不超过100%)
|
|
'yesterday_new' =>$yesterdayNew,//昨日拉新人数
|
|
'yesterday_new_rate' => $lastNewTotal ? min(100, round($yesterdayNew / $lastNewTotal * 100, 2)) : 0,//昨日拉新人数比例(不超过100%)
|
|
'today_new' => $todayNew,//今日拉新人数
|
|
'today_new_rate' => $lastNewTotal ? min(100, round($todayNew / $lastNewTotal * 100, 2)) : 0,//今日拉新人数比例(不超过100%)
|
|
'xzzy' => $todayNew,
|
|
'yjsr' => $yjsr,
|
|
'wfpsl' => $wfpsl,
|
|
'bytc' => $bytc,
|
|
'gdsl' => $gdsl
|
|
]
|
|
];
|
|
}elseif(in_array('sale',$role_key_arr) || in_array('sale_manager',$role_key_arr)){
|
|
|
|
$startTime = date('Y-m-01 00:00:00');
|
|
$endTime = date('Y-m-t 23:59:59');
|
|
|
|
$last_monthstartTime = date('Y-m-01 00:00:00', strtotime('first day of last month'));
|
|
$last_monthendTime = date('Y-m-t 23:59:59', strtotime('last day of last month'));
|
|
|
|
$resource_list = $resourceSharing->where(['shared_by' => $personnel_id])->select();
|
|
$assigned = 0;
|
|
$contacted = 0;
|
|
$unconverted = 0;
|
|
$renewal = 0;
|
|
$closed = 0;
|
|
|
|
$lastAssigned = 0;
|
|
$lastContacted = 0;
|
|
$lastUnconverted = 0;
|
|
$lastClosed = 0;
|
|
foreach($resource_list as $item){
|
|
$sharedAt = strtotime($item['shared_at']);
|
|
|
|
if ($sharedAt >= strtotime($startTime) && $sharedAt <= strtotime($endTime)) {
|
|
$assigned++;
|
|
$records = $communication_records->where(['resource_id' => $item['resource_id']])->order("id desc")->find();
|
|
if($records){
|
|
$contacted++;
|
|
if($records['communication_result'] !== 'success'){
|
|
$unconverted++;
|
|
}
|
|
}
|
|
|
|
if($six_speed->where(['resource_id' => $item['resource_id'],'is_closed' => 1])->find()){
|
|
$closed++;
|
|
}
|
|
}
|
|
|
|
|
|
if ($sharedAt >= strtotime($last_monthstartTime) && $sharedAt <= strtotime($last_monthendTime)) {
|
|
$lastAssigned++;
|
|
$records = $communication_records->where(['resource_id' => $item['resource_id']])->order("id desc")->find();
|
|
if($records){
|
|
$lastContacted++;
|
|
if($records['communication_result'] !== 'success'){
|
|
$lastUnconverted++;
|
|
}
|
|
}
|
|
|
|
if($six_speed->where(['resource_id' => $item['resource_id'],'is_closed' => 1])->find()){
|
|
$lastClosed++;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//销售人员统计数据(买课的)
|
|
$data = [
|
|
'date_range'=>$dateRange,//拼接日期范围字符串
|
|
'role_type'=>'sale_type',//角色类型|market_type=市场,sale_type=销售 infoData.month
|
|
//本月
|
|
'month' => [
|
|
'assigned_clients' => $assigned,//分配给我的客户人数
|
|
'contacted_clients' => $contacted,//已沟通的客户人数
|
|
'contact_rate' => $assigned ? min(100, round($contacted / $assigned * 100, 2)) : 0,//沟通率(不超过100%)
|
|
'unconverted_clients' => $unconverted,//已沟通但没成交的客户人数
|
|
'unconverted_rate' => $contacted ? min(100, round($unconverted / $contacted * 100, 2)) : 0,//未成交率(不超过100%)
|
|
'renewal_clients' => ($renewal = rand(5, 30)),//待续费的客户人数
|
|
'renewal_rate' => $assigned ? min(100, round($renewal / $assigned * 100, 2)) : 0,//待续费率(不超过100%)
|
|
'closed_clients' => $closed,//已关单的客户人数
|
|
'closed_rate' => $contacted ? min(100, round($closed / $contacted * 100, 2)) : 0,//关单率(不超过100%)
|
|
'conversion_rate' => $assigned ? min(100, round($closed / $assigned * 100, 2)) : 0,//整体转化率(不超过100%)
|
|
],
|
|
//上月
|
|
'last_month' => [
|
|
'assigned_clients' => $lastAssigned,//分配给我的客户人数
|
|
'contacted_clients' => $lastContacted,//已沟通的客户人数
|
|
'contact_rate' => $lastAssigned ? min(100, round($lastContacted / $lastAssigned * 100, 2)) : 0,//沟通率(不超过100%)
|
|
'unconverted_clients' => $lastUnconverted,//已沟通但没成交的客户人数
|
|
'unconverted_rate' => $lastContacted ? min(100, round($lastUnconverted / $lastContacted * 100, 2)) : 0,//未成交率(不超过100%)
|
|
'renewal_clients' => ($lastRenewal = rand(3, 25)),//待续费的客户人数
|
|
'renewal_rate' => $lastAssigned ? min(100, round($lastRenewal / $lastAssigned * 100, 2)) : 0,//待续费率(不超过100%)
|
|
'closed_clients' => $lastClosed,//已关单的客户人数
|
|
'closed_rate' => $lastContacted ? min(100, round($lastClosed / $lastContacted * 100, 2)) : 0,//关单率(不超过100%)
|
|
'conversion_rate' => $lastAssigned ? min(100, round($lastClosed / $lastAssigned * 100, 2)) : 0,//整体转化率(不超过100%)
|
|
]
|
|
];
|
|
}else{
|
|
return fail('角色权限不正确');
|
|
}
|
|
|
|
|
|
return success($data);
|
|
}
|
|
|
|
/**
|
|
* 员工端统计(销售)-获取销售数据页统计
|
|
*/
|
|
public function marketData(Request $request)
|
|
{
|
|
$resourceSharing = new ResourceSharing();
|
|
$customerResources = new CustomerResources();
|
|
$campus_person_role = new CampusPersonRole();
|
|
$personnel_id = $request->param('personnel_id','');//员工表id
|
|
$role_key_arr = $request->param('role_key_arr','');//array 角色key
|
|
$role_key_arr = explode(',',$role_key_arr);
|
|
if(empty($personnel_id)|| empty($role_key_arr)){
|
|
return fail('缺少参数');
|
|
}
|
|
|
|
// $role_key_arr = ['sale'];//@todo 调试使用
|
|
|
|
$assigned = 0;
|
|
$unassigned = 0;
|
|
$lastWeekNewTotal = 0;
|
|
// $resource_list = $resourceSharing->where(['shared_by' => $personnel_id])->select();
|
|
// foreach($resource_list as $item){
|
|
// $sharedAt = strtotime($item['shared_at']);
|
|
// $date = date("Y-m-d",$sharedAt);
|
|
// if(isDateInThisWeek($date)){
|
|
// $assigned++;
|
|
// }
|
|
//
|
|
// }
|
|
|
|
$customer_list = $customerResources->where(['consultant' => $personnel_id])->select();
|
|
|
|
|
|
foreach($customer_list as $item){
|
|
if($resourceSharing->where(['resource_id' => $item['id']])->find()){
|
|
if(isDateInThisWeek($item['create_date'])){
|
|
$assigned++;
|
|
}
|
|
}
|
|
if(!$resourceSharing->where(['resource_id' => $item['id']])->find()){
|
|
if(isDateInThisWeek($item['create_date'])){
|
|
$unassigned++;
|
|
}
|
|
}
|
|
if(isDateInLastWeek($item['create_date'])){
|
|
$lastWeekNewTotal++;
|
|
}
|
|
|
|
}
|
|
|
|
$staff_list = $campus_person_role
|
|
->alias("a")
|
|
->join(['school_personnel' => 'c'],'a.person_id = c.id','left')
|
|
->where([
|
|
['a.dept_id','in',1]
|
|
])
|
|
->field("c.id,c.name")
|
|
->select();
|
|
|
|
$staff_list = $staff_list->toArray(); // 转换成数组
|
|
$customer_list = $customerResources->select();
|
|
$goal = count($customer_list);
|
|
$staff_map = [];
|
|
foreach($staff_list as &$staff){
|
|
$staff['goal'] = $goal;
|
|
$staff['wx_yj'] = 0;
|
|
$staff_map[$staff['id']] = &$staff; // 现在可以引用赋值了
|
|
}
|
|
|
|
foreach ($customer_list as $val){
|
|
if(isset($staff_map[$val['consultant']])){
|
|
$staff_map[$val['consultant']]['wx_yj']++;
|
|
}
|
|
}
|
|
unset($staff); // 清除引用
|
|
|
|
|
|
if(in_array('market',$role_key_arr) || in_array('market_manager',$role_key_arr)){
|
|
|
|
//市场人员统计数据(地推拉人头的)
|
|
$data = [
|
|
'role_type'=>'market_type',//角色类型|market_type=市场,sale_type=销售
|
|
|
|
'num_1' => $assigned,//本周已分配
|
|
'num_2' => $unassigned,//本周未分配
|
|
'total_1' => ($newTotal = $assigned + $unassigned),//本周拉新总数
|
|
'total_2' => $lastWeekNewTotal,//上周拉新总数
|
|
|
|
'num_1_rate' => $newTotal ? min(100, round($assigned / $newTotal * 100, 2)) : 0,//已分配比例
|
|
'num_2_rate' => $newTotal ? min(100, round($unassigned / $newTotal * 100, 2)) : 0,//未分配比例
|
|
'num_3_rate' => $lastWeekNewTotal ? min(100, round($newTotal / max(1, $lastWeekNewTotal) * 100, 2)) : 0, // 本周拉新率(对比上周)
|
|
'num_4_rate' => $lastWeekNewTotal ? min(100, round($assigned / max(1, $lastWeekNewTotal) * 100, 2)) : 0, // 本周分配率(对比上周)
|
|
'staff_list' => $staff_list,
|
|
];
|
|
}elseif(in_array('sale',$role_key_arr) || in_array('sale_manager',$role_key_arr)){
|
|
//销售人员统计数据(买课的)
|
|
$data = [
|
|
'role_type'=>'sale',//角色类型|market_type=市场,sale_type=销售
|
|
|
|
'num_1' => $assigned,//本周已分配
|
|
'num_2' => $unassigned,//本周未分配
|
|
'total_1' => ($newTotal = $assigned + $unassigned),//本周拉新总数
|
|
'total_2' => $lastWeekNewTotal,//上周拉新总数
|
|
|
|
'num_1_rate' => $newTotal ? min(100, round($assigned / $newTotal * 100, 2)) : 0,//已分配比例
|
|
'num_2_rate' => $newTotal ? min(100, round($unassigned / $newTotal * 100, 2)) : 0,//未分配比例
|
|
'num_3_rate' => $lastWeekNewTotal ? min(100, round($newTotal / max(1, $lastWeekNewTotal) * 100, 2)) : 0, // 本周拉新率(对比上周)
|
|
'num_4_rate' => $lastWeekNewTotal ? min(100, round($assigned / max(1, $lastWeekNewTotal) * 100, 2)) : 0, // 本周分配率(对比上周)
|
|
'staff_list' => $staff_list,
|
|
];
|
|
}else{
|
|
return fail('角色权限不正确');
|
|
}
|
|
return success($data);
|
|
}
|
|
|
|
public function getStaffStatistics(Request $request){
|
|
$date = $request->param('date','');
|
|
$date = strtotime($date);
|
|
$year = date('Y', $date);
|
|
|
|
$campus_person_role = new CampusPersonRole();
|
|
$customerResources = new CustomerResources();
|
|
|
|
$dict = new \app\model\dict\Dict();
|
|
$dict_info = $dict->where([['key', '=', 'SourceChannel']])->findOrEmpty()->toArray();
|
|
$channelList = $dict_info['dictionary'];
|
|
|
|
|
|
$staff_list = $campus_person_role
|
|
->alias("a")
|
|
->join(['school_personnel' => 'c'],'a.person_id = c.id','left')
|
|
->where([
|
|
['a.dept_id','in',1]
|
|
])
|
|
->field("c.id,c.name")
|
|
->select();
|
|
$staff_list = $staff_list->toArray(); // 转换成数组
|
|
$customer_list = $customerResources->select()->toArray();
|
|
|
|
|
|
|
|
$staff_map = [];
|
|
foreach($staff_list as &$staff){
|
|
$staff['weekCount'] = 0;
|
|
$staff['monthCount'] = 0;
|
|
$staff['yearCount'] = 0;
|
|
$staff['total'] = 0;
|
|
foreach($channelList as $channel){
|
|
$staff['channel'][$channel['value']] = 0;
|
|
}
|
|
$staff_map[$staff['id']] = &$staff;
|
|
}
|
|
|
|
foreach ($customer_list as $val){
|
|
$dateTimestamp = strtotime($val['create_date']);
|
|
$dateYear = date('Y', $dateTimestamp);
|
|
|
|
if(isset($staff_map[$val['consultant']])){
|
|
|
|
if(isDateInThisWeek($val['create_date'])){
|
|
$staff_map[$val['consultant']]['weekCount']++;
|
|
}
|
|
if($val['create_year_month'] == date("Y-m",$date)){
|
|
$staff_map[$val['consultant']]['monthCount']++;
|
|
}
|
|
|
|
if($dateYear == $year){
|
|
$staff_map[$val['consultant']]['yearCount']++;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$campusModel = new \app\model\campus\Campus();
|
|
|
|
$campus = $campusModel->select();
|
|
$campus = $campus->toArray();
|
|
|
|
$channel_list = [];
|
|
foreach($channelList as &$channel){
|
|
foreach ($campus as $key=>$val){
|
|
$channel[$val['id']] = 0;
|
|
$channel['total'] = 0;
|
|
// $channel['staffs'] = $staff_map;
|
|
}
|
|
|
|
$channel_list[$channel['value']] = &$channel;
|
|
}
|
|
|
|
$customer_list = $customerResources->where([
|
|
['create_year_month','=',date("Y-m",$date)],
|
|
])->select()->toArray();
|
|
|
|
foreach ($customer_list as $val){
|
|
// dump($staff_map[$val['consultant']]['channel'][1]);die;
|
|
|
|
if(isset($channel_list[$val['source_channel']])){
|
|
$channel_list[$val['source_channel']]['total']++;
|
|
if(isset($channel_list[$val['source_channel']][$val['campus']])){
|
|
$channel_list[$val['source_channel']][$val['campus']]++;
|
|
}
|
|
|
|
|
|
if(isset($staff_map[$val['consultant']])){
|
|
|
|
if(isset($staff_map[$val['consultant']]['channel'][$val['source_channel']])){
|
|
|
|
$staff_map[$val['consultant']]['channel'][$val['source_channel']]++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
return success([
|
|
'staffData' => $staff_map,
|
|
'schoolList' => $campus,
|
|
'channelList' => $channel_list,
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|