request->only(['connect', 'label', 'start_date', 'end_date', 'keyword']); $employeeId = $this->request->token['employee_id']; $condition = [ ['customer.crm_res_id', 'NOTNULL', null], ['customer.employee_id', '=', $employeeId], ['customer.state', 'in', Customer::changeState('待确认', 'chaos')], ['customer.valid_time','NULL', null] ]; if (!empty($param['end_date'])) { $condition[] = ['customer.employee_time', '<', date('Y-m-d H:i:s', strtotime($param['end_date']) + 86400)]; } if (!empty($param['start_date'])) { $condition[] = ['customer.employee_time', '>=', date('Y-m-d H:i:s', strtotime($param['start_date']))]; } if (!empty($param['label'])) $condition[] = ['customer.remark', 'like', '%' . $param['label'] . '%']; $orCondition = []; if (!empty($param['keyword'])) { $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->encrypt($param['keyword']); $orCondition = [ [['customer.name', 'like', '%' . $param['keyword'] . '%']], [['customer.community_name', 'like', '%' . $param['keyword'] . '%']], [['customer.phone', '=', $value]] ]; } // 是否显示分配前的跟进信息 $visitReset = Setting::where([['root_id', '=', $this->request->token['root_org']], ['name', '=', 'visit_reset']])->findOrEmpty(); $visitResetSetting = !$visitReset->isEmpty() ? (int)$visitReset->content : 1; // 并未查已联系未联系 if (!isset($param['connect']) || $param['connect'] == '') { $data = Customer::alias('customer')->where($condition)->where(function ($query) use ($orCondition) { $query->whereOr($orCondition); })->field('id,sex,name,community_name,square,employee_time,remark,phone')->page($page, $limit)->order('updatetime desc')->select(); } else { $calCondition = [['out_call_log.employee_id', '=', $employeeId]]; $mbCalCondition = [['out_call_mb_log.employee_id', '=', $employeeId]]; $visitCondition = [['customer_visit_log.employee_id', '=', $employeeId], ['customer_visit_log.remark', '<>', '资源库分配']]; if (!$visitResetSetting) { $calCondition[] = ['out_call_log.addtime', '>', Db::raw('customer.employee_time')]; $mbCalCondition[] = ['out_call_mb_log.addtime', '>', Db::raw('customer.employee_time')]; $visitCondition[] = ['customer_visit_log.addtime', '>', Db::raw('customer.employee_time')]; } // 查询已联系客户id及联系次数 $calNum = OutCallLog::withJoin('customer') ->where($condition) ->where($calCondition) ->where([['is_redistribution','=',0]])//只查询重新分配之后的外呼记录 ->where(function ($query) use ($orCondition) { $query->whereOr($orCondition); })->group('customer.id') ->column('count(customer.id) as num', 'customer.id'); // 手机直接拨打 $mbCalNum = OutCallMbLog::withJoin('customer') ->where($condition) ->where($mbCalCondition) ->where([['is_redistribution','=',0]])//只查询重新分配之后的外呼记录 ->where(function ($query) use ($orCondition) { $query->whereOr($orCondition); })->group('customer.id') ->column('count(customer.id) as num', 'customer.id'); // 跟进过的客户 $visitNum = CustomerVisitLog::withJoin('customer') ->where($condition) ->where($visitCondition) ->where(function ($query) use ($orCondition) { $query->whereOr($orCondition); })->group('customer.id') ->column('count(customer.id) as num', 'customer.id'); $connectCustomerId = array_keys($calNum); $connectCustomerId1 = array_keys($mbCalNum); $connectCustomerId2 = array_keys($visitNum); $connectCustomerId = array_merge_recursive($connectCustomerId, $connectCustomerId1, $connectCustomerId2); $connectCustomerId = array_unique($connectCustomerId); // 查询已联系未联系 $order = 'updatetime desc'; if ($param['connect'] < 0) $condition[] = ['id', 'not in', $connectCustomerId]; else { $order = 'updatetime desc'; $condition[] = ['id', 'in', $connectCustomerId]; } $data = Customer::alias('customer')->where($condition)->where(function ($query) use ($orCondition) { $query->whereOr($orCondition); })->field('id,sex,name,community_name,square,employee_time,remark,phone')->page($page, $limit)->order($order)->select(); } if (!$visitResetSetting) { $data = $data->toArray(); $pageCustomerId = array_column($data, 'id'); $visited = CustomerVisitLog::withJoin('customer') ->where([ ['customer_visit_log.customer_id', 'in', $pageCustomerId], ['customer_visit_log.addtime', '>', Db::raw('customer.employee_time')] ])->group('customer_visit_log.customer_id')->column('customer_visit_log.customer_id'); foreach ($data as &$d) { if (!in_array($d['id'], $visited)) $d['remark'] = ''; } } return json(['code' => self::success, 'data' => $data]); } /** * 统计 */ public function statistics() { $param = $this->request->only(['start_date', 'end_date']); $startDate = empty($param['end_date']) ? date('Y-m-d H:i:s', 0) : date('Y-m-d H:i:s', strtotime($param['start_date'])); $endDate = empty($param['end_date']) ? date('Y-m-d H:i:s') : date('Y-m-d H:i:s', strtotime($param['end_date']) + 86400); $employeeId = $this->request->token['employee_id']; $data = ['total' => 0, 'call_total' => 0, 'call_put_on' => 0, 'not_connected' => 0, 'billsec' => 0, 'wechat_num' => 0, 'intention' => 0]; // 客户资源总数 $data['total'] = Customer::where([ ['crm_res_id', 'NOTNULL', null], ['employee_id', '=', $employeeId], //['state', 'in', Customer::changeState('待确认', 'chaos')], ['employee_time', '>=', $startDate], ['employee_time', '<', $endDate], ])->count(); // $list = Customer::with(['outLog' => function ($query) use ($employeeId, $startDate, $endDate) { // $query->field('id,customer_id,billsec,status')->where([['employee_id', '=', $employeeId]]); // }])->field('id,add_wechat_time') // ->where([ // ['crm_res_id', 'NOTNULL', null], // ['employee_id', '=', $employeeId], // ['addtime', '>=', $startDate], // ['addtime', '<', $endDate], // ]) // ->select(); // foreach ($list as $i) { // if (!empty($i->add_wechat_time)) $data['wechat_num']++; // 已加微信数 // foreach ($i->outLog as $o) { // // 拨打电话数 // $data['call_total']++; // $o->status > 0 ? $data['call_put_on']++ : $data['not_connected']++; // 拨打电话数 未接通数 // $data['billsec'] += $o->billsec; // 通话时长 // } // } // 电话量大统计按时间统计不再按客户分配时间统计(外呼系统) $list = OutCallLog::hasWhere('customer', function ($query) use ($employeeId) { $query->where([ ['crm_res_id', 'NOTNULL', null] ]); })->where([ ['OutCallLog.addtime', '>=', $startDate], ['OutCallLog.addtime', '<', $endDate], ['OutCallLog.employee_id', '=', $employeeId] ])->select(); foreach ($list as $item) { $data['call_total']++; $item->status > 0 ? $data['call_put_on']++ : $data['not_connected']++; $data['billsec'] += $item->billsec; } // 普通电话拨打记录 $mbCount = OutCallMbLog::hasWhere('customer', function ($query) use ($employeeId) { $query->where([ ['crm_res_id', 'NOTNULL', null] ]); })->where([ ['OutCallMbLog.addtime', '>=', $startDate], ['OutCallMbLog.addtime', '<', $endDate], ['OutCallMbLog.employee_id', '=', $employeeId] ])->count(); $data['call_total'] += $mbCount; $data['wechat_num'] = Customer::where([ ['crm_res_id', 'NOTNULL', null], ['employee_id', '=', $employeeId], ['add_wechat_time', '>=', $startDate], ['add_wechat_time', '<', $endDate], ])->count(); $data['intention'] = Customer::where([ ['crm_res_id', 'NOTNULL', null], ['employee_id', '=', $employeeId], ['valid_time', '>=', $startDate], ['valid_time', '<', $endDate], ])->count(); return json(['code' => self::success, 'data' => $data]); } /** * 标签添加 */ public function label($cid, $v) { $customer = Customer::where([ ['id', '=', $cid], ['crm_res_id', 'NOTNULL', null], ['employee_id', '=', $this->request->token['employee_id']], ['state', 'in', Customer::changeState('待确认', 'chaos')] ])->find(); if (empty($customer)) return json(['code' => self::error_msg, 'msg' => '客户不存在']); // 将汉语中的#替换成英文# $v = str_replace('#', '#', $v); // 检测是否有标签 if (preg_match('/#(?P