| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- <?php
- // +----------------------------------------------------------------------
- // | Description: 商业智能-排行榜
- // +----------------------------------------------------------------------
- // | Author: Michael_xu | gengxiaoxu@5kcrm.com
- // +----------------------------------------------------------------------
-
- namespace app\bi\controller;
-
- use app\admin\controller\ApiCommon;
- use app\bi\traits\SortTrait;
- use think\Hook;
- use think\Request;
- use think\Db;
- use app\bi\model\Customer as CustomerModel;
- use app\bi\model\Contract as ContractModel;
- use app\bi\logic\ExcelLogic;
-
- class Ranking extends ApiCommon
- {
- use SortTrait;
-
- /**
- * 用于判断权限
- * @permission 无限制
- * @allow 登录用户可访问
- * @other 其他根据系统设置
- **/
- public function _initialize()
- {
- $action = [
- 'permission' => [''],
- 'allow' => [
- 'contract',
- 'receivables',
- 'signing',
- 'addcustomer',
- 'addcontacts',
- 'recordnun',
- 'recordcustomer',
- 'examine',
- 'product',
- 'excelexport'
- ]
- ];
- Hook::listen('check_auth', $action);
- $request = Request::instance();
- $a = strtolower($request->action());
- if (!in_array($a, $action['permission'])) {
- parent::_initialize();
- }
- if (!checkPerByAction('bi', 'ranking', 'read')) {
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode(['code' => 102, 'error' => '无权操作']));
- }
- }
-
- /**
- * 合同金额排行
- * @param
- * @return
- * @author Michael_xu
- */
- public function contract($param = '')
- {
- if($param['excel_type']!=1){
- $param = $this->param;
- }
-
- if (!empty($param['start_time'])) $param['start_time'] =$param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
-
- $whereArr = $this->com($param, 'contract');
- $whereArr['check_status'] = 2;
-
- //导出使用
- if (!empty($param['excel_type'])) {
- $data=$this->handel(
- new \app\bi\model\Contract,
- $whereArr,
- ['field' => 'SUM(`money`)', 'alias' => 'money', 'default' => '0.00'],
- $param['excel_type']
- );
- return $data;
- }
-
-
- return $this->handel(
- new \app\bi\model\Contract,
- $whereArr,
- ['field' => 'SUM(`money`)', 'alias' => 'money', 'default' => '0.00']
- );
- }
-
- /**
- * 回款金额排序
- * @return
- */
- public function receivables($param = '')
- {
- if($param['excel_type']!=1){
- $param = $this->param;
- }
- if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
- $whereArr = $this->com($param, 'receivables');
- $whereArr['check_status'] = 2;
- //导出使用
- if (!empty($param['excel_type'])) {
- $data=$this->handel(
- new \app\bi\model\Receivables,
- $whereArr,
- ['field' => 'SUM(`money`)', 'alias' => 'money', 'default' => '0.00'],
- $param['excel_type']
- );
- return $data;
- }
- return $this->handel(
- new \app\bi\model\Receivables,
- $whereArr,
- ['field' => 'SUM(`money`)', 'alias' => 'money', 'default' => '0.00']
- );
- }
-
- /**
- * 签约合同排序
- * @return
- */
- public function signing($param = '')
- {
- if($param['excel_type']!=1){
- $param = $this->param;
- }
- if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
- $whereArr = $this->com($param, 'contract');
- // $whereArr['check_status'] = 2;
-
- //导出使用
- if (!empty($param['excel_type'])) {
- $data= $this->handel(
- new ContractModel,
- $whereArr,
- ['field' => 'COUNT(*)', 'alias' => 'count', 'default' => 0],
- $param['excel_type']
- );
- return $data;
- }
-
- return $this->handel(
- new ContractModel,
- $whereArr,
- ['field' => 'COUNT(*)', 'alias' => 'count', 'default' => 0]
- );
- }
-
- /**
- * 新增客户排序
- * @return
- */
- public function addCustomer($param = '')
- {
- if($param['excel_type']!=1){
- $param = $this->param;
- }
- if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
- $whereArr = $this->com($param, 'customer');
-
- $poolWhere = $this->getWhereByPool();
- $poolId = db('crm_customer')->alias('customer')->where($poolWhere)->column('customer_id');
- if (!empty($poolId)) $whereArr['customer_id'] = ['notin', $poolId];
-
- //导出使用
- if (!empty($param['excel_type'])) return $this->handel(
- new \app\bi\model\Customer,
- $whereArr,
- ['field' => 'COUNT(*)', 'alias' => 'count', 'default' => 0],
- $param['excel_type']
- );
- return $this->handel(
- new \app\bi\model\Customer,
- $whereArr,
- ['field' => 'COUNT(*)', 'alias' => 'count', 'default' => 0]
- );
- }
-
- /**
- * 新增联系人排序
- * @return
- */
- public function addContacts()
- {
- $param = $this->param;
- if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
- $whereArr = $this->com($param, 'contacts');
-
- //导出使用
- if (!empty($param['excel_type'])) return $this->handel(
- new \app\bi\model\Contacts,
- $whereArr,
- ['field' => 'COUNT(*)', 'alias' => 'count', 'default' => 0],
- $param['excel_type']
- );
- return $this->handel(
- new \app\bi\model\Contacts,
- $whereArr,
- ['field' => 'COUNT(*)', 'alias' => 'count', 'default' => 0]
- );
- }
-
- /**
- * 跟进次数排行
- *
- * @param string $param
- * @return array|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function recordNun($param = '')
- {
- if($param['excel_type']!=1){
- $param = $this->param;
- }
- if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
- $whereArr = $this->com($param, 'record');
-
- # 权限内的员工列表
- $userData = [];
- $userList = db('admin_user')->alias('user')
- ->field(['user.id', 'user.realname AS user_name', 'structure.name AS structure_name'])
- ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id')
- ->whereIn('user.id', $whereArr['create_user_id'][1])->select();
- foreach ($userList AS $key => $value) {
- $userData[$value['id']]['user_name'] = $value['user_name'];
- $userData[$value['id']]['structure_name'] = $value['structure_name'];
- }
-
- # 跟进记录列表
- $data = [];
- $recordWhere['type'] = 1;
- $recordWhere['activity_type'] = 2;
- $recordWhere['create_user_id'] = ['in', $whereArr['create_user_id'][1]];
- $recordWhere['create_time'] = ['between', [$whereArr['create_time'][1][0], $whereArr['create_time'][1][1]]];
- $recordList = db('crm_activity')->field(['count(*) as count', 'create_user_id'])->where($recordWhere)
- ->group('create_user_id')->order('count', 'desc')->select();
-
- foreach ($recordList AS $key => $value) {
- $data[] = [
- 'count' => $value['count'],
- 'user_name' => $userData[$value['create_user_id']]['user_name'],
- 'structure_name' => $userData[$value['create_user_id']]['structure_name']
- ];
- }
-
- //导出使用
- if (!empty($param['excel_type'])) return $data;
-
- return resultArray(['data' => $data]);
- }
-
- /**
- * 跟进客户数排行
- *
- * @param string $param
- * @return mixed|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function recordCustomer($param = '')
- {
- if($param['excel_type']!=1){
- $param = $this->param;
- }
- if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] =$param['end_time'] . ' 23:59:59';
- $whereArr = $this->com($param, 'record');
-
- # 权限内的员工列表
- $userData = [];
- $userList = db('admin_user')->alias('user')
- ->field(['user.id', 'user.realname AS user_name', 'structure.name AS structure_name'])
- ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id')
- ->whereIn('user.id', $whereArr['create_user_id'][1])->select();
- foreach ($userList AS $key => $value) {
- $userData[$value['id']]['user_name'] = $value['user_name'];
- $userData[$value['id']]['structure_name'] = $value['structure_name'];
- }
-
- # 跟进记录列表
- $data = [];
- $recordWhere['type'] = 1;
- $recordWhere['activity_type'] = 2;
- $recordWhere['create_user_id'] = ['in', $whereArr['create_user_id'][1]];
- $recordWhere['create_time'] = ['between', [$whereArr['create_time'][1][0], $whereArr['create_time'][1][1]]];
- $recordList = db('crm_activity')->field(['create_user_id', 'activity_type_id'])->where($recordWhere)
- ->group('create_user_id, activity_type_id')->select();
-
- foreach ($recordList AS $key => $value) {
- if (empty($data[$value['create_user_id']]['user_name'])) {
- $data[$value['create_user_id']]['user_name'] = $userData[$value['create_user_id']]['user_name'];
- $data[$value['create_user_id']]['structure_name'] = $userData[$value['create_user_id']]['structure_name'];
- $data[$value['create_user_id']]['count'] = 1;
- } else {
- $data[$value['create_user_id']]['count'] = $data[$value['create_user_id']]['count'] + 1;
- }
- }
-
- $data = $this->sortCommon($data, 'count', 'desc');
-
- //导出使用
- if (!empty($param['excel_type'])) return $data;
-
- return resultArray(['data' => $data]);
- }
-
- /**
- * 出差次数排行
- * @return
- */
- public function examine($param = '')
- {
- if($param['excel_type']!=1){
- $param = $this->param;
- }
- if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
- $whereArr = $this->com($param, 'record');
- $whereArr['category_id'] = 3; // 审批类型,3出差
- $whereArr['check_status'] = 2;
-
- //导出使用
- if (!empty($param['excel_type'])) return $this->handel(
- new \app\bi\model\Examine,
- $whereArr,
- ['field' => 'COUNT(*)', 'alias' => 'count', 'default' => 0],
- $param['excel_type'],
- 'create_user_id'
- );
-
- return $this->handel(
- new \app\bi\model\Examine,
- $whereArr,
- ['field' => 'COUNT(*)', 'alias' => 'count', 'default' => 0],
- '',
- 'create_user_id'
- );
- }
-
- /**
- * 产品销量排行
- * @return
- */
- public function product($param = '')
- {
- $userModel = new \app\admin\model\User();
- $productModel = new \app\bi\model\Product();
- if($param['excel_type']!=1){
- $param = $this->param;
- }
- if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
- if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
- $list = $productModel->getSortByProduct($param);
- $list = array_column($list, null, 'owner_user_id');
-
- $whereArr = $this->com($param, 'contract');
-
- $data = [];
- foreach ($whereArr['owner_user_id'][1] as $val) {
- $user = $userModel->getUserById($val);
- $item = [];
- $item['num'] = !empty($list[$val]['num']) ? (int)$list[$val]['num'] : 0;
- $item['user_name'] = $user['realname'];
- $item['structure_name'] = $user['structure_name'];
- $data[] = $item;
- }
-
- # 排序
- if (!empty($data)) $data = $this->sortCommon($data, 'num', 'desc');
- //导出使用
- if (!empty($param['excel_type'])) return $data;
-
- return resultArray(['data' => $data]);
- }
-
- /**
- * 查询条件
- * @return
- */
- private function com($param, $type = '')
- {
- $userModel = new \app\admin\model\User();
- $adminModel = new \app\admin\model\Admin();
- $perUserIds = $userModel->getUserByPer('bi', 'ranking', 'read'); //权限范围内userIds
- $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
- $userIds = $whereData['userIds'];
- $between_time = $whereData['between_time'];
- if ($type == 'contract') {
- $where_time = 'order_date';
- } elseif (in_array($type, ['record', 'customer', 'contacts'])) {
- $where_time = 'create_time';
- } elseif ($type == 'receivables') {
- $where_time = 'return_time';
- } else {
- $where_time = 'start_time';
- }
- //时间戳:新增客户排行
- if ($type == 'contract' || $type == 'receivables') {
- $whereArr[$where_time] = array('between', array(date('Y-m-d', $between_time[0]), date('Y-m-d', $between_time[1])));
- } else {
- $whereArr[$where_time] = array('between', array($between_time[0], $between_time[1]));
- }
-
- if (in_array($type, ['customer', 'contract', 'receivables', 'contacts'])) {
- $whereArr['owner_user_id'] = ['IN', $userIds];
- } else {
- $whereArr['create_user_id'] = ['IN', $userIds];
- }
-
- return $whereArr;
- }
-
- /**
- * 查询统计数据
- *
- * @param model $model
- * @param array $whereArr
- * @return void
- * @author Ymob
- * @datetime 2019-11-25 11:11:59
- */
- private function handel($model, $whereArr, $field, $excel_type = '', $user_field = 'owner_user_id')
- {
- $userModel = new \app\admin\model\User();
- $sql = $model->field([
- $user_field,
- $field['field'] => $field['alias']
- ])
- ->where($whereArr)
- ->group($user_field)
- ->fetchSql()
- ->select();
-
- $list = queryCache($sql);
- $list = array_column($list, null, $user_field);
- $data = [];
-
- foreach ($whereArr[$user_field][1] as $val) {
- $user = $userModel->getUserById($val);
- $item = [];
- $item[$field['alias']] = $list[$val][$field['alias']] ?: $field['default'];
- $item['user_name'] = $user['realname'];
- $item['structure_name'] = $user['structure_name'];
- $data[] = $item;
- }
- array_multisort($data, SORT_DESC, array_column($data, $field['alias']));
- if (!empty($excel_type)) return $data;
-
- return resultArray(['data' => $data]);
- }
-
- /**
- * 导出
- * @param $type
- * @param $types
- */
- public function excelExport()
- {
- $param = $this->param;
- $excel_type = $param['excel_type'];
- $type = [];
- $type['excel_types'] = $param['excel_types'];
- switch ($param['excel_types']) {
- case 'contract':
- $list = $this->contract($param);
- $param['type'] = '合同金额排行';
- break;
- case 'receivablesRanKingExport':
- $list = $this->receivables($param);
- $param['type'] = '回款金额排行';
- break;
- case 'signing':
- $list = $this->signing($param);
- $param['type'] = '签约合同排行';
- break;
- case 'product':
- $list = $this->product($param);
- $param['type'] = '产品销量排行';
- break;
- case 'addCustomer':
- $list = $this->addCustomer($param);
- $param['type'] = '新增客户数排行';
- break;
- case 'addContacts':
- $list = $this->addContacts($param);
- $param['type'] = '新增联系人数排行';
- break;
- case 'recordNun':
- $list = $this->recordNun($param);
- $param['type'] = '跟进次数排行';
- break;
- case 'recordCustomer':
- $list = $this->recordCustomer($param);
- $param['type'] = '跟进客户数排行';
- break;
- case 'examine':
- $list = $this->examine($param);
- $param['type'] = '出差次数排行';
- break;
- }
- if(empty($list)){
- return resultArray(['data'=>'数据不存在']);
- }
- $excelLogic = new ExcelLogic();
- foreach ($list as $key => $v) {
- $list[$key]['id'] = $key + 1;
- }
- $data = $excelLogic->rankingExcle($param, $list);
- return $data;
- }
-
- /**
- * [客户公海条件]
- * @author Michael_xu
- * @param
- * @return
- */
- public function getWhereByPool()
- {
- $configModel = new \app\crm\model\ConfigData();
- $configInfo = $configModel->getData();
- $config = $configInfo['config'] ? : 0;
- $follow_day = $configInfo['follow_day'] ? : 0;
- $deal_day = $configInfo['deal_day'] ? : 0;
- $whereData = [];
- //启用
- if ($config == 1) {
- //默认公海条件(没有负责人或已经到期)
- $data['follow_time'] = time()-$follow_day*86400;
- $data['deal_time'] = time()-$deal_day*86400;
- $data['deal_status'] = '未成交';
- if ($follow_day < $deal_day) {
- $whereData = function($query) use ($data){
- $query->where(['customer.owner_user_id'=>0])
- ->whereOr(function ($query) use ($data) {
- $query->where(function ($query) use ($data) {
- $query->where(['customer.update_time' => array('elt',$data['follow_time'])])
- ->whereOr(['customer.deal_time' => array('elt',$data['deal_time'])]);
- })
- ->where(['customer.is_lock' => 0])
- ->where(['customer.deal_status' => ['neq','已成交']]);
- });
- };
- } else {
- $whereData = function($query) use ($data){
- $query->where(['customer.owner_user_id'=>0])
- ->whereOr(function ($query) use ($data) {
- $query->where(function ($query) use ($data) {
- $query->where(['customer.deal_time' => array('elt',$data['deal_time'])]);
- })
- ->where(['customer.is_lock' => 0])
- ->where(['customer.deal_status' => ['neq','已成交']]);
- });
- };
- }
- } else {
- $whereData['customer.owner_user_id'] = 0;
- }
- return $whereData ? : '';
- }
- }
|