1, 'msg' => '请登录'])->send(); exit; } $employee = session('platform'); // $empid = Request::param('emp_id'); // $employee = Employee::find($empid); $this->company = Company::where([['root_id', '=', $employee->root_id]])->find(); $start_date = Request::param('start_date'); $end_date = Request::param('end_date'); $this->start_date = empty($start_date) ? date('Y-m-d 00:00:00', strtotime($this->company->addtime)) : date('Y-m-d 00:00:00', strtotime($start_date)); $this->end_date = empty($end_date) ? date('Y-m-d 00:00:00', time()) : date('Y-m-d 00:00:00', strtotime($end_date) + 86400); $dateDiff = get_date_diff($this->start_date, $this->end_date); $this->dayOrMonth = $dateDiff->days > 45 ? 'month' : 'day'; $this->orgList = orgSubIds($employee->org_id); } /** * 按月获取线索量 */ public function clue() { $sql = CustomerClue::where([ ['org_id', 'in', $this->orgList], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date] ]); if ($this->dayOrMonth == 'month') $data = $sql->field('YEAR(addtime) as year, MONTH(addtime) as month, count(*) as clue_num')->group('YEAR(addtime),MONTH(addtime)')->order("year desc,month desc")->select(); else $data = $sql->field('YEAR(addtime) as year, MONTH(addtime) as month, DAY(addtime) as day,count(*) as clue_num')->group('YEAR(addtime),MONTH(addtime),DAY(addtime)')->order("year desc,month desc, day desc")->select(); $data = $this->setMonthOrDay($data, ['clue_num' => 0]); $count = array_sum(array_column($data, 'clue_num')); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 线索详情 */ public function clueDetail(int $page = 1) { $condition = [ ['org_id', 'in', $this->orgList], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date] ]; $data = CustomerClue::with(['org', 'employee', 'user'])->where($condition)->order('addtime desc')->page($page, 10)->select(); $data = $data->visible(['nickname', 'employee_name', 'org_name', 'addtime'])->toArray(); $count = CustomerClue::where($condition)->count(); foreach ($data as &$item) { $orglist = explode('/', $item['org_name']); $item['org_name'] = array_pop($orglist); } return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 按月获取加微客户量 */ public function wx() { $sql = Customer::where([ ['org_id', 'in', $this->orgList], ['add_wechat_time', 'NOTNULL', ''], ['add_wechat_time', '>=', $this->start_date], ['add_wechat_time', '<', $this->end_date] ]); if ($this->dayOrMonth == 'month') $data = $sql->field('YEAR(add_wechat_time) as year, MONTH(add_wechat_time) as month, count(*) as wx_num') ->group('YEAR(add_wechat_time),MONTH(add_wechat_time)') ->order('year desc, month desc') ->select(); else $data = $sql->field('YEAR(add_wechat_time) as year, MONTH(add_wechat_time) as month, DAY(add_wechat_time) as day, count(*) as wx_num') ->group('YEAR(add_wechat_time),MONTH(add_wechat_time),DAY(add_wechat_time)') ->order('year desc, month desc, day desc') ->select(); $data = $this->setMonthOrDay($data, ['wx_num' => 0]); $count = array_sum(array_column($data, 'wx_num')); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function wxDetail(int $page = 1) { $condition = [ ['org_id', 'in', $this->orgList], ['add_wechat_time', 'NOTNULL', ''], ['add_wechat_time', '>=', $this->start_date], ['add_wechat_time', '<', $this->end_date] ]; $data = Customer::with(['employee', 'org', 'source'])->where($condition) ->order('add_wechat_time desc')->page($page, 10)->select(); $data = $data->visible(['name', 'add_wechat_time', 'employee.name', ' org.name', 'community_name', 'square', 'source.source'])->toArray(); $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 获取意向客户量以及总量 */ public function intention() { $intentionId = CustomerPortraitField::where(['root_id' => $this->company['root_id'], 'keyname' => 'intention'])->value('id'); $intention = CustomerPortraitFieldSelect::where(['pid' => $intentionId])->field('id,name')->select(); $data = []; foreach ($intention as $i) { $sql = Customer::where([ ['org_id', 'in', $this->orgList], ['ext', 'REGEXP', '"value": "' . $i['id'] . '", "keyname": "intention"'], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date] ]); if ($this->dayOrMonth == 'month') $intentionMonthData = $sql->field('YEAR(addtime) as year, MONTH(addtime) as month, count(*) as num')->group('YEAR(addtime),MONTH(addtime)')->order('year desc,month desc')->select(); else $intentionMonthData = $sql->field('YEAR(addtime) as year, MONTH(addtime) as month, DAY(addtime) as day, count(*) as num')->group('YEAR(addtime),MONTH(addtime),DAY(addtime)')->order('year desc,month desc,day desc')->select(); $intentionMonthData = $this->setMonthOrDay($intentionMonthData, ['num' => 0]); $intentionCount = array_sum(array_column($intentionMonthData, 'num')); $data[] = ['intention' => $i['name'], 'data' => $intentionMonthData, 'count' => $intentionCount]; } $total = array_sum(array_column($data, 'count')); return json(['code' => 0, 'data' => $data, 'count' => $total]); } public function intentionDetail(int $page = 1, string $intention = '') { $intentionId = CustomerPortraitField::where(['root_id' => $this->company['root_id'], 'keyname' => 'intention'])->value('id'); $condition = [ ['org_id', 'in', $this->orgList], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date] ]; if (empty($intention)) { $condition[] = ['ext', 'REGEXP', '"value": "[0-9]+", "keyname": "intention"']; } else { $intentionId = CustomerPortraitField::where(['root_id' => $this->company['root_id'], 'keyname' => 'intention'])->value('id'); $intention = CustomerPortraitFieldSelect::where(['pid' => $intentionId, 'name' => $intention])->value('id'); $condition[] = ['ext', 'REGEXP', '"value": "' . $intention . '", "keyname": "intention"']; } $intentionMonthData = Customer::with(['employee', 'org', 'source'])->where($condition) ->order('addtime desc')->page($page, 10)->select(); $intentionMonthData = $intentionMonthData->visible(['name', 'employee.name', 'org_name', 'community_name', 'square', 'source.source'])->toArray(); $count = Customer::where($condition)->count(); return json(['code' => 0, 'data' => $intentionMonthData, 'count' => $count]); } /** * 按月获取量房,到店客户量 */ public function tan() { $orgList = $this->orgList; $sql = CustomerVisitLog::withJoin(['customer'])->where([ ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')], ['customer.org_id', 'in', $orgList], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date] ]); if ($this->dayOrMonth == 'month') $daodian = $sql->order('year desc, month desc') ->group('customer_visit_log.customer_id')->column('YEAR(min(customer_visit_log.addtime)) as year, MONTH(min(customer_visit_log.addtime)) as month, customer_visit_log.customer_id'); else $daodian = $sql->order('year desc, month desc, day desc') ->group('customer_visit_log.customer_id')->column('YEAR(min(customer_visit_log.addtime)) as year, MONTH(min(customer_visit_log.addtime)) as month, DAY(min(customer_visit_log.addtime)) as day, customer_visit_log.customer_id'); $daodiancount = $this->yearMonthNum($daodian); $daodiancount = $this->setMonthOrDay($daodiancount, ['num' => 0]); $daodiantotal = array_sum(array_column($daodiancount, 'num')); $sql1 = CustomerVisitLog::withJoin(['customer'])->where([ ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')], ['customer.org_id', 'in', $orgList], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date] ]); if ($this->dayOrMonth == 'month') $liangfang = $sql1->order('year desc, month desc') ->group('customer_visit_log.customer_id')->column('YEAR(min(customer_visit_log.addtime)) as year, MONTH(min(customer_visit_log.addtime)) as month, customer_visit_log.customer_id'); else $liangfang = $sql1->order('year desc, month desc, day desc') ->group('customer_visit_log.customer_id')->column('YEAR(min(customer_visit_log.addtime)) as year, MONTH(min(customer_visit_log.addtime)) as month, DAY(min(customer_visit_log.addtime)) as day, customer_visit_log.customer_id'); $liangfangcount = $this->yearMonthNum($liangfang); $liangfangcount = $this->setMonthOrDay($liangfangcount, ['num' => 0]); $liangfangtotal = array_sum(array_column($liangfangcount, 'num')); return json(['code' => 0, 'data' => [ 'liangfang' => ['data' => $liangfangcount, 'count' => $liangfangtotal], 'daodian' => ['data' => $daodiancount, 'count' => $daodiantotal] ]]); } /** * 按区域获取客户量 */ public function region() { $city = $this->company['city']; $cityNum = CountryCity::where(['name' => $city])->value('num'); $provinceNum = substr((string)$cityNum, 0, 2); $province = CountryCity::where(['num' => $provinceNum])->value('name'); $qu = CountryCity::where([['num', 'like', $cityNum . '%'], ['num', '<>', $cityNum]])->column('name'); $provinceCity = $province . '/' . $city . '/'; $data = []; foreach ($qu as $q) { $pcq = $provinceCity . $q; $q_num = Customer::where([ ['org_id', 'in', $this->orgList], ['ext', 'like', '%"value": "' . $pcq . '", "keyname": "house_location"%'], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date] ])->count(); $data[] = ['county' => $q, 'count' => $q_num]; } return json(['code' => 0, 'data' => $data, 'province_city' => $provinceCity]); } /** * 按区域获取客户量 */ public function regionDetail($qu, int $page = 1) { $city = $this->company['city']; $cityNum = CountryCity::where(['name' => $city])->value('num'); $provinceNum = substr((string)$cityNum, 0, 2); $province = CountryCity::where(['num' => $provinceNum])->value('name'); $pcq = $province . '/' . $city . '/' . $qu; $data = Customer::with(['employee', 'org', 'source']) ->where([ ['org_id', 'in', $this->orgList], ['ext', 'like', '%"value": "' . $pcq . '", "keyname": "house_location"%'], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date] ])->page($page, 10)->select(); $data = $data->visible(['name', 'employee.name', 'org_name', 'community_name', 'square', 'source.source'])->toArray(); $count = Customer::where([ ['org_id', 'in', $this->orgList], ['ext', 'like', '%"value": "' . $pcq . '", "keyname": "house_location"%'], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date] ])->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 按月获取交定数量和交定金额 */ public function ding() { $orgList = $this->orgList; $sql = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date] ]); if ($this->dayOrMonth == 'month') $ding = $sql->order('year desc, month desc') ->group('customer_visit_log.customer_id') ->column('YEAR(max(customer_visit_log.addtime)) as year, MONTH(max(customer_visit_log.addtime)) as month, customer_visit_log.customer_id, customer.deposit_money as money'); else $ding = $sql->order('year desc, month desc, day desc') ->group('customer_visit_log.customer_id') ->column('YEAR(max(customer_visit_log.addtime)) as year, MONTH(max(customer_visit_log.addtime)) as month, DAY(max(customer_visit_log.addtime)) as day, customer_visit_log.customer_id, customer.deposit_money as money'); $dingcount = $this->yearMonthNum($ding); $dingcount = $this->setMonthOrDay($dingcount, ['num' => 0, 'money' => 0]); $total = array_sum(array_column($dingcount, 'num')); $money = array_sum(array_column($dingcount, 'money')); return json(['code' => 0, 'data' => $dingcount, 'count' => $total, 'money' => $money]); } public function detail(int $page = 1, string $type) { $orgList = $this->orgList; $logQuery = CustomerVisitLog::withJoin('customer') ->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已' . $type, 'chaos')], ])->group('customer.id')->fetchSql(true)->column('customer.id as customer_id, min(customer_visit_log.confirm_date) as first_meet_time'); $qian = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->start_date], ['first_meet_time', '<', $this->end_date] ])->page($page, 10)->column('customer_id, first_meet_time'); $count = Db::table('(' . $logQuery . ') a') ->where([ ['first_meet_time', '>=', $this->start_date], ['first_meet_time', '<', $this->end_date] ])->count(); $customerIds = array_column($qian, 'customer_id'); $customer = Customer::with(['employee', 'org', 'source'])->where([['id', 'in', $customerIds]])->select(); $customer = $customer->toArray(); $customer = array_combine(array_column($customer, 'id'), $customer); $data = []; foreach ($qian as $item) { $data[] = [ 'name' => $customer[$item['customer_id']]['name'], 'employee_name' => empty($customer[$item['customer_id']]['employee']) ? '' : $customer[$item['customer_id']]['employee']['name'], 'org_name' => $customer[$item['customer_id']]['org_name'], 'community_name' => $customer[$item['customer_id']]['community_name'], 'square' => $customer[$item['customer_id']]['square'], 'source' => !isset($customer[$item['customer_id']]['source']) || empty($customer[$item['customer_id']]['source']) ? '' : $customer[$item['customer_id']]['source']['source'], 'addtime' => $item['first_meet_time'] ]; } return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 按月获取签单数量和签单金额,总数量,总金额 */ public function sign() { $orgList = $this->orgList; $sql = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_visit_log.confirm_date', '>=', $this->start_date], ['customer_visit_log.confirm_date', '<', $this->end_date] ]); if ($this->dayOrMonth == 'month') $sign = $sql->order('year desc, month desc') ->group('customer_visit_log.customer_id') ->column('YEAR(max(customer_visit_log.confirm_date)) as year, MONTH(max(customer_visit_log.confirm_date)) as month, customer_visit_log.customer_id, if(customer.signed_money,customer.signed_money,0) as money'); else $sign = $sql->order('year desc, month desc, day desc') ->group('customer_visit_log.customer_id') ->column('YEAR(max(customer_visit_log.confirm_date)) as year, MONTH(max(customer_visit_log.confirm_date)) as month, DAY(max(customer_visit_log.confirm_date)) as day, customer_visit_log.customer_id, if(customer.signed_money,customer.signed_money,0) as money'); $signcount = $this->yearMonthNum($sign); $signcount = $this->setMonthOrDay($signcount, ['num' => 0, 'money' => 0]); $signTotal = ['count' => 0, 'money' => 0]; foreach ($signcount as $item) { $signTotal['count'] += $item['num']; $signTotal['money'] += $item['money']; } return json(['code' => 0, 'data' => ['month' => $signcount, 'total' => $signTotal]]); } /** * 按时间查询总业绩 */ public function yeji() { $orgList = $this->orgList; $data = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date], ])->group('customer.id')->column('customer.id, customer.signed_money as money'); $total = array_sum(array_column($data, 'money')); return json(['code' => 0, 'data' => $total]); } /** * 按时间查询员工排行 */ public function employee() { $orgList = $this->orgList; $data = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date], ])->group('customer.employee_id,customer.id')->column('customer.employee_id,customer.signed_money as money'); $d = []; foreach ($data as $item) { if (!isset($d[$item['employee_id']])) $d[$item['employee_id']] = 0; $d[$item['employee_id']] += $item['money']; } $employees = Employee::where([['id', 'in', array_keys($d)]])->column('name', 'id'); $r = []; foreach ($d as $empid => $money) { $r[] = [ 'employee_name' => $employees[$empid], 'money' => $money ]; } array_multisort(array_column($r, 'money'), SORT_DESC, $r); return json(['code' => 0, 'data' => $r]); } /** * 按时间查询团队排行 */ public function team() { $orgList = $this->orgList; $data = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date], ])->group('customer.id') ->column('customer.org_id,customer.signed_money as money'); $d = []; foreach ($data as $item) { if (!isset($d[$item['org_id']])) $d[$item['org_id']] = 0; $d[$item['org_id']] += $item['money']; } $orgs = Org::where([['id', 'in', array_keys($d)]])->column('name', 'id'); $r = []; foreach ($d as $orgid => $money) { $r[] = [ 'org_name' => $orgs[$orgid], 'money' => $money ]; } array_multisort(array_column($r, 'money'), SORT_DESC, $r); return json(['code' => 0, 'data' => $r]); } /** * 按时间查询业绩排行 */ public function performance() { $orgList = $this->orgList; // 客户总量 $total = Customer::where([['org_id', 'in', $orgList], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date]])->count(); // 加微客户统计 $jiawei = Customer::where([['org_id', 'in', $orgList], ['add_wechat_time', 'NOTNULL', ''], ['add_wechat_time', '>=', $this->start_date], ['add_wechat_time', '<', $this->end_date]])->count(); // 意向客户统计 $yixiang = Customer::where([['org_id', 'in', $this->orgList], ['ext', 'REGEXP', '"value": "[0-9]+", "keyname": "intention"'], ['addtime', '>=', $this->start_date], ['addtime', '<', $this->end_date]])->count(); // 量房客户统计 $liangfang = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date] ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->count(); // 到店客户统计 $daodian = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date] ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->count(); // 交定客户统计 $jiaoding = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date] ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->count(); // 签单客户统计 $sign = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date] ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->count(); // 二访数据统计 $daodian2 = CustomerVisitLog::withJoin(['customer'])->where([ ['customer.org_id', 'in', $orgList], ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')], ['customer_visit_log.addtime', '>=', $this->start_date], ['customer_visit_log.addtime', '<', $this->end_date] ])->group('customer_visit_log.customer_id')->field('customer_visit_log.customer_id')->having('count(customer_visit_log.id)=2')->count(); // 比例计算 $jiaodingType = '签单'; $qiandanType = '转单'; $data = [ ['lv' => $this->lv($jiawei, $total), 'type' => '加微客户', 'count' => $jiawei], ['lv' => $this->lv($yixiang, $total), 'type' => '意向客户', 'count' => $yixiang], ['lv' => $this->lv($liangfang, $total), 'type' => '量房客户', 'count' => $liangfang], ['lv' => $this->lv($daodian, $total), 'type' => '到店客户', 'count' => $daodian], ['lv' => $this->lv($daodian2, $total), 'type' => '二访客户', 'count' => $daodian2], ['lv' => $this->lv($jiaoding, $total), 'type' => $jiaodingType . '客户', 'count' => $jiaoding], ['lv' => $this->lv($sign, $total), 'type' => $qiandanType . '客户', 'count' => $sign] ]; // 数据排序 // array_multisort($data, array_column($data, 'count')); return json(['code' => 0, 'data' => $data]); } private function lv($chued, $chu) { if (empty($chued)) return '0%'; if (empty($chu)) return '100%'; return round($chued / $chu * 100, 2) . '%'; } private function yearMonthNum($data) { $count = []; foreach ($data as $row) { $year = $row['year']; $month = $row['month']; // 构建年月的键名 $key = $year . '-' . $month; if ($this->dayOrMonth == 'day') { $key .= '-' . $row['day']; } if (!isset($count[$key])) $count[$key]['num'] = 0; $count[$key]['num']++; if (isset($row['money'])) { if (!isset($count[$key]['money'])) $count[$key]['money'] = 0; $count[$key]['money'] += $row['money']; } } $data = []; foreach ($count as $ym => $num) { if ($this->dayOrMonth == 'day') { list($year, $month, $day) = explode('-', $ym); $r = [ 'year' => $year, 'month' => $month, 'day' => $day, ]; } else { list($year, $month) = explode('-', $ym); $r = [ 'year' => $year, 'month' => $month ]; } foreach ($num as $k => $v) { $r[$k] = $v; } $data[] = $r; } return $data; } /** * 处理中间没有月份的数据 */ private function setMonthOrDay($existingData, $initData) { $startTimestamp = strtotime($this->end_date); $endTimestamp = strtotime($this->start_date); $result = array(); while ($startTimestamp >= $endTimestamp) { $year = date('Y', $startTimestamp); $month = date('n', $startTimestamp); $day = date('j', $startTimestamp); $row = ['year' => $year, 'month' => $month]; $this->dayOrMonth !== 'day' ?: $row['day'] = $day; $row = array_merge($row, $initData); foreach ($existingData as $existing) { if ($existing['year'] == $year && $existing['month'] == $month) { if ($this->dayOrMonth == 'day' && $existing['day'] == $day) { $row = $existing; break; } elseif ($this->dayOrMonth == 'month') { $row = $existing; break; } } } $result[] = $row; $startTimestamp = strtotime('-1 ' . $this->dayOrMonth, $startTimestamp); } return $result; } public function logout() { session('platform', null); return json(['code' => 0, 'msg' => '退出登录成功']); } }