You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wkcrm/application/crm/model/Index.php

293 lines
11 KiB

4 years ago
<?php
// +----------------------------------------------------------------------
// | Description: CRM工作台
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com
// +----------------------------------------------------------------------
namespace app\crm\model;
use think\Db;
use app\admin\model\Common;
use think\Request;
use think\Validate;
use think\helper\Time;
class Index extends Common
{
4 years ago
/**
* 销售简报
* @param
* @return
* @author Michael_xu
*/
public function getSalesData($param)
{
$where = array();
$start_time = $param['start_time'];
$where['create_time'] = Time::today();
}
public function getQueryRepeat($type, $content)
4 years ago
{
4 years ago
$result = [];
4 years ago
$customerList = [];
4 years ago
$poolList = [];
$leadsList = [];
4 years ago
# 客户列表
$customerList = $this->getCustomerList($type, $content);
# 公海列表
if (count($customerList) < 10) $poolList = $this->getPoolList($type, $content);
# 线索列表
if (count($customerList) + count($poolList) < 10) $leadsList = $this->getLeadsList($type, $content);
# 处理客户列表数据
4 years ago
foreach ($customerList as $key => $value) {
4 years ago
$ownerUserName = !empty($value['owner_user_id']) ? db('admin_user')->where('id', $value['owner_user_id'])->value('realname') : '';
4 years ago
if (!empty($ownerUserName)) {
$result[] = [
'id' => $value['customer_id'],
'name' => $value['name'],
'create_time' => date('Y-m-d H:i:s', $value['create_time']),
'mobile' => !empty($value['mobile']) ? $value['mobile'] : '',
'telephone' => !empty($value['telephone']) ? $value['telephone'] : '',
'last_time' => !empty($value['deal_time']) ? date('Y-m-d H:i:s', $value['deal_time']) : '',
'owner_user_name' => $ownerUserName,
'module' => '客户模块',
'type' => 2
];
}
4 years ago
}
# 处理公海列表数据
4 years ago
foreach ($poolList as $key => $value) {
4 years ago
$ownerUserName = !empty($value['owner_user_id']) ? db('admin_user')->where('id', $value['owner_user_id'])->value('realname') : '';
4 years ago
4 years ago
$result[] = [
4 years ago
'id' => $value['customer_id'],
'name' => $value['name'],
'create_time' => date('Y-m-d H:i:s', $value['create_time']),
'mobile' => !empty($value['mobile']) ? $value['mobile'] : '',
'telephone' => !empty($value['telephone']) ? $value['telephone'] : '',
'last_time' => !empty($value['deal_time']) ? date('Y-m-d H:i:s', $value['deal_time']) : '',
4 years ago
'owner_user_name' => $ownerUserName,
4 years ago
'module' => '公海模块',
'type' => 9,
// guogaobo 公海数据权限
'poolAuthList' => [
'receive' => true,
'excelexport' => true,
'poolId' => $value['customer_id'], // guogaobo 多公海使用 公海类型id
'index' => true,
'distribute' => true,
'delete' => true
]
4 years ago
];
}
# 处理线索模块数据
4 years ago
foreach ($leadsList as $key => $value) {
4 years ago
$ownerUserName = !empty($value['owner_user_id']) ? db('admin_user')->where('id', $value['owner_user_id'])->value('realname') : '';
4 years ago
4 years ago
$result[] = [
4 years ago
'id' => $value['leads_id'],
'name' => $value['name'],
'create_time' => date('Y-m-d H:i:s', $value['create_time']),
'mobile' => !empty($value['mobile']) ? $value['mobile'] : '',
'telephone' => !empty($value['telephone']) ? $value['telephone'] : '',
'last_time' => '',
4 years ago
'owner_user_name' => $ownerUserName,
4 years ago
'module' => '线索模块',
'type' => 1
4 years ago
];
}
return $result;
}
/**
* 获取客户列表
*
* @param $type
* @param $content
* @return bool|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function getCustomerList($type, $content)
4 years ago
{
# 默认条件
$customerWhere = $this->getCustomerWhere();
# 查询条件
$searchWhere = $this->getSearchWhere($type, $content);
# 查询字段
$field = ['customer_id', 'name', 'create_time', 'owner_user_id', 'deal_time', 'telephone', 'mobile'];
return db('crm_customer')->alias('customer')->field($field)->where($customerWhere)
->where($searchWhere)->limit(10)->order('update_time', 'desc')->select();
}
4 years ago
/**
* 获取公海客户列表
*
* @param $type
* @param $content
* @return bool|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function getPoolList($type, $content)
4 years ago
{
# 公海条件
$poolWhere = $this->getPoolWhere();
# 查询条件
$searchWhere = $this->getSearchWhere($type, $content);
# 查询字段
$field = ['customer_id', 'name', 'create_time', 'owner_user_id', 'deal_time', 'telephone', 'mobile'];
return db('crm_customer')->alias('customer')->field($field)->where($poolWhere)
->where($searchWhere)->limit(10)->order('update_time', 'desc')->select();
}
4 years ago
/**
* 获取线索列表
*
* @param $type
* @param $content
* @return bool|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
private function getLeadsList($type, $content)
4 years ago
{
# 查询条件
$searchWhere = $this->getSearchWhere($type, $content);
# 查询字段
$field = ['leads_id', 'name', 'telephone', 'mobile', 'owner_user_id', 'create_time', 'is_transform'];
return db('crm_leads')->field($field)->where($searchWhere)->where('is_transform', 0)->limit(10)
->order('update_time', 'desc')->select();
}
4 years ago
/**
* 获取查询条件
*
* @param $type
* @param $content
* @return array|\Closure
*/
private function getSearchWhere($type, $content)
4 years ago
{
$searchWhere = [];
# 查询客户名称
if ($type == 'name') {
$searchWhere = function ($query) use ($content) {
$query->where('name', 'like', '%' . $content . '%');
};
4 years ago
}
4 years ago
# 查询手机或电话
if ($type == 'phone') {
$searchWhere = function ($query) use ($content) {
$query->where(function ($query) use ($content) {
$query->whereOr('telephone', $content);
$query->whereOr('mobile', $content);
});
};
}
return $searchWhere;
}
4 years ago
/**
* [客户公海条件]
* @param
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
private function getPoolWhere()
4 years ago
{
$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', '已成交']]);
});
};
4 years ago
} else {
4 years ago
$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', '已成交']]);
});
};
4 years ago
}
4 years ago
} else {
$whereData['customer.owner_user_id'] = 0;
4 years ago
}
4 years ago
return $whereData ?: [];
}
4 years ago
/**
* [客户默认条件]
* @param
* @return
4 years ago
* @author Michael_xu
4 years ago
*/
private function getCustomerWhere()
4 years ago
{
$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;
//默认条件(没有到期或已锁定)
$data['follow_time'] = time() - $follow_day * 86400;
$data['deal_time'] = time() - $deal_day * 86400;
if ($config == 1) {
if ($follow_day < $deal_day) {
$whereData = function ($query) use ($data) {
$query->where(function ($query) use ($data) {
$query->where(['customer.update_time' => array('gt', $data['follow_time']), 'customer.deal_time' => array('gt', $data['deal_time'])]);
})
->whereOr(['customer.deal_status' => '已成交'])
->whereOr(['customer.is_lock' => 1]);
};
} else {
$whereData = function ($query) use ($data) {
$query->where(function ($query) use ($data) {
$query->where(['customer.deal_time' => array('gt', $data['deal_time'])]);
})
->whereOr(['customer.deal_status' => '已成交'])
->whereOr(['customer.is_lock' => 1]);
};
4 years ago
}
}
4 years ago
return $whereData ?: [];
}
4 years ago
}