token; $root_id = $token['root_org']; $where = [ ['name', '=', 'CustomerStatisticsSettingForMini'], ['root_id', '=', $root_id] ]; $data = Setting::where($where)->findOrEmpty(); if ($data->isEmpty()) { $content = ['valid', 'call', 'jiav', 'contact', 'subscribe', 'measure_room', 'arrival', 'deposit', 'signed', 'maika']; } else { $content = explode(',', $data['content']); } return json(['code' => self::success, 'msg' => 'success', 'data'=> $content]); } /** * 获取部门 */ public function get_org(){ $token = request()->token; $path = Org::where('id', '=', $token['org_id'])->value('path'); $list = Org::where([['path', 'like', $path . '%'], ['status', '=', 1]])->order('path asc, pid asc')->select()->toArray(); return json(['code'=> self::success, 'data'=> $list]); } /** * 统计首页 */ public function index(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $sub_org = orgSubIds($token['org_id']); // 所选部门员工统计数据 $e_where[] = ['org_id', 'in', $sub_org]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_ids = Employee::where($e_where)->column('id'); // 客户分配数 $wuxiao = Customer::changeState('无效', 'chaos'); $not_sure = Customer::changeState('待确认', 'chaos'); $customer_count = Customer::where([['employee_id', 'in', $employee_ids], ['sign_time', 'between', [$start_time, $end_time]], ['died', '<>', 2], ['state', 'not in', array_merge($wuxiao, $not_sure)]])->count(); $data['customer_count'] = $customer_count; // 打电话次数 $call_count = OutCallLog::where([['employee_id', 'in', $employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count(); // 手机打电话次数 $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count(); $data['call_count'] = $call_count + $mbCallCount; // 加微统计 $jiav_count = Customer::where([['employee_id', 'in', $employee_ids], ['add_wechat_time', 'between', [$start_time, $end_time]]])->count(); $data['jiav_count'] = $jiav_count; //沟通跟进次数 // 内容调用次数 $share_where[] = ['share_time', 'between', [$start_time, $end_time]]; $share_where[] = ['employee_id', 'in', $employee_ids]; $shared_count = ShareLog::where($share_where)->count(); // 微信沟通次数 $wx_contact_count = CustomerVisitLog::where([['org_id', 'in', $sub_org], ['addtime', 'between', [$start_time, $end_time]], ['type', '=', 3]])->count(); $contact_count = $call_count + $mbCallCount + $shared_count + $wx_contact_count; $data['contact_count'] = $contact_count; // 预约活动数量 $hd_subscribe_count = CustomersSubscribe::where([['employee_id', 'in', $employee_ids], ['subscribe_date', 'between', [$start_time, $end_time]], ['type', '=', 2], ['state', 'in', [0, 1]]])->count(); $data['hd_subscribe_count'] = $hd_subscribe_count; // 跟踪记录 // $visit_log = CustomerVisitLog::where([['org_id', 'in', $sub_org], ['confirm_date', 'between', [$start_time, $end_time]]])->column('customer_id,state,money'); $db_where[] = ['Customer.org_id', 'in', $sub_org]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,state,money') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->select() ->toArray(); $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $dd_state = CustomerVisitLog::changeState('已到店', 'chaos'); $jd_state = CustomerVisitLog::changeState('已交定', 'chaos'); $qd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos'); // 见面状态 // $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); // $make_state0 = array_merge($dd_state, $lf_state, $state3); $lf_count = []; $dd_count = []; $jd_count = []; $qd_count = []; $mk_count = []; $jd_money = 0; foreach ($visit_log as $k => $v) { if (in_array($v['state'], $lf_state) && !in_array($v['customer_id'], $lf_count)) { $lf_count[] = $v['customer_id']; } if (in_array($v['state'], $dd_state) && !in_array($v['customer_id'], $dd_count)) { $dd_count[] = $v['customer_id']; } if (in_array($v['state'], $jd_state) && !in_array($v['customer_id'], $jd_count)) { $jd_count[] = $v['customer_id']; $jd_money += $v['money']; } if (in_array($v['state'], $qd_state) && !in_array($v['customer_id'], $qd_count)) { $qd_count[] = $v['customer_id']; } if (in_array($v['state'], $mk_state) && !in_array($v['customer_id'], $mk_count)) { $mk_count[] = $v['customer_id']; } } $data['lf_count'] = count($lf_count); $data['dd_count'] = count($dd_count); $data['jd_count'] = count($jd_count); $data['jd_money'] = $jd_money; $data['qd_count'] = count($qd_count); $data['mk_count'] = count($mk_count); $qd_money = Customer::where('id', 'in', $qd_count)->sum('signed_money'); $data['qd_money'] = $qd_money; $data['org_name'] = Org::where('id', request()->token['org_id'])->value('name'); return json(['code'=> self::success, 'data'=> $data]); } /** * 到店统计(全部) */ public function arrival(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 部门信息 $data['org_name'] = Org::where('id', '=', $org_id)->value('name'); //员工数量 $sub_org = orgSubIds($org_id); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $data['employee_count'] = count($sub_employee_ids); // 所有客户量 $wuxiao = Customer::changeState('无效', 'chaos'); $c_where[] = ['org_id', 'in', $sub_org]; $c_where[] = ['sign_time', 'between', [$start_time, $end_time]]; $c_where[] = ['state', 'NOT NULL', null]; $c_where[] = ['state', 'not in', $wuxiao]; $c_where[] = ['died', '<>', 2]; $customer_list = Customer::where($c_where)->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]); })->column('id,addtime'); // 见面状态 // $state1 = CustomerVisitLog::changeState('已量房', 'chaos'); // $state2 = CustomerVisitLog::changeState('已到店', 'chaos'); // $state3 = CustomerVisitLog::changeState('已到场', 'chaos'); // $make_state0 = array_merge($state1, $state2, $state3); $dd_state = CustomerVisitLog::changeState('已到店', 'chaos'); $db_where[] = ['Customer.org_id', 'in', $sub_org]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state]; $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date,employee_id') ->view('Customer', 'addtime customer_addtime', 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->select() ->toArray(); $all_customer_count = count($customer_list); $list = []; foreach ($visit_log as $k => $v){ $list[$v['customer_id']][] = $v; } $data['all_count'] = count($visit_log); // 到店次数 $one = []; $two = []; $three = []; $one_arrival = 0; // 一次到店量 $two_arrival = 0; $three_arrival = 0; $one_days = 0; // 一次到店天数间隔 $two_days = 0; $three_days = 0; $lf_count = 0; // 量房转到店 $lf_days = 0; $wc_count = 0; // 外场转到店 $wc_days = 0; $nc_count = 0; // 内场活动到店 $one_customer_ids = []; $two_customer_ids = []; $three_customer_ids = []; // 先查出来后面要用到的已量房、已到场记录 $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $wc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $lw_where[] = ['Customer.org_id', 'in', $sub_org]; $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time]; $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)]; $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($lw_where) ->order('confirm_date asc') ->select() ->toArray(); $lf_list = []; $wc_list = []; foreach ($lw_log_list as $k => $v) { if (in_array($v['state'], $lf_state, true)) { if (empty($lf_list[$v['customer_id']])) { $lf_list[$v['customer_id']] = $v['confirm_date']; } } if (in_array($v['state'], $wc_state, true)) { if (empty($wc_list[$v['customer_id']])) { $wc_list[$v['customer_id']] = $v['confirm_date']; } } } foreach ($list as $k => $v){ if (count($v) == 1) { $one_arrival ++; $one_customer_ids[] = $k; $long = (strtotime($v[0]['confirm_date']) - strtotime($v[0]['customer_addtime'])) > 0 ? (strtotime($v[0]['confirm_date']) - strtotime($v[0]['customer_addtime'])) : 0; $days = ceil($long / (24*3600)); $one_days += $days; } if (count($v) == 2) { $two_arrival ++; $two_customer_ids[] = $k; $days = ceil((strtotime($v[1]['confirm_date']) - strtotime($v[0]['confirm_date'])) / (24*3600)); $two_days += $days; } if (count($v) > 2) { $three_arrival ++; $three_customer_ids[] = $k; $days = ceil((strtotime($v[2]['confirm_date']) - strtotime($v[1]['confirm_date'])) / (24*3600)); $three_days += $days; } if (isset($lf_list[$k])) { $lf_count ++; $days = ceil((strtotime($v[0]['confirm_date']) - strtotime($lf_list[$k])) / (24*3600)); $lf_days += $days; } if (isset($wc_list[$k])) { $wc_count ++; $days = ceil((strtotime($v[0]['confirm_date']) - strtotime($wc_list[$k])) / (24*3600)); $wc_days += $days; } // 内场活动到店 if (!empty($v[0]['aid'])){ $nc_count ++; } } // 签单 $qd_state = CustomerVisitLog::changeState('已签单', 'chaos'); // $qd_where[] = ['customer_id', 'in', $all_customer]; $qd_where[] = ['state', 'in', $qd_state]; $qd_where[] = ['org_id', 'in', $sub_org]; $qd_customer_ids = CustomerVisitLog::where($qd_where)->column('customer_id'); $one_qd = count(array_unique(array_intersect($qd_customer_ids, $one_customer_ids))); $two_qd = count(array_unique(array_intersect($qd_customer_ids, $two_customer_ids))); $three_qd = count(array_unique(array_intersect($qd_customer_ids, $three_customer_ids))); $one['count'] = $one_arrival; $one['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0; $one['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0; $one['lf_count'] = $lf_count; $one['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0; $one['wc_count'] = $wc_count; $one['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0; $one['nc_count'] = $nc_count; $one['qd_count'] = $one_qd; $two['count'] = $two_arrival; $two['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0; $two['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0; $two['qd_count'] = $two_qd; $three['count'] = $three_arrival; $three['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0; $three['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0; $three['qd_count'] = $three_qd; // 邀约计算 $all_customer = array_values(array_unique(array_merge($one_customer_ids, $two_customer_ids, $three_customer_ids))); $subscribe_where[] = ['state', 'in', [0, 1]]; $subscribe_where[] = ['type', '=', 1]; $subscribe_where[] = ['customer_id', 'in', $all_customer]; $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray(); $xs_count_one = 0; // 销售一次 $sj_count_one = 0; // 设计一次 $xs_count_two = 0; // 销售二次 $sj_count_two = 0; // 设计二次 $xs_count_three = 0; // 销售三次 $sj_count_three = 0; // 设计三次 foreach ($subscribe as $k => $v){ if (empty($v['designer_id']) && in_array($v['customer_id'], $one_customer_ids)){ $xs_count_one ++; } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $one_customer_ids)){ $sj_count_one ++; } if (empty($v['designer_id']) && in_array($v['customer_id'], $two_customer_ids)){ $xs_count_two ++; } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $two_customer_ids)){ $sj_count_two ++; } if (empty($v['designer_id']) && in_array($v['customer_id'], $three_customer_ids)){ $xs_count_three ++; } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $three_customer_ids)){ $sj_count_three ++; } } $one['xs_yaoyue'] = $xs_count_one; $one['xs_yaoyue_lv'] = $one_arrival > 0 ? round($xs_count_one * 100 / $one_arrival, 2) : 0; $one['sj_yaoyue'] = $sj_count_one; $one['sj_yaoyue_lv'] = $one_arrival > 0 ? round($sj_count_one * 100 / $one_arrival, 2) : 0; $two['xs_yaoyue'] = $xs_count_two; $two['xs_yaoyue_lv'] = $two_arrival > 0 ? round($xs_count_two * 100 / $two_arrival, 2) : 0; $two['sj_yaoyue'] = $sj_count_two; $two['sj_yaoyue_lv'] = $two_arrival > 0 ? round($sj_count_two * 100 / $two_arrival, 2) : 0; $three['xs_yaoyue'] = $xs_count_three; $three['xs_yaoyue_lv'] = $three_arrival > 0 ? round($xs_count_three * 100 / $three_arrival, 2) : 0; $three['sj_yaoyue'] = $sj_count_three; $three['sj_yaoyue_lv'] = $three_arrival > 0 ? round($sj_count_three * 100 / $three_arrival, 2) : 0; $data['one'] = $one; $data['two'] = $two; $data['three'] = $three; return json(['code'=> self::success, 'data'=> $data, 'msg'=> 'success']); } /** * 到店统计(部门) */ public function arrivalOrg(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $times = input('times', 0, 'intval'); if (!in_array($times, [1,2,3])){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', '', 'intval'); if (!$org_id) { $org_id = $token['org_id']; } if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所有客户量 $sub_org_ids = orgSubIds($org_id); $c_where[] = ['org_id', 'in', $sub_org_ids]; $c_where[] = ['state', 'NOT NULL', null]; $customer_all = Customer::where($c_where)->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]); })->column('id,addtime,employee_id,died,sign_time,state'); $customer = []; // 时间段内有效 $wuxiao = Customer::changeState('无效', 'chaos'); $start_totime = strtotime($start_time); $end_totime = strtotime($end_time); $customer_employee_id = []; // 客户所属员工id // 每个员工的客户 $employee_customer = []; foreach ($customer_all as $k => $v) { $customer_employee_id[$v['id']] = $v['employee_id']; if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) { $customer[] = $v; if (isset($employee_customer[$v['employee_id']])) { $employee_customer[$v['employee_id']][] = $v; } else { $employee_customer[$v['employee_id']] = []; $employee_customer[$v['employee_id']][] = $v; } } } $dd_state = CustomerVisitLog::changeState('已到店', 'chaos'); $db_where[] = ['Customer.org_id', 'in', $sub_org_ids]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state]; $log_list = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date,employee_id') ->view('Customer', 'sign_time customer_addtime', 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->order('confirm_date asc') ->select() ->toArray(); // 先查出来后面要用到的已量房、已到场记录 $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $wc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $lw_where[] = ['Customer.org_id', 'in', $sub_org_ids]; $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time]; $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)]; $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($lw_where) ->order('confirm_date asc') ->select() ->toArray(); $lf_list = []; $wc_list = []; foreach ($lw_log_list as $k => $v) { if (in_array($v['state'], $lf_state, true)) { if (empty($lf_list[$v['customer_id']])) { $lf_list[$v['customer_id']] = $v['confirm_date']; } } if (in_array($v['state'], $wc_state, true)) { if (empty($wc_list[$v['customer_id']])) { $wc_list[$v['customer_id']] = $v['confirm_date']; } } } // 查出来签单的数据 $qd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $qd_where[] = ['Customer.org_id', 'in', $sub_org_ids]; $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state]; $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id') ->view('Customer', 'org_id', 'Customer.id=CustomerVisitLog.customer_id') ->where($qd_where) ->select() ->toArray(); $qd_list = []; foreach ($qd_log_list as $k => $v) { $qd_list[$v['org_id']][] = $v['customer_id']; } // 每个员工的跟进记录 $employee_log = []; foreach ($log_list as $k => $v) { if (isset($customer_employee_id[$v['customer_id']])) { $employee_log[$customer_employee_id[$v['customer_id']]][] = $v; } else { $employee_log[$v['employee_id']][] = $v; } } $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray(); foreach ($sub_org as $k => $v) { // 是否有子部门 $have_sub_org = Org::where('pid', '=', $v['id'])->count(); if ($have_sub_org > 0) { $sub_org[$k]['have_sub_org'] = true; } else { $sub_org[$k]['have_sub_org'] = false; } $v_sub_org = orgSubIds($v['id']); $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $sub_org[$k]['employee_count'] = count($org_employee_ids); // 所有客户量 $cusdata = []; $s_list = []; // log foreach ($org_employee_ids as $kk => $vv) { if (isset($employee_customer[$vv])) { $cusdata = array_merge($cusdata, $employee_customer[$vv]); } if (isset($employee_log[$vv])) { $s_list = array_merge($s_list, $employee_log[$vv]); } } $all_customer_count = count($cusdata); $list = []; foreach ($s_list as $kk => $vv){ $list[$vv['customer_id']][] = $vv; } $customer_ids = []; switch ($times){ case 1: // 到店次数 $one_arrival = 0; // 一次到店量 $one_days = 0; // 一次到店天数间隔 $lf_count = 0; // 量房转到店 $lf_days = 0; $wc_count = 0; // 外场转到店 $wc_days = 0; $nc_count = 0; // 内场活动到店 foreach ($list as $kk => $vv){ if (count($vv) == 1) { $one_arrival ++; $customer_ids[] = $kk; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600)); $one_days += $days; if (isset($lf_list[$kk])) { $lf_count ++; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600)); $lf_days += $days; } if (isset($wc_list[$kk])) { $wc_count ++; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600)); $wc_days += $days; } // 内场活动到店 if (!empty($vv[0]['aid'])){ $nc_count ++; } } } $qd_find = []; foreach ($sub_org_ids as $o_id) { if (isset($qd_list[$o_id])) { $qd_find = array_merge($qd_find, $qd_list[$o_id]); } } $one_qd = count(array_unique(array_intersect($qd_find, $customer_ids))); $sub_org[$k]['count'] = $one_arrival; $sub_org[$k]['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0; $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0; $sub_org[$k]['lf_count'] = $lf_count; $sub_org[$k]['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0; $sub_org[$k]['wc_count'] = $wc_count; $sub_org[$k]['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0; $sub_org[$k]['nc_count'] = $nc_count; $sub_org[$k]['qd_count'] = $one_qd; break; case 2: // 到店次数 $two_arrival = 0; // 到店量 $two_days = 0; // 到店天数间隔 foreach ($list as $kk => $vv){ if (count($vv) == 2) { $two_arrival ++; $customer_ids[] = $kk; $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600)); $two_days += $days; } } $qd_find = []; foreach ($sub_org_ids as $o_id) { if (isset($qd_list[$o_id])) { $qd_find = array_merge($qd_find, $qd_list[$o_id]); } } $two_qd = count(array_unique(array_intersect($qd_find, $customer_ids))); $sub_org[$k]['count'] = $two_arrival; $sub_org[$k]['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0; $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0; $sub_org[$k]['qd_count'] = $two_qd; break; case 3: $three_arrival = 0; // 到店量 $three_days = 0; // 到店天数间隔 foreach ($list as $kk => $vv){ if (count($vv) > 2) { $three_arrival ++; $customer_ids[] = $kk; $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600)); $three_days += $days; } } $qd_find = []; foreach ($sub_org_ids as $o_id) { if (isset($qd_list[$o_id])) { $qd_find = array_merge($qd_find, $qd_list[$o_id]); } } $three_qd = count(array_unique(array_intersect($qd_find, $customer_ids))); $sub_org[$k]['count'] = $three_arrival; $sub_org[$k]['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0; $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0; $sub_org[$k]['qd_count'] = $three_qd; break; default: break; } // 邀约计算 unset($subscribe_where); $subscribe_where[] = ['state', 'in', [0, 1]]; $subscribe_where[] = ['type', '=', 1]; $subscribe_where[] = ['customer_id', 'in', $customer_ids]; $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray(); $xs_count = 0; // 销售一次 $sj_count = 0; // 设计一次 foreach ($subscribe as $sub_v){ if (empty($sub_v['designer_id'])){ $xs_count ++; } else { $sj_count ++; } } $sub_org[$k]['xs_yaoyue'] = $xs_count; $sub_org[$k]['xs_yaoyue_lv'] = $sub_org[$k]['count'] > 0 ? round($xs_count * 100 / $sub_org[$k]['count'], 2) : 0; $sub_org[$k]['sj_yaoyue'] = $sj_count; $sub_org[$k]['sj_yaoyue_lv'] = $sub_org[$k]['count'] > 0 ? round($sj_count * 100 / $sub_org[$k]['count'], 2) : 0; } $order = input('order', '', 'trim'); if ($order) { $order_arr = explode(' ', $order); $sort_field = $order_arr[0] ?? 'count'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'count': $field = 'count'; break; case 'wc_count': $field = 'wc_count'; break; case 'qd_count': $field = 'qd_count'; break; case 'lv': $field = 'lv'; break; default: $field = 'notFound'; break; } if ($sort_order == 'asc'){ $sort = SORT_ASC; } else { $sort = SORT_DESC; } $arr = []; foreach ($sub_org as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $sub_org); } } return json(['code'=> self::success, 'data'=> $sub_org, 'msg'=> 'success']); } /** * 到店统计(员工) */ public function arrivalEmployee(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $times = input('times', 0, 'intval'); if (!in_array($times, [1,2,3])){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所选部门员工统计数据 $e_where[] = ['org_id', '=', $org_id]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_list = Employee::with(['user'=> function($query){ $query->field('id,headimgurl'); }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray(); $all_where[] = ['org_id', '=', $org_id]; $all_where[] = ['state', 'NOT NULL', null]; $all_customer_list = Customer::where($all_where)->where(function($query){ $not_sure = Customer::changeState('待确认', 'chaos'); $or1[] = ['state', 'not in', $not_sure]; $or1[] = ['crm_res_id', '>', 0]; $or2[] = ['crm_res_id', 'null', null]; $query->whereOr([$or1, $or2]); })->column('id,employee_id,died,sign_time,state'); $customer = []; // 时间段内有效 $wuxiao = Customer::changeState('无效', 'chaos'); $start_totime = strtotime($start_time); $end_totime = strtotime($end_time); // 每个员工的客户 $employee_customer = []; $customer_employee_id = []; // 客户所属员工id foreach ($all_customer_list as $k => $v) { $customer_employee_id[$v['id']] = $v['employee_id']; if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) { $customer[] = $v; if (isset($employee_customer[$v['employee_id']])) { $employee_customer[$v['employee_id']][] = $v; } else { $employee_customer[$v['employee_id']] = []; $employee_customer[$v['employee_id']][] = $v; } } } $dd_state = CustomerVisitLog::changeState('已到店', 'chaos'); $db_where[] = ['Customer.org_id', '=', $org_id]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state]; $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,addtime,employee_id,confirm_date,aid') ->view('Customer', 'addtime customer_addtime', 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->select() ->toArray(); // 先查出来后面要用到的已量房、已到场记录 $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $wc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $lw_where[] = ['Customer.org_id', '=', $org_id]; $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time]; $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)]; $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($lw_where) ->order('confirm_date asc') ->select() ->toArray(); $lf_list = []; $wc_list = []; foreach ($lw_log_list as $k => $v) { if (in_array($v['state'], $lf_state, true)) { if (empty($lf_list[$v['customer_id']])) { $lf_list[$v['customer_id']] = $v['confirm_date']; } } if (in_array($v['state'], $wc_state, true)) { if (empty($wc_list[$v['customer_id']])) { $wc_list[$v['customer_id']] = $v['confirm_date']; } } } // 查出来签单的数据 $qd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $qd_where[] = ['Customer.org_id', '=', $org_id]; $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state]; $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id') ->view('Customer', 'employee_id', 'Customer.id=CustomerVisitLog.customer_id') ->where($qd_where) ->select() ->toArray(); $qd_list = []; foreach ($qd_log_list as $k => $v) { $qd_list[$v['customer_id']] = 1; } $employee_log = []; foreach ($visit_log as $k => $v) { if (isset($customer_employee_id[$v['customer_id']])) { $employee_log[$customer_employee_id[$v['customer_id']]][] = $v; } else { $employee_log[$v['employee_id']][] = $v; } } foreach ($employee_list as $k => $v) { // 所有客户量 $all_customer_count = 0; if (isset($employee_customer[$v['id']])) { $all_customer_count = count($employee_customer[$v['id']]); } $list = []; $v_visit_log = []; if (isset($employee_log[$v['id']])) { $v_visit_log = $employee_log[$v['id']]; } foreach ($v_visit_log as $kk => $vv){ $list[$vv['customer_id']][] = $vv; } $customer_ids = []; switch ($times){ case 1: // 到店次数 $one_arrival = 0; // 一次到店量 $one_days = 0; // 一次到店天数间隔 $lf_count = 0; // 量房转到店 $lf_days = 0; $wc_count = 0; // 外场转到店 $wc_days = 0; $nc_count = 0; // 内场活动到店 $one_qd = 0; foreach ($list as $kk => $vv){ if (count($vv) == 1) { $one_arrival ++; $customer_id[] = $kk; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600)); $one_days += $days; if (isset($qd_list[$kk])) { $one_qd ++; } if (isset($lf_list[$kk])) { $lf_count ++; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600)); $lf_days += $days; } if (isset($wc_list[$kk])) { $wc_count ++; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600)); $wc_days += $days; } // 内场活动到店 if (!empty($vv[0]['aid'])){ $nc_count ++; } } } $employee_list[$k]['count'] = $one_arrival; $employee_list[$k]['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0; $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0; $employee_list[$k]['lf_count'] = $lf_count; $employee_list[$k]['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0; $employee_list[$k]['wc_count'] = $wc_count; $employee_list[$k]['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0; $employee_list[$k]['nc_count'] = $nc_count; $employee_list[$k]['qd_count'] = $one_qd; break; case 2: // 到店次数 $two_arrival = 0; // 到店量 $two_days = 0; // 到店天数间隔 $two_qd = 0; foreach ($list as $kk => $vv){ if (count($vv) == 2) { $two_arrival ++; $customer_ids[] = $kk; $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600)); $two_days += $days; if (isset($qd_list[$kk])) { $two_qd ++; } } } $employee_list[$k]['count'] = $two_arrival; $employee_list[$k]['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0; $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0; $employee_list[$k]['qd_count'] = $two_qd; break; case 3: $three_arrival = 0; // 到店量 $three_days = 0; // 到店天数间隔 $three_qd = 0; foreach ($list as $kk => $vv){ if (count($vv) > 2) { $three_arrival ++; $customer_ids[] = $kk; $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600)); $three_days += $days; if (isset($qd_list[$kk])) { $three_qd ++; } } } $employee_list[$k]['count'] = $three_arrival; $employee_list[$k]['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0; $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0; $employee_list[$k]['qd_count'] = $three_qd; break; default: break; } // 邀约计算 unset($subscribe_where); $subscribe_where[] = ['state', 'in', [0, 1]]; $subscribe_where[] = ['type', '=', 1]; $subscribe_where[] = ['customer_id', 'in', $customer_ids]; $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray(); $xs_count = 0; // 销售一次 $sj_count = 0; // 设计一次 foreach ($subscribe as $sub_v){ if (empty($sub_v['designer_id'])){ $xs_count ++; } else { $sj_count ++; } } $employee_list[$k]['xs_yaoyue'] = $xs_count; $employee_list[$k]['xs_yaoyue_lv'] = $employee_list[$k]['count'] > 0 ? round($xs_count * 100 / $employee_list[$k]['count'], 2) : 0; $employee_list[$k]['sj_yaoyue'] = $sj_count; $employee_list[$k]['sj_yaoyue_lv'] = $employee_list[$k]['count'] > 0 ? round($sj_count * 100 / $employee_list[$k]['count'], 2) : 0; } $order = input('order', '', 'trim'); if ($order) { $order_arr = explode(' ', $order); $sort_field = $order_arr[0] ?? 'count'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'count': $field = 'count'; break; case 'wc_count': $field = 'wc_count'; break; case 'qd_count': $field = 'qd_count'; break; case 'lv': $field = 'lv'; break; default: $field = 'notFound'; break; } if ($sort_order == 'asc'){ $sort = SORT_ASC; } else { $sort = SORT_DESC; } $arr = []; foreach ($employee_list as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $employee_list); } } $page = input('page', 1, 'intval'); $limit = input('limit', 10, 'intval'); $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit); return json(['code'=> self::success, 'data'=> $employee_list, 'msg'=> 'success']); } /** * 到店统计(客户) */ public function arrivalCustomer(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $times = input('times', 0, 'intval'); if (!in_array($times, [1,2,3])){ return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']); } $employee_id = input('employee_id', '', 'intval'); $sub_org = orgSubIds($token['org_id']); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); if (!in_array($employee_id, $sub_employee_ids)){ return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']); } $dd_state = CustomerVisitLog::changeState('已到店', 'chaos'); $wuxiao = Customer::changeState('无效', 'chaos'); $db_where[] = ['Customer.employee_id', '=', $employee_id]; $db_where[] = ['Customer.state', 'NOT NULL', null]; $db_where[] = ['Customer.state', 'not in', $wuxiao]; $db_where[] = ['Customer.died', '<>', 2]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state]; $s_list = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date') ->view('Customer', 'sign_time customer_addtime', 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->order('confirm_date asc') ->select() ->toArray(); $list = []; foreach ($s_list as $k => $v){ $list[$v['customer_id']][] = $v; } // 先查出来后面要用到的已量房、已到场记录 $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $wc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $lw_where[] = ['Customer.employee_id', '=', $employee_id]; $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time]; $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)]; $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($lw_where) ->order('confirm_date asc') ->select() ->toArray(); $lf_list = []; $wc_list = []; foreach ($lw_log_list as $k => $v) { if (in_array($v['state'], $lf_state, true)) { if (empty($lf_list[$v['customer_id']])) { $lf_list[$v['customer_id']] = $v['confirm_date']; } } if (in_array($v['state'], $wc_state, true)) { if (empty($wc_list[$v['customer_id']])) { $wc_list[$v['customer_id']] = $v['confirm_date']; } } } // 查出来签单的数据 $qd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $qd_where[] = ['Customer.employee_id', '=', $employee_id]; $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state]; $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id') ->view('Customer', 'org_id', 'Customer.id=CustomerVisitLog.customer_id') ->where($qd_where) ->select() ->toArray(); $qd_list = []; foreach ($qd_log_list as $k => $v) { $qd_list[$v['customer_id']] = 1; } $wuxiao = Customer::changeState('无效', 'chaos'); $not_sure = Customer::changeState('待确认', 'chaos'); $wuxiao_not_sure = array_merge($wuxiao, $not_sure); $all_where[] = ['employee_id', '=', $employee_id]; $all_where[] = ['state', 'NOT NULL', null]; $all_where[] = ['state', 'NOT IN', $wuxiao_not_sure]; $all_where[] = ['addtime', 'between', [$start_time, $end_time]]; $all_where[] = ['died', '<>', 2]; $all_customer_count = Customer::where($all_where)->where(function($query){ $not_sure = Customer::changeState('待确认', 'chaos'); $or1[] = ['state', 'not in', $not_sure]; $or1[] = ['crm_res_id', '>', 0]; $or2[] = ['crm_res_id', 'null', null]; $query->whereOr([$or1, $or2]); })->count(); $employee_data['count'] = 0; $employee_data['days'] = 0; $employee_data['lv'] = 0; $employee_data['lf_count'] = 0; $employee_data['lf_days'] = 0; $employee_data['wc_count'] = 0; $employee_data['wc_days'] = 0; $employee_data['nc_count'] = 0; $employee_data['qd_count'] = 0; $customer_ids = []; switch ($times){ case 1: // 到店次数 $one_arrival = 0; // 一次到店量 $one_days = 0; // 一次到店天数间隔 $lf_count = 0; // 量房转到店 $lf_days = 0; $wc_count = 0; // 外场转到店 $wc_days = 0; $nc_count = 0; // 内场活动到店 $one_qd = 0; foreach ($list as $kk => $vv){ if (count($vv) == 1) { $one_arrival ++; $customer_ids[] = $kk; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600)); $one_days += $days; if (isset($qd_list[$kk])) { $one_qd ++; } if (isset($lf_list[$kk])) { $lf_count ++; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600)); $lf_days += $days; } if (isset($wc_list[$kk])) { $wc_count ++; $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600)); $wc_days += $days; } // 内场活动到店 if (!empty($vv[0]['aid'])){ $nc_count ++; } } } $employee_data['count'] = $one_arrival; $employee_data['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0; $employee_data['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0; $employee_data['lf_count'] = $lf_count; $employee_data['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0; $employee_data['wc_count'] = $wc_count; $employee_data['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0; $employee_data['nc_count'] = $nc_count; $employee_data['qd_count'] = $one_qd; break; case 2: // 到店次数 $two_arrival = 0; // 到店量 $two_days = 0; // 到店天数间隔 $two_qd = 0; foreach ($list as $kk => $vv){ if (count($vv) == 2) { $two_arrival ++; $customer_ids[] = $kk; $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600)); $two_days += $days; if (isset($qd_list[$kk])) { $two_qd ++; } } } $employee_data['count'] = $two_arrival; $employee_data['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0; $employee_data['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0; $employee_data['qd_count'] = $two_qd; break; case 3: $three_arrival = 0; // 到店量 $three_days = 0; // 到店天数间隔 $three_qd = 0; foreach ($list as $kk => $vv){ if (count($vv) > 2) { $three_arrival ++; $customer_ids[] = $kk; $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600)); $three_days += $days; if (isset($qd_list[$kk])) { $three_qd ++; } } } $employee_data['count'] = $three_arrival; $employee_data['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0; $employee_data['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0; $employee_data['qd_count'] = $three_qd; break; default: break; } // 邀约计算 $subscribe_where[] = ['state', 'in', [0, 1]]; $subscribe_where[] = ['type', '=', 1]; $subscribe_where[] = ['customer_id', 'in', $customer_ids]; $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray(); $xs_count = 0; // 销售一次 $sj_count = 0; // 设计一次 foreach ($subscribe as $sub_v){ if (empty($sub_v['designer_id'])){ $xs_count ++; } else { $sj_count ++; } } $employee_data['xs_yaoyue'] = $xs_count; $employee_data['xs_yaoyue_lv'] = $employee_data['count'] > 0 ? round($xs_count * 100 / $employee_data['count'], 2) : 0; $employee_data['sj_yaoyue'] = $sj_count; $employee_data['sj_yaoyue_lv'] = $employee_data['count'] > 0 ? round($sj_count * 100 / $employee_data['count'], 2) : 0; $employee_name = Employee::where('id', $employee_id)->value('name'); $employee_data['employee_name'] = $employee_name; $data['employee'] = $employee_data; $customer_id = []; $customer_log_latest = []; foreach ($list as $k => $v) { switch ($times) { case 1: if (count($v) == 1) { $customer_id[] = $k; $customer_log_latest[$k] = $v[0]['confirm_date']; } break; case 2: if (count($v) == 2) { $customer_id[] = $k; $customer_log_latest[$k] = $v[1]['confirm_date']; } break; case 3: if (count($v) > 2) { $customer_id[] = $k; $add_key = count($v) - 1; $customer_log_latest[$k] = $v[$add_key]['confirm_date']; } break; default: break; } } $type = input('type', '', 'trim'); if ($type){ switch ($type){ case 'waichang': $customer_id = []; // 外场转到店 $wc_state = CustomerVisitLog::changeState('已到场', 'chaos'); foreach ($customer_log_latest as $k => $v) { unset($wc_where); $wc_where[] = ['customer_id', '=', $k]; $wc_where[] = ['state', 'in', $wc_state]; $wc_where[] = ['confirm_date', '<', $v]; $wc_find = CustomerVisitLog::where($wc_where)->order('confirm_date asc')->findOrEmpty(); if (!$wc_find->isEmpty()){ $customer_id[] = $k; } } break; case 'liangfang': $customer_id = []; // 量房转到店 $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); foreach ($customer_log_latest as $k => $v) { unset($lf_where); $lf_where[] = ['customer_id', '=', $k]; $lf_where[] = ['state', 'in', $lf_state]; $lf_where[] = ['confirm_date', '<', $v]; $lf_find = CustomerVisitLog::where($lf_where)->order('confirm_date asc')->findOrEmpty(); if (!$lf_find->isEmpty()) { $customer_id[] = $k; } } break; case 'qiandan': // 签单 $jd_state = CustomerVisitLog::changeState('已交定', 'chaos'); $jd_where[] = ['customer_id', 'in', $customer_id]; $jd_where[] = ['state', 'in', $jd_state]; $customer_id = CustomerVisitLog::where($jd_where)->column('customer_id'); break; default: $customer_id = []; break; } } $page = input('page', 1, 'intval'); $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select(); $customers = $customers->visible(['id', 'employee_id', 'designer_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'employee.name', 'agent.agent_name'])->toArray(); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach ($customers as $k => $v) { $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; } $data['customer'] = $customers; return json(['code'=> self::success, 'data'=> $data, 'msg'=> 'success']); } /** * 量房统计 */ public function measure_house_index() { $token = $this->request->token; $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', '', 'intval'); $org_id = !empty($org_id) ? $org_id : $token['org_id']; if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); //员工数量 $org_name = Org::where('id', $org_id)->value('name'); $sub_org = orgSubIds($org_id); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); // 所有客户量 $wuxiao = Customer::changeState('无效', 'chaos'); $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $c_where[] = ['org_id', 'in', $sub_org]; $c_where[] = ['sign_time', 'between', [$start_time, $end_time]]; $c_where[] = ['state', 'NOT NULL', null]; $c_where[] = ['state', 'not in', $wuxiao]; $c_where[] = ['died', '<>', 2]; $cusdata = Customer::where($c_where)->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]); })->column('id,addtime'); $db_where[] = ['Customer.org_id', 'in', $sub_org]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state]; $list = Db::view('CustomerVisitLog', 'id,customer_id,measure_room_img_type,addtime,confirm_date') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->select() ->toArray(); $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0; $meatime = []; $ylids = []; foreach($list as $key => $val){ if(!empty($val['measure_room_img_type'])){ $allmea = explode(',', $val['measure_room_img_type']); foreach($allmea as $k => $v){ if(isset($v) && strstr($v, '量房图片')) $mesimg++; if(isset($v) && strstr($v, '量房报告图片')) $demand++; if(isset($v) && strstr($v, '客户需求图片')) $report++; } } if(!in_array($val['customer_id'], array_keys($meatime))) $meatime[$val['customer_id']] = $val['confirm_date']; if(!in_array($val['customer_id'], $ylids)) $ylids[] = $val['customer_id']; } $ylcus = Customer::where('id', 'in', $ylids)->field('id,addtime')->select()->toArray(); $agvtime = []; foreach($ylcus as $key => $val){ if(!empty($meatime[$val['id']])) $agvtime[] = strtotime($meatime[$val['id']]) - strtotime($val['addtime']) > 0 ? strtotime($meatime[$val['id']]) - strtotime($val['addtime']) : 0; } $meahounum = count($list); $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0; $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0; $data = ['org_name'=> $org_name, 'employee_count'=> count($sub_employee_ids), 'report'=> $report, 'demand'=> $demand, 'mesimg'=> $mesimg, 'meahounum'=> $meahounum, 'agvcycle'=> $agvcycle, 'mearate'=> $mearate]; return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]); } /** * 量房部门数据 */ public function measure_house_org(){ $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time','order']); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', '', 'intval'); if (!$org_id) { $org_id = $token['org_id']; } if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所选部门 $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray(); // 所有客户量 用于后面计算百分比 $sub_org_ids = orgSubIds($org_id); $c_where[] = ['org_id', 'in', $sub_org_ids]; $c_where[] = ['state', 'NOT NULL', null]; $customer_all = Customer::where($c_where)->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]); })->column('id,addtime,employee_id,died,sign_time,state'); $customer = []; // 时间段内有效 $wuxiao = Customer::changeState('无效', 'chaos'); $start_totime = strtotime($start_time); $end_totime = strtotime($end_time); $customer_employee_id = []; // 客户所属员工id // 每个员工的客户 $employee_customer = []; foreach ($customer_all as $k => $v) { $customer_employee_id[$v['id']] = $v['employee_id']; if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) { $customer[] = $v; if (isset($employee_customer[$v['employee_id']])) { $employee_customer[$v['employee_id']][] = $v; } else { $employee_customer[$v['employee_id']] = []; $employee_customer[$v['employee_id']][] = $v; } } } $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $db_where[] = ['Customer.org_id', 'in', $sub_org_ids]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state]; $log_list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date,employee_id') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->order('confirm_date asc') ->select() ->toArray(); // 每个员工的跟进记录 $employee_log = []; foreach ($log_list as $k => $v) { if (isset($customer_employee_id[$v['customer_id']])) { $employee_log[$customer_employee_id[$v['customer_id']]][] = $v; } else { $employee_log[$v['employee_id']][] = $v; } } foreach ($sub_org as $k => $v){ // 是否有子部门 $have_sub_org = Org::where('pid', '=', $v['id'])->count(); if ($have_sub_org > 0){ $sub_org[$k]['have_sub_org'] = true; } else { $sub_org[$k]['have_sub_org'] = false; } unset($meatime); unset($ylids); unset($agvtime); $v_sub_org = orgSubIds($v['id']); $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $sub_org[$k]['employee_count'] = count($org_employee_ids); $cusdata = []; foreach ($org_employee_ids as $kk => $vv) { if (isset($employee_customer[$vv])) { $cusdata = array_merge($cusdata, $employee_customer[$vv]); } } $list = []; foreach ($org_employee_ids as $kk => $vv) { if (isset($employee_log[$vv])) { $list = array_merge($list, $employee_log[$vv]); } } $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0; $meatime = []; $ylids = []; foreach($list as $key => $val){ if(!empty($val['measure_room_img_type'])){ $allmea = explode(',', $val['measure_room_img_type']); foreach($allmea as $m=>$p){ if(isset($p) && strstr($p,'量房图片')) $mesimg++; if(isset($p) && strstr($p,'量房报告图片')) $demand++; if(isset($p) && strstr($p,'客户需求图片')) $report++; } } if(!in_array($val['customer_id'], array_keys($meatime))) $meatime[$val['customer_id']] = $val['confirm_date']; if(!in_array($val['customer_id'], $ylids)) $ylids[] = $val['customer_id']; } $ylcus = Customer::where('id', 'in', $ylids)->field('id,sign_time')->select()->toArray(); $agvtime = []; foreach($ylcus as $key => $val){ if(!empty($meatime[$val['id']])) $agvtime[] = (strtotime($meatime[$val['id']]) - strtotime($val['sign_time'])) > 0 ? strtotime($meatime[$val['id']]) - strtotime($val['sign_time']) : 0; } $meahounum = count($list); $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0; $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0; $sub_org[$k]['report'] = $report; $sub_org[$k]['demand'] = $demand; $sub_org[$k]['mesimg'] = $mesimg; $sub_org[$k]['meahounum'] = $meahounum; $sub_org[$k]['agvcycle'] = $agvcycle; $sub_org[$k]['mearate'] = $mearate; } if (!empty($param['order'])) { $order_arr = explode(' ', $param['order']); $sort_field = $order_arr[0] ?? 'meahounum'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'meahounum': $field = 'meahounum'; break; case 'agvcycle': $field = 'agvcycle'; break; case 'mearate': $field = 'mearate'; break; default: $field = 'notFound'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($sub_org as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $sub_org); } } return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]); } /** * 量房员工数据 */ public function measure_house_employee(){ $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); // 所选部门 $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id']; $root_sub = orgSubIds($token['root_org']); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所选部门员工统计数据 $e_where[] = ['org_id', '=', $org_id]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_list = Employee::with(['user'=> function($query){ $query->field('id,headimgurl'); }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray(); // 所有客户量 $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $c_where[] = ['org_id', '=', $org_id]; $c_where[] = ['state', 'NOT NULL', null]; $customer_all = Customer::where($c_where)->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]); })->column('id,addtime,employee_id,died,sign_time,state'); $customer = []; // 时间段内有效 $wuxiao = Customer::changeState('无效', 'chaos'); $start_totime = strtotime($start_time); $end_totime = strtotime($end_time); // 每个员工的客户 $employee_customer = []; $customer_employee_id = []; // 客户所属员工id foreach ($customer_all as $k => $v) { $customer_employee_id[$v['id']] = $v['employee_id']; if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) { $customer[] = $v; if (isset($employee_customer[$v['employee_id']])) { $employee_customer[$v['employee_id']][] = $v; } else { $employee_customer[$v['employee_id']] = []; $employee_customer[$v['employee_id']][] = $v; } } } $db_where[] = ['Customer.org_id', '=', $org_id]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state]; $log_list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date,employee_id') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->order('confirm_date asc') ->select() ->toArray(); // 每个员工的跟进记录 $employee_log = []; foreach ($log_list as $k => $v) { if (isset($customer_employee_id[$v['customer_id']])) { $employee_log[$customer_employee_id[$v['customer_id']]][] = $v; } else { $employee_log[$v['employee_id']][] = $v; } } foreach($employee_list as $key=>$val){ $cusdata = []; if (isset($employee_customer[$val['id']])) { $cusdata = $employee_customer[$val['id']]; } $list = []; if (isset($employee_log[$val['id']])) { $list = $employee_log[$val['id']]; } $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0; $meatime =[]; $ylids = []; foreach($list as $k => $v){ if(!empty($v['measure_room_img_type'])){ $allmea = explode(',', $v['measure_room_img_type']); foreach($allmea as $kk => $vv){ if(isset($vv) && strstr($vv,'量房图片')) $mesimg++; if(isset($vv) && strstr($vv,'量房报告图片')) $demand++; if(isset($vv) && strstr($vv,'客户需求图片')) $report++; } } if(!in_array($v['customer_id'], array_keys($meatime))) $meatime[$v['customer_id']] = $v['confirm_date']; if(!in_array($v['customer_id'], $ylids)) $ylids[] = $v['customer_id']; } $ylcus = Customer::where('id','in',$ylids)->field('id,sign_time')->select()->toArray(); $agvtime = []; foreach($ylcus as $k => $v){ if(!empty($meatime[$v['id']])) $agvtime[] = (strtotime($meatime[$v['id']]) - strtotime($v['sign_time'])) > 0 ? strtotime($meatime[$v['id']]) - strtotime($v['sign_time']) : 0; } $meahounum = count($list); $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0; $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0; $employee_list[$key]['employee_count'] = count($employee_list); $employee_list[$key]['report'] = $report; $employee_list[$key]['demand'] = $demand; $employee_list[$key]['mesimg'] = $mesimg; $employee_list[$key]['meahounum'] = $meahounum; $employee_list[$key]['agvcycle'] = $agvcycle; $employee_list[$key]['mearate'] = $mearate; } if (!empty($param['order'])) { $order_arr = explode(' ', $param['order']); $sort_field = $order_arr[0] ?? 'meahounum'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'meahounum': $field = 'meahounum'; break; case 'agvcycle': $field = 'agvcycle'; break; case 'mearate': $field = 'mearate'; break; default: $field = 'notFound'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($employee_list as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $employee_list); } } $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']); return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]); } /** * 量房员工客户数据 */ public function measure_house_empcus() { $token = $this->request->token; $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $employee_id = input('employee_id', '', 'intval'); $sub_org = orgSubIds($token['org_id']); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); if (!in_array($employee_id, $sub_employee_ids)){ return json(['code'=> self::success, 'data'=> ['empdata'=> [], 'list'=> []], 'msg'=> 'success']); } $lf_state = CustomerVisitLog::changeState('已量房', 'chaos'); $wuxiao = Customer::changeState('无效', 'chaos'); $db_where[] = ['Customer.employee_id', '=', $employee_id]; $db_where[] = ['Customer.state', 'NOT NULL', null]; $db_where[] = ['Customer.state', 'not in', $wuxiao]; $db_where[] = ['Customer.died', '<>', 2]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state]; $list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date') ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->order('confirm_date asc') ->select() ->toArray(); $cusids = array_unique(array_column($list,'customer_id')); $cuslist = Customer::with(['employee'=>function($query){ $query->field('id,name'); }])->where('id', 'in', $cusids)->where('employee_id', '=', $param['employee_id'])->field('id,name,phone,state,addtime,community_name,employee_id,last_contact_date,level,sex,square,updatetime')->page($param['page'],$param['limit'])->select()->toArray(); $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find(); if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach($cuslist as $key=>$val){ $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']]; } $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0; $meatime = []; $ylids = []; foreach ($list as $k => $v) { if (!empty($v['measure_room_img_type'])) { $allmea = explode(',', $v['measure_room_img_type']); foreach ($allmea as $kk => $vv) { if (isset($vv) && strstr($vv, '量房图片')) $mesimg++; if (isset($vv) && strstr($vv, '量房报告图片')) $demand++; if (isset($vv) && strstr($vv, '客户需求图片')) $report++; } } if (!in_array($v['customer_id'], array_keys($meatime))) $meatime[$v['customer_id']] = $v['confirm_date']; if (!in_array($v['customer_id'], $ylids)) $ylids[] = $v['customer_id']; } $wuxiao = Customer::changeState('无效', 'chaos'); $not_sure = Customer::changeState('待确认', 'chaos'); $wuxiao_not_sure = array_merge($wuxiao, $not_sure); $all_where[] = ['employee_id', '=', $employee_id]; $all_where[] = ['state', 'NOT NULL', null]; $all_where[] = ['state', 'NOT IN', $wuxiao_not_sure]; $all_where[] = ['sign_time', 'between', [$start_time, $end_time]]; $all_where[] = ['died', '<>', 2]; $cusdata = Customer::where($all_where)->count(); $ylcus = Customer::where('id', 'in', $ylids)->field('id,sign_time')->select()->toArray(); $agvtime = []; foreach ($ylcus as $k => $v) { if (!empty($meatime[$v['id']])) $agvtime[] = (strtotime($meatime[$v['id']]) - strtotime($v['sign_time'])) > 0 ? strtotime($meatime[$v['id']]) - strtotime($v['sign_time']) : 0; } $meahounum = count($list); $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime) / count($agvtime)) / 86400) : 0; $mearate = $cusdata && !empty($meatime) ? round(count($meatime) / $cusdata, 2) : 0; $empdata = ['empname'=> $employeedata['name'], 'employee_count'=> 1, 'report'=> $report, 'demand'=> $demand, 'mesimg'=> $mesimg, 'meahounum'=> $meahounum, 'agvcycle'=> $agvcycle, 'mearate'=> $mearate]; $data = ['empdata'=> $empdata, 'list'=> $cuslist]; return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]); } /** * 转单首页统计 */ public function sign_index() { $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time']); //员工数量 $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id']; $org_name = Org::where('id',$org_id)->value('name'); $sub_org = orgSubIds($org_id); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); //$cusdata = Customer::where([['addtime','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray(); $cusdata = Customer::where([['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray(); //$where[] = ['org_id','in',$sub_org]; $where[] = ['employee_id','in',$sub_employee_ids]; $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')]; $where[] = ['confirm_date', 'between', [$param['start_time'] . ' 00:00:00', $param['end_time'] . ' 23:59:59']]; $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray(); $signids = array_unique(array_column($list,'customer_id')); //$shopdep_list = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',array_merge(CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已交定','chaos'))]])->field('id,customer_id,state')->select()->toArray(); $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id'); $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id'); $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0; $signnum = count($list); $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0; $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0; $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0; $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0; $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0; $data = ['org_name'=>$org_name,'shoprate'=>$shoprate,'depositrate'=>$depositrate,'signmoney'=>$signmoney,'signnum'=>$signnum,'signagv'=>$signagv,'signrate'=>$signrate]; return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]); } /** * 转单部门数据 */ public function sign_org() { $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time','order']); $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id']; // 所选部门 $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray(); foreach ($sub_org as $k => $v){ // 是否有子部门 $have_sub_org = Org::where('pid', '=', $v['id'])->count(); if ($have_sub_org > 0){ $sub_org[$k]['have_sub_org'] = true; } else { $sub_org[$k]['have_sub_org'] = false; } unset($where); unset($meatime); unset($ylids); unset($agvtime); $v_sub_org = orgSubIds($v['id']); $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $sub_org[$k]['employee_count'] = count($org_employee_ids); //$cusdata = Customer::where([['addtime','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','in',$org_employee_ids]])->field('id,addtime')->select()->toArray(); $cusdata = Customer::where([['employee_id','in',$org_employee_ids]])->field('id,addtime')->select()->toArray(); //$where[] = ['org_id','in',$v_sub_org]; $where[] = ['employee_id','in',$org_employee_ids]; $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')]; $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']]; $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray(); $signids = array_unique(array_column($list,'customer_id')); //$shopdep_list = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',array_merge(CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已交定','chaos'))]])->field('id,customer_id,state')->select()->toArray(); $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id'); $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id'); $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0; $signnum = count($list); $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0; $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0; $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0; $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0; $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0; $sub_org[$k]['shoprate'] = $shoprate; $sub_org[$k]['depositrate'] = $depositrate; $sub_org[$k]['signmoney'] = $signmoney; $sub_org[$k]['signnum'] = $signnum; $sub_org[$k]['signagv'] = $signagv; $sub_org[$k]['signrate'] = $signrate; } if (!empty($param['order'])) { $order_arr = explode(' ', $param['order']); $sort_field = $order_arr[0] ?? 'signnum'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'signnum': $field = 'signnum'; break; case 'signmoney': $field = 'signmoney'; break; default: $field = 'notFound'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($sub_org as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $sub_org); } } return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]); } /** * 转单员工数据 */ public function sign_employee() { $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]); // 所选部门 $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id']; // 所选部门员工统计数据 $e_where[] = ['org_id', '=', $org_id]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_list = Employee::with(['user'=> function($query){ $query->field('id,headimgurl'); }])->where($e_where)->field('id,name,org_id,root_id,uid')->page($param['page'],$param['limit'])->select()->toArray(); foreach($employee_list as $key=>$val){ unset($where); //$cusdata = Customer::where([['addtime','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','=',$val['id']]])->field('id,addtime')->select()->toArray(); $cusdata = Customer::where([['employee_id','=',$val['id']]])->field('id,addtime')->select()->toArray(); $where[] = ['employee_id','=',$val['id']]; //$where[] = ['customer_id','in',$cusids]; $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')]; $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']]; $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray(); $signids = array_unique(array_column($list,'customer_id')); //$shopdep_list = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',array_merge(CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已交定','chaos'))]])->field('id,customer_id,state')->select()->toArray(); $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id'); $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id'); $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0; $signnum = count($list); $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0; $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0; $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0; $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0; $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0; $employee_list[$key]['employee_count'] = count($employee_list); $employee_list[$key]['shoprate'] = $shoprate; $employee_list[$key]['depositrate'] = $depositrate; $employee_list[$key]['signmoney'] = $signmoney; $employee_list[$key]['signnum'] = $signnum; $employee_list[$key]['signagv'] = $signagv; $employee_list[$key]['signrate'] = $signrate; } if (!empty($param['order'])) { $order_arr = explode(' ', $param['order']); $sort_field = $order_arr[0] ?? 'signnum'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'signnum': $field = 'signnum'; break; case 'signmoney': $field = 'signmoney'; break; default: $field = 'notFound'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($employee_list as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $employee_list); } } return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]); } /** * 签单客户数据 */ public function sign_empcus() { $token = $this->request->token; $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]); $where[] = ['employee_id','=',$param['employee_id']]; $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')]; $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']]; $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray(); $cusids = array_unique(array_column($list,'customer_id')); $cuslist = Customer::with(['employee'=>function($query){ $query->field('id,name'); }])->where('id','in',$cusids)->where('employee_id','=',$param['employee_id'])->field('id,name,phone,state,addtime,community_name,employee_id,last_contact_date,level,sex,square,updatetime')->page($param['page'],$param['limit'])->select()->toArray(); $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find(); if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach($cuslist as $key=>$val){ $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']]; } $cusdata = Customer::where([['employee_id','=',$param['employee_id']]])->field('id,addtime')->select()->toArray(); $shopids = CustomerVisitLog::where([['customer_id', 'in', $cusids], ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]])->group('customer_id')->column('customer_id'); $depids = CustomerVisitLog::where([['customer_id', 'in', $cusids], ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]])->group('customer_id')->column('customer_id'); $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0; $signnum = count($list); $signrate = !empty($cusdata) && !empty($list) ? round(count($list) / count($cusdata), 2) * 100 : 0; $signagv = !empty($list) ? round(array_sum(array_column($list, 'money')) / count($list), 2) : 0; $signmoney = !empty($list) ? array_sum(array_column($list, 'money')) : 0; $depositrate = !empty($list) && !empty($depids) ? round(count($depids) / count($list), 2) * 100 : 0; $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids) / count($list), 2) * 100 : 0; $empdata = ['empname'=>$employeedata['name'],'employee_count'=>1,'shoprate'=>$shoprate,'depositrate'=>$depositrate,'signmoney'=>$signmoney,'signnum'=>$signnum,'signagv'=>$signagv,'signrate'=>$signrate]; $data = ['empdata'=>$empdata,'list'=>$cuslist]; return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]); } /** * 交定(签单)统计首页 */ public function deposit_index() { $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time']); //员工数量 $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id']; $org_name = Org::where('id',$org_id)->value('name'); $sub_org = orgSubIds($org_id); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); //$cusdata = Customer::where([['addtime','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray(); $cusdata = Customer::where([['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray(); //$where[] = ['org_id','in',$sub_org]; $where[] = ['employee_id','in',$sub_employee_ids]; $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')]; $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']]; $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray(); $depids = array_unique(array_column($list,'customer_id')); $deppacnum = $demand = $depmoney = $depnum = $agvcycle = $one_agvcycle = $two_agvcycle = $three_agvcycle = $deprate = $oneshop_depnum = $twoshop_depnum = $threeshop_depnum = $oneshop_deprate = $twoshop_deprate = $threeshop_deprate = $oneshop_depagv = $twoshop_depagv = $threeshop_depagv = 0; $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = []; foreach($list as $key=>$val){ if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date']; if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id']; } $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray(); $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray(); $yddvlg = CustomerVisitLog::where([['customer_id','in',$yjdids],['state','in',CustomerVisitLog::changeState('已到店','chaos')],['org_id','in',$sub_org]])->field('id,addtime,customer_id,state')->select()->toArray(); foreach($yddvlg as $key=>$val){ $yddarr[$val['customer_id']][]=$val; } foreach($yddarr as $key=>$val){ if(empty($val)) continue; if(count($val)==1){ $oneshop_depnum++; $oneshop_ids[] = $key; } if(count($val)==2){ $twoshop_depnum++; $twoshop_ids[] = $key; } if(count($val)>=3){ $threeshop_depnum++; $threeshop_ids[] = $key; } } foreach($yjdcus as $key=>$val){ if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($val['package_id'])){ $allpid = explode(',',$val['package_id']); foreach($allpid as $k=>$v){ $alljdpid[]=$v; } } } foreach($package as $key=>$val){ $package[$key]['num'] = 0; foreach($alljdpid as $k=>$v){ if($val['id']==$v){ $package[$key]['num']++; } } } $depnum = count($list); $deprate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0; $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0; $depmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0; //$deppacnum = count($alljdpid); $oneshop_depnum = $oneshop_depnum; $twoshop_depnum = $twoshop_depnum; $threeshop_depnum = $threeshop_depnum; $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0; $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0; $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0; $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0; $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0; $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0; $package_depnum = !empty($package) ? array_sum(array_column($package,'num')) : 0; $data = ['org_name'=>$org_name,'package'=>$package,'package_depnum'=>$package_depnum,'depnum'=>$depnum,'deprate'=>$deprate,'agvcycle'=>$agvcycle,'depmoney'=>$depmoney,'deppacnum'=>$deppacnum,'oneshop_depnum'=>$oneshop_depnum,'twoshop_depnum'=>$twoshop_depnum,'threeshop_depnum'=>$threeshop_depnum,'oneshop_deprate'=>round($oneshop_deprate,2),'twoshop_deprate'=>round($twoshop_deprate,2),'threeshop_deprate'=>round($threeshop_deprate,2),'one_agvcycle'=>$one_agvcycle,'two_agvcycle'=>$two_agvcycle,'three_agvcycle'=>$three_agvcycle]; return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]); } /** * 签订部门数据 */ public function deposit_org() { $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time','order']); $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id']; // 所选部门 $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray(); foreach ($sub_org as $k => $v){ // 是否有子部门 $have_sub_org = Org::where('pid', '=', $v['id'])->count(); if ($have_sub_org > 0){ $sub_org[$k]['have_sub_org'] = true; } else { $sub_org[$k]['have_sub_org'] = false; } unset($where); $v_sub_org = orgSubIds($v['id']); $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $sub_org[$k]['employee_count'] = count($org_employee_ids); $cusdata = Customer::where([['sign_time','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','in',$org_employee_ids]])->field('id,addtime')->select()->toArray(); //$where[] = ['org_id','in',$v_sub_org]; $where[] = ['employee_id','in',$org_employee_ids]; $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')]; $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']]; $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray(); $depids = array_unique(array_column($list,'customer_id')); $deppacnum = $demand = $depmoney = $depnum = $agvcycle = $one_agvcycle = $two_agvcycle = $three_agvcycle = $deprate = $oneshop_depnum = $twoshop_depnum = $threeshop_depnum = $oneshop_deprate = $twoshop_deprate = $threeshop_deprate = $oneshop_depagv = $twoshop_depagv = $threeshop_depagv = 0; $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = []; foreach($list as $key=>$val){ if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date']; if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id']; } $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray(); $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray(); $yddvlg = CustomerVisitLog::where([['customer_id','in',$yjdids],['state','in',CustomerVisitLog::changeState('已到店','chaos')],['org_id','in',$v_sub_org]])->field('id,addtime,customer_id,state')->select()->toArray(); foreach($yddvlg as $key=>$val){ $yddarr[$val['customer_id']][]=$val; } foreach($yddarr as $key=>$val){ if(empty($val)) continue; if(count($val)==1){ $oneshop_depnum++; $oneshop_ids[] = $key; } if(count($val)==2){ $twoshop_depnum++; $twoshop_ids[] = $key; } if(count($val)>=3){ $threeshop_depnum++; $threeshop_ids[] = $key; } } foreach($yjdcus as $key=>$val){ if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($val['package_id'])){ $allpid = explode(',',$val['package_id']); foreach($allpid as $m=>$p){ $alljdpid[]=$p; } } } foreach($package as $key=>$val){ $package[$key]['num'] = 0; foreach($alljdpid as $m=>$p){ //if(in_array($val['id'],$alljdpid)){ if($val['id']==$p){ $package[$key]['num']++; } } } $sub_org[$k]['depnum'] = count($list); $sub_org[$k]['deprate'] = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0; $sub_org[$k]['agvcycle'] = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0; $sub_org[$k]['depmoney'] = !empty($list) ? array_sum(array_column($list,'money')) : 0; $sub_org[$k]['deppacnum'] = count($alljdpid); $sub_org[$k]['oneshop_depnum'] = $oneshop_depnum; $sub_org[$k]['twoshop_depnum'] = $twoshop_depnum; $sub_org[$k]['threeshop_depnum'] = $threeshop_depnum; $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0; $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0; $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0; $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0; $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0; $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0; $sub_org[$k]['oneshop_deprate'] = round($oneshop_deprate,2); $sub_org[$k]['twoshop_deprate'] = round($twoshop_deprate,2); $sub_org[$k]['threeshop_deprate'] = round($threeshop_deprate,2); $sub_org[$k]['one_agvcycle'] = $one_agvcycle; $sub_org[$k]['two_agvcycle'] = $two_agvcycle; $sub_org[$k]['three_agvcycle'] = $three_agvcycle; $sub_org[$k]['package'] = $package; $sub_org[$k]['package_depnum'] = !empty($package) ? array_sum(array_column($package,'num')) : 0; } if (!empty($param['order'])) { $order_arr = explode(' ', $param['order']); $sort_field = $order_arr[0] ?? 'depnum'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'depnum': $field = 'depnum'; break; case 'package_depnum': $field = 'package_depnum'; break; case 'depmoney': $field = 'depmoney'; break; default: $field = 'notFound'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($sub_org as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $sub_org); } } return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]); } /** * 签订员工数据 */ public function deposit_employee() { $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]); // 所选部门 $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id']; // 所选部门员工统计数据 $e_where[] = ['org_id', '=', $org_id]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_list = Employee::with(['user'=> function($query){ $query->field('id,headimgurl'); }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray(); foreach($employee_list as $k=>$v){ unset($where); $cusdata = Customer::where([['sign_time','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','=',$v['id']]])->field('id,sign_time')->select()->toArray(); $where[] = ['employee_id','=',$v['id']]; //$where[] = ['customer_id','in',$cusids]; $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')]; $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']]; $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray(); $signids = array_unique(array_column($list,'customer_id')); $deppacnum = $demand = $depmoney = $depnum = $agvcycle = $one_agvcycle = $two_agvcycle = $three_agvcycle = $deprate = $oneshop_depnum = $twoshop_depnum = $threeshop_depnum = $oneshop_deprate = $twoshop_deprate = $threeshop_deprate = $oneshop_depagv = $twoshop_depagv = $threeshop_depagv = 0; $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = []; foreach($list as $key=>$val){ if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date']; if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id']; } $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray(); $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray(); $yddvlg = CustomerVisitLog::where([['customer_id','in',$yjdids],['state','in',CustomerVisitLog::changeState('已到店','chaos')],['employee_id','=',$v['id']]])->field('id,addtime,customer_id,state')->select()->toArray(); foreach($yddvlg as $key=>$val){ $yddarr[$val['customer_id']][]=$val; } foreach($yddarr as $key=>$val){ if(empty($val)) continue; if(count($val)==1){ $oneshop_depnum++; $oneshop_ids[] = $key; } if(count($val)==2){ $twoshop_depnum++; $twoshop_ids[] = $key; } if(count($val)>=3){ $threeshop_depnum++; $threeshop_ids[] = $key; } } foreach($yjdcus as $key=>$val){ if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if(!empty($val['package_id'])){ $allpid = explode(',',$val['package_id']); foreach($allpid as $m=>$p){ $alljdpid[]=$p; } } } foreach($package as $key=>$val){ $package[$key]['num'] = 0; foreach($alljdpid as $m=>$p){ //if(in_array($val['id'],$alljdpid)){ if($val['id']==$p){ $package[$key]['num']++; } } } $employee_list[$k]['depnum'] = count($list); $employee_list[$k]['deprate'] = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0; $employee_list[$k]['agvcycle'] = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0; $employee_list[$k]['depmoney'] = !empty($list) ? array_sum(array_column($list,'money')) : 0; $employee_list[$k]['deppacnum'] = count($alljdpid); $employee_list[$k]['oneshop_depnum'] = $oneshop_depnum; $employee_list[$k]['twoshop_depnum'] = $twoshop_depnum; $employee_list[$k]['threeshop_depnum'] = $threeshop_depnum; $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0; $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0; $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0; $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0; $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0; $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0; $employee_list[$k]['oneshop_deprate'] = round($oneshop_deprate,2); $employee_list[$k]['twoshop_deprate'] = round($twoshop_deprate,2); $employee_list[$k]['threeshop_deprate'] = round($threeshop_deprate,2); $employee_list[$k]['one_agvcycle'] = round($one_agvcycle,2); $employee_list[$k]['two_agvcycle'] = round($two_agvcycle,2); $employee_list[$k]['three_agvcycle'] = round($three_agvcycle,2); $employee_list[$k]['package'] = $package; $employee_list[$k]['package_depnum'] = !empty($package) ? array_sum(array_column($package,'num')) : 0; } if (!empty($param['order'])) { $order_arr = explode(' ', $param['order']); $sort_field = $order_arr[0] ?? 'depnum'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'depnum': $field = 'depnum'; break; case 'package_depnum': $field = 'package_depnum'; break; case 'depmoney': $field = 'depmoney'; break; default: $field = 'notFound'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($employee_list as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $employee_list); } } $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']); return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]); } /** * 签订员工客户列表 */ public function depsignmea_empcus() { $token = $this->request->token; $param = $this->request->only(['employee_id', 'start_time','end_time','status','page'=>1,'limit'=>10]); //if(empty($param['state']) || !in_array($param['state'],['已量房','已交定','已签单'])) return json(['code'=> 1, 'msg'=> 'state参数错误']); $where[] = ['employee_id','=',$param['employee_id']]; $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')]; $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']]; $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray(); $deppacnum = $demand = $depmoney = $depnum = $agvcycle = $one_agvcycle = $two_agvcycle = $three_agvcycle = $deprate = $oneshop_depnum = $twoshop_depnum = $threeshop_depnum = $oneshop_deprate = $twoshop_deprate = $threeshop_deprate = $oneshop_depagv = $twoshop_depagv = $threeshop_depagv = 0; $youjdpid = $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = []; foreach ($list as $key => $val) { if (!in_array($val['customer_id'], array_keys($deptime))) $deptime[$val['customer_id']] = $val['confirm_date']; if (!in_array($val['customer_id'], $yjdids)) $yjdids[] = $val['customer_id']; } $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray(); $yjdcus = Customer::where('id', 'in', $yjdids)->field('id,sign_time,package_id')->select()->toArray(); $yddvlg = CustomerVisitLog::where([['customer_id', 'in', $yjdids], ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')], ['employee_id', '=', $param['employee_id']]])->field('id,addtime,customer_id,state')->select()->toArray(); foreach ($yddvlg as $key => $val) { $yddarr[$val['customer_id']][] = $val; } foreach ($yddarr as $key => $val) { if (empty($val)) continue; if (count($val) == 1) { $oneshop_depnum++; $oneshop_ids[] = $key; } if (count($val) == 2) { $twoshop_depnum++; $twoshop_ids[] = $key; } if (count($val) >= 3) { $threeshop_depnum++; $threeshop_ids[] = $key; } } foreach ($yjdcus as $key => $val) { if (!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if (!empty($deptime[$val['id']]) && in_array($val['id'], $oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if (!empty($deptime[$val['id']]) && in_array($val['id'], $twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if (!empty($deptime[$val['id']]) && in_array($val['id'], $threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']); if (!empty($val['package_id'])) { $allpid = explode(',', $val['package_id']); foreach ($allpid as $m => $p) { $alljdpid[] = $p; } $youjdpid[] = $val['id']; } } foreach ($package as $key => $val) { $package[$key]['num'] = 0; foreach ($alljdpid as $m => $p) { //if(in_array($val['id'],$alljdpid)){ if ($val['id'] == $p) { $package[$key]['num']++; } } } $cusids = !empty($list) ? array_unique(array_column($list,'customer_id')) : []; if(!empty($param['status'])){ if($param['status']==1) $cusids = $oneshop_ids; if($param['status']==2) $cusids = $twoshop_ids; if($param['status']==3) $cusids = $threeshop_ids; if($param['status']==4) $cusids = $youjdpid; } $cuslist = Customer::with(['employee'=>function($query){ $query->field('id,name'); }])->where('id','in',$cusids)->where('employee_id','=',$param['employee_id'])->field('id,name,phone,state,addtime,community_name,employee_id,last_contact_date,level,sex,square,updatetime')->page($param['page'],$param['limit'])->select()->toArray(); $cusdata = Customer::where([['sign_time','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','=',$param['employee_id']]])->field('id,addtime')->select()->toArray(); $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find(); if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach($cuslist as $key=>$val){ $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']]; } $depnum = count($list); $deprate = !empty($cusdata) && !empty($list) ? sprintf("%.2f",count($list) / count($cusdata)) * 100 : 0; $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime) / count($agvtime)) / 86400) : 0; $depmoney = !empty($list) ? array_sum(array_column($list, 'money')) : 0; $deppacnum = count($alljdpid); $oneshop_depnum = $oneshop_depnum; $twoshop_depnum = $twoshop_depnum; $threeshop_depnum = $threeshop_depnum; $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$oneshop_depnum / count($cusdata)) * 100 : 0; $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$twoshop_depnum / count($cusdata)) * 100 : 0; $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$threeshop_depnum / count($cusdata)) * 100 : 0; $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime) / count($one_agvtime)) / 86400) : 0; $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime) / count($two_agvtime)) / 86400) : 0; $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime) / count($three_agvtime)) / 86400) : 0; $package = $package; $package_depnum = !empty($package) ? array_sum(array_column($package, 'num')) : 0; $empdata = ['empname'=>$employeedata['name'],'depnum'=>$depnum,'deprate'=>round($deprate,2),'agvcycle'=>$agvcycle,'depmoney'=>$depmoney,'deppacnum'=>$deppacnum,'oneshop_depnum'=>$oneshop_depnum,'twoshop_depnum'=>$twoshop_depnum,'threeshop_depnum'=>$threeshop_depnum,'oneshop_deprate'=>round($oneshop_deprate,2),'twoshop_deprate'=>round($twoshop_deprate,2),'threeshop_deprate'=>round($threeshop_deprate,2),'one_agvcycle'=>$one_agvcycle,'two_agvcycle'=>$two_agvcycle,'three_agvcycle'=>$three_agvcycle,'package'=>$package,'package_depnum'=>$package_depnum]; $data = ['empdata'=>$empdata,'list'=>$cuslist]; return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]); } /** * 打电话统计(全部) */ public function call(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 数据总览 // 部门信息 $data['org_name'] = Org::where('id', '=', $org_id)->value('name'); //员工数量 $sub_org = orgSubIds($org_id); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $data['employee_count'] = count($sub_employee_ids); $where[] = ['employee_id', 'in', $sub_employee_ids]; $where[] = ['addtime', 'between', [$start_time, $end_time]]; $list = OutCallLog::where($where)->column('id,status,billsec'); $count = count($list); $data['count'] = $count; $ok = 0; // 接通数量 $long = 0; // 通话时长 s foreach ($list as $k => $v) { if ($v['status'] == 1){ $ok ++; } $long += $v['billsec']; } $data['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : $ok * 100; $data['long'] = round($long / 60, 2); $data['average_long'] = $count > 0 ? round($long / 60 / $count, 2) : round($long / 60, 2); // 手机打电话次数 $mbCount = OutCallMbLog::where($where)->count(); $data['count'] += $mbCount; return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']); } /** * 打电话统计(部门) */ public function callOrg(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->column('id,name,pid'); foreach ($sub_org as $k => $v){ // 是否有子部门 $have_sub_org = Org::where('pid', '=', $v['id'])->count(); if ($have_sub_org > 0){ $sub_org[$k]['have_sub_org'] = true; } else { $sub_org[$k]['have_sub_org'] = false; } $v_sub_org = orgSubIds($v['id']); $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $sub_org[$k]['employee_count'] = count($org_employee_ids); unset($where); $where[] = ['employee_id', 'in', $org_employee_ids]; $where[] = ['addtime', 'between', [$start_time, $end_time]]; $list = OutCallLog::where($where)->column('id,status,billsec'); $count = count($list); $sub_org[$k]['count'] = $count; $ok = 0; // 接通数量 $long = 0; // 通话时长 s foreach ($list as $kk => $vv) { if ($vv['status'] == 1){ $ok ++; } $long += $vv['billsec']; } $sub_org[$k]['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : 0; $m_long = round($long / 60, 2); $sub_org[$k]['long'] = $m_long; $sub_org[$k]['average_long'] = $count > 0 ? round($long / 60 / $count, 2) : $m_long; // 手机打电话次数 $mbCount = OutCallMbLog::where($where)->count(); $sub_org[$k]['count'] += $mbCount; } $order = input('order', '', 'trim'); if (!empty($order)) { $order_arr = explode(' ', $order); $sort_field = $order_arr[0] ?? 'count'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'count': $field = 'count'; break; default: $field = 'notFound'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($sub_org as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $sub_org); } } return json(['code'=> self::success, 'data'=> $sub_org, 'msg'=> '请求成功']); } /** * 打电话统计(员工) */ public function callEmployee(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所选部门员工统计数据 $e_where[] = ['org_id', '=', $org_id]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_list = Employee::with(['user'=> function($query){ $query->field('id,headimgurl'); }])->where($e_where)->column('id,name,org_id,root_id,uid'); $employee_ids = array_column($employee_list, 'id'); $where[] = ['employee_id', 'in', $employee_ids]; $where[] = ['addtime', 'between', [$start_time, $end_time]]; $list = OutCallLog::where($where)->column('id,employee_id,status,billsec'); $employee_data = []; foreach ($list as $k => $v) { if (isset($employee_data[$v['employee_id']])) { $employee_data[$v['employee_id']][] = $v; } else { $employee_data[$v['employee_id']] = []; $employee_data[$v['employee_id']][] = $v; } } // 手机打电话次数 $mbCount = OutCallMbLog::where($where)->column('id,employee_id'); $mbCount_data = []; foreach ($mbCount as $k => $v) { if (isset($mbCount_data[$v['employee_id']])) { $mbCount_data[$v['employee_id']][] = $v; } else { $mbCount_data[$v['employee_id']] = []; $mbCount_data[$v['employee_id']][] = $v; } } foreach ($employee_list as $k => $v){ $count = 0; $ok = 0; // 接通数量 $long = 0; // 通话时长 s if (isset($employee_data[$v['id']])) { $count = count($employee_data[$v['id']]); foreach ($employee_data[$v['id']] as $kk => $vv) { if ($vv['status'] == 1){ $ok ++; } $long += $vv['billsec']; } } $employee_list[$k]['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : 0; $m_long = round($long / 60, 2); $employee_list[$k]['long'] = $m_long; $employee_list[$k]['average_long'] = $count > 0 ? round($m_long / $count, 2) : $m_long; $cmb_count = 0; if (isset($mbCount_data[$v['id']])) { $cmb_count = count($mbCount_data[$v['id']]); } $employee_list[$k]['count'] = $count + $cmb_count; } $order = input('order', '', 'trim'); if (!empty($order)) { $order_arr = explode(' ', $order); $sort_field = $order_arr[0] ?? 'count'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'count': $field = 'count'; break; default: $field = 'notFound'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($employee_list as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $employee_list); } } $page = input('page', 1, 'intval'); $limit = input('limit', 10, 'intval'); $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit); return json(['code'=> self::success, 'data'=> $employee_list, 'msg'=> '请求成功']); } /** * 打电话统计(客户列表) */ public function callCustomer(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $employee_id = input('employee_id', '', 'intval'); $sub_org = orgSubIds($token['org_id']); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); if (!in_array($employee_id, $sub_employee_ids)){ return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']); } $where[] = ['employee_id', '=', $employee_id]; $where[] = ['addtime', 'between', [$start_time, $end_time]]; $list = OutCallLog::where($where)->select()->toArray(); $customer_id = array_column($list, 'customer_id'); $employee_data['count'] = count($list); $ok = 0; // 接通数量 $long = 0; // 通话时长 s foreach ($list as $kk => $vv) { if ($vv['status'] == 1){ $ok ++; } $long += $vv['billsec']; } $employee_data['lv'] = count($list) > 0 ? round($ok * 100 / count($list), 2) : 0; $m_long = round($long / 60, 2); $employee_data['long'] = $m_long; $employee_data['average_long'] = count($list) > 0 ? round($m_long / count($list), 2) : $m_long; $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name'); // 手机打电话次数 $mbCount = OutCallMbLog::where($where)->count(); $employee_data['count'] += $mbCount; $data['employee'] = $employee_data; $page = input('page', 1, 'intval'); $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select(); $customers = $customers->visible(['id', 'employee_id', 'designer_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'employee.name', 'agent.agent_name'])->toArray(); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach ($customers as $k => $v) { $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; } $data['customer'] = $customers; return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']); } /** * 活动列表 */ public function activity(){ $token = request()->token; $list = Activity::where([['root_id', '=', $token['root_org']], ['cate', '=', 1]])->order('addtime desc')->field('id,title')->select()->toArray(); return json(['code'=> self::success, 'data'=> $list]); } /** * 预约活动(主页) */ public function subscribeActivity(){ $token = $this->request->token; $id = input('id', '', 'intval'); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 数据总览 // 部门信息 $data['org_name'] = Org::where('id', '=', $org_id)->value('name'); //员工数量 $sub_org = orgSubIds($org_id); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $data['employee_count'] = count($sub_employee_ids); $sub_where[] = ['employee_id', 'in', $sub_employee_ids]; $sub_where[] = ['aid', '=', $id]; $sub_where[] = ['type', '=', 2]; $sub_where[] = ['state', 'in', [0,1]]; $sub_where[] = ['subscribe_date', '>=', $start_time]; $sub_where[] = ['subscribe_date', '<', $end_time]; $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id'); $sub_count = count($sub_customer); $data['subscribe_count'] = $sub_count; $dc_where[] = ['customer_employee_id', 'in', $sub_employee_ids]; $dc_where[] = ['aid', '=', $id]; $dc_where[] = ['confirm_date', '>=', $start_time]; $dc_where[] = ['confirm_date', '<', $end_time]; $dc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $dc_where[] = ['state', 'in', $dc_state]; $daochang = CustomerVisitLog::where($dc_where)->count(); $data['daochang_count'] = $daochang; $data['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ; $jd_where[] = ['customer_id', 'in', $sub_customer]; $jd_where[] = ['confirm_date', '>=', $start_time]; $jd_where[] = ['confirm_date', '<', $end_time]; $jd_state = CustomerVisitLog::changeState('已交定', 'chaos'); $jd_where[] = ['state', 'in', $jd_state]; $jiaoding = CustomerVisitLog::where($jd_where)->count(); $data['jiaoding_count'] = $jiaoding; $data['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ; $zd_where[] = ['customer_id', 'in', $sub_customer]; $zd_where[] = ['confirm_date', '>=', $start_time]; $zd_where[] = ['confirm_date', '<', $end_time]; $zd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $zd_where[] = ['state', 'in', $zd_state]; $zhuandan = CustomerVisitLog::where($zd_where)->count(); $data['zhuandan_count'] = $zhuandan; $data['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ; return json(['code'=> self::success, 'data'=> $data]); } /** * 预约活动(部门) */ public function subscribeActivityOrg(){ $token = $this->request->token; $id = input('id', '', 'intval'); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray(); foreach ($sub_org as $k => $v) { // 是否有子部门 $have_sub_org = Org::where('pid', '=', $v['id'])->count(); if ($have_sub_org > 0) { $sub_org[$k]['have_sub_org'] = true; } else { $sub_org[$k]['have_sub_org'] = false; } $v_sub_org = orgSubIds($v['id']); $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $sub_org[$k]['employee_count'] = count($org_employee_ids); unset($sub_where); $sub_where[] = ['employee_id', 'in', $org_employee_ids]; $sub_where[] = ['aid', '=', $id]; $sub_where[] = ['type', '=', 2]; $sub_where[] = ['state', 'in', [0,1]]; $sub_where[] = ['subscribe_date', '>=', $start_time]; $sub_where[] = ['subscribe_date', '<', $end_time]; $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id'); $sub_count = count($sub_customer); $sub_org[$k]['subscribe_count'] = $sub_count; unset($dc_where); $dc_where[] = ['customer_employee_id', 'in', $org_employee_ids]; $dc_where[] = ['aid', '=', $id]; $dc_where[] = ['confirm_date', '>=', $end_time]; $dc_where[] = ['confirm_date', '<', $end_time]; $dc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $dc_where[] = ['state', 'in', $dc_state]; $daochang = CustomerVisitLog::where($dc_where)->count(); $sub_org[$k]['daochang_count'] = $daochang; $sub_org[$k]['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ; unset($jd_where); $jd_where[] = ['customer_id', 'in', $sub_customer]; $jd_where[] = ['confirm_date', '>=', $start_time]; $jd_where[] = ['confirm_date', '<', $end_time]; $jd_state = CustomerVisitLog::changeState('已交定', 'chaos'); $jd_where[] = ['state', 'in', $jd_state]; $jiaoding = CustomerVisitLog::where($jd_where)->count(); $sub_org[$k]['jiaoding_count'] = $jiaoding; $sub_org[$k]['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ; unset($zd_where); $zd_where[] = ['customer_id', 'in', $sub_customer]; $zd_where[] = ['confirm_date', '>=', $start_time]; $zd_where[] = ['confirm_date', '<', $end_time]; $zd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $zd_where[] = ['state', 'in', $zd_state]; $zhuandan = CustomerVisitLog::where($zd_where)->count(); $sub_org[$k]['zhuandan_count'] = $zhuandan; $sub_org[$k]['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ; } $order = input('order', '', 'trim'); if ($order) { $order_arr = explode(' ', $order); $sort_field = $order_arr[0] ?? 'yuyue'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'yuyue': $field = 'subscribe_count'; break; case 'daochang': $field = 'daochang_count'; break; case 'qiandan': $field = 'jiaoding_count'; break; case 'zhuandan': $field = 'zhuandan_count'; break; default: $field = 'notFound'; break; } if ($sort_order == 'asc'){ $sort = SORT_ASC; } else { $sort = SORT_DESC; } $arr = []; foreach ($sub_org as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $sub_org); } } $page = input('page', 1, 'intval'); $limit = input('limit', 10, 'intval'); $list = array_slice($sub_org, ($page-1)*$limit, $page*$limit-1); return json(['code'=> self::success, 'data'=> $list]); } /** * 预约活动(员工) */ public function subscribeActivityEmployee(){ $token = $this->request->token; $id = input('id', '', 'intval'); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所选部门员工统计数据 $e_where[] = ['org_id', '=', $org_id]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_list = Employee::with(['user'=> function($query){ $query->field('id,headimgurl'); }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray(); foreach ($employee_list as $k => $v) { unset($sub_where); $sub_where[] = ['employee_id', '=', $v['id']]; $sub_where[] = ['aid', '=', $id]; $sub_where[] = ['type', '=', 2]; $sub_where[] = ['state', 'in', [0,1]]; $sub_where[] = ['subscribe_date', '>=', $start_time]; $sub_where[] = ['subscribe_date', '<', $end_time]; $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id'); $sub_count = count($sub_customer); $employee_list[$k]['subscribe_count'] = $sub_count; unset($dc_where); $dc_where[] = ['customer_employee_id', '=', $v['id']]; $dc_where[] = ['aid', '=', $id]; $dc_where[] = ['confirm_date', '>=', $start_time]; $dc_where[] = ['confirm_date', '<', $end_time]; $dc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $dc_where[] = ['state', 'in', $dc_state]; $daochang = CustomerVisitLog::where($dc_where)->count(); $employee_list[$k]['daochang_count'] = $daochang; $employee_list[$k]['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ; unset($jd_where); $jd_where[] = ['customer_id', 'in', $sub_customer]; $jd_where[] = ['customer_employee_id', '=', $v['id']]; $jd_where[] = ['confirm_date', '>=', $start_time]; $jd_where[] = ['confirm_date', '<', $end_time]; $jd_state = CustomerVisitLog::changeState('已交定', 'chaos'); $jd_where[] = ['state', 'in', $jd_state]; $jiaoding = CustomerVisitLog::where($jd_where)->count(); $employee_list[$k]['jiaoding_count'] = $jiaoding; $employee_list[$k]['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ; unset($zd_where); $zd_where[] = ['customer_id', 'in', $sub_customer]; $zd_where[] = ['customer_employee_id', '=', $v['id']]; $zd_where[] = ['confirm_date', '>=', $start_time]; $zd_where[] = ['confirm_date', '<', $end_time]; $zd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $zd_where[] = ['state', 'in', $zd_state]; $zhuandan = CustomerVisitLog::where($zd_where)->count(); $employee_list[$k]['zhuandan_count'] = $zhuandan; $employee_list[$k]['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ; } $order = input('order', '', 'trim'); if ($order) { $order_arr = explode(' ', $order); $sort_field = $order_arr[0] ?? 'yuyue'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'yuyue': $field = 'subscribe_count'; break; case 'daochang': $field = 'daochang_count'; break; case 'qiandan': $field = 'jiaoding_count'; break; case 'zhuandan': $field = 'zhuandan_count'; break; default: $field = 'notFound'; break; } if ($sort_order == 'asc'){ $sort = SORT_ASC; } else { $sort = SORT_DESC; } $arr = []; foreach ($employee_list as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $employee_list); } } $page = input('page', 1, 'intval'); $limit = input('limit', 10, 'intval'); $list = array_slice($employee_list, ($page-1)*$limit, $limit); return json(['code'=> self::success, 'data'=> $list]); } /** * 预约活动(客户) */ public function subscribeActivityCustomer(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $employee_id = input('employee_id', '', 'intval'); $sub_org = orgSubIds($token['org_id']); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); if (!in_array($employee_id, $sub_employee_ids)){ return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']); } $aid = input('id', '', 'intval'); $sub_where[] = ['employee_id', '=', $employee_id]; $sub_where[] = ['aid', '=', $aid]; $sub_where[] = ['type', '=', 2]; $sub_where[] = ['state', 'in', [0,1]]; $sub_where[] = ['subscribe_date', '>=', $start_time]; $sub_where[] = ['subscribe_date', '<', $end_time]; $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id'); $sub_count = count($sub_customer); $employee_data['subscribe_count'] = $sub_count; $dc_where[] = ['customer_employee_id', '=', $employee_id]; $dc_where[] = ['aid', '=', $aid]; $dc_where[] = ['confirm_date', '>=', $start_time]; $dc_where[] = ['confirm_date', '<', $end_time]; $dc_state = CustomerVisitLog::changeState('已到场', 'chaos'); $dc_where[] = ['state', 'in', $dc_state]; $daochang_ids = CustomerVisitLog::where($dc_where)->column('customer_id'); $employee_data['daochang_count'] = count($daochang_ids); $employee_data['daochang_lv'] = $sub_count > 0 ? round(count($daochang_ids) * 100 / $sub_count, 2) : 0 ; $jd_where[] = ['customer_id', 'in', $sub_customer]; $jd_where[] = ['customer_employee_id', '=', $employee_id]; $jd_where[] = ['confirm_date', '>=', $start_time]; $jd_where[] = ['confirm_date', '<', $end_time]; $jd_state = CustomerVisitLog::changeState('已交定', 'chaos'); $jd_where[] = ['state', 'in', $jd_state]; $jiaoding = CustomerVisitLog::where($jd_where)->count(); $employee_data['jiaoding_count'] = $jiaoding; $employee_data['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ; $zd_where[] = ['customer_id', 'in', $sub_customer]; $zd_where[] = ['customer_employee_id', '=', $employee_id]; $zd_where[] = ['confirm_date', '>=', $start_time]; $zd_where[] = ['confirm_date', '<', $end_time]; $zd_state = CustomerVisitLog::changeState('已签单', 'chaos'); $zd_where[] = ['state', 'in', $zd_state]; $zhuandan = CustomerVisitLog::where($zd_where)->count(); $employee_data['zhuandan_count'] = $zhuandan; $employee_data['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ; $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name'); /* $where[] = ['employee_id', '=', $employee_id]; $where[] = ['aid', '=', $aid]; $where[] = ['type', '=', 2]; $where[] = ['state', 'in', [0,1]]; $where[] = ['subscribe_date', '>=', $start_time]; $where[] = ['subscribe_date', '<', $end_time]; $customer_id = CustomersSubscribe::where($where)->column('customer_id'); */ // 直接到场的客户 $customer_id = array_values(array_unique(array_merge($daochang_ids, $sub_customer))); $type = input('type', 'subscribe', 'trim'); switch ($type){ case 'subscribe': break; case 'daochang': $state = CustomerVisitLog::changeState('已到场', 'chaos'); $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id'); break; case 'qiandan': $state = CustomerVisitLog::changeState('已交定', 'chaos'); $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id'); break; case 'zhuandan': $state = CustomerVisitLog::changeState('已签单', 'chaos'); $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id'); break; default: break; } $page = input('page', 1, 'intval'); $c_where[] = ['id', 'in', $customer_id]; $c_where[] = ['employee_id', '=', $employee_id]; $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where($c_where)->page($page, 10)->select(); $customers = $customers->visible(['id', 'employee_id', 'designer_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'employee.name', 'agent.agent_name'])->toArray(); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach ($customers as $k => $v) { $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; } $data['employee'] = $employee_data; $data['customer'] = $customers; return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']); } /** * 客户分配数 */ public function customerDistribution() { $token = $this->request->token; // $token['org_id'] = 43; // $token['employee_id'] = 125; // $token['root_org'] = 23; $date = date('Y-m-d'); $param = Request::only(['start_time' => $date, 'end_time' => $date,'employee_id'=>0,'org_id'=>0]); $param['start_time'] = date('Y-m-d',strtotime($param['start_time'])); $param['end_time'] = date('Y-m-d',strtotime($param['end_time'])); $where[] = ['sign_time', 'between', [$param['start_time'].' 00:00:00', $param['end_time'] . ' 23:59:59']]; $where[] = ['employee_id','>',0]; if ($param['employee_id']) { $where[] = ['employee_id','=',$param['employee_id']]; }else{ $org_id = $param['org_id'] ?: $token['org_id']; $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path'); if(empty($path)) return json(['code' => 0, 'data' => [],'title'=>[]]); $org_ids = Org::where([['path','like',$path.'%']])->column('id'); $where[] = ['org_id','in',$org_ids]; } $wuxiao = Customer::changeState('无效', 'chaos'); $not_sure = Customer::changeState('待确认', 'chaos'); $where[] = ['state', 'not in', array_merge($wuxiao, $not_sure)]; $where[] = ['died', '<>', 2]; //房屋状态 $pid = CustomerPortraitField::where([['keyname', '=', 'house_type'], ['root_id', '=', $token['root_org']]])->value('id'); $house_status = CustomerPortraitFieldSelect::where([['pid', '=', $pid]])->order('id asc')->column("name,id,concat('house_type','#',id) as keyname,0 as count"); $count = ['id' => 0, 'count' => 0, 'keyname' => 'count', 'name' => '有效客户数', 'key' => '客户']; $existing_homes = ['id' => 0, 'count' => 0, 'keyname' => 'existing_homes', 'name' => '现房数量占比', 'key' => '现房']; $forward_housing = ['id' => 0, 'count' => 0, 'keyname' => 'forward_housing', 'name' => '期房数量占比', 'key' => '期房']; $ershoufang = ['id' => 0, 'count' => 0, 'keyname' => 'ershoufang', 'name' => '二手房数量占比', 'key' => '二手房']; $customer = Customer::where($where)->column('house_type'); $count['count'] = count($customer); foreach ($customer as $k => $v) { switch ($v) { case '现房': $existing_homes['count'] ++; break; case '期房': $forward_housing['count'] ++; break; case '二手房': $ershoufang['count'] ++; break; default: break; } } foreach ($house_status as $k2 => $v2) { $house_status[$k2]['name'] = $v2['name'] . '数量占比'; } array_unshift($house_status, $count, $existing_homes, $forward_housing,$ershoufang); //比例 if ($count['count']) { foreach ($house_status as $k3 => $v3) { $house_status[$k3]['grawth'] = round($v3['count'] / $count['count'] * 100, 2) . '%'; } } else { foreach ($house_status as $k3 => $v3) { $house_status[$k3]['grawth'] = '0%'; } } $org_id = $param['org_id'] ?: $token['org_id']; $house_status[0]['org_name'] = Org::where('id',$org_id)->value('name'); return json(['code' => 0,'title'=>$house_status[0], 'data' => array_slice($house_status,1,3)]); } /** * 客户分配列表 */ public function customerDistributionList() { $token = $this->request->token; // $token['org_id'] = 23; // $token['employee_id'] = 0; // $token['root_org'] = 23; $date = date('Y-m-d'); $param = Request::only(['key' => '', 'page' => 1, 'limit' => 10, 'start_time' => $date, 'end_time' => $date,'employee_id'=>0,'org_id'=>0]); $param['start_time'] = date('Y-m-d',strtotime($param['start_time'])); $param['end_time'] = date('Y-m-d',strtotime($param['end_time'])); // $arr = ['现房', '期房']; $where[] = ['sign_time', 'between', [$param['start_time'].' 00:00:00', $param['end_time'] . ' 23:59:59']]; $where[] = ['employee_id','>',0]; if ($param['employee_id']) { $where[] = ['employee_id','=',$param['employee_id']]; }else{ $org_id = $param['org_id'] ?: $token['org_id']; $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path'); if(empty($path)) return json(['code' => 0, 'data' => [],'count'=>0]); $org_ids = Org::where([['path','like',$path.'%']])->column('id'); $where[] = ['org_id','in',$org_ids]; } if(empty($param['key'])) return json(['code' => 0, 'data' =>[], 'count' => []]); $where[] = ['house_type', '=', $param['key']]; $wuxiao = Customer::changeState('无效', 'chaos'); $not_sure = Customer::changeState('待确认', 'chaos'); $where[] = ['state', 'not in', array_merge($wuxiao, $not_sure)]; $where[] = ['died', '<>', 2]; $list = Customer::where($where)->page($param['page'], $param['limit'])->field('id,sex,name,phone,level,community_name,square,last_contact_date,phone1,phone2')->select()->toArray(); if($list) $list = $this->getCustomer($list); $count = Customer::where($where)->count(); return json(['code' => 0, 'data' => $list, 'count' => $count]); } /** * 客户跟进数据 */ private function getCustomer($arr){ $customerIdList = array_column($arr,'id'); //统计 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $customerIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach ($arr as $key => $item) { $arr[$key]['stateNum'] = isset($customersState[$item['id']]) ? $customersState[$item['id']] : ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; } return $arr; } /** * 加微统计 */ public function weChatStatistics() { $token = $this->request->token; $date = date('Y-m-d'); $param = Request::only(['start_time'=> $date, 'end_time'=> $date, 'employee_id'=> 0, 'org_id'=> 0, 'page'=> 0, 'limit'=> 10]); $param['start_time'] = date('Y-m-d 00:00:00', strtotime($param['start_time'])); $param['end_time'] = date('Y-m-d 23:59:59', strtotime($param['end_time'])); $where[] = ['employee_id', '>', 0]; if ($param['employee_id']) { $where[] = ['employee_id', '=', $param['employee_id']]; } else { $org_id = $param['org_id'] ? : $token['org_id']; $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path'); if(empty($path)) return json(['code' => 0, 'data' => []]); $org_ids = Org::where([['path', 'like', $path.'%']])->column('id'); $where[] = ['org_id', 'in', $org_ids]; } // 所有客户 $c_where = $where; $wuxiao = Customer::changeState('无效', 'chaos'); $c_where[] = ['sign_time', 'between', [$param['start_time'], $param['end_time']]]; $c_where[] = ['state', 'NOT NULL', null]; $c_where[] = ['state', 'not in', $wuxiao]; $c_where[] = ['died', '<>', 2]; $customer = Customer::where($c_where)->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]); })->column('id,addtime,employee_id'); $org_customer = []; $emp_customer = []; foreach ($customer as $v2) { @$org_customer[$v2['org_id']] += 1; @$emp_customer[$v2['employee_id']] += 1; } //日期范围内所有加微客户 $where[] = ['add_wechat_time', 'between', [$param['start_time'], $param['end_time']]]; $wechat = Customer::where($where)->column('id,employee_id,org_id'); $org_customer_wechat = []; $emp_customer_wechat = []; foreach ($wechat as $v3) { @$org_customer_wechat[$v3['org_id']]+=1; @$emp_customer_wechat[$v3['employee_id']]+=1; } $title['img'] = ''; $title['count'] = count($customer); $title['wechat_count'] = count($wechat); $title['grawth'] = ($title['count'] == 0 || $title['wechat_count'] == 0) ? '0%' : round($title['wechat_count'] / $title['count']*100, 2).'%'; if ($param['employee_id']) { $user = Employee::with(['user'=> function($query){ $query->field('id,headimgurl')->bind(['img'=> 'headimgurl']); }])->where('id', $param['employee_id'])->field('uid')->find(); $title['img'] = $user->img; } if ($param['employee_id']) { //客户列表 有分页 无排序 $list = array_slice($wechat, ($param['page'] - 1) * $param['limit'], $param['limit']); $list = Customer::where([['id','in',array_column($list,'id')]])->field('id,sex,name,phone,level,community_name,square,last_contact_date,phone1,phone2')->select()->toArray(); if($list) $list = $this->getCustomer($list); $count = $title['wechat_count']; return json(['code' => 0, 'data' => $list, 'count' => $count,'title'=>$title]); }else{ $sort = Request::only(['order'=>'desc','field'=>'count']); $order = $sort['order']=='desc' ? SORT_DESC : SORT_ASC; $field = $sort['field']=='count' ? 'wechat_count' : 'order'; //直属业务员,直属子部门列表 无分页 有排序 //直属业务员 列表 $emp = Employee::where([['org_id','=',$org_id],['uid','>',0],['state','=','在职']])->column('id,name,uid'); $users = User::where([['id','in',array_column($emp,'uid')]])->column('headimgurl','id'); foreach ($emp as $k5 => $v5) { $emp[$k5]['count'] = isset($emp_customer[$v5['id']]) ? $emp_customer[$v5['id']] : 0; $emp[$k5]['wechat_count'] = isset($emp_customer_wechat[$v5['id']]) ? $emp_customer_wechat[$v5['id']] : 0; $emp[$k5]['order'] = ($emp[$k5]['count']==0 || $emp[$k5]['wechat_count']==0) ? 0 : round($emp[$k5]['wechat_count']/$emp[$k5]['count']*100,2); $emp[$k5]['img'] = $users[$v5['uid']]; $emp[$k5]['grawth'] = $emp[$k5]['order'].'%'; } if($emp) array_multisort(array_column($emp, $field), $order, $emp); //部门列表 $info = Org::where('id', $org_id)->find(); //直属子部门 $orgs = Org::where([['pid', '=', $org_id]])->column('id,name,path'); //所有子部门 $s_orgs = Org::where([['path','like',$info->path.'%']])->column('id,pid,path'); $org_ids = []; foreach ($orgs as $k6 => $v6) { $orgs[$k6]['count'] = 0; $orgs[$k6]['wechat_count'] = 0; $orgs[$k6]['emp_count'] = 0; $orgs[$k6]['child_org_ids'] = []; foreach ($s_orgs as $v7) { if(stripos($v7['path'],$v6['path']) === 0){ if(isset($org_customer[$v7['id']])) $orgs[$k6]['count']+=$org_customer[$v7['id']]; if(isset($org_customer_wechat[$v7['id']])) $orgs[$k6]['wechat_count']+=$org_customer_wechat[$v7['id']]; $orgs[$k6]['child_org_ids'][] = $v7['id']; $org_ids[] = $v7['id']; } } $orgs[$k6]['order'] = ($orgs[$k6]['count']==0 || $orgs[$k6]['wechat_count']==0) ? 0 : round($orgs[$k6]['wechat_count']/$orgs[$k6]['count']*100,2); $orgs[$k6]['grawth'] = $orgs[$k6]['order'].'%'; } if($orgs) array_multisort(array_column($orgs,$field),$order, $orgs); //部门下员工数量 $emps = Employee::where([['org_id','in',$org_ids],['state','=','在职'],['uid','>',0]])->group('org_id')->column('count(id)','org_id'); foreach ($orgs as $k8 => $v8) { foreach ($v8['child_org_ids'] as $v9) { if(isset($emps[$v9])) $orgs[$k8]['emp_count']+=$emps[$v9]; } } $org_id = $param['org_id'] ?: $token['org_id']; $title['org_name'] = Org::where('id',$org_id)->value('name'); return json(['code'=> 0, 'data'=> ['emp'=> $emp, 'org'=> $orgs, 'title'=> $title]]); } } /** * 沟通跟进次数 */ public function followUpTimes() { $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 数据总览 // 部门信息 $data['org_name'] = Org::where('id', '=', $org_id)->value('name'); //员工数量 $sub_org = orgSubIds($org_id); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $data['employee_count'] = count($sub_employee_ids); // 电话沟通次数 $call_where[] = ['employee_id', 'in', $sub_employee_ids]; $call_where[] = ['addtime', 'between', [$start_time, $end_time]]; $call_where[] = ['status', '=', 1]; //电话沟通 $call_count = 0; $call_time = 0; $call_avg = 0; $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec'); if ($call_log) { $call_count = count($call_log); $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2); $call_employee = count(array_unique(array_column($call_log,'employee_id'))); $call_avg = round(array_sum(array_column($call_log,'billsec')) / 60 / $call_employee, 2); } // 手机打电话次数 $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $sub_employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count(); $call_count = $call_count + $mbCallCount; $data['call_count'] = $call_count; $data['call_time'] = $call_time; $data['call_avg'] = $call_avg; // 内容调用次数 $share_where[] = ['share_time', 'between', [$start_time, $end_time]]; $share_where[] = ['employee_id', 'in', $sub_employee_ids]; // 转发次数 $share_all = ShareLog::where($share_where)->select(); $share_count = $share_all->count(); $wechat_where[] = ['employee_id', 'in', $sub_employee_ids]; $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]]; //查询每日发圈转发次数 $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count(); $data['share_count'] = $k_share_count; // 浏览时长 $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card']; $foot_where[] = ['employee_id', 'in', $sub_employee_ids]; $foot_where[] = ['pipe_type', 'in', $clue_type]; $foot_where[] = ['addtime', 'between', [$start_time, $end_time]]; $foot_count = Footprints::where($foot_where)->count(); // 次数 $visit_long = Footprints::where($foot_where)->sum('visit_due_time'); $data['visit_long'] = round($visit_long / 60, 2); $data['visit_avg'] = $foot_count > 0 ? round($visit_long / $foot_count /60, 2) : round($visit_long / 60, 2); //微信沟通 $visit_where[] = ['employee_id', 'in', $sub_employee_ids]; $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]]; $visit_where[] = ['type', '=', 3]; $wechat_visit_count = CustomerVisitLog::where($visit_where)->count(); $data['wechat_visit_count'] = $wechat_visit_count; $all_customer_where[] = ['employee_id', 'in', $sub_employee_ids]; $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]]; $customer_count = Customer::where($all_customer_where)->count(); $data['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count; $data['all_count'] = $data['call_count'] + $k_share_count + $data['wechat_visit_count']; return json(['code'=> self::success, 'data'=> $data]); } /** * 沟通跟进次数 (部门) */ public function followUpTimesOrg() { $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray(); foreach ($sub_org as $k => $v) { // 是否有子部门 $have_sub_org = Org::where('pid', '=', $v['id'])->count(); if ($have_sub_org > 0) { $sub_org[$k]['have_sub_org'] = true; } else { $sub_org[$k]['have_sub_org'] = false; } $v_sub_org = orgSubIds($v['id']); $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $sub_org[$k]['employee_count'] = count($org_employee_ids); // 电话沟通次数 unset($call_where); $call_where[] = ['employee_id', 'in', $org_employee_ids]; $call_where[] = ['addtime', 'between', [$start_time, $end_time]]; $call_where[] = ['status', '=', 1]; //电话沟通 $call_count = 0; $call_time = 0; $call_avg = 0; $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec'); if ($call_log) { $call_count = count($call_log); $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2); $call_employee = count(array_unique(array_column($call_log,'employee_id'))); $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2); } // 手机打电话次数 $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $org_employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count(); $call_count = $call_count + $mbCallCount; $sub_org[$k]['call_count'] = $call_count; $sub_org[$k]['call_time'] = $call_time; $sub_org[$k]['call_avg'] = $call_avg; // 内容调用次数 unset($share_where); $share_where[] = ['share_time', 'between', [$start_time, $end_time]]; $share_where[] = ['employee_id', 'in', $org_employee_ids]; // 转发次数 $share_all = ShareLog::where($share_where)->select(); $share_count = $share_all->count(); unset($wechat_where); $wechat_where[] = ['employee_id', 'in', $org_employee_ids]; $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]]; //查询每日发圈转发次数 $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count(); $sub_org[$k]['share_count'] = $k_share_count; // 浏览时长 $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card']; unset($foot_where); $foot_where[] = ['employee_id', 'in', $org_employee_ids]; $foot_where[] = ['pipe_type', 'in', $clue_type]; $foot_where[] = ['addtime', 'between', [$start_time, $end_time]]; $foot_count = Footprints::where($foot_where)->count(); // 次数 $visit_long = Footprints::where($foot_where)->sum('visit_due_time'); $sub_org[$k]['visit_long'] = round($visit_long / 60, 2); $sub_org[$k]['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2); //微信沟通 unset($visit_where); $visit_where[] = ['employee_id', 'in', $org_employee_ids]; $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]]; $visit_where[] = ['type', '=', 3]; $wechat_visit_count = CustomerVisitLog::where($visit_where)->count(); $sub_org[$k]['wechat_visit_count'] = $wechat_visit_count; unset($all_customer_where); $all_customer_where[] = ['employee_id', 'in', $org_employee_ids]; $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]]; $customer_count = Customer::where($all_customer_where)->count(); $sub_org[$k]['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count; $sub_org[$k]['all_count'] = $call_count + $k_share_count + $wechat_visit_count; } $order = input('order', '', 'trim'); if ($order) { $order_arr = explode(' ', $order); $sort_field = $order_arr[0] ?? 'call'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'call': $field = 'call_count'; break; case 'share': $field = 'share_count'; break; case 'wechat': $field = 'wechat_visit_count'; break; default: $field = 'notFound'; break; } if ($sort_order == 'asc'){ $sort = SORT_ASC; } else { $sort = SORT_DESC; } $arr = []; foreach ($sub_org as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $sub_org); } } return json(['code'=> self::success, 'data'=> $sub_org]); } /** * 沟通跟进次数(员工) */ public function followUpTimesEmployee() { $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', $token['org_id'], 'intval'); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所选部门员工统计数据 $e_where[] = ['org_id', '=', $org_id]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_list = Employee::with(['user'=> function($query){ $query->field('id,headimgurl'); }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray(); foreach ($employee_list as $k => $v) { // 电话沟通次数 unset($call_where); $call_where[] = ['employee_id', '=', $v['id']]; $call_where[] = ['addtime', 'between', [$start_time, $end_time]]; $call_where[] = ['status', '=', 1]; //电话沟通 $call_count = 0; $call_time = 0; $call_avg = 0; $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec'); if ($call_log) { $call_count = count($call_log); $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2); $call_employee = count(array_unique(array_column($call_log,'employee_id'))); $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2); } // 手机打电话次数 $mbCallCount = OutCallMbLog::where([['employee_id', '=', $v['id']], ['addtime', 'between', [$start_time, $end_time]]])->count(); $call_count = $call_count + $mbCallCount; $employee_list[$k]['call_count'] = $call_count; $employee_list[$k]['call_time'] = $call_time; $employee_list[$k]['call_avg'] = $call_avg; // 内容调用次数 unset($share_where); $share_where[] = ['share_time', 'between', [$start_time, $end_time]]; $share_where[] = ['employee_id', '=', $v['id']]; // 转发次数 $share_all = ShareLog::where($share_where)->select(); $share_count = $share_all->count(); unset($wechat_where); $wechat_where[] = ['employee_id', '=', $v['id']]; $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]]; //查询每日发圈转发次数 $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count(); $employee_list[$k]['share_count'] = $k_share_count; // 浏览时长 $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card']; unset($foot_where); $foot_where[] = ['employee_id', '=', $v['id']]; $foot_where[] = ['pipe_type', 'in', $clue_type]; $foot_where[] = ['addtime', 'between', [$start_time, $end_time]]; $foot_count = Footprints::where($foot_where)->count(); // 次数 $visit_long = Footprints::where($foot_where)->sum('visit_due_time'); $employee_list[$k]['visit_long'] = round($visit_long / 60, 2); $employee_list[$k]['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2); //微信沟通 unset($visit_where); $visit_where[] = ['employee_id', '=', $v['id']]; $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]]; $visit_where[] = ['type', '=', 3]; $wechat_visit_count = CustomerVisitLog::where($visit_where)->count(); $employee_list[$k]['wechat_visit_count'] = $wechat_visit_count; unset($all_customer_where); $all_customer_where[] = ['employee_id', '=', $v['id']]; $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]]; $customer_count = Customer::where($all_customer_where)->count(); $employee_list[$k]['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count; $employee_list[$k]['all_count'] = $call_count + $k_share_count + $wechat_visit_count; } $order = input('order', '', 'trim'); if ($order) { $order_arr = explode(' ', $order); $sort_field = $order_arr[0] ?? 'call'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'call': $field = 'call_count'; break; case 'share': $field = 'share_count'; break; case 'wechat': $field = 'wechat_visit_count'; break; default: $field = 'notFound'; break; } if ($sort_order == 'asc'){ $sort = SORT_ASC; } else { $sort = SORT_DESC; } $arr = []; foreach ($employee_list as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $employee_list); } } $page = input('page', 1, 'intval'); $limit = input('limit', 10, 'intval'); $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit); return json(['code'=> self::success, 'data'=> $employee_list]); } /** * 沟通跟进次数 (客户) */ public function followUpTimesCustomer(){ $token = $this->request->token; $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $employee_id = input('employee_id', '', 'intval'); $sub_org = orgSubIds($token['org_id']); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); if (!in_array($employee_id, $sub_employee_ids)){ return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']); } // 电话沟通次数 $call_where[] = ['employee_id', '=', $employee_id]; $call_where[] = ['addtime', 'between', [$start_time, $end_time]]; $call_where[] = ['status', '=', 1]; //电话沟通 $call_count = 0; $call_time = 0; $call_avg = 0; $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec'); if ($call_log) { $call_count = count($call_log); $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2); $call_employee = count(array_unique(array_column($call_log,'employee_id'))); $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2); } // 手机打电话次数 $mbCallCount = OutCallMbLog::where([['employee_id', '=', $employee_id], ['addtime', 'between', [$start_time, $end_time]]])->count(); $call_count = $call_count + $mbCallCount; $employee_data['call_count'] = $call_count; $employee_data['call_time'] = $call_time; $employee_data['call_avg'] = $call_avg; $customer_call = array_column($call_log, 'customer_id'); // 打电话的客户id // 内容调用次数 $share_where[] = ['share_time', 'between', [$start_time, $end_time]]; $share_where[] = ['employee_id', '=', $employee_id]; // 转发次数 $share_all = ShareLog::where($share_where)->select(); $share_count = $share_all->count(); $wechat_where[] = ['employee_id', '=', $employee_id]; $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]]; //查询每日发圈转发次数 $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count(); $employee_data['share_count'] = $k_share_count; $clue_ids = CustomerClue::where([['employee_id', '=', $employee_id], ['addtime', 'between', [$start_time, $end_time]]])->column('id'); $customer_share = Customer::where('clue_id', 'in', $clue_ids)->column('id'); // 浏览时长 $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card']; unset($foot_where); $foot_where[] = ['employee_id', '=', $employee_id]; $foot_where[] = ['pipe_type', 'in', $clue_type]; $foot_where[] = ['addtime', 'between', [$start_time, $end_time]]; $foot_count = Footprints::where($foot_where)->count(); // 次数 $visit_long = Footprints::where($foot_where)->sum('visit_due_time'); $employee_data['visit_long'] = round($visit_long / 60, 2); $employee_data['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2); //微信沟通 $visit_where[] = ['employee_id', '=', $employee_id]; $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]]; $visit_where[] = ['type', '=', 3]; $wechat_visit = CustomerVisitLog::where($visit_where)->select()->toArray(); $wechat_visit_count = count($wechat_visit); $employee_data['wechat_visit_count'] = $wechat_visit_count; $customer_wechat = array_column($wechat_visit, 'customer_id'); $all_customer_where[] = ['employee_id', '=', $employee_id]; $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]]; $customer_count = Customer::where($all_customer_where)->count(); $employee_data['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count; $employee_data['all_count'] = $call_count + $k_share_count + $wechat_visit_count; $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name'); $data['employee'] = $employee_data; $type = input('type', '', 'trim'); switch ($type){ case 'call': $customer_id = $customer_call; break; case 'share': $customer_id = $customer_share; break; case 'wechat': $customer_id = $customer_wechat; break; default: $customer_id = array_values(array_filter(array_unique(array_merge($customer_call, $customer_share, $customer_wechat)))); break; } $page = input('page', 1, 'intval'); $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select(); $customers = $customers->visible(['id', 'employee_id', 'designer_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'employee.name', 'agent.agent_name'])->toArray(); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach ($customers as $k => $v) { $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; } $data['customer'] = $customers; return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']); } /** * 卖卡首页统计 */ public function maika_index() { $token = $this->request->token; $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', '', 'intval'); $org_id = !empty($org_id) ? $org_id : $token['org_id']; if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); //员工数量 $org_name = Org::where('id', $org_id)->value('name'); $sub_org = orgSubIds($org_id); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos'); $db_where[] = ['Customer.org_id', 'in', $sub_org]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state]; $count = Db::view('CustomerVisitLog', 'id') ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->group('Customer.id') ->count(); $data = ['org_name'=> $org_name, 'employee_count'=> count($sub_employee_ids), 'count'=> $count]; return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]); } /** * 卖卡部门数据 */ public function maika_org(){ $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time','order']); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $root_sub = orgSubIds($token['root_org']); // 所选部门 $org_id = input('org_id', '', 'intval'); if (!$org_id) { $org_id = $token['org_id']; } if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所选部门 $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray(); // 所有客户量 用于后面计算百分比 $sub_org_ids = orgSubIds($org_id); $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos'); $db_where[] = ['Customer.org_id', 'in', $sub_org_ids]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state]; $log_list = Db::view('CustomerVisitLog', 'id') ->view('Customer', 'id cid,org_id', 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->order('confirm_date asc') ->select() ->toArray(); // 每个员工的跟进记录 $org_log = []; foreach ($log_list as $k => $v) { if (empty($org_log[$v['org_id']])) { $org_log[$v['org_id']] = []; } elseif (in_array($v['cid'], $org_log[$v['org_id']])) { continue; } $org_log[$v['org_id']][] = $v['cid']; } foreach ($sub_org as $k => $v){ // 是否有子部门 $have_sub_org = Org::where('pid', '=', $v['id'])->count(); if ($have_sub_org > 0){ $sub_org[$k]['have_sub_org'] = true; } else { $sub_org[$k]['have_sub_org'] = false; } $v_sub_org = orgSubIds($v['id']); $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); $sub_org[$k]['employee_count'] = count($org_employee_ids); $sub_org[$k]['count'] = !empty($org_log[$v['id']]) ? count($org_log[$v['id']]) : 0; } if (!empty($param['order'])) { $order_arr = explode(' ', $param['order']); $sort_field = $order_arr[0] ?? 'count'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'count': $field = 'count'; break; default: $field = 'count'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($sub_org as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $sub_org); } } return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]); } /** * 卖卡员工数据 */ public function maika_employee(){ $token = $this->request->token; $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); // 所选部门 $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id']; $root_sub = orgSubIds($token['root_org']); if (!in_array($org_id, $root_sub)){ return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']); } // 所选部门员工统计数据 $e_where[] = ['org_id', '=', $org_id]; $e_where[] = ['state', '=', '在职']; $e_where[] = ['uid', '<>', 0]; $employee_list = Employee::with(['user'=> function($query){ $query->field('id,headimgurl'); }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray(); // 所有客户量 $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos'); // 每个员工的客户 $db_where[] = ['Customer.org_id', '=', $org_id]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state]; $log_list = Db::view('CustomerVisitLog', 'id,customer_employee_id') ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->order('confirm_date asc') ->select() ->toArray(); // 每个员工的跟进记录 $employee_log = []; foreach ($log_list as $k => $v) { if (empty($employee_log[$v['customer_employee_id']])) { $employee_log[$v['customer_employee_id']] = []; } elseif (in_array($v['cid'], $employee_log[$v['customer_employee_id']])) { continue; } $employee_log[$v['customer_employee_id']][] = $v['cid']; } foreach($employee_list as $k => $v){ $employee_list[$k]['employee_count'] = count($employee_list); $employee_list[$k]['count'] = !empty($employee_log[$v['id']]) ? count($employee_log[$v['id']]) : 0; } if (!empty($param['order'])) { $order_arr = explode(' ', $param['order']); $sort_field = $order_arr[0] ?? 'count'; $sort_order = $order_arr[1] ?? 'desc'; switch ($sort_field){ case 'count': $field = 'count'; break; default: $field = 'count'; break; } $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC; $arr = []; foreach ($employee_list as $v){ if (!isset($v[$field])){ break; } $arr[] = $v[$field]; } if (!empty($arr)){ array_multisort($arr, $sort, $employee_list); } } $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']); return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]); } /** * 卖卡员工客户数据 */ public function maika_customer() { $token = $this->request->token; $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]); $start_time_get = input('start_time', time(), 'strtotime'); $end_time_get = input('end_time', time(), 'strtotime'); $start_time = date('Y-m-d 00:00:00', $start_time_get); $end_time = date('Y-m-d 23:59:59', $end_time_get); $employee_id = input('employee_id', '', 'intval'); $sub_org = orgSubIds($token['org_id']); $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id'); if (!in_array($employee_id, $sub_employee_ids)){ return json(['code'=> self::success, 'data'=> ['empdata'=> [], 'list'=> []], 'msg'=> 'success']); } $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos'); $wuxiao = Customer::changeState('无效', 'chaos'); $db_where[] = ['Customer.employee_id', '=', $employee_id]; $db_where[] = ['Customer.state', 'NOT NULL', null]; $db_where[] = ['Customer.state', 'not in', $wuxiao]; $db_where[] = ['Customer.died', '<>', 2]; $db_where[] = ['CustomerVisitLog.is_merge', '=', 0]; $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]]; $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state]; $customer_ids = Db::view('CustomerVisitLog', 'id') ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id') ->where($db_where) ->group('Customer.id') ->column('Customer.id'); $cuslist = Customer::with(['employee'=>function($query){ $query->field('id,name'); }])->where('id', 'in', $customer_ids)->page($param['page'],$param['limit'])->column('id,name,phone,state,addtime,community_name,employee_id,last_contact_date,level,sex,square,updatetime'); $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find(); if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $customer_ids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; } foreach($cuslist as $key=>$val){ $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']]; } $empdata = ['empname'=> $employeedata['name'], 'employee_count'=> 1, 'count'=> count($customer_ids)]; $data = ['empdata'=> $empdata, 'list'=> $cuslist]; return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]); } }