|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\crm\model;
|
|
|
|
|
|
|
|
|
|
use app\admin\model\Common;
|
|
|
|
|
use app\admin\model\Message;
|
|
|
|
|
use app\admin\traits\FieldVerificationTrait;
|
|
|
|
|
use think\Db;
|
|
|
|
|
|
|
|
|
|
class CustomerCheck extends Common
|
|
|
|
|
{
|
|
|
|
|
use FieldVerificationTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 为了数据库的整洁,同时又不影响Model和Controller的名称
|
|
|
|
|
* 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
|
|
|
|
|
*/
|
|
|
|
|
protected $name = 'crm_customer_check';
|
|
|
|
|
protected $createTime = 'create_time';
|
|
|
|
|
protected $updateTime = 'update_time';
|
|
|
|
|
protected $autoWriteTimestamp = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 待审核客户列表
|
|
|
|
|
* @param $request
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public function getDataList($request)
|
|
|
|
|
{
|
|
|
|
|
$userModel = new \app\admin\model\User();
|
|
|
|
|
$structureModel = new \app\admin\model\Structure();
|
|
|
|
|
$fieldModel = new \app\admin\model\Field();
|
|
|
|
|
$search = $request['search'];
|
|
|
|
|
$user_id = $request['user_id'];
|
|
|
|
|
$scene_id = (int)$request['scene_id'];
|
|
|
|
|
$contacts_id = $request['contacts_id'];
|
|
|
|
|
$order_field = $request['order_field'];
|
|
|
|
|
$order_type = $request['order_type'];
|
|
|
|
|
$is_excel = $request['is_excel']; //导出
|
|
|
|
|
$getCount = $request['getCount'];
|
|
|
|
|
$businessTypeId = $request['typesId']; // 针对mobile
|
|
|
|
|
$businessStatusId = $request['statusId']; // 针对mobile
|
|
|
|
|
$overdue = $request['overdue']; // 待办事项下需联系商机(逾期)
|
|
|
|
|
$businessIdArray = $request['businessIdArray']; // 待办事项提醒参数
|
|
|
|
|
$isMessage = !empty($request['isMessage']);
|
|
|
|
|
unset($request['scene_id']);
|
|
|
|
|
unset($request['search']);
|
|
|
|
|
unset($request['user_id']);
|
|
|
|
|
unset($request['contacts_id']);
|
|
|
|
|
unset($request['order_field']);
|
|
|
|
|
unset($request['order_type']);
|
|
|
|
|
unset($request['is_excel']);
|
|
|
|
|
unset($request['getCount']);
|
|
|
|
|
unset($request['typesId']);
|
|
|
|
|
unset($request['statusId']);
|
|
|
|
|
unset($request['overdue']);
|
|
|
|
|
unset($request['isMessage']);
|
|
|
|
|
unset($request['businessIdArray']);
|
|
|
|
|
$request = $this->fmtRequest($request);
|
|
|
|
|
$requestMap = $request['map'] ?: [];
|
|
|
|
|
$sceneModel = new \app\admin\model\Scene();
|
|
|
|
|
# getCount是代办事项传来的参数,代办事项不需要使用场景
|
|
|
|
|
$sceneMap = [];
|
|
|
|
|
if (empty($getCount)) {
|
|
|
|
|
if ($scene_id) {
|
|
|
|
|
//自定义场景
|
|
|
|
|
$sceneMap = $sceneModel->getDataById($scene_id, $user_id, 'business') ?: [];
|
|
|
|
|
} else {
|
|
|
|
|
//默认场景
|
|
|
|
|
$sceneMap = $sceneModel->getDefaultData('crm_business', $user_id) ?: [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($search || $search == '0') {
|
|
|
|
|
//普通筛选
|
|
|
|
|
$sceneMap['name'] = ['condition' => 'contains', 'value' => $search, 'form_type' => 'text', 'name' => '商机名称'];
|
|
|
|
|
}
|
|
|
|
|
if (isset($requestMap['type_id'])) {
|
|
|
|
|
$requestMap['type_id']['value'] = $requestMap['type_id']['type_id'];
|
|
|
|
|
if (in_array($requestMap['type_id']['status_id'], [1, 2, 3])) {
|
|
|
|
|
$requestMap['is_end'] = $requestMap['type_id']['status_id'];
|
|
|
|
|
} else {
|
|
|
|
|
if ($requestMap['type_id']['status_id']) $requestMap['status_id']['value'] = $requestMap['type_id']['status_id'];
|
|
|
|
|
$requestMap['is_end'] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($sceneMap['type_id']) {
|
|
|
|
|
$requestMap['type_id']['value'] = $sceneMap['type_id']['type_id'];
|
|
|
|
|
if (in_array($sceneMap['type_id']['status_id'], [1, 2, 3])) {
|
|
|
|
|
$sceneMap['is_end'] = $sceneMap['type_id']['status_id'];
|
|
|
|
|
} else {
|
|
|
|
|
if ($sceneMap['type_id']['status_id']) $requestMap['status_id']['value'] = $sceneMap['type_id']['status_id'];
|
|
|
|
|
$sceneMap['is_end'] = 0;
|
|
|
|
|
}
|
|
|
|
|
unset($sceneMap['type_id']);
|
|
|
|
|
}
|
|
|
|
|
$partMap = [];
|
|
|
|
|
$teamMap = $requestMap['team_id'];
|
|
|
|
|
//团队成员 高级筛选
|
|
|
|
|
if ($teamMap) {
|
|
|
|
|
$partMap = advancedQueryFormatForTeam($teamMap, 'business', '');
|
|
|
|
|
unset($requestMap['team_id']);
|
|
|
|
|
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
|
|
|
|
|
} else {
|
|
|
|
|
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
|
|
|
|
|
}
|
|
|
|
|
//高级筛选
|
|
|
|
|
$map = advancedQuery($map, 'crm', 'business', 'index');
|
|
|
|
|
$authMap = [];
|
|
|
|
|
$a = 'index';
|
|
|
|
|
if ($is_excel) $a = 'excelExport';
|
|
|
|
|
$auth_user_ids = $userModel->getUserByPer('crm', 'business', $a);
|
|
|
|
|
if (isset($map['business.owner_user_id'])) {
|
|
|
|
|
if (!is_array($map['business.owner_user_id'][1])) {
|
|
|
|
|
$map['business.owner_user_id'][1] = [$map['business.owner_user_id'][1]];
|
|
|
|
|
}
|
|
|
|
|
if (in_array($map['business.owner_user_id'][0], ['neq', 'notin'])) {
|
|
|
|
|
$auth_user_ids = array_diff($auth_user_ids, $map['business.owner_user_id'][1]) ?: []; //取差集
|
|
|
|
|
} else {
|
|
|
|
|
$auth_user_ids = array_intersect($map['business.owner_user_id'][1], $auth_user_ids) ?: []; //取交集
|
|
|
|
|
}
|
|
|
|
|
unset($map['business.owner_user_id']);
|
|
|
|
|
$auth_user_ids = array_merge(array_unique(array_filter($auth_user_ids))) ?: ['-1'];
|
|
|
|
|
$authMap['business.owner_user_id'] = array('in', $auth_user_ids);
|
|
|
|
|
} else {
|
|
|
|
|
if (!$isMessage) {
|
|
|
|
|
$authMapData = [];
|
|
|
|
|
$authMapData['auth_user_ids'] = $auth_user_ids;
|
|
|
|
|
$authMapData['user_id'] = $user_id;
|
|
|
|
|
$authMap = function ($query) use ($authMapData) {
|
|
|
|
|
$query->where('business.owner_user_id', array('in', $authMapData['auth_user_ids']))
|
|
|
|
|
->whereOr('business.ro_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'))
|
|
|
|
|
->whereOr('business.rw_user_id', array('like', '%,' . $authMapData['user_id'] . ',%'));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//联系人商机
|
|
|
|
|
if ($contacts_id) {
|
|
|
|
|
$business_id = Db::name('crm_contacts_business')->where(['contacts_id' => $contacts_id])->column('business_id');
|
|
|
|
|
if ($business_id) {
|
|
|
|
|
$map['business.business_id'] = array('in', $business_id);
|
|
|
|
|
} else {
|
|
|
|
|
$map['business.business_id'] = array('eq', -1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//列表展示字段
|
|
|
|
|
$indexField = $fieldModel->getIndexField('crm_business', $user_id, 1) ?: array('name');
|
|
|
|
|
if (!empty($indexField)) {
|
|
|
|
|
foreach ($indexField as $key => $value) {
|
|
|
|
|
if ($value == 'business.customer_name') unset($indexField[(int)$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$userField = $fieldModel->getFieldByFormType('crm_business', 'user'); //人员类型
|
|
|
|
|
$structureField = $fieldModel->getFieldByFormType('crm_business', 'structure'); //部门类型
|
|
|
|
|
|
|
|
|
|
// $fieldGrant = db('admin_field_mask')->where('types', 'business')->select();
|
|
|
|
|
# 处理人员和部门类型的排序报错问题(前端传来的是包含_name的别名字段)
|
|
|
|
|
$temporaryField = str_replace('_name', '', $order_field);
|
|
|
|
|
if (in_array($temporaryField, $userField) || in_array($temporaryField, $structureField)) {
|
|
|
|
|
$order_field = $temporaryField;
|
|
|
|
|
}
|
|
|
|
|
//排序
|
|
|
|
|
if ($order_type && $order_field) {
|
|
|
|
|
$order = $fieldModel->getOrderByFormtype('crm_business', 'business', $order_field, $order_type);
|
|
|
|
|
} else {
|
|
|
|
|
$order = 'business.update_time desc';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 商机组和商机状态搜索
|
|
|
|
|
if (!empty($businessTypeId)) $map['business.type_id'] = ['eq', $businessTypeId];
|
|
|
|
|
if (!empty($businessStatusId)) {
|
|
|
|
|
if (preg_match("/^[1-9][0-9]*$/", $businessStatusId)) {
|
|
|
|
|
$map['is_end'] = 0;
|
|
|
|
|
$map['business.status_id'] = ['eq', $businessStatusId];
|
|
|
|
|
} else {
|
|
|
|
|
$map['is_end'] = abs($businessStatusId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
# 待办事项查询参数
|
|
|
|
|
$dealtWhere = [];
|
|
|
|
|
if (!empty($businessIdArray)) $dealtWhere['business.business_id'] = ['in', $businessIdArray];
|
|
|
|
|
// 待办事项下需联系商机(逾期)
|
|
|
|
|
$overdueWhere = '';
|
|
|
|
|
if (!empty($overdue)) {
|
|
|
|
|
$overdueWhere = "(FROM_UNIXTIME(`business`.`last_time`,'%Y-%m-%d') < FROM_UNIXTIME(`business`.`next_time`,'%Y-%m-%d') OR (ISNULL(`business`.`last_time`) AND `business`.`next_time` < " . time() . "))";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$readAuthIds = $userModel->getUserByPer('crm', 'business', 'read');
|
|
|
|
|
$updateAuthIds = $userModel->getUserByPer('crm', 'business', 'update');
|
|
|
|
|
$deleteAuthIds = $userModel->getUserByPer('crm', 'business', 'delete');
|
|
|
|
|
$dataCount = db('crm_business')
|
|
|
|
|
->alias('business')
|
|
|
|
|
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
|
|
|
|
|
->where($map)->where($partMap)->where($authMap)->where($overdueWhere)->where($dealtWhere)->count('business_id');
|
|
|
|
|
if (!empty($getCount) && $getCount == 1) {
|
|
|
|
|
$data['dataCount'] = !empty($dataCount) ? $dataCount : 0;
|
|
|
|
|
# 商机总金额
|
|
|
|
|
$sumMoney = Db::name('crm_business')->alias('business')
|
|
|
|
|
->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
|
|
|
|
|
$data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$list = db('crm_business')
|
|
|
|
|
->alias('business')
|
|
|
|
|
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
|
|
|
|
|
->where($map)
|
|
|
|
|
->where($partMap)
|
|
|
|
|
->where($authMap)
|
|
|
|
|
->where($overdueWhere)
|
|
|
|
|
->where($dealtWhere)
|
|
|
|
|
->limit($request['offset'], $request['length'])
|
|
|
|
|
->field('business.*,customer.name as customer_name')
|
|
|
|
|
->orderRaw($order)
|
|
|
|
|
->select();
|
|
|
|
|
$endStatus = ['1' => '赢单', '2' => '输单', '3' => '无效'];
|
|
|
|
|
# 扩展数据
|
|
|
|
|
$extraData = [];
|
|
|
|
|
|
|
|
|
|
$list = getFieldData($list, 'crm_business', $user_id);
|
|
|
|
|
foreach ($list as $k => $v) {
|
|
|
|
|
$list[$k]['customer_id_info']['customer_id'] = $v['customer_id'];
|
|
|
|
|
$list[$k]['customer_id_info']['name'] = $v['customer_name'];
|
|
|
|
|
$list[$k]['create_user_id_info'] = isset($v['create_user_id']) ? $userModel->getUserById($v['create_user_id']) : [];
|
|
|
|
|
$list[$k]['owner_user_id_info'] = isset($v['owner_user_id']) ? $userModel->getUserById($v['owner_user_id']) : [];
|
|
|
|
|
$list[$k]['create_user_name'] = !empty($list[$k]['create_user_id_info']['realname']) ? $list[$k]['create_user_id_info']['realname'] : '';
|
|
|
|
|
$list[$k]['owner_user_name'] = !empty($list[$k]['owner_user_id_info']['realname']) ? $list[$k]['owner_user_id_info']['realname'] : '';
|
|
|
|
|
|
|
|
|
|
$statusInfo = [];
|
|
|
|
|
$status_count = 0;
|
|
|
|
|
if (!$v['is_end']) {
|
|
|
|
|
$statusInfo = db('crm_business_status')->where('status_id', $v['status_id'])->find();
|
|
|
|
|
if ($statusInfo['order_id'] < 99) {
|
|
|
|
|
$status_count = db('crm_business_status')->where('type_id', ['eq', $v['type_id']])->count();
|
|
|
|
|
}
|
|
|
|
|
//进度
|
|
|
|
|
$list[$k]['status_progress'] = [$statusInfo['order_id'], $status_count + 1];
|
|
|
|
|
} else {
|
|
|
|
|
$statusInfo['name'] = $endStatus[$v['is_end']];
|
|
|
|
|
}
|
|
|
|
|
$list[$k]['status_id_info'] = $statusInfo['name'];//销售阶段
|
|
|
|
|
$list[$k]['type_id_info'] = db('crm_business_type')->where('type_id', $v['type_id'])->value('name');//商机状态组
|
|
|
|
|
|
|
|
|
|
//权限
|
|
|
|
|
$roPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'read');
|
|
|
|
|
$rwPre = $userModel->rwPre($user_id, $v['ro_user_id'], $v['rw_user_id'], 'update');
|
|
|
|
|
$permission = [];
|
|
|
|
|
$is_read = 0;
|
|
|
|
|
$is_update = 0;
|
|
|
|
|
$is_delete = 0;
|
|
|
|
|
if (in_array($v['owner_user_id'], $readAuthIds) || $roPre || $rwPre) $is_read = 1;
|
|
|
|
|
if (in_array($v['owner_user_id'], $updateAuthIds) || $rwPre) $is_update = 1;
|
|
|
|
|
if (in_array($v['owner_user_id'], $deleteAuthIds)) $is_delete = 1;
|
|
|
|
|
$permission['is_read'] = $is_read;
|
|
|
|
|
$permission['is_update'] = $is_update;
|
|
|
|
|
$permission['is_delete'] = $is_delete;
|
|
|
|
|
$list[$k]['permission'] = $permission;
|
|
|
|
|
# 下次联系时间
|
|
|
|
|
$list[$k]['next_time'] = !empty($v['next_time']) ? date('Y-m-d H:i:s', $v['next_time']) : null;
|
|
|
|
|
# 关注
|
|
|
|
|
$starWhere = ['user_id' => $user_id, 'target_id' => $v['business_id'], 'type' => 'crm_business'];
|
|
|
|
|
$star = Db::name('crm_star')->where($starWhere)->value('star_id');
|
|
|
|
|
$list[$k]['star'] = !empty($star) ? 1 : 0;
|
|
|
|
|
# 日期
|
|
|
|
|
$list[$k]['create_time'] = !empty($v['create_time']) ? date('Y-m-d H:i:s', $v['create_time']) : null;
|
|
|
|
|
$list[$k]['update_time'] = !empty($v['update_time']) ? date('Y-m-d H:i:s', $v['update_time']) : null;
|
|
|
|
|
$list[$k]['last_time'] = !empty($v['last_time']) ? date('Y-m-d H:i:s', $v['last_time']) : null;
|
|
|
|
|
|
|
|
|
|
# 系统字段 负责人部门 zjf 20210726
|
|
|
|
|
$list[$k]['owner_user_structure_name'] = $list[$k]['owner_user_id_info']['structure_name'];
|
|
|
|
|
}
|
|
|
|
|
$data = [];
|
|
|
|
|
$data['list'] = $list ?: [];
|
|
|
|
|
$data['dataCount'] = $dataCount ?: 0;
|
|
|
|
|
# 商机总金额
|
|
|
|
|
$sumMoney = Db::name('crm_business')->alias('business')
|
|
|
|
|
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
|
|
|
|
|
->whereIn('is_end', [0, 1])->where($map)->where($partMap)->where($authMap)->sum('money');
|
|
|
|
|
$data['extraData']['money'] = ['businessSumMoney' => !empty($sumMoney) ? sprintf("%.2f", $sumMoney) : 0.00];
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建客户捞取审批信息
|
|
|
|
|
* @param
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public function createData($param)
|
|
|
|
|
{
|
|
|
|
|
$createData = [];
|
|
|
|
|
foreach ($param['customer_id'] as $key => $value) {
|
|
|
|
|
$createData[$key]['customer_id'] = $value;
|
|
|
|
|
$createData[$key]['check_status'] = $param['check_status'] ?? 0;
|
|
|
|
|
$createData[$key]['flow_id'] = $param['flow_id'] ?? 0;
|
|
|
|
|
$createData[$key]['order_id'] = $param['order_id'] ?? 0;
|
|
|
|
|
$createData[$key]['check_user_id'] = $param['check_user_id'] ?? 0;
|
|
|
|
|
$createData[$key]['flow_user_id'] = $param['flow_user_id'] ?? 0;
|
|
|
|
|
$createData[$key]['user_id'] = $param['user_id'] ?? 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->startTrans();
|
|
|
|
|
try {
|
|
|
|
|
$data = $this->saveAll($createData);
|
|
|
|
|
$this->commit();
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->error = '领取失败';
|
|
|
|
|
$this->rollback();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
|
//站内信
|
|
|
|
|
$send_user_id = stringToArray($param['check_user_id']);
|
|
|
|
|
if ($send_user_id && empty($param['check_status'])) {
|
|
|
|
|
(new Message())->send(
|
|
|
|
|
Message::CUSTOMER_CHECK_TO_DO,
|
|
|
|
|
[
|
|
|
|
|
'title' => $param['name'],
|
|
|
|
|
'action_id' => $v['customer_check_id']
|
|
|
|
|
],
|
|
|
|
|
$send_user_id
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$data = [];
|
|
|
|
|
$data['customer_check_id'] = $v['customer_check_id'];
|
|
|
|
|
echo $v['customer_check_id'];
|
|
|
|
|
# 创建待办事项的关联数据
|
|
|
|
|
$checkUserIds = db('crm_customer_check')->where('customer_check_id', $data['customer_check_id'])->value('check_user_id');
|
|
|
|
|
$checkUserIdArray = stringToArray($checkUserIds);
|
|
|
|
|
$dealtData = [];
|
|
|
|
|
foreach ($checkUserIdArray as $kk => $vv) {
|
|
|
|
|
$dealtData[] = [
|
|
|
|
|
'types' => 'crm_customer_check',
|
|
|
|
|
'types_id' => $data['customer_check_id'],
|
|
|
|
|
'user_id' => $vv
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
if (!empty($dealtData)) db('crm_dealt_relation')->insertAll($dealtData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|