@ -12,7 +12,9 @@ use app\admin\model\ActionRecord;
use app\admin\model\Common;
use app\admin\model\Field;
use app\admin\model\File;
use app\admin\model\Message;
use app\admin\model\Record;
use app\admin\model\User;
use app\crm\model\CustomerConfig;
use PDOStatement;
use think\Collection;
@ -25,9 +27,9 @@ class CustomerPoolLogic extends Common
* 公海列表
*
* @param $param
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
public function getPoolList($param)
{
@ -46,10 +48,10 @@ class CustomerPoolLogic extends Common
$searchMap = [];
if ($param['search'] == '0' || !empty($param['search'])) {
$search = $param['search'];
$searchMap = function($query) use ($search) {
$query->where('customer.name',array('like','%'.$search.'%'))
->whereOr('customer.mobile',array('like','%'.$search.'%'))
->whereOr('customer.telephone',array('like','%'.$search.'%'));
$searchMap = function ($query) use ($search) {
$query->where('customer.name', array('like', '%' . $search . '%'))
->whereOr('customer.mobile', array('like', '%' . $search . '%'))
->whereOr('customer.telephone', array('like', '%' . $search . '%'));
};
}
@ -60,11 +62,11 @@ class CustomerPoolLogic extends Common
}
// 公海条件
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)) {
$order = $fieldModel->getOrderByFormtype('crm_customer','customer', $orderField, $orderType);
$order = $fieldModel->getOrderByFormtype('crm_customer', 'customer', $orderField, $orderType);
} else {
$order = 'customer.update_time desc';
}
@ -76,7 +78,7 @@ class CustomerPoolLogic extends Common
unset($param['is_excel']);
unset($param['customer_id']);
// 格式化参数
$request = $this->fmtRequest( $param );
$request = $this->fmtRequest($param);
$requestMap = !empty($request['map']) ? $request['map'] : [];
// 高级搜索
@ -114,12 +116,12 @@ class CustomerPoolLogic extends Common
$extraData = [];
$customerIds = array_column($customerPoolList, 'customer_id');
$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'];
}
// 整理公海数据
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]['before_owner_user_name'] = !empty($userData[$value['before_owner_user_id']]) ? $userData[$value['before_owner_user_id']] : '';
@ -130,38 +132,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;
// 处理日期时间类型的自定义字段
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]);
}
// 处理人员类型的自定义字段
foreach ($userField AS $k => $v) {
foreach ($userField as $k => $v) {
if (!empty($value[$v])) {
$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);
}
// 布尔值类型字段
foreach ($booleanField AS $k => $v) {
foreach ($booleanField as $k => $v) {
$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;
}
// 处理地址类型字段的格式
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;
}
// 手写签名类型字段
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;
$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;
}
}
@ -176,9 +178,9 @@ class CustomerPoolLogic extends Common
* 公海详情
*
* @param array $param pool_id 公海ID, customer_id 客户ID
* @author fanqi
* @since 2021-04-14
* @return array|bool|PDOStatement|string|Model|null
* @since 2021-04-14
* @author fanqi
*/
public function getPoolData($param)
{
@ -214,19 +216,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;
# 处理日期时间类型的自定义字段
foreach ($datetimeField AS $k => $v) {
foreach ($datetimeField as $k => $v) {
if (isset($data[$v])) {
$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])) {
$data[$v] = $this->fieldTransformToText($data[$v], $userData);
}
}
# 处理部门类型的自定义字段
foreach ($structureField AS $k => $v) {
foreach ($structureField as $k => $v) {
if (isset($data[$v]) & & !empty($data[$v])) {
$data[$v] = $this->fieldTransformToText($data[$v], $structureData);
}
@ -252,7 +254,7 @@ class CustomerPoolLogic extends Common
$customerData = $this->getCustomerList($customerId);
# 验证是否是公海数据
foreach ($customerId AS $key => $value) {
foreach ($customerId as $key => $value) {
if (empty($customerData[$value])) {
$message[] = '删除 《' . $customerData[$value]['name'] . '》 失败,原因:公海客户不存在!';
@ -275,7 +277,7 @@ class CustomerPoolLogic extends Common
$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)) {
$message[] = '删除 《' . $customerData[$value]['name'] . '》 失败,原因:客户下存在联系人!';
@ -307,7 +309,7 @@ class CustomerPoolLogic extends Common
# 记录到数据操作日志
$ip = request()->ip();
$addOperationLogData = [];
foreach ($customerId AS $key => $value) {
foreach ($customerId as $key => $value) {
$addOperationLogData[] = [
'user_id' => $param['user_id'],
'client_ip' => $ip,
@ -332,9 +334,9 @@ class CustomerPoolLogic extends Common
* 获取公海池列表
*
* @param array $param 查询参数: user_id 用户id, structure_id 部门id
* @author fanqi
* @since 2021-04-21
* @return bool|PDOStatement|string|Collection
* @since 2021-04-21
* @author fanqi
*/
public function getPondList($param)
{
@ -353,9 +355,9 @@ class CustomerPoolLogic extends Common
* 获取公海字段
*
* @param array $param pool_id 公海ID, action 操作类型, action_id 数据id
* @author fanqi
* @since 2021-04-13
* @return array
* @since 2021-04-13
* @author fanqi
*/
public function getFieldList($param)
{
@ -370,7 +372,7 @@ class CustomerPoolLogic extends Common
$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'];
# 处理别名
@ -411,9 +413,9 @@ class CustomerPoolLogic extends Common
/**
* 高级筛选字段列表
*
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
public function getAdvancedFilterFieldList()
{
@ -434,7 +436,7 @@ class CustomerPoolLogic extends Common
$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']);
}
@ -603,9 +605,9 @@ class CustomerPoolLogic extends Common
* 分配客户
*
* @param array $param user_id 员工ID, customer_id 客户ID
* @author fanqi
* @since 2021-04-15
* @return array
* @since 2021-04-15
* @author fanqi
*/
public function distributeCustomer($param)
{
@ -621,7 +623,7 @@ class CustomerPoolLogic extends Common
$customerData = $this->getCustomerList($customerId);
# 剔除非公海客户
foreach ($customerId AS $key => $value) {
foreach ($customerId as $key => $value) {
if (!empty($customerData[$value]['owner_user_id'])) {
$message[] = '将客户《' . $customerData[$value]['name'] . '》分配给员工' . $username . '失败,失败原因:不是公海客户!';
@ -641,7 +643,7 @@ class CustomerPoolLogic extends Common
$failCustomer = [];
if (!is_bool($exceedCount) & & !empty($exceedCount) & & $exceedCount > 0) {
$failCustomer = array_slice($customerId, count($customerId) - $exceedCount);
foreach ($failCustomer AS $key => $value) {
foreach ($failCustomer as $key => $value) {
$message[] = '将客户《' . $customerData[$value]['name'] . '》分配给员工' . $username . '失败,失败原因:持有客户数达到上限!';
}
}
@ -652,7 +654,7 @@ class CustomerPoolLogic extends Common
# 检查是否还有要领取的客户
if (empty($addCustomerId)) return $message;
# 查询分配客户的公海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 = [
'owner_user_id' => $userId,
@ -669,7 +671,7 @@ class CustomerPoolLogic extends Common
$addActionRecordData = [];
$addOperationLogData = [];
$addReceiveData = [];
foreach ($addCustomerId AS $key => $value) {
foreach ($addCustomerId as $key => $value) {
$addActionRecordData[] = [
'user_id' => $userId,
'types' => 'crm_customer',
@ -730,9 +732,9 @@ class CustomerPoolLogic extends Common
* 公海权限
*
* @param $param
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
public function getAuthorityData($param)
{
@ -778,9 +780,9 @@ class CustomerPoolLogic extends Common
* 获取用户公海字段样式
*
* @param array $param pool 公海ID, user_is 用户ID
* @author fanqi
* @since 2021-04-22
* @return array[]
* @since 2021-04-22
* @author fanqi
*/
public function getFieldConfigIndex($param)
{
@ -789,12 +791,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 = !empty($data) ? json_decode($data, true) :[];
$data = !empty($data) ? json_decode($data, true) : [];
if (!empty($data)) {
$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 (!empty($value['is_hidden'])) {
@ -811,7 +813,7 @@ class CustomerPoolLogic extends Common
'form_type' => ['notin', ['file', 'handwriting_sign', 'desc_text', 'detail_table']]
];
$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'])) {
$showList[] = [
'field' => $value['field_name'],
@ -837,10 +839,10 @@ class CustomerPoolLogic extends Common
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 = !empty($data) ? json_decode($data, true) :[];
$data = !empty($data) ? json_decode($data, true) : [];
if (!empty($data)) {
foreach ($data AS $key => $value) {
foreach ($data as $key => $value) {
if ($param['field'] == $value['field']) {
$data[$key]['width'] = $param['width'];
}
@ -852,7 +854,7 @@ class CustomerPoolLogic extends Common
} else {
$result = [];
$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']) {
$value['width'] = $param['width'];
}
@ -888,7 +890,7 @@ class CustomerPoolLogic extends Common
$showList = $param['value'];
$hideList = $param['hide_value'];
foreach ($showList AS $key => $value) {
foreach ($showList as $key => $value) {
$data[] = [
'field' => $value['field'],
'name' => $value['name'],
@ -896,7 +898,7 @@ class CustomerPoolLogic extends Common
'width' => $value['width']
];
}
foreach ($hideList AS $key => $value) {
foreach ($hideList as $key => $value) {
$data[] = [
'field' => $value['field'],
'name' => $value['name'],
@ -926,9 +928,9 @@ class CustomerPoolLogic extends Common
*
* @param string $source 源ids
* @param array $target 目标数组
* @author fanqi
* @since 2021-04-14
* @return string
* @since 2021-04-14
* @author fanqi
*/
private function fieldTransformToText($source, $target)
{
@ -936,7 +938,7 @@ class CustomerPoolLogic extends Common
$array = explode(',', trim($source, ','));
foreach ($array AS $kk => $vv) {
foreach ($array as $kk => $vv) {
if (!empty($target[$vv])) $result[] = $target[$vv];
}
@ -947,14 +949,14 @@ class CustomerPoolLogic extends Common
* 查询表字段
*
* @param int $poolId 公海ID
* @author fanqi
* @since 2021-04-14
* @return string
* @since 2021-04-14
* @author fanqi
*/
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';
# 自定增加表别名
@ -968,9 +970,9 @@ class CustomerPoolLogic extends Common
/**
* 获取员工列表
*
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
private function getUserList()
{
@ -978,7 +980,7 @@ class CustomerPoolLogic extends Common
$list = db('admin_user')->field(['id', 'realname'])->select();
foreach ($list AS $key => $value) {
foreach ($list as $key => $value) {
$result[$value['id']] = $value['realname'];
}
@ -988,9 +990,9 @@ class CustomerPoolLogic extends Common
/**
* 获取部门列表
*
* @author fanqi
* @since 2021-04-14
* @return array
* @since 2021-04-14
* @author fanqi
*/
private function getStructureList()
{
@ -998,7 +1000,7 @@ class CustomerPoolLogic extends Common
$list = db('admin_structure')->field(['id', 'name'])->select();
foreach ($list AS $key => $value) {
foreach ($list as $key => $value) {
$result[$value['id']] = $value['name'];
}
@ -1009,9 +1011,9 @@ class CustomerPoolLogic extends Common
* 获取客户列表
*
* @param array $customerId 客户ID
* @author fanqi
* @since 2021-04-15
* @return array
* @since 2021-04-15
* @author fanqi
*/
private function getCustomerList($customerId)
{
@ -1021,7 +1023,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();
# 整理客户数据
foreach ($customerList AS $key => $value) {
foreach ($customerList as $key => $value) {
$result[$value['customer_id']] = $value;
}
@ -1050,7 +1052,7 @@ class CustomerPoolLogic extends Common
$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;
$result[] = [
@ -1086,12 +1088,12 @@ class CustomerPoolLogic extends Common
# 公海字段-后台配置数据
$poolField = db('crm_customer_pool_field_setting')->where('pool_id', $param['pool_id'])->select();
$poolData = [];
foreach ($poolField AS $key => $value) {
foreach ($poolField as $key => $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']))) {
unset($fieldStyleList[$key]);
@ -1108,7 +1110,7 @@ class CustomerPoolLogic extends Common
# 新增(隐藏后又开启)字段
if (!empty($poolData)) {
foreach ($poolData AS $key => $value) {
foreach ($poolData as $key => $value) {
if (empty($value['is_hidden'])) {
$fieldStyleList[] = [
'field' => $value['field_name'],
@ -1127,4 +1129,64 @@ 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; // 审批是否停用
$param['check_user_id'] =1 ;// 临时测试需删除
unset($param['examineStatus']);
$dataInfo = db('crm_customer_check')->where('customer_id', 'in', $param['customer_id'])->find();
if ($dataInfo) {
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();
}
}
}