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/controller/CustomerPool.php

540 lines
18 KiB

3 years ago
<?php
/**
* 客户公海
*
* @author fanqi
* @since 2021-04-13
*/
namespace app\crm\controller;
use app\admin\controller\ApiCommon;
use app\admin\model\Message;
use app\admin\model\User;
3 years ago
use app\crm\logic\CustomerPoolLogic;
use think\Hook;
use think\Request;
use think\response\Json;
class CustomerPool extends ApiCommon
{
/**
* 用于判断权限
* @permission 无限制
* @allow 登录用户可访问
* @other 其他根据系统设置
**/
public function _initialize()
{
$action = [
'permission' => [],
'allow' => [
'index',
'read',
'pondlist',
'field',
'advanced',
'authority',
'receive',
'distribute',
'delete',
'fieldconfig',
'setfieldwidth',
'setfieldconfig',
'exceldownload',
'import',
2 years ago
'export',
'checkCustomerReveive'
3 years ago
]
];
3 years ago
Hook::listen('check_auth', $action);
3 years ago
$request = Request::instance();
$a = strtolower($request->action());
if (!in_array($a, $action['permission'])) {
parent::_initialize();
}
}
3 years ago
3 years ago
/**
* 公海列表
*
* @return Json
3 years ago
* @since 2021-04-14
* @author fanqi
3 years ago
*/
public function index()
{
if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
3 years ago
3 years ago
$data = (new CustomerPoolLogic())->getPoolList($this->param);
3 years ago
3 years ago
return resultArray(['data' => $data]);
}
3 years ago
3 years ago
/**
* 详情
*
* @return Json
3 years ago
* @since 2021-04-14
* @author fanqi
3 years ago
*/
public function read()
{
if (empty($this->param['pool_id']) || empty($this->param['customer_id'])) return resultArray(['error' => '参数错误!']);
3 years ago
3 years ago
$userInfo = $this->userInfo;
$param = $this->param;
$param['user_id'] = $userInfo['id'];
3 years ago
3 years ago
$data = (new CustomerPoolLogic())->getPoolData($param);
3 years ago
3 years ago
return resultArray(['data' => $data]);
}
3 years ago
3 years ago
/**
* 删除公海客户
*
* @return Json
3 years ago
* @since 2021-04-15
* @author fanqi
3 years ago
*/
public function delete()
{
if (empty($this->param['id'])) return resultArray(['error' => '请选择要删除的客户!']);
3 years ago
3 years ago
$this->param['user_id'] = $this->userInfo['id'];
3 years ago
3 years ago
$result = (new CustomerPoolLogic())->deletePoolCustomer($this->param);
3 years ago
3 years ago
if (!empty($result)) return resultArray(['error' => $result]);
3 years ago
3 years ago
return resultArray(['data' => '删除成功!']);
}
3 years ago
3 years ago
/**
* 公海池列表
*
* @return Json
3 years ago
* @since 2021-04-13
* @author fanqi
3 years ago
*/
public function pondList()
{
$data = (new CustomerPoolLogic())->getPondList(['user_id' => $this->userInfo['id'], 'structure_id' => $this->userInfo['structure_id']]);
3 years ago
3 years ago
return resultArray(['data' => $data]);
}
3 years ago
3 years ago
/**
* 公海字段
*
* @return Json
3 years ago
* @since 2021-04-13
* @author fanqi
3 years ago
*/
public function field()
{
if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
3 years ago
3 years ago
$userInfo = $this->userInfo;
$param = $this->param;
$param['user_id'] = $userInfo['id'];
3 years ago
3 years ago
$data = (new CustomerPoolLogic())->getFieldList($param);
3 years ago
3 years ago
return resultArray(['data' => $data]);
}
3 years ago
3 years ago
/**
* 高级筛选字段列表
*
* @return Json
3 years ago
* @since 2021-04-14
* @author fanqi
3 years ago
*/
public function advanced()
{
if (empty($this->param['types'])) return resultArray(['error' => '缺少模块类型!']);
3 years ago
3 years ago
$data = (new CustomerPoolLogic())->getAdvancedFilterFieldList($this->param);
3 years ago
3 years ago
return resultArray(['data' => $data]);
}
3 years ago
3 years ago
/**
* 领取公海池客户
*
* @return Json
3 years ago
* @since 2021-04-15
* @author fanqi
3 years ago
*/
public function receive()
{
if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要领取的公海客户!']);
3 years ago
3 years ago
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
3 years ago
$result = (new CustomerPoolLogic())->customerReceiveExamine($param);
3 years ago
3 years ago
if (!empty($result)) return resultArray(['error' => $result]);
3 years ago
3 years ago
return resultArray(['data' => '领取成功!']);
}
3 years ago
3 years ago
/**
* 分配公海客户
*
* @return Json
3 years ago
* @since 2021-04-15
* @author fanqi
3 years ago
*/
public function distribute()
{
if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要分配的公海客户!']);
if (empty($this->param['user_id'])) return resultArray(['error' => '请选择要分配的员工!']);
3 years ago
3 years ago
$result = (new CustomerPoolLogic())->distributeCustomer($this->param);
3 years ago
3 years ago
if (!empty($result)) return resultArray(['error' => $result]);
3 years ago
3 years ago
return resultArray(['data' => '分配成功!']);
}
3 years ago
3 years ago
// 公海客户导入模板下载
3 years ago
public function excelDownload($save_path = '')
3 years ago
{
$excelModel = new \app\admin\model\Excel();
3 years ago
$param = $this->param;
$field_list = $this->fieldsData($param);
$excelModel->excelImportDownload($field_list, 'crm_pool', $save_path);
3 years ago
}
3 years ago
3 years ago
// 导入
public function import()
{
3 years ago
3 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$excelModel = new \app\admin\model\Excel();
$param['create_user_id'] = $userInfo['id'];
$param['deal_time'] = time();
$param['types'] = 'crm_customer';
3 years ago
$param['deal_status'] = '未成交';
3 years ago
$file = request()->file('file');
// $res = $excelModel->importExcel($file, $param, $this);
$res = $excelModel->batchImportData($file, $param, $this);
3 years ago
RecordActionLog($userInfo['id'], 'crm_customer', 'excel', '导入公海客户', '', '', '导入公海客户');
3 years ago
return resultArray(['data' => $excelModel->getError()]);
}
3 years ago
3 years ago
// 导出
public function export()
{
$param = $this->param;
$userInfo = $this->userInfo;
3 years ago
$action_name = '导出全部';
3 years ago
if ($param['customer_id']) {
3 years ago
$action_name = '导出选中';
3 years ago
}
$param['is_excel'] = 1;
$excelModel = new \app\admin\model\Excel();
// 导出的字段列表
3 years ago
$field_list = $this->fieldsData($param);
3 years ago
// 文件名
$file_name = '5kcrm_customer_' . date('Ymd');
$model = model('Customer');
$temp_file = $param['temp_file'];
unset($param['temp_file']);
$page = $param['page'] ?: 1;
unset($param['page']);
unset($param['export_queue_index']);
3 years ago
// p($field_list);
3 years ago
return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
$param['page'] = $page;
$param['limit'] = $limit;
$data = (new CustomerPoolLogic())->getPoolList($param);
$data['list'] = $model->exportHandle($data['list'], $field_list, 'customer');
return $data;
});
3 years ago
RecordActionLog($userInfo['id'], 'crm_customer', 'excelexport', $action_name, '', '', '导出客户');
3 years ago
return resultArray(['error' => 'error']);
}
3 years ago
3 years ago
/**
* 公海权限
*
* @return Json
3 years ago
* @since 2021-04-14
* @author fanqi
3 years ago
*/
public function authority()
{
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
$param['structure_id'] = $this->userInfo['structure_id'];
3 years ago
3 years ago
$data = (new CustomerPoolLogic())->getAuthorityData($param);
3 years ago
3 years ago
return resultArray(['data' => $data]);
}
3 years ago
3 years ago
/**
* 公海字段样式
*
* @return Json
3 years ago
* @since 2021-04-22
* @author fanqi
3 years ago
*/
public function fieldConfig()
{
if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
3 years ago
3 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
3 years ago
3 years ago
$data = (new CustomerPoolLogic())->getFieldConfigIndex($param);
3 years ago
3 years ago
return resultArray(['data' => $data]);
}
3 years ago
3 years ago
/**
* 设置公海字段宽度
*
* @return Json
3 years ago
* @since 2021-04-22
* @author fanqi
3 years ago
*/
public function setFieldWidth()
{
if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
if (empty($this->param['field'])) return resultArray(['error' => '缺少字段名称!']);
if (empty($this->param['width'])) return resultArray(['error' => '缺少宽度值!']);
3 years ago
3 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
3 years ago
3 years ago
(new CustomerPoolLogic())->setFieldWidth($param);
3 years ago
3 years ago
return resultArray(['data' => '操作成功!']);
}
3 years ago
3 years ago
/**
* 设置公海字段样式
*
* @return Json
3 years ago
* @since 2021-04-22
* @author fanqi
3 years ago
*/
public function setFieldConfig()
{
if (empty($this->param['pool_id'])) return resultArray(['缺少公海ID']);
if (empty($this->param['value']) && empty($this->param['hide_value'])) return resultArray(['error' => '字段参数错误!']);
3 years ago
3 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
3 years ago
3 years ago
(new CustomerPoolLogic())->setFieldConfig($param);
3 years ago
3 years ago
return resultArray(['data' => '操作成功!']);
}
3 years ago
/**
* 导入模板
* @param $param
*
* @author alvin guogaobo
* @version 1.0 版本号
* @since 2021/5/17 0017 11:34
*/
public function fieldsData($param)
{
$where=['pool_id' => $param['pool_id'], 'is_hidden' => 0,
'field_name' => ['not in', ['deal_status','create_user_id']]];
if($param['is_excel']==1){
$where=['pool_id' => $param['pool_id'], 'is_hidden' => 0,
'field_name' => ['not in', ['deal_status']]];
}
$pool_list = db('crm_customer_pool_field_setting')->where($where)->field('field_name as field,form_type,name')->select();
3 years ago
$fieldParam['types'] = 'crm_customer';
$fieldParam['action'] = 'excel';
3 years ago
$setting = [];
$list=[];
foreach ($pool_list as $k => &$v) {
switch ($v['field']) {
case 'address':
if($param['is_excel']!=1){
$v['field'] = 'customer_address';
$v['form_type'] = 'map_address';
$list[]=$v;
unset($pool_list[$k]);
}
break;
case 'detail_address':
3 years ago
if($param['is_excel']!=1){
unset($pool_list[$k]);
}
3 years ago
break;
case ($v['form_type'] == 'select') || ($v['form_type'] == 'checkbox') :
$set= db('admin_field')->where(['types' => 'crm_customer', 'field' => $v['field']])->find();
$pool_list[$k]['setting'] = explode(chr(10), $set['setting']);
break;
default :
if($param['is_excel']!=1){
$pool_list[$k]['setting'] = [];
}
break;
}
// if($param['is_excel']==1){
// foreach ( $pool_list as &$v){
// if($v['field']=='create_user_id'){
// $v['field'] = 'create_user_name';
// }elseif($v['field']=='before_owner_user_id'){
// $v['field'] = 'create_user_name';
// }
// }
// }
}
$pool_list=array_merge($pool_list,$list);
return $pool_list;
3 years ago
}
/**
* 领取公海池客户
*
* @return Json
* @since 2021-04-15
* @author fanqi
*/
public function checkCustomerReveive()
{
$param = $this->param;
$userInfo = $this->userInfo;
$user_id = $userInfo['id'];
$customerCheckModel = model('CustomerCheck');
$examineStepModel = new \app\admin\model\ExamineStep();
$examineRecordModel = new \app\admin\model\ExamineRecord();
$examineFlowModel = new \app\admin\model\ExamineFlow();
$customerModel = model('Customer');
$customerCheckData = [];
$customerCheckData['update_time'] = time();
$customerCheckData['check_status'] = 1; //0待审核1审核通中2审核通过3审核未通过
//权限判断
if (!$examineStepModel->checkExamine($user_id, 'crm_contract', $param['id'])) {
return resultArray(['error' => $examineStepModel->getError()]);
}
//审批主体详情
$dataInfo = db('crm_customer_check')->where('customer_check_id', 'in', $param['customer_check_id'])->find();
$flowInfo = $examineFlowModel->getDataById($dataInfo['flow_id']);
$is_end = 0; // 1审批结束
$status = $param['status'] ? 1 : 0; //1通过0驳回
$checkData = [];
$checkData['check_user_id'] = $user_id;
$checkData['types'] = 'crm_customer_check';
$checkData['types_id'] = $param['id'];
$checkData['check_time'] = time();
$checkData['content'] = $param['content'];
$checkData['flow_id'] = $dataInfo['flow_id'];
$checkData['order_id'] = $dataInfo['order_id'] ? : 1;
$checkData['status'] = $status;
if ($status == 1) {
if ($flowInfo['config'] == 1) {
//固定流程
//获取下一审批信息
$nextStepData = $examineStepModel->nextStepUser($dataInfo['owner_user_id'], $dataInfo['flow_id'], 'crm_customer_check', $param['id'], $dataInfo['order_id'], $user_id);
$next_user_ids = $nextStepData['next_user_ids'] ? : [];
$customerCheckData['order_id'] = $nextStepData['order_id'] ? : '';
if (!$next_user_ids) {
$is_end = 1;
//审批结束
$checkData['check_status'] = !empty($status) ? 2 : 3;
$customerCheckData['check_user_id'] = '';
} else {
//修改主体相关审批信息
$customerCheckData['check_user_id'] = arrayToString($next_user_ids);
}
} else {
//自选流程
$is_end = $param['is_end'] ? 1 : '';
$check_user_id = $param['check_user_id'] ? : '';
if ($is_end !== 1 && empty($check_user_id)) {
return resultArray(['error' => '请选择下一审批人']);
}
$customerCheckData['check_user_id'] = arrayToString($param['check_user_id']);
}
if ($is_end == 1) {
$checkData['check_status'] = !empty($status) ? 2 : 3;
$customerCheckData['check_user_id'] = '';
$customerCheckData['check_status'] = 2;
}
} else {
//审批驳回
$is_end = 1;
$customerCheckData['check_status'] = 3;
//将审批记录至为无效
// $examineRecordModel->setEnd(['types' => 'crm_contract','types_id' => $param['id']]);
}
//已审批人ID
$customerCheckData['flow_user_id'] = stringToArray($dataInfo['flow_user_id']) ? arrayToString(array_merge(stringToArray($dataInfo['flow_user_id']),[$user_id])) : arrayToString([$user_id]);
$resCustomerCheck = db('crm_customer_check')->where(['customer_check_id' => $param['id']])->update($customerCheckData);
if ($resCustomerCheck) {
//审批记录
$resRecord = $examineRecordModel->createData($checkData);
//审核通过,修改相关信息
if ($is_end == 1 && !empty($status)) {
// 审批通过消息告知负责人
(new Message())->send(
Message::CUSTOMER_CHECK_PASS,
[
'title' => $dataInfo['name'],
'action_id' => $param['id']
],
$dataInfo['owner_user_id']
);
$customerData = [];
$customerData['user_id']=$dataInfo['user_id'];
$customerData['customer_id']=$dataInfo['customer_id'];
$result = (new CustomerPoolLogic())->receiveCustomers($customerData);
if (!empty($result)) return resultArray(['error' => $result]);
return resultArray(['data' => '领取成功!']);
} else {
if ($status) {
//发送站内信
// 通过未完成,发送消息给
(new Message())->send(
Message::CUSTOMER_CHECK_TO_DO,
[
'from_user' => User::where(['id' => $dataInfo['owner_user_id']])->value('realname'),
'title' => $dataInfo['name'],
'action_id' => $param['id']
],
stringToArray($customerCheckData['check_user_id'])
);
} else {
(new Message())->send(
Message::CONTRACT_REJECT,
[
'title' => $dataInfo['name'],
'action_id' => $param['id']
],
$dataInfo['owner_user_id']
);
}
}
return resultArray(['data' => '审批成功']);
} else {
return resultArray(['error' => '审批失败,请重试!']);
}
}
3 years ago
}