@ -12,9 +12,7 @@ use app\admin\model\ActionRecord;
use app\admin\model\Common;
use app\admin\model\Common;
use app\admin\model\Field;
use app\admin\model\Field;
use app\admin\model\File;
use app\admin\model\File;
use app\admin\model\Message;
use app\admin\model\Record;
use app\admin\model\Record;
use app\admin\model\User;
use app\crm\model\CustomerConfig;
use app\crm\model\CustomerConfig;
use PDOStatement;
use PDOStatement;
use think\Collection;
use think\Collection;
@ -27,9 +25,9 @@ class CustomerPoolLogic extends Common
* 公海列表
* 公海列表
*
*
* @param $param
* @param $param
* @return array
* @since 2021-04-14
* @author fanqi
* @author fanqi
* @since 2021-04-14
* @return array
*/
*/
public function getPoolList($param)
public function getPoolList($param)
{
{
@ -48,10 +46,10 @@ class CustomerPoolLogic extends Common
$searchMap = [];
$searchMap = [];
if ($param['search'] == '0' || !empty($param['search'])) {
if ($param['search'] == '0' || !empty($param['search'])) {
$search = $param['search'];
$search = $param['search'];
$searchMap = function ($query) use ($search) {
$searchMap = function($query) use ($search) {
$query->where('customer.name', array('like', '%' . $search . '%'))
$query->where('customer.name',array('like','%'.$search.'%'))
->whereOr('customer.mobile', array('like', '%' . $search . '%'))
->whereOr('customer.mobile',array('like','%'.$search.'%'))
->whereOr('customer.telephone', array('like', '%' . $search . '%'));
->whereOr('customer.telephone',array('like','%'.$search.'%'));
};
};
}
}
@ -62,11 +60,11 @@ class CustomerPoolLogic extends Common
}
}
// 公海条件
// 公海条件
if ($param['is_excel'] == 1 & & !empty($param['customer_id'])) {
if ($param['is_excel'] == 1 & & !empty($param['customer_id'])) {
$authMap['customer.customer_id'] = ['in', trim(arrayToString($param['customer_id']), ',')];
$authMap['customer.customer_id'] = ['in', trim(arrayToString($param['customer_id']),',')];
}
}
// 排序
// 排序
if (!empty($orderField) & & !empty($orderType)) {
if (!empty($orderField) & & !empty($orderType)) {
$order = $fieldModel->getOrderByFormtype('crm_customer', 'customer', $orderField, $orderType);
$order = $fieldModel->getOrderByFormtype('crm_customer','customer', $orderField, $orderType);
} else {
} else {
$order = 'customer.update_time desc';
$order = 'customer.update_time desc';
}
}
@ -78,7 +76,7 @@ class CustomerPoolLogic extends Common
unset($param['is_excel']);
unset($param['is_excel']);
unset($param['customer_id']);
unset($param['customer_id']);
// 格式化参数
// 格式化参数
$request = $this->fmtRequest($param);
$request = $this->fmtRequest( $param );
$requestMap = !empty($request['map']) ? $request['map'] : [];
$requestMap = !empty($request['map']) ? $request['map'] : [];
// 高级搜索
// 高级搜索
@ -116,12 +114,12 @@ class CustomerPoolLogic extends Common
$extraData = [];
$extraData = [];
$customerIds = array_column($customerPoolList, 'customer_id');
$customerIds = array_column($customerPoolList, 'customer_id');
$extraList = db('crm_customer_data')->whereIn('customer_id', $customerIds)->select();
$extraList = db('crm_customer_data')->whereIn('customer_id', $customerIds)->select();
foreach ($extraList as $key => $value) {
foreach ($extraList AS $key => $value) {
$extraData[$value['customer_id']][$value['field']] = $value['content'];
$extraData[$value['customer_id']][$value['field']] = $value['content'];
}
}
// 整理公海数据
// 整理公海数据
foreach ($customerPoolList as $key => $value) {
foreach ($customerPoolList AS $key => $value) {
$customerPoolList[$key]['create_user_name'] = !empty($userData[$value['create_user_id']]) ? $userData[$value['create_user_id']] : '';
$customerPoolList[$key]['create_user_name'] = !empty($userData[$value['create_user_id']]) ? $userData[$value['create_user_id']] : '';
$customerPoolList[$key]['before_owner_user_name'] = !empty($userData[$value['before_owner_user_id']]) ? $userData[$value['before_owner_user_id']] : '';
$customerPoolList[$key]['before_owner_user_name'] = !empty($userData[$value['before_owner_user_id']]) ? $userData[$value['before_owner_user_id']] : '';
@ -132,38 +130,38 @@ class CustomerPoolLogic extends Common
$customerPoolList[$key]['into_pool_time'] = !empty($value['into_pool_time']) ? date('Y-m-d H:i:s', $value['into_pool_time']) : null;
$customerPoolList[$key]['into_pool_time'] = !empty($value['into_pool_time']) ? date('Y-m-d H:i:s', $value['into_pool_time']) : null;
// 处理日期时间类型的自定义字段
// 处理日期时间类型的自定义字段
foreach ($datetimeField as $k => $v) {
foreach ($datetimeField AS $k => $v) {
if (!empty($value[$v])) $customerPoolList[$key][$v] = date('Y-m-d H:i:s', $value[$v]);
if (!empty($value[$v])) $customerPoolList[$key][$v] = date('Y-m-d H:i:s', $value[$v]);
}
}
// 处理人员类型的自定义字段
// 处理人员类型的自定义字段
foreach ($userField as $k => $v) {
foreach ($userField AS $k => $v) {
if (!empty($value[$v])) {
if (!empty($value[$v])) {
$customerPoolList[$key][$v] = $this->fieldTransformToText($value[$v], $userData);
$customerPoolList[$key][$v] = $this->fieldTransformToText($value[$v], $userData);
}
}
}
}
// 处理部门类型的自定义字段
// 处理部门类型的自定义字段
foreach ($structureField as $k => $v) {
foreach ($structureField AS $k => $v) {
if (!empty($value[$v])) $customerPoolList[$key][$v] = $this->fieldTransformToText($value[$v], $structureData);
if (!empty($value[$v])) $customerPoolList[$key][$v] = $this->fieldTransformToText($value[$v], $structureData);
}
}
// 布尔值类型字段
// 布尔值类型字段
foreach ($booleanField as $k => $v) {
foreach ($booleanField AS $k => $v) {
$customerPoolList[$key][$v] = !empty($value[$v]) ? (string)$value[$v] : '0';
$customerPoolList[$key][$v] = !empty($value[$v]) ? (string)$value[$v] : '0';
}
}
// 处理日期区间类型字段的格式
// 处理日期区间类型字段的格式
foreach ($dateIntervalField as $k => $v) {
foreach ($dateIntervalField AS $k => $v) {
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
}
}
// 处理地址类型字段的格式
// 处理地址类型字段的格式
foreach ($positionField as $k => $v) {
foreach ($positionField AS $k => $v) {
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
}
}
// 手写签名类型字段
// 手写签名类型字段
foreach ($handwritingField as $k => $v) {
foreach ($handwritingField AS $k => $v) {
$handwritingData = !empty($value[$v]) ? db('admin_file')->where('file_id', $value[$v])->value('file_path') : null;
$handwritingData = !empty($value[$v]) ? db('admin_file')->where('file_id', $value[$v])->value('file_path') : null;
$customerPoolList[$key][$v] = ['url' => !empty($handwritingData) ? getFullPath($handwritingData) : null];
$customerPoolList[$key][$v] = ['url' => !empty($handwritingData) ? getFullPath($handwritingData) : null];
}
}
// 定位类型字段
// 定位类型字段
foreach ($locationField as $k => $v) {
foreach ($locationField AS $k => $v) {
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
$customerPoolList[$key][$v] = !empty($extraData[$value['customer_id']][$v]) ? json_decode($extraData[$value['customer_id']][$v], true) : null;
}
}
}
}
@ -178,9 +176,9 @@ class CustomerPoolLogic extends Common
* 公海详情
* 公海详情
*
*
* @param array $param pool_id 公海ID, customer_id 客户ID
* @param array $param pool_id 公海ID, customer_id 客户ID
* @return array|bool|PDOStatement|string|Model|null
* @since 2021-04-14
* @author fanqi
* @author fanqi
* @since 2021-04-14
* @return array|bool|PDOStatement|string|Model|null
*/
*/
public function getPoolData($param)
public function getPoolData($param)
{
{
@ -216,19 +214,19 @@ class CustomerPoolLogic extends Common
$data['into_pool_time'] = !empty($data['into_pool_time']) ? date('Y-m-d H:i:s', $data['into_pool_time']) : null;
$data['into_pool_time'] = !empty($data['into_pool_time']) ? date('Y-m-d H:i:s', $data['into_pool_time']) : null;
# 处理日期时间类型的自定义字段
# 处理日期时间类型的自定义字段
foreach ($datetimeField as $k => $v) {
foreach ($datetimeField AS $k => $v) {
if (isset($data[$v])) {
if (isset($data[$v])) {
$data[$v] = !empty($data[$v]) ? date('Y-m-d H:i:s', $data[$v]) : null;
$data[$v] = !empty($data[$v]) ? date('Y-m-d H:i:s', $data[$v]) : null;
}
}
}
}
# 处理人员类型的自定义字段
# 处理人员类型的自定义字段
foreach ($userField as $k => $v) {
foreach ($userField AS $k => $v) {
if (isset($data[$v]) & & !empty($data[$v])) {
if (isset($data[$v]) & & !empty($data[$v])) {
$data[$v] = $this->fieldTransformToText($data[$v], $userData);
$data[$v] = $this->fieldTransformToText($data[$v], $userData);
}
}
}
}
# 处理部门类型的自定义字段
# 处理部门类型的自定义字段
foreach ($structureField as $k => $v) {
foreach ($structureField AS $k => $v) {
if (isset($data[$v]) & & !empty($data[$v])) {
if (isset($data[$v]) & & !empty($data[$v])) {
$data[$v] = $this->fieldTransformToText($data[$v], $structureData);
$data[$v] = $this->fieldTransformToText($data[$v], $structureData);
}
}
@ -254,7 +252,7 @@ class CustomerPoolLogic extends Common
$customerData = $this->getCustomerList($customerId);
$customerData = $this->getCustomerList($customerId);
# 验证是否是公海数据
# 验证是否是公海数据
foreach ($customerId as $key => $value) {
foreach ($customerId AS $key => $value) {
if (empty($customerData[$value])) {
if (empty($customerData[$value])) {
$message[] = '删除 《' . $customerData[$value]['name'] . '》 失败,原因:公海客户不存在!';
$message[] = '删除 《' . $customerData[$value]['name'] . '》 失败,原因:公海客户不存在!';
@ -277,7 +275,7 @@ class CustomerPoolLogic extends Common
$customerContractData = db('crm_contract')->whereIn('customer_id', $customerId)->column('customer_id');
$customerContractData = db('crm_contract')->whereIn('customer_id', $customerId)->column('customer_id');
# 验证客户下是否存在联系人、商机、合同
# 验证客户下是否存在联系人、商机、合同
foreach ($customerId as $key => $value) {
foreach ($customerId AS $key => $value) {
if (in_array($value, $customerContactsData)) {
if (in_array($value, $customerContactsData)) {
$message[] = '删除 《' . $customerData[$value]['name'] . '》 失败,原因:客户下存在联系人!';
$message[] = '删除 《' . $customerData[$value]['name'] . '》 失败,原因:客户下存在联系人!';
@ -309,13 +307,13 @@ class CustomerPoolLogic extends Common
# 记录到数据操作日志
# 记录到数据操作日志
$ip = request()->ip();
$ip = request()->ip();
$addOperationLogData = [];
$addOperationLogData = [];
foreach ($customerId as $key => $value) {
foreach ($customerId AS $key => $value) {
$addOperationLogData[] = [
$addOperationLogData[] = [
'user_id' => $param['user_id'],
'user_id' => $param['user_id'],
'client_ip' => $ip,
'client_ip' => $ip,
'module' => 'crm_customer',
'module' => 'crm_customer',
'action_id' => $value,
'action_id' => $value,
'content' => '删除了客户:' . $customerData[$value]['name'],
'content' => '删除了客户:' . $customerData[$value]['name'],
'create_time' => time(),
'create_time' => time(),
'action_name' => 'delete',
'action_name' => 'delete',
'target_name' => $customerData[$value]['name']
'target_name' => $customerData[$value]['name']
@ -334,9 +332,9 @@ class CustomerPoolLogic extends Common
* 获取公海池列表
* 获取公海池列表
*
*
* @param array $param 查询参数: user_id 用户id, structure_id 部门id
* @param array $param 查询参数: user_id 用户id, structure_id 部门id
* @return bool|PDOStatement|string|Collection
* @since 2021-04-21
* @author fanqi
* @author fanqi
* @since 2021-04-21
* @return bool|PDOStatement|string|Collection
*/
*/
public function getPondList($param)
public function getPondList($param)
{
{
@ -347,7 +345,7 @@ class CustomerPoolLogic extends Common
->where(function ($query) use ($param, $adminTypes) {
->where(function ($query) use ($param, $adminTypes) {
if (!in_array(1, $adminTypes)) $query->where('admin_user_ids', 'like', '%,' . $param['user_id'] . ',%');
if (!in_array(1, $adminTypes)) $query->where('admin_user_ids', 'like', '%,' . $param['user_id'] . ',%');
if (!in_array(1, $adminTypes)) $query->whereOr('user_ids', 'like', '%,' . $param['user_id'] . ',%');
if (!in_array(1, $adminTypes)) $query->whereOr('user_ids', 'like', '%,' . $param['user_id'] . ',%');
if (!in_array(1, $adminTypes)) $query->whereOr('department_ids', '%,' . $param['structure_id'] . ',%');
if (!in_array(1, $adminTypes)) $query->whereOr('department_ids', '%,' . $param['structure_id'] . ',%');
})->select();
})->select();
}
}
@ -355,9 +353,9 @@ class CustomerPoolLogic extends Common
* 获取公海字段
* 获取公海字段
*
*
* @param array $param pool_id 公海ID, action 操作类型, action_id 数据id
* @param array $param pool_id 公海ID, action 操作类型, action_id 数据id
* @return array
* @since 2021-04-13
* @author fanqi
* @author fanqi
* @since 2021-04-13
* @return array
*/
*/
public function getFieldList($param)
public function getFieldList($param)
{
{
@ -365,14 +363,14 @@ class CustomerPoolLogic extends Common
# 自定义字段
# 自定义字段
$where = [
$where = [
'pool_id' => $param['pool_id'],
'pool_id' => $param['pool_id'],
'is_hidden' => 0,
'is_hidden' => 0,
'form_type' => ['notin', ['file', 'desc_text', 'detail_table']]
'form_type' => ['notin', ['file', 'desc_text', 'detail_table']]
];
];
$list = db('crm_customer_pool_field_setting')->where($where)->select();
$list = db('crm_customer_pool_field_setting')->where($where)->select();
# 处理公海字段
# 处理公海字段
foreach ($list as $key => $value) {
foreach ($list AS $key => $value) {
$list[$key]['field'] = $value['field_name'];
$list[$key]['field'] = $value['field_name'];
# 处理别名
# 处理别名
@ -394,12 +392,12 @@ class CustomerPoolLogic extends Common
}
}
$data[$list[$key]['field']] = [
$data[$list[$key]['field']] = [
'field' => $list[$key]['field'],
'field' => $list[$key]['field'],
'fieldName' => $list[$key]['fieldName'],
'fieldName' => $list[$key]['fieldName'],
'name' => $list[$key]['name'],
'name' => $list[$key]['name'],
'width' => '',
'width' => '',
'is_hidden' => 1,
'is_hidden' => 1,
'system' => $list[$key]['system'],
'system' => $list[$key]['system'],
'form_type' => $list[$key]['form_type']
'form_type' => $list[$key]['form_type']
];
];
}
}
@ -413,9 +411,9 @@ class CustomerPoolLogic extends Common
/**
/**
* 高级筛选字段列表
* 高级筛选字段列表
*
*
* @return array
* @since 2021-04-14
* @author fanqi
* @author fanqi
* @since 2021-04-14
* @return array
*/
*/
public function getAdvancedFilterFieldList()
public function getAdvancedFilterFieldList()
{
{
@ -436,7 +434,7 @@ class CustomerPoolLogic extends Common
$list = array_merge($list, $base);
$list = array_merge($list, $base);
# 整理数据
# 整理数据
foreach ($list as $key => $value) {
foreach ($list AS $key => $value) {
if (!empty($value['setting'])) $list[$key]['setting'] = explode(chr(10), $value['setting']);
if (!empty($value['setting'])) $list[$key]['setting'] = explode(chr(10), $value['setting']);
}
}
@ -447,9 +445,9 @@ class CustomerPoolLogic extends Common
* 领取公海客户
* 领取公海客户
*
*
* @param array $param user_id 领取人ID, customer_id 要领取的客户ID
* @param array $param user_id 领取人ID, customer_id 要领取的客户ID
* @return array
* @since 2021-04-15
* @author fanqi
* @author fanqi
* @since 2021-04-15
* @return array
*/
*/
public function receiveCustomers($param)
public function receiveCustomers($param)
{
{
@ -457,7 +455,7 @@ class CustomerPoolLogic extends Common
if (empty($param['user_id'])) return ['缺少员工ID'];
if (empty($param['user_id'])) return ['缺少员工ID'];
# 查询参数
# 查询参数
$userId = $param['user_id'];
$userId = $param['user_id'];
$customerId = $param['customer_id'];
$customerId = $param['customer_id'];
# 消息数据
# 消息数据
@ -467,7 +465,7 @@ class CustomerPoolLogic extends Common
$customerData = $this->getCustomerList($customerId);
$customerData = $this->getCustomerList($customerId);
# 剔除非公海客户
# 剔除非公海客户
foreach ($customerId as $key => $value) {
foreach ($customerId AS $key => $value) {
if (!empty($customerData[$value]['owner_user_id'])) {
if (!empty($customerData[$value]['owner_user_id'])) {
$message[] = '客户《' . $customerData[$value]['name'] . '》领取失败,失败原因:不是公海客户!';
$message[] = '客户《' . $customerData[$value]['name'] . '》领取失败,失败原因:不是公海客户!';
@ -487,7 +485,7 @@ class CustomerPoolLogic extends Common
$failCustomer = [];
$failCustomer = [];
if (!is_bool($exceedCount) & & !empty($exceedCount) & & $exceedCount > 0) {
if (!is_bool($exceedCount) & & !empty($exceedCount) & & $exceedCount > 0) {
$failCustomer = array_slice($customerId, count($customerId) - $exceedCount);
$failCustomer = array_slice($customerId, count($customerId) - $exceedCount);
foreach ($failCustomer as $key => $value) {
foreach ($failCustomer AS $key => $value) {
$message[] = '客户《' . $customerData[$value]['name'] . '》领取失败,失败原因:持有客户数达到上限!';
$message[] = '客户《' . $customerData[$value]['name'] . '》领取失败,失败原因:持有客户数达到上限!';
}
}
}
}
@ -497,18 +495,18 @@ class CustomerPoolLogic extends Common
# 检查是否还有要领取的客户
# 检查是否还有要领取的客户
if (empty($addCustomerId)) return $message;
if (empty($addCustomerId)) return $message;
# 查询领取客户的公海id
# 查询领取客户的公海id
$poolId = db('crm_customer_pool_relation')->whereIn('customer_id', $customerId)->value('pool_id');
$poolId=db('crm_customer_pool_relation')->whereIn('customer_id',$customerId)->value('pool_id');
# 公海配置
# 公海配置
$poolConfig = db('crm_customer_pool')->field(['before_owner_conf', 'before_owner_day', 'receive_conf', 'receive_count'])->where('pool_id', $poolId)->find();
$poolConfig = db('crm_customer_pool')->field(['before_owner_conf', 'before_owner_day', 'receive_conf', 'receive_count'])->where('pool_id', $poolId)->find();
# 前负责人N天内不能领取客户
# 前负责人N天内不能领取客户
if (!empty($poolConfig['before_owner_conf'])) {
if (!empty($poolConfig['before_owner_conf'])) {
foreach ($addCustomerId as $key => $value) {
foreach ($addCustomerId AS $key => $value) {
# 是前负责人,检查前负责人是否能够领取。
# 是前负责人,检查前负责人是否能够领取。
if ($userId == $customerData[$value]['before_owner_user_id']) {
if ($userId == $customerData[$value]['before_owner_user_id']) {
$restrictDay = $customerData[$value]['into_pool_time'] + 86400 * $poolConfig['before_owner_day'];
$restrictDay = $customerData[$value]['into_pool_time'] + 86400 * $poolConfig['before_owner_day'];
if (time() < $restrictDay) {
if (time() < $restrictDay) {
$message[] = '客户《' . $customerData[$value]['name'] . '》领取失败,失败原因:进入公海后,' . $poolConfig['before_owner_day'] . '天内不能领取!';
$message[] = '客户《' . $customerData[$value]['name'] . '》领取失败,失败原因:进入公海后,'.$poolConfig['before_owner_day'].'天内不能领取!';
unset($addCustomerId[(int)$key]);
unset($addCustomerId[(int)$key]);
}
}
@ -527,47 +525,46 @@ class CustomerPoolLogic extends Common
$receiveCount = db('crm_customer_pool_record')->where($countWhere)->count();
$receiveCount = db('crm_customer_pool_record')->where($countWhere)->count();
if (!empty($poolConfig['receive_conf']) & & $receiveCount + count($addCustomerId) > $poolConfig['receive_count']) {
if (!empty($poolConfig['receive_conf']) & & $receiveCount + count($addCustomerId) > $poolConfig['receive_count']) {
$overQuantity = ($receiveCount + count($addCustomerId)) - $poolConfig['receive_count'];
$overQuantity = ($receiveCount + count($addCustomerId)) - $poolConfig['receive_count'];
$message[] = '领取客户失败,失败原因:超出当日可领取数量,超出' . $overQuantity . '个!';
$message[] = '领取客户失败,失败原因:超出当日可领取数量,超出'.$overQuantity.'个!';
return $message;
return $message;
}
}
# 整理客户更新数据
# 整理客户更新数据
$addCustomerData = [
$addCustomerData = [
'owner_user_id' => $userId,
'owner_user_id' => $userId,
'before_owner_user_id' => 0,
'before_owner_user_id' => 0,
'into_pool_time' => 0,
'into_pool_time' => 0,
'obtain_time' => time(),
'obtain_time' => time()
'is_allocation' => 1,
];
];
# 整理字段操作记录和数据日志的数据
# 整理字段操作记录和数据日志的数据
$ip = request()->ip();
$ip = request()->ip();
$addActionRecordData = [];
$addActionRecordData = [];
$addOperationLogData = [];
$addOperationLogData = [];
$addReceiveData = [];
$addReceiveData = [];
foreach ($addCustomerId as $key => $value) {
foreach ($addCustomerId AS $key => $value) {
$addActionRecordData[] = [
$addActionRecordData[] = [
'user_id' => $userId,
'user_id' => $userId,
'types' => 'crm_customer',
'types' => 'crm_customer',
'action_id' => $value,
'action_id' => $value,
'content' => '领取了客户',
'content' => '领取了客户',
'create_time' => time()
'create_time' => time()
];
];
$addOperationLogData[] = [
$addOperationLogData[] = [
'user_id' => $userId,
'user_id' => $userId,
'client_ip' => $ip,
'client_ip' => $ip,
'module' => 'crm_customer',
'module' => 'crm_customer',
'action_id' => $value,
'action_id' => $value,
'content' => '领取了客户',
'content' => '领取了客户',
'create_time' => time(),
'create_time' => time(),
'action_name' => 'update',
'action_name' => 'update',
'target_name' => $customerData[$value]['name']
'target_name' => $customerData[$value]['name']
];
];
$addReceiveData[] = [
$addReceiveData[] = [
'customer_id' => $value,
'customer_id' => $value,
'user_id' => $userId,
'user_id' => $userId,
'pool_id' => $poolId,
'pool_id' => $poolId,
'type' => 1,
'type' => 1,
'create_time' => time()
'create_time' => time()
];
];
}
}
@ -606,16 +603,16 @@ class CustomerPoolLogic extends Common
* 分配客户
* 分配客户
*
*
* @param array $param user_id 员工ID, customer_id 客户ID
* @param array $param user_id 员工ID, customer_id 客户ID
* @return array
* @since 2021-04-15
* @author fanqi
* @author fanqi
* @since 2021-04-15
* @return array
*/
*/
public function distributeCustomer($param)
public function distributeCustomer($param)
{
{
# 查询参数
# 查询参数
$userId = $param['user_id'];
$userId = $param['user_id'];
$customerId = $param['customer_id'];
$customerId = $param['customer_id'];
$username = db('admin_user')->where('id', $userId)->value('realname');
$username = db('admin_user')->where('id', $userId)->value('realname');
# 消息数据
# 消息数据
$message = [];
$message = [];
@ -624,7 +621,7 @@ class CustomerPoolLogic extends Common
$customerData = $this->getCustomerList($customerId);
$customerData = $this->getCustomerList($customerId);
# 剔除非公海客户
# 剔除非公海客户
foreach ($customerId as $key => $value) {
foreach ($customerId AS $key => $value) {
if (!empty($customerData[$value]['owner_user_id'])) {
if (!empty($customerData[$value]['owner_user_id'])) {
$message[] = '将客户《' . $customerData[$value]['name'] . '》分配给员工' . $username . '失败,失败原因:不是公海客户!';
$message[] = '将客户《' . $customerData[$value]['name'] . '》分配给员工' . $username . '失败,失败原因:不是公海客户!';
@ -644,7 +641,7 @@ class CustomerPoolLogic extends Common
$failCustomer = [];
$failCustomer = [];
if (!is_bool($exceedCount) & & !empty($exceedCount) & & $exceedCount > 0) {
if (!is_bool($exceedCount) & & !empty($exceedCount) & & $exceedCount > 0) {
$failCustomer = array_slice($customerId, count($customerId) - $exceedCount);
$failCustomer = array_slice($customerId, count($customerId) - $exceedCount);
foreach ($failCustomer as $key => $value) {
foreach ($failCustomer AS $key => $value) {
$message[] = '将客户《' . $customerData[$value]['name'] . '》分配给员工' . $username . '失败,失败原因:持有客户数达到上限!';
$message[] = '将客户《' . $customerData[$value]['name'] . '》分配给员工' . $username . '失败,失败原因:持有客户数达到上限!';
}
}
}
}
@ -655,16 +652,16 @@ class CustomerPoolLogic extends Common
# 检查是否还有要领取的客户
# 检查是否还有要领取的客户
if (empty($addCustomerId)) return $message;
if (empty($addCustomerId)) return $message;
# 查询分配客户的公海id
# 查询分配客户的公海id
$poolId = db('crm_customer_pool_relation')->whereIn('customer_id', $customerId)->value('pool_id');
$poolId=db('crm_customer_pool_relation')->whereIn('customer_id',$customerId)->value('pool_id');
# 整理客户更新数据
# 整理客户更新数据
$addCustomerData = [
$addCustomerData = [
'owner_user_id' => $userId,
'owner_user_id' => $userId,
'before_owner_user_id' => 0,
'before_owner_user_id' => 0,
'into_pool_time' => 0,
'into_pool_time' => 0,
'obtain_time' => time(),
'obtain_time' => time(),
'is_dealt' => 0,
'is_dealt' => 0,
'is_allocation' => 1,
'is_allocation' => 1,
'follow' => '待跟进'
'follow' => '待跟进'
];
];
# 整理字段操作记录和数据日志的数据
# 整理字段操作记录和数据日志的数据
@ -672,29 +669,29 @@ class CustomerPoolLogic extends Common
$addActionRecordData = [];
$addActionRecordData = [];
$addOperationLogData = [];
$addOperationLogData = [];
$addReceiveData = [];
$addReceiveData = [];
foreach ($addCustomerId as $key => $value) {
foreach ($addCustomerId AS $key => $value) {
$addActionRecordData[] = [
$addActionRecordData[] = [
'user_id' => $userId,
'user_id' => $userId,
'types' => 'crm_customer',
'types' => 'crm_customer',
'action_id' => $value,
'action_id' => $value,
'content' => '将客户 ' . $customerData[$value]['name'] . ' 分配给员工 ' . $username,
'content' => '将客户 ' . $customerData[$value]['name'] . ' 分配给员工 ' . $username,
'create_time' => time()
'create_time' => time()
];
];
$addOperationLogData[] = [
$addOperationLogData[] = [
'user_id' => $userId,
'user_id' => $userId,
'client_ip' => $ip,
'client_ip' => $ip,
'module' => 'crm_customer',
'module' => 'crm_customer',
'action_id' => $value,
'action_id' => $value,
'content' => '将客户 ' . $customerData[$value]['name'] . ' 分配给员工 ' . $username,
'content' => '将客户 ' . $customerData[$value]['name'] . ' 分配给员工 ' . $username,
'create_time' => time(),
'create_time' => time(),
'action_name' => 'update',
'action_name' => 'update',
'target_name' => $customerData[$value]['name']
'target_name' => $customerData[$value]['name']
];
];
$addReceiveData[] = [
$addReceiveData[] = [
'customer_id' => $value,
'customer_id' => $value,
'user_id' => $userId,
'user_id' => $userId,
'pool_id' => $poolId,
'pool_id' => $poolId,
'type' => 3,
'type' => 3,
'create_time' => time()
'create_time' => time()
];
];
}
}
@ -733,26 +730,26 @@ class CustomerPoolLogic extends Common
* 公海权限
* 公海权限
*
*
* @param $param
* @param $param
* @return array
* @since 2021-04-14
* @author fanqi
* @author fanqi
* @since 2021-04-14
* @return array
*/
*/
public function getAuthorityData($param)
public function getAuthorityData($param)
{
{
# 权限
# 权限
$authority = [
$authority = [
'index' => false, # 列表
'index' => false, # 列表
'receive' => false, # 领取
'receive' => false, # 领取
'distribute' => false, # 分配
'distribute' => false, # 分配
'excelexport' => false, # 导出
'excelexport' => false, # 导出
'excelimport' => false, # 导入
'excelimport' => false, # 导入
'delete' => false, # 删除
'delete' => false, # 删除
];
];
if (empty($param['pool_id']) || empty($param['user_id']) || empty($param['structure_id'])) return $authority;
if (empty($param['pool_id']) || empty($param['user_id']) || empty($param['structure_id'])) return $authority;
$poolId = $param['pool_id'];
$poolId = $param['pool_id'];
$userId = $param['user_id'];
$userId = $param['user_id'];
$structureId = $param['structure_id'];
$structureId = $param['structure_id'];
# 是否是超级管理员
# 是否是超级管理员
@ -762,17 +759,17 @@ class CustomerPoolLogic extends Common
$data = db('crm_customer_pool')->field(['admin_user_ids', 'user_ids', 'department_ids'])->where('pool_id', $poolId)->find();
$data = db('crm_customer_pool')->field(['admin_user_ids', 'user_ids', 'department_ids'])->where('pool_id', $poolId)->find();
# 管理员、成员、部门
# 管理员、成员、部门
$adminUserIds = !empty($data['admin_user_ids']) ? explode(',', trim($data['admin_user_ids'], ',')) : [];
$adminUserIds = !empty($data['admin_user_ids']) ? explode(',', trim($data['admin_user_ids'], ',')) : [];
$userIds = !empty($data['user_ids']) ? explode(',', trim($data['user_ids'], ',')) : [];
$userIds = !empty($data['user_ids']) ? explode(',', trim($data['user_ids'], ',')) : [];
$structureIds = !empty($data['department_ids']) ? explode(',', trim($data['department_ids'], ',')) : [];
$structureIds = !empty($data['department_ids']) ? explode(',', trim($data['department_ids'], ',')) : [];
# 权限判断
# 权限判断
$authority['index'] = ($userLevel || in_array($userId, $adminUserIds)) || (in_array($userId, $userIds) || in_array($structureId, $structureIds));
$authority['index'] = ($userLevel || in_array($userId, $adminUserIds)) || (in_array($userId, $userIds) || in_array($structureId, $structureIds));
$authority['receive'] = ($userLevel || in_array($userId, $adminUserIds)) || (in_array($userId, $userIds) || in_array($structureId, $structureIds));
$authority['receive'] = ($userLevel || in_array($userId, $adminUserIds)) || (in_array($userId, $userIds) || in_array($structureId, $structureIds));
$authority['distribute'] = $userLevel || in_array($userId, $adminUserIds);
$authority['distribute'] = $userLevel || in_array($userId, $adminUserIds);
$authority['excelexport'] = $userLevel || in_array($userId, $adminUserIds);
$authority['excelexport'] = $userLevel || in_array($userId, $adminUserIds);
$authority['excelimport'] = $userLevel || in_array($userId, $adminUserIds);
$authority['excelimport'] = $userLevel || in_array($userId, $adminUserIds);
$authority['delete'] = $userLevel || in_array($userId, $adminUserIds);
$authority['delete'] = $userLevel || in_array($userId, $adminUserIds);
return $authority;
return $authority;
}
}
@ -781,9 +778,9 @@ class CustomerPoolLogic extends Common
* 获取用户公海字段样式
* 获取用户公海字段样式
*
*
* @param array $param pool 公海ID, user_is 用户ID
* @param array $param pool 公海ID, user_is 用户ID
* @return array[]
* @since 2021-04-22
* @author fanqi
* @author fanqi
* @since 2021-04-22
* @return array[]
*/
*/
public function getFieldConfigIndex($param)
public function getFieldConfigIndex($param)
{
{
@ -792,12 +789,12 @@ class CustomerPoolLogic extends Common
# 公海字段-用户配置数据
# 公海字段-用户配置数据
$data = db('crm_customer_pool_field_style')->where(['pool_id' => $param['pool_id'], 'user_id' => $param['user_id']])->value('content');
$data = db('crm_customer_pool_field_style')->where(['pool_id' => $param['pool_id'], 'user_id' => $param['user_id']])->value('content');
$data = !empty($data) ? json_decode($data, true) : [];
$data = !empty($data) ? json_decode($data, true) :[];
if (!empty($data)) {
if (!empty($data)) {
$exceptFields = db('admin_field')->where(['types' => 'crm_customer', 'form_type' => ['in', ['file', 'handwriting_sign', 'desc_text', 'detail_table']]])->column('field');
$exceptFields = db('admin_field')->where(['types' => 'crm_customer', 'form_type' => ['in', ['file', 'handwriting_sign', 'desc_text', 'detail_table']]])->column('field');
foreach ($data as $key => $value) {
foreach ($data AS $key => $value) {
if (in_array($value['field'], $exceptFields)) continue;
if (in_array($value['field'], $exceptFields)) continue;
if (!empty($value['is_hidden'])) {
if (!empty($value['is_hidden'])) {
@ -809,12 +806,12 @@ class CustomerPoolLogic extends Common
} else {
} else {
# 公海字段-后台配置数据
# 公海字段-后台配置数据
$where = [
$where = [
'pool_id' => $param['pool_id'],
'pool_id' => $param['pool_id'],
'is_hidden' => 0,
'is_hidden' => 0,
'form_type' => ['notin', ['file', 'handwriting_sign', 'desc_text', 'detail_table']]
'form_type' => ['notin', ['file', 'handwriting_sign', 'desc_text', 'detail_table']]
];
];
$poolField = db('crm_customer_pool_field_setting')->where($where)->select();
$poolField = db('crm_customer_pool_field_setting')->where($where)->select();
foreach ($poolField as $key => $value) {
foreach ($poolField AS $key => $value) {
if (empty($value['is_hidden'])) {
if (empty($value['is_hidden'])) {
$showList[] = [
$showList[] = [
'field' => $value['field_name'],
'field' => $value['field_name'],
@ -840,10 +837,10 @@ class CustomerPoolLogic extends Common
public function setFieldWidth($param)
public function setFieldWidth($param)
{
{
$data = db('crm_customer_pool_field_style')->where(['pool_id' => $param['pool_id'], 'user_id' => $param['user_id']])->value('content');
$data = db('crm_customer_pool_field_style')->where(['pool_id' => $param['pool_id'], 'user_id' => $param['user_id']])->value('content');
$data = !empty($data) ? json_decode($data, true) : [];
$data = !empty($data) ? json_decode($data, true) :[];
if (!empty($data)) {
if (!empty($data)) {
foreach ($data as $key => $value) {
foreach ($data AS $key => $value) {
if ($param['field'] == $value['field']) {
if ($param['field'] == $value['field']) {
$data[$key]['width'] = $param['width'];
$data[$key]['width'] = $param['width'];
}
}
@ -855,7 +852,7 @@ class CustomerPoolLogic extends Common
} else {
} else {
$result = [];
$result = [];
$poolField = db('crm_customer_pool_field_setting')->where('pool_id', $param['pool_id'])->select();
$poolField = db('crm_customer_pool_field_setting')->where('pool_id', $param['pool_id'])->select();
foreach ($poolField as $key => $value) {
foreach ($poolField AS $key => $value) {
if ($param['field'] == $value['field_name']) {
if ($param['field'] == $value['field_name']) {
$value['width'] = $param['width'];
$value['width'] = $param['width'];
}
}
@ -887,11 +884,11 @@ class CustomerPoolLogic extends Common
*/
*/
public function setFieldConfig($param)
public function setFieldConfig($param)
{
{
$data = [];
$data = [];
$showList = $param['value'];
$showList = $param['value'];
$hideList = $param['hide_value'];
$hideList = $param['hide_value'];
foreach ($showList as $key => $value) {
foreach ($showList AS $key => $value) {
$data[] = [
$data[] = [
'field' => $value['field'],
'field' => $value['field'],
'name' => $value['name'],
'name' => $value['name'],
@ -899,7 +896,7 @@ class CustomerPoolLogic extends Common
'width' => $value['width']
'width' => $value['width']
];
];
}
}
foreach ($hideList as $key => $value) {
foreach ($hideList AS $key => $value) {
$data[] = [
$data[] = [
'field' => $value['field'],
'field' => $value['field'],
'name' => $value['name'],
'name' => $value['name'],
@ -929,9 +926,9 @@ class CustomerPoolLogic extends Common
*
*
* @param string $source 源ids
* @param string $source 源ids
* @param array $target 目标数组
* @param array $target 目标数组
* @return string
* @since 2021-04-14
* @author fanqi
* @author fanqi
* @since 2021-04-14
* @return string
*/
*/
private function fieldTransformToText($source, $target)
private function fieldTransformToText($source, $target)
{
{
@ -939,7 +936,7 @@ class CustomerPoolLogic extends Common
$array = explode(',', trim($source, ','));
$array = explode(',', trim($source, ','));
foreach ($array as $kk => $vv) {
foreach ($array AS $kk => $vv) {
if (!empty($target[$vv])) $result[] = $target[$vv];
if (!empty($target[$vv])) $result[] = $target[$vv];
}
}
@ -950,14 +947,14 @@ class CustomerPoolLogic extends Common
* 查询表字段
* 查询表字段
*
*
* @param int $poolId 公海ID
* @param int $poolId 公海ID
* @return string
* @since 2021-04-14
* @author fanqi
* @author fanqi
* @since 2021-04-14
* @return string
*/
*/
private function getPoolQueryField($poolId)
private function getPoolQueryField($poolId)
{
{
# 自定义字段
# 自定义字段
$customerFields = db('crm_customer_pool_field_setting')->where('pool_id', $poolId)->where('is_hidden', 0)->column('field_name');
$customerFields = db('crm_customer_pool_field_setting')->where('pool_id', $poolId)->where('is_hidden',0)->column('field_name');
$customerFields[] = 'customer_id';
$customerFields[] = 'customer_id';
# 自定增加表别名
# 自定增加表别名
@ -971,9 +968,9 @@ class CustomerPoolLogic extends Common
/**
/**
* 获取员工列表
* 获取员工列表
*
*
* @return array
* @since 2021-04-14
* @author fanqi
* @author fanqi
* @since 2021-04-14
* @return array
*/
*/
private function getUserList()
private function getUserList()
{
{
@ -981,7 +978,7 @@ class CustomerPoolLogic extends Common
$list = db('admin_user')->field(['id', 'realname'])->select();
$list = db('admin_user')->field(['id', 'realname'])->select();
foreach ($list as $key => $value) {
foreach ($list AS $key => $value) {
$result[$value['id']] = $value['realname'];
$result[$value['id']] = $value['realname'];
}
}
@ -991,9 +988,9 @@ class CustomerPoolLogic extends Common
/**
/**
* 获取部门列表
* 获取部门列表
*
*
* @return array
* @since 2021-04-14
* @author fanqi
* @author fanqi
* @since 2021-04-14
* @return array
*/
*/
private function getStructureList()
private function getStructureList()
{
{
@ -1001,7 +998,7 @@ class CustomerPoolLogic extends Common
$list = db('admin_structure')->field(['id', 'name'])->select();
$list = db('admin_structure')->field(['id', 'name'])->select();
foreach ($list as $key => $value) {
foreach ($list AS $key => $value) {
$result[$value['id']] = $value['name'];
$result[$value['id']] = $value['name'];
}
}
@ -1012,9 +1009,9 @@ class CustomerPoolLogic extends Common
* 获取客户列表
* 获取客户列表
*
*
* @param array $customerId 客户ID
* @param array $customerId 客户ID
* @return array
* @since 2021-04-15
* @author fanqi
* @author fanqi
* @since 2021-04-15
* @return array
*/
*/
private function getCustomerList($customerId)
private function getCustomerList($customerId)
{
{
@ -1024,7 +1021,7 @@ class CustomerPoolLogic extends Common
$customerList = db('crm_customer')->field(['customer_id', 'owner_user_id', 'name', 'into_pool_time', 'before_owner_user_id'])->whereIn('customer_id', $customerId)->select();
$customerList = db('crm_customer')->field(['customer_id', 'owner_user_id', 'name', 'into_pool_time', 'before_owner_user_id'])->whereIn('customer_id', $customerId)->select();
# 整理客户数据
# 整理客户数据
foreach ($customerList as $key => $value) {
foreach ($customerList AS $key => $value) {
$result[$value['customer_id']] = $value;
$result[$value['customer_id']] = $value;
}
}
@ -1053,16 +1050,16 @@ class CustomerPoolLogic extends Common
$list = json_decode($list, true);
$list = json_decode($list, true);
foreach ($list as $key => $value) {
foreach ($list AS $key => $value) {
if (!empty($value['is_hidden']) || empty($data[$value['field']])) continue;
if (!empty($value['is_hidden']) || empty($data[$value['field']])) continue;
$result[] = [
$result[] = [
'field' => $value['field'],
'field' => $value['field'],
'fieldName' => $data[$value['field']]['fieldName'],
'fieldName' => $data[$value['field']]['fieldName'],
'name' => $value['name'],
'name' => $value['name'],
'width' => $value['width'],
'width' => $value['width'],
'is_hidden' => 0,
'is_hidden' => 0,
'system' => $data[$value['field']]['system'],
'system' => $data[$value['field']]['system'],
'form_type' => $data[$value['field']]['form_type'],
'form_type' => $data[$value['field']]['form_type'],
];
];
}
}
@ -1089,12 +1086,12 @@ class CustomerPoolLogic extends Common
# 公海字段-后台配置数据
# 公海字段-后台配置数据
$poolField = db('crm_customer_pool_field_setting')->where('pool_id', $param['pool_id'])->select();
$poolField = db('crm_customer_pool_field_setting')->where('pool_id', $param['pool_id'])->select();
$poolData = [];
$poolData = [];
foreach ($poolField as $key => $value) {
foreach ($poolField AS $key => $value) {
$poolData[$value['field_name']] = $value;
$poolData[$value['field_name']] = $value;
}
}
# 去掉隐藏的字段 + 去掉已经存在的字段 = 剩下的就是新增(隐藏后又开启)的字段
# 去掉隐藏的字段 + 去掉已经存在的字段 = 剩下的就是新增(隐藏后又开启)的字段
foreach ($fieldStyleList as $key => $value) {
foreach ($fieldStyleList AS $key => $value) {
# 去掉隐藏的字段
# 去掉隐藏的字段
if (empty($poolData[$value['field']]) || (!empty($poolData[$value['field']]) & & !empty($poolData[$value['field']]['is_hidden']))) {
if (empty($poolData[$value['field']]) || (!empty($poolData[$value['field']]) & & !empty($poolData[$value['field']]['is_hidden']))) {
unset($fieldStyleList[$key]);
unset($fieldStyleList[$key]);
@ -1111,7 +1108,7 @@ class CustomerPoolLogic extends Common
# 新增(隐藏后又开启)字段
# 新增(隐藏后又开启)字段
if (!empty($poolData)) {
if (!empty($poolData)) {
foreach ($poolData as $key => $value) {
foreach ($poolData AS $key => $value) {
if (empty($value['is_hidden'])) {
if (empty($value['is_hidden'])) {
$fieldStyleList[] = [
$fieldStyleList[] = [
'field' => $value['field_name'],
'field' => $value['field_name'],
@ -1130,64 +1127,4 @@ class CustomerPoolLogic extends Common
}
}
}
}
}
}
/**
* 发起捞起客户审批
* @param array $param user_id 领取人ID, customer_id 要领取的客户ID check_user_id 审核人id
*/
public function customerReceiveExamine($param)
{
$customerCheckModel = model('CustomerCheck');
$examineStepModel = new \app\admin\model\ExamineStep();
$examineStatus = $param['examineStatus'] ?? 1; // 审批是否停用
unset($param['examineStatus']);
$dataInfo = db('crm_customer_check')->where('customer_id', 'in', $param['customer_id'])->find();
if (!empty($dataInfo)& & $dataInfo['check_status'] < 2 ) {
return '客户已被领取待审批,请联系管理员';
}
if (($examineStatus != false & & $examineStatus != 'false') || $examineStatus == 1) {
// 审核判断(是否有符合条件的审批流)
$examineFlowModel = new \app\admin\model\ExamineFlow();
if (!$examineFlowModel->checkExamine($param['user_id'], 'crm_customer_check')) {
return '暂无审批人,无法创建';
}
//添加审批相关信息
$examineFlowData = $examineFlowModel->getFlowByTypes($param['user_id'], 'crm_customer_check');
if (!$examineFlowData) {
return '无可用审批流,请联系管理员';
}
$param['flow_id'] = $examineFlowData['flow_id'];
//获取审批人信息
if ($examineFlowData['config'] == 1) {
//固定审批流
$nextStepData = $examineStepModel->nextStepUser($param['user_id'], $examineFlowData['flow_id'], 'crm_customer_check', 0, 0, 0);
$next_user_ids = arrayToString($nextStepData['next_user_ids']) ?: '';
$check_user_id = $next_user_ids ?: [];
$param['order_id'] = 1;
} else {
$check_user_id = $param['check_user_id'] ? ',' . $param['check_user_id'] . ',' : '';
}
if (!$check_user_id) {
return '无可用审批人,请联系管理员';
}
$param['check_user_id'] = is_array($check_user_id) ? ',' . implode(',', $check_user_id) . ',' : $check_user_id;
} else {
# 审批流停用,将状态改为审核通过
// $param['check_status'] = 2;
# 审批流停用,将状态改为正常 zjf 20210727 默认值为7
$param['check_status'] = 7;
}
if ($customerCheckModel->createData($param)) {
return '添加成功';
} else {
return $customerCheckModel->getError();
}
}
}
}