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

400 lines
12 KiB

4 years ago
<?php
/**
* 客户公海
*
* @author fanqi
* @since 2021-04-13
*/
namespace app\crm\controller;
use app\admin\controller\ApiCommon;
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',
'export'
]
];
3 years ago
Hook::listen('check_auth', $action);
4 years ago
$request = Request::instance();
$a = strtolower($request->action());
if (!in_array($a, $action['permission'])) {
parent::_initialize();
}
}
3 years ago
4 years ago
/**
* 公海列表
*
* @return Json
3 years ago
* @since 2021-04-14
* @author fanqi
4 years ago
*/
public function index()
{
if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
3 years ago
4 years ago
$data = (new CustomerPoolLogic())->getPoolList($this->param);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 详情
*
* @return Json
3 years ago
* @since 2021-04-14
* @author fanqi
4 years ago
*/
public function read()
{
if (empty($this->param['pool_id']) || empty($this->param['customer_id'])) return resultArray(['error' => '参数错误!']);
3 years ago
4 years ago
$userInfo = $this->userInfo;
$param = $this->param;
$param['user_id'] = $userInfo['id'];
3 years ago
4 years ago
$data = (new CustomerPoolLogic())->getPoolData($param);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 删除公海客户
*
* @return Json
3 years ago
* @since 2021-04-15
* @author fanqi
4 years ago
*/
public function delete()
{
if (empty($this->param['id'])) return resultArray(['error' => '请选择要删除的客户!']);
3 years ago
4 years ago
$this->param['user_id'] = $this->userInfo['id'];
3 years ago
4 years ago
$result = (new CustomerPoolLogic())->deletePoolCustomer($this->param);
3 years ago
4 years ago
if (!empty($result)) return resultArray(['error' => $result]);
3 years ago
4 years ago
return resultArray(['data' => '删除成功!']);
}
3 years ago
4 years ago
/**
* 公海池列表
*
* @return Json
3 years ago
* @since 2021-04-13
* @author fanqi
4 years ago
*/
public function pondList()
{
$data = (new CustomerPoolLogic())->getPondList(['user_id' => $this->userInfo['id'], 'structure_id' => $this->userInfo['structure_id']]);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 公海字段
*
* @return Json
3 years ago
* @since 2021-04-13
* @author fanqi
4 years ago
*/
public function field()
{
if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
3 years ago
4 years ago
$userInfo = $this->userInfo;
$param = $this->param;
$param['user_id'] = $userInfo['id'];
3 years ago
4 years ago
$data = (new CustomerPoolLogic())->getFieldList($param);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 高级筛选字段列表
*
* @return Json
3 years ago
* @since 2021-04-14
* @author fanqi
4 years ago
*/
public function advanced()
{
if (empty($this->param['types'])) return resultArray(['error' => '缺少模块类型!']);
3 years ago
4 years ago
$data = (new CustomerPoolLogic())->getAdvancedFilterFieldList($this->param);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 领取公海池客户
*
* @return Json
3 years ago
* @since 2021-04-15
* @author fanqi
4 years ago
*/
public function receive()
{
if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要领取的公海客户!']);
3 years ago
4 years ago
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
3 years ago
4 years ago
$result = (new CustomerPoolLogic())->receiveCustomers($param);
3 years ago
4 years ago
if (!empty($result)) return resultArray(['error' => $result]);
3 years ago
4 years ago
return resultArray(['data' => '领取成功!']);
}
3 years ago
4 years ago
/**
* 分配公海客户
*
* @return Json
3 years ago
* @since 2021-04-15
* @author fanqi
4 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
4 years ago
$result = (new CustomerPoolLogic())->distributeCustomer($this->param);
3 years ago
4 years ago
if (!empty($result)) return resultArray(['error' => $result]);
3 years ago
4 years ago
return resultArray(['data' => '分配成功!']);
}
3 years ago
4 years ago
// 公海客户导入模板下载
3 years ago
public function excelDownload($save_path = '')
4 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);
4 years ago
}
3 years ago
4 years ago
// 导入
public function import()
{
3 years ago
4 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'] = '未成交';
4 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', '导入公海客户', '', '', '导入公海客户');
4 years ago
return resultArray(['data' => $excelModel->getError()]);
}
3 years ago
4 years ago
// 导出
public function export()
{
$param = $this->param;
$userInfo = $this->userInfo;
3 years ago
$action_name = '导出全部';
4 years ago
if ($param['customer_id']) {
3 years ago
$action_name = '导出选中';
4 years ago
}
$param['is_excel'] = 1;
$excelModel = new \app\admin\model\Excel();
// 导出的字段列表
3 years ago
$field_list = $this->fieldsData($param);
4 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);
4 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, '', '', '导出客户');
4 years ago
return resultArray(['error' => 'error']);
}
3 years ago
4 years ago
/**
* 公海权限
*
* @return Json
3 years ago
* @since 2021-04-14
* @author fanqi
4 years ago
*/
public function authority()
{
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
$param['structure_id'] = $this->userInfo['structure_id'];
3 years ago
4 years ago
$data = (new CustomerPoolLogic())->getAuthorityData($param);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 公海字段样式
*
* @return Json
3 years ago
* @since 2021-04-22
* @author fanqi
4 years ago
*/
public function fieldConfig()
{
if (empty($this->param['pool_id'])) return resultArray(['error' => '缺少公海ID']);
3 years ago
4 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
3 years ago
4 years ago
$data = (new CustomerPoolLogic())->getFieldConfigIndex($param);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 设置公海字段宽度
*
* @return Json
3 years ago
* @since 2021-04-22
* @author fanqi
4 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
4 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
3 years ago
4 years ago
(new CustomerPoolLogic())->setFieldWidth($param);
3 years ago
4 years ago
return resultArray(['data' => '操作成功!']);
}
3 years ago
4 years ago
/**
* 设置公海字段样式
*
* @return Json
3 years ago
* @since 2021-04-22
* @author fanqi
4 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
4 years ago
$param = $this->param;
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
3 years ago
4 years ago
(new CustomerPoolLogic())->setFieldConfig($param);
3 years ago
4 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();
4 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':
unset($pool_list[$k]);
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;
4 years ago
}
3 years ago
}