isAjax()) { // 部门 $org = OrgLogic::struc($request->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); View::assign('type', $type); View::assign('protected', $protected); $source = CustomerSource::where('root_id', '=', request()->employee->root_id)->select()->toArray(); View::assign('source', $source); //设计师获取 $w[] = ['path', 'like', request()->employee->root_id . '-%']; $w[] = ['org_type', '=', 2]; $orgs = Org::where($w)->column('id'); $designer = Employee::where([['org_id', 'in', $orgs], ['state', '=', '在职'], ['show', '=', 0]])->field('id,name,initials s')->order('s asc')->select()->toArray(); View::assign('designer', $designer); //小区名称获取 /*$communities = Community::where('root_id', '=', request()->employee->root_id)->where([['type', '=', 0]])->order('pinyin asc')->select()->toArray(); if (!empty($communities)) { $communities = hanziheadstr($communities); } View::assign('communityList', $communities);*/ $xinjushang = 0; View::assign('xinjushang', $xinjushang); return View::fetch(); } $param = $request->only(['page', 'limit', 'order', 'org', 'emp_id', 'state', 'protected', 'type', 'time', 'followed_time', 'protected_time', 'square_start', 'square_end', 'source', 'designer', 'customer_type', 'community', 'level', 'name'=> '', 'phone'=> '', 'no_visit_time'=> '']); $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $subOrg)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds(request()->employee->root_id); } if (!empty($param['protected'])) { $org_employee = Employee::where('org_id', 'in', $orgids)->column('id'); $condition[] = ['employee_id', 'in', $org_employee]; } else { $condition = [['org_id', 'in', $orgids]]; } $order = isset($param['order']) ? $param['order'] : 'id desc'; $cids = []; $condition[] = ['employee_id', 'not null', '']; $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $condition[] = ['died', '<>', 2]; if (!empty($param['time'])) { //录入时间 $addtimeArr = explode(' - ', $param['time']); $start = $addtimeArr[0]; $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400); $condition[] = ['addtime', '>=', $start]; $condition[] = ['addtime', '<', $end]; } if (!empty($param['followed_time'])) $condition[] = ['last_contact_date', 'between', explode(' - ', $param['followed_time'])]; if (!empty($param['protected_time']) && !empty($param['protected'])) $condition[] = ['protected_to', 'between', explode(' - ', $param['protected_time'])]; $whereRaw = []; if (!empty($param['square_start'])) { $whereRaw[] = "square +0 >=" . $param['square_start']; } if (!empty($param['square_end'])) { $whereRaw[] = "square +0 <=" . $param['square_end']; } if (!empty($param['source'])) { if ($param['source'] == -1) { $condition[] = ['source_id', '=', null]; } else { $condition[] = ['source_id', '=', $param['source']]; } } if (!empty($param['designer'])) $condition[] = ['designer_id', '=', $param['designer']]; if (!empty($param['customer_type'])) { switch ($param['customer_type']) { case 1: $condition[] = ['crm_res_id', '>', 0]; $condition[] = ['remark', '<>', '公海获取']; break; case 2: $condition[] = ['remark', '=', '活动报名建档']; break; case 3: $condition[] = ['agents_id', '>', 0]; break; case 4: $condition[] = ['crm_res_id', 'NULL', null]; $condition[] = ['remark', '<>', '活动报名建档']; $condition[] = ['agents_id', 'NULL', null]; break; case 5: $condition[] = ['remark', '=', '公海获取']; break; default: break; } } if (!empty($param['community'])) $condition[] = ['community_name', 'like', '%' . $param['community'] . '%']; if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']]; if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%']; if (!empty($param['phone'])){ $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->encrypt($param['phone']); $condition[] = ['phone', '=', $value]; } if (!empty($param['no_visit_time'])){ switch ((int)$param['no_visit_time']) { case 3: $no_date_s = strtotime(date('Y-m-d')); $no_date_e = strtotime(date('Y-m-d')) - 2*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 7: $no_date_s = strtotime(date('Y-m-d')) - 2*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 6*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 15: $no_date_s = strtotime(date('Y-m-d')) - 6*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 14*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 30: $no_date_s = strtotime(date('Y-m-d')) - 14*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 29*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 31: $no_date_s = strtotime(date('Y-m-d')) - 30*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; break; default: break; } } setCondition($param, ['emp_id', 'employee_id'], '=', $condition); $whereOr = []; //or条件二维数据 if (empty($param['state'])) { //2023-02-07 客户有效状态 为排除待确认和无效, 或者 待确认并且不是资源库过来的 $state1 = Customer::changeState('待确认', 'chaos'); $state2 = Customer::changeState('无效', 'chaos'); $whereOr[] = [['state', 'not in', array_merge($state1,$state2)], [['state', 'in',$state1],['crm_res_id','NULL',NULL]],[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',NULL]]]; } elseif ($param['state'] == '待确认') { $state1 = Customer::changeState('待确认', 'chaos'); //$condition[] = ['crm_res_id', 'NULL', NULL]; //$condition[] = ['state', 'in', $state1]; $whereOr[] = [[['state', 'in',$state1],['crm_res_id','NULL',NULL]],[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',NULL]]]; } elseif ($param['state'] == '有效'){ $condition[] = ['state','not in',Customer::changeState('待确认','chaos')]; } else { $state = CustomerVisitLog::changeState($param['state'], 'chaos'); $where = [ ['org_id', 'in', $orgids], // ['state', 'REGEXP', '(^|,)(' . implode('|', $state) . ')(,|$)'] ['state', 'in', $state] ]; $cids = CustomerVisitLog::where($where)->column('customer_id'); } $where_raw = implode(' and ', $whereRaw); //保护期筛选判断 if (empty($param['protected'])) { if ($where_raw) { $model = Customer::where($condition)->whereRaw($where_raw); } else { $model = Customer::where($condition); } // or 条件处理 foreach ($whereOr as $v) { $model = $model->where(function($query) use ($v){ $query->whereOr($v); }); } // id集合合并 if (!empty($cids)) { $m_cids = $model->column('id'); $ids_all = array_intersect($cids, $m_cids); $c_model = Customer::where('id', 'in', $ids_all); } else { $c_model = $model; } $data = $c_model->with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) { $query->field('id,customer_id,state')->group('state,customer_id'); }])->withCount(['visitLog' => function ($query) { $query->where(function ($query) { $query->whereOr([[CustomerVisitLog::changeState(['state', '=', '预约量房'])], [CustomerVisitLog::changeState(['state', '=', '预约到店'])], [CustomerVisitLog::changeState(['state', '=', '预约活动'])]]); }); }])->withCount(['activityFrequency' => function ($query) { $query->where([CustomerVisitLog::changeState(['state', '=', '确认到场'])]); }])->page($param['page'], $param['limit'])->order($order)->select()->visible(['id', 'employee_id', 'name', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'crm_res_id', 'remark', 'agents_id'])->toArray(); foreach ($data as $k => $v) { if ($v['remark'] == '公海获取') { $customer_type = '公海获取'; } elseif ($v['agents_id']) { $customer_type = '装修推荐官'; } elseif ($v['remark'] == '活动报名建档') { $customer_type = '活动报名'; } elseif ($v['crm_res_id']) { $customer_type = '资源库'; } else { $customer_type = '自建'; } $data[$k]['customer_type'] = $customer_type; if ($data[$k]['state'] == '已签单') { $data[$k]['state'] = '已转单'; } if ($data[$k]['state'] == '已交定') { $data[$k]['state'] = '已签单'; } $data[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6); $data[$k]['phone1'] = $v['phone1'] ? substr_replace($v['phone1'], '******', 3, 6) : ''; $data[$k]['phone2'] = $v['phone2'] ? substr_replace($v['phone2'], '******', 3, 6) : ''; //处理历史记录 $allstate = ''; foreach ($v['visitLog'] as $m => $p) { if ($p['state'] == '已签单') { $p['state'] = '已转单'; } if ($p['state'] == '已交定') { $p['state'] = '已签单'; } if ($p['state'] == '回访') { $p['state'] = '待确认'; } $allstate .= $p['state'] . ','; } $data[$k]['allstate'] = trim($allstate, ','); //增加跟进次数 $data[$k]['lognum'] = CustomerVisitLog::with('employee')->where('customer_id', '=', $v['id'])->count(); } $count = $c_model->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } else { if ($where_raw) { $data_obj = Customer::where($condition)->whereRaw($where_raw); } else { $data_obj = Customer::where($condition); } $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', request()->employee->root_id]])->findOrEmpty(); if (!$setting->isEmpty()) { $content = !empty($setting['content']) ? json_decode($setting['content'], true) : []; $empty = false; if (!empty($content)) { foreach ($content as $k => $v) { if (!isset($v['day']) || !isset($v['state'])) { $empty = true; } } } if ($empty) { $content = []; } $or = []; foreach ($content as $k => $v) { if ($v['state'] == 1 && $v['day'] > 0) { $or[] = [ ['state', 'in', Customer::changeState($k, 'chaos')], ['protected_to', '<', date('Y-m-d H:i:s')], ['protected_to', 'NOTNULL', null] ]; } } if (!empty($or)) { $whereOr[] = $or; } else { return json(['code' => 0, 'data' => [], 'count' => 0]); } foreach ($whereOr as $v) { $data_obj = $data_obj->where(function($query) use ($v){ $query->whereOr($v); }); } } else { //没设置就返回空 return json(['code' => 0, 'data' => [], 'count' => 0]); } // id集合合并 if (!empty($cids)) { $m_cids = $data_obj->column('id'); $ids_all = array_intersect($cids, $m_cids); $c_model = Customer::where('id', 'in', $ids_all); } else { $c_model = $data_obj; } $data_obj = $c_model->with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) { $query->field('id,customer_id,state')->group('state,customer_id'); }])->withCount(['visitLog' => function ($query) { $query->where(function ($query) { $query->whereOr([[CustomerVisitLog::changeState(['state', '=', '预约量房'])], [CustomerVisitLog::changeState(['state', '=', '预约到店'])], [CustomerVisitLog::changeState(['state', '=', '预约活动'])]]); }); }])->withCount(['activityFrequency' => function ($query) { $query->where([CustomerVisitLog::changeState(['state', '=', '确认到场'])]); }]); $data = $data_obj->page($param['page'], $param['limit'])->order($order)->select()->visible(['id', 'employee_id', 'name', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'crm_res_id', 'remark', 'agents_id'])->toArray(); if (!$setting->isEmpty()) { $setting_content = !empty($setting['content']) ? json_decode($setting['content'], true) : []; $empty = false; if (!empty($setting_content)) { foreach ($setting_content as $k => $v) { if (!isset($v['day']) || !isset($v['state'])) { $empty = true; } } } if ($empty) { $setting_content = []; } } else { $setting_content = []; } foreach ($data as &$item) { $item['un_protected'] = false; if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) { $state_n = Customer::changeState($item['state'], 'n'); if (!empty($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) { $item['un_protected'] = true; } } $item['square'] = floatval($item['square']); $item['mobile'] = $item['phone']; $item['mobile1'] = $item['phone1']; $item['mobile2'] = $item['phone2']; $item['phone'] = substr_replace($item['phone'], '******', 3, 6); $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : ''; $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : ''; //处理历史记录 $allstate = ''; foreach ($item['visitLog'] as $m => $p) { if ($p['state'] == '已签单') { $p['state'] = '已转单'; } if ($p['state'] == '已交定') { $p['state'] = '已签单'; } if ($p['state'] == '回访') { $p['state'] = '待确认'; } $allstate .= $p['state'] . ','; } $data[$k]['allstate'] = trim($allstate, ','); //增加跟进次数 $item['lognum'] = CustomerVisitLog::with('employee')->where('customer_id', '=', $item['id'])->count(); //客户种类 $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '经纪人' : '自建')); if ($item['state'] == '已交定') { $item['state'] = '已签单'; } if ($item['state'] == '已签单') { $item['state'] = '已转单'; } } $count = $c_model->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } } /** * 客户详情 */ public function view() { $id = input('id', '', 'intval'); // if (!request()->isAjax()){ // View::assign('id', $id); // return View::fetch(); // } $where[] = ['id', '=', $id]; $data = Customer::with(['source', 'designer' => function ($query) { $query->field('id,name,headimgurl,org_id,uid'); }, 'employee'])->where($where)->find(); if (empty($data)) return json(['code' => 1, 'msg' => '数据不存在']); $data->phone = substr_replace($data->phone, '******', 3, 6); $orgids = orgSubIds(request()->employee->root_id); if (!in_array($data['org_id'], $orgids)) { return json(['code' => 1, 'msg' => '数据不存在']); } $count = CustomerVisitLog::where('customer_id', $id)->count(); $data['visit_count'] = $count; $data['deposit'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '交定'])])->order('addtime desc')->value('addtime'); $data['sign'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '签单'])])->order('addtime desc')->value('addtime'); // 量房、活动、到店次数统计 $data['liangfangNum'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '确认量房'])])->count(); $data['activityNum'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '确认到场'])])->count(); $data['daodianNum'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '确认到店'])])->count(); $data['deposit_count'] = CustomerVisitLog::where([['customer_id', '=', $id], ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]])->count(); $data['square'] = floatval($data['square']); // 查询客户是否有预约 $data['subscirbe'] = CustomersSubscribe::where([['customer_id', '=', $id], ['state', '=', 0]])->field('id,state,type,subscribe_date')->select()->toArray(); //查询我最近一次的签单和交定金额 $state = CustomerVisitLog::changeState('已交定', 'chaos'); $data['deposit_money1'] = CustomerVisitLog::where([['employee_id', '=', request()->employee->id], ['customer_id', '=', $id], ['state', 'in', $state]])->order('id desc')->value('money') ?: ''; $data['money'] = $data->getData('signed_money'); // return json(['code' => 0, 'data' => $data]); //处理新设置的客户扩展字段 $newext = $this->get_portrait_field($data); //array_splice($newext,1,'basic_info'); $topdata = []; $arr = ['level', 'source_id', 'phone']; $arr_check = ['level' => 0, 'source_id' => 0, 'phone' => 0]; $topext = ['intention', 'point', 'age_range', 'first', 'follow', 'wechat']; foreach ($newext as $key => $val) { foreach ($val['child'] as $k => $v) { if (in_array($v['keyname'], $topext)) { $topdata[] = $v; } if (in_array($v['keyname'], $arr)) $arr_check[$v['keyname']] = 1; } } //unset($newext['basic_info']); $data['topdata'] = $topdata; $data['newext'] = $newext; View::assign('data', $data); View::assign('arr_check', $arr_check); View::assign('root_id', request()->employee->root_id); //顶部固定字段权限 return View::fetch(); } //新获取客户扩展字段 public function get_portrait_field($cusdata) { $root_id = request()->employee->root_id; $w[] = ['root_id', '=', $root_id]; $list = CustomerPortraitField::where([['root_id', '=', $root_id], ['pid', '=', 0], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray(); $city = Company::where('root_id', $root_id)->value('city'); foreach ($list as $key => $val) { $child = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['pid', '<>', 0], ['pid', '=', $val['id']], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray(); foreach ($child as $k => $v) { if ($v['keyname'] == 'current_region') { $child[$k]['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $v['id']]; } if ($v['keyname'] == 'source_id') { $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray(); $child[$k]['select'] = $soudata; } if ($v['keyname'] == 'deco_style') { $decostyles = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray(); $child[$k]['select'] = $decostyles; } $child[$k]['value'] = ''; $child[$k]['valname'] = ''; } $list[$key]['child'] = $child; } $new = $list; $cuslog = new CustomerLogic; if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') { //$extdata=get_object_vars($cusdata['ext']); $extdata = json_decode($cusdata['ext'], true); if (isset($extdata['ext1'])) { $new = $cuslog->old_data_save($cusdata, $list); } else { $new = $cuslog->new_data_save($cusdata, $extdata, $list); } } return $new; //return json(['code' => 0, 'data' => $new]); } /** * 客户意向折线图 */ public function line_chart() { $customerId = request()->param('customer_id'); $w[] = ['id', '=', $customerId]; $w[] = ['employee_id', '=', request()->employee->id]; $check = Customer::where($w)->findOrEmpty(); if ($check->isEmpty()) return json(['code' => 0, 'data' => [], 'msg' => '操作成功']); $w1[] = ['customer_id', '=', $customerId]; $w1[] = ['starts', '>', 0]; $ids = CustomerVisitLog::where($w1)->order('id desc')->limit(30)->column('id'); $w1[] = ['id', 'in', $ids]; $row = CustomerVisitLog::where($w1)->order('id asc')->column('starts'); return json(['code' => 0, 'data' => $row, 'msg' => '操作成功']); } /** * 客户线索 */ public function clue() { $request = request(); if (!$request->isAjax()) { // 部门 $org = OrgLogic::struc($request->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); return View::fetch(); } $param = $request->only(['page', 'limit', 'order', 'org', 'emp_id', 'state', 'phone', 'time']); $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $orgSub)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds($root_id); } $condition = [['org_id', 'in', $orgids]]; $order = isset($param['order']) ? $param['order'] : 'id desc'; if (!empty($param['phone'])) { if ($param['phone'] == 1) { $condition[] = ['phone', '<>', '']; } elseif ($param['phone'] == 2) { $condition[] = ['phone', '=', null]; } } if (!empty($param['time'])) { list($startDate, $endDate) = explode(' - ', $param['time']); $endTime = strtotime($endDate) + 86400; $condition[] = ['addtime', 'between', [$startDate . ' 00:00:00', date('Y-m-d H:i:s', $endTime)]]; } // $orgids = orgSubIds(request()->employee->org_id); // if ($request->employee->is_manager) { // if (!empty($param['org']) && in_array($param['org'], $orgids)) { // $orgids = orgSubIds($param['org']); // $condition[] = ['org_id', 'in', $orgids]; // } else { // $condition[] = ['org_id', 'in', $orgids]; // //$condition[] = ['org_id', 'in', $request->org]; // } // if (isset($param['emp_id'])) { // $condition[] = ['employee_id', '=', $param['emp_id']]; // } setCondition($param, ['emp_id', 'employee_id'], '=', $condition); // } else { // $condition[] = ['employee_id', '=', $request->employee->id]; // } setCondition($param, 'state', '=', $condition); $data = ClueLogic::list($condition, $param['page'], $param['limit'], $order); $count = ClueLogic::count($condition); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function test(){ // $aw[] = ['id', 'in', array_keys($cidList)]; // array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos')) $type = input('type', '', 'intval'); $protected = input('protected', '', 'intval'); $request = request(); if (!$request->isAjax()) { $type = input('type', '', 'intval'); $protected = input('protected', '', 'intval'); $request = request(); if (!$request->isAjax()) { // 部门 $org = OrgLogic::struc($request->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); View::assign('type', $type); View::assign('protected', $protected); $source = CustomerSource::where('root_id', '=', request()->employee->root_id)->select()->toArray(); View::assign('source', $source); //设计师获取 $w[] = ['path', 'like', request()->employee->root_id . '-%']; $w[] = ['org_type', '=', 2]; $orgs = Org::where($w)->column('id'); $designer = Employee::where([['org_id', 'in', $orgs], ['state', '=', '在职'], ['show', '=', 0]])->field('id,name,initials s')->order('s asc')->select()->toArray(); View::assign('designer', $designer); //小区名称获取 /*$communities = Community::where('root_id', '=', request()->employee->root_id)->where([['type', '=', 0]])->order('pinyin asc')->select()->toArray(); if (!empty($communities)) { $communities = hanziheadstr($communities); } View::assign('communityList', $communities);*/ $xinjushang = 0; View::assign('xinjushang', $xinjushang); return View::fetch(); } } $param = $request->only(['page', 'limit', 'order', 'org','died_time', 'emp_id', 'state','died', 'protected', 'type', 'time', 'followed_time', 'protected_time', 'square_start', 'square_end', 'source', 'designer', 'customer_type', 'community', 'level', 'name'=> '', 'phone'=> '', 'no_visit_time'=> '']); $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $subOrg)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds(request()->employee->root_id); } if (!empty($param['protected'])) { $org_employee = Employee::where('org_id', 'in', $orgids)->column('id'); $condition[] = ['employee_id', 'in', $org_employee]; } else { $condition = [['org_id', 'in', $orgids]]; } $order = isset($param['order']) ? $param['order'] : 'id desc'; $cids = []; // $condition[] = ['employee_id', 'not null', '']; if (!empty($param['time'])) { //录入时间 $addtimeArr = explode(' - ', $param['time']); $start = $addtimeArr[0]; $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400); $condition[] = ['addtime', '>=', $start]; $condition[] = ['addtime', '<', $end]; } if (!empty($param['followed_time'])) $condition[] = ['last_contact_date', 'between', explode(' - ', $param['followed_time'])]; if (!empty($param['died_time'])) { list($startDate, $endDate) = explode(' - ', $param['died_time']); $endTime = date('Y-m-d', strtotime($endDate) + 86400); $condition[] = ['died_date', '>=', $startDate . ' 00:00:00']; $condition[] = ['died_date', '<', $endTime . ' 00:00:00']; } if (!empty($param['protected_time']) && !empty($param['protected'])) $condition[] = ['protected_to', 'between', explode(' - ', $param['protected_time'])]; $whereRaw = []; if (!empty($param['square_start'])) { $whereRaw[] = "square +0 >=" . $param['square_start']; } if (!empty($param['square_end'])) { $whereRaw[] = "square +0 <=" . $param['square_end']; } if (!empty($param['source'])) { if ($param['source'] == -1) { $condition[] = ['source_id', '=', null]; } else { $condition[] = ['source_id', '=', $param['source']]; } } if (!empty($param['designer'])) $condition[] = ['designer_id', '=', $param['designer']]; if (!empty($param['customer_type'])) { switch ($param['customer_type']) { case 1: $condition[] = ['crm_res_id', '>', 0]; $condition[] = ['remark', '<>', '公海获取']; break; case 2: $condition[] = ['remark', '=', '活动报名建档']; break; case 3: $condition[] = ['agents_id', '>', 0]; break; case 4: $condition[] = ['crm_res_id', 'NULL', null]; $condition[] = ['remark', '<>', '活动报名建档']; $condition[] = ['agents_id', 'NULL', null]; break; case 5: $condition[] = ['remark', '=', '公海获取']; break; default: break; } } if (!empty($param['community'])) $condition[] = ['community_name', 'like', '%' . $param['community'] . '%']; if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']]; if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%']; if (!empty($param['phone'])){ $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->encrypt($param['phone']); $condition[] = ['phone', '=', $value]; } if (!empty($param['no_visit_time'])){ switch ((int)$param['no_visit_time']) { case 3: $no_date_s = strtotime(date('Y-m-d')); $no_date_e = strtotime(date('Y-m-d')) - 2*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 7: $no_date_s = strtotime(date('Y-m-d')) - 2*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 6*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 15: $no_date_s = strtotime(date('Y-m-d')) - 6*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 14*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 30: $no_date_s = strtotime(date('Y-m-d')) - 14*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 29*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 31: $no_date_s = strtotime(date('Y-m-d')) - 30*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; break; default: break; } } setCondition($param, ['emp_id', 'employee_id'], '=', $condition); $whereOr = []; //or条件二维数据 if (empty($param['state'])) { if (empty($param['died_time']) && empty($param['followed_time']) ){ $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')]; $whereOr[] = [['died','=',2]]; } if (!empty($param['followed_time']) ){ $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')]; } if (!empty($param['died_time'])){ $whereOr[] = [['died','=',2]]; } //2023-02-07 客户有效状态 为排除待确认和无效, 或者 待确认并且不是资源库过来的 // $state1 = Customer::changeState('待确认', 'chaos'); // $state1 = []; // $state2 = Customer::changeState('无效', 'chaos'); // $whereOr[] = [['state', 'not in', array_merge($state1,$state2)], [['state', 'in',$state1],['crm_res_id','NULL',NULL]],[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',NULL]]]; } elseif ($param['state'] == '待确认') { $state1 = Customer::changeState('待确认', 'chaos'); //$condition[] = ['crm_res_id', 'NULL', NULL]; //$condition[] = ['state', 'in', $state1]; $whereOr[] = [[['state', 'in', $state1],['crm_res_id','NULL',NULL]],[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',NULL]]]; } elseif ($param['state'] == '有效'){ $condition[] = ['state','not in',Customer::changeState('待确认','chaos')]; }elseif ($param['state'] == '无效'){ $condition[] = ['state','in',Customer::changeState('无效','chaos')]; // print_r($condition); // die; } elseif ($param['state'] == '死单'){ $condition[] = ['died', '=', 2]; // print_r($condition); // die; } $where_raw = implode(' and ', $whereRaw); // print_r($condition); // print_r($whereOr); // print_r($where_raw); // die; if (!empty($param['died_time'])) { $condition[] = ['died', '=', 2]; $whereOr = []; } if (!empty($param['followed_time'])) { $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')]; $whereOr = []; } //保护期筛选判断 if ($where_raw) { $model = Customer::where($condition)->whereRaw($where_raw); } else { $model = Customer::where($condition)->whereOr($whereOr); } $c_model = $model; $data = $c_model->with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) { $query->field('id,customer_id,state')->group('state,customer_id'); }])->withCount(['visitLog' => function ($query) { $query->where(function ($query) { $query->whereOr([[CustomerVisitLog::changeState(['state', '=', '预约量房'])], [CustomerVisitLog::changeState(['state', '=', '预约到店'])], [CustomerVisitLog::changeState(['state', '=', '预约活动'])]]); }); }])->withCount(['activityFrequency' => function ($query) { $query->where([CustomerVisitLog::changeState(['state', '=', '确认到场'])]); }])->page($param['page'], $param['limit']) ->order($order) ->select() ->visible(['id', 'employee_id', 'name', 'died_date','community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'crm_res_id', 'remark', 'agents_id']) ->toArray(); foreach ($data as $k => $v) { if ($v['remark'] == '公海获取') { $customer_type = '公海获取'; } elseif ($v['agents_id']) { $customer_type = '装修推荐官'; } elseif ($v['remark'] == '活动报名建档') { $customer_type = '活动报名'; } elseif ($v['crm_res_id']) { $customer_type = '资源库'; } else { $customer_type = '自建'; } $data[$k]['customer_type'] = $customer_type; if ($data[$k]['state'] == '已签单') { $data[$k]['state'] = '已转单'; } if ($data[$k]['state'] == '已交定') { $data[$k]['state'] = '已签单'; } $data[$k]['phone'] = $v['phone']; $data[$k]['phone1'] = $v['phone1'] ; $data[$k]['phone2'] = $v['phone2']; //处理历史记录 $allstate = ''; foreach ($v['visitLog'] as $m => $p) { if ($p['state'] == '已签单') { $p['state'] = '已转单'; } if ($p['state'] == '已交定') { $p['state'] = '已签单'; } if ($p['state'] == '回访') { $p['state'] = '待确认'; } $allstate .= $p['state'] . ','; } $data[$k]['allstate'] = trim($allstate, ','); //增加跟进次数 $data[$k]['lognum'] = CustomerVisitLog::with('employee')->where('customer_id', '=', $v['id'])->count(); } $count = $c_model->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function export(){ $type = input('type', '', 'intval'); $protected = input('protected', '', 'intval'); $request = request(); $param = $request->only(['page', 'limit', 'order', 'org', 'died_time', 'emp_id', 'state','died', 'protected', 'type', 'time', 'followed_time', 'protected_time', 'square_start', 'square_end', 'source', 'designer', 'customer_type', 'community', 'level', 'name'=> '', 'phone'=> '', 'no_visit_time'=> '']); $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $subOrg)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds(request()->employee->root_id); } if (!empty($param['protected'])) { $org_employee = Employee::where('org_id', 'in', $orgids)->column('id'); $condition[] = ['employee_id', 'in', $org_employee]; } else { $condition = [['org_id', 'in', $orgids]]; } $order = isset($param['order']) ? $param['order'] : 'id desc'; $cids = []; // $condition[] = ['employee_id', 'not null', '']; if (!empty($param['time'])) { //录入时间 $addtimeArr = explode(' - ', $param['time']); $start = $addtimeArr[0]; $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400); $condition[] = ['addtime', '>=', $start]; $condition[] = ['addtime', '<', $end]; } if (!empty($param['followed_time'])) $condition[] = ['last_contact_date', 'between', explode(' - ', $param['followed_time'])]; if (!empty($param['died_time'])) { list($startDate, $endDate) = explode(' - ', $param['died_time']); $endTime = date('Y-m-d', strtotime($endDate) + 86400); $condition[] = ['died_date', '>=', $startDate . ' 00:00:00']; $condition[] = ['died_date', '<', $endTime . ' 00:00:00']; } if (!empty($param['protected_time']) && !empty($param['protected'])) $condition[] = ['protected_to', 'between', explode(' - ', $param['protected_time'])]; $whereRaw = []; if (!empty($param['square_start'])) { $whereRaw[] = "square +0 >=" . $param['square_start']; } if (!empty($param['square_end'])) { $whereRaw[] = "square +0 <=" . $param['square_end']; } if (!empty($param['source'])) { if ($param['source'] == -1) { $condition[] = ['source_id', '=', null]; } else { $condition[] = ['source_id', '=', $param['source']]; } } if (!empty($param['designer'])) $condition[] = ['designer_id', '=', $param['designer']]; if (!empty($param['customer_type'])) { switch ($param['customer_type']) { case 1: $condition[] = ['crm_res_id', '>', 0]; $condition[] = ['remark', '<>', '公海获取']; break; case 2: $condition[] = ['remark', '=', '活动报名建档']; break; case 3: $condition[] = ['agents_id', '>', 0]; break; case 4: $condition[] = ['crm_res_id', 'NULL', null]; $condition[] = ['remark', '<>', '活动报名建档']; $condition[] = ['agents_id', 'NULL', null]; break; case 5: $condition[] = ['remark', '=', '公海获取']; break; default: break; } } if (!empty($param['community'])) $condition[] = ['community_name', 'like', '%' . $param['community'] . '%']; if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']]; if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%']; if (!empty($param['phone'])){ $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $value = $aec->encrypt($param['phone']); $condition[] = ['phone', '=', $value]; } if (!empty($param['no_visit_time'])){ switch ((int)$param['no_visit_time']) { case 3: $no_date_s = strtotime(date('Y-m-d')); $no_date_e = strtotime(date('Y-m-d')) - 2*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 7: $no_date_s = strtotime(date('Y-m-d')) - 2*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 6*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 15: $no_date_s = strtotime(date('Y-m-d')) - 6*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 14*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 30: $no_date_s = strtotime(date('Y-m-d')) - 14*24*3600; $no_date_e = strtotime(date('Y-m-d')) - 29*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)]; break; case 31: $no_date_s = strtotime(date('Y-m-d')) - 30*24*3600; $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)]; break; default: break; } } setCondition($param, ['emp_id', 'employee_id'], '=', $condition); $whereOr = []; //or条件二维数据 if (empty($param['state'])) { if (empty($param['died_time']) && empty($param['followed_time']) ){ $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')]; $whereOr[] = [['died','=',2]]; } if (!empty($param['followed_time']) ){ $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')]; } if (!empty($param['died_time'])){ $whereOr[] = [['died','=',2]]; } //2023-02-07 客户有效状态 为排除待确认和无效, 或者 待确认并且不是资源库过来的 // $state1 = Customer::changeState('待确认', 'chaos'); // $state1 = []; // $state2 = Customer::changeState('无效', 'chaos'); // $whereOr[] = [['state', 'not in', array_merge($state1,$state2)], [['state', 'in',$state1],['crm_res_id','NULL',NULL]],[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',NULL]]]; } elseif ($param['state'] == '待确认') { $state1 = Customer::changeState('待确认', 'chaos'); //$condition[] = ['crm_res_id', 'NULL', NULL]; //$condition[] = ['state', 'in', $state1]; $whereOr[] = [[['state', 'in', $state1],['crm_res_id','NULL',NULL]],[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',NULL]]]; } elseif ($param['state'] == '有效'){ $condition[] = ['state','not in',Customer::changeState('待确认','chaos')]; }elseif ($param['state'] == '无效'){ $condition[] = ['state','in',Customer::changeState('无效','chaos')]; // print_r($condition); // die; } elseif ($param['state'] == '死单'){ $condition[] = ['died', '=', 2]; // print_r($condition); // die; } $where_raw = implode(' and ', $whereRaw); // print_r($condition); // die; //保护期筛选判断 if (!empty($param['died_time'])) { $condition[] = ['died', '=', 2]; $whereOr = []; } if (!empty($param['followed_time'])) { $condition[] = ['state', 'in', Customer::changeState('无效', 'chaos')]; $whereOr = []; } if ($where_raw) { $model = Customer::where($condition)->whereRaw($where_raw); } else { $model = Customer::where($condition)->whereOr($whereOr); } $c_model = $model; $data = $c_model->with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) { $query->field('id,customer_id,state')->group('state,customer_id'); }])->withCount(['visitLog' => function ($query) { $query->where(function ($query) { $query->whereOr([[CustomerVisitLog::changeState(['state', '=', '预约量房'])], [CustomerVisitLog::changeState(['state', '=', '预约到店'])], [CustomerVisitLog::changeState(['state', '=', '预约活动'])]]); }); }])->withCount(['activityFrequency' => function ($query) { $query->where([CustomerVisitLog::changeState(['state', '=', '确认到场'])]); }]) ->order($order) ->select() ->visible(['id', 'employee_id', 'name', 'community_name','died_date', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'crm_res_id', 'remark', 'agents_id']) ->toArray(); foreach ($data as $k => $v) { if ($v['remark'] == '公海获取') { $customer_type = '公海获取'; } elseif ($v['agents_id']) { $customer_type = '装修推荐官'; } elseif ($v['remark'] == '活动报名建档') { $customer_type = '活动报名'; } elseif ($v['crm_res_id']) { $customer_type = '资源库'; } else { $customer_type = '自建'; } $data[$k]['customer_type'] = $customer_type; $data[$k]['phone'] = $v['phone']; $data[$k]['phone1'] = $v['phone1'] ; $data[$k]['phone2'] = $v['phone2']; //处理历史记录 $allstate = ''; foreach ($v['visitLog'] as $m => $p) { if ($p['state'] == '已签单') { $p['state'] = '已转单'; } if ($p['state'] == '已交定') { $p['state'] = '已签单'; } if ($p['state'] == '回访') { $p['state'] = '待确认'; } $allstate .= $p['state'] . ','; } $data[$k]['allstate'] = trim($allstate, ','); //增加跟进次数 $data[$k]['lognum'] = CustomerVisitLog::with('employee')->where('customer_id', '=', $v['id'])->count(); } // return json(['code' => 0, 'data' => $data]); $result = []; foreach($data as $v) { $vv = []; $vv['id'] = $v['id']; $vv['name'] = $v['name']; $vv['phone'] = $v['phone']; $vv['employee_name'] = $v['employee']['name'] ?? '无'; $vv['designer'] = $v['designer']['name'] ?? '无'; $vv['org_name'] = $v['org_name'] ?? ''; $vv['source'] = $v['source']['source'] ?? ''; $vv['customer_type'] = $v['customer_type']; $vv['community_name'] = $v['community_name']; $vv['level'] = $v['level']; $vv['state'] = $v['state']; $vv['square'] = $v['square']; $vv['died_date'] = $v['died_date']; $vv['last_contact_date'] = $v['last_contact_date']; $vv['lognum'] = $v['lognum']; $vv['revisit_time'] = $v['revisit_time']; $vv['protected_to'] = $v['protected_to']; $vv['visit_log_count'] = $v['visit_log_count']; $vv['activity_frequency_count'] = $v['activity_frequency_count']; $vv['addtime'] = $v['addtime']; $result[] = $vv; } $header = [ "A1" => "编号", "B1" => "业主姓名", "C1" => "电话", "D1" => "所属员工", "E1" => "设计师", "F1" => '所属部门', "G1" => '来源渠道', "H1" => '客户种类', "I1" => '小区', "J1" => '重要', "K1" => '状态', "L1" => '面积', "M1" => '死单时间', "N1" => '无效时间', "O1" => '跟进次数', "P1" => '下次回访时间', "Q1" => '保护至', "R1" => '预约次数', "S1" => '参加活动次数', "T1" => '添加时间', ]; if (!empty($param['died_time'])){ $param['state'] = '死单'; } if (!empty($param['followed_time'])){ $param['state'] = '无效'; } $fileName = empty($param['state']) ? '无效和死单客户_' : $param['state'].'客户_'; $fileName .= date('Y-m-d H:i'); // return json(['code' => 0, 'data' => $result]); $this->getExport($header, true, $result, $fileName); } // 导出 public function getExport($header = [], $type = false, $data = [], $fileName) { // 实例化类 $preadsheet = new Spreadsheet(); // 创建sheet $sheet = $preadsheet->getActiveSheet(); // 循环设置表头数据 foreach ($header as $k => $v) { $sheet->setCellValue($k, $v); } // 生成数据 $sheet->fromArray($data, null, "A2"); // 样式设置 $sheet->getDefaultColumnDimension()->setWidth(12); // 设置下载与后缀 if ($type) { header("Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); $type = "Xlsx"; $suffix = "xlsx"; } else { header("Content-Type:application/vnd.ms-excel"); $type = "Xls"; $suffix = "xls"; } ob_end_clean(); //清楚缓存区 // 激活浏览器窗口 header("Content-Disposition:attachment;filename=$fileName.$suffix"); //缓存控制 header("Cache-Control:max-age=0"); // 调用方法执行下载 $writer = IOFactory::createWriter($preadsheet, $type); // 数据流 $writer->save("php://output"); } /** * 客户跟进 */ public function tracking() { $request = request(); $id = $request->param('id'); $orgids = orgSubIds(request()->employee->root_id); $customer = Customer::where([['id', '=', $id], ['org_id', 'in', $orgids]])->field('revisit_time')->find(); if (!$request->isAjax()) { View::assign('revisit_time', str_replace('00:00:00', '', $customer->revisit_time)); View::assign('id', $id); return View::fetch(); } $page = $request->param('page', 1); $limit = $request->param('limit', 10); $data = CustomerVisitLog::with(['employee'])->where('customer_id', $id)->page($page, $limit)->order('addtime desc')->select(); $count = CustomerVisitLog::where('customer_id', $id)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 客户展示足迹 */ public function foot() { $request = request(); $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); if (!$request->isAjax()) { $id = $request->param('id'); $data = Customer::find($id); if (in_array($data['org_id'], $subOrg)) { View::assign('uid', $data['uid']); } else { View::assign('uid', ''); } return View::fetch(); } $uid = $request->param('uid'); $param = $request->only(['customer', 'page', 'limit']); $data = Footprints::where([['uid', '=', $uid], ['org_id', 'in', $subOrg]])->order('addtime desc')->page($param['page'], $param['limit'])->select(); $count = Footprints::where([['uid', '=', $uid], ['org_id', 'in', $subOrg]])->count(); foreach ($data as $k => &$v) { $v['visit_due_time'] = $v['visit_due_time'] ?: 0; } return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 线索展示足迹 */ public function cluefoot() { $request = request(); $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); if (!$request->isAjax()) { $id = $request->param('id'); $data = CustomerClue::find($id); if (in_array($data['org_id'], $subOrg)) { View::assign('uid', $data['uid']); } else { View::assign('uid', ''); } View::assign('uid', $data['uid']); return View::fetch(); } $uid = $request->param('uid'); $param = $request->only(['customer', 'page', 'limit']); $data = Footprints::where([['uid', '=', $uid], ['org_id', 'in', $subOrg]])->page($param['page'], $param['limit'])->select(); $count = Footprints::where([['uid', '=', $uid], ['org_id', 'in', $subOrg]])->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 线索无效 */ public function invalidClue($id) { $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); $result = CustomerClue::where([['id', '=', $id], ['org_id', 'in', $subOrg]])->update(['state' => 2]); if ($result !== false) { return json(['code' => 0, 'msg' => '操作成功']); } else { return json(['code' => 1, 'msg' => '操作失败']); } } /** * 导入前日志存储 */ public function importLog() { $request = request(); $param = $request->only(['file_name', 'source', 'file']); $logData = [ 'name' => $param['file_name'], 'employee_id' => $request->employee->id, 'org_id' => $request->employee->root_id, 'count' => 0, 'avaliable_count' => 0, 'bad_phone_num' => 0, 'left_num' => 0, 'price' => '', 'source' => $param['source'], 'repeat_phone_number' => 0, 'path'=> $param['file'], 'root_id'=> $request->employee->root_id ]; $data = CrmImportLog::create($logData); if ($data == false) { return json(['code' => 1, 'msg' => '添加失败']); } $data->status = 1; $data->save(); $jobHandlerClassName = 'app\jobs\CrmImport'; $jobQueueName = 'crm_import'; $orderData = ['id'=> $data->id]; //这个是需要传到消费者的数据 Queue::later(0, $jobHandlerClassName, $orderData, $jobQueueName); return json(['code' => 0, 'msg' => '正在导入中,请刷新列表查看']); } /** * 客户excel添加 */ public function add() { $request = request(); $orgIds = orgSubIds($request->employee->root_id); $logId = $request->param('log_id'); $sourceId = $request->param('source_id'); $log = CrmImportLog::where('id', $logId)->find(); if (empty($log) || !in_array($log->org_id, $orgIds)) return json(['code' => 1, 'msg' => '请求错误,记录不存在']); $orgIds = orgSubIds($request->employee->root_id); $datas = $request->param('customers'); if (count($datas) == 0) return json(['code' => 1, 'msg' => '请求错误,未传输数据']); //客户来源扩展字段 $ext_where = [ ['root_id', '=', $request->employee->root_id], ['keyname', '=', 'source_id'] ]; $ext = []; $source_field_id = CustomerPortraitField::where($ext_where)->value('id'); if($source_field_id) $ext[] = ['id' => (string)$source_field_id, 'keyname' => 'source_id', 'value' => $sourceId]; $unit_number_id = CustomerPortraitField::where([['root_id','=',$request->employee->root_id],['keyname','=','unit_number']])->find(); $house_location = CustomerPortraitField::where([['root_id','=',$request->employee->root_id],['keyname','=','house_location']])->find(); //error 手机号错误数量 repeat 手机号重复数量 $rs = ['ok' => 0, 'error' => 0, 'repeat' => 0]; $phones = []; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); foreach ($datas as $k => &$data) { $data['phone1'] = $data['phone2'] = ''; ////2023-02-15 修改手机号可以输入多个 $s = true; $v_phone = []; $ls_phone = explode('、',$data['phone']); if(!empty($ls_phone) && is_array($ls_phone)){ $p = 0; foreach ($ls_phone as $k2 => $v2) { if ($this->checkphone($v2)) { $field = $p ? 'phone'.$p : 'phone'; $data[$field] = $aec->encrypt($v2); $p+=1; $v_phone[] = $data[$field]; } if($p>2) break;//只取三个有效手机号 } if($p>0) $s = false; } if($s){ unset($datas[$k]); $rs['error']++; continue; } // 判断重复 $intersect = array_intersect($v_phone, $phones); if (!empty($intersect)) { unset($datas[$k]); $rs['repeat']++; continue; } else { $phones = array_merge($phones, $v_phone); } // 数据处理 $data['house_type'] = mb_strlen($data['house_type']) > 20 ? mb_substr($data['house_type'],0,20) : trim($data['house_type']); $data['age_range'] = mb_strlen($data['age_range']) > 5 ? mb_substr($data['age_range'],0,5) : trim($data['age_range']); $data['name'] = mb_strlen($data['name']) > 20 ? mb_substr($data['name'],0,20) : trim($data['name']); $data['crm_res_id'] = $log->id; $data['state'] = 0; $data['sex'] = ($data['sex'] == '男') ? 1 : 2; $data['org_id'] = $request->employee->root_id; $data['is_resource'] = 1; $data['bad_phone'] = 0; $data['source_id'] = $sourceId; $ext1 = $ext; if ($data['unit_number'] && !$unit_number_id->isEmpty()) $ext1[] = ['id' => (string)$unit_number_id->id, 'keyname' => 'unit_number', 'value' => $data['unit_number']]; if ($data['house_location'] && !$house_location->isEmpty()) $ext1[] = ['id' => (string)$house_location->id, 'keyname' => 'house_location', 'value' => $data['house_location']]; $data['ext'] = $ext1 ? json_encode($ext1) : NULL; unset($data['unit_number']); unset($data['house_location']); $data['level'] = in_array($data['level'], ['A','B','C','D']) ? trim($data['level']) : null; $rs['ok']++; } Db::startTrans(); try { Customer::insertAll($datas); $log->repeat_phone_number += $rs['repeat']; $log->bad_phone_num -= $rs['ok']; $log->avaliable_count += $rs['ok']; $log->left_num += $rs['ok']; $log->save(); Db::commit(); } catch (\Exception $e) { trace($e->getMessage(), 'error'); // 回滚事务 Db::rollback(); return json(['code' => 1, 'msg' => $e->getMessage()]); } return json(['code' => 0, 'msg' => '保存成功', 'data' => $rs]); } public function editing($id) { /* $condition = ['root_id' => request()->employee->root_id]; //小区名称获取 $communities = Community::where($condition)->where([['type', '=', 1]])->select()->toArray(); View::assign('communities', $communities); //风格获取 $decostyles = Decostyle::where($condition)->where([['type', '=', 1]])->select()->toArray(); View::assign('decostyles', $decostyles); //关注点产品获取 $products = Product::where($condition)->select()->toArray(); View::assign('products', $products); //来源获取 $sources = CustomerSource::where($condition)->select()->toArray(); View::assign('sources', $sources); $customer = Customer::where('id', $id)->with(['source'])->find()->toArray(); $customer['community_id'] = Community::where(['name' => $customer['community_name'], 'root_id' => request()->employee->root_id])->where([['type', '=', 1]])->column('id'); $customer['style_id'] = Decostyle::where(['name' => $customer['deco_style'], 'root_id' => request()->employee->root_id])->where([['type', '=', 1]])->column('id'); $customer['product_id'] = Product::where(['product' => $customer['product'], 'root_id' => request()->employee->root_id])->column('id'); $customer['source'] = CustomerSource::where(['id' => $customer['source_id'], 'root_id' => request()->employee->root_id])->column('source'); $customer['revisit_time'] = $customer['revisit_time'] ? date('Y-m-d', strtotime($customer['revisit_time'])) : ''; $customer['before_pool'] = NULL; View::assign('customer', json_encode($customer)); View::assign('customer_id', $id); */ return View::fetch(); } public function prefileaddingcheck() { $request = request(); $clueid = $request->param('clueid'); $empid = $request->param('empid'); $orgids = orgSubIds(request()->employee->root_id); $where[] = ['org_id', 'in', $orgids]; $clue = CustomerClue::find($clueid); $where[] = ['uid', '=', $clue->uid]; $where[] = ['state', '=', 1]; $where[] = ['id', '<>', $clueid]; $fileadded = CustomerClue::where($where)->find(); if ($fileadded && $fileadded->employee_id != $empid) { return json(['code' => 1, 'msg' => '不可建档,此客户此前已建档']); } else { return json(['code' => 0, 'msg' => '']); } } public function fileadding($empid, $clueid) { $condition = ['root_id' => request()->employee->root_id]; //小区名称获取 $communities = Community::where($condition)->where([['type', '=', 1]])->select()->toArray(); View::assign('communities', $communities); //风格获取 $decostyles = Decostyle::where($condition)->where([['type', '=', 1]])->select()->toArray(); View::assign('decostyles', $decostyles); //关注点产品获取 $products = Product::where($condition)->select()->toArray(); View::assign('products', $products); //来源获取 $sources = CustomerSource::where($condition)->select()->toArray(); View::assign('sources', $sources); View::assign('employee_id', $empid); $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); $clue = CustomerClue::where([['id', '=', $clueid], ['org_id', 'in', $subOrg]])->with('user')->findOrEmpty(); if ($clue->isEmpty()) { $j_clue = []; } else { $j_clue = json_encode($clue->toArray()); } View::assign('clue', $j_clue); View::assign('uid', $clue['uid']); View::assign('clue_id', $clueid); $footprint = Footprints::where(['pipe_type' => $clue['pipe_type'], 'id' => $clue['pipe_id']])->findOrEmpty(); $thefrom = ''; if (!$footprint->isEmpty()) { $thefrom = $footprint->reg_info; } View::assign('frompipe', $thefrom); return View::fetch(); } public function choiceadding() { $type = input('type'); if ($type == 'product') { return View::fetch('productadding'); } if ($type == 'source') { return View::fetch('sourceadding'); } } /* * 产品添加 */ public function apiproductadding() { $param = input(); $condition = [ 'root_id' => request()->employee->root_id ]; if (isset($param['id'])) { $condition['id'] = $param['id']; } else { $condition['product'] = $param['product']; } $exitedobj = Product::where($condition)->findOrEmpty(); if (empty($param['id']) && !$exitedobj->isEmpty()) { return json(['code' => 1, 'msg' => '产品已存在']); } $exitedobj->save([ 'root_id' => request()->employee->root_id, 'product' => $param['product'] ]); return json(['code' => 0, 'msg' => '保存成功', 'data' => ['id' => $exitedobj->id, 'name' => $exitedobj->product]]); } public function apisourceadding() { $param = input(); $condition = [ 'root_id' => request()->employee->root_id ]; if (isset($param['id'])) { $condition['id'] = $param['id']; } else { $condition['source'] = $param['source']; } $exitedobj = CustomerSource::where($condition)->findOrEmpty(); if (empty($param['id']) && !$exitedobj->isEmpty()) { return json(['code' => 1, 'msg' => '客户来源已存在']); } $exitedobj->save([ 'root_id' => request()->employee->root_id, 'source' => $param['source'] ]); return json(['code' => 0, 'msg' => '保存成功', 'data' => ['id' => $exitedobj->id, 'name' => $exitedobj->source]]); } // public function apicustomeradding() { $data = input(); $community = Community::where(['id' => $data['community_id'], 'root_id' => request()->employee->root_id])->find(); // $data['community_name'] = ($community) ? $community->name : ''; $decostyle = Decostyle::where(['id' => $data['style_id'], 'root_id' => request()->employee->root_id])->find(); $data['deco_style'] = ($decostyle) ? $decostyle->name : ''; // $data['product'] = Product::where(['id' => $data['product_id'], 'root_id' => request()->employee->root_id])->column('product'); $data['source_id'] = ($data['source_id'] == 0) ? null : $data['source_id']; // unset($data['community_id']); unset($data['style_id']); unset($data['product_id']); // $data['employee_id'] = request()->employee->id; $data['org_id'] = request()->employee->org_id; //首次到访时间小于上次沟通时间小于下次回访时间 /*if ($data['first_visit_date'] > $data['last_contact_date']) { return json(['code' => 1, 'msg' => '首次到访时间要小于上次沟通时间']); } if ($data['last_contact_date'] > $data['revisit_time']) { return json(['code' => 1, 'msg' => '上次沟通时间要小于回访时间']); } $data['first_visit_date'] = ($data['first_visit_date'] == '')?NULL:$data['first_visit_date']; $data['last_contact_date'] = ($data['last_contact_date'] == '')?NULL:$data['last_contact_date'];*/ $data['revisit_time'] = ($data['revisit_time'] == '') ? NULL : $data['revisit_time']; //本公司客户录入唯一 $orgids = orgSubIds(request()->employee->root_id); $condition[] = ['phone', '=', cypherphone(trim($data['phone']))]; $condition[] = ['org_id', 'in', $orgids]; $the_customer_by_other = Customer::where($condition)->find(); if ($the_customer_by_other && $the_customer_by_other->employee_id) { $emp = Employee::find($the_customer_by_other->employee_id); return json(['code' => 2, 'msg' => '录入失败。' . $data['phone'] . '为 ' . $emp->name . ' 的客户。已于' . $the_customer_by_other->addtime . '录入。']); } // $the_customer = Customer::where(['employee_id' => $data['employee_id'], 'phone' => trim($data['phone'])])->find(); if ($the_customer) { // Customer::where(['employee_id' => $data['employee_id'], 'phone' => trim($data['phone'])])->update($data); return json(['code' => 1, 'msg' => '客户已存在']); } else { $obj = new Customer(); if ($obj->save($data)) { if (isset($data['clue_id'])) { CustomerClue::where('id', $data['clue_id'])->update(['state' => 1]); } return json(['code' => 0, 'msg' => '保存成功']); } else { return json(['code' => 1, 'msg' => '保存失败']); } } } public function apicustomerediting() { $data = input(); //本公司客户录入唯一 $orgids = orgSubIds(request()->employee->root_id); $condition[] = ['phone', '=', cypherphone(trim($data['phone']))]; $condition[] = ['org_id', 'in', $orgids]; $condition[] = ['id', '<>', $data['id']]; $the_customer_by_other = Customer::where($condition)->find(); if ($the_customer_by_other && $the_customer_by_other->employee_id) { $emp = Employee::find($the_customer_by_other->employee_id); return json(['code' => 2, 'msg' => '录入失败。' . $data['phone'] . '为 ' . $emp->name . ' 的客户。已于' . $the_customer_by_other->addtime . '录入。']); } /// $community = Community::where(['id' => $data['community_id'], 'root_id' => request()->employee->root_id])->find(); // $data['community_name'] = ($community) ? $community->name : ''; $decostyle = Decostyle::where(['id' => $data['style_id'], 'root_id' => request()->employee->root_id])->find(); $data['deco_style'] = ($decostyle) ? $decostyle->name : ''; // $data['product'] = Product::where(['id' => $data['product_id'], 'root_id' => request()->employee->root_id])->column('product'); // $data['square'] = $data['square'] . '平方'; // unset($data['community_id']); unset($data['style_id']); unset($data['product_id']); if ($data['source_id'] === 'null') unset($data['source_id']); //$data['first_visit_date'] = ($data['first_visit_date'] == '')?NULL:$data['first_visit_date']; //$data['last_contact_date'] = ($data['last_contact_date'] == '')?NULL:$data['last_contact_date']; $data['revisit_time'] = ($data['revisit_time'] == '') ? NULL : $data['revisit_time']; if ($data['state'] == '无效') { $data['employee_id'] = NULL; $date['is_resource'] = 0; } $customer = Customer::where(['id' => $data['id']])->find(); if ($customer) { $customer->save($data); return json(['code' => 0, 'msg' => '保存成功']); } else { return json(['code' => 1, 'msg' => '保存失败']); } } public function apicustomerfileadding() { $data = input(); //首次到访时间小于上次沟通时间小于下次回访时间 if ($data['first_visit_date'] > $data['last_contact_date']) { return json(['code' => 1, 'msg' => '首次到访时间要小于上次沟通时间']); } if ($data['last_contact_date'] > $data['revisit_time']) { return json(['code' => 1, 'msg' => '上次沟通时间要小于回访时间']); } $community = Community::where(['id' => $data['community_id'], 'root_id' => request()->employee->root_id])->find(); $data['community_name'] = ($community) ? $community->name : ''; $decostyle = Decostyle::where(['id' => $data['style_id'], 'root_id' => request()->employee->root_id])->find(); $data['deco_style'] = ($decostyle) ? $decostyle->name : ''; $data['product'] = NULL; $data['square'] = $data['square'] . '平方'; $data['source_id'] = NULL; // unset($data['community_id']); unset($data['style_id']); unset($data['product_id']); // $data['employee_id'] = request()->employee_id; $data['org_id'] = request()->employee->org_id; $data['first_visit_date'] = ($data['first_visit_date'] == '') ? NULL : $data['first_visit_date']; $data['last_contact_date'] = ($data['last_contact_date'] == '') ? NULL : $data['last_contact_date']; $data['revisit_time'] = ($data['revisit_time'] == '') ? NULL : $data['revisit_time']; // $the_customer = Customer::where(['employee_id' => $data['employee_id'], 'phone' => trim($data['phone'])])->find(); if ($the_customer) { Customer::where(['employee_id' => $data['employee_id'], 'phone' => trim($data['phone'])])->update($data); } else { $obj = new Customer(); if ($obj->save($data)) { if (isset($data['clue_id'])) { CustomerClue::where('id', $data['clue_id'])->update(['state' => 1]); } return json(['code' => 0, 'msg' => '保存成功']); } else { return json(['code' => 1, 'msg' => '保存失败']); } } } /////////////////////////////// /// Tracking /// ////////////////////////// public function addtracking() { $request = request(); $id = $request->param('id'); View::assign('id', $id); View::assign('empid', $request->employee->id); return View::fetch(); } //public function apitrackingadding() //{ // $data = input(); // if(isset($data['revisit_time'])){ // Customer::where('id',$data['customer_id'])->update(['revisit_time' => $data['revisit_time']]); // } // // $obj = new Tracking(); // if ($obj->save($data)) { // return json(['code' => 0, 'msg' => '保存成功', 'data' => ['id' => $obj->id]]); // } //} public function apitrackingadding() { $request = request(); $param = $request->only(['customer_id', 'type', 'next_contact_date', 'remark', 'img']); $param['employee_id'] = $request->employee->id; $param['user_id'] = $request->employee->uid; !empty($param['next_contact_date']) ?: $param['next_contact_date'] = date('Y-m-d'); $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); $condition = [ ['id', '=', $param['customer_id']], ['state', 'not in', Customer::changeState('无效', 'chaos')], ['died', '<>', 2], ['org_id', 'in', $subOrg] ]; $had = Customer::where($condition)->find(); if (!$had) { return json(['code' => 0, 'msg' => '添加失败,数据不存在']); } //$state = $request->param('state'); //$param['state'] = $state; //根据跟进类型5678,设置保护时间 56类型为arrived到访 7类型为measured量房 8类型为deposited定金 $param['protected_to'] = NULL; /*if (in_array($param['type'], [5, 6, 7, 8])) { switch ($param['type']) { case 5: $protect_days_num = pubpoolSetting('arrived'); break; case 6: $protect_days_num = pubpoolSetting('arrived'); break; case 7: $protect_days_num = pubpoolSetting('measured'); break; case 8: $protect_days_num = pubpoolSetting('deposited'); break; } $time_til = time() + $protect_days_num * 86400; $had->protected_to = date('Y-m-d H:i:s', $time_til); $param['protected_to'] = date('Y-m-d H:i:s', $time_til); }*/ //// CustomerVisitLog::create($param); //$had->state = $state; $had->revisit_time = $param['next_contact_date']; $had->save(); return json(['code' => 0, 'msg' => '添加成功', 'revisit_time' => $param['next_contact_date']]); } /////////////////////////////// /// Pool 公海 /// ////////////////////////// public function pool() { $request = request(); if (!$request->isAjax()) { //公海 $pool = PoolLogic::struc(); View::assign('pool', $pool); // 部门 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); return View::fetch(); } $param = $request->only(['page', 'limit', 'order', 'pool']); $condition = []; $order = isset($param['order']) ? $param['order'] : 'id desc'; $condition[] = ['employee_id', '=', NULL]; $condition[] = ['is_resource', '=', 0]; $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); if (isset($param['pool']) && in_array($param['pool'], $orgSub)) { //$pool_obj = Pool::find($param['pool']); $condition[] = ['org_id', '=', $param['pool']]; } else { $orgids = orgSubIds($request->employee->root_id); $condition[] = ['org_id', 'in', $orgids]; } $data = CustomerLogic::poollist($condition, $param['page'], $param['limit'], $order); $count = CustomerLogic::count($condition); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function addpool() { $request = request(); if (!$request->isAjax()) { // 部门层级level $level = OrgLogic::level(); View::assign('level', $level); return View::fetch(); } $param = $request->param(); unset($param['checkbox']); $nodeArr = explode(',', $param['nodes']); foreach ($nodeArr as $orgid) { $orgobj = Org::find($orgid); $poolname = $orgobj->name . ' ' . $param['name'] . ' 公海'; PoolLogic::addpool($poolname, $orgid, $param['level']); } return json(['code' => 0, 'msg' => '保存成功']); } public function apilevelnodes() { $request = request(); $param = $request->only(['level']); $nodes = OrgLogic::levelnodes($param['level']); return json(['code' => 0, 'data' => $nodes, 'msg' => '获取成功']); } /// public function apisubnodes() { $request = request(); $param = $request->param(); $nodes = []; if ($param['org'] != 0) { //$nodes = OrgLogic::subnodes($param['org']); $nodes = OrgLogic::subfirstlevelnodes($param['org']); } return json(['code' => 0, 'data' => $nodes, 'msg' => '获取成功']); } /// public function apisubnodeswithempnum() { $request = request(); $param = $request->param(); $nodes = []; if ($param['org'] != 0) { $nodes = OrgLogic::subfirstlevelnodeswithempnum($param['org']); } return json(['code' => 0, 'data' => $nodes, 'msg' => '获取成功']); } /// public function distribute() { $request = request(); $param = $request->param(); $targetstr = substr($param['data'], 0, strlen($param['data']) - 1); View::assign('targetstr', $targetstr); //公海 $pool = PoolLogic::struc(); View::assign('pool', $pool); // 部门 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); return View::fetch(); } /** * 公海管理分配客户 */ public function distributing() { $request = request(); $param = $request->param(); $target_customer_ids_arr = explode(',', $param['targets']); $root_id = request()->employee->root_id; $subOrg = orgSubIds($root_id); $target_customer_ids_arr = Customer::where([['id', 'in', $target_customer_ids_arr], ['org_id', 'in', $subOrg]])->column('id'); if ($param['type'] == 'toemp' && isset($param['emp_id'])) { $employee_id = $param['emp_id']; $emp_obj = Employee::find($employee_id); $org_id = $emp_obj->org_id; // 设计师能否报备、获取客户 $de_where[] = ['root_id', '=', $root_id]; $de_where[] = ['name', '=', 'designer_get_customer']; $info_de_where = Setting::where($de_where)->findOrEmpty(); $org_type = Org::where('id', '=', $org_id)->value('org_type'); if (!$info_de_where->isEmpty() && $org_type == 2) { if ((int)$info_de_where['content'] != 1) { return json(['code' => 1, 'msg' => '无法分配给设计师']); } } //分配给业务员该名下不能存在相同客户(phone、phone1、phone2) $pool_crm = Customer::where('id', 'in', $target_customer_ids_arr)->field('name,phone,phone1,phone2')->select(); $name_str = []; foreach ($pool_crm as $item) { $phone_arr = []; if (!empty($item['phone'])) $phone_arr[] = cypherphone($item['phone']); if (!empty($item['phone1'])) $phone_arr[] = cypherphone($item['phone1']); if (!empty($item['phone2'])) $phone_arr[] = cypherphone($item['phone2']); $repeat_crm = Customer::where([['employee_id', '=', $param['emp_id']], ['phone|phone1|phone2', 'in', $phone_arr]])->count(); if (!empty($repeat_crm)) $name_str[] = $item['name'] ? $item['name'] : '未知'; } if (!empty($name_str)) { $name_str = implode(',', $name_str); return json(['code' => 1, 'msg' => '分配失败:员工名下已存在客户[' . $name_str . ']的联系方式']); } } else { $employee_id = null; $org_id = $param['org_pool']; } foreach ($target_customer_ids_arr as $item_id) { $before_pool = NULL; if ($param['type'] == 'topool') { $where = [ ['customer_id', '=', (int)$item_id] ]; $last_cvl = CustomerVisitLog::where($where)->with('employee')->order('addtime desc')->find(); if ($last_cvl) { $before_pool = [ 'name' => $last_cvl->employee->name, 'state' => $last_cvl->state, 'addtime' => $last_cvl->addtime ]; } else { $customer = Customer::find((int)$item_id); $before_pool = [ 'name' => '系统', 'state' => '待确认', 'addtime' => $customer->addtime ]; } // add customer visit log for frontend info showing $data['employee_id'] = request()->employee->id; $data['user_id'] = request()->employee->uid; $data['customer_id'] = (int)$item_id; $data['state'] = '无效'; $data['remark'] = '系统回收'; $data['type'] = 1; CustomerVisitLog::create($data); } $thisCustomer = Customer::find((int)$item_id); if ($thisCustomer->state == '无效') { $newstate = '待确认'; } else { $newstate = $thisCustomer->state; } if (!$employee_id && !empty($thisCustomer->employee_id)) { event(new Msg($thisCustomer->employee_id, '您有客户被回收到公海啦~快去看看吧!', 'toPool')); } if ($param['type'] == 'topool') { $newdata = [ 'employee_id' => $employee_id, 'org_id' => $org_id, 'state' => $newstate, 'remark' => '公海获取', 'before_pool' => $before_pool ? json_encode($before_pool) : NULL, 'ext->ext6' => '', 'designer_id' => NULL, 'employee_time' => date('Y-m-d H:i:s', time()) ]; } else { $newdata = [ 'employee_id' => $employee_id, 'org_id' => $org_id, 'state' => $newstate, 'remark' => '公海获取', 'fresh' => 1, 'before_pool' => $before_pool ? json_encode($before_pool) : NULL, 'ext->ext6' => '', 'designer_id' => NULL, 'employee_time' => date('Y-m-d H:i:s', time()) ]; } Customer::where(['id' => (int)$item_id])->update($newdata); } if ($param['type'] == 'toemp' && isset($param['emp_id'])) { // 更新客户保护期 Customer::changeProtectedTo($target_customer_ids_arr, request()->employee->root_id); } if (!empty($employee_id)) { event(new Msg($employee_id, request()->employee->name . '给您分配了一批客户资源,请前往查看', 'fromPool')); } return json(['code' => 0, 'msg' => '操作成功']); } public function withdrawl() { $request = request(); $param = $request->param(); $targetstr = substr($param['data'], 0, strlen($param['data']) - 1); View::assign('targetstr', $targetstr); //公海 $pool = PoolLogic::struc(); View::assign('pool', $pool); // 部门 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); //View::assign('orgids', json_encode($request->org)); return View::fetch(); } /** * 资源库导入 */ public function importing(){ $request = request(); // 页面展示 if (!$request->isPost()) { $data = CustomerSource::field('id,source')->where(['root_id' => request()->employee->root_id])->select(); View::assign('source', $data); $old_source = CrmImportLog::where('employee_id', request()->employee->id)->order('id desc')->value('source'); View::assign('old_source', $old_source); return View::fetch(); } $file = request()->file('file'); $root_id = request()->employee->root_id; if (empty($file)) { return json(['code' => 1, 'msg' => '请上传导入文件', 'data' => []]); } $source = input('source', '', 'intval'); $log['pid'] = 0; $log['org_id'] = $root_id; $log['employee_id'] = request()->employee->id; $log['name'] = $file->getOriginalName(); $log['count'] = 0; $log['avaliable_count'] = 0; $log['bad_phone_num'] = 0; $log['source'] = $source; $log['repeat_phone_number'] = 0; $log['status'] = 0; $log['root_id'] = $root_id; $log['path'] = Request::param('file'); $log['execute_rows'] = 0; $log['upload_source'] = 0; $log_id = CrmImportLog::insertGetId($log); if (!$log_id) { return json(['code'=> 1, 'msg'=> '导入失败']); } (new CrmImportLog())->save(['id'=> $log_id, 'status'=> 1]); $jobHandlerClassName = 'app\jobs\CrmImport'; $jobQueueName = 'crm_import'; $orderData = ['id'=> $log_id]; //这个是需要传到消费者的数据 Queue::later(0, $jobHandlerClassName, $orderData, $jobQueueName); return json(['code' => 0, 'msg' => '上传中,请手动刷新上传结果']); } private function checkphone($value) { if (preg_match("/^1[356789]\d{9}$/", $value)) { return true; } else { return false; } } public function resource() { $request = request(); if (!$request->isAjax()) { //公海 $pool = PoolLogic::struc(); View::assign('pool', $pool); // 部门 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); $data = CustomerSource::field('id,source')->where(['root_id' => request()->employee->root_id])->select(); View::assign('source', $data); //导入人员只显示有记录的人员 $org_ids = Org::where([['path', 'like', request()->employee->root_id . '-%']])->column('id'); $ids = CrmImportLog::where([['org_id', 'in', $org_ids]])->group('employee_id')->column('employee_id'); $person = Employee::where([['root_id', '=', request()->employee->root_id], ['grant_id', '>', 0], ['id', 'in', $ids]])->field('opt_name,id')->select(); View::assign('person', $person); $resourcefb = Setting::where(['root_id' => request()->employee->root_id, 'name' => 'resource_fp'])->value('content'); View::assign('resourcefb', $resourcefb === null ? 1 : (int)$resourcefb); return View::fetch('resource_new'); } $param = $request->only(['page', 'limit', 'org_pool', 'state' => '', 'source' => '', 'person' => '', 'start_date' => '', 'end_date' => '']); $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); if (isset($param['org_pool']) && in_array($param['org_pool'], $orgSub)) { $org = $param['org_pool']; } else { $org = request()->employee->root_id; } //$where[] = ['org_id', '=', $org]; $orgids = orgSubIds(request()->employee->root_id); /* $where[] = ['id', '>', 0]; $where1[] = ['org_id', 'in', $orgids]; $where1[] = ['pid', '=', 0]; $whereOr[] = ['org_id', 'in', $orgids]; $whereOr[] = ['employee_id', 'NOT NULL', null]; $whereOr[] = ['upload_source', '=', 1]; */ // 查询逻辑调整 $where[] = ['org_id', 'in', $orgids]; $where[] = ['pid', '=', 0]; //分配状态 if ($param['state'] !== '') $where[] = ['state', '=', $param['state']]; //来源渠道 if ($param['source'] !== '') $where[] = ['source', '=', $param['source']]; // 导入人员 if ($param['person'] !== '') $where[] = ['employee_id', '=', $param['person']]; //开始时间 if ($param['start_date'] !== '') $where[] = ['addtime', '>=', $param['start_date']]; //结束时间 if ($param['end_date'] !== '') $where[] = ['addtime', '<=', $param['end_date'] . ' 23:59:59']; $data = CrmImportLog::where($where)->with(['employee', 'org', 'optname'])->page($param['page'])->limit($param['limit'])->order('addtime', 'desc')->select(); //已分配数量 //$y_count = Customer::where([['crm_res_id','in',array_column($data->toArray(),'id')]])->group('crm_res_id')->column('count(id)','id'); $listid = CrmImportLog::where('id', '>', 0)->field('id,pid')->select()->toArray(); $pids = CrmImportLog::where([['pid', 'in', array_column($data->toArray(), 'id')]])->column('id,pid'); $arr = []; foreach ($pids as $k => $v) { $arr[$v['pid']][] = $v['id']; } foreach ($data as &$item) { if ($item->pid == 0) { $from = '数据导入'; } else { $cil = CrmImportLog::with('org')->find($item->pid); $from = $cil->org_info . ' 资源库'; } $item['from'] = $from; $item['belong'] = $item->org_info; $item['employee_name'] = $item['employee_opt_name'] ?: $item['employee_name']; $item['source_name'] = ''; if ($item['source']) { $source_name = CustomerSource::where('id', intval($item['source']))->value('source'); if ($source_name) { $item['source_name'] = $source_name; } else { $item['source_name'] = '其他'; } } //已分配数量 //$item['y_count'] = isset($y_count[$item['id']]) ? $y_count[$item['id']] : 0; $s_id = isset($arr[$item['id']]) ? array_merge($arr[$item['id']], [$item['id']]) : [$item['id']]; $ids = $this->getAllLowerLevelId($listid, $item['id'], $is_first_time = true); $ids[] = $item['id']; // if ($item['upload_source'] == 0) { $item['y_count'] = $item['avaliable_count'] - $item['left_num']; } else { $item['y_count'] = Customer::where([['is_distribution', '=', 1], ['crm_res_id', 'in', $ids]])->count(); } //$item['y_count'] = Customer::where([['is_distribution','=',1],['crm_res_id','in',$s_id]])->count(); $item['state_name'] = $item['state'] == 0 ? '未分配' : '已分配'; } $count = CrmImportLog::where($where)->count(); //查询资源库分组是否是最高层 $org_pid = Org::where('id', $org)->value('pid'); return json(['code' => 0, 'data' => $data, 'count' => $count, 'org' => $org_pid]); } public function resourcepool() { $request = request(); if (!$request->isAjax()) { //公海 $pool = PoolLogic::struc(); View::assign('pool', $pool); // 部门 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); View::assign('cilid', $request->param('cilid')); return View::fetch(); } $param = $request->only(['page', 'limit', 'order', 'cilid']); $condition = []; $order = isset($param['order']) ? $param['order'] : 'id desc'; $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); $condition[] = ['employee_id', '=', NULL]; $condition[] = ['is_resource', '=', 1]; $condition[] = ['crm_res_id', '=', $param['cilid']]; $condition[] = ['bad_phone', '=', 0]; $condition[] = ['org_id', 'in', $orgSub]; $data = CustomerLogic::resourcelist($condition, $param['page'], $param['limit'], $order); $count = CustomerLogic::count($condition); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function resdistribute() { $request = request(); $param = $request->param(); $targetstr = substr($param['data'], 0, strlen($param['data']) - 1); View::assign('targetstr', $targetstr); //批次id View::assign('cilid', $param['cilid']); //公海 $pool = PoolLogic::struc(); View::assign('pool', $pool); // 部门 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); //$orgids = orgSubIds(request()->employee->org_id); //View::assign('orgids', json_encode($orgids)); $log = CrmImportLog::find($param['cilid']); $theorg = Org::find($log->org_id); $orgids = Org::where([['path', 'like', $theorg->path . '%'], ['id', '<>', $log->org_id]])->column('id'); // $orgids = orgSubIds($log->org_id); // 当前资源库所辖subnodeid View::assign('orgids', json_encode($orgids)); $orgids[] = $theorg->id; View::assign('emporgids', json_encode($orgids)); return View::fetch(); } public function resdistributing() { $orgs = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['id', '<>', request()->employee->root_id]])->column('id'); $request = request(); $param = $request->param(); $target_customer_ids_arr = explode(',', $param['targets']); $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); $target_customer_ids_arr = Customer::where([['id', 'in', $target_customer_ids_arr], ['org_id', 'in', $orgSub]])->column('id'); $using_num = count($target_customer_ids_arr); $repeat = 0; //重复phone if ($param['type'] == 'toemp' && isset($param['emp_id'])) { $employee_id = $param['emp_id']; $emp_obj = Employee::find($employee_id); $org_id = $emp_obj->org_id; $is_source = 0; //2022-10-16 逻辑修改重复录入客户表有重复手机号,手动分配需要去重 $customers = Customer::where([['id', 'in', $target_customer_ids_arr]])->column('id,phone,phone1,phone2'); $phone = Customer::where('employee_id', $employee_id)->column('phone,phone1,phone2'); $phones = []; foreach ($phone as $v) { $phones = array_merge($phones, array_values($v)); } $phones = array_filter($phones); // $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $cids = []; foreach ($customers as $v2) { $l_phone = array_filter([$v2['phone'], $v2['phone1'], $v2['phone2']]); if (empty(array_intersect($l_phone, $phones))) { $cids[] = $v2['id']; } else { $repeat += 1; } } $using_num -= $repeat; if ($cids) { Customer::where([['id', 'in', $cids]])->update([ 'employee_id' => $employee_id, 'org_id' => $org_id, 'is_resource' => $is_source, 'ext->ext6' => '', 'designer_id' => NULL, 'employee_time' => date('Y-m-d H:i:s', time()) ]); Customer::changeProtectedTo($cids, request()->employee->root_id); $this->sendOfficialAccount([$employee_id => count($cids)]); //添加一条跟进记录 $visitlog = []; foreach ($cids as $val) { $visitlog[] = [ 'customer_id' => $val, 'type' => 1, 'remark' => '资源库分配', 'employee_id' => request()->employee->id, 'user_id' => request()->employee->uid, 'state' => '待确认' ]; } CustomerVisitLog::insertAll($visitlog); } // ---旧版本 // foreach ($target_customer_ids_arr as $item_id) { // Customer::where(['id' => (int)$item_id])->update([ // 'employee_id' => $employee_id, // 'org_id' => $org_id, // 'is_resource' => $is_source, // 'ext->ext6' => '', // 'designer_id' => NULL, // 'employee_time' => date('Y-m-d H:i:s', time()) // ]); // } // // 更新客户保护期 // Customer::changeProtectedTo($target_customer_ids_arr, request()->employee->root_id); //// 公众号消息 // $this->sendOfficialAccount([$employee_id => count($target_customer_ids_arr)]); } else { if (empty($param['org_pool'])) return json(['code' => 1, 'msg' => '请选择要分配至的资源库']); $employee_id = null; $aimed_org = Org::find($param['org_pool']); $org_id = $aimed_org->id; $is_source = 1; foreach ($target_customer_ids_arr as $item_id) { Customer::where(['id' => (int)$item_id])->update([ 'employee_id' => $employee_id, 'org_id' => $org_id, 'is_resource' => $is_source, 'ext->ext6' => '', 'designer_id' => NULL ]); } //build log $metaLog = CrmImportLog::find($param['cilid']); $newlog = new CrmImportLog(); $newdata = [ 'pid' => $metaLog->id, 'employee_id' => $metaLog->employee_id, 'org_id' => $org_id, 'name' => $metaLog->name, 'count' => $using_num, 'state' => 0, 'left_num' => $using_num, 'source' => $metaLog->source ]; $newlog->save($newdata); /// Customer::where(['crm_res_id' => $param['cilid'], 'org_id' => $org_id])->update(['crm_res_id' => $newlog->id]); } $leftNum = CrmImportLog::where('id', $param['cilid'])->column('left_num'); $newleftNum = ($leftNum[0] - $using_num) > 0 ? ($leftNum[0] - $using_num) : 0; if (CrmImportLog::where('id', $param['cilid'])->update(['state' => 1, 'left_num' => $newleftNum]) !== false) { return json(['code' => 0, 'msg' => '操作成功', 'repeat' => $repeat]); } else { return json(['code' => 1, 'msg' => '操作失败']); } } public function patchdistribute() { $request = request(); $param = $request->param(); if (!$request->isAjax()) { //公海 $pool = PoolLogic::struc(); View::assign('pool', $pool); // 部门 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); //$orgids = orgSubIds(request()->employee->org_id); //$orgids = [request()->employee->org_id]; //只显示自己所在的node选项 $log = CrmImportLog::find($request->param('cilid')); View::assign('resnum', $log->left_num); View::assign('cilid', $log->id); $orgids = [$log->org_id]; //显示此资源库所辖node选项 View::assign('orgids', json_encode($orgids)); return View::fetch(); } $target_customer_ids_arr = explode(',', $param['targets']); $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); $target_customer_ids_arr = Customer::where([['id', 'in', $target_customer_ids_arr], ['org_id', 'in', $orgSub]])->column('id'); if ($param['type'] == 'toemp' && isset($param['emp_id'])) { $employee_id = $param['emp_id']; $emp_obj = Employee::find($employee_id); $org_id = $emp_obj->org_id; $employee_time = date('Y-m-d H:i:s', time()); } else { $employee_id = null; $aimed_org_id_arr = Pool::where('id', $param['pool'])->column('org_id'); $org_id = $aimed_org_id_arr[0]; $employee_time = NULL; } foreach ($target_customer_ids_arr as $item_id) { Customer::where(['id' => (int)$item_id])->update([ 'employee_id' => $employee_id, 'org_id' => $org_id, 'ext->ext6' => '', 'designer_id' => NULL, 'employee_time' => $employee_time ]); } if ($param['type'] == 'toemp' && isset($param['emp_id'])) { // 更新客户保护期 Customer::changeProtectedTo($target_customer_ids_arr, request()->employee->root_id); } return json(['code' => 0, 'msg' => '操作成功']); } public function patchdistributing() { $orgs = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['id', '<>', request()->employee->root_id]])->column('id'); $request = request(); $param = $request->param(); $repeat = []; //重复客户 $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); if ($param['type'] == 'topool') { $leftNum = 0; // 更新数据准备 $target_ids_arr = json_decode($param['targets'], true); $target_ids_arr = Customer::where([['id', 'in', $target_ids_arr], ['org_id', 'in', $target_ids_arr]])->column('id'); $group = count($target_ids_arr); $resource = Customer::where(['crm_res_id' => $param['cilid'], 'bad_phone' => 0])->column('id'); // $resource = Customer::where(['crm_res_id' => $param['cilid'], 'bad_phone' => 0])->column('id,phone,phone1,phone2'); // $cid = []; // foreach ($resource as $resource_item) { // // $cid[] = $resource_item['id']; // $query = []; // $l_phone = array_filter([$resource_item['phone'],$resource_item['phone1'],$resource_item['phone2']]); // $query[] = ['phone|phone1|phone2','in',$l_phone]; // $query[] = ['org_id','in',$orgs];//所有的公海都不能重复 // $query[] = ['employee_id','=',null]; // $check = Customer::where($query)->findOrEmpty(); // $check->isEmpty() ? $cid[] = $resource_item['id'] : $repeat[] = $check->id; // } // $leftNum += count($repeat);//剩余的数量等于重复的加上 // if ($cid) { // $resource = $cid; // 数据分割 $resourceChunk = array_chunk($resource, $group); // id分组 $groupIds = []; foreach ($resourceChunk as $item) { if (count($item) != $group) { $leftNum += count($item); break; } foreach ($item as $g => $v) { $groupIds[$g][] = $v; } } // 查询老记录 $metaLog = CrmImportLog::find($param['cilid']); // 更新记录 foreach ($target_ids_arr as $i => $crmResId) { $pageNum = count($groupIds[$i]); $log = CrmImportLog::create([ 'pid' => $metaLog->id, 'employee_id' => $metaLog->employee_id, 'org_id' => $crmResId, 'name' => $metaLog->name, 'count' => $pageNum, 'state' => 0, 'left_num' => $pageNum, 'source' => $metaLog->source ]); Customer::where([['id', 'in', $groupIds[$i]]])->update([ 'employee_id' => NULL, 'org_id' => $crmResId, 'is_resource' => 1, 'crm_res_id' => $log->id, 'ext->ext6' => '', 'designer_id' => NULL ]); } // } } if ($param['type'] == 'toemp') { //2022-10-16 分配逻辑修改 单个业务员不出现重复 单次导入的表格中一定没有重复手机号 $employees = json_decode($param['targets'], true); //业务员 $employees = Employee::where([['id', 'in', $employees]])->column('org_id', 'id'); $where[] = ['crm_res_id', '=', $param['cilid']]; $where[] = ['bad_phone', '=', 0]; $where[] = ['employee_id', '=', NULL]; $customers = Customer::where($where)->column('id,phone,phone1,phone2'); //要分配的所有客户 if (empty($employees) || empty($customers)) return json(['code' => 1, 'msg' => '分配失败']); $eids = array_keys($employees); $arr = []; shuffle($customers); //数组顺序打乱随机分配 // 检测电话是否已经存在 // $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); foreach ($customers as $v) { $l_phone = array_filter([$v['phone'], $v['phone1'], $v['phone2']]); //分给没有此手机号的业务员 单个业务员的客户手机号不能重复 $query = []; $query[] = ['phone|phone1|phone2', 'in', $l_phone]; $query[] = ['employee_id', 'in', $eids]; $eid = Customer::where($query)->group('employee_id')->column('employee_id'); $eid = array_values(array_diff($eids, $eid)); //去除已经有此手机号的业务员 if (empty($eid)) { //如果手机号所有人都有则 不分配 $repeat[] = $v['id']; continue; } if (count($eid) == 1) { isset($arr[$eid[0]]) ? array_push($arr[$eid[0]], $v['id']) : $arr[$eid[0]] = [$v['id']]; // $arr[$eid[0]][] = $v['id'];//只有一人可分 直接分配 } else { //超过一人分给数量最少的业务员 //没有分过的业务员优先 $check = true; foreach ($eid as $v2) { if (!isset($arr[$v2])) { $arr[$v2] = [$v['id']]; $check = false; break; } } //都分过则分给数量最少的业务员 if ($check) { $count = count($arr[$eid[0]]); $f_eid = $eid[0]; foreach ($eid as $v3) { if (count($arr[$v3]) < $count) $f_eid = $v3; $count = count($arr[$v3]); } array_push($arr[$f_eid], $v['id']); } } } if ($arr) { $visitlog = []; //分配客户 $send = []; //发送公众号消息数组 foreach ($arr as $k4 => $v4) { Customer::where([['id', 'in', $v4]])->update([ 'employee_id' => $k4, 'org_id' => $employees[$k4], 'is_resource' => 0, 'ext->ext6' => '', 'designer_id' => NULL, 'employee_time' => date('Y-m-d H:i:s', time()) ]); // 更新客户保护期 Customer::changeProtectedTo($v4, request()->employee->root_id); $send[$k4] = count($v4); //添加一条跟进记录 foreach ($v4 as $val) { $visitlog[] = [ 'customer_id' => $val, 'type' => 1, 'remark' => '资源库分配', 'employee_id' => request()->employee->id, 'user_id' => request()->employee->uid, 'state' => '待确认' ]; } } if ($visitlog) CustomerVisitLog::insertAll($visitlog); $this->sendOfficialAccount($send); } $leftNum = count($repeat); // ---旧版本 // $target_ids_arr = json_decode($param['targets'], true); // $where[] = ['crm_res_id', '=', $param['cilid']]; // $where[] = ['bad_phone', '=', 0]; // $where[] = ['employee_id', '=', NULL]; // $resource = Customer::where($where)->select()->toArray(); // $timesNum = floor(count($resource) / count($target_ids_arr)); // $leftNum = count($resource) % count($target_ids_arr); // $send = []; // for ($i = 0; $i < count($target_ids_arr); $i++) { // $empObj = Employee::find($target_ids_arr[$i]); // for ($t = 0; $t < $timesNum; $t++) { // Customer::where('id', $resource[$i + $t * count($target_ids_arr)]['id'])->update([ // 'employee_id' => $target_ids_arr[$i], // 'org_id' => $empObj->org_id, // 'is_resource' => 0, // 'ext->ext6' => '', // 'designer_id' => NULL, // 'employee_time' => date('Y-m-d H:i:s', time()) // ]); // // 更新客户保护期 // Customer::changeProtectedTo($resource[$i + $t * count($target_ids_arr)]['id'], request()->employee->root_id); // //消息 // isset($send[$target_ids_arr[$i]]) ? $send[$target_ids_arr[$i]] += 1 : $send[$target_ids_arr[$i]] = 1; // } // } //发送消息提醒 // $this->sendOfficialAccount($send); } if ($param['type'] == 'toinstitue') { $target_arr = json_decode($param['targets'], true); $resource = Customer::where(['crm_res_id' => $param['cilid'], 'bad_phone' => 0])->column('id'); // $resource = Customer::where(['crm_res_id' => $param['cilid'], 'bad_phone' => 0])->column('id,phone,phone1,phone2'); // $cid = []; // foreach ($resource as $resource_item) { // $query = []; // $l_phone = array_filter([$resource_item['phone'],$resource_item['phone1'],$resource_item['phone2']]); // $query[] = ['phone|phone1|phone2','in',$l_phone]; // $query[] = ['org_id','in',$orgs];//所有的公海都不能重复 // $query[] = ['employee_id','=',null]; // $check = Customer::where($query)->findOrEmpty(); // $check->isEmpty() ? $cid[] = $resource_item['id'] : $repeat[] = $check->id; // } // $resource = $cid; $totalNum = count($resource); $data_arr = []; $start = 0; foreach ($target_arr['_vals'] as $i => $l) { $data_arr[$target_arr['_keys'][$i]] = array_slice($resource, $start, $l); $start += $l; } ///build log $metaLog = CrmImportLog::find($param['cilid']); foreach ($data_arr as $org_id => $empList) { $pageNum = count($empList); $log = CrmImportLog::create([ 'pid' => $metaLog->id, 'employee_id' => $metaLog->employee_id, 'org_id' => $org_id, 'name' => $metaLog->name, 'count' => $pageNum, 'state' => 0, 'left_num' => $pageNum, 'source' => $metaLog->source ]); Customer::where([['id', 'in', $empList]])->update([ 'employee_id' => NULL, 'org_id' => $org_id, 'is_resource' => 1, 'crm_res_id' => $log->id, 'ext->ext6' => '', 'designer_id' => NULL ]); } $leftNum = ($totalNum - $start) > 0 ? $totalNum - $start : 0; $leftNum += count($repeat); //剩余的加上重复的 } if (CrmImportLog::where('id', $param['cilid'])->update(['state' => 1, 'left_num' => $leftNum]) !== false) { return json(['code' => 0, 'msg' => '操作成功', 'repeat' => count($repeat)]); } else { return json(['code' => 1, 'msg' => '操作失败']); } } /** * 发送公众号消息 * employee_id => count 业务员id =》 分配数量 */ public function sendOfficialAccount($send) { $client_type = Miniprogram::where('root_id', request()->employee->root_id)->value('notify'); foreach ($send as $k => $v) { //业务员客户数量 $count = Customer::where([['employee_id', '=', $k], ['crm_res_id', '>', 0]])->count(); $data = $msg = []; $data['page'] = "/pages/index/index?office_type=allocation&clientype=" . $client_type; $data['param'] = [ 'first' => '客户分配提醒', //标题 'keyword1' => '共计' . $v . '个客户资源', 'keyword2' => date('Y-m-d H:i:s'), 'remark' => '累计分配' . $count . '个客户资源,请及时跟进' // ]; $msg = [ 'type' => 'resourceAllocation', 'employee_id' => $k ]; EmployeeMsg::temploateMsg($msg, $data); unset($data); unset($msg); } return true; } public function cil_detail() { $request = request(); $param = $request->param(); if (!$request->isAjax()) { $rs = Customer::where([['crm_res_id', '=', $param['cilid']], ['employee_id', 'not null', '']])->group('employee_id')->column('employee_id, count(id) as count'); $emp = Employee::where([['id', 'in', array_column($rs, 'employee_id')]])->column('name', 'id'); foreach ($rs as &$customer) { $customer['name'] = isset($emp[$customer['employee_id']]) ? $emp[$customer['employee_id']] : ''; } $logs = CrmImportLog::where('pid', $param['cilid'])->column('org_id, count'); $orgs = Org::where([['id', 'in', array_column($logs, 'org_id')]])->column('info', 'id'); foreach ($logs as &$item) { $item['name'] = isset($orgs[$item['org_id']]) ? $orgs[$item['org_id']] : ''; } View::assign('respool', $logs); View::assign('emp', $rs); return View::fetch(); } } /** * 来源列表 */ public function sourcelist() { return View::fetch(); } public function typelist() { return View::fetch(); } public function source($page, $limit) { $data = CustomerSource::field('id,source')->where(['root_id' => request()->employee->root_id])->page($page, $limit)->order('id desc')->select(); $count = CustomerSource::where(['root_id' => request()->employee->root_id])->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function product($page, $limit) { $data = Product::field('id,product')->where(['root_id' => request()->employee->root_id])->page($page, $limit)->select(); $count = Product::where(['root_id' => request()->employee->root_id])->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function community($page, $limit) { $data = Community::field('id,name as community')->where(['root_id' => request()->employee->root_id])->where([['type', '=', 1]])->page($page, $limit)->select(); $count = Community::where(['root_id' => request()->employee->root_id])->where([['type', '=', 1]])->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function decostyle($page, $limit) { $type = input('type', 1); $data = Decostyle::field('id,name as decostyle')->where(['root_id' => request()->employee->root_id])->where([['type', '=', $type]])->order('id desc')->page($page, $limit)->select(); $count = Decostyle::where(['root_id' => request()->employee->root_id])->where([['type', '=', $type]])->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } public function delproduct($id) { Product::where(['root_id' => request()->employee->root_id, 'id' => $id])->delete(); return json(['code' => 0, 'msg' => '删除成功']); } public function delsource($id) { CustomerSource::where(['root_id' => request()->employee->root_id, 'id' => $id])->delete(); return json(['code' => 0, 'msg' => '删除成功']); } public function delcommunity($id) { Community::where(['root_id' => request()->employee->root_id, 'id' => $id])->delete(); return json(['code' => 0, 'msg' => '删除成功']); } public function deldecostyle($id) { Decostyle::where(['root_id' => request()->employee->root_id, 'id' => $id])->delete(); return json(['code' => 0, 'msg' => '删除成功']); } public function log() { $request = request(); $id = input('id', 0); view::assign('id', $id); $root_id = request()->employee->root_id; $ew[] = ['root_id', '=', $root_id]; $employee = Employee::where($ew)->field('id,name')->select()->toArray(); view::assign('employee', $employee); if (!$request->isAjax()) return View::fetch(); $page = input('page', 0); $limit = input('limit', 0); $eid = input('eid', 0); if ($eid) { $w[] = ['employee_id', '=', $eid]; } $type = input('type', ''); if ($type) { $w[] = ['state', '=', $type]; } $method = input('method', ''); if ($method) { $w[] = ['type', '=', $method]; } $w[] = ['customer_id', '=', $id]; $res = CustomerVisitLog::with('employee')->where($w)->page($page, $limit)->order('id desc')->select(); $new_res = []; foreach ($res as $k => $v) { $new_res[$k] = $v->toArray(); //返回数字状态 $new_res[$k]['old_state'] = $v->getData('state'); $new_res[$k]['name'] = $v['employee']['name']; if ($v['state'] == '已签单') { $new_res[$k]['state'] = '已转单'; } if ($v['state'] == '已交定') { $new_res[$k]['state'] = '已签单'; } } $count = CustomerVisitLog::with('employee')->where($w)->count(); return json(['code' => 0, 'data' => $new_res, 'count' => $count, 'msg' => '获取成功']); } /** * 删除客户 */ public function delete() { $ids = request()->param('ids'); if ($ids) { $orgids = orgSubIds(request()->employee->root_id); $condition[] = ['org_id', 'in', $orgids]; $condition[] = ['id', 'in', $ids]; $condition[] = ['employee_id', '=', NULL]; $condition[] = ['is_resource', '=', 0]; $customers = Customer::where($condition)->column('*'); $del_false = 0; foreach ($customers as $k => $v) { Db::startTrans(); $v['customer_id'] = $v['id']; if ($v['revisit_time'] == '0000-00-00 00:00:00') { $v['revisit_time'] = null; } $v['package_id'] = !empty($v['package_id']) ? $v['package_id'] : 0; unset($v['id']); unset($v['employee_time']); unset($v['add_wechat_time']); unset($v['group_building_date']); unset($v['live_broadcast_date']); unset($v['subscribe_meet_outside']); unset($v['meet_outside']); unset($v['sign_time']); unset($v['reputation_gather']); unset($v['into_owner_group']); $result = (new CustomerRecycle())->insertGetId($v); if ($result) { $del_result = Customer::where('id', $v['customer_id'])->find()->force()->delete(); if ($del_result) { Db::commit(); } else { $del_false++; Db::rollback(); } } } if ($del_false > 0) { return json(['code' => 0, 'msg' => '部分客户删除成功']); } else { return json(['code' => 0, 'msg' => '删除成功']); } } else { return json(['code' => 0, 'msg' => '删除失败']); } } /** * 客户回收站 */ public function recycle() { $request = request(); if (!$request->isAjax()) { // 部门 $org = OrgLogic::struc($request->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); return View::fetch(); } $param = $request->only(['page', 'limit', 'order', 'org', 'state']); $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $orgSub)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds($root_id); } $condition = [['org_id', 'in', $orgids]]; $order = isset($param['order']) ? $param['order'] : 'id desc'; setCondition($param, 'state', '=', $condition); $data = CustomerRecycleLogic::list($condition, $param['page'], $param['limit'], $order); $count = CustomerRecycleLogic::count($condition); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 取消删除 * 2022-10-19 恢复 在公海中查询重复,如果有重复则不让恢复 */ public function undelete() { $customer_ids = request()->param('ids', '', 'trim'); $orgids = orgSubIds(request()->employee->root_id); // $condition[] = ['org_id', 'in', $orgids]; $customer_ids = array_values(array_filter(explode(',', $customer_ids))); if (count($customer_ids) == 1) { $condition[] = ['customer_id', '=', $customer_ids[0]]; } else { $condition[] = ['customer_id', 'in', $customer_ids]; } $customers = CustomerRecycle::where($condition)->column('*'); $error_number = 0; //$orgs = Org::where([['path', 'like', request()->employee->root_id . '-%']])->column('id'); $repeat = 0; $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); foreach ($customers as $k => $v) { if (!in_array($v['org_id'], $orgids)) { continue; } //查询公海中是否有重复的数据 $l_phone = array_values(array_filter([$v['phone'], $v['phone1'], $v['phone2']])); $query = []; if (count($l_phone) == 1) { $query[] = ['phone|phone1|phone2', '=', $l_phone[0]]; } else { $query[] = ['phone|phone1|phone2', 'in', $l_phone]; } $query[] = ['employee_id', '=', null]; $query[] = ['org_id', 'in', $orgids]; $query[] = ['is_resource', '=', 0]; //公海 $check = Customer::where($query)->findOrEmpty(); if (!$check->isEmpty()) { $repeat += 1; continue; } if ($v['phone']) $v['phone'] = $aec->decrypt($v['phone']); if ($v['phone1']) $v['phone1'] = $aec->decrypt($v['phone1']); if ($v['phone2']) $v['phone2'] = $aec->decrypt($v['phone2']); $info = $v; unset($info['id']); unset($info['deletetime']); $info['id'] = $info['customer_id']; $info['designer_id'] = NULL; unset($info['customer_id']); //删除所有的虚拟字段 unset($info['sign_time'],$info['add_wechat_time'],$info['group_building_date'],$info['live_broadcast_date'],$info['reputation_gather'],$info['into_owner_group'], $info['subscribe_meet_outside'], $info['meet_outside']); Db::startTrans(); $result = Customer::create($info); if ($result) { //$result['ext->ext6'] = ''; $result->save(); $del_result = (new CustomerRecycle())->where('id', '=', $v['id'])->delete(); if ($del_result !== false) { Db::commit(); } else { Db::rollback(); $error_number++; } } else { Db::rollback(); $error_number++; } } if ($error_number) { return json(['code' => 0, 'msg' => '部分数据操作成功', 'repeat' => $repeat]); } else { return json(['code' => 0, 'msg' => '操作成功', 'repeat' => $repeat]); } } /** * 企微客户列表 * @return string|\think\response\Json */ public function wework() { $request = request(); if (!$request->isAjax()) { // 判断此家企业是否关联企业微信 $company_id = Company::where('root_id', '=', request()->employee->root_id)->value('id'); $company_setting = WeworksingleCompanySetting::where('company_id', '=', $company_id)->find(); if (empty($company_setting)) { // 没配置就是没开,跳转到没开通的页面 } // 部门 $org = OrgLogic::struc($request->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); return View::fetch(); } // 外部联系人关联的customer_id $company_id = Company::where('root_id', '=', request()->employee->root_id)->value('id'); $customer_ids = WeworksingleCustomer::where('company_id', '=', $company_id)->column('customer_id'); $condition[] = ['id', 'in', $customer_ids]; $param = $request->only(['page', 'limit', 'order', 'org', 'emp_id', 'state']); $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $orgSub)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds($root_id); } $condition[] = ['org_id', 'in', $orgids]; $order = isset($param['order']) ? $param['order'] : 'id desc'; $condition[] = ['employee_id', 'not null', '']; setCondition($param, ['emp_id', 'employee_id'], '=', $condition); setCondition($param, 'state', '=', $condition); $data = CustomerLogic::list($condition, $param['page'], $param['limit'], $order); $count = CustomerLogic::count($condition); $customer_ext_ids = array_column($data, 'id'); $external_list = WeworksingleCustomer::where('customer_id', 'in', $customer_ext_ids)->select(); foreach ($data as $k => $v) { foreach ($external_list as $kk => $vv) { if ($v['id'] == $vv['customer_id']) { $data[$k]['wework_name'] = $vv['name']; $data[$k]['wework_avatar'] = $vv['avatar']; } } } return json(['code' => 0, 'data' => $data, 'count' => $count]); } //资源库迭代新增接口 /** * 客户画像 */ public function portrait() { $token = request()->employee; $info = CustomerPortrait::where('root_id', $token['root_id'])->findOrEmpty(); $data = $info->fields; $data['basic_info_edit'] = $data['basic_info']; $data['basic_info'] = []; $arr = ['name', 'phone', 'sex']; foreach ($data['basic_info_edit'] as $k => $v) { if (in_array($k, $arr)) { $data['basic_info'][$k] = $v; unset($data['basic_info_edit'][$k]); } } view::assign('data', $data); return View::fetch(); } /** * 客户画像 */ public function portrait_edit() { $type = input('type', ''); $field = input('field', ''); if ($type && $field) { $token = request()->employee; $info = CustomerPortrait::where('root_id', $token['root_id'])->findOrEmpty(); $json = $info->fields; $json[$type][$field]['status'] = $json[$type][$field]['status'] ? 0 : 1; //房屋类型和交房时间绑定一起 if ($type == 'house_info' && in_array($field, ['house_type', 'house_delivery_time'])) { $binding = $field == 'house_type' ? 'house_delivery_time' : 'house_type'; $json[$type][$binding]['status'] = $json[$type][$field]['status']; } if ($json[$type][$field]['status'] == 1) { $coll = 0; $json['info_status'][$type] = 1; } else { //取消时查询是否全部取消 $coll = 1; foreach ($json[$type] as $k => $v) { if ($v['status'] == 1) $coll = 0; } if ($coll == 1) { $json['info_status'][$type] = 0; } } !$info->isEmpty() ?: $info->root_id = $token['root_id']; $info->fields = $json; $info->save(); return json(['code' => 0, 'data' => '操作成功', 'coll' => $coll]); } return json(['code' => 1, 'data' => '操作失败']); } /** * 客户画像 */ public function portrait_type() { $type = input('type', ''); if ($type) { $token = request()->employee; $info = CustomerPortrait::where('root_id', $token['root_id'])->findOrEmpty(); $json = $info->fields; $json['info_status'][$type] = $json['info_status'][$type] ? 0 : 1; foreach ($json[$type] as $k => $v) { $json[$type][$k]['status'] = $json['info_status'][$type]; } !$info->isEmpty() ?: $info->root_id = $token['root_id']; $info->fields = $json; $info->save(); return json(['code' => 0, 'data' => '操作成功']); } return json(['code' => 1, 'data' => '操作失败']); } /** * 自动回收列表 */ public function drop_pool() { $request = request(); if (!$request->isAjax()) { // 部门 $org = OrgLogic::struc($request->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('empid', $request->employee->id); // View::assign('manager', $request->employee->is_manager); return View::fetch(); } $param = $request->only(['page', 'limit', 'order', 'org', 'emp_id', 'state']); $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $orgSub)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds($root_id); } $condition = [['org_id', 'in', $orgids]]; $order = isset($param['order']) ? $param['order'] : 'id desc'; $condition[] = ['employee_id', 'not null', '']; setCondition($param, ['emp_id', 'employee_id'], '=', $condition); empty($param['state']) ?: setCondition(['state' => Customer::changeState($param['state'], 'chaos')], 'state', 'in', $condition); $data = CustomerDropPool::with(['employee', 'org', 'designer'])->withCount(['visitLog' => function ($query) { $query->whereOr([[CustomerVisitLog::changeState(['state', '=', '预约量房'])], [CustomerVisitLog::changeState(['state', '=', '预约到店'])], [CustomerVisitLog::changeState(['state', '=', '预约活动'])]]); }])->withCount(['activityFrequency' => function ($query) { $query->where([CustomerVisitLog::changeState(['state', '=', '确认到场'])]); }]) ->where($condition) ->page($param['page'], $param['limit'])->order($order)->select(); $data = $data->visible(['id', 'employee_id', 'name', 'community_name', 'phone', 'phone1', 'phone2', 'level', 'state', 'square', 'revisit_time', 'addtime', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'droptime'])->toArray(); foreach ($data as &$item) { $item['protected'] = false; if (isset($item['protected_to']) && time() < strtotime($item['protected_to'])) { $item['protected'] = true; } $item['square'] = floatval($item['square']); $item['mobile'] = $item['phone']; $item['mobile1'] = $item['phone1']; $item['mobile2'] = $item['phone2']; $item['phone'] = substr_replace($item['phone'], '******', 3, 6); } $count = CustomerDropPool::where($condition)->count(); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 主动回收 */ public function drop_to_pool() { $ids = input('id', '', 'trim'); $orgIds = orgSubIds(request()->employee->root_id); $data = Customer::where([['org_id', 'in', $orgIds], ['id', 'in', $ids]])->select(); foreach ($data as $item) { Db::startTrans(); $drop_data = $item->toArray(); $drop_data['customer_id'] = $drop_data['id']; unset($drop_data['id']); unset($drop_data['add_wechat_time']); unset($drop_data['group_building_date']); unset($drop_data['live_broadcast_date']); unset($drop_data['subscribe_meet_outside']); unset($drop_data['meet_outside']); unset($drop_data['sign_time']); unset($drop_data['reputation_gather']); unset($drop_data['into_owner_group']); $drop_data['package_id'] = !empty($drop_data['package_id']) ? $drop_data['package_id'] : 0; $drop_data['deposit_money'] = $item->getData('deposit_money'); $drop_data['signed_money'] = $item->getData('signed_money'); $save_result = CustomerDropPool::create($drop_data); $delete_result = $item->force()->delete(); $record_data['customer_id'] = $item->id; CustomerDropRecord::create($record_data); // 客户预约中改为无效 CustomersSubscribe::where([['customer_id', '=', $item->id], ['state', '=', 0]])->save(['state' => -1]); if ($save_result && $delete_result) { Db::commit(); $param = [ 'customer_id' => $item->id, 'type' => 1, 'remark' => '客户回收', 'employee_id' => request()->employee->id, 'user_id' => request()->employee->uid, 'state' => '' ]; CustomerVisitLog::create($param); } else { Db::rollback(); } } return json(['code' => 0, 'msg' => '回收成功']); } public function drop_to_pool_check() { $ids = input('id', '', 'trim'); $customer_list = Customer::where([['id', 'in', $ids]])->select(); $state_list = $customer_list->column('state'); $sign_state = Customer::changeState('签单', 'chaos'); $sign_count = 0; foreach ($state_list as $k => $v) { if (in_array($v, $sign_state)) { $sign_count++; } } $protected = 0; $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', request()->employee->root_id]])->find(); if (!empty($setting)) { $content = !empty($setting['content']) ? json_decode($setting['content'], true) : []; $empty = false; if (!empty($content)) { foreach ($content as $k => $v) { if (!isset($v['day']) || !isset($v['state'])) { $empty = true; } } } if ($empty) { $content = []; } foreach ($customer_list as $k => $v) { $n = Customer::changeState($v['state'], 'n'); foreach ($content as $kk => $vv) { if (intval($kk) == intval($n) && $vv['state'] == 1 && $vv['day'] > 0 && !empty($v['protected_to']) && $v['protected_to'] > date('Y-m-d H:i:s', time())) { $protected++; } } } } $msg = ''; if ($sign_count && $protected) { $msg = '包含已签单及未到保护期客户'; } elseif ($sign_count) { $msg = '包含已签单客户'; } elseif ($protected) { $msg = '包含未到保护期客户'; } if ($msg == '') { return json(['code' => 0, 'msg' => '']); } else { return json(['code' => 1, 'msg' => $msg]); } } /** * 自动回收设置 */ public function drop_pool_setting() { $root_id = request()->employee->root_id; $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'pubpool']])->findOrEmpty(); $empty = false; //判断是否是正常数据,因为之前可能有垃圾数据 if ($setting->isEmpty()) { $empty = true; } if (!empty($setting)) { $content = !empty($setting['content']) ? json_decode($setting['content'], true) : []; if (!empty($content)) { foreach ($content as $k => $v) { if (!isset($v['day']) || !isset($v['state'])) { $empty = true; } } } } // 标准状态 $sys_content = [ 0 => ['day' => 0, 'state' => 0, 'name' => '待确认'], 1 => ['day' => 0, 'state' => 0, 'name' => '未到访'], 2 => ['day' => 0, 'state' => 0, 'name' => '已到店'], 3 => ['day' => 0, 'state' => 0, 'name' => '已到场'], 4 => ['day' => 0, 'state' => 0, 'name' => '已量房'], 5 => ['day' => 0, 'state' => 0, 'name' => '已交定'], ]; if ($empty) { $data['name'] = 'pubpool'; $data['state'] = 1; $data['root_id'] = $root_id; $data['content'] = json_encode($sys_content); Setting::create($data); $data['content'] = [ 'a' => ['day' => 0, 'state' => 0, 'name' => '待确认'], 'b' => ['day' => 0, 'state' => 0, 'name' => '已到店'], 'c' => ['day' => 0, 'state' => 0, 'name' => '已到场'], 'd' => ['day' => 0, 'state' => 0, 'name' => '已量房'], 'e' => ['day' => 0, 'state' => 0, 'name' => '已交定'], 'f' => ['day' => 0, 'state' => 0, 'name' => '未到访'], ]; View::assign('info', $data); } else { $key_val = [ 'a' => 0, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 1 ]; $content = !empty($setting['content']) ? json_decode($setting['content'], true) : []; $new_content = []; foreach ($key_val as $k => $v) { $new_content[$k] = $sys_content[$v]; foreach ($content as $kk => $vv) { if ($kk == $v) { $new_content[$k] = $vv; } } } $setting['content'] = $new_content; View::assign('info', $setting); } return View::fetch(); } /** * 修改自动回收设置 * @return string */ public function drop_pool_setting_edit() { if (input('aa', '')) { $arr = Customer::where('1=1')->order('id desc')->limit(4)->column('id,protected_to'); foreach ($arr as $v) { $date = $v['protected_to'] ?: date('Y-m-d H:i:s'); $date = date('Y-m-d H:i:s', strtotime($date) - 86400); Customer::where('id', $v['id'])->update(['protected_to' => $date]); } } $param = request()->only(['a', 'a_switch', 'b', 'b_switch', 'c', 'c_switch', 'd', 'd_switch', 'e', 'e_switch', 'f', 'f_switch']); $root_id = request()->employee->root_id; $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'pubpool']])->findOrEmpty(); if ($setting->isEmpty()) { $data['name'] = 'pubpool'; $data['state'] = 1; //$param['off'] ?? 0; $data['root_id'] = $root_id; $content = [ 0 => ['day' => intval($param['a']), 'state' => $param['a_switch'] ?? 0, 'name' => '待确认'], 1 => ['day' => intval($param['f']), 'state' => $param['f_switch'] ?? 0, 'name' => '未到访'], 2 => ['day' => intval($param['b']), 'state' => $param['b_switch'] ?? 0, 'name' => '已到店'], 3 => ['day' => intval($param['c']), 'state' => $param['c_switch'] ?? 0, 'name' => '已到场'], 4 => ['day' => intval($param['d']), 'state' => $param['d_switch'] ?? 0, 'name' => '已量房'], 5 => ['day' => intval($param['e']), 'state' => $param['e_switch'] ?? 0, 'name' => '已交定'], ]; $data['content'] = json_encode($content); if (isset($param['a_switch']) && intval($param['a']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['b_switch']) && intval($param['b']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['c_switch']) && intval($param['c']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['d_switch']) && intval($param['d']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['e_switch']) && intval($param['e']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['f_switch']) && intval($param['f']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } $result = Setting::create($data); if ($result) { $orgIds = orgSubIds($setting['root_id']); // 待确认的客户修改保护期 if (isset($param['a_switch'])) { $a_where[] = ['state', 'in', Customer::changeState('待确认', 'chaos')]; $a_where[] = ['org_id', 'in', $orgIds]; $a_where[] = ['employee_id', '<>', 0]; Customer::where($a_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['a']) * 24 * 3600)]); } // 已到店的客户修改保护期 if (isset($param['b_switch'])) { $b_where[] = ['state', 'in', Customer::changeState('已到店', 'chaos')]; $b_where[] = ['org_id', 'in', $orgIds]; $b_where[] = ['employee_id', '<>', 0]; Customer::where($b_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['b']) * 24 * 3600)]); } // 已到场的客户修改保护期 if (isset($param['c_switch'])) { $c_where[] = ['state', 'in', Customer::changeState('已到场', 'chaos')]; $c_where[] = ['org_id', 'in', $orgIds]; $c_where[] = ['employee_id', '<>', 0]; Customer::where($c_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['c']) * 24 * 3600)]); } // 已量房的客户修改保护期 if (isset($param['d_switch'])) { $d_where[] = ['state', 'in', Customer::changeState('已量房', 'chaos')]; $d_where[] = ['org_id', 'in', $orgIds]; $d_where[] = ['employee_id', '<>', 0]; Customer::where($d_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['d']) * 24 * 3600)]); } // 已交定的客户修改保护期 if (isset($param['e_switch'])) { $e_where[] = ['state', 'in', Customer::changeState('已交定', 'chaos')]; $e_where[] = ['org_id', 'in', $orgIds]; $e_where[] = ['employee_id', '<>', 0]; Customer::where($e_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['e']) * 24 * 3600)]); } // 未到访的客户修改保护期 if (isset($param['f_switch'])) { $f_where[] = ['state', 'in', Customer::changeState('未到访', 'chaos')]; $f_where[] = ['org_id', 'in', $orgIds]; $f_where[] = ['employee_id', '<>', 0]; Customer::where($f_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['f']) * 24 * 3600)]); } return json(['code' => 0, 'msg' => '保存成功']); } else { return json(['code' => 1, 'msg' => '保存失败']); } } else { $old_content = json_decode($setting['content'], 'true'); if (isset($param['a_switch']) && intval($param['a']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['b_switch']) && intval($param['b']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['c_switch']) && intval($param['c']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['d_switch']) && intval($param['d']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['e_switch']) && intval($param['e']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } if (isset($param['f_switch']) && intval($param['f']) < 1) { return json(['code' => 1, 'msg' => '天数至少为一天']); } $content = [ 0 => ['day' => intval($param['a']), 'state' => $param['a_switch'] ?? 0, 'name' => '待确认'], 1 => ['day' => intval($param['f']), 'state' => $param['f_switch'] ?? 0, 'name' => '未到访'], 2 => ['day' => intval($param['b']), 'state' => $param['b_switch'] ?? 0, 'name' => '已到店'], 3 => ['day' => intval($param['c']), 'state' => $param['c_switch'] ?? 0, 'name' => '已到场'], 4 => ['day' => intval($param['d']), 'state' => $param['d_switch'] ?? 0, 'name' => '已量房'], 5 => ['day' => intval($param['e']), 'state' => $param['e_switch'] ?? 0, 'name' => '已交定'], ]; $setting->content = json_encode($content); $result = $setting->save(); if ($result) { $orgIds = orgSubIds($setting['root_id']); // 待确认的客户修改保护期 if (isset($param['a_switch'])) { $a_n = Customer::changeState('待确认', 'n'); if ($old_content[$a_n]['state'] == 0) { $a_where[] = ['state', 'in', Customer::changeState('待确认', 'chaos')]; $a_where[] = ['org_id', 'in', $orgIds]; $a_where[] = ['employee_id', '<>', 0]; Customer::where($a_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['a']) * 24 * 3600)]); } } // 已到店的客户修改保护期 if (isset($param['b_switch'])) { $b_n = Customer::changeState('已到店', 'n'); if ($old_content[$b_n]['state'] == 0) { $b_where[] = ['state', 'in', Customer::changeState('已到店', 'chaos')]; $b_where[] = ['org_id', 'in', $orgIds]; $b_where[] = ['employee_id', '<>', 0]; Customer::where($b_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['b']) * 24 * 3600)]); } } // 已到场的客户修改保护期 if (isset($param['c_switch'])) { $c_n = Customer::changeState('已到场', 'n'); if ($old_content[$c_n]['state'] == 0) { $c_where[] = ['state', 'in', Customer::changeState('已到场', 'chaos')]; $c_where[] = ['org_id', 'in', $orgIds]; $c_where[] = ['employee_id', '<>', 0]; Customer::where($c_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['c']) * 24 * 3600)]); } } // 已量房的客户修改保护期 if (isset($param['d_switch'])) { $d_n = Customer::changeState('已量房', 'n'); if ($old_content[$d_n]['state'] == 0) { $d_where[] = ['state', 'in', Customer::changeState('已量房', 'chaos')]; $d_where[] = ['org_id', 'in', $orgIds]; $d_where[] = ['employee_id', '<>', 0]; Customer::where($d_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['d']) * 24 * 3600)]); } } // 已交定的客户修改保护期 if (isset($param['e_switch'])) { $e_n = Customer::changeState('已交定', 'n'); if ($old_content[$e_n]['state'] == 0) { $e_where[] = ['state', 'in', Customer::changeState('已交定', 'chaos')]; $e_where[] = ['org_id', 'in', $orgIds]; $e_where[] = ['employee_id', '<>', 0]; Customer::where($e_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['e']) * 24 * 3600)]); } } // 已交定的客户修改保护期 if (isset($param['f_switch'])) { $f_n = Customer::changeState('已交定', 'n'); if ($old_content[$f_n]['state'] == 0) { $f_where[] = ['state', 'in', Customer::changeState('未到访', 'chaos')]; $f_where[] = ['org_id', 'in', $orgIds]; $f_where[] = ['employee_id', '<>', 0]; Customer::where($f_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['f']) * 24 * 3600)]); } } return json(['code' => 0, 'msg' => '保存成功']); } else { return json(['code' => 1, 'msg' => '保存失败']); } } } /** * 还原 */ public function undrop() { $id = input('id', '', 'trim'); if (!$id) { return json(['code' => 1, 'msg' => '参数错误']); } $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $orgSub)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds($root_id); } $list = CustomerDropPool::where([['id', 'in', $id], ['org_id', 'in', $orgids]])->select(); $root_id = request()->employee->root_id; $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'pubpool']])->findOrEmpty(); $error = 0; $repeat = 0; foreach ($list as $k => $v) { //查询重复 $l_phone = array_filter([$v->getData('phone'), $v->getData('phone1'), $v->getData('phone2')]); $query = []; $query[] = ['phone|phone1|phone2', 'in', $l_phone]; $query[] = ['employee_id', '=', $v->employee_id]; $check = Customer::where($query)->findOrEmpty(); if (!$check->isEmpty()) { $repeat += 1; continue; } $customer_state = Customer::changeState($v->state, 'n'); $info = $v->toArray(); $info['protected_to'] = null; if (!empty($setting)) { $setting_content = json_decode($setting['content'], true); if (!empty($setting_content[$customer_state]['state']) && $setting_content[$customer_state]['state'] != 0 && !empty($setting_content[$customer_state]['day'])) { $day = $setting_content[$customer_state]['day']; $info['protected_to'] = date('Y-m-d H:i:s', time() + $day * 24 * 3600); } } $info['deposit_money'] = $v->getData('deposit_money'); $info['signed_money'] = $v->getData('signed_money'); $info['id'] = $info['customer_id']; unset($info['customer_id']); unset($info['droptime']); unset($info['add_wechat_time']); unset($info['group_building_date']); unset($info['live_broadcast_date']); unset($info['subscribe_meet_outside']); unset($info['meet_outside']); unset($info['sign_time']); unset($info['reputation_gather']); unset($info['into_owner_group']); Db::startTrans(); $result = Customer::create($info); $d_result = $v->delete(); if ($result && $d_result) { Db::commit(); $param = [ 'customer_id' => $info['id'], 'type' => 1, 'remark' => '客户还原', 'employee_id' => request()->employee->id, 'user_id' => request()->employee->uid, 'state' => '' ]; CustomerVisitLog::create($param); } else { Db::rollback(); $error++; } } if ($error == 0) { return json(['code' => 0, 'msg' => '操作成功', 'repeat' => $repeat]); } else { return json(['code' => 0, 'msg' => '操作成功,部分数据失败', 'repeat' => $repeat]); } } /** * 重新指派 * @return string */ public function drop_pool_resdistribute() { $request = request(); $param = $request->param(); if (!request()->isAjax()) { View::assign('from', $param['from']); $targetstr = substr($param['data'], 0, strlen($param['data']) - 1); View::assign('targetstr', $targetstr); // 部门 $org = OrgLogic::struc(request()->employee->root_id); View::assign('org', $org); $orgids = orgSubIds(request()->employee->root_id); View::assign('orgids', json_encode($orgids)); View::assign('emporgids', json_encode($orgids)); View::assign('check', input('check', '')); return View::fetch(); } $target_ids_arr = explode(',', $param['targets']); if ($param['type'] == 'toemp' && !empty($param['emp_id'])) { $employee_id = $param['emp_id']; $emp_obj = Employee::find($employee_id); $org_id = $emp_obj->org_id; // 设计师能否报备、获取客户 $root_id = $request->employee->root_id; $de_where[] = ['root_id', '=', $root_id]; $de_where[] = ['name', '=', 'designer_get_customer']; $info_de_where = Setting::where($de_where)->findOrEmpty(); $org_type = Org::where('id', '=', $org_id)->value('org_type'); if (!$info_de_where->isEmpty() && $org_type == 2) { if ((int)$info_de_where['content'] != 1) { return json(['code' => 1, 'msg' => '无法分配给设计师']); } } } else { return json(['code' => 1, 'msg' => '操作失败']); } //2022-10-21 19:25:59----------------- //2022-10-21 重复录入 重复验证 check=1,2@3,4 表示ID=2和3的客户手机号重复 1,4不重复 ,1,2,4和业务员有重复则不让添加 $repeat_customer = []; $vislog_id = []; //跟进记录合并数组 if (!empty($param['check'])) { $target_ids_arr = []; $check_arr = explode(',', $param['check']); //先已第一个位置 以后可能会已保护期排序 foreach ($check_arr as $check_arr_key => $check_arr_val) { //默认第一个ID 为准 $temporary_arr = explode('@', $check_arr_val); $vislog_id[$temporary_arr[0]] = array_values($temporary_arr); } foreach ($vislog_id as $vislog_id_key => $vislog_id_val) { $temporary_customer = ($param['from'] == 'index') ? Customer::where('id', $vislog_id_key)->find() : CustomerDropPool::where('id', $vislog_id_key)->find(); $query = []; $query[] = ['employee_id', '=', $employee_id]; $l_phone = array_filter([$temporary_customer->getData('phone'), $temporary_customer->getData('phone1'), $temporary_customer->getData('phone2')]); $query[] = ['phone|phone1|phone2', 'in', $l_phone]; $check = Customer::where($query)->findOrEmpty(); if ($check->isEmpty()) { $target_ids_arr[] = $vislog_id_key; } else { $repeat_customer[] = $temporary_customer->name; //重复的客户 } } if ($repeat_customer) { return json(['code' => 1, 'msg' => '您选择的客户“' . implode('”,“', $repeat_customer) . '”手机号已经属于业务员' . $emp_obj->name . '请重新选择']); } if ($param['from'] != 'index') { $drop = $vislog_id; $vislog_ids = []; foreach ($vislog_id as $k => $v) { $ls_id = CustomerDropPool::where('id', $k)->value('customer_id'); $vislog_ids[$ls_id] = CustomerDropPool::where([['id', 'in', $v]])->column('customer_id'); } $vislog_id = $vislog_ids; } unset($param['check']); } //2022-10-21 19:25:59----------------- $root_id = request()->employee->root_id; $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'pubpool']])->findOrEmpty(); $error = 0; $repeat = []; //重复数量 //2022-10-17 重新分配逻辑修改,数据重复合并数据"1.重复客户分给同一个业务员时,提示分配人员(重复客户分配给一个业务员将合并客户信息,是否继续),点击继续 则合并客户(客户信息按最后置为回收站内的客户信息显示) // 2.分配到不同业务员时,客户不合并,各自显示各自的客户信息 // 3.分配的业务员名下已存在该客户手机号时,提示分配人员(该业务员已有改客户手机号)" $z_type = $param['from']; if (!empty($target_ids_arr)) { if ($param['from'] == 'index') { $list = Customer::where([['id', 'in', $target_ids_arr]])->select(); if (!$list->isEmpty()) { foreach ($list as $k => $v) { //如果该客户已经属于业务员则不需要分配 ,提示分配人 if ($v->employee_id == $employee_id) { $repeat[] = [$v->name, $v->getData('phone')]; continue; } $customer_state = Customer::changeState($v['state'], 'n'); $v_data['protected_to'] = NULL; if (!$setting->isEmpty()) { $setting_content = !empty($setting['content']) ? json_decode($setting['content'], true) : []; if (!empty($setting_content[$customer_state]['state']) && $setting_content[$customer_state]['state'] != 0 && !empty($setting_content[$customer_state]['day'])) { $day = $setting_content[$customer_state]['day']; $v_data['protected_to'] = date('Y-m-d H:i:s', time() + $day * 24 * 3600); } } //重复手机号验证 ,如果重复则合并客户信息和跟进记录 // $query = []; // $l_phone = array_filter([$v->getData('phone'),$v->getData('phone1'),$v->getData('phone2')]); // $query[] = ['phone|phone1|phone2','in',$l_phone]; // $query[] = ['employee_id','=',$employee_id]; // $check = Customer::where($query)->column('*'); // if ($check) { // $this->customerMerge($v->id,$check,'emp',$v_data['protected_to']);/// // continue; // } $v_data['employee_id'] = $employee_id; $v_data['org_id'] = $org_id; $v_data['designer_id'] = NULL; $v_data['ext->ext6'] = ''; $v_data['revisit_time'] = NULL; $result = Customer::where('id', $v->id)->update($v_data); if ($result === false) { $error++; } else { $param = [ 'customer_id' => $v->id, 'type' => 1, 'remark' => '客户再分配', 'employee_id' => request()->employee->id, 'user_id' => request()->employee->uid, 'state' => '' ]; CustomerVisitLog::create($param); } } } } elseif ($param['from'] == 'drop') { $list = CustomerDropPool::where([['id', 'in', $target_ids_arr]])->select(); if (!$list->isEmpty()) { foreach ($list as $k => $v) { $customer_state = Customer::changeState($v['state'], 'n'); $info = $v->toArray(); $info['protected_to'] = NULL; if (!$setting->isEmpty()) { $setting_content = !empty($setting['content']) ? json_decode($setting['content'], true) : []; if (!empty($setting_content[$customer_state]['state']) && $setting_content[$customer_state]['state'] != 0 && !empty($setting_content[$customer_state]['day'])) { $day = $setting_content[$customer_state]['day']; $info['protected_to'] = date('Y-m-d H:i:s', time() + $day * 24 * 3600); } } // //重复手机号验证 ,如果重复则合并客户信息和跟进记录 // $query = []; // $l_phone = array_filter([$v->getData('phone'),$v->getData('phone1'),$v->getData('phone2')]); // $query[] = ['phone|phone1|phone2','in',$l_phone]; // $query[] = ['employee_id','=',$employee_id]; // $check = Customer::where($query)->column('*'); // if ($check) { // $this->customerMerge($v->id,$check,'drop',$info['protected_to']); // continue; // } $info['employee_id'] = $employee_id; $info['org_id'] = $org_id; $info['id'] = $info['customer_id']; $info['signed_money'] = $v->getData('signed_money'); $info['deposit_money'] = $v->getData('deposit_money'); $info['designer_id'] = NULL; $info['revisit_time'] = NULL; unset($info['customer_id']); unset($info['droptime']); Db::startTrans(); $result = Customer::create($info); $d_result = $v->delete(); if (!empty($drop[$v->id])) CustomerDropPool::where([['id', 'in', $drop[$v->id]]])->delete(); if ($result && $d_result) { $result['ext->ext6'] = ''; $result->save(); Db::commit(); $param = [ 'customer_id' => $info['id'], 'type' => 1, 'remark' => '客户再分配', 'employee_id' => request()->employee->id, 'user_id' => request()->employee->uid, 'state' => '' ]; CustomerVisitLog::create($param); } else { Db::rollback(); $error++; } } } } } $repeat = count($repeat); if ($error == 0) { //2022-10-21 19:25:59----------------- // $this->customerMerge($vislog_id, $z_type); //2022-10-21 19:25:59----------------- return json(['code' => 0, 'msg' => '操作成功', 'repeat' => $repeat]); } else { return json(['code' => 0, 'msg' => '操作成功,部分数据失败', 'repeat' => $repeat]); } } //资源库迭代新增接口 /** * */ public function distribution() { $id = input('id', 0); //资源id if (!$this->checkResourceIsMy($id)) return; $count = input('count', 0); //可分配资源数量 View::assign('id', $id); View::assign('count', $count); $customer_id = input('customer_id', ''); View::assign('customer_id', $customer_id); return View::fetch(); } /** * 获取部门树 * 分配弹框页面接口 */ public function get_orgs() { $root_id = request()->employee->root_id; $keyword = input('keyword', ''); $where = [ ['path', 'like', $root_id . '-%'], ['status', '=', 1] ]; $count = Employee::where([['root_id', '=', $root_id], ['state', 'like', '%在职%'], ['uid', '>', 0]])->group('org_id')->column('count(*) count', 'org_id'); $allnodes = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select()->toArray(); foreach ($allnodes as $k => $v) { $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0; //本部门人数 $allnodes[$k]['all_count'] = 0; //包含子部门总数 $allnodes[$k]['org_class'] = 'org_' . $v['id']; $allnodes[$k]['per_class'] = 'per_org_' . $v['id']; $allnodes[$k]['type'] = 'org'; foreach ($allnodes as $k2 => $v2) { if (strpos($v2['path'], $v['path']) !== false) { $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']] + $allnodes[$k]['all_count'] : $allnodes[$k]['all_count']; } } $allnodes[$k]['title'] = $allnodes[$k]['title'] . ' (' . $allnodes[$k]['all_count'] . ')'; } $tree = $this->tree($allnodes, 0); return json(['code' => 0, 'data' => $tree, 'checkOrg' => '']); } public function tree($data, $pid = 0) { $new_arr = []; foreach ($data as $k => $v) { if ($v['pid'] == $pid) { $children = $this->tree($data, $v['id']); $v['children'] = $children; if (empty($v['children'])) $v['disabled'] = true; $new_arr[] = $v; } } return $new_arr; } /** * 获取公司人员 * 分配弹框页面接口 */ public function get_persons() { $request = request(); $root_id = $request->employee->root_id; $param = $request->only(['page' => 1, 'limit' => 10000, 'keyword' => '', 'type' => 1, 'ids' => '']); $where = [ ['root_id', '=', $root_id], ['uid', '>', 0], ['state', '=', '在职'] ]; if ($param['type'] == 1 && $param['keyword']) { $org_ids = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['name', 'like', '%' . $param['keyword'] . '%']])->column('id'); $where[] = ['org_id', 'in', $org_ids]; } elseif ($param['type'] == 2 && $param['keyword']) { $where[] = ['name', 'like', '%' . $param['keyword'] . '%']; } $list = Employee::with(['org' => function ($query) { $query->field(['id', 'name'])->bind(['org_name' => 'name']); }])->where($where)->field('id,org_id,name')->page($param['page'], $param['limit'])->order('id asc')->select(); $count = Employee::where($where)->count(); if ($param['ids']) { $arr = explode(',', $param['ids']); foreach ($list as $k => $v) { $list[$k]['checked'] = in_array($v['id'], $arr); } } else { foreach ($list as $k => $v) { $list[$k]['checked'] = false; } } return json(['code' => 0, 'data' => $list, 'count' => $count]); } /** * 获取公司部门 * 分配弹框页面接口 */ public function get_org() { $request = request(); $root_id = $request->employee->root_id; $where = [ ['path', 'like', $root_id . '-%'], ['status', '=', 1] ]; $list = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select(); return json(['code' => 0, 'data' => $list]); } /** * 分配资源 * 分配弹框页面接口 * type=org分配给部门,person分配给员工, * resources_id批量分配时资源id ,必传 * content 被分配部门id/员工id和数量 [{con_id:1,count:2},{con_id:89,count:4}] * customer_id 手动分配时选择的客户id */ public function distribution_resources() { $request = request(); $root_id = $request->employee->root_id; $param = $request->only(['type' => 'org', 'resources_id' => 0, 'content' => [], 'customer_id' => '']); if (!$this->checkResourceIsMy($param['resources_id'])) return; if (empty($param['content']) || !is_array($param['content'])) return json(['code' => 1, 'data' => '请选择分配对象', 'msg' => '请选择分配对象']); $state = Customer::changeState('待确认', 'chaos'); $customer = $param['customer_id'] ? explode(',', $param['customer_id']) : Customer::where([['crm_res_id', '=', $param['resources_id']], ['employee_id', 'null', null], ['bad_phone', '=', 0], ['is_resource', '=', 1],['state','in',$state]])->column('id'); shuffle($customer); //随机分配 $count = count($customer); $all_count = array_sum(array_column($param['content'], 'count')); if ($all_count > $count) return json(['code' => 1, 'data' => '分配数量不能大于资源总数', 'msg' => '分配数量不能大于资源总数']); // 查询老记录 $metaLog = CrmImportLog::find($param['resources_id']); $repeat = []; //指派类型 if ($param['type'] == 'org') { foreach ($param['content'] as $k => $v) { $con = array_slice($customer, 0, $v['count']); $param['content'][$k]['customer'] = $con; $customer = array_diff($customer, $con); } //指派到部门 foreach ($param['content'] as $key => $val) { if ($val['count'] > 0) { $log = CrmImportLog::create([ 'pid' => $param['resources_id'], 'employee_id' => null, 'org_id' => $val['con_id'], 'name' => $metaLog->name, 'count' => $val['count'], 'state' => 0, 'left_num' => $val['count'], 'source' => $metaLog->source ]); $where[] = ['id', 'in', $val['customer']]; Customer::where($where)->update([ 'employee_id' => NULL, 'org_id' => $val['con_id'], 'is_resource' => 1, 'crm_res_id' => $log->id, 'is_distribution' => 1, 'employee_time'=>date('Y-m-d H:i:s') ]); unset($where); } } } elseif ($param['type'] == 'person') { //分配到人去重 $eids = array_column($param['content'], 'con_id'); //所有人 // 设计师能否报备、获取客户 $de_where[] = ['root_id', '=', $root_id]; $de_where[] = ['name', '=', 'designer_get_customer']; $info_de_where = Setting::where($de_where)->findOrEmpty(); $employee_org_ids = Employee::where('id', 'in', $eids)->column('org_id'); $org_type = Org::where('id', 'in', $employee_org_ids)->column('org_type'); if (!$info_de_where->isEmpty() && in_array(2, $org_type)) { if ((int)$info_de_where['content'] != 1) { return json(['code' => 1, 'msg' => '无法分配给设计师']); } } $phones = Customer::where([['employee_id', 'in', $eids]])->column('employee_id,name,phone,phone1,phone2'); //所有人的手机号 $phone_emp = []; //待分配的业务员 foreach ($phones as $k => $v) { $phone_emp[$v['employee_id']] = isset($phone_emp[$v['employee_id']]) ? $phone_emp[$v['employee_id']] : []; array_push($phone_emp[$v['employee_id']], $v['phone'], $v['phone1'], $v['phone2']); } //待分配的业务员 已有的手机号 foreach ($phone_emp as $k2 => $v2) { $phone_emp[$k2] = array_unique(array_filter($v2)); } foreach ($param['content'] as $k3 => $v3) { //已有手机号 $param['content'][$k3]['phones'] = isset($phone_emp[$v3['con_id']]) ? $phone_emp[$v3['con_id']] : []; //待分配的客户 $param['content'][$k3]['cids'] = []; } //分配 $customer = Customer::where([['id', 'in', $customer], ['employee_id', '=', null]])->column('id,name,phone,phone1,phone2'); foreach ($customer as $k4 => $v4) { $ls_phones = array_filter([$v4['phone'], $v4['phone1'], $v4['phone2']]); $i = 0; foreach ($param['content'] as $k5 => $v5) { //没有分配完成 if ($v5['count'] > count($v5['cids'])) { //并且手机号不重复 if ($v5['phones'] == []) { $param['content'][$k5]['cids'][] = $v4['id']; $param['content'][$k5]['phones'] = array_merge($v5['phones'], $ls_phones); $i = 1; break; } else { $phone_diff = array_intersect($v5['phones'], $ls_phones); if ($phone_diff) { continue; //手机号重复 分配给下一个客户 } else { $param['content'][$k5]['cids'][] = $v4['id']; $param['content'][$k5]['phones'] = array_merge($v5['phones'], $ls_phones); $i = 1; break; } } } } //无法分配 if ($i == 0) { $repeat[$v4['id']] = $v4['name']; } } //分配 $visitlog = []; $date = date('Y-m-d'); foreach ($param['content'] as $k6 => $v6) { if ($v6['cids']) { $emp = Employee::where('id', $v6['con_id'])->findOrEmpty(); Customer::where([['id', 'in', $v6['cids']]])->update([ 'employee_id' => $v6['con_id'], 'org_id' => $emp->org_id, 'is_resource' => 0, 'is_distribution' => 1, 'employee_time' => date('Y-m-d H:i:s'), 'remark' => '', 'valid_time'=>null, 'state'=>0 ]); CustomerVisitLog::where([['customer_id','in',$v6['cids']],['data_type','=','out_call']])->delete(); OutCallLog::where([['customer_id','in',$v6['cids']]])->update(['is_redistribution'=>1]); OutCallMbLog::where([['customer_id','in',$v6['cids']]])->update(['is_redistribution'=>1]); //添加跟进记录 foreach ($v6['cids'] as $v7) { $visitlog[] = [ 'customer_id' => $v7, 'type' => 1, 'remark' => '资源库分配', 'employee_id' => $v6['con_id'], 'user_id' => $emp->uid, 'state' => '待确认', 'next_contact_date' => $date ]; } } } if ($visitlog) CustomerVisitLog::insertAll($visitlog); //2023-02-28分配之后记录分配记录 $log = []; $time = time(); foreach ($param['content'] as $key8 => $val8) { if (!empty($val8['cids'])) { $log[] = [ 'employee_id'=>$val8['con_id'], 'root_id' => $root_id, 'crm_res_id' => $param['resources_id'], 'type' => 0, 'source_type' => 0, 'count'=>count($val8['cids']), 'customer_ids' => implode(',',$val8['cids']), 'operator_employee_id' => $request->employee->id, 'addtime' => $time ]; } } if($log) CrmImportRetrieveLog::insertAll($log); } else { return json(['code' => 1, 'data' => '数据错误', 'msg' => '数据错误']); } //查询未分配数量 $left_num = Customer::where([['crm_res_id', '=', $param['resources_id']], ['employee_id', '=', null],['state','in',$state]])->count(); CrmImportLog::where('id', $param['resources_id'])->update(['state' => 1, 'left_num' => $left_num]); $j = 0; if ($param['type'] == 'person') { foreach ($param['content'] as $v7) { if ($v7['count'] > count($v7['cids'])) $j = 1; } } if ($repeat && $j == 1) { $name = implode(',', array_values($repeat)); return json(['code' => 1, 'data' => '客户:' . $name . '。手机号重复无法分配', 'msg' => '客户:' . $name . '。手机号重复无法分配']); } else { return json(['code' => 0, 'data' => '分配完成', 'msg' => '分配完成']); } } /** * 详情页获取基本信息 * 详情页 */ public function read() { $request = request(); $root_id = $request->employee->root_id; $param = $request->only(['id' => 0]); $data = CrmImportLog::with(['employee', 'source' => function ($query) { $query->field('id,source')->bind(['source_name' => 'source']); }])->where('id', $param['id'])->findOrEmpty(); $data->state = $data->state == 0 ? '未分配' : '已分配'; //已分配数量 //$rids = CrmImportLog::where('pid|id', $param['id'])->column('id'); $listid = CrmImportLog::where('id', '>', 0)->field('id,pid')->select()->toArray(); $rids = $this->getAllLowerLevelId($listid, $param['id'], $is_first_time = true); $rids[] = $param['id']; $data->y_count = Customer::where([['crm_res_id', 'in', $rids], ['is_distribution', '=', 1]])->count(); return $data; } /** * 已分配页面获取客户跟进数据 * 详情页 */ public function get_customer_data() { $request = request(); $root_id = $request->employee->root_id; $param = $request->only(['id' => 0]); $orgSub = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $orgSub)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds($root_id); } //分配记录 $ids = CrmImportLog::where([['pid|id', '=', $param['id']], ['org_id', 'in', $orgids]])->column('id'); //已分配的数量包括,已经分配 和 曾经分配过当前处于无效状态的客户 $customer_id1 = Customer::where([['crm_res_id', 'in', $ids], ['employee_id', '>', 0], ['is_distribution', '=', 1]])->column('id'); $customer_id2 = Customer::where([['crm_res_id', 'in', $ids], ['employee_id', 'null', null], ['is_distribution', '=', 1]])->column('id'); $customer_id3 = CustomerVisitLog::where([['customer_id', 'in', $customer_id2]])->group('customer_id')->column('customer_id'); $customer_id = array_merge($customer_id1, $customer_id3); //有效状态 未到访,已到访,确定到场,已量房,交定,签单 // $state1 = CustomerVisitLog::changeState('待确认', 'chaos'); $state2 = CustomerVisitLog::changeState('未到访', 'chaos'); $state3 = CustomerVisitLog::changeState('已到访', 'chaos'); $state4 = CustomerVisitLog::changeState('已到场', 'chaos'); $state5 = CustomerVisitLog::changeState('已量房', 'chaos'); $state6 = CustomerVisitLog::changeState('已交定', 'chaos'); $state7 = CustomerVisitLog::changeState('已签单', 'chaos'); $state = array_merge($state2, $state3, $state4, $state5, $state6, $state7); //有效客户 排除待回访,和无效 //2023-02-02 逻辑修改 新分配的客户为待确认状态,确认标记有效后到我的客户,状态为有效 $query1 = ['id', 'in', $customer_id]; $query2 = ['is_distribution', '=', 1]; $query3 = ['is_distribution', '=', 0]; $query4 = ['died', '<>', 2]; //排除死单 $query5 = ['customer_id', 'in', $customer_id]; $query6 = ['state', 'in', Customer::changeState('待确认', 'chaos')]; $query7 = ['state', 'not in', Customer::changeState('待确认', 'chaos')]; $query8 = ['state', 'not in', Customer::changeState('无效', 'chaos')]; $query9 = ['state', 'in', Customer::changeState('无效', 'chaos')]; //有效 $data['valid'] = Customer::where([$query1, $query2, $query7, $query8])->count(); // $data['valid'] = CustomerVisitLog::where([ // ['customer_id', 'in', $customer_id], // ['state', 'in', $state] // ])->group('customer_id')->count(); //无效 $data['invalid'] = Customer::where([$query1, $query2, $query9])->count(); //待确认 $data['confirmed'] = Customer::where([$query1, $query2, $query6])->count(); // $state8 = CustomerVisitLog::changeState('预约到店', 'chaos'); // $state9 = CustomerVisitLog::changeState('预约量房', 'chaos'); // $state10 = CustomerVisitLog::changeState('预约活动', 'chaos'); // $state11 = CustomerVisitLog::changeState('无效', 'chaos'); // $state12 = CustomerVisitLog::changeState('未到店', 'chaos'); // $state13 = CustomerVisitLog::changeState('未量房', 'chaos'); // $state14 = CustomerVisitLog::changeState('已卖卡', 'chaos'); // $state15 = CustomerVisitLog::changeState('未到场', 'chaos'); // $l_ids = CustomerVisitLog::where([ // ['customer_id', 'in', $customer_id], // ['state', 'in', array_merge($state15, $state2, $state3, $state4, $state5, $state6, $state7, $state9, $state8, $state10, $state11, $state12, $state13, $state14)] // ])->group('customer_id')->column('customer_id'); // $l_ids = array_diff($customer_id, $l_ids); // $data['confirmed'] = CustomerVisitLog::where([ // ['customer_id', 'in', $l_ids] // ])->group('customer_id')->count(); //已到店,,曾经已到店 $data['arrived_at_the_store'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')] ])->group('customer_id')->count(); //已量房 $data['measured_room'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')] ])->group('customer_id')->count(); //已到场 $data['already_present'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')] ])->group('customer_id')->count(); //已交定 $data['settled'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')] ])->group('customer_id')->count(); //已签单 $data['sign_the_bill'] = CustomerVisitLog::where([ $query5, ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')] ])->group('customer_id')->count(); //2023-02-02 已联系包括’分配资源‘中的客户已经调用外呼或者标记时remark有值的客户 //2023-04-21 点击拨打按钮就算已联系 $data['visit'] = OutCallMbLog::where([$query5])->group('customer_id')->count(); //未联系 $data['unvisit'] = count($customer_id) - $data['visit']; //电话未联系和未接通 $call_list = OutCallLog::where([['customer_id', 'in', $customer_id]])->order('addtime desc')->field('id,status,customer_id')->select()->toArray(); $call_ids = []; $call_status = []; $no_connect = 0; foreach ($call_list as $key => $val) { if (!in_array($val['customer_id'], $call_ids)) { $call_ids[] = $val['customer_id']; $call_status[$val['customer_id']] = $val['status']; } } foreach ($call_status as $key => $val) { if (in_array($val, [-1, -2])) $no_connect++; } $yfp = Customer::where([['crm_res_id', 'in', $ids], ['is_distribution', '=', 1]])->count(); $data['no_connect'] = $no_connect; $data['no_call'] = $yfp - count($call_ids); return $data; } /** * 已分配客户筛选多种状态处理 */ public function cus_arrstatus($state, $where) { $arr_state = explode(',', $state); $allids = []; $newids = []; $cids = Customer::where($where)->column('id'); foreach ($arr_state as $key => $val) { $arr = ['已到店', '已量房', '已到场', '已交定', '已签单']; $v_where[] = ['customer_id', 'in', $cids]; if (in_array($val, $arr)) { $v_where[] = ['state', 'in', CustomerVisitLog::changeState($val, 'chaos')]; $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id'); unset($v_where); $allids[] = $ids; } elseif ($val == '待确认') { //排除有效就是待确认 $ids = Customer::where($where)->where('state', 'in', Customer::changeState('待确认', 'chaos'))->column('id'); $allids[] = $ids; } elseif ($val == '有效') { //2023-02-02修改 有效状态 客户表state字段不为待确认和无效,就是有效 $state1 = Customer::changeState('待确认', 'chaos'); $state2 = Customer::changeState('无效', 'chaos'); $allids[] = Customer::where($where)->where('state', 'not in', array_merge($state1,$state2))->column('id'); }elseif($val == '无效'){ $state = Customer::changeState('无效', 'chaos'); $allids[] = Customer::where($where)->where('state', 'in', $state)->column('id'); } } foreach ($allids as $key => $val) { foreach ($val as $k => $v) { $newids[] = $v; } } $newids = array_unique($newids); return $newids; } /** * 查询所有资源的下级 */ function getAllLowerLevelId($list, $p_id = '0', $isFirstTime = true) { static $arr = []; if ($isFirstTime) { $arr = []; } foreach ($list as $key => $val) { if ($val['pid'] == $p_id) { $arr[] = $val['id']; $this->getAllLowerLevelId($list, $val['id'], false); } } return $arr; } /** * 已分配客户列表 * 分配弹框页面接口 */ public function get_customers() { $request = request(); $root_id = $request->employee->root_id; $param = $request->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'type' => 1, 'id' => 0, 'keyword3' => '', 'state' => '', 'recovery' => 0, 'ids' => '', 'call_state']); if (!$this->checkResourceIsMy($param['id'])) return; //分配记录 //$ids = CrmImportLog::where('pid|id', $param['id'])->column('id'); $orgSub = orgSubIds($root_id); if (!empty($param['org']) && in_array($param['org'], $orgSub)) { $orgids = orgSubIds($param['org']); } else { $orgids = orgSubIds($root_id); } $listid = CrmImportLog::where([['id','>',0], ['org_id', 'in', $orgids]])->field('id,pid')->select()->toArray(); $ids = $this->getAllLowerLevelId($listid, $param['id'], $is_first_time = true); $ids[] = $param['id']; //已分配的数量包括,已经分配 和 曾经分配过当前处于无效状态的客户 和 包括分配到部门资源库的 $customer_id = Customer::where([['crm_res_id', 'in', $ids], ['is_distribution', '=', 1]])->column('id'); //分配到人 // $customer_id2 = Customer::where([['crm_res_id', 'in', $ids]])->column('id'); // $customer_id3 = CustomerVisitLog::where([['customer_id', 'in', $customer_id2]])->group('customer_id')->column('customer_id'); // $customer_id4 = Customer::where([['crm_res_id', 'in', array_diff($ids, [$param['id']])], ['employee_id', '=', null]])->column('id'); // $customer_id = array_unique(array_merge($customer_id1, $customer_id3, $customer_id4)); $where[] = ['id', 'in', $customer_id]; if ($param['type'] == 1 && $param['keyword']) { $org_ids = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['name', 'like', '%' . $param['keyword'] . '%']])->column('id'); $where[] = ['org_id', 'in', $org_ids]; } elseif ($param['type'] == 2 && $param['keyword']) { $emp_ids = Employee::where([['name', 'like', '%' . $param['keyword'] . '%'], ['uid', '>', 0], ['root_id', '=', request()->employee->root_id], ['state', '=', '在职']])->column('id'); // $where[] = ['name', 'like', '%' . $param['keyword'] . '%']; $where[] = ['employee_id', 'in', $emp_ids]; } //小区,手机号搜索 if ($param['keyword3']) { $ids = []; $keyword = Customer::where($where)->field('id,community_name,phone,phone1,phone2')->select(); foreach ($keyword as $key => $val) { $str = $val['community_name'] . $val['phone'] . $val['phone1'] . $val['phone2']; if (strpos($str, $param['keyword3']) !== false) $ids[] = $val['id']; } $where[] = ['id', 'in', $ids]; } //状态搜索 if ($param['state']) { $state_ids = $this->cus_arrstatus($param['state'], $where); unset($where); $where[] = ['id', 'in', $state_ids]; // $arr = ['已到店','已量房','已到场','已交定','已签单']; // $cids = Customer::where($where)->column('id'); // $v_where[] = ['customer_id','in',$cids]; // if (in_array($param['state'],$arr)) { // $v_where[] = ['state','in',CustomerVisitLog::changeState($param['state'], 'chaos')]; // $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id'); // unset($where); // $where[] = ['id','in',$ids]; // } elseif ($param['state']=='待确认') { // //排除有效就是待确认 // $cids = Customer::where($where)->column('id'); // $state2 = CustomerVisitLog::changeState('未到访', 'chaos'); // $state3 = CustomerVisitLog::changeState('已到访', 'chaos'); // $state4 = CustomerVisitLog::changeState('已到场', 'chaos'); // $state5 = CustomerVisitLog::changeState('已量房', 'chaos'); // $state6 = CustomerVisitLog::changeState('已交定', 'chaos'); // $state7 = CustomerVisitLog::changeState('已签单', 'chaos'); // $state = array_merge($state2, $state3, $state4, $state5, $state6, $state7); // $v_where[] = ['customer_id','in',$cids]; // $v_where[] = ['state','in',$state]; // $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id'); // $where[] = ['id','not in',$ids]; // } elseif ($param['state']=='有效'){ // $cids = Customer::where($where)->column('id'); // $state2 = CustomerVisitLog::changeState('未到访', 'chaos'); // $state3 = CustomerVisitLog::changeState('已到访', 'chaos'); // $state4 = CustomerVisitLog::changeState('已到场', 'chaos'); // $state5 = CustomerVisitLog::changeState('已量房', 'chaos'); // $state6 = CustomerVisitLog::changeState('已交定', 'chaos'); // $state7 = CustomerVisitLog::changeState('已签单', 'chaos'); // $state = array_merge($state2, $state3, $state4, $state5, $state6, $state7); // $v_where[] = ['customer_id','in',$cids]; // $v_where[] = ['state','in',$state]; // $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id'); // unset($where); // $where[] = ['id','in',$ids]; // } } //打电话状态筛选 if (!empty($param['call_state'])) { //电话未联系和未接通 $call_list = OutCallLog::where([['customer_id', 'in', $customer_id]])->order('addtime desc')->field('id,status,customer_id')->select()->toArray(); $call_ids = []; $call_status = []; $no_connect_ids = []; $call_all = []; foreach ($call_list as $key => $val) { if (!in_array($val['customer_id'], $call_ids)) { $call_ids[] = $val['customer_id']; $call_status[$val['customer_id']] = $val['status']; } $call_all[$val['customer_id']][] = $val; } foreach ($call_status as $key => $val) { if (in_array($val, [-1, -2])) $no_connect_ids[] = $key; } $no_call_ids = array_diff($customer_id, $call_ids) ? array_diff($customer_id, $call_ids) : []; unset($where); if ($param['call_state'] == '已联系') { //2023-02-02 已联系包括打电话和remark不为空的客户 $id1 = array_column($call_list, 'customer_id'); $id2 = Customer::whereOr([[['id', 'in', $customer_id], ['remark', '<>', '']], [['id', 'in', $id1]]])->column('id'); $where[] = ['id', 'in', $id2]; // if (!empty($state_ids) && !empty($call_ids)) { // $call_ids = array_intersect($state_ids, $call_ids); // } // $where[] = ['id', 'in', $call_ids]; } if ($param['call_state'] == '未联系') { //2023-02-02 未打电话并且remark为空 $id1 = array_column($call_list, 'customer_id'); $id2 = Customer::whereOr([[['id', 'in', $customer_id], ['remark', '<>', '']], [['id', 'in', $id1]]])->column('id'); $where[] = ['id', 'not in', $id2]; // if (!empty($state_ids) && !empty($no_call_ids)) { // $no_call_ids = array_intersect($state_ids, $no_call_ids); // } $where[] = ['id', 'in', $no_call_ids]; } if ($param['call_state'] == '未接通') { if (!empty($state_ids) && !empty($no_connect_ids)) { $no_connect_ids = array_intersect($state_ids, $no_connect_ids); } $where[] = ['id', 'in', $no_connect_ids]; } } //回收客户 if ($param['recovery'] == 1) { if (empty($param['ids'])) return json(['code' => 1, 'data' => '请选择要回收的客户', 'msg' => '请选择要回收的客户']); $where = [['id', 'in', explode(',', $param['ids'])]]; $list = Customer::where($where)->field('id,name,employee_id,org_id,designer_id,crm_res_id,state')->select()->toArray(); //---start 2023-01-04--------//已经交定签单不能回收 // $state_ids = array_column($list, 'id'); // $state11 = CustomerVisitLog::changeState('已交定', 'chaos'); // $state12 = CustomerVisitLog::changeState('已签单', 'chaos'); // $log_where = [ // ['customer_id', 'in', $state_ids], // ['state', 'in', array_merge($state11, $state12)] // ]; // $repeat_ids = CustomerVisitLog::where($log_where)->group('customer_id')->column('customer_id'); // foreach ($list as $item_key => $item_val) { // if (in_array($item_val['id'], $repeat_ids)) { // unset($list[$item_key]); // $repeat_name[] = $item_val['name']; // } // } $repeat_name = []; foreach ($list as $item_key => $item_val) { if (!in_array($item_val['state'], ['待确认', 0], true)) { unset($list[$item_key]); $repeat_name[] = $item_val['name']; } } //---end 2023-01-04--------//已经交定签单不能回收 $info = CrmImportLog::where('id', $param['id'])->find(); $listids = CrmImportLog::where('id','>',0)->column('pid,id'); $cids = array_column($list,'id'); $recory = Customer::where([['id','in',$cids],['employee_id','not null',null]])->column('employee_id,id'); Db::startTrans(); try { foreach($list as $k=>$v){ $this->recovery_crm($v,$param['id'],$listids); } Db::commit(); }catch (\Exception $e) { Db::rollback(); //var_dump($e->getMessage()); return json(['code' => 1, 'msg' => '回收数据失败.']); } //保存回收记录 只保存分配到人的客户记录 $recorys = []; foreach ($recory as $key => $val) { $recorys[$val['employee_id']][] = $val['id']; } $saves = []; $time = time(); foreach ($recorys as $key1 => $val1) { $saves[] = [ 'root_id' => request()->employee->root_id, 'employee_id' => $key1, 'crm_res_id' => $param['id'], 'type' => 1, 'source_type' => 0, 'count' => count($val1), 'customer_ids' => implode(',',$val1), 'addtime' => $time, 'operator_employee_id' => $request->employee->id ]; } if($saves) CrmImportRetrieveLog::insertAll($saves); if ($repeat_name) { $name = implode(',', $repeat_name); return json(['code' => 1, 'data' => '客户“' . $name . '”已经标记为有效无法回收', 'msg' => '客户“' . $name . '”已经标记为有效无法回收']); } else { return json(['code' => 0, 'data' => '回收完成', 'msg' => '回收完成']); } } $list = Customer::with(['orgs' => function ($query) { $query->field(['info', 'id', 'name'])->bind(['org_name' => 'name']); }, 'designer' => function ($query) { $query->field(['id', 'name'])->bind(['designer_name' => 'name']); }, 'employee' => function ($query) { $query->field(['id', 'name'])->bind(['sale_name' => 'name']); }])->withCount(['outcall'])->where($where)->page($param['page'], $param['limit'])->field('id,employee_id,name,community_name,org_id,designer_id,crm_res_id,state,employee_time')->order('employee_id asc,id asc')->select(); $recovery = Customer::where($where)->column('id'); $count = count($recovery); $recovery = implode(',',$recovery); //分配时间 $times_arr = CrmImportLog::where([['id', 'in', array_column($list->toArray(), 'crm_res_id')]])->column('addtime', 'id'); foreach ($list as $key => $val) { $list[$key]['allocate_time'] = $times_arr[$val['crm_res_id']]; } // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($list->toArray(), 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray(); $customersState = []; foreach ($customersStateList as $s) { if (!isset($customersState[$s['customer_id']])) $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0, 'ydf' => 0, 'wdf' => 0]; $customersState[$s['customer_id']]['count'] += $s['num']; // 到店,量房,活动,定金,签单 if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num']; elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num']; elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num']; elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num']; elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num']; elseif ($s['state'] == '已到访') $customersState[$s['customer_id']]['ydf'] += $s['num']; elseif ($s['state'] == '未到访') $customersState[$s['customer_id']]['wdf'] += $s['num']; } $list = $list->toArray(); $state1 = Customer::changeState('待确认', 'chaos'); $state2 = Customer::changeState('无效', 'chaos'); foreach ($list as $key => &$value) { $value['stateNum'] = $customersState[$value['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0, 'ydf' => 0, 'wdf' => 0]; // if(!empty($call_all) && !empty($call_all[$value['id']])){ // $value['call_num'] = count($call_all[$value['id']]); // }else{ // $value['call_num'] = 0; // } //2023-02-02 修改逻辑 分配后为待确认状态,不为待确认和无效就是有效状态 $value['state'] = in_array($value['state'], $state1, true) ? '待确认' : (in_array($value['state'], $state2) ? '无效' : '有效'); } return json(['code' => 0, 'data' => $list, 'count' => $count,'recovery'=>$recovery]); } /** * 循环调用回收客户 */ public function recovery_crm($crm, $crm_res_id, $listids) { $crm_log = CrmImportLog::where('id', $crm['crm_res_id'])->find(); if ($crm_res_id != $crm_log['id']) { $upids = $this->getInfo($crm_log['id'], $listids, $crm_res_id); foreach ($upids as $k => $v) { $res_data = CrmImportLog::find($v['id']); $count = $res_data->count - 1; $left_num = $res_data->left_num - 1; $res_data->count = $count ? $count : 0; if ($crm_log->id == $res_data->id && empty($crm['employee_id'])) $res_data->left_num = $left_num ? $left_num : 0; $res_data->save(); } } $ben_log = CrmImportLog::where('id', $crm_res_id)->find(); $ben_left_num = $ben_log->left_num + 1; $ben_log->left_num = $ben_left_num ?: 0; $ben_log->save(); $is_distribution = $ben_log->pid ? 1 : 0; Customer::where([['id', '=', $crm['id']]])->update(['crm_res_id' => $ben_log->id, 'org_id' => $ben_log->org_id, 'employee_id' => null, 'employee_time' => null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => $is_distribution, 'state' => 0]); } /** * 电话联系次数详情 */ public function call_cuslist() { $request = request(); $param = $request->only(['customer_id' => 0, 'page' => 1, 'limit' => 10]); if (!request()->isAjax()) { View::assign('customer_id', $param['customer_id']); return View::fetch(); } $info = Customer::where('id', $param['customer_id'])->find(); if (empty($info)) return json(['code' => 1, 'msg' => '客户不存在']); $list = OutCallLog::where('customer_id', $param['customer_id'])->field('id,fwd_start_time,status')->page($param['page'], $param['limit'])->select()->toArray(); foreach ($list as $key => $val) { $list[$key]['status_name'] = '无'; if ($val['status'] == 1) $list[$key]['status_name'] = '接通'; if ($val['status'] == -1) $list[$key]['status_name'] = '未接通'; if ($val['status'] == -2) $list[$key]['status_name'] = '未接听'; } $count = OutCallLog::where('customer_id', $param['customer_id'])->count(); return json(['code' => 0, 'data' => $list, 'msg' => '获取成功', 'count' => $count]); } /** * 已分配客户列表 * 分配弹框页面接口 */ public function recovery_customers() { $request = request(); // $root_id = $request->employee->root_id; $param = $request->only(['customer_id' => 0, 'crm_res_id']); if (!$this->checkResourceIsMy($param['crm_res_id'])) return; $info = Customer::where('id', $param['customer_id'])->findOrEmpty(); if (!$info->isEmpty() && $info->crm_res_id) { // $state11 = CustomerVisitLog::changeState('已交定', 'chaos'); // $state12 = CustomerVisitLog::changeState('已签单', 'chaos'); // $log_where = [ // ['customer_id', '=', $param['customer_id']], // ['state', 'in', array_merge($state11, $state12)] // ]; // $repeat = CustomerVisitLog::where($log_where)->findOrEmpty(); // if (!$repeat->isEmpty()) { // return json(['code' => 1, 'data' => '客户“' . $info->name . '”已经交定或签单无法回收', 'msg' => '客户“' . $info->name . '”已经交定或签单无法回收']); // } if (!in_array($info->state, [0, '待确认'], true)) { return json(['code' => 1, 'data' => '客户“' . $info->name . '”已标记为有效', 'msg' => '客户“' . $info->name . '”已标记为有效无法回收']); } // //客户所在资源库 // $crm_log = CrmImportLog::where('id', $info->crm_res_id)->find(); // //如果有上级资源库上级资源库+1 本级资源库-1 // if ($crm_log->pid) { // //本级资源库-1 // $count = $crm_log->count - 1; // $crm_log->count = $count ?: 0; // $crm_log->save(); // //上级资源库 // $pid_info = CrmImportLog::where('id', $crm_log->pid)->find(); // Customer::where([['id', '=', $param['customer_id']]])->update(['crm_res_id' => $pid_info->id, 'org_id' => $pid_info->org_id, 'employee_id' => null,'employee_time'=>null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => 0,'state'=>'待确认']); // $pid = $pid_info->id; // //查询未分配数量 // $left_num = Customer::where([['crm_res_id', '=', $crm_log->id], ['employee_id', '=', null]])->count(); // CrmImportLog::where('id', $crm_log->id)->update(['left_num' => $left_num]); // } else { // $pid = $crm_log->id; // Customer::where([['id', '=', $param['customer_id']]])->update(['org_id' => $crm_log->org_id, 'employee_id' => null, 'employee_time'=>null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => 0,'state'=>'待确认']); // } // //查询未分配数量 // $left_num = Customer::where([['crm_res_id', '=', $pid], ['employee_id', '=', null],['is_distribution','=',0]])->count(); // CrmImportLog::where('id', $pid)->update(['left_num' => $left_num]); Db::startTrans(); try { $crm_log = CrmImportLog::where('id', $info->crm_res_id)->find(); if ($param['crm_res_id'] != $crm_log['id']) { $listids = CrmImportLog::where('id', '>', 0)->column('pid,id'); // var_dump($crm_log['id']); // exit; $upids = $this->getInfo($crm_log['id'], $listids, $param['crm_res_id']); // var_dump($upids); // exit; foreach ($upids as $k => $v) { $res_data = CrmImportLog::find($v['id']); $count = $res_data->count - 1; $left_num = $res_data->left_num - 1; $res_data->count = $count ? $count : 0; if ($info->crm_res_id == $res_data->id && empty($info->employee_id)) $res_data->left_num = $left_num ? $left_num : 0; $res_data->save(); } } $ben_log = CrmImportLog::where('id', $param['crm_res_id'])->find(); $ben_left_num = $ben_log->left_num + 1; $ben_log->left_num = $ben_left_num ?: 0; $ben_log->save(); $is_distribution = $ben_log->pid ? 1 : 0; Customer::where([['id', '=', $param['customer_id']]])->update(['crm_res_id' => $ben_log->id, 'org_id' => $ben_log->org_id, 'employee_id' => null, 'employee_time' => null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => $is_distribution, 'state' => 0]); //保存回收记录 if ($info->employee_id) { CrmImportRetrieveLog::insert([ 'root_id' => request()->employee->root_id, 'employee_id' => $info->employee_id, 'crm_res_id' => $param['crm_res_id'], 'type' => 1, 'source_type' => 0, 'count' => 1, 'customer_ids' => $param['customer_id'], 'addtime' => time(), 'operator_employee_id' => request()->employee->id ]); } Db::commit(); } catch (\Exception $e) { Db::rollback(); var_dump($e->getMessage()); return json(['code' => 1, 'msg' => '回收失败.']); } return json(['code' => 0, 'data' => '回收成功', 'msg' => '回收成功']); } else { return json(['code' => 0, 'data' => '回收成功', 'msg' => '回收成功']); } } /** * 查询所有的上级id */ function getInfo($id, $all, $pid) { $result = []; if ($id != $pid) { //将id等于0作为递归终点,如果要将无法查找到作为递归终点,也要在下面的循环遍历之后判断 foreach ($all as $item) { if ($item['id'] == $id) { $result[] = $item; break; } } $parents = $this->getInfo($result[0]['pid'], $all, $pid); if (!empty($parents)) { $result = array_merge($result, $parents); } } return $result; } /** * 未分配分配客户列表 * 分配弹框页面接口 */ public function get_uncustomers() { $request = request(); $root_id = $request->employee->root_id; $param = $request->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'org_id' => 0, 'id' => 0]); if (!$this->checkResourceIsMy($param['id'])) return; $where[] = ['employee_id', '=', NULL]; $where[] = ['crm_res_id', '=', $param['id']]; $where[] = ['is_distribution', '=', 0]; if ($param['keyword']) { $ids = []; $keyword = Customer::where($where)->field('id,community_name,phone,phone1,phone2')->select(); foreach ($keyword as $key => $val) { $str = $val['community_name'] . $val['phone'] . $val['phone1'] . $val['phone2']; if (strpos($str, $param['keyword']) !== false) $ids[] = $val['id']; } $where[] = ['id', 'in', $ids]; } $customer_ids = Customer::where($where)->column('id'); //排除已经分配后又置为无效的数据 // $query[] = ['customer_id', 'in', $customer_ids]; // $customer_ids1 = CustomerVisitLog::where($query)->column('customer_id'); // $where[] = ['id', 'in', array_diff($customer_ids, $customer_ids1)]; $list = Customer::where($where)->field('id,name,community_name,square,phone,phone1,phone2')->page($param['page'], $param['limit'])->select(); foreach ($list as &$item) { $item['phone'] = substr_replace($item['phone'], '******', 3, 6); $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : ''; $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : ''; } $count = Customer::where($where)->count(); return json(['code' => 0, 'data' => $list, 'count' => $count]); } /** * 获取导入失败的客户 */ public function get_error_customers(){ $param = request()->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'id' => 0]); $list = CrmImportRecord::where([['log_id', '=', $param['id']], ['content', 'not null', null]])->order('id asc')->column('content'); $new_list = []; foreach($list as $k => $v){ if (!empty($v)) { $one = json_decode($v); $new_list = array_merge($new_list, $one); } } $count = count($new_list); $data = array_slice($new_list, ($param['page'] - 1) * $param['limit'], $param['limit']); return json(['code' => 0, 'data' => $data, 'count' => $count]); } /** * 获取子部门 */ public function get_child_orgs($id) { $info = Org::where('id', $id)->findOrEmpty(); $path = $info->path; $ids = Org::where([['path', 'like', $path . '%']])->column('id,name'); return json(['code' => 0, 'data' => array_column($ids, 'id'), 'param' => $ids]); } /** * 新详情页 * 详情页 */ public function read_new() { $request = request(); $id = input('id', 0); if (!$this->checkResourceIsMy($id)) return; $type = input('type', 0); //handdistribution //基本信息 $data = $this->read($id); View::assign('data', $data); $counts = $this->get_customer_data($id); View::assign('counts', $counts); View::assign('id', $id); $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name')->select()->toArray(); View::assign('org', $org); View::assign('type', $type); $root_id = $request->employee->root_id; View::assign('root_id', $root_id); return View::fetch(); } /** * 删除资源 */ public function delete_new() { $request = request(); $id = input('id', 0); $root_id = $request->employee->root_id; if (!$this->checkResourceIsMy($id)) return; $orgids = orgSubIds($root_id); if ($id > 0) { $where[] = ['id', '=', $id]; $where[] = ['org_id', 'in', $orgids]; $info = CrmImportLog::where($where)->findOrEmpty(); $listids = CrmImportLog::where([['id', '>', $info['id']], ['org_id', 'in', $orgids]])->column('id,pid'); $ids = $this->getAllLowerLevelId($listids, $info['id']); $info->delete(); CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete(); //记录操作删除资源库信息 $msg = '操作回收客户员工id:' . $request->employee->id . '要删除资源库的id' . $id; trace($msg,'资源删除'); return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']); // if ($info->state == 0) { // $info->delete(); // CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete(); // //CrmImportLog::where($where)->delete(); // if ($info->count > 0) Customer::where([['crm_res_id', '=', $id]])->useSoftDelete('delete_time', time())->delete(); // return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']); // } else { // $iscus = Customer::where([['crm_res_id', 'in', $ids], ['org_id', 'in', $orgids], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->count(); // $bencus = Customer::where([['crm_res_id', '=', $info['id']], ['org_id', 'in', $orgids], ['employee_id', 'NOTNULL', null], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->count(); // if ($iscus && $bencus) return json(['code' => 1, 'msg' => '请先回收未转化为客户的资源']); // //删除已经回收的客户 // //$isdelcus = Customer::where([['crm_res_id','=',$info['id']],['state','in',Customer::changeState('待确认','chaos')]])->delete(); // $info->delete(); // CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete(); // return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']); // } } return json(['code' => 1, 'data' => '删除失败', 'msg' => '删除失败']); } /** * 客户转移 */ public function transfer() { if (!request()->isAjax()) { $customer_id = input('data', '', 'trim'); //客户的id View::assign('customer_id', $customer_id); $customer_ids = array_filter(explode(',', $customer_id)); View::assign('count', count($customer_ids)); return View::fetch(); } else { $request = request(); $param = $request->only(['content' => [], 'customer_id' => '']); if (empty($param['content']) || !is_array($param['content'])) return json(['code' => 1, 'data' => '请选择分配对象', 'msg' => '请选择分配对象']); $customer_id = explode(',', $param['customer_id']); $root_id = request()->employee->root_id; $orgSub = orgSubIds($root_id); $customer_id = Customer::where([['id', 'in', $customer_id], ['org_id', 'in', $orgSub]])->column('id'); shuffle($customer_id); //随机分配 $count = count($customer_id); $all_count = array_sum(array_column($param['content'], 'count')); if ($all_count > $count) return json(['code' => 1, 'data' => '客户转移数量不能大于被转移客户总数', 'msg' => '客户转移数量不能大于被转移客户总数']); $repeat = []; // 无法分配 //指派类型 //分配到人去重 $eids = array_column($param['content'], 'con_id'); $phones = Customer::where([['employee_id', 'in', $eids]])->column('employee_id,name,phone,phone1,phone2'); $phone_emp = []; //待分配的业务员 foreach ($phones as $k => $v) { $phone_emp[$v['employee_id']] = isset($phone_emp[$v['employee_id']]) ? $phone_emp[$v['employee_id']] : []; array_push($phone_emp[$v['employee_id']], $v['phone'], $v['phone1'], $v['phone2']); } foreach ($phone_emp as $k => $v) { $phone_emp[$k] = array_unique(array_filter($v)); } foreach ($param['content'] as $k => $v) { //原有手机号 $param['content'][$k]['phones'] = isset($phone_emp[$v['con_id']]) ? $phone_emp[$v['con_id']] : []; //分配的客户 $param['content'][$k]['cids'] = []; } //分配 $customer = Customer::where([['id', 'in', $customer_id]])->column('id,name,phone,phone1,phone2'); $t_customer = []; //已转移客户 foreach ($customer as $k => $v) { $ls_phones = array_filter([$v['phone'], $v['phone1'], $v['phone2']]); $i = 0; foreach ($param['content'] as $k1 => $v1) { //没有分配完成 if ($v1['count'] > count($v1['cids'])) { //并且手机号不重复 if ($v1['phones'] == []) { $param['content'][$k1]['cids'][] = $v['id']; $i = 1; break; } else { $phone_intersect = array_intersect($v1['phones'], $ls_phones); if ($phone_intersect) { continue; //手机号重复 分配给下一个员工 } else { $param['content'][$k1]['cids'][] = $v['id']; $i = 1; break; } } } } //无法分配 if ($i == 0) { $repeat[$v['id']] = $v['name']; } } //分配 $visitlog = []; $date = date('Y-m-d'); $old_employee = []; // 原先的员工 $old_manager = []; // 原先的员工领导 $new_manager = []; // 新员工领导 $org_list = []; foreach ($param['content'] as $k1 => $v1) { if ($v1['cids']) { $new_org = Employee::where('id', '=', $v1['con_id'])->value('org_id'); $t_customer = array_merge($t_customer, $v1['cids']); $emp = Employee::where('id', $v1['con_id'])->findOrEmpty(); $old_employee_s = Customer::where([['id', 'in', $v1['cids']]])->column('employee_id,name'); $old_employee = array_merge($old_employee, $old_employee_s); // 同一部门内转移不给领导通知 foreach ($old_employee_s as $k_old => $v_old) { $org_id = Employee::where('id', '=', $v_old['employee_id'])->value('org_id'); if ($new_org != $org_id) { // 旧部门领导 if (isset($org_list[$org_id])) { $manager = $org_list[$org_id]; } else { $manager = Employee::where([['org_id', '=', $org_id], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id'); if ($manager) { $org_list[$org_id] = $manager; } } if ($manager) { foreach ($manager as $m) { $old_manager[$m][] = $v_old['name'] ? $v_old['name'] : '未知'; } } // 新部门领导 if (isset($org_list[$new_org])) { $manager = $org_list[$new_org]; } else { $manager = Employee::where([['org_id', '=', $new_org], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id'); if ($manager) { $org_list[$new_org] = $manager; } } if ($manager) { foreach ($manager as $m) { $new_manager[$m][] = $v_old['name'] ? $v_old['name'] : '未知'; } } } $old_employee_s[$k_old]['name'] = $v_old['name'] ? $v_old['name'] : '未知'; } Customer::where([['id', 'in', $v1['cids']]])->update([ 'employee_id' => $v1['con_id'], 'org_id' => $emp->org_id, 'is_resource' => 0, 'is_distribution' => 1 ]); event(new Msg($v1['con_id'], '有' . count($v1['cids']) . '个客户转移给您,请及时查看!', 'customerTransfer')); $title = '您好!你有' . count($v1['cids']) . '个新的分配客户,请及时查看'; $this->sendOfficialAccountTransfer($v1['con_id'], array_column($old_employee_s, 'name'), $title); //添加跟进记录 foreach ($v1['cids'] as $v2) { $visitlog[] = [ 'customer_id' => $v2, 'type' => '', 'remark' => '客户转移', 'employee_id' => $v1['con_id'], 'user_id' => $emp->uid, 'state' => '待确认', 'next_contact_date' => $date ]; } } } $old_msg_employee = []; foreach ($old_employee as $k => $v) { $old_msg_employee[$v['employee_id']][] = $v['name'] ? $v['name'] : '未知'; } // 通知原先的员工 foreach ($old_msg_employee as $k => $v) { event(new Msg($k, '您名下有' . count($v) . '个客户已被转移', 'customerTransfer')); $title = '您好!你名下有' . count($v) . '个客户已被转移'; $this->sendOfficialAccountTransfer($k, $v, $title); } // 通知原先部门领导 foreach ($old_manager as $k => $v) { event(new Msg($k, '您部门有' . count($v) . '个客户已被转移', 'customerTransfer')); $title = '您好!你部门有' . count($v) . '个客户已被转移'; $this->sendOfficialAccountTransfer($k, $v, $title); } // 通知新部门领导 foreach ($new_manager as $k => $v) { event(new Msg($k, '您部门新分配' . count($v) . '个客户', 'customerTransfer')); $title = '您好!你部门新分配' . count($v) . '个客户,请及时处理'; $this->sendOfficialAccountTransfer($k, $v, $title); } if ($visitlog) CustomerVisitLog::insertAll($visitlog); //查询未分配数量 // $diff = array_diff($customer_id, $t_customer); if ($repeat) { $name = implode(',', array_values($repeat)); return json(['code' => 1, 'data' => '客户:' . $name . '。手机号重复无法分配', 'msg' => '客户:' . $name . '。手机号重复无法分配']); } else { return json(['code' => 0, 'data' => '分配完成', 'msg' => '分配完成']); } } } /** * 发送公众号消息(客户转移) * employee_id => count 业务员id =》 分配数量 */ public function sendOfficialAccountTransfer($employee, $customer, $title) { $client_type = Miniprogram::where('root_id', request()->employee->root_id)->value('notify'); $keword1 = implode(',', $customer); //业务员客户数量 $data = $msg = []; $data['page'] = "/pages/index/index?office_type=allocation&clientype=" . $client_type; $data['param'] = [ 'first' => $title, //标题 'keyword1' => $keword1, 'keyword2' => date('Y-m-d H:i:s'), 'remark' => '感谢您的使用' // ]; $msg = [ 'type' => 'customerTransfer', 'employee_id' => $employee ]; EmployeeMsg::temploateMsg($msg, $data); return true; } /** * 资源标签设置 */ public function label() { $employee = request()->employee; $labelSetting = Setting::where(['root_id' => $employee->root_id, 'name' => 'crm_label', 'state' => 1])->findOrEmpty(); $label = $labelSetting->isEmpty() ? [] : explode(',', $labelSetting->content); view::assign('label', $label); return View::fetch(); } /** * 添加资源标识 */ public function addLabel($value) { $employee = request()->employee; $labelSetting = Setting::where(['root_id' => $employee->root_id, 'name' => 'crm_label', 'state' => 1])->findOrEmpty(); if ($labelSetting->isEmpty()) { $labelSetting->root_id = $employee->root_id; $labelSetting->name = 'crm_label'; $labelSetting->state = 1; } else { $label = explode(',', $labelSetting->content); } $label[] = $value; $labelSetting->content = implode(',', $label); $labelSetting->save(); return json(['code' => 0, 'msg' => '添加成功']); } /** * 删除资源标识 */ public function deleteLabel($value) { $employee = request()->employee; $labelSetting = Setting::where(['root_id' => $employee->root_id, 'name' => 'crm_label', 'state' => 1])->find(); if ($labelSetting->isEmpty()) return json(['code' => 0, 'msg' => '删除成功']); $label = explode(',', $labelSetting->content); $key = array_search($value, $label); unset($label[$key]); $labelSetting->content = implode(',', $label); $labelSetting->save(); return json(['code' => 0, 'msg' => '删除成功']); } /** * 资源库自动回收设置 */ public function auto_recovery() { $employee = request()->employee; $param = request()->only(['day' => 0]); $data = Setting::where(['root_id' => $employee->root_id, 'name' => 'auto_recovery', 'state' => 1])->findOrEmpty(); if (!request()->isAjax()) { View::assign('day', $data->isEmpty() ? 0 : $data->content); return View::fetch(); } if ($data->isEmpty()) { $data->root_id = $employee->root_id; $data->name = 'auto_recovery'; $data->state = 1; $data->content = $param['day']; } else { $data->content = $param['day']; } $data->save(); return json(['code' => 0, 'msg' => '设置成功']); } /** * 检测资源的操作人是否是上传人 */ private function checkResourceIsMy($resourceid){ $resourcefb = Setting::where(['root_id'=>request()->employee->root_id, 'name'=>'resource_fp'])->value('content'); if($resourcefb !== null && $resourcefb == 0) return true; $resource = CrmImportLog::find($resourceid); if($resource->upload_source == 0 && $resource->employee_id != request()->employee->id) return false; return true; } /** * 网销渠道设置 */ public function channel_index(){ $request = request(); $root_id = $request->employee->root_id; $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting'); $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $data = ['fish_account' => '', 'fish_adv' => '', 'fish_open' => 0, 'fish_access_token' => '', 'fish_refresh_token' => '', 'fish_refresh_time' => 0,'tx_account' => '', 'fish_update_time' => '', 'tx_adv' => '', 'tx_open' => 0, 'tx_access_token' => '', 'tx_refresh_token' => '', 'tx_update_time' => '']; if (isset($channel_setting)&&$channel_setting) $data = json_decode($channel_setting, true); $appid = config('app.fish_clue_appid'); $secret = config('app.fish_clue_secret'); $url = (new Fish($appid, $secret))->authorizedAddress($root_id); //生成授权地址 $tx_url = (new Tencent(config('app.tx_clue_appid'),config('app.tx_clue_secret')))->authorizedAddress($root_id); View::assign('url', $url); View::assign('fish_account_token', $aec->encrypt($root_id)); View::assign('tx_url', $tx_url); View::assign('data', $data); // todo 威弘 $channel_setting_wh = Company::where(['root_id' => $root_id])->value('channel_setting_wh'); // $aec = new Aec(config('app.aec_key'), config('app.aec_iv')); $data_wh = ['fish_account' => '', 'fish_adv' => '', 'fish_open' => 0, 'fish_access_token' => '', 'fish_refresh_token' => '', 'fish_refresh_time' => 0,'tx_account' => '', 'fish_update_time' => '', 'tx_adv' => '', 'tx_open' => 0, 'tx_access_token' => '', 'tx_refresh_token' => '', 'tx_update_time' => '']; if (isset($channel_setting_wh)&&$channel_setting_wh) $data_wh = json_decode($channel_setting_wh, true); // if(empty($data_wh['fish_account']))$data_wh['fish_account'] = $aec->encrypt($root_id); $appid_wh = config('app.fish_clue_appid_wh'); $secret_wh = config('app.fish_clue_secret_wh'); $url_wh = (new \clue\FishWh($appid_wh, $secret_wh))->authorizedAddress($root_id); //生成授权地址 View::assign('url_wh', $url_wh); // $data_wh['fish_account_wh'] = $aec->encrypt($root_id); $data_wh['fish_adv_wh'] = $data_wh['fish_adv']??''; View::assign('data_wh', $data_wh); return View::fetch(); } /** * 设置网销渠道 */ public function channel_edit() { $request = request(); $root_id = $request->employee->root_id; $param = $request->only(['fish_account' => '', 'fish_access_token' => '', 'fish_refresh_token' => '', 'fish_update_time' => '', 'fish_refresh_time' => 0, 'fish_adv' => '', 'fish_open' => 0, 'tx_account' => '', 'tx_adv' => '', 'tx_open' => 0, 'tx_access_token' => '', 'tx_refresh_token' => '', 'tx_update_time' => '']); //todo 威弘 $param_wh_post = $request->only([ 'fish_account_wh' => '', 'fish_access_token' => '', 'fish_refresh_token' => '', 'fish_update_time' => '', 'fish_refresh_time' => 0, 'fish_adv_wh' => '', 'fish_open' => 0, 'tx_account' => '', 'tx_adv' => '', 'tx_open' => 0, 'tx_access_token' => '', 'tx_refresh_token' => '', 'tx_update_time' => '' ]); $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting'); if (isset($channel_setting) && $channel_setting) { $data = json_decode($channel_setting, true); $param['fish_access_token'] = $data['fish_access_token']; $param['fish_refresh_token'] = $data['fish_refresh_token']; $param['fish_update_time'] = $data['fish_update_time']; $param['fish_refresh_time'] = $data['fish_refresh_time']; $param['tx_access_token'] = $data['tx_access_token']; $param['tx_refresh_token'] = $data['tx_refresh_token']; $param['tx_update_time'] = isset($data['tx_update_time'])?$data['tx_update_time']:''; } Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($param)]); //todo 威弘 $param_wh['fish_open'] = 0; $param_wh['tx_account'] = ''; $param_wh['tx_adv'] = ''; $param_wh['tx_open'] = 0; $param_wh['fish_account'] = $param_wh_post['fish_account_wh']; $param_wh['fish_adv'] = $param_wh_post['fish_adv_wh']; $param_wh['fish_account_wh'] = $param_wh_post['fish_account_wh']; $param_wh['fish_access_token'] = $param_wh_post['fish_access_token']; $param_wh['fish_refresh_token'] = $param_wh_post['fish_refresh_token']; $param_wh['fish_update_time'] = $param_wh_post['fish_update_time']; $param_wh['fish_refresh_time'] = $param_wh_post['fish_refresh_time']; $param_wh['tx_access_token'] = $param_wh_post['fish_refresh_token']; $param_wh['tx_refresh_token'] = $param_wh_post['fish_update_time']; $param_wh['tx_update_time'] = $param_wh_post['fish_refresh_time']; $channel_setting_wh = Company::where(['root_id' => $root_id])->value('channel_setting_wh'); if (isset($channel_setting_wh) && $channel_setting_wh) { $data_wh = json_decode($channel_setting_wh, true); $param_wh['fish_access_token'] = $data_wh['fish_access_token']; $param_wh['fish_refresh_token'] = $data_wh['fish_refresh_token']; $param_wh['fish_update_time'] = $data_wh['fish_update_time']; $param_wh['fish_refresh_time'] = $data_wh['fish_refresh_time']; $param_wh['tx_access_token'] = $data_wh['tx_access_token']; $param_wh['tx_refresh_token'] = $data_wh['tx_refresh_token']; $param_wh['tx_update_time'] = isset($data_wh['tx_update_time'])?$data_wh['tx_update_time']:''; } Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($param_wh)]); $fish = CustomerSource::where(['root_id' => $root_id, 'source' => '飞鱼线索'])->find(); if (!$fish) CustomerSource::create(['root_id' => $root_id, 'source' => '飞鱼线索']); $tx = CustomerSource::where(['root_id' => $root_id, 'source' => '腾讯线索'])->find(); if (!$tx) CustomerSource::create(['root_id' => $root_id, 'source' => '腾讯线索']); $op = CustomerSource::where(['root_id' => $root_id, 'source' => '欧派线索'])->find(); if (!$op) CustomerSource::create(['root_id' => $root_id, 'source' => '欧派线索']); return json(['code' => 0, 'msg' => '设置成功']); } }