| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858 |
- <?php
- // +----------------------------------------------------------------------
- // | Description: 消息模块
- // +----------------------------------------------------------------------
- // | Author: Michael_xu | gengxiaoxu@5kcrm.com
- // +----------------------------------------------------------------------
-
- namespace app\crm\controller;
-
- use app\admin\controller\ApiCommon;
- use app\crm\logic\InvoiceLogic;
- use think\cache\driver\Redis;
- use think\Db;
- use think\Hook;
- use think\Request;
-
- class Message extends ApiCommon
- {
- /**
- * 用于判断权限
- * @permission 无限制
- * @allow 登录用户可访问
- * @other 其他根据系统设置
- **/
- public function _initialize()
- {
- $action = [
- 'permission' => [],
- 'allow' => [
- 'num',
- 'todayleads',
- 'todaycustomer',
- 'todaybusiness',
- 'followleads',
- 'followcustomer',
- 'checkcontract',
- 'checkreceivables',
- 'remindreceivablesplan',
- 'endcontract',
- 'remindcustomer',
- 'checkinvoice',
- 'visitcontract',
- 'alldeal'
- ]
- ];
- Hook::listen('check_auth',$action);
- $request = Request::instance();
- $a = strtolower($request->action());
- if (!in_array($a, $action['permission'])) {
- parent::_initialize();
- }
- }
-
- /**
- * 系统通知
- *
- * @author Michael_xu
- * @return
- */
- public function index()
- {
- $messageModel = model('Message');
- $param = $this->param;
- $userInfo = $this->userInfo;
- $param['user_id'] = $userInfo['id'];
- $param['module_name'] = 'crm';
- $data = $messageModel->getDataList($param);
- return resultArray(['data' => $data]);
- }
-
- /**
- * 消息数
- *
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function num()
- {
- $userInfo = $this->userInfo;
- $configDataModel = model('ConfigData');
- $configData = $configDataModel->getData();
- $data = [];
-
- # 今日需联系线索
- $todayLeadsTime = cache('todayLeadsTime'.$userInfo['id']);
- $todayLeadsCount = cache('todayLeadsCount'.$userInfo['id']);
- if (time() <= $todayLeadsTime) {
- $data['todayLeads'] = (int)$todayLeadsCount;
- } else {
- $todayLeads = $this->todayLeads(true);
- $data['todayLeads'] = $todayLeads['dataCount'] ? : 0;
- cache('todayLeadsCount'.$userInfo['id'], $data['todayLeads']);
- cache('todayLeadsTime'.$userInfo['id'], time() + 180);
- }
- # 今日需联系客户
- $todayCustomerTime = cache('todayCustomerTime'.$userInfo['id']);
- $todayCustomerCount = cache('todayCustomerCount'.$userInfo['id']);
- if (time() <= $todayCustomerTime) {
- $data['todayCustomer'] = (int)$todayCustomerCount;
- } else {
- $todayCustomer = $this->todayCustomer(true);
- $data['todayCustomer'] = $todayCustomer['dataCount'] ? : 0;
- cache('todayCustomerCount'.$userInfo['id'], $data['todayCustomer']);
- cache('todayCustomerTime'.$userInfo['id'], time() + 180);
- }
- # 今日需联系商机
- $todayBusinessTime = cache('todayBusinessTime'.$userInfo['id']);
- $todayBusinessCount = cache('todayBusinessCount'.$userInfo['id']);
- if ($todayBusinessTime <= time()) {
- $data['todayBusiness'] = (int)$todayBusinessCount;
- } else {
- $todayBusiness = $this->todayBusiness(true);
- $data['todayBusiness'] = $todayBusiness['dataCount'] ? : 0;
- cache('todayBusinessCount'.$userInfo['id'], $data['todayBusiness']);
- cache('todayBusinessTime'.$userInfo['id'], time() + 180);
- }
- # 分配给我的线索
- $followLeadsTime = cache('followLeadsTime'.$userInfo['id']);
- $followLeadsCount = cache('followLeadsCount'.$userInfo['id']);
- if (time() <= $followLeadsTime) {
- $data['followLeads'] = (int)$followLeadsCount;
- } else {
- $followLeads = $this->followLeads(true);
- $data['followLeads'] = $followLeads['dataCount'] ? : 0;
- cache('followLeadsCount'.$userInfo['id'], $data['followLeads']);
- cache('followLeadsTime'.$userInfo['id'], time() + 180);
- }
- # 分配给我的客户
- $followCustomerTime = cache('followCustomerTime'.$userInfo['id']);
- $followCustomerCount = cache('followCustomerCount'.$userInfo['id']);
- if (time() <= $followCustomerTime) {
- $data['followCustomer'] = (int)$followCustomerCount;
- } else {
- $followCustomer = $this->followCustomer(true);
- $data['followCustomer'] = $followCustomer['dataCount'] ? : 0;
- cache('followCustomerCount'.$userInfo['id'], $data['followCustomer']);
- cache('followCustomerTime'.$userInfo['id'], time() + 180);
- }
- # 待审核合同
- $checkContractTime = cache('checkContractTime'.$userInfo['id']);
- $checkContractCount = cache('checkContractCount'.$userInfo['id']);
- if (time() <= $checkContractTime) {
- $data['checkContract'] = (int)$checkContractCount;
- } else {
- $checkContract = $this->checkContract(true);
- $data['checkContract'] = $checkContract['dataCount'] ? : 0;
- cache('checkContractCount'.$userInfo['id'], $data['checkContract']);
- cache('checkContractTime'.$userInfo['id'], time() + 180);
- }
- # 待审核回款
- $checkReceivablesTime = cache('checkReceivablesTime'.$userInfo['id']);
- $checkReceivablesCount = cache('checkReceivablesCount'.$userInfo['id']);
- if (time() <= $checkReceivablesTime) {
- $data['checkReceivables'] = (int)$checkReceivablesCount;
- } else {
- $checkReceivables = $this->checkReceivables(true);
- $data['checkReceivables'] = $checkReceivables['dataCount'] ? : 0;
- cache('checkReceivablesCount'.$userInfo['id'], $data['checkReceivables']);
- cache('checkReceivablesTime'.$userInfo['id'], time() + 180);
- }
- # 待审核发票
- $checkInvoiceTime = cache('checkInvoiceTime'.$userInfo['id']);
- $checkInvoiceCount = cache('checkInvoiceCount'.$userInfo['id']);
- if (time() <= $checkInvoiceTime) {
- $data['checkInvoice'] = (int)$checkInvoiceCount;
- } else {
- $checkInvoice = $this->checkInvoice(true);
- $data['checkInvoice'] = $checkInvoice['dataCount'] ? : 0;
- cache('checkInvoiceCount'.$userInfo['id'], $data['checkInvoice']);
- cache('checkInvoiceTime'.$userInfo['id'], time() + 180);
- }
- # 待回款提醒
- $remindReceivablesPlanTime = cache('remindReceivablesPlanTime'.$userInfo['id']);
- $remindReceivablesPlanCount = cache('remindReceivablesPlanCount'.$userInfo['id']);
- if (time() <= $remindReceivablesPlanTime) {
- $data['remindReceivablesPlan'] = (int)$remindReceivablesPlanCount;
- } else {
- $remindReceivablesPlan = $this->remindReceivablesPlan(true);
- $data['remindReceivablesPlan'] = $remindReceivablesPlan['dataCount'] ? : 0;
- cache('remindReceivablesPlanCount'.$userInfo['id'], $data['remindReceivablesPlan']);
- cache('remindReceivablesPlanTime'.$userInfo['id'], time() + 180);
- }
- # 待回访合同
- $visitContractTime = cache('visitContractTime'.$userInfo['id']);
- $visitContractCount = cache('visitContractCount'.$userInfo['id']);
- if (time() <= $visitContractTime) {
- $data['returnVisitRemind'] = (int)$visitContractCount;
- } else {
- $visitContract = $this->visitContract(true);
- $data['returnVisitRemind'] = $visitContract['dataCount'] ? : 0;
- cache('visitContractCount'.$userInfo['id'], $data['returnVisitRemind']);
- cache('visitContractTime'.$userInfo['id'], time() + 180);
- }
- # 即将到期合同
- if ($configData['contract_config'] == 1) {
- $endContractTime = cache('endContractTime'.$userInfo['id']);
- $endContractCount = cache('endContractCount'.$userInfo['id']);
- if (time() <= $endContractTime) {
- $data['endContract'] = (int)$endContractCount;
- } else {
- $endContract = $this->endContract(true);
- $data['endContract'] = $endContract['dataCount'] ? : 0;
- cache('endContractCount'.$userInfo['id'], $data['endContract']);
- cache('endContractTime'.$userInfo['id'], time() + 180);
- }
- }
- # 待进入公海提醒
- if ($configData['remind_config'] == 1) {
- $remindCustomerTime = cache('remindCustomerTime'.$userInfo['id']);
- $remindCustomerCount = cache('remindCustomerCount'.$userInfo['id']);
- if (time() <= $remindCustomerTime) {
- $data['putInPoolRemind'] = (int)$remindCustomerCount;
- } else {
- $remindCustomer = $this->remindCustomer(true);
- $data['putInPoolRemind'] = $remindCustomer['dataCount'] ? : 0;
- cache('remindCustomerCount'.$userInfo['id'], $data['putInPoolRemind']);
- cache('remindCustomerTime'.$userInfo['id'], time() + 180);
- }
- }
-
- return resultArray(['data' => $data]);
- }
-
- /**
- * 今日需联系线索
- *
- * @param false $getCount
- * @return array|\think\response\Json
- */
- public function todayLeads($getCount = false)
- {
- $param = $this->param;
- $userId = $this->userInfo['id'];
- $types = $param['types'];
- $type = !empty($param['type']) ? $param['type'] : 1;
- $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
- $todayTime = getTimeByType('today');
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
-
- if ($getCount == true) $param['getCount'] = 1;
-
- $leadsModel = new \app\crm\model\Leads();
-
- # 负责人
- $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
- # 类型:1今日需联系;2已逾期;3已联系
- switch ($type) {
- case '1' :
- $param['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
- break;
- case '2' :
- $param['next_time'] = ['between', [1, time()]];
- break;
- case '3' :
- $param['last_time'] = ['between', [$todayTime[0], $todayTime[1]]];
- $param['follow'] = ['eq', '已跟进'];
- break;
- }
-
- $param['user_id'] = $userId;
- $data = $leadsModel->getDataList($param);
-
- if ($types == 'list') return resultArray(['data' => $data]);
-
- return $data;
- }
-
- /**
- * 今日需联系客户
- *
- * @param string $getCount
- * @return \think\response\Json
- */
- public function todayCustomer($getCount = false)
- {
- $param = $this->param;
- $userInfo = $this->userInfo;
- $types = $param['types'];
- $type = $param['type'] ? : 1;
- $isSub = $param['isSub'] ? : '';
- if ($getCount == true) {
- $param['getCount'] = 1;
- }
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
- $customerModel = model('Customer');
- $todayTime = getTimeByType('today');
-
- $param['owner_user_id'] = !empty($isSub) ? ['in',getSubUserId(false, 0, $userInfo['id'])] : $userInfo['id'];
-
- switch ($type) {
- case '1' :
- $param['next_time'] = ['between',array($todayTime[0],$todayTime[1])];
- // $param['follow'] = ['neq','已跟进'];
- break;
- case '2' :
- $param['next_time'] = ['between',array(1,time())];
- // $param['today_param'] = 'customer.next_time>record.update_time';
- break;
- case '3' :
- $param['last_time'] = ['between',array($todayTime[0],$todayTime[1])];
- $param['follow'] = ['eq','已跟进'];
- break;
- }
- $data = $customerModel->getDataList($param);
- if ($types == 'list') {
- return resultArray(['data' => $data]);
- }
- return $data;
- }
-
- /**
- * 今日需联系商机
- *
- * @param false $getCount
- * @return array|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function todayBusiness($getCount = false)
- {
- $param = $this->param;
- $userId = $this->userInfo['id'];
- $types = $param['types'];
- $type = !empty($param['type']) ? $param['type'] : 1;
- $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
- $todayTime = getTimeByType('today');
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
-
- if ($getCount == true) $param['getCount'] = 1;
-
- $businessModel = new \app\crm\model\Business();
-
- # 负责人
- $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userId)] : $userId;
- # 类型:1今日需联系;2已逾期;3已联系
- switch ($type) {
- case '1' :
- $param['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
- break;
- case '2' :
- $param['next_time'] = ['between', [1, time()]];
- break;
- case '3' :
- $param['last_time'] = ['between', [$todayTime[0], $todayTime[1]]];
- break;
- }
-
- $param['user_id'] = $userId;
- $data = $businessModel->getDataList($param);
-
- if ($types == 'list') return resultArray(['data' => $data]);
-
- return $data;
- }
-
- /**
- * 待跟进线索
- * @author Michael_xu
- * @return
- */
- public function followLeads($getCount = false)
- {
- $param = $this->param;
- $userInfo = $this->userInfo;
- $types = $param['types'];
- $type = $param['type'] ? : 1;
- $isSub = $param['isSub'] ? : '';
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
- $leadsModel = model('Leads');
-
- if ($getCount == true) $param['getCount'] = 1;
-
- $param['owner_user_id'] = $userInfo['id'];
-
- switch ($type) {
- case '1' : $param['follow'] = ['neq','已跟进']; break;
- case '2' : $param['follow'] = ['eq','已跟进']; break;
- }
- $param['user_id'] = $userInfo['id'];
- $data = $leadsModel->getDataList($param);
- if ($types == 'list') {
- return resultArray(['data' => $data]);
- }
- return $data;
- }
-
- /**
- * 待跟进客户
- * @author Michael_xu
- * @return
- */
- public function followCustomer($getCount = false)
- {
- $param = $this->param;
- $userInfo = $this->userInfo;
- $types = $param['types'];
- $type = $param['type'] ? : 1;
- $isSub = $param['isSub'] ? : '';
- if ($getCount == true) {
- $param['getCount'] = 1;
- }
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
- $customerModel = model('Customer');
-
- $param['owner_user_id'] = $userInfo['id'];
-
- switch ($type) {
- case '1' : $param['follow'] = ['eq','待跟进']; break;
- case '2' : $param['follow'] = ['eq','已跟进']; break;
- }
- $data = $customerModel->getDataList($param);
- if ($types == 'list') {
- return resultArray(['data' => $data]);
- }
- return $data;
- }
-
- /**
- * 待审核合同
- *
- * @param false $getCount
- * @return \think\response\Json
- */
- public function checkContract($getCount = false)
- {
- $param = $this->param;
- $userInfo = $this->userInfo;
- $types = $param['types'];
- $type = $param['type'] ? : 1;
- $isSub = $param['isSub'] ? : '';
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
- $contractModel = model('Contract');
- if ($getCount == true) {
- $param['getCount'] = 1;
- }
-
- switch ($type) {
- case '1' :
- $param['check_status'] = ['lt','2'];
- $param['check_user_id'] = ['like','%,'.$userInfo['id'].',%'];
- break;
- case '2' :
- // $param['check_status'] = ['egt','2'];
- $param['flow_user_id'] = ['like','%,'.$userInfo['id'].',%'];
- break;
- }
- $param['user_id'] = $userInfo['id'];
- $data = $contractModel->getDataList($param);
- if ($types == 'list') {
- return resultArray(['data' => $data]);
- }
- return $data;
- }
-
- /**
- * 待审核回款
- * @author Michael_xu
- * @return
- */
- public function checkReceivables($getCount = false)
- {
- $param = $this->param;
- $userInfo = $this->userInfo;
- $types = $param['types'];
- $type = $param['type'] ? : 1;
- $isSub = $param['isSub'] ? : '';
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
- $receivablesModel = model('Receivables');
- if ($getCount == true) $param['getCount'] = 1;
-
- switch ($type) {
- case '1' :
- # 待审核、审核中
- $param['check_status'] = ['lt','2'];
- $param['check_user_id'] = ['like','%,'.$userInfo['id'].',%'];
- break;
- case '2' :
- # 全部
- $param['flow_user_id'] = ['like','%,'.$userInfo['id'].',%'];
- break;
- }
- $param['user_id'] = $userInfo['id'];
- $data = $receivablesModel->getDataList($param);
- if ($types == 'list') {
- return resultArray(['data' => $data]);
- }
- return $data;
- }
-
- /**
- * 待审核发票
- *
- * @param InvoiceLogic $invoiceLogic
- * @return array|\think\response\Json
- * @throws \think\exception\DbException
- */
- public function checkInvoice($getCount = false)
- {
- $param = $this->param;
- $userId = $this->userInfo['id'];
- $types = $param['types'];
- $type = !empty($param['type']) ? $param['type'] : 1;
- $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
- if ($getCount == true) $param['getCount'] = 1;
-
- # 清除与模型无关的数据
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
-
- switch ($type) {
- case '1' :
- # 待审核、审核中
- $param['check_status'] = ['lt', 2];
- $param['check_user_id'] = ['like', '%,'. $userId .',%'];
- break;
- case '2' :
- # 全部
- $param['flow_user_id'] = ['like', '%,'. $userId .',%'];
- break;
- }
-
- $data = (new InvoiceLogic())->index($param);
-
- if ($types == 'list') return resultArray(['data' => $data]);
-
- return $data;
- }
-
- /**
- * 待回款提醒
- * @author Michael_xu
- * @return
- */
- public function remindReceivablesPlan($getCount = false)
- {
- $param = $this->param;
- $userInfo = $this->userInfo;
- $types = $param['types'];
- $type = $param['type'] ? : 1;
- $isSub = $param['isSub'] ? : '';
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
- $receivablesPlanModel = model('ReceivablesPlan');
-
- if ($getCount == true) $param['getCount'] = 1;
-
- $param['owner_user_id'] = $userInfo['id'];
- if ($isSub) {
- $param['owner_user_id'] = array('in',getSubUserId(false));
- }
- switch ($type) {
- case '1' : $param['receivables_id'] = 0;
- $param['check_status'] = array('lt',2);
- $param['remind_date'] = array('elt',date('Y-m-d',time()));
- $param['return_date'] = array('egt',date('Y-m-d',time()));
- $param['types'] = 1;
- break;
- case '2' : $param['receivables_id'] = array('gt',0);
- $param['check_status'] = 2;
- break;
- case '3' : $param['receivables_id'] = 0;
- $param['remind_date'] = array('lt',date('Y-m-d',time()));
- break;
- }
- $data = $receivablesPlanModel->getDataList($param);
- if ($types == 'list') {
- return resultArray(['data' => $data]);
- }
- return $data;
- }
-
- /**
- * 即将到期合同
- * @author Michael_xu
- * @return
- */
- public function endContract($getCount = false)
- {
- $param = $this->param;
- $userInfo = $this->userInfo;
- $types = $param['types'];
- $type = $param['type'] ? : 1;
- $isSub = $param['isSub'] ? : '';
- if ($getCount == true) $param['getCount'] = 1;
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
- $contractModel = model('Contract');
- $configModel = new \app\crm\model\ConfigData();
- $configInfo = $configModel->getData();
- $expireDay = $configInfo['contract_day'] ? : '7';
-
- $param['owner_user_id'] = $userInfo['id'];
- if ($isSub) {
- $param['owner_user_id'] = array('in',getSubUserId(false));
- }
- switch ($type) {
- case '1' :
- $param['end_time'] = array('between',array(date('Y-m-d',time()),date('Y-m-d',time()+86400*$expireDay)));
- $param['expire_remind'] = 1;
- break;
- case '2' : $param['end_time'] = array('lt',date('Y-m-d',time())); break;
- }
- $data = $contractModel->getDataList($param);
- if ($types == 'list') {
- return resultArray(['data' => $data]);
- }
- return $data;
- }
-
- /**
- * 待进入客户池(默认5天)
- * @author Michael_xu
- * @return
- */
- public function remindCustomer($getCount = false)
- {
- $customerModel = model('Customer');
-
- $param = $this->param;
- $userInfo = $this->userInfo;
- $types = $param['types'];
- $isSub = $param['isSub'] ? : '';
- if ($getCount == true) $param['getCount'] = 1;
- unset($param['types']);
- unset($param['type']);
- unset($param['isSub']);
- unset($param['deal_status']);
- unset($param['owner_user_id']);
-
- # 负责人
- $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false, 0, $userInfo['id'])] : $userInfo['id'];
-
- # 是否提醒
- $remind = db('crm_config')->where('name', 'remind_config')->value('value');
-
- $whereData = $param ? : [];
- $whereData['is_remind'] = !empty($remind) ? 1 : 0;
- $whereData['user_id'] = $userInfo['id'];
- $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => 'me'])->value('scene_id');
- if ($isSub) {
- $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => 'sub'])->value('scene_id');
- }
- $data = $customerModel->getDataList($whereData);
- if ($types == 'list') {
- return resultArray(['data' => $data]);
- }
- return $data;
- }
-
- /**
- * 待回访合同
- *
- * @param false $getCount
- * @return array|\think\response\Json
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function visitContract($getCount = false)
- {
- $param = $this->param;
- $userId = $this->userInfo['id'];
- $isSub = !empty($param['isSub']) ? $param['isSub'] : 0;
- $types = !empty($param['types']) ? $param['types'] : '';
- if ($getCount == true) $param['getCount'] = 1;
- unset($param['isSub']);
- unset($param['types']);
-
- $param['is_visit'] = 0; # 未回访
- $param['check_status'] = 2; # 审核通过
-
- $contractModel = new \app\crm\model\Contract();
-
- # 负责人
- $param['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
-
- $param['user_id'] = $userId;
- $data = $contractModel->getDataList($param);
-
- if ($types == 'list') return resultArray(['data' => $data]);
-
- return $data;
- }
-
- /**
- * 全部标记已处理
- *
- * @return \think\response\Json
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function allDeal()
- {
- $type = $this->param['type'];
- $typeId = !empty($this->param['type_id']) ? $this->param['type_id'] : '';
- $isSub = !empty($this->param['isSub']) ? $this->param['isSub'] : 0;
- $userId = $this->userInfo['id'];
-
- if (empty($type)) return resultArray(['error' => '缺少模块类型参数']);
-
- # 获得今日开始和结束时间戳
- $todayTime = getTimeByType('today');
-
- # 处理今日需联系线索、客户、商机
- if (in_array($type, ['todayLeads', 'todayCustomer', 'todayBusiness'])) {
- # 负责人
- $where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId;
- # 下次联系时间
- $where['next_time'] = ['between', [$todayTime[0], $todayTime[1]]];
-
- # 线索
- if ($type == 'todayLeads') {
- $leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
- Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update([
- 'next_time' => 0,
- 'last_time' => time(),
- 'follow' => '已跟进'
- ]);
- }
- # 客户
- if ($type == 'todayCustomer') {
- $customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
- Db::name('crm_customer')->whereIn('customer_id', $customerId)->update([
- 'next_time' => 0,
- 'last_time' => time(),
- 'follow' => '已跟进'
- ]);
- }
- # 商机
- if ($type == 'todayBusiness') {
- $businessId = !empty($typeId) ? $typeId : Db::name('crm_business')->where($where)->column('business_id');
- Db::name('crm_business')->whereIn('business_id', $businessId)->update([
- 'next_time' => 0,
- 'last_time' => time()
- ]);
- }
- }
-
- # 处理分配给我的线索、客户
- if (in_array($type, ['followLeads', 'followCustomer'])) {
- $where['owner_user_id'] = $userId;
- $where['follow'] = ['neq','已跟进'];
-
- # 线索
- if ($type == 'followLeads') {
- $leadsId = !empty($typeId) ? $typeId : Db::name('crm_leads')->where($where)->column('leads_id');
- Db::name('crm_leads')->whereIn('leads_id', $leadsId)->update(['follow' => '已跟进']);
- }
- # 客户
- if ($type == 'followCustomer') {
- $customerId = !empty($typeId) ? $typeId : Db::name('crm_customer')->where($where)->column('customer_id');
- Db::name('crm_customer')->whereIn('customer_id', $customerId)->update(['follow' => '已跟进']);
- }
- }
-
- # 处理待审核合同、回款、发票
- if (in_array($type, ['checkContract', 'checkReceivables', 'checkInvoice'])) {
- $where['check_status'] = ['lt','2'];
- $where['check_user_id'] = ['like','%,' . $userId . ',%'];
-
- $update = [
- 'check_status' => 2,
- 'flow_id' => 0,
- 'order_id' => 0,
- 'check_user_id' => '',
- 'flow_user_id' => ',' . $userId . ','
- ];
-
- # 合同
- if ($type == 'checkContract') {
- $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
- Db::name('crm_contract')->whereIn('contract_id', $contractId)->update($update);
- }
- # 回款
- if ($type == 'checkReceivables') {
- $receivablesId = !empty($typeId) ? $typeId : Db::name('crm_receivables')->where($where)->column('receivables_id');
- Db::name('crm_receivables')->whereIn('receivables_id', $receivablesId)->update($update);
- }
- # 发票
- if ($type == 'checkInvoice') {
- $invoiceId = !empty($typeId) ? $typeId : Db::name('crm_invoice')->where($where)->column('invoice_id');
- Db::name('crm_invoice')->whereIn('invoice_id', $invoiceId)->update($update);
- }
- }
-
- # 处理到期合同
- if ($type == 'endContract') {
- $configModel = new \app\crm\model\ConfigData();
- $configInfo = $configModel->getData();
- $expireDay = $configInfo['contract_day'] ? : '7';
-
- $where['owner_user_id'] = $userId;
- $where['end_time'] = ['between', [date('Y-m-d',time()), date('Y-m-d',time()+86400*$expireDay)]];
- $where['expire_remind'] = 1;
-
- $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
- Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['expire_remind' => 0]);
- }
-
- # 处理待回访合同
- if ($type == 'returnVisitRemind') {
- $where['owner_user_id'] = !empty($isSub) ? ['in', getSubUserId(false)] : $userId; # 负责人
- $where['is_visit'] = 0; # 未回访
- $where['check_status'] = 2; # 审核通过
-
- $contractId = !empty($typeId) ? $typeId : Db::name('crm_contract')->where($where)->column('contract_id');
- Db::name('crm_contract')->whereIn('contract_id', $contractId)->update(['is_visit' => 1]);
- }
-
- # 处理待进入公海
- if ($type == 'putInPoolRemind') {
- if (!empty($typeId)) {
- Db::name('crm_customer')->whereIn('customer_id', $typeId)->update([
- 'follow' => '已跟进',
- 'last_time' => time(),
- 'update_time' => time()
- ]);
- } else {
- $whereData['page'] = 1;
- $whereData['limit'] = 30;
- $whereData['is_remind'] = db('crm_config')->where('name', 'remind_config')->value('value');;
- $whereData['user_id'] = $userId;
- $whereData['scene_id'] = db('admin_scene')->where(['types' => 'crm_customer','bydata' => empty($isSub) ? 'me' : 'sub'])->value('scene_id');
- $poolCustomers = (new \app\crm\model\Customer())->getDataList($whereData);
- $ids = [];
- foreach ($poolCustomers['list'] AS $key => $value) {
- if (!empty($value['customer_id'])) $ids[] = $value['customer_id'];
- }
- if (!empty($ids)) Db::name('crm_customer')->whereIn('customer_id', $ids)->update([
- 'follow' => '已跟进',
- 'last_time' => time(),
- 'update_time' => time()
- ]);
- }
- }
-
-
- return resultArray(['data' => '操作成功!']);
- }
- }
|