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/model/CustomerCheck.php

164 lines
5.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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();
$fieldModel = new \app\admin\model\Field();
$customer_check_id= $request['customer_check_id'];
$order_field = $request['order_field'];
$order_type = $request['order_type'];
$getCount = $request['getCount'];
$customerCheckIdArray = $request['customerCheckIdArray']; // 待办事项提醒参数
unset($request['scene_id']);
unset($request['search']);
unset($request['user_id']);
unset($request['$customer_check_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['customerCheckIdArray']);
$request = $this->fmtRequest($request);
$requestMap = $request['map'] ?: [];
# getCount是代办事项传来的参数代办事项不需要使用场景
$sceneMap = [];
$partMap = [];
$teamMap = $requestMap['team_id'];
//团队成员 高级筛选
if ($teamMap) {
$partMap = advancedQueryFormatForTeam($teamMap, 'customer_check', '');
unset($requestMap['team_id']);
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
} else {
$map = $requestMap ? array_merge($sceneMap, $requestMap) : $sceneMap;
}
//排序
if ($order_type && $order_field) {
$order = $fieldModel->getOrderByFormtype('crm_customer_check', 'customer_check', $order_field, $order_type);
} else {
$order = 'customer_check.update_time desc';
}
# 待办事项查询参数
$dealtWhere = [];
if (!empty($customerCheckIdArray)) $dealtWhere['customer_check.customer_check_id'] = ['in', $customerCheckIdArray];
$dataCount = db('crm_customer_check')
->alias('customer_check')
->join('__CRM_CUSTOMER__ customer', 'customer_check.customer_check_id = customer.customer_id', 'LEFT')
->where($map)->where($partMap)->where($dealtWhere)->count('customer_check_id');
if (!empty($getCount) && $getCount == 1) {
$data['dataCount'] = !empty($dataCount) ? $dataCount : 0;
return $data;
}
$list = db('crm_customer_check')
->alias('customer_check')
->join('__CRM_CUSTOMER__ customer', 'business.customer_id = customer.customer_id', 'LEFT')
->where($map)
->where($partMap)
->where($dealtWhere)
->limit($request['offset'], $request['length'])
->field('customer_check.*,customer.name as customer_name')
->orderRaw($order)
->select();
# 扩展数据
$extraData = [];
$data = [];
$data['list'] = $list ?: [];
$data['dataCount'] = $dataCount ?: 0;
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);
}
}
}