request->token; $agtdata = $this->request->agtdata; $integral = AgentIntegralLog::where('agt_id', '=', $agtdata['id'])->order('id desc')->value('now_integral'); //查询出属于我的客户 $customer_id = Customer::where('agents_id', '=', $agtdata['id'])->column('id'); // 待确认数量统计 $visit = Customer::where(array_merge([CustomerVisitLog::changeState(['state', '=', '待确认'])], [['id', 'in', $customer_id]]))->count(); // 交定数量统计 $ding = CustomerVisitLog::where(array_merge([CustomerVisitLog::changeState(['state', '=', '交定'])], [['customer_id', 'in', $customer_id]]))->count(); // 签单数量统计 $sign = CustomerVisitLog::where(array_merge([CustomerVisitLog::changeState(['state', '=', '签单'])], [['customer_id', 'in', $customer_id]]))->count(); // 正在进行的活动查询 $acting = Activity::where(['del' => 0, 'show' => 1, 'root_id' => $token['root_org']]) ->whereBetweenTimeField('start_date', 'end_date')->count(); //线索量 $clue_count = CustomerClue::where(['agent_id' => $agtdata['id']])->count(); // 企业名获取 $companyName = Company::where(['root_id'=>$agtdata['root_id']])->value('company_name'); $data = [ 'agttype' => $agtdata['type'], 'acting' => $acting, 'integral' => $integral, 'visit' => $visit, 'ding' => $ding, 'sign' => $sign, 'count' => $customer_id ? count($customer_id) : 0, 'clue_count' => $clue_count, 'company_name'=>$companyName ]; return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']); } //经济人报备客户 public function add_customer() { $token = $this->request->token; $agtdata = $this->request->agtdata; $data = $this->request->only(['name', 'phone', 'sex' => 1, 'community_name', 'last_contact_date' => date('Y-m-d'), 'source_id', 'deposit_money' => null, 'signed_money' => null, 'external_userid', 'remark']); // 设计师能否报备、获取客户 $root_id = !empty($token['root_org']) ? $token['root_org'] : $agtdata['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', '=', $agtdata['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' => '请联系业务员进行报备客户']); } } $data['employee_id'] = $agtdata['agent_employee_id']; $data['org_id'] = $agtdata['org_id']; $data['state'] = '待确认'; $data['level'] = 'C'; $data['agents_id'] = $agtdata['id']; $orgids = orgSubIds(!empty($token['root_org']) ? $token['root_org'] : $agtdata['root_id']); // 新建档,检测手机号是否重复 $pharr = [$data['phone']]; $tips = (new AgentsLogic())->checkPepeat($pharr, $orgids, $data['employee_id']); if (!empty($tips)) return $tips; //插入经济人提报的客户 $data['phone'] = trim($data['phone']); Db::startTrans(); try { $ms = Customer::create($data); $empuid = Employee::where('id', $agtdata['agent_employee_id'])->value('uid'); $visitlog = CustomerVisitLog::where([['customer_id', '=', $ms->id], ['state', 'in', CustomerVisitLog::changeState('待确认', 'chaos')]])->find(); if (!$visitlog) { $save = []; $save['customer_id'] = $ms->id; $save['type'] = ''; $save['remark'] = !empty($data['remark']) ? $data['remark'] : '经纪人报备客户'; $save['employee_id'] = $agtdata['agent_employee_id']; $save['user_id'] = $empuid; $save['state'] = 1; //$save['aid'] = $param['aid']; CustomerVisitLog::create($save); } //报备增加积分 $rule_type = 'putong_integral'; $rule_data = AgentType::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $agtdata['type']]])->value('count'); $rule_list = !empty($rule_data) ? json_decode($rule_data, true) : ''; $itg_rule = !empty($rule_list) ? $rule_list['putong_integral'] : 0; //$itg_rule = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $agtdata['root_id']]])->value('value'); $state = '经纪人手动录入客户'; $add = array( 'agent_id' => $agtdata['id'], 'type' => 4, 'integral' => $itg_rule, 'addtime' => time(), 'state' => $state, 'customer_id' => $ms->id ); AgentIntegral::insert($add); Db::commit(); } catch (\Exception $e) { Db::rollback(); return json(['code' => 1, 'msg' => '录入数据失败.']); } if ($ms) { return json(['code' => 0, 'msg' => '录入成功.']); } else { return json(['code' => 1, 'msg' => '录入失败.']); } } /** * 经纪人客户列表 */ public function customer_list() { $token = $this->request->token; $agtdata = $this->request->agtdata; $param = $this->request->only([ 'state' => '', 'page' => 1, 'limit' => 20, 'keyword' => '', 'source' => '', 'date' => '', 'visit' => '', 'addtime' => '', 'agent_source_type' => '' ]); $agt_id = [$agtdata['id']]; // 1)查询条件 $condition = [['agents_id', 'in', $agt_id]]; empty($param['agent_source_type']) ?: $condition[] = ['agent_source_type', '=', $param['agent_source_type']]; // 可回收列表 if (!empty($param['protected'])) { $orgids = orgSubIds($this->request->token['org_id']); $condition[] = ['org_id', 'in', $orgids]; } setCondition($param, 'level', '=', $condition); // 3)来源筛选 $source = [ 'ownadd' => [ ['crm_res_id', '=', null], ['is_resource', '=', 0], ['remark', '=', ''] ], 'crmres' => [ ['crm_res_id', 'not null', ''], ['is_resource', '=', 0] ], 'public' => [ ['remark', '=', '公海获取'] ], ]; // 来源 (empty($param['source']) || !isset($source[$param['source']])) ?: $condition = array_merge_recursive($condition, $source[$param['source']]); // 5)待回访 empty($param['visit']) ?: $condition[] = empty($param['date']) ? ['revisit_time', '=', date('Y-m-d') . ' 00:00:00'] : ['revisit_time', '=', $param['date'] . ' 00:00:00']; // 6)录入时间筛选 empty($param['addtime']) ?: $condition[] = ['addtime', 'like', date('Y-m-d', strtotime($param['addtime'])) . '%']; //增加按跟进时间排序 $order = 'addtime desc'; empty($param['following']) ?: $order = 'last_contact_date ' . $param['following'] . ',' . $order; //关键词搜索 if (!empty($param['keyword'])) { $list = Customer::where($condition)->order($order)->field('id,name,community_name,phone')->select()->toArray(); $customersIdList = []; foreach ($list as $k => $v) { $str = $v['name'] . $v['community_name'] . $v['phone']; if (strpos($str, trim($param['keyword'])) !== false) $customersIdList[] = $v['id']; } } else { $customersIdList = Customer::where($condition)->order($order)->column('id'); } // 剔除掉未经历该状态的客户id if (!empty($param['state'])) { if (CustomerVisitLog::changeState($param['state']) == '回访' || $param['state'] == '有效') { // 查询经历过回访以外所有状态的客户id(有效) $expectCustomersIdList = CustomerVisitLog::where([['customer_id', 'in', $customersIdList], ['state', 'not in', CustomerVisitLog::changeState('回访', 'chaos')], ['employee_id', '=', $agtdata['agent_employee_id']]])->group('customer_id')->column('customer_id'); if ($param['state'] == '有效') { $customersIdList = $expectCustomersIdList; } else { $customersIdList = array_diff($customersIdList, $expectCustomersIdList); } } else { $customersIdList = CustomerVisitLog::where([['customer_id', 'in', $customersIdList], ['state', 'in', CustomerVisitLog::changeState($param['state'], 'chaos')]])->group('customer_id')->column('customer_id'); } } // 置顶数据获取 $top = AgentCustomerTop::where([['root_id', '=', $token['root_org']], ['agent_id', '=', $agtdata['id']]])->where([['customer_id', 'in', $customersIdList]])->order('addtoptime asc')->column('customer_id'); //var_dump($top); //exit; // 新数据获取 //$new = Customer::where([['id', 'in', $customersIdList], ['fresh', '=', 1]])->order($order)->column('id'); $new = []; // 剔除不应该出现的客户 $top = array_diff($top, array_diff($top, $customersIdList)); // 剔除不应该出现的新客户 $new = array_diff($new, array_diff($new, $customersIdList)); // 剔除置顶的客户 $notTop = array_diff($customersIdList, $top); // 剔除新客户 $notNew = array_diff($notTop, $new); // 将置顶客户放置到前面 $customersIdList = array_unique(array_merge($top, $new, $notNew)); // 总数获取 $count = Customer::where($condition)->count(); //返回到特定位置 $id = input('id', 0); if ($id) { $ids = array_chunk($customersIdList, $param['limit']); foreach ($ids as $k => $v) { $a = false; foreach ($v as $k2 => $v2) { if ($id == $v2) { $a = true; $param['page'] = $k + 1; break; } } if ($a) break; } } // 分页 $pageIdList = array_slice($customersIdList, ($param['page'] - 1) * $param['limit'], $param['limit']); $condition[] = ['id', 'in', $pageIdList]; $customers = Customer::with(['designer', 'designerOrg'])->where($condition)->select(); // 总数获取 $count = $customers->count(); $customers = $customers->visible(['id', 'employee_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'agent_source_type'])->toArray(); if (!empty($setting)) { $setting_content = json_decode($setting['content'], true); } else { $setting_content = []; } foreach ($customers as $k => $v) { $customers[$k]['un_protected'] = 0; $customers[$k]['protected_tips'] = ''; if (!empty($v['protected_to']) && time() > strtotime($v['protected_to'])) { $state_n = Customer::changeState($v['state'], 'n'); if (!empty($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) { $customers[$k]['un_protected'] = 1; $customers[$k]['protected_tips'] = '已过期'; } } if (!empty($v['protected_to']) && time() < strtotime($v['protected_to'])) { $day = floor((strtotime($v['protected_to']) - time()) / (24 * 3600)); $customers[$k]['protected_tips'] = $day ? '保护期' . $day . '天' : '即将过期'; } } $customers = array_combine(array_column($customers, 'id'), $customers); // 状态统计获取 $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $pageIdList]])->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]; $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']; } // 排序(按每页id顺序取值) $listData = []; foreach ($pageIdList as $customerId) { $customers[$customerId]['top'] = 0; $customers[$customerId]['is_new'] = $customers[$customerId]['fresh'] == 1 ? 1 : 0; $customers[$customerId]['addtoptime'] = null; if (in_array($customerId, $top)) $customers[$customerId]['addtoptime'] = 1; $customers[$customerId]['stateNum'] = $customersState[$customerId] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0]; $listData[] = $customers[$customerId]; } $data = [ 'list' => $listData, 'count' => $count ]; return json(['code' => 0, 'data' => $data, 'page' => $param['page']]); } //经济人核销记录 public function already_list() { $agtdata = $this->request->agtdata; $data = $this->request->only(['page' => 1, 'limit' => 15]); //查询申请记录 $rule_type = 'reduce_integral'; $already_rule = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $agtdata['root_id']]])->value('value'); $already_rule = $already_rule ? json_decode($already_rule, true) : ['jifen' => 1, 'money' => 1]; $list = AgentApplyIntegral::where([['root_id', '=', $agtdata['root_id']], ['agent_id', '=', $agtdata['id']]])->page($data['page'], $data['limit']) ->field('id,addtime,status,integral,good_id')->order('id desc')->select()->toArray(); foreach ($list as $key => $val) { if (in_array($val['status'], [1, 2])) { $status = '核销中'; } elseif ($val['status'] == 3) { $status = '兑换成功'; } elseif ($val['status'] == 4) { $status = '核销驳回'; } $list[$key]['status'] = $status; $list[$key]['good_name'] = !empty($val['good_id']) ? AgentPrize::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $val['good_id']]])->value('good_name') : ''; $list[$key]['money'] = !empty($val['integral']) ? bcdiv($val['integral'], $already_rule['jifen'], 1) * $already_rule['money'] : 0; } return json(['code' => 0, 'data' => $list, 'msg' => '请求成功.']); } //经纪人获得积分统计 public function intregral_total() { $agtdata = $this->request->agtdata; $all_integral = $all_already = 0; $rule_type = 'reduce_integral'; $already_rule = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $agtdata['root_id']]])->value('value'); $already_rule = $already_rule ? json_decode($already_rule, true) : ['jifen' => 1, 'money' => 0]; $integral = AgentIntegralLog::where([['agt_id', '=', $agtdata['id']]])->fieldRaw('sum(integral) as at,type')->group('type')->select()->toArray(); if (!empty($integral)) { foreach ($integral as $val) { if ($val['type'] == 1) { $all_integral = $val['at'] ? $val['at'] : 0; } if ($val['type'] == 2) { $all_already = $val['at'] ? $val['at'] : 0; } } } $no_already = AgentIntegralLog::where('agt_id', '=', $agtdata['id'])->order('id desc')->value('now_integral'); $no_already = $no_already ? $no_already : 0; $no_already_money = !empty($no_already) ? bcdiv($no_already, $already_rule['jifen'], 1) * $already_rule['money'] : 0; $data = array('all_intregral' => $all_integral, 'all_already' => $all_already, 'ke_already' => $no_already, 'no_already' => $no_already, 'no_already_money' => $no_already_money); return json(['code' => 0, 'data' => $data, 'msg' => '请求成功.']); } /** * 经纪人积分明细 */ public function integral_log() { $agtdata = $this->request->agtdata; $data = $this->request->only(['page' => 1, 'limit' => 10]); //先进行积分到账期限查询并添加到账 //$this->sel_call_integral($agtdata['id']); //添加到账end $list = AgentIntegral::with(['customer' => function ($query) { $query->field('id,name,sex'); }])->where([['agent_id', '=', $agtdata['id']]])->page($data['page'], $data['limit'])->order('addtime desc')->select()->toArray(); foreach ($list as $key => $val) { $list[$key]['addtime'] = date('Y-m-d H:i:s', $val['addtime']); $list[$key]['status'] = $val['status'] == 1 ? '待确认' : '到账'; //type:1客户变更状态,2分享获取,4网红录入 //$list[$key]['from'] = $val['type'] == 1 || $val['type'] == 4 ? 'customer' : 'share'; $list[$key]['from'] = in_array($val['type'], [1, 2, 3, 4]) ? 'customer' : 'share'; } return json(['code' => 0, 'data' => $list, 'msg' => '请求成功.']); } //活动列表接口 public function activity_list() { $token = $this->request->token; $agtdata = $this->request->agtdata; $param = $this->request->only(['page' => 1, 'limit' => 10, 'type']); $condition = ['root_id' => $token['root_org']]; if (isset($param['type']) && is_numeric($param['type']) && in_array($param['type'], [0, 1, 2])) $condition['cate'] = $param['type']; $data = Activity::where($condition) ->field('id,cover,title,start_date,end_date,IF(start_date>curdate(),1,IF(end_dateorder('timestate asc,addtime desc') ->page($param['page'], $param['limit']) ->select()->toArray(); foreach ($data as $key => $val) { $data[$key]['cover'] = str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $val['cover']); } return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']); } //活动详情 public function activity_detail($id) { $token = request()->token; $agtdata = $this->request->agtdata; $data = Activity::where(['id' => $id, 'del' => 0, 'root_id' => $token['root_org']])->field('id,cover,title,content,type,start_date,end_date,end_date as timestate,view,money,cate,address,poster')->find(); //是否收藏 $collectedIds = UserCollect::where(['user_id' => $token['uid'], 'content_type' => 'activity', 'content_id' => $id])->count(); $data['collected'] = $collectedIds > 0 ? true : false; Activity::where('id', $id)->inc('view')->update(); $data['employee_id'] = $agtdata['agent_employee_id']; $data['agent_id'] = $agtdata['id']; return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']); } //经纪人申请核销积分 public function apply_wipen() { $agtdata = $this->request->agtdata; $param = $this->request->only(['wiped_integral']); if (empty($param['wiped_integral'])) return json(['code' => 1, 'msg' => '请填写大于1的核销积分']); $agt_integral = AgentIntegralLog::where('agt_id', $agtdata['id'])->order('id desc')->value('now_integral'); if (empty($agt_integral) || $param['wiped_integral'] > $agt_integral) { return json(['code' => 1, 'msg' => '积分为零或者大于现有积分']); } $add = [ 'agent_id' => $agtdata['id'], 'empid' => $agtdata['agent_employee_id'], 'integral' => $param['wiped_integral'], 'root_id' => $agtdata['root_id'] ]; $bili = Db::table('fl_credits_setting')->where([['code', '=', 'reduce_integral'], ['root_id', '=', $agtdata['root_id']]])->value('value'); $bili = !empty($bili) ? json_decode($bili, true) : ['jifen' => 1, 'money' => 1]; $money = bcdiv($param['wiped_integral'], $bili['jifen'], 1) * $bili['money']; Db::startTrans(); try { $ms = AgentApplyIntegral::insertGetId($add); // 添加记录 AgentIntegralLog::create([ 'agt_id' => $agtdata['id'], 'addtime' => time(), 'integral' => $param['wiped_integral'], 'type' => 2, 'money' => $money, 'now_integral' => $agt_integral - $param['wiped_integral'] ]); } catch (Exception $e) { // 回滚事务 Db::rollback(); return json(['code' => 1, 'msg' => '兑换失败']); } Db::commit(); $msg = '经纪人【' . $agtdata['agent_name'] . '】提交核销积分申请'; if ($ms) { event(new Msg($agtdata['agent_employee_id'], $msg, 'agentWipen', $ms)); return json(['code' => 0, 'msg' => '申请成功']); } else { return json(['code' => 1, 'msg' => '申请失败']); } } //获取加入的企业 public function company() { $token = $this->request->token; // 用token中的openid获取用户的uid列表 $uidList = User::where(['mini_openid' => $token['openid']])->column('id'); $employees = AgentUser::where([['uid', 'in', $uidList], ['status', '=', 1], ['is_review', '=', 2]])->column('root_id,org_id', 'root_id'); $orgs = Org::where([['id', 'in', array_column($employees, 'org_id')]])->column('id,name', 'id'); $notifyList = Miniprogram::where([['root_id', 'in', array_column($employees, 'root_id')]])->column('root_id,notify', 'root_id'); $companyList = Company::with(['brand'])->where([['root_id', 'in', array_column($employees, 'root_id')]])->select()->visible(['company_name', 'root_id', 'status', 'end_date'])->toArray(); foreach ($companyList as &$item) { $item['client_type'] = isset($notifyList[$item['root_id']]) ? $notifyList[$item['root_id']]['notify'] : ''; //$item['is_manager'] = isset($employees[$item['root_id']]) ? $employees[$item['root_id']]['is_manager'] : 0; $item['org_name'] = (isset($employees[$item['root_id']]) && isset($orgs[$employees[$item['root_id']]['org_id']])) ? $orgs[$employees[$item['root_id']]['org_id']]['name'] : ''; if ($item['status'] == 2) { $item['off'] = 1; $item['off_remark'] = '账号被禁用'; } else { $item['off'] = 0; $item['off_remark'] = '账号正常'; } } return json(['code' => 0, 'data' => $companyList]); } //设置经纪人客户置顶 public function set_top() { $token = $this->request->token; $agtdata = $this->request->agtdata; $param = $this->request->only(['customer_id']); $you = AgentCustomerTop::where([['root_id', '=', $agtdata['root_id']], ['agent_id', '=', $agtdata['id']], ['customer_id', '=', $param['customer_id']]])->find(); if (!empty($you)) { $ms = AgentCustomerTop::where([['root_id', '=', $agtdata['root_id']], ['agent_id', '=', $agtdata['id']], ['customer_id', '=', $param['customer_id']]])->delete(); if ($ms) { return json(['code' => 0, 'msg' => '取消成功']); } else { return json(['code' => 1, 'msg' => '取消失败']); } } else { $add = ['agent_id' => $agtdata['id'], 'root_id' => $agtdata['root_id'], 'customer_id' => $param['customer_id']]; $ms = AgentCustomerTop::insert($add); if ($ms) { return json(['code' => 0, 'msg' => '设置成功']); } else { return json(['code' => 1, 'msg' => '设置失败']); } } } //核销规则 public function wiped_rule() { $token = $this->request->token; $agtdata = $this->request->agtdata; $rule_type = 'agent_hx_time'; $daoday = CreditsSetting::where([['code', '=', $rule_type], ['root_id', '=', $agtdata['root_id']]])->value('value'); if (!empty($daoday)) { $data1 = '经纪人推荐的客户在到店,交定,签单所产生的积分,会在' . $daoday . '天后自动到达积分账户'; $data2 = '经纪人推荐的客户所产生的积分,会在' . $daoday . '天后自动到达积分账户'; $data = $agtdata['type'] == 1 ? $data1 : $data2; } else { $data = '暂未设置'; } return json(['code' => 0, 'data' => $data, 'msg' => '请求成功']); } /** * 经纪人端首页排行榜 */ public function agent_rank_list() { $param = $this->request->only(['page' => 1, 'limit' => 10]); $agtdata = $this->request->agtdata; $list = AgentUser::with(['user' => function ($query) { $query->field('id,nickname,headimgurl'); }])->withCount(['customer' => function ($query, &$alias) { $alias = 'reccont'; }])->where([['root_id', '=', $agtdata['root_id']], ['is_review', '=', 2], ['status', '=', 1]])->field('id,agent_name,type,uid')->select()->toArray(); foreach ($list as &$val) { $val['type'] = $val['type'] == 1 ? '普通' : '网红'; $parr[] = $val['reccont']; } if (!empty($list)) { array_multisort($parr, SORT_DESC, $list); } $count = count($list); $list = array_slice($list, ($param['page'] - 1) * $param['limit'], $param['limit']); return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']); } /** * 分享任务、分享内容(展示我分享过的) */ // public function share_articles() // { // $param = $this->request->only(['page' => 1, 'limit' => 10, 'from' => '']); // $token = $this->request->token; // $agtdata = $this->request->agtdata; // $where = [ // ['root_id', '=', $token['root_org']], // ['employee_id', '=', $agtdata['agent_employee_id']], // ['disable', '=', 0], // ]; // //分享记录 // $log = ShareLog::where(['agent_id' => $agtdata['id'], 'type' => 'AgentArticle'])->group('data_id')->column('data_id'); // if (isset($param['from']) && $param['from'] == 'shareLog') { // //分享内容只展示我分享过的文章 // $where[] = ['id', 'in', $log]; // } // $data = AgentArticle::where($where)->withCount(['clue' => function($query, &$alias){ // $query->where([['pipe_type', '=', 'agentArticle']]); // $alias = 'clue_num'; // }])->field('id,title,file,share_num,addtime,type')->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray(); // foreach ($data as &$item) { // $item['new'] = in_array($item['id'], $log) ? false : true; // } // return json(['code' => 0, 'data' => $data]); // } /** * 分享任务、分享内容(展示我分享过的) */ public function share_article() { $param = $this->request->only(['page' => 1, 'limit' => 10, 'from' => '', 'type' => 1]); $token = $this->request->token; $agtdata = $this->request->agtdata; $where = [ ['root_id', '=', $token['root_org']], //['employee_id', '=', $agtdata['agent_employee_id']], ['disable', '=', 0], ]; $or1 = ['employee_id', '=', $agtdata['agent_employee_id']]; $or2 = ['from', '=', 2]; //分享内容只展示我分享过的文章 if (isset($param['from']) && $param['from'] == 'shareLog') { $content_id = []; $content = AgentShareContent::where($where)->where(function($query) use ($or1,$or2){ $query->whereOr([$or1,$or2]); })->select(); $log = ShareLog::where(['agent_id' => $agtdata['id']])->select(); foreach($content as $val) { foreach($log as $item) { if($val['data_id'] == $item['data_id'] && $val['type'] == $item['type']) { $content_id[] = $val['id']; } } } $where[] = ['id', 'in', $content_id]; $data = AgentShareContent::where($where)->where(function($query) use ($or1,$or2){ $query->whereOr([$or1,$or2]); })->order('addtime desc')->order('addtime desc')->page($param['page'], $param['limit'])->select()->toArray(); }else{ $data = AgentShareContent::where($where)->where('employee_id', '=', $agtdata['agent_employee_id'])->order('addtime desc')->order('addtime desc')->page($param['page'], $param['limit'])->select()->toArray(); } foreach ($data as &$item) { //分享量 $item['share_num'] = ShareLog::where([['type', '=', $item['type']], ['data_id', '=', $item['data_id']], ['agent_id', '<>', 'NULL']])->count(); //线索 $item['clue_num'] = CustomerClue::where([['pipe_type', '=', $item['type']], ['pipe_id', '=', $item['data_id']], ['agent_id', '<>', 0]])->count(); //是否未分享 //$item['new'] = in_array($item['id'], $content_id) ? false : true; switch ($item['type']) { case 'Article': $find = Article::where([ ['root_id', '=', $token['root_org']], ['delete_time', '=', 0], ['publish', '=', 1], ['id', '=', $item['data_id']] ])->field('id,title,cover_img')->find(); $item['content_id'] = $find['id']; $item['title'] = $find['title']; $item['cover'] = $find['cover_img']; break; case 'MaterialCase': $find = MaterialCase::where([ ['root_id', '=', $token['root_org']], ['del', '=', 0], ['publish', '=', 1], ['id', '=', $item['data_id']] ])->field('id,title,cover_img')->find(); $item['content_id'] = $find['id']; $item['title'] = $find['title']; $item['cover'] = $find['cover_img']; break; case 'Video': $find = Video::where([ ['root_id', '=', $token['root_org']], ['delete_time', '=', 0], ['publish', '=', 1], ['id', '=', $item['data_id']] ])->field('id,title,video_url')->find(); $item['content_id'] = $find['id']; $item['title'] = $find['title']; $item['cover'] = $find['video_url']; break; case 'CompanyStrength': $find = CompanyStrength::where([ ['root_id', '=', $token['root_org']], ['del', '=', 0], ['publish', '=', 1], ['id', '=', $item['data_id']] ])->field('id,title,cover,pics,difference')->find(); $item['content_id'] = $find['id']; $item['title'] = $find['title']; $item['cover'] = $find['cover']; $item['pics'] = $find['pics']; $item['difference'] = $find['difference']; break; case 'MaterialEvidence': $find = MaterialEvidence::where([ ['root_id', '=', $token['root_org']], ['del', '=', 0], ['publish', '=', 1], ['id', '=', $item['data_id']] ])->field('id,title,pics,difference')->find(); $item['content_id'] = $find['id']; $item['title'] = $find['title']; $item['cover'] = $find['pics']; $item['difference'] = $find['difference']; break; case 'Building': $find = Building::where([ ['root_id', '=', $token['root_org']], ['del', '=', 0], ['id', '=', $item['data_id']] ])->field('id,community_id,name,cover')->find(); $item['content_id'] = $find['id']; $item['title'] = $find['name']; $item['cover'] = $find['cover']; break; case 'AgentArticle': $find = AgentArticle::where([ ['root_id', '=', $token['root_org']], ['id', '=', $item['data_id']] ])->field('id,title,file,type')->find(); $item['content_id'] = $find['id']; $item['title'] = $find['title']; $item['cover'] = $find['file']; $item['file_type'] = $find['type']; break; default: break; } } //查询分享的奖励规则 $rule_data = AgentType::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $agtdata['type']]])->value('count'); $rule_list = !empty($rule_data) ? json_decode($rule_data, true) : ''; $reward_rules = []; if (!empty($rule_list)) { $reward_rules[] = '1 推荐一个客户' . $rule_list['putong_integral'] . '积分'; $reward_rules[] = '2 分享一条内容' . $rule_list['ptagentshare_integral'] . '积分'; $reward_rules[] = '3 分享内容获得一条线索' . $rule_list['produceclue_integral'] . '积分'; $reward_rules[] = '4 客户到店' . $rule_list['daodian_integral'] . '积分'; $reward_rules[] = '5 客户签单' . $rule_list['jiaoding_integral'] . '积分'; $reward_rules[] = '6 客户转单' . $rule_list['qiandan_integral'] . '积分'; } //$reward_rules = !empty($rule_list) ? '分享一条内容'.$rule_list['ptagentshare_integral'].'积分' : ''; $list = ['list' => $data, 'reward_rules' => $reward_rules]; return json(['code' => 0, 'data' => $list]); } /** * 公司上传内容列表 */ public function company_artlist() { $agtdata = $this->request->agtdata; $token = $this->request->token; $param = $this->request->only(['page' => 1, 'limit' => 10]); $where = [ ['root_id', '=', $token['root_org']], ['from', '=', 2], ['disable', '=', 0] ]; $vrObj = new Vr(); $list = AgentArticle::where($where)->field('id,title,file,type,content,addtime,vr_group_ids')->page($param['page'], $param['limit'])->select()->toArray(); $show_url = config('app.vr_show_domain'); foreach ($list as $key => $val) { //分享量 $list[$key]['share_num'] = ShareLog::where([['type', '=', 'AgentArticle'], ['data_id', '=', $val['id']], ['agent_id', '<>', 'NULL']])->count(); //线索 $list[$key]['clue_num'] = CustomerClue::where([['pipe_type', '=', 'AgentArticle'], ['pipe_id', '=', $val['id']], ['agent_id', '<>', 0]])->count(); $list[$key]['files'] = ''; if ($val['type'] == 2) $list[$key]['cover'] = !empty($val['file'][0]) ? $val['file'][0] : ''; if ($val['type'] == 3) { $list[$key]['files'] = !empty($val['file'][0]) ? str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $val['file'][0]) : ''; $list[$key]['cover'] = !empty($val['file'][0]) ? $vrObj->getFirstImg(str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $val['file'][0])) : ''; } if ($val['type'] == 4) { $group = VrGroup::where('id', $val['vr_group_ids'])->field('pic_path')->findOrEmpty(); $list[$key]['cover'] = $group->isEmpty() ? '' : $group->pic_path; } $list[$key]['vr_group_url'] = ''; if ($val['type'] == 4) { $group = VrGroup::where('id', $val['vr_group_ids'])->field('pic_path,sid')->findOrEmpty(); if (!$group->isEmpty()) { $list[$key]['vr_group_url'] = $show_url . $group->sid; $list[$key]['file_img'] = $group->pic_path; } $data['file_img'] = $group->isEmpty() ? '' : $group->pic_path; } } //查询分享的奖励规则 $rule_data = AgentType::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $agtdata['type']]])->value('count'); $rule_list = !empty($rule_data) ? json_decode($rule_data, true) : ''; $reward_rules = []; if (!empty($rule_list)) { $reward_rules[] = '1 推荐一个客户' . $rule_list['putong_integral'] . '积分'; $reward_rules[] = '2 分享一条内容' . $rule_list['ptagentshare_integral'] . '积分'; $reward_rules[] = '3 分享内容获得一条线索' . $rule_list['produceclue_integral'] . '积分'; $reward_rules[] = '4 客户到店' . $rule_list['daodian_integral'] . '积分'; $reward_rules[] = '5 客户签单' . $rule_list['jiaoding_integral'] . '积分'; $reward_rules[] = '6 客户转单' . $rule_list['qiandan_integral'] . '积分'; } $list = ['list' => $list, 'reward_rules' => $reward_rules]; return json(['code' => 0, 'data' => $list, 'msg' => '获取成功']); } /** * 查询我是否还有未分享的任务(用于显示红点图标) */ public function newShareArticle() { $agtdata = $this->request->agtdata; $token = $this->request->token; $where = [ ['root_id', '=', $token['root_org']], ['employee_id', '=', $agtdata['agent_employee_id']], ['disable', '=', 0], ]; $data = AgentShareContent::where($where)->select(); $count = 0; foreach ($data as $item) { $log = ShareLog::where(['agent_id' => $agtdata['id'], 'type' => $item['type'], 'data_id' => $item['data_id']])->count(); !empty($log) ? $count += $log : 0; } return json(['code' => 0, 'new' => empty($count) ? false : true]); } /** * 分享任务详情 */ public function share_article_details() { $id = $this->request->param('id'); $from = $this->request->param('from'); $token = $this->request->token; $agtdata = $this->request->agtdata; $where = [ ['root_id', '=', $token['root_org']], ['id', '=', $id], ['disable', '=', 0] ]; if (empty($from)) $where[] = ['employee_id', '=', $agtdata['agent_employee_id']]; $data = AgentArticle::where($where)->field('id,title,file,content,talkskill,type,vr_group_ids')->find(); //var_dump($data);exit; $vrObj = new Vr(); $data['file_img'] = $data['files'] = ''; $show_url = config('app.vr_show_domain'); if ($data['type'] == 3) { $data['files'] = !empty($data['file'][0]) ? str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $data['file'][0]) : ''; $data['file_img'] = !empty($data['file'][0]) ? $vrObj->getFirstImg(str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $data['file'][0])) : ''; } $data['vr_group_url'] = ''; if ($data['type'] == 4) { $group = VrGroup::where('id', $data['vr_group_ids'])->field('pic_path,sid')->findOrEmpty(); if (!$group->isEmpty()) { $data['vr_group_url'] = $show_url . $group->sid; $data['file_img'] = $group->pic_path; } $data['file_img'] = $group->isEmpty() ? '' : $group->pic_path; } return json(['code' => 0, 'data' => $data]); } /** * 上传分享记录 * type:Article文章、Video视频、MaterialCase案例、CompanyStrength实力、MaterialEvidence客户好评、Building楼盘、AgentArticle业务员上传 */ public function upload_share_log() { $param = $this->request->only(['article_id' => 0, 'file_media_id', 'remarks', 'type' => '']); $token = $this->request->token; $agtdata = $this->request->agtdata; $param['root_id'] = $token['root_org']; $param['employee_id'] = $agtdata['agent_employee_id']; $param['uid'] = $agtdata['uid']; $param['addtime'] = time(); //上传文件 if (!empty($param['file_media_id'])) { $file_media_id = []; foreach ($param['file_media_id'] as $k => $v) { $file_media_id[$k] = [ 'serverId' => !empty($v['serverId']) ? $v['serverId'] : '', 'img' => !empty($v['img']) ? str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $v['img']) : '' ]; } $param['down_status'] = 1; $param['file_media_id'] = json_encode($file_media_id); } $add = AgentShareLog::insert($param); Console::call('download', ['agent_share_log']); if ($add) { return json(['code' => 0, 'msg' => '保存成功']); } else { return json(['code' => 1, 'msg' => '保存失败']); } } /** * 经纪人分享文章成功回调 * type:Article文章、Video视频、MaterialCase案例、CompanyStrength实力、MaterialEvidence客户好评、Building楼盘、AgentArticle上传分享内容 */ public function agent_sharelog($article_id, $type) { if (!isset($article_id) || empty($article_id)) return json(['code' => self::error_msg, 'msg' => '缺少分享参数']); $token = $this->request->token; $agtdata = $this->request->agtdata; ShareLog::create([ 'employee_id' => $agtdata['agent_employee_id'], 'type' => $type, 'data_id' => $article_id, 'agent_id' => $agtdata['id'] ]); return json(['code' => self::success, 'msg' => '记录添加成功']); } /** * 经纪人上传截图列表 */ public function uploadlog_list() { $param = $this->request->only(['page' => 1, 'limit' => 10]); $token = $this->request->token; $agtdata = $this->request->agtdata; $agt_typename = !empty($agtdata['type']) ? AgentType::where([['root_id', '=', $agtdata['root_id']], ['id', '=', $agtdata['type']]])->value('type_name') : ''; $list = AgentShareLog::with(['user', 'agentUser'])->where([['root_id', '=', $agtdata['root_id']], ['uid', '=', $agtdata['uid']]]) ->field('id,employee_id,uid,img,addtime,remarks,status')->order('id desc')->page($param['page'], $param['limit'])->select()->toArray(); foreach ($list as $key => $val) { $list[$key]['type_name'] = $agt_typename; } return json(['code' => 0, 'data' => $list, 'msg' => '获取成功']); } /** * 经纪人线索列表 * state:0、1、2 * phone:has、hasno * getDate:Y-m-d */ public function clue_list($page, $limit, $getDate = '', $state = '', $phone = '') { $token = $this->request->token; $agtdata = $this->request->agtdata; $count = [ 'count_clue' => 0, 'creat_clue' => 0, 'day_clue' => 0 ]; //总线索 $count['count_clue'] = CustomerClue::where([['agent_id', '=', $agtdata['id']]])->count(); //已建档 $count['creat_clue'] = CustomerClue::where([['agent_id', '=', $agtdata['id']], ['state', '=', 1]])->count(); //今日线索 $count['day_clue'] = CustomerClue::where([['agent_id', '=', $agtdata['id']], ['addtime', 'between', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')]]])->count(); $condition = [ ['agent_id', '=', $agtdata['id']], //['pipe_type', '=', 'agentArticle'] ]; if (isset($state)) $condition[] = ['state', '=', $state]; if (!empty($getDate)) $condition[] = ['addtime', 'like', $getDate . '%']; if ($phone == 'has') $condition[] = ['phone', 'not null', '']; elseif ($phone == 'hasno') $condition[] = ['phone', 'null', '']; $data = CustomerClue::field('id,uid,phone,addtime,updatetime,state')->with(['user'])->withCount(['subscribe' => function ($query) use ($token) { $query->where('root_id', $token['root_org']); }, 'footprints'])->where($condition)->order('updatetime desc')->page($page, $limit)->select(); foreach ($data as $k => $v) { $data[$k]['updatetime'] = $v['updatetime'] ? explode(' ', $v['updatetime']) : explode(' ', $v['addtime']); } return json(['code' => 0, 'msg' => '获取成功', 'data' => ['count' => $count, 'data' => $data]]); } /** * 经纪人查看客户访问记录(👣) */ public function footprints($id) { $token = $this->request->token; $agtdata = $this->request->agtdata; $where = [ ['id', '=', $id], ['agent_id', '=', $agtdata['id']] ]; $clue = CustomerClue::where($where)->find(); if (empty($clue)) return json(['code' => self::success, 'data' => [], 'msg' => '获取成功']); // 查询条件设置 $condition = [ ['addtime', '>=', $clue->addtime], ['uid', '=', $clue->uid], ['org_id', 'in', orgSubIds($token['root_org'])] ]; $data = Footprints::where($condition)->field('id,pipe_type,reg_info,visit_due_time,addtime,uid')->order('addtime desc')->select(); if ($data) { //装修案例预约记录 $w2[] = ['uid', 'in', array_column($data->toArray(), 'uid')]; $w2[] = ['root_id', '=', $token['root_org']]; $sub = CustomerSubscribe::where($w2)->field('sucai_id,uid')->select()->toArray(); foreach ($data as $k => $v) { if ($v['pipe_type'] == 'materialCase') { $data[$k]['subscribe'] = 0; foreach ($sub as $k2 => $v2) { $json = json_decode($v->getData('reg_info'), true); if ($v2['uid'] == $v['uid'] && $v2['sucai_id'] == $json['id']) { $data[$k]['subscribe'] = 1; break; } } } else { $data[$k]['subscribe'] = 0; } } } return json(['code' => self::success, 'data' => $data, 'msg' => '获取成功']); } /** * 兑换奖品列表 */ public function prize_list() { $token = $this->request->token; $agtdata = $this->request->agtdata; $agt_integral = AgentIntegralLog::where([['agt_id', '=', $agtdata['id']]])->order('id desc')->value('now_integral'); $list = AgentPrize::where([['root_id', '=', $token['root_org']], ['status', '=', 1]])->select()->toArray(); $agtemp = Employee::where([['root_id', '=', $token['root_org']], ['id', '=', $agtdata['agent_employee_id']], ['state', '=', '在职']])->field('id,phone,wx')->find(); $data = ['list' => $list, 'agt_integral' => $agt_integral ? $agt_integral : 0, 'agtemp' => $agtemp]; return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']); } /** * 兑换奖品 */ public function exchange_prize() { $token = $this->request->token; $agtdata = $this->request->agtdata; $param = $this->request->only(['id']); // 积分兑换的商品 $prize_data = AgentPrize::where([['root_id', '=', $token['root_org']], ['id', '=', $param['id']]])->find(); // 经纪人积分获取 $agtIntegral = AgentIntegralLog::where([['agt_id', '=', $agtdata['id']]])->order('id desc')->value('now_integral'); if ($prize_data['integral'] > $agtIntegral) return json(['code' => 1, 'msg' => '当前积分不能兑换此奖品']); $bili = Db::table('fl_credits_setting')->where([['code', '=', 'reduce_integral'], ['root_id', '=', $token['root_org']]])->value('value'); $bili = !empty($bili) ? json_decode($bili, true) : ['jifen' => 1, 'money' => 1]; Db::startTrans(); try { $add = [ 'agent_id' => $agtdata['id'], 'empid' => $agtdata['agent_employee_id'], 'integral' => $prize_data['integral'], 'root_id' => $agtdata['root_id'], 'good_id' => $param['id'] ]; $ms = AgentApplyIntegral::insertGetId($add); $money = bcdiv($prize_data['integral'], $bili['jifen'], 1) * $bili['money']; // 添加积分兑换记录 AgentIntegralLog::create([ 'agt_id' => $agtdata['id'], 'addtime' => time(), 'integral' => $prize_data['integral'], 'type' => 2, 'money' => $money, 'now_integral' => $agtIntegral - $prize_data['integral'] ]); } catch (\Exception $e) { // 回滚事务 Db::rollback(); return json(['code' => 1, 'msg' => '兑换失败']); } Db::commit(); $msg = '经纪人【' . $agtdata['agent_name'] . '】提交兑换奖品申请'; event(new Msg($agtdata['agent_employee_id'], $msg, 'agentWipen', $ms)); return json(['code' => 0, 'msg' => '兑换申请已提交']); } /** * 经纪人端是否展示【推荐分享】板块(后台设置开关) */ public function show_recommend_share() { $token = $this->request->token; $setdata = Setting::where([['root_id', '=', $token['root_org']], ['name', '=', 'agentEmployeeAdd']])->value('content'); $setdata = $setdata ?: 'true'; return json(['code' => 0, 'data' => $setdata]); } }