123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918 |
- <?php
- namespace app\api\controller;
- use app\model\Customer;
- use app\model\CustomerVisitLog;
- use app\model\Employee;
- use app\model\OutCallLog;
- use app\model\OutCallMbLog;
- use app\model\Setting;
- use think\facade\Db;
- use toolkits\Aec;
- use app\model\CustomerSource;
- use app\model\Company;
- use app\model\FishData;
- use app\model\Org;
- use app\model\CustomerPortraitField;
- use clue\Fish;
- class Resources extends Base
- {
- /**
- * 客户分配列表
- */
- public function list($page = 1, $limit = 20)
- {
- $param = $this->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<label>.*)#.*/', $v, $matches)) {
- if ($matches['label'] !== '有效') {
- $customer->save(['remark' => $matches['label']]);
- } else {
- // 同部门能否重复录入开关设置
- $root_id = request()->token['root_org'];
- $empcrm_repeat[] = ['root_id', '=', $root_id];
- $empcrm_repeat[] = ['name', '=', 'empcrm_customer_repeat'];
- $repeat_setting = Setting::where($empcrm_repeat)->findOrEmpty();
- $emp_org = request()->token['org_id'];
- //判断同部门
- if (!$repeat_setting->isEmpty()) {
- $repeat_org = explode(',', $repeat_setting['content']);
- if (in_array($emp_org, $repeat_org)) {
- // 需要验证
- // 新建档,检测手机号是否重复
- $customerExist = Customer::where([
- ['phone|phone1|phone2', '=', cypherphone($customer['phone'])],
- ['org_id', 'in', $repeat_org],
- ['employee_id', '>', 0] // 员工客户
- ])->where(
- function($query) {
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', '>', 0];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- }
- )->field('id,employee_id,is_resource,org_id')->select()->toArray();
- if (!empty($customerExist)) {
- foreach ($customerExist as $ex) {
- if ($ex['employee_id'] != $customer['employee_id']) {
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 1, 'msg' => '该客户与员工' . $have_emp . '撞单,无法标记有效']);
- }
- }
- }
- }
- }
-
- $customer->save(['state' => 1, 'valid_time' => date('Y-m-d H:i:s')]);
- }
- }
- $employee = Employee::find($this->request->token['employee_id']);
- // 添加跟进记录
- CustomerVisitLog::Create([
- 'customer_id' => $customer->id,
- 'type' => 1,
- 'employee_id' => $this->request->token['employee_id'],
- 'user_id' => $employee->uid,
- 'org_id' => $employee->org_id,
- 'customer_employee_id' => $customer->employee_id,
- 'customer_org_id' => $customer->org_id,
- 'data_type' => 'out_call',
- 'remark' => $v
- ]);
- return json(['code' => self::success, 'msg' => '标识成功']);
- }
- /**
- * 标签列表接口
- */
- public function labelList()
- {
- $label = Setting::where(['root_id' => $this->request->token['root_org'], 'name' => 'crm_label'])->value('content');
- $label = empty($label) ? [] : explode(',', $label);
- return json(['code' => self::success, 'data' => $label]);
- }
- /**
- * 标注记录接口
- */
- public function labelog($cid){
- $log = CustomerVisitLog::where(['employee_id' => $this->request->token['employee_id'], 'customer_id' => $cid, 'data_type' => 'out_call'])->field('addtime,remark')->order('addtime desc')->select();
- return json(['code'=>self::success, 'data'=>$log]);
- }
-
- /**
- * 飞鱼线索
- */
- public function lableClueList()
- {
- $channel_setting = Company::where(['root_id' => $this->request->token['root_org']])->value('channel_setting');
- if(isset($channel_setting) && $channel_setting){
- $empdata = Employee::find($this->request->token['employee_id']);
- // $employeeId = $this->request->token['employee_id'];
- // $empdata['clue_disable'] == 0//允许查看
- $tmp_data = json_decode($channel_setting, true);
- $fish = isset($tmp_data['fish_access_token']) && $empdata['clue_disable'] == 0 ? true : false;
- $tx = isset($tmp_data['tx_access_token']) && $empdata['clue_disable'] == 0 ? true : false;
- return json(['code' => self::success, 'data' => ['fish' => $fish, 'tx' => $tx,'root_id' => $this->request->token['root_org']]]);
- }
- return json(['code' => self::success, 'data' => ['fish' => false,'tx' => false, 'root_id' => $this->request->token['root_org']]]);
- }
- /**
- * 飞鱼资源线索统计
- */
- public function statistics_fish()
- {
- $token = $this->request->token;
- $param = $this->request->only(['start_date', 'end_date', 'type' => 1]);
- $employeeId = $token['employee_id'];
- $data = ['total' => 0, 'call_total' => 0, 'call_put_on' => 0, 'not_connected' => 0, 'billsec' => 0, 'allocation'=>0,'zhuang'=>0];
- //如果是部门领导
- $is_manager = $token['isManager'];
- $condition[] = ['root_id', '=', $token['root_org']];
- if ($param['type'] == 2) {
- $condition[] = ['type', '=', $param['type']];
- } else {
- $condition[] = ['type', 'in', [1, 4]];
- }
- $org_employee = [$employeeId];
- if($is_manager == 1){
- $team_orgs = orgSubIds($token['org_id']);
- $condition[] = ['org_id', 'in', $team_orgs];
- }else{
- $condition[] = ['employee_id', 'in', $org_employee];
- }
- if (!empty($param['end_date'])) {
- $condition[] = ['create_time_detail', '<', date('Y-m-d H:i:s', strtotime($param['end_date']) + 86400)];
- }
- if (!empty($param['start_date'])) {
- $condition[] = ['create_time_detail', '>=', date('Y-m-d H:i:s', strtotime($param['start_date']))];
- }
- $fish_list = FishData::where($condition)->field('id,customer_id,employee_id,is_allocation,employee_remark')->select();
- $data['total'] = count($fish_list);
- $fish_id = [];
- foreach ($fish_list as $val) {
- $fish_id[] = $val['id'];
- if ($val['is_allocation'] == 1) $data['allocation']++; //已分配数量
- $pos = strpos($val['employee_remark'], '撞单');
- if($pos !== false) $data['zhuang']++;
- }
- $list = OutCallLog::where([['call_id', 'in', $fish_id]])->select();
- foreach ($list as $item) {
- $data['call_total']++;
- $item->status > 0 ? $data['call_put_on']++ : $data['not_connected']++;
- $data['billsec'] += $item->billsec;
- }
- return json(['code' => self::success, 'data' => $data]);
- }
- /**
- * 飞鱼标签
- */
- public function list_fish_tags()
- {
- $table_tags = [
- [
- 'title' => '高级意向',
- 'name' => 'all0',
- 'value' => 0,
- 'tags' =>
- [
- ['title' => '已签约合同', 'name' => 'all0_1'],
- ['title' => '已缴纳定金', 'name' => 'all0_2'],
- ['title' => '客户已到店', 'name' => 'all0_3'],
- ['title' => '预约上门量房', 'name' => 'all0_4'],
- ]
- ],
- [
- 'title' => '中级意向',
- 'name' => 'all1',
- 'value' => 1,
- 'tags' =>
- [
- ['title' => '能提供户型图', 'name' => 'all1_1'],
- ['title' => '有装修预算', 'name' => 'all1_2'],
- ['title' => '添加微信待通过', 'name' => 'all1_3'],
- ['title' => '添加微信已通过', 'name' => 'all1_4'],
- ]
- ],
- [
- 'title' => '初级意向',
- 'name' => 'all2',
- 'value' => 2,
- 'tags' =>
- [
- ['title' => '仅咨询了解', 'name' => 'all2_1'],
- ['title' => '无真实需求', 'name' => 'all2_2'],
- ['title' => '对比其他平台', 'name' => 'all2_3'],
- ['title' => '对价格不反感', 'name' => 'all2_4'],
- ['title' => '近期有装修需求', 'name' => 'all2_5'],
- ['title' => '添加微信', 'name' => 'all2_6'],
- ]
- ],
- [
- 'title' => '意向',
- 'name' => 'all3',
- 'value' => 3,
- 'tags' =>
- [
- ['title' => '低意向', 'name' => 'all3_1'],
- ['title' => '中意向', 'name' => 'all3_2'],
- ['title' => '高意向', 'name' => 'all3_3'],
- ]
- ],
- [
- 'title' => '无效',
- 'name' => 'all4',
- 'value' => 4,
- 'tags' =>
- [
- ['title' => '添加微信未通过', 'name' => 'all4_1'],
- ['title' => '多次联系不上', 'name' => 'all4_2'],
- ['title' => '拒绝加微信', 'name' => 'all4_3'],
- ['title' => '无需求', 'name' => 'all4_4'],
- ['title' => '错号', 'name' => 'all4_5'],
- ['title' => '关机', 'name' => 'all4_6'],
- ['title' => '空号', 'name' => 'all4_7'],
- ['title' => '联系不上', 'name' => 'all4_8'],
- ['title' => '无意愿', 'name' => 'all4_9'],
- ['title' => '非本人', 'name' => 'all4_10']
- ]
- ],
- [
- 'title' => '默认',
- 'name' => 'all5',
- 'value' => 5,
- 'tags' =>
- [
- ['title' => '加微信', 'name' => 'all5_1'],
- ['title' => '待确认', 'name' => 'all5_2'],
- ['title' => '无效', 'name' => 'all5_3'],
- ]
- ],
- ];
- return json(['code' => self::success, 'data' => $table_tags]);
- }
- /**
- * 详情
- */
- public function list_fish_info()
- {
- $token = $this->request->token;
- $is_manager = $token['isManager'];
- $emp_id = $token['employee_id'];
- $param = $this->request->only(['id']);
- $info = FishData::where(['id' => $param['id']])->find();
- if(empty($info)) return json(['code' => 1, 'msg' => '线索信息有误']);
- if($info['type'] == 2){
- $clue_source = [
- '0' => '未知',
- '1' => '其他',
- '2' => '外部导入',
- '3' => '广告创意',
- '4' => '蹊径落地页',
- '5' => '一叶智能',
- '6' => '微信原生推广页',
- '7' => '讯达小程序',
- '8'=>'互动落地页',
- '9'=>'教培通'
- ];
- $clue_state = [
- 'LEADS_FOLLOW_TAG_DEFAULT' => '未分配',
- 'LEADS_FOLLOW_TAG_ASSIGNED' => '已分配待跟进',
- 'LEADS_FOLLOW_TAG_FOLLOWING' => '跟进中',
- 'LEADS_FOLLOW_TAG_VALID' => '已转换-潜在客户',
- 'LEADS_FOLLOW_TAG_VALID_HIGH_INTENTION' => '已转换-高意向客户',
- 'LEADS_FOLLOW_TAG_VALID_CONVERTED' => '已转换-已成单',
- 'LEADS_FOLLOW_TAG_INVALID_REPEAT' => '无效-重复',
- 'LEADS_FOLLOW_TAG_INVALID_NOTSELF' => '无效-非本人',
- 'LEADS_FOLLOW_TAG_INVALID_NOTTOUCH' => '无效-未接通',
- 'LEADS_FOLLOW_TAG_INVALID_NOTINTENTION' => '无效-无意向',
- 'LEADS_FOLLOW_TAG_INVALID_REGION_MISMATCHED' => '无效-定向外',
- 'LEADS_FOLLOW_TAG_INVALID_OTHER' => '无效-其他',
- 'LEADS_INEFFECT_REASON_TEL_NOT_CONNECTED' => '推送',
- ];
- $info['clue_state_name'] = $clue_state[$info['clue_state_name']];
- }else{
- $clue_type = [0 => '表单提交', 1 => '在线咨询', 2 => '智能电话', 3 => '网页回呼', 4 => '卡券', 5 => '抽奖'];
- $clue_source = [0 => '外部流量', 1 => '正常投放', 2 => '外部导入', 3 => '异常提交', 4 => '广告预览', 5 => '抖音私信', 6 => '鲁班线索'];
- // $clue_state = [1 => '新线索', 2 => '有意向', 3 => '转商机', 4 => '无效', 5 => '已加微信', 6 => '待再次沟通'];
- $info['clue_type'] = $clue_type[$info['clue_type']];
- }
- $info['clue_source'] = $clue_source[$info['clue_source']];
- $info['telephone'] = substr_replace($info['telephone'], '******', 3, 6);
- $info['gender'] = ['未知','男','女'][$info['gender']];
- $info['allocation_status'] = $info['is_allocation'] == 1 ? '已分配' : '未分配';
- $info['is_followup'] = 0;
- if($emp_id == $info['employee_id']) $info['is_followup'] = 1;
-
- if(!empty($info['remark_dict'])){
- $tmp = json_decode($info['remark_dict'],true);
- $info['remark_dict'] = implode(',',$tmp);
- }
- return json(['code' => 0, 'data' => $info]);
- }
- /**
- * 更新数据
- */
- public function list_fish_info_update()
- {
- // $request = request();
- $param = $this->request->only(['id', 'address', 'age', 'date', 'email', 'gender', 'location', 'name', 'qq', 'remark', 'weixin', 'employee_remark', 'tags']);
- $data = FishData::find($param['id']);
- if (empty($data)) return json(['code' => 1, 'msg' => '数据信息有误']);
- $param['gender'] = $param['gender'] == '男' ? 1 : ($param['gender'] == '女' ? 2 : 0);
- $data->save($param);
- return json(['code' => 0, 'msg' => '确认完成']);
- }
- /**
- * 客户分配列表
- */
- public function list_fish($page = 1, $limit = 20)
- {
- $token = $this->request->token;
- $param = $this->request->only(['connect', 'start_date', 'end_date', 'keyword','allocation','type']);
- $employeeId = $token['employee_id'];
- $order = 'id desc';
- //如果是部门领导
- $is_manager = $token['isManager'];
- $condition[] = ['root_id', '=', $token['root_org']];
- if ($param['type'] == 1) {
- $condition[] = ['type', 'in', [1, 4]];
- } else {
- $condition[] = ['type', '=', $param['type']];
- }
- $org_employee = [$employeeId];
- if($is_manager == 1){
- $team_orgs = orgSubIds($token['org_id']);
- $condition[] = ['org_id', 'in', $team_orgs];
- }else{
- $condition[] = ['employee_id', 'in', $org_employee];
- }
- if (!empty($param['end_date'])) {
- $condition[] = ['create_time_detail', '<', date('Y-m-d H:i:s', strtotime($param['end_date']) + 86400)];
- }
- if (!empty($param['start_date'])) {
- $condition[] = ['create_time_detail', '>=', date('Y-m-d H:i:s', strtotime($param['start_date']))];
- }
- if (!empty($param['connect'])) {
- if ($param['connect'] == 1) {
- $condition[] = ['is_contact', '=', 1];
- $order = 'is_contact desc,update_time_detail desc';
- }
- if ($param['connect'] == -1) {
- $condition[] = ['is_contact', '=', 0];
- $order = 'is_contact asc';
- }
- }
- if (!empty($param['allocation'])) {
- if ($param['allocation'] == 1) {
- $condition[] = ['is_allocation', '=', 1];
- }
- if ($param['allocation'] == -1) {
- $condition[] = ['is_allocation', '=', 0];
- }
- }
- $orCondition = [];
- if (!empty($param['keyword'])) {
- $orCondition = [
- [['location', 'like', '%' . $param['keyword'] . '%']],
- [['clue_owner_name', 'like', '%' . $param['keyword'] . '%']],
- [['telephone', '=', $param['keyword']]]
- ];
- }
- $path = Org::where([['pid','=',$token['org_id']]])->count();
- $data = FishData::where($condition)->field('id,name,telephone,location,create_time_detail,is_allocation,employee_id,is_contact')->whereOr($orCondition)->page($page, $limit)->order($order)->select();
- foreach($data as $key => $val){
- $data[$key]['phone'] = $val['telephone'];
- $data[$key]['is_followup'] = 0;
- if($is_manager==1 && empty($path)){
- $data[$key]['is_followup'] = 1;
- }
- if($employeeId == $data[$key]['employee_id']){
- $data[$key]['is_followup'] = 2;
- }
- }
-
- return json(['code' => self::success, 'data' => $data]);
- }
- //拨号
- public function create_bridge_call()
- {
- $token = $this->request->token;
- $root_id = $token['root_org'];
- $employee_id = $token['employee_id'];
- $param = $this->request->only(['customer_id']);
- $fish_data = FishData::where(['id' => $param['customer_id']])->find();
- if (empty($fish_data)) return json(['code' => 1, 'msg' => '线索信息有误']);
- $empcrm = Employee::where('id', $token['employee_id'])->find();
- $employeePhone = $empcrm['phone'];
- $customerPhone = $fish_data['telephone'];
- if ($fish_data['type'] == 1) {
- $fish = new Fish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- } elseif($fish_data['type'] == 4) {
- $fish = new Fish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh'));
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
- } else {
- //腾讯外呼目前直接返回手机号
- // 添加记录
- $logData = [
- 'session_id' => str_rand(10),
- 'bind_num' => $customerPhone,
- 'caller_num' => $employeePhone,
- 'callee_num' => $customerPhone,
- 'employee_id' => $employee_id,
- 'root_id' => $root_id,
- 'customer_id' => 0, //$param['customer_id']
- 'call_id' => $param['customer_id'] // 存储飞鱼线索id
- ];
- $log = OutCallLog::create($logData);
- // 添加访问记录
- CustomerVisitLog::Create([
- 'customer_id' => 0,
- 'type' => 1,
- 'employee_id' => $employee_id,
- 'user_id' => $empcrm['uid'],
- 'org_id' => $empcrm['org_id'],
- 'customer_employee_id' => 0,
- 'customer_org_id' => 0,
- 'data_type' => 'out_call',
- 'data_id' => $log->id,
- 'remark' => '回访跟踪了腾讯线索客户'
- ]);
- $fish_data->save(['is_contact' => 1, 'update_time_detail' => date('Y-m-d H:i:s')]);
- return ['code' => 0, 'msg' => '拨打中,请接听', 'mobile' => $customerPhone];
- }
- // $fish = new Fish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
- // $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
- $tmp_data = json_decode($channel_setting, true);
- $tmp_info = [
- //广告主id
- 'advertiser_id' => intval($fish_data['advertiser_id']),
- //线索id
- 'clue_id' => intval($fish_data['clue_id']),
- // 主叫号码,必须为11位手机号码,否则呼叫失败
- 'caller_number' => $employeePhone,//'15713803101'
- // 被叫号码,即线索号码
- 'callee_number' => $customerPhone,
- ];
- $rs = $fish->getCallVirtualNumber($tmp_data['fish_access_token'], $tmp_info);
- if ($rs['code'] != 0) {
- $virtual_number = $customerPhone;
- // 添加记录
- $logData = [
- 'session_id' => str_rand(10),
- 'bind_num' => $virtual_number,
- 'caller_num' => $employeePhone,
- 'callee_num' => $customerPhone,
- 'employee_id' => $employee_id,
- 'root_id' => $root_id,
- 'customer_id' => 0, //$param['customer_id']
- 'call_id' => $param['customer_id'] // 存储飞鱼线索id
- ];
- $log = OutCallLog::create($logData);
- // return json(['code' => 1, 'msg' => '呼叫失败']);
- } else {
- if($rs['data']['call_result_code'] != 103){
- return json(['code' => 1, 'msg' => $rs['data']['call_result_message']]);
- }
- $virtual_number = $rs['data']['virtual_number'];
- // 添加记录
- $logData = [
- 'session_id' => $rs['data']['contact_id'],
- 'bind_num' => $virtual_number,
- 'caller_num' => $employeePhone,
- 'callee_num' => $customerPhone,
- 'employee_id' => $employee_id,
- 'root_id' => $root_id,
- 'customer_id' => 0, //$param['customer_id']
- 'call_id' => $param['customer_id'] // 存储飞鱼线索id
- ];
- $log = OutCallLog::create($logData);
- }
- // 添加访问记录
- CustomerVisitLog::Create([
- 'customer_id' => 0,
- 'type' => 1,
- 'employee_id' => $employee_id,
- 'user_id' => $empcrm['uid'],
- 'org_id' => $empcrm['org_id'],
- 'customer_employee_id' => 0,
- 'customer_org_id' => 0,
- 'data_type' => 'out_call',
- 'data_id' => $log->id,
- 'remark' => '回访跟踪了飞鱼线索客户'
- ]);
- $fish_data->save(['is_contact' => 1, 'update_time_detail' => date('Y-m-d H:i:s')]);
- if (isset($virtual_number)) {
- return ['code' => 0, 'msg' => '拨打中,请接听', 'mobile' => $virtual_number];
- } else {
- return ['code' => 1, 'msg' => '拨打中,请接听'];
- }
- }
- /**
- * 童通话记录接口
- */
- public function labelog_fish($cid){
- $log = OutCallLog::where(['call_id' => $cid])->field('addtime,call_msg as remark')->order('addtime desc')->select();
- foreach ($log as &$val) {
- $val['remark'] = (isset($val['remark']) && $val['remark']) ? $val['remark'] : '跟踪回访了此客户';
- }
- return json(['code'=>self::success, 'data'=>$log]);
- }
- /**
- * 转化
- */
- public function fish_validation()
- {
- $token = $this->request->token;
- $root_id = $token['root_org'];
- $empid = $token['employee_id'];
- $orgids = orgSubIds($root_id);
- $param = $this->request->only(['customer_id']);
- $customer_info = FishData::where(['id'=>$param['customer_id']])->find();
- if(empty($customer_info)) return json(['code' => 1, 'msg' => '线索信息有误']);
- if($customer_info['is_allocation'] == 1) return json(['code' => 1, 'msg' => '线索已转化,无法重复转化']);
- $customerExist = Customer::where([
- ['phone|phone1|phone2', '=', cypherphone($customer_info['telephone'])],
- ['org_id', 'in', $orgids],
- ['employee_id', '>', 0]
- ])->where(
- function($query) {
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', '>', 0];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- }
- )->field('id,employee_id,is_resource,org_id,source_id')->select()->toArray();
- if (!empty($customerExist)) {
- foreach ($customerExist as $ex) {
- if ($ex['employee_id'] != $empid) {
- //查询撞单客户渠道
- $source_name = '';
- if ($ex['source_id']) {
- $source = CustomerSource::where(['id' => $ex['source_id']])->value('source');
- $source_name = $source . '渠道客户';
- }
- $have_emp = Employee::where('id', '=', $ex['employee_id'])->value('name');
- return json(['code' => 1, 'msg' => '该客户与员工' . $have_emp . $source_name . '撞单,无法转化']);
- }
- }
- }
- //检查线索来源
- if ($customer_info['type'] == 1 || $customer_info['type'] == 4) {
- $fish_source = CustomerSource::where(['root_id' => $root_id, 'source' => '飞鱼线索'])->find();
- } else {
- $fish_source = CustomerSource::where(['root_id' => $root_id, 'source' => '腾讯线索'])->find();
- }
- //检查是否被分配
- if (empty($customer_info['employee_id']) || !isset($customer_info['employee_id'])) return json(['code' => 1, 'msg' => '请先分配线索']);
- $employee_info = Employee::where(['id' => $customer_info['employee_id'], 'root_id' => $root_id])->field('id,name,org_id,uid')->find();
- if (!$employee_info) return json(['code' => 1, 'msg' => '员工信息有误']);
- $save['employee_id'] = $customer_info['employee_id'];
- $save['phone'] = $customer_info['telephone'];
- $save['name'] = $customer_info['name'];
- $save['is_distribution'] = 0;
- $save['is_resource'] = 0;
- $save['state'] = 1;
- $save['community_name'] = $customer_info['location'];
- $save['addtime'] = $customer_info['create_time_detail'];
- $save['org_id'] = $employee_info['org_id'];
- $save['sex'] = boolval($employee_info['gender']);
- $save['source_id'] = $fish_source['id'];
- $save['employee_time'] = date('Y-m-d H:i:s');
- $save['valid_time'] = date('Y-m-d H:i:s');
- //客户来源扩展字段
- $visitField_decode = [];
- $visit_default = CustomerPortraitField::where([['root_id', '=', $root_id], ['keyname', 'in', ['source_id', 'sign_time']]])->column('id', 'keyname');
- if (isset($visit_default['sign_time']) && $visit_default['sign_time']){
- $visitField_decode[] = ['id' => $visit_default['sign_time'], 'keyname' => 'sign_time', 'value' => $customer_info['create_time_detail']];
- }
- if (isset($visit_default['source_id']) && $visit_default['source_id']){
- $visitField_decode[] = ['id' => $visit_default['source_id'], 'keyname' => 'source_id', 'value' => $fish_source['id']];
- }
- $save['ext'] = !empty($visitField_decode) ? json_encode($visitField_decode) : NULL;
- $ms = Customer::create($save);
- if ($ms) {
- FishData::where(['id' => $param['customer_id']])->update(['is_allocation' => 1, 'customer_id' => $ms->id, 'is_contact' => 1]);
- //更新通话记录
- $log_list = OutCallLog::where(['call_id' => $param['customer_id']])->select()->toArray();
- OutCallLog::where(['call_id' => $param['customer_id']])->update(['customer_id' => $ms->id]);
- foreach ($log_list as $val) {
- CustomerVisitLog::where(['data_id' => $val['id'], 'data_type' => 'out_call'])->update(['customer_id' => $ms->id]);
- }
- if(!isset($log_list) || empty($log_list)){
- CustomerVisitLog::Create([
- 'customer_id' => $ms->id,
- 'type' => '',
- 'employee_id' => $empid,
- 'user_id' => $employee_info['uid'],
- 'org_id' => $employee_info['org_id'],
- 'customer_employee_id' => 0,
- 'customer_org_id' => 0,
- 'data_type' => '',
- 'data_id' => '',
- 'remark' => '标记有效'
- ]);
- }
- return json(['code' => 0, 'msg' => '确认完成']);
- } else {
- return json(['code' => 1, 'msg' => '确认失败']);
- }
- }
- /**
- * 重新分配员工
- */
- public function fish_validation_again()
- {
- $token = $this->request->token;
- $root_id = $token['root_org'];
- $param = $this->request->only(['customer_id', 'employee_id']);
- $data = FishData::find($param['customer_id']);
- if (empty($data)) return json(['code' => 1, 'msg' => '数据信息有误']);
- $employee_info = Employee::where(['id' => $param['employee_id'], 'root_id' => $root_id])->field('id,name,org_id')->find();
- if (!$employee_info) return json(['code' => 1, 'msg' => '员工信息有误']);
- $data->save(['allocation_status' => 1, 'org_id' => $employee_info['org_id'], 'employee_id' => $employee_info['id'], 'clue_owner_name' => $employee_info['name']]);
- return json(['code' => 0, 'msg' => '确认完成']);
- }
- /**
- * 重新分配员工
- */
- public function fish_validation_lable()
- {
- $token = $this->request->token;
- $root_id = $token['root_org'];
- $param = $this->request->only(['customer_id', 'remark']);
- $data = FishData::find($param['customer_id']);
- if (empty($data)) return json(['code' => 1, 'msg' => '数据信息有误']);
- $data->save(['employee_remark' => $param['remark']]);
- return json(['code' => 0, 'msg' => '确认完成']);
- }
- }
|