123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320 |
- <?php
- namespace app\sys\controller;
- use app\BaseController;
- use app\model\CompanyBossOpen;
- use app\model\Customer;
- use app\model\CustomerClue;
- use app\model\Employee;
- use app\model\Grant;
- use app\model\Org;
- use app\model\Permission;
- use app\model\User;
- use app\model\Company;
- use app\model\CustomerVisitLog;
- use think\facade\Db;
- use think\facade\View;
- use think\facade\Session;
- use wx\Oplatform;
- use app\model\SmartScreen;
- use app\model\Setting;
- class Index extends BaseController
- {
- /**
- * 面板
- */
- public function index()
- {
- $grant = Grant::where('id', $this->request->employee['grant_id'])->find();
- $permission = is_null($grant) ? [] : (array)$grant->permission;
- $company = Org::where('id', $this->request->employee['root_id'])->value('name');
- $companyInfo = Company::where('root_id', $this->request->employee['root_id'])->find();
- $menu = (new Permission())->getTree([['id', 'in', $permission], ['is_menu', '=', 1]], $this->request->employee['root_id']);
- //2022-12-29 智慧屏菜单和智慧屏首页绑定
- $smart = SmartScreen::where([['root_id', '=', $this->request->employee['root_id']], ['del', '=', 0]])->column('title', 'code');
- //智慧屏菜单排序
- $where = [
- ['name', '=', 'wisdomMenuOrder'],
- ['root_id', '=', $this->request->employee['root_id']]
- ];
- $order = Setting::where($where)->value('content');
- foreach ($menu as $k => $v) {
- if ($v['auth_name'] == '智慧屏设置') {
- foreach ($v['child'] as $k2 => $v2) {
- $arr = explode('?type=', $v2['uri']);
- if (count($arr) == 2 && isset($smart[$arr[1]])) {
- $menu[$k]['child'][$k2]['auth_name'] = $smart[$arr[1]];
- }
- }
- if ($order) {
- $ids = explode(',', $order);
- $nsortData = [];
- $sortData = [];
- foreach ($menu[$k]['child'] as $v4) {
- $sk = array_search($v4['id'], $ids);
- if ($sk === false) $nsortData[] = $v4;
- else $sortData[$sk] = $v4;
- }
- ksort($sortData);
- $new_order = array_merge($nsortData, $sortData);
- $menu[$k]['child'] = $new_order;
- }
- break;
- }
- }
- View::assign("menus", $menu);
- View::assign("company", $company);
- View::assign("company_info", $companyInfo);
- View::assign("employee", $this->request->employee);
- // 判断当前店面是否是集团
- if ($companyInfo->company_group === 0 && $this->request->employee['grant_id'] == 2) {
- //话术添加二级分类要打开新浏览器窗口判断
- $type = input('type', '');
- $id = input('id', 0);
- if ($type == 'talkskill') {
- View::assign("homePageUrl", url('talkskill/cate2_view', ['id' => $id]));
- } else {
- View::assign("homePageUrl", url('company/brand'));
- }
- } else {
- //话术添加二级分类要打开新浏览器窗口判断
- $type = input('type', '');
- $id = input('id', 0);
- if ($type == 'talkskill') {
- View::assign("homePageUrl", url('talkskill/cate2_view', ['id' => $id]));
- } else {
- View::assign("homePageUrl", url('index/welcome'));
- }
- //
- }
- // 判断当前是否绑定微信
- $domain = request()->domain();
- if (strpos($domain, "zqxg.cc") !== false) {
- $bind_field = 'xq_qrcode_show'; //薛鹊主体绑定
- $openid = $this->request->employee->xq_web_openid;
- $appid = config('app.xqweb_appid');
- $field = 'xq_unionid';
- } else {
- $bind_field = 'qrcode_show'; //原有主体绑定
- $openid = $this->request->employee->openid;
- $appid = config('app.kfweb_appid');
- $field = 'unionid';
- }
- if (!session($bind_field) && empty($openid)) { //第一次登陆 并且 openid为空 弹出否则不谈
- // $data->nickname = ' ';
- $state = rand(1000, 9999);
- View::assign('state', $state);
- View::assign('domain', $domain);
- View::assign('appid', $appid);
- session($bind_field, true);
- View::assign('qrcode_show', true);
- } else {
- View::assign('qrcode_show', false);
- }
- //切换店面查询手机号下其他店面
- $login_type = session('login_type');
- $w[] = ['root_id', '<>', $this->request->employee['root_id']];
- $w[] = ['grant_id', '>', 0];
- if ($login_type == 'phone_login') {
- $w[] = ['phone', '=', cypherphone($this->request->employee->phone)];
- } else {
- $w[] = [$field, '=', $this->request->employee->$field];
- }
- $root_ids = Employee::where($w)->column('phone', 'root_id');
- $w1[] = ['root_id', 'in', array_keys($root_ids)];
- $ydate = date('Y-m-d', strtotime('-1 day'));
- $w1[] = ['end_date', '>', $ydate . ' 23:59:59'];
- $w1[] = ['status', '=', 0]; //排除禁用
- // $w1[] = ['company_group','>',0];//排除集团账号
- $shop = Company::with(['brand'])->where($w1)->field('company_name,root_id,logo,company_brand')->select()->toArray();
- foreach ($shop as $key => $value) {
- $shop[$key]['phone'] = $root_ids[$value['root_id']];
- if ($value['brand'] && $value['brand']['logo'] && !$value['logo']) {
- $shop[$key]['logo'] = $value['brand']['logo'];
- }
- }
- View::assign('shop', $shop);
- return View::fetch();
- }
- /*
- * 首页数据中心
- */
- public function welcome()
- {
- $customerObj = new Customer();
- $customerClueObj = new CustomerClue();
- $request = request();
- // if ($request->employee->is_manager) {
- $customerObj = $customerObj->where([['org_id', 'in', $request->org]]);
- $customerClueObj = $customerClueObj->where([['org_id', 'in', $request->org]]);
- // } else {
- // $customerObj = $customerObj->where([['employee_id', '=', $request->employee->id]]);
- // $customerClueObj = $customerClueObj->where([['employee_id', '=', $request->employee->id]]);
- // }
- // 客户,定金,签单,线索
- $firstDay = date('Y-m-d 00:00:00', strtotime('first day of this month'));
- $lastDay = date('Y-m-d 00:00:00', strtotime('first day of next month'));
- // $prevFirstDay = date('Y-m-d 00:00:00', strtotime('first day of previous month'));
- // 客户
- $thisMonthCustomerNum = (clone $customerObj)->where([['addtime', 'between', [$firstDay, $lastDay]]])->count();
- // $prevMonthCustomerNum = (clone $customerObj)->where([['addtime', 'between', [$prevFirstDay, $firstDay]]])->count();
- $totalCustomerNum = (clone $customerObj)->where([['employee_id', 'not null', '']])->count();
- // 定金
- $thisMonthDingNum = (clone $customerObj)->where([['state', 'in', Customer::changeState('交定', 'chaos')], ['updatetime', 'between', [$firstDay, $lastDay]]])->count();
- // $prevMonthDingNum = (clone $customerObj)->where([['state', '=', '交定'], ['updatetime', 'between', [$prevFirstDay, $firstDay]]])->count();
- $totalDingNum = (clone $customerObj)->where([['state', 'in', Customer::changeState('交定', 'chaos')]])->count();
- // 签单
- $thisMonthSignNum = (clone $customerObj)->where([['state', 'in', Customer::changeState('签单', 'chaos')], ['updatetime', 'between', [$firstDay, $lastDay]]])->count();
- // $prevMonthSignNum = (clone $customerObj)->where([['state', '=', '签单'], ['updatetime', 'between', [$prevFirstDay, $firstDay]]])->count();
- $totalSignNum = (clone $customerObj)->where([['state', 'in', Customer::changeState('签单', 'chaos')]])->count();
- // 线索
- $thisMonthClueNum = (clone $customerClueObj)->where([['addtime', 'between', [$firstDay, $lastDay]]])->count();
- // $prevMonthClueNum = (clone $customerClueObj)->where([['addtime', 'between', [$prevFirstDay, $firstDay]]])->count();
- $totalClueNum = (clone $customerClueObj)->count();
- View::assign('thisMonthCustomerNum', $thisMonthCustomerNum);
- // View::assign('prevMonthCustomerNum', $prevMonthCustomerNum);
- View::assign('totalCustomerNum', $totalCustomerNum);
- View::assign('thisMonthDingNum', $thisMonthDingNum);
- // View::assign('prevMonthDingNum', $prevMonthDingNum);
- View::assign('totalDingNum', $totalDingNum);
- View::assign('thisMonthSignNum', $thisMonthSignNum);
- // View::assign('prevMonthSignNum', $prevMonthSignNum);
- View::assign('totalSignNum', $totalSignNum);
- View::assign('thisMonthClueNum', $thisMonthClueNum);
- // View::assign('prevMonthClueNum', $prevMonthClueNum);
- View::assign('totalClueNum', $totalClueNum);
- // 线性图取几个月
- $m = 5;
- $mMonthFirstDay = date('Y-m-1 00:00:00', strtotime('-' . ($m - 1) . ' month'));
- $now = date('Y-m-d H:i:s');
- // 数据统计
- $ding = (clone $customerObj)->where([['state', 'in', Customer::changeState('交定', 'chaos')], ['updatetime', 'between', [$mMonthFirstDay, $now]]])->group('month')->column("DATE_FORMAT(updatetime,'%c') as month,COUNT(id) as num");
- $sign = (clone $customerObj)->where([['state', 'in', Customer::changeState('签单', 'chaos')], ['updatetime', 'between', [$mMonthFirstDay, $now]]])->group('month')->column("DATE_FORMAT(updatetime,'%c') as month,COUNT(id) as num");
- $noVisit = (clone $customerObj)->where([['first_visit_date', 'null', ''], ['addtime', 'between', [$mMonthFirstDay, $now]]])->group('month')->column("DATE_FORMAT(addtime,'%c') as month,COUNT(id) as num");
- $valid = (clone $customerObj)->where([['state', 'in', Customer::changeState('无效', 'chaos')], ['updatetime', 'between', [$mMonthFirstDay, $now]]])->group('month')->column("DATE_FORMAT(addtime,'%c') as month,COUNT(id) as num");
- $wait = (clone $customerObj)->where([['state', 'in', Customer::changeState('待确认', 'chaos')], ['updatetime', 'between', [$mMonthFirstDay, $now]]])->group('month')->column("DATE_FORMAT(addtime,'%c') as month,COUNT(id) as num");
- $ding = array_combine(array_column($ding, 'month'), array_column($ding, 'num'));
- $sign = array_combine(array_column($sign, 'month'), array_column($sign, 'num'));
- $noVisit = array_combine(array_column($noVisit, 'month'), array_column($noVisit, 'num'));
- $valid = array_combine(array_column($valid, 'month'), array_column($valid, 'num'));
- $wait = array_combine(array_column($wait, 'month'), array_column($wait, 'num'));
- $thisMonth = date('n');
- $data = [
- 'month' => [],
- 'ding' => [],
- 'sign' => [],
- 'noVisit' => [],
- 'valid' => [],
- 'wait' => []
- ];
- for ($i = 0; $i < $m; $i++) {
- $month = ($thisMonth - $i) > 0 ? $thisMonth - $i : $thisMonth - $i + 12;
- array_unshift($data['month'], $month . '月');
- array_unshift($data['ding'], isset($ding[$month]) ? $ding[$month] : 0);
- array_unshift($data['sign'], isset($sign[$month]) ? $sign[$month] : 0);
- array_unshift($data['noVisit'], isset($noVisit[$month]) ? $noVisit[$month] : 0);
- array_unshift($data['valid'], isset($valid[$month]) ? $valid[$month] : 0);
- array_unshift($data['wait'], isset($wait[$month]) ? $wait[$month] : 0);
- }
- foreach ($data as $k => $val) {
- View::assign($k, json_encode($val));
- }
- // if (!$request->employee->is_manager) {
- // // 业绩排行(个人)
- // $dingTotalNum = Customer::where([['org_id', 'in', $request->org], ['state', '=', '交定'], ['updatetime', 'between', [$firstDay, $lastDay]]])->group('employee_id')->order('num desc')->column('count(id) num', 'employee_id');
- // $signTotalNum = Customer::where([['org_id', 'in', $request->org], ['state', '=', '签单'], ['updatetime', 'between', [$firstDay, $lastDay]]])->group('employee_id')->order('num desc')->column('count(id) num', 'employee_id');
- // $dingNum = $dingTotalNum[$request->employee->id] ?? 0;
- // sort($dingTotalNum);
- // $dingStage = array_search($dingNum, $dingTotalNum);
- // $dingStage != false ?: $dingStage = count($dingTotalNum);
- // $signNum = $signTotalNum[$request->employee->id] ?? 0;
- // sort($signTotalNum);
- // $signStage = array_search($signNum, $signTotalNum);
- // $signStage != false ?: $signStage = count($signTotalNum);
- // $totalDing = array_sum($dingTotalNum);
- // $totalSign = array_sum($signTotalNum);
- // View::assign('dingStage', $dingStage);
- // View::assign('dingNum', $dingNum);
- // View::assign('signStage', $signStage);
- // View::assign('signNum', $signNum);
- // View::assign('totalDing', $totalDing);
- // View::assign('totalSign', $totalSign);
- // } else {
- // 团队
- $dingTotalNum = Customer::where([['org_id', 'in', $request->org], ['state', '=', '交定'], ['updatetime', 'between', [$firstDay, $lastDay]]])->group('employee_id')->order('num desc')->column('employee_id, count(id) num');
- $signTotalNum = Customer::where([['org_id', 'in', $request->org], ['state', '=', '签单'], ['updatetime', 'between', [$firstDay, $lastDay]]])->group('employee_id')->order('num desc')->column('employee_id, count(id) num');
- $employeeIds = array_merge(array_column($dingTotalNum, 'employee_id'), array_column($signTotalNum, 'employee_id'));
- $employeeList = Employee::where([['id', 'in', $employeeIds]])->column('name,uid', 'id');
- foreach ($dingTotalNum as &$ding) {
- if (isset($employeeList[$ding['employee_id']])) {
- $ding['employee_name'] = $employeeList[$ding['employee_id']]['name'];
- } else {
- $ding['employee_name'] = '';
- }
- }
- foreach ($signTotalNum as &$sign) {
- if (isset($employeeList[$sign['employee_id']])) {
- $sign['employee_name'] = $employeeList[$sign['employee_id']]['name'];
- } else {
- $sign['employee_name'] = '';
- }
- }
- View::assign('dingStage', $dingTotalNum);
- View::assign('signStage', $signTotalNum);
- // }
- // View::assign('isManager', $request->employee->is_manager);
- $xinjushang = 0;
- View::assign('xinjushang', $xinjushang);
- return View::fetch();
- }
- /**
- * 退出登陆
- */
- public function logout()
- {
- //Session::clear();
- Session::delete('employee');
- Session::delete('login_type');
- $url = url('login/index');
- Session::delete('qrcode_show');
- $url = session('login_source')==1 ? $domain : $url;
- session::delete('login_source');
- return redirect($url);
- }
- /**
- * 个人资料
- */
- public function info()
- {
- $request = request();
- if (!$request->isAjax()) {
- $empid = $request->employee->id;
- $empobj = Employee::where('id', $empid)->find();
- View::assign('data', $empobj);
- // 微信登陆验证
- $state = rand(1000, 9999);
- View::assign('state', $state);
- $domain = request()->domain();
- View::assign('domain', $domain);
- $appid = strpos($domain, "zqxg.cc") !== false ? config('app.xqweb_appid') : config('app.kfweb_appid');
- View::assign('appid', $appid);
- return View::fetch();
- }
- }
- /**
- * 修改姓名
- */
- public function changename()
- {
- $request = request();
- $name = $request->param('name');
- $empid = $request->employee->id;
- if (Employee::update(['opt_name' => $name], ['id' => $empid])) {
- $this->request->employee['opt_name'] = $name;
- session('employee', $this->request->employee);
- return json(['code' => 0, 'msg' => '姓名更改成功']);
- } else {
- return json(['code' => 1, 'msg' => '姓名更改失败']);
- }
- }
- /**
- * 手机号修改
- */
- public function changephone()
- {
- $request = request();
- $phone = $request->param('phone');
- $empid = $request->employee->id;
- if (Employee::update(['phone' => $phone], ['id' => $empid])) {
- return json(['code' => 0, 'msg' => '手机号更换成功']);
- } else {
- return json(['code' => 1, 'msg' => '手机号已被绑定']);
- }
- }
- /**
- * 微信扫码换绑
- */
- public function wechat()
- {
- $request = request();
- $code = $request->param('code');
- View::assign('code', $code);
- return View::display("<script>parent.bind('" . $code . "');</script>");
- }
- public function wxBind($code)
- {
- $domain = request()->domain();
- if (strpos($domain, "zqxg.cc") !== false) {
- //薛鹊开放平台 web应用
- $appid = config('app.xqweb_appid');
- $secret = config('app.xqweb_secret');
- $field1 = 'xq_web_openid';
- $field2 = 'xq_unionid';
- } else {
- //原有主体开放平台 web应用
- $appid = config('app.kfweb_appid');
- $secret = config('app.kfweb_secret');
- $field1 = 'openid';
- $field2 = 'unionid';
- }
- // 授权获取accesstoken
- $tokendata = (new Oplatform())->getAccessToken($appid, $secret, $code);
- if (!$tokendata) {
- trace('授权获取用户信息', 'error');
- trace($tokendata, 'error');
- return json(['code' => 1, 'msg' => '请求失败,请刷新页面后重试!']);
- }
- if (isset($tokendata['errcode'])) { // code已经被使用
- if (in_array($tokendata['errcode'], [40029, 40163, 41008, 42003])) {
- return json(['code' => 1, 'msg' => '请求失败,请刷新页面后重试!']);
- } else {
- trace('授权获取用户信息', 'error');
- trace($tokendata, 'error');
- return json(['code' => 1, 'msg' => $tokendata['errmsg']]);
- }
- }
- // 拉取用户信息
- $getUserInfoUrl = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $tokendata['access_token'] . '&openid=' . $tokendata['openid'] . '&lang=zh_CN';
- $data = curl($getUserInfoUrl);
- $data = json_decode($data, true);
- if (isset($data['errcode']) || empty($data['openid'])) {
- trace('拉取用户信息', 'error');
- trace($data, 'error');
- return json(['code' => 1, 'msg' => '请求失败']);
- }
- // 更新用户数据
- $empid = request()->employee->id;
- $empobj = Employee::where('id', $empid)->find();
- $empobj->save([
- $field1 => $data['openid'],
- $field2 => $data['unionid'],
- 'nickname' => $data['nickname'],
- 'headimgurl' => $data['headimgurl'],
- ]);
- if ($empobj->uid && $field1 == 'xq_web_openid') {
- User::where('id', $empobj->uid)->update([$field1 => $data['openid'], $field2 => $data['unionid']]);
- }
- return json(['code' => 0, 'msg' => '绑定成功!']);
- }
- /**
- * 多企业选择页面
- */
- public function company()
- {
- $request = request();
- $rootId = $request->param('rootid');
- $phone = session('employee');
- if (is_object($phone)) return redirect(url('index/index'));
- $domain = request()->domain();
- $field = strpos($domain, "zqxg.cc") !== false ? 'phone|xq_unionid' : 'phone|unionid';
- if ($rootId) {
- $employee = Employee::where([[$field, '=', $phone], ['root_id', '=', $rootId], ['grant_id', '<>', 0]])->find();
- if (!empty($employee)) {
- $company = Company::where('root_id', $employee['root_id'])->find();
- if ($company['status'] == 1) {
- return json(['code' => 1, 'msg' => '账号被禁用,请联系管理员']);
- } elseif ($company['end_date'] . ' 23:59:59' < date('Y-m-d H:i:s', time())) {
- return json(['code' => 1, 'msg' => '账号已过期,请联系管理员']);
- }
- session('employee', $employee);
- event('SysOperate', [$employee, '登录']);
- cache('employee_' . $employee['phone'], Session::getId());
- return redirect(url('index/index'));
- $session_field = strpos($domain, "zqxg.cc") !== false ? 'xq_qrcode_show' : 'qrcode_show';
- Session::delete($session_field);
- }
- }
- $companys = Employee::with(['company', 'companys'])->where($field, '=', $phone)->where('grant_id', '<>', 0)->select()->toArray();
- foreach ($companys as $k => $v) {
- if ($v['account_status'] == 1) {
- $companys[$k]['off'] = 1;
- $companys[$k]['off_remark'] = '账号被禁用';
- } elseif ($v['account_end_date'] . ' 23:59:59' < date('Y-m-d H:i:s', time())) {
- $companys[$k]['off'] = 1;
- $companys[$k]['off_remark'] = '账号已过期';
- } else {
- $companys[$k]['off'] = 0;
- $companys[$k]['off_remark'] = '账号正常';
- }
- }
- View::assign('companys', $companys);
- return View::fetch();
- }
- /**
- * 统计查询条件
- */
- public function getStatisticsCondition()
- {
- $param = $this->request->param();
- /* $startCondition = ['addtime', '>', date('Y-m-d H:i:s', 0)];
- $endCondition = ['addtime', '<', date('Y-m-d H:i:s')]; */
- $startCondition = [];
- $endCondition = [];
- if (!empty($param['start_date'])) {
- $startCondition = ['addtime', '>=', date('Y-m-d H:i:s', strtotime($param['start_date']))];
- }
- if (!empty($param['end_date'])) {
- $endCondition = ['addtime', '<=', date('Y-m-d', strtotime($param['end_date'])) . ' 23:59:59'];
- }
- if (empty($param['org_id'])) {
- $param['org_id'] = $this->request->employee['root_id'];
- }
- // 防止跨范围查询部门
- $path = Org::where([['id', '=', $param['org_id']], ['path', 'like', $this->request->employee['root_id'] . '-%']])->value('path');
- if (empty($path)) return [['1', '<>', '1'], ['1', '<>', '1'], ['1', '<>', '1'], ['1', '<>', '1'], $this->request->employee['root_id'] . '-'];
- $orgIdList = Org::where([['path', 'like', $path . '%']])->column('id');
- $orgCondition = ['org_id', 'in', $orgIdList];
- $employeeIdList = Employee::where([$orgCondition, ['state', '=', '在职'], ['uid', '<>', '']])->whereNotNull('uid')->column('id');
- $employeeCondition = ['employee_id', 'in', $employeeIdList];
- return [$startCondition, $endCondition, $employeeCondition, $orgCondition, $path];
- }
- /**
- * 部门列表获取
- */
- public function org()
- {
- $orgList = Org::where([['path', 'like', $this->request->employee['root_id'] . '-%'], ['pid', '<>', 0], ['org_type', 'in', [0, 1]]])->order('pid desc, id asc')->column('id,name,pid');
- $data = [];
- foreach ($orgList as $item) {
- if (isset($data[$item['id']])) {
- $item['child'] = $data[$item['id']];
- unset($data[$item['id']]);
- }
- $data[$item['pid']][] = $item;
- }
- return json(['code' => 0, 'data' => array_pop($data)]);
- }
- /**
- * 签单线型图
- */
- public function signLine()
- {
- $orgid = $this->request->param('org_id', $this->request->employee['root_id']);
- list($startDate, $endDate, $x, $mysqlDateFormat) = $this->getLineContition();
- $employeeCondition = $this->getEmployeeCondition($orgid);
- // Y轴数据
- $rs = CustomerVisitLog::where([CustomerVisitLog::changeState(['state', '=', '签单']), ['confirm_date', '>', $startDate . ' 00:00:00'], ['confirm_date', '<', $endDate . ' 23:59:59'], $employeeCondition])->column("customer_id, DATE_FORMAT(confirm_date, '$mysqlDateFormat') as d");
- $list = [];
- foreach ($rs as $k => $v) {
- $list[$v['d']][] = $v['customer_id'];
- }
- // 数据整合
- $y = [];
- $have_ids = []; // 已有的数据customer_id
- foreach ($x as $d) {
- if (isset($list[$d])) {
- $d_list = array_diff($list[$d], $have_ids);
- $y[] = count($d_list);
- $have_ids = array_merge($have_ids, $list[$d]);
- } else {
- $y[] = 0;
- }
- }
- return json(['code' => 0, 'data' => ['x' => $x, 'y' => $y]]);
- }
- /**
- * 签单数据统计
- */
- public function signData()
- {
- $param = $this->request->param();
- $start_time = date('Y-m-d H:i:s', 0);
- $end_time = date('Y-m-d H:i:s');
- if (!empty($param['start_date'])) {
- $start_time = date('Y-m-d H:i:s', strtotime($param['start_date']));
- }
- if (!empty($param['end_date'])) {
- $end_time = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- }
- $token = request()->employee;
- $org_id = input('org_id', '', 'intval');
- $org_id = $org_id ? $org_id : $token['root_id'];
- $sub_orgs = orgSubIds($token['root_id']);
- if (!in_array($org_id, $sub_orgs)) {
- return json(['code' => 0, 'data' => []]);
- }
- $org_ids = orgSubIds($org_id);
- /* $wuxiao = Customer::changeState('无效', 'chaos');
- $c_where[] = ['org_id', 'in', $org_ids];
- $c_where[] = ['addtime|employee_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]);
- })->count(); */
- $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
- $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state4 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state5 = CustomerVisitLog::changeState('已签单', 'chaos');
- $meet_state = array_merge($state1, $state2, $state3, $state4, $state5);
- /* $customer_list = CustomerVisitLog::where([['state', 'in', $meet_state], ['confirm_date', 'between', [$start_time, $end_time]], ['customer_org_id', 'in', $org_ids]])->group('customer_id')->count(); */
- $c_where[] = ['CustomerVisitLog.state', 'in', $meet_state];
- $c_where[] = ['Customer.org_id', 'in', $org_ids];
- $c_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $customer_list = Db::view('Customer', 'id cid')
- ->view('CustomerVisitLog', 'id lid', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($c_where)
- ->group('Customer.id')
- ->count();
- $data['num'] = $customer_list;
- // 签单数量获取
- $vis_where[] = ['is_merge', '=', 0];
- $vis_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
- $vis_where[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
- $vis_where[] = ['org_id', 'in', $org_ids];
- $customerIdList = CustomerVisitLog::where($vis_where)->column('customer_id');
- $customerIdList = array_unique($customerIdList);
- $data['sign_num'] = count($customerIdList);
- $data['sign_money'] = Customer::where([['id', 'in', $customerIdList]])->sum('signed_money');
- // 本月数据获取
- $thisMonthCount = CustomerVisitLog::where([['org_id', 'in', $org_ids], CustomerVisitLog::changeState(['state', '=', '签单']), ['confirm_date', '>', date('Y-m') . '-01 00:00:00'], ['confirm_date', '<', date('Y-m-d 00:00:00', strtotime('next month'))]])->count();
- // 上月数据获取
- $prevMonthCount = CustomerVisitLog::where([['org_id', 'in', $org_ids], CustomerVisitLog::changeState(['state', '=', '签单']), ['confirm_date', '<', date('Y-m') . '-01 00:00:00'], ['confirm_date', '>', date('Y-m-d 00:00:00', strtotime('first day of previous month'))]])->count();
- $data['month_lv'] = $prevMonthCount == 0 ? ($thisMonthCount * 100) . '%' : round($thisMonthCount / $prevMonthCount * 100, 2) . '%';
- // 今日新增数量
- $data['today'] = CustomerVisitLog::where([['org_id', 'in', $org_ids], CustomerVisitLog::changeState(['state', '=', '签单']), ['confirm_date', 'like', date('Y-m-d') . '%']])->count();
- // 员工数量仅统计销售
- $path = Org::where('id', '=', $org_id)->value('path');
- $noDesignerOrgIdList = Org::where([['path', 'like', $path . '%'], ['org_type', 'in', [0, 1]]])->column('id');
- $data['employee_num'] = Employee::where([['org_id', 'in', $noDesignerOrgIdList], ['state', '=', '在职'], ['uid', '<>', '']])->count();
- $data['sign_lv'] = $data['num'] == 0 ? ($data['sign_num'] * 100) . '%' : round($data['sign_num'] / $data['num'] * 100, 2) . '%';
- $data['sign_avg'] = !empty($data['sign_money']) && !empty($data['sign_num']) ? round($data['sign_money'] / $data['sign_num'], 2) : 0;
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 线型图条件获取
- */
- public function getLineContition()
- {
- $param = $this->request->only(['start_date' => date('Y-m-d', strtotime('first day of next month previous year')), 'end_date' => date('Y-m-d')]);
- $startDataTime = date_create($param['start_date']);
- $endDataTime = date_create($param['end_date']);
- $interval = date_diff($startDataTime, $endDataTime);
- // X轴数据
- $x = [];
- if ($interval->m > 1 || $interval->y > 0) {
- // 按月取数据,初始天按传的那天,结束天按传的那天
- $startDate = $param['start_date']; //date('Y-m-d', strtotime(date('Y-m-01', strtotime($param['start_date']))));
- $endDate = $param['end_date']; //date('Y-m-d');
- $sdate = date_create($startDate);
- $edate = date_create($endDate);
- while ($sdate->format('Y-m') <= $edate->format('Y-m')) {
- $x[] = $sdate->format('Y-m');
- date_add($sdate, date_interval_create_from_date_string('1 month'));
- }
- $mysqlDateFormat = "%Y-%m";
- } else {
- // 按天取数据
- $startDate = $param['start_date'];
- $endDate = $param['end_date'];
- $sdate = date_create($startDate);
- while ($sdate->format('Y-m-d') <= $endDate) {
- $x[] = $sdate->format('m-d');
- date_add($sdate, date_interval_create_from_date_string('1 days'));
- }
- $mysqlDateFormat = "%m-%d";
- }
- return [$startDate, $endDate, $x, $mysqlDateFormat];
- }
- /**
- * 获取员工查询范围
- */
- public function getEmployeeCondition($orgid)
- {
- if (empty($orgid)) $orgid = $this->request->employee['root_id'];
- $path = Org::where([['id', '=', $orgid], ['path', 'like', $this->request->employee['root_id'] . '-%']])->value('path');
- if (empty($path)) {
- $employeeCondition = ['employee_id', 'in', []];
- } else {
- $orgIdList = Org::where([['path', 'like', $path . '%']])->column('id');
- $orgCondition = ['org_id', 'in', $orgIdList];
- $employeeIdList = Employee::where([$orgCondition, ['state', '=', '在职'], ['uid', '<>', '']])->whereNotNull('uid')->column('id');
- $employeeCondition = ['employee_id', 'in', $employeeIdList];
- }
- return $employeeCondition;
- }
- /**
- * 收定线型图
- */
- public function depositLine()
- {
- $root_id = request()->employee['root_id'];
- $orgid = input('org_id', '', 'intval');
- $orgid = $orgid ? $orgid : $root_id;
- list($startDate, $endDate, $x, $mysqlDateFormat) = $this->getLineContition();
- $employeeCondition = $this->getEmployeeCondition($orgid);
- // Y轴数据
- $rs = CustomerVisitLog::where([CustomerVisitLog::changeState(['state', '=', '交定']), ['confirm_date', '>', $startDate . ' 00:00:00'], ['confirm_date', '<', $endDate . ' 23:59:59'], $employeeCondition])->column("customer_id, DATE_FORMAT(confirm_date, '$mysqlDateFormat') as d");
- $list = [];
- foreach ($rs as $k => $v) {
- $list[$v['d']][] = $v['customer_id'];
- }
- // 数据整合
- $y = [];
- $have_ids = []; // 已有的数据customer_id
- foreach ($x as $d) {
- if (isset($list[$d])) {
- $d_list = array_diff($list[$d], $have_ids);
- $y[] = count($d_list);
- $have_ids = array_merge($have_ids, $list[$d]);
- } else {
- $y[] = 0;
- }
- }
- return json(['code' => 0, 'data' => ['x' => $x, 'y' => $y]]);
- }
- /**
- * 收定数据统计
- */
- public function depositData()
- {
- $param = $this->request->param();
- $start_time = date('Y-m-d H:i:s', 0);
- $end_time = date('Y-m-d H:i:s');
- if (!empty($param['start_date'])) {
- $start_time = date('Y-m-d H:i:s', strtotime($param['start_date']));
- }
- if (!empty($param['end_date'])) {
- $end_time = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- }
- $token = request()->employee;
- $org_id = input('org_id', '', 'intval');
- $org_id = $org_id ? $org_id : $token['root_id'];
- $sub_orgs = orgSubIds($token['root_id']);
- if (!in_array($org_id, $sub_orgs)) {
- return json(['code' => 0, 'data' => []]);
- }
- $org_ids = orgSubIds($org_id);
- /* $wuxiao = Customer::changeState('无效', 'chaos');
- $c_where[] = ['org_id', 'in', $org_ids];
- $c_where[] = ['addtime|employee_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]);
- })->count(); */
- $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
- $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
- $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
- $state4 = CustomerVisitLog::changeState('已交定', 'chaos');
- $state5 = CustomerVisitLog::changeState('已签单', 'chaos');
- $meet_state = array_merge($state1, $state2, $state3, $state4, $state5);
- // $customer_list = CustomerVisitLog::where([['state', 'in', $meet_state], ['confirm_date', 'between', [$start_time, $end_time]], ['customer_org_id', 'in', $org_ids]])->group('customer_id')->count();
- $c_where[] = ['CustomerVisitLog.state', 'in', $meet_state];
- $c_where[] = ['Customer.org_id', 'in', $org_ids];
- $c_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $customer_list = Db::view('Customer', 'id cid')
- ->view('CustomerVisitLog', 'id lid', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($c_where)
- ->group('Customer.id')
- ->count();
- $data['num'] = $customer_list;
- // 数量获取
- $vis_where[] = ['is_merge', '=', 0];
- $vis_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
- $vis_where[] = ['state', 'in', CustomerVisitLog::changeState('交定', 'chaos')];
- $vis_where[] = ['org_id', 'in', $org_ids];
- $customerIdList = CustomerVisitLog::where($vis_where)->column('customer_id');
- $customerIdList = array_unique($customerIdList);
- $data['deposit_num'] = count($customerIdList);
- $data['deposit_money'] = Customer::where([['id', 'in', $customerIdList]])->sum('deposit_money');
- // 今日新增数量
- $data['today'] = CustomerVisitLog::whereOr([
- [['org_id', 'in', $org_ids], CustomerVisitLog::changeState(['state', '=', '交定']), ['confirm_date', 'like', date('Y-m-d') . '%']]
- ])->count();
- $data['deposit_lv'] = $data['num'] == 0 ? ($data['deposit_num'] * 100) . '%' : round($data['deposit_num'] / $data['num'] * 100, 2) . '%';
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 见面数据线型图
- */
- public function visitLine()
- {
- $token = request()->employee;
- $org_id = input('org_id', '', 'intval');
- $org_id = $org_id ? $org_id : $token['root_id'];
- $sub_orgs = orgSubIds($token['root_id']);
- if (!in_array($org_id, $sub_orgs)) {
- return json(['code' => 0, 'data' => []]);
- }
- $org_ids = orgSubIds($org_id);
- list($startDate, $endDate, $x, $mysqlDateFormat) = $this->getLineContition();
- // Y轴数据
- $shopState = CustomerVisitLog::changeState('确认到店', 'chaos');
- $presentState = CustomerVisitLog::changeState('确认到场', 'chaos');
- $measureState = CustomerVisitLog::changeState('确认量房', 'chaos');
- $rs = CustomerVisitLog::where([
- ['state', 'in', array_merge($shopState, $presentState, $measureState)],
- ['confirm_date', '>', $startDate . ' 00:00:00'],
- ['confirm_date', '<', $endDate . ' 23:59:59'],
- ['org_id', 'in', $org_ids]
- ])->field(
- "state,customer_id,
- DATE_FORMAT(confirm_date, '$mysqlDateFormat') as d"
- )->select()->toArray();
- $d = [];
- foreach ($rs as $r) {
- if (!isset($d[$r['d']])) {
- $d[$r['d']] = ['shop_num' => 0, 'present_num' => 0, 'measure_num' => 0, 'shop_customer_id' => [], 'present_customer_id' => [], 'measure_customer_id' => []];
- }
- if ($r['state'] == '已到店' && !in_array($r['customer_id'], $d[$r['d']]['shop_customer_id'])) {
- $d[$r['d']]['shop_customer_id'][] = $r['customer_id'];
- $d[$r['d']]['shop_num']++;
- }
- if ($r['state'] == '已到场' && !in_array($r['customer_id'], $d[$r['d']]['present_customer_id'])) {
- $d[$r['d']]['present_customer_id'][] = $r['customer_id'];
- $d[$r['d']]['present_num']++;
- }
- if ($r['state'] == '已量房' && !in_array($r['customer_id'], $d[$r['d']]['measure_customer_id'])) {
- $d[$r['d']]['measure_customer_id'][] = $r['customer_id'];
- $d[$r['d']]['measure_num']++;
- }
- }
- // 数据整合
- $yShop = [];
- $yPresent = [];
- $yMeasure = [];
- foreach ($x as $day) {
- $yShop[] = isset($d[$day]['shop_num']) ? $d[$day]['shop_num'] : 0;
- $yPresent[] = isset($d[$day]['present_num']) ? $d[$day]['present_num'] : 0;
- $yMeasure[] = isset($d[$day]['measure_num']) ? $d[$day]['measure_num'] : 0;
- }
- $y = [$yShop, $yPresent, $yMeasure];
- return json(['code' => 0, 'data' => ['x' => $x, 'y' => $y]]);
- }
- /**
- * 见面数据统计
- */
- public function visitData()
- {
- $param = $this->request->param();
- $start_time = date('Y-m-d H:i:s', 0);
- $end_time = date('Y-m-d H:i:s');
- if (!empty($param['start_date'])) {
- $start_time = date('Y-m-d H:i:s', strtotime($param['start_date']));
- }
- if (!empty($param['end_date'])) {
- $end_time = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- }
- $token = request()->employee;
- $org_id = input('org_id', '', 'intval');
- $org_id = $org_id ? $org_id : $token['root_id'];
- $sub_orgs = orgSubIds($token['root_id']);
- if (!in_array($org_id, $sub_orgs)) {
- return json(['code' => 0, 'data' => []]);
- }
- $org_ids = orgSubIds($org_id);
- $shopState = CustomerVisitLog::changeState('确认到店', 'chaos');
- $presentState = CustomerVisitLog::changeState('确认到场', 'chaos');
- $measureState = CustomerVisitLog::changeState('确认量房', 'chaos');
- $qdState = CustomerVisitLog::changeState('已签单', 'chaos');
- $d = CustomerVisitLog::where([
- ['state', 'in', array_merge($shopState, $presentState, $measureState, $qdState)],
- ['confirm_date', 'between', [$start_time, $end_time]],
- ['org_id', 'in', $org_ids]
- ])->field('id,state,customer_id')->select()->toArray();
- $data = ['confirm_shop_num' => 0, 'confirm_present_num' => 0, 'confirm_measure_num' => 0];
- // 到店数组
- $confirm_shop_arr = [];
- // 到场数组
- $confirm_present_arr = [];
- // 量房数组
- $confirm_measure_arr = [];
- // 签单数组
- $qd_list = [];
- foreach ($d as $r) {
- if (in_array($r['state'], $shopState, true)) {
- $confirm_shop_arr[] = $r;
- }
- if (in_array($r['state'], $presentState, true)) {
- $confirm_present_arr[] = $r;
- }
- if (in_array($r['state'], $measureState, true)) {
- $confirm_measure_arr[] = $r;
- }
- if (in_array($r['state'], $qdState, true)) {
- $qd_list[] = $r;
- }
- }
- //签单数据
- $qd_customer_id = array_column($qd_list, 'customer_id');
- $qd_customer_id = array_values(array_unique(array_filter($qd_customer_id)));
- $data['num'] = count($qd_list);
- $qd_shop_num = 0; //到店签单数
- $qd_present_num = 0; //到场签单数
- $qd_measure_num = 0; //量房签单数
- $confirm_shop_customer_id = array_values(array_unique(array_filter(array_column($confirm_shop_arr, 'customer_id')))); // 到店客户id
- $confirm_present_customer_id = array_values(array_unique(array_filter(array_column($confirm_present_arr, 'customer_id')))); // 到场客户id
- $confirm_measure_customer_id = array_values(array_unique(array_filter(array_column($confirm_measure_arr, 'customer_id')))); // 量房客户id
- $data['confirm_shop_num'] = count($confirm_shop_customer_id);
- $data['confirm_present_num'] = count($confirm_present_customer_id);
- $data['confirm_measure_num'] = count($confirm_measure_customer_id);
- $qd_shop_num = count(array_intersect($confirm_shop_customer_id, $qd_customer_id));
- $qd_present_num = count(array_intersect($confirm_present_customer_id, $qd_customer_id));
- $qd_measure_num = count(array_intersect($confirm_measure_customer_id, $qd_customer_id));
- $data['confirm_shop_lv'] = $data['confirm_shop_num'] == 0 ? 0 : round($qd_shop_num / $data['confirm_shop_num'] * 100, 2);
- $data['confirm_present_lv'] = $data['confirm_present_num'] == 0 ? 0 : round($qd_present_num / $data['confirm_present_num'] * 100, 2);
- $data['confirm_measure_lv'] = $data['confirm_measure_num'] == 0 ? 0 : round($qd_measure_num / $data['confirm_measure_num'] * 100, 2);
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 客户分类统计
- */
- public function customerLevelData()
- {
- list($startCondition, $endCondition,, $orgCondition) = $this->getStatisticsCondition();
- $where[] = $orgCondition;
- if (!empty($startCondition)) {
- $where[] = $startCondition;
- }
- if (!empty($endCondition)) {
- $where[] = $endCondition;
- }
- $state_wuxiao = Customer::changeState('无效', 'chaos');
- $data = Customer::field('
- count(id) as num,
- count(if(level="A",1,null)) as A_num,
- count(if(level="B",1,null)) as B_num,
- count(if(level="C",1,null)) as C_num,
- count(if(level="D",1,null)) as D_num
- ')->where($where)->where([['died', '<>', 2], ['state', 'not in', $state_wuxiao], ['employee_id', '>', 0]])->where(function($query){
- $state1 = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', 'not null', null];
- $or2[] = ['state', 'not in', $state1];
- $or3[] = ['valid_time','NOTNULL',NULL];
- $query->whereOr([$or1, $or2, $or3]);
- })->find();
- // A类客户签单数量
- $data['a_sign_num'] = Customer::where([['level', '=', 'A'], ['state', 'in', Customer::changeState('签单', 'chaos')]])->where($where)->count();
- $data['a_sign_lv'] = $data['A_num'] == 0 ? ($data['a_sign_num'] * 100) . '%' : round($data['a_sign_num'] / $data['A_num'] * 100, 2) . '%';
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 线索线型图
- */
- public function clueLine()
- {
- $orgid = $this->request->param('org_id', $this->request->employee['root_id']);
- list($startDate, $endDate, $x, $mysqlDateFormat) = $this->getLineContition();
- $employeeCondition = $this->getEmployeeCondition($orgid);
- // Y轴数据
- $rs = CustomerClue::where([['addtime', '>', $startDate . ' 00:00:00'], ['addtime', '<', $endDate . ' 23:59:59'], $employeeCondition])->group('d')->field("count(id) as num,count(if(state=1,1,null)) as valid_num,DATE_FORMAT(addtime, '$mysqlDateFormat') as d")->select()->toArray();
- $rs = array_combine(array_column($rs, 'd'), $rs);
- // 数据整合
- $n = [];
- $valid = [];
- foreach ($x as $d) {
- if (isset($rs[$d])) {
- $n[] = $rs[$d]['num'];
- $valid[] = $rs[$d]['valid_num'];
- } else {
- $n[] = 0;
- $valid[] = 0;
- }
- }
- return json(['code' => 0, 'data' => ['x' => $x, 'y' => [$n, $valid]]]);
- }
- /**
- * 线索数据统计
- */
- public function clueData()
- {
- list($startCondition, $endCondition, $employeeCondition) = $this->getStatisticsCondition();
- $where[] = $employeeCondition;
- if (!empty($startCondition)) {
- $where[] = $startCondition;
- }
- if (!empty($endCondition)) {
- $where[] = $endCondition;
- }
- $data = CustomerClue::field('
- count(id) as num,
- count(if(state=1,1,null)) as valid_num,
- count(if(state=2,1,null)) as invalid_num
- ')->where($where)->find();
- $data['valid_lv'] = $data['num'] == 0 ? '0%' : round($data['valid_num'] / $data['num'], 2) * 100 . '%';
- // 今日新增数据
- $data['today'] = CustomerClue::where([$employeeCondition, ['addtime', 'like', date('Y-m-d') . '%']])->count();
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 排行榜
- */
- public function signRank()
- {
- $param = $this->request->param();
- $start_time = date('Y-m-d H:i:s', 0);
- $end_time = date('Y-m-d H:i:s');
- if (!empty($param['start_date'])) {
- $start_time = date('Y-m-d H:i:s', strtotime($param['start_date']));
- }
- if (!empty($param['end_date'])) {
- $end_time = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- }
- $token = request()->employee;
- $org_id = input('org_id', '', 'intval');
- $org_id = $org_id ? $org_id : $token['root_id'];
- $sub_orgs = orgSubIds($token['root_id']);
- if (!in_array($org_id, $sub_orgs)) {
- return json(['code' => 0, 'data' => []]);
- }
- $path = Org::where('id', '=', $org_id)->value('path');
- $orgIdList = Org::where([['path', 'like', $path . '%'], ['org_type', 'in', [0, 1]]])->column('id');
- // 签单数据
- $db_where[] = ['CustomerVisitLog.state', 'in', CustomerVisitLog::changeState('签单', 'chaos')];
- $db_where[] = ['Org.id', 'in', $orgIdList];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['Customer.state', 'NOT NULL', null];
- $visit_log_list = Db::view('CustomerVisitLog', 'id,customer_id')
- ->view('Customer', 'employee_id', 'Customer.id=CustomerVisitLog.customer_id')
- ->view('Org', [], 'Customer.org_id=Org.id')
- ->where($db_where)
- ->select()
- ->toArray();
- $emp_data = [];
- foreach ($visit_log_list as $k => $v) {
- if (empty($emp_data[$v['employee_id']])) {
- $emp_data[$v['employee_id']][] = $v['customer_id'];
- } elseif (!in_array($v['customer_id'], $emp_data[$v['employee_id']])) {
- $emp_data[$v['employee_id']][] = $v['customer_id'];
- }
- }
- $data = [];
- foreach ($emp_data as $k => $v) {
- $data[] = [
- 'employee_id' => $k,
- 'num' => count($v)
- ];
- }
- // 员工编号
- $employeeIdlist = array_column($data, 'employee_id');
- // 总单数据
- $not_sure = Customer::changeState('待确认', 'chaos');
- $wuxiao = Customer::changeState('无效', 'chaos');
- $c_where[] = ['addtime|employee_time', 'between', [$start_time, $end_time]];
- $c_where[] = ['employee_id', 'in', $employeeIdlist];
- $c_where[] = ['state', 'not in', array_merge($not_sure, $wuxiao)];
- $c_where[] = ['died', '<>', 2];
- $customerStatistics = Customer::where($c_where)->group('employee_id')->column('count(id) as num', 'employee_id');
- // 员工数据获取
- $employees = Employee::where([['id', 'in', $employeeIdlist]])->column('name,uid,org_id', 'id');
- $userIdList = array_column($employees, 'uid');
- $users = User::where([['id', 'in', $userIdList]])->column('headimgurl', 'id');
- $orgs = Org::where([['id', 'in', array_column($employees, 'org_id')]])->column('name', 'id');
- foreach ($data as &$item) {
- $item['lv'] = (!isset($customerStatistics[$item['employee_id']]) || $customerStatistics[$item['employee_id']] == 0) ? ($item['num'] * 100) . '%' : round($item['num'] / $customerStatistics[$item['employee_id']] * 100, 2) . '%';
- $item['name'] = $employees[$item['employee_id']]['name'];
- $item['headimgurl'] = $users[$employees[$item['employee_id']]['uid']];
- $item['org_name'] = $orgs[$employees[$item['employee_id']]['org_id']];
- }
- array_multisort(array_column($data, 'num'), SORT_DESC, $data);
- return json(['code' => 0, 'data' => $data]);
- }
- public function depositRank()
- {
- $param = $this->request->param();
- $start_time = date('Y-m-d H:i:s', 0);
- $end_time = date('Y-m-d H:i:s');
- if (!empty($param['start_date'])) {
- $start_time = date('Y-m-d H:i:s', strtotime($param['start_date']));
- }
- if (!empty($param['end_date'])) {
- $end_time = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- }
- $token = request()->employee;
- $org_id = input('org_id', '', 'intval');
- $org_id = $org_id ? $org_id : $token['root_id'];
- $sub_orgs = orgSubIds($token['root_id']);
- if (!in_array($org_id, $sub_orgs)) {
- return json(['code' => 0, 'data' => []]);
- }
- $path = Org::where('id', '=', $org_id)->value('path');
- $orgIdList = Org::where([['path', 'like', $path . '%'], ['org_type', 'in', [0, 1]]])->column('id');
- $db_where[] = ['CustomerVisitLog.state', 'in', CustomerVisitLog::changeState('交定', 'chaos')];
- $db_where[] = ['Org.id', 'in', $orgIdList];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['Customer.state', 'NOT NULL', null];
- $visit_log_list = Db::view('CustomerVisitLog', 'id,customer_id')
- ->view('Customer', 'employee_id', 'Customer.id=CustomerVisitLog.customer_id')
- ->view('Org', [], 'Customer.org_id=Org.id')
- ->where($db_where)
- ->select()
- ->toArray();
- $emp_data = [];
- foreach ($visit_log_list as $k => $v) {
- if (empty($v['employee_id'])) {
- continue;
- }
- if (empty($emp_data[$v['employee_id']])) {
- $emp_data[$v['employee_id']][] = $v['customer_id'];
- } elseif (!in_array($v['customer_id'], $emp_data[$v['employee_id']])) {
- $emp_data[$v['employee_id']][] = $v['customer_id'];
- }
- }
- $data = [];
- foreach ($emp_data as $k => $v) {
- $data[] = [
- 'employee_id' => $k,
- 'num' => count($v)
- ];
- }
- // 员工编号
- $employeeIdlist = array_column($data, 'employee_id');
- // 总单数据
- $not_sure = Customer::changeState('待确认', 'chaos');
- $wuxiao = Customer::changeState('无效', 'chaos');
- $c_where[] = ['addtime|employee_time', 'between', [$start_time, $end_time]];
- $c_where[] = ['employee_id', 'in', $employeeIdlist];
- $c_where[] = ['state', 'not in', array_merge($not_sure, $wuxiao)];
- $c_where[] = ['died', '<>', 2];
- $customerStatistics = Customer::where($c_where)->group('employee_id')->column('count(id) as num', 'employee_id');
- // 员工数据获取
- $employees = Employee::where([['id', 'in', $employeeIdlist]])->column('name,uid,org_id', 'id');
- $userIdList = array_column($employees, 'uid');
- $users = User::where([['id', 'in', $userIdList]])->column('headimgurl', 'id');
- $orgs = Org::where([['id', 'in', array_column($employees, 'org_id')]])->column('name', 'id');
- foreach ($data as &$item) {
- $item['lv'] = (!isset($customerStatistics[$item['employee_id']]) || $customerStatistics[$item['employee_id']] == 0) ? ($item['num'] * 100) . '%' : round($item['num'] / $customerStatistics[$item['employee_id']] * 100, 2) . '%';
- $item['name'] = $employees[$item['employee_id']]['name'] ?? '';
- $item['headimgurl'] = isset($employees[$item['employee_id']]) && isset($users[$employees[$item['employee_id']]['uid']]) ? $users[$employees[$item['employee_id']]['uid']] : '';
- $item['org_name'] = isset($employees[$item['employee_id']]) && isset($orgs[$employees[$item['employee_id']]['org_id']]) ? $orgs[$employees[$item['employee_id']]['org_id']] : '';
- }
- array_multisort(array_column($data, 'num'), SORT_DESC, $data);
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 跟进数据统计
- */
- public function followUpData()
- {
- $param = $this->request->param();
- $start_time = date('Y-m-d H:i:s', 0);
- $end_time = date('Y-m-d H:i:s');
- if (!empty($param['start_date'])) {
- $start_time = date('Y-m-d H:i:s', strtotime($param['start_date']));
- }
- if (!empty($param['end_date'])) {
- $end_time = date('Y-m-d 23:59:59', strtotime($param['end_date']));
- }
- $token = request()->employee;
- $org_id = input('org_id', '', 'intval');
- $org_id = $org_id ? $org_id : $token['root_id'];
- $sub_orgs = orgSubIds($token['root_id']);
- if (!in_array($org_id, $sub_orgs)) {
- return json(['code' => 0, 'data' => []]);
- }
- $org_ids = orgSubIds($org_id);
- $shopState = CustomerVisitLog::changeState('确认到店', 'chaos');
- $presentState = CustomerVisitLog::changeState('确认到场', 'chaos');
- $measureState = CustomerVisitLog::changeState('确认量房', 'chaos');
- $signState = CustomerVisitLog::changeState('签单', 'chaos');
- $depositState = CustomerVisitLog::changeState('交定', 'chaos');
- $dataAll = CustomerVisitLog::where([['state', 'in', array_merge($shopState, $presentState, $measureState, $signState, $depositState)], ['org_id', 'in', $org_ids], ['confirm_date', 'between', [$start_time, $end_time]]])->field('id,state')->select();
- $data = ['shop_num' => 0, 'present_num' => 0, 'measure_num' => 0, 'deposit_num' => 0, 'sign_num' => 0];
- foreach ($dataAll as $d) {
- if ($d['state'] == '已到店') $data['shop_num']++;
- if ($d['state'] == '已到场') $data['present_num']++;
- if ($d['state'] == '已量房') $data['measure_num']++;
- if ($d['state'] == '已交定') $data['deposit_num']++;
- if ($d['state'] == '已签单') $data['sign_num']++;
- }
- $wuxiao = Customer::changeState('无效', 'chaos');
- $c_where[] = ['org_id', 'in', $org_ids];
- $c_where[] = ['addtime|employee_time', 'between', [$start_time, $end_time]];
- $c_where[] = ['state', 'NOT NULL', null];
- $c_where[] = ['state', 'not in', $wuxiao];
- $c_where[] = ['died', '<>', 2];
- $valid_num = 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]);
- })->count();
- $data['valid_num'] = $valid_num;
- return json(['code' => 0, 'data' => $data]);
- }
- /**
- * 切换店面
- */
- public function switch_shop()
- {
- $request = request();
- $rootId = $request->param('rootid');
- $phone = $request->param('phone');
- if ($rootId) {
- $employee = Employee::where([['phone|openid', '=', $phone], ['root_id', '=', $rootId], ['grant_id', '<>', 0]])->find();
- if (!empty($employee)) {
- $company = Company::where('root_id', $employee['root_id'])->find();
- if ($company['status'] == 1) {
- return json(['code' => 1, 'msg' => '账号被禁用,请联系管理员']);
- } elseif ($company['end_date'] . ' 23:59:59' < date('Y-m-d H:i:s', time())) {
- return json(['code' => 1, 'msg' => '账号已过期,请联系管理员']);
- }
- session('employee', $employee);
- event('SysOperate', [$employee, '登录']);
- cache('employee_' . $employee['phone'], Session::getId());
- $domain = request()->domain();
- Session::delete(strpos($domain, "zqxg.cc") !== false ? 'xq_qrcode_show' : 'qrcode_show');
- return json(['code' => 0, 'msg' => '切换店面成功']);
- }
- }
- return json(['code' => 1, 'msg' => '切换失败']);
- }
- }
|