|
@@ -4,116 +4,221 @@
|
|
|
namespace app\admin\service\consultant;
|
|
|
|
|
|
|
|
|
+use app\admin\service\member\MemberService;
|
|
|
use app\model\Consultant;
|
|
|
use app\model\MarketCustomer;
|
|
|
+use app\model\MarketCustomerFollow;
|
|
|
+use app\model\MarketCustomerLogs;
|
|
|
use app\model\Member;
|
|
|
+use app\model\SysDept;
|
|
|
use support\Db;
|
|
|
use support\exception\BusinessException;
|
|
|
+use support\Request;
|
|
|
+use Tinywan\Jwt\JwtToken;
|
|
|
|
|
|
class CustomService
|
|
|
{
|
|
|
+ const DIFF_TIME = (60 * 60 * 24 * 30);
|
|
|
/**
|
|
|
- * Notes: 处理参数
|
|
|
+ * Notes: 返回选项配置信息
|
|
|
* User: yb
|
|
|
- * Date: 2024/8/6
|
|
|
- * Time: 11:43
|
|
|
- * @param $data
|
|
|
- * @return mixed
|
|
|
+ * Date: 2024/8/5
|
|
|
+ * Time: 16:05
|
|
|
+ * @return array
|
|
|
*/
|
|
|
- protected static function handleNumParams($data)
|
|
|
+ public static function config()
|
|
|
{
|
|
|
- $setField = ['require_area', 'requirement', 'age_range', 'focus', 'region'];
|
|
|
- foreach ($data as $k => $v) {
|
|
|
- if (in_array($k, $setField)) {
|
|
|
- if (is_numeric($v)) {
|
|
|
- $data[$k] = $v + 1;
|
|
|
- } else {
|
|
|
- $data[$k] = null;
|
|
|
- }
|
|
|
+ return MarketCustomer::config();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function commonSearch($params)
|
|
|
+ {
|
|
|
+ $where = [];
|
|
|
+ $currentTime = time();
|
|
|
+ $diffNums = self::DIFF_TIME;
|
|
|
+ $deptIds = TeamService::getIdsByUser();
|
|
|
+ if (false === $deptIds) {
|
|
|
+ //无权限
|
|
|
+ $where[] = ['dept_id', '=', 0];
|
|
|
+ } else if (is_array($deptIds)) {
|
|
|
+ //指定团队下的权限
|
|
|
+ if (!empty($params['dept_id'])) {
|
|
|
+ $deptId = end($params['dept_id']);
|
|
|
+ $deptIds = SysDept::where('dept_super_path','like', "%/{$deptId}/%")->where('dept_category', '=', TeamService::DEPT_CATEGORY)->pluck('dept_id')->toArray();
|
|
|
+ }
|
|
|
+ $where[] = [function($query) use ($deptIds) {
|
|
|
+ $query->whereIn('dept_id', $deptIds);
|
|
|
+ }];
|
|
|
+ } else {
|
|
|
+ if (!empty($params['dept_id'])) {
|
|
|
+ $deptId = end($params['dept_id']);
|
|
|
+ $deptIds = SysDept::where('dept_super_path','like', "%/{$deptId}/%")->where('dept_category', '=', TeamService::DEPT_CATEGORY)->pluck('dept_id')->toArray();
|
|
|
+ $where[] = [function($query) use ($deptIds) {
|
|
|
+ $query->whereIn('dept_id', $deptIds);
|
|
|
+ }];
|
|
|
}
|
|
|
}
|
|
|
- if ($data['type'] == 1) {
|
|
|
- //来电
|
|
|
- if (!empty($data['area'])) {
|
|
|
- $area = $data['area'];
|
|
|
- $requireArea = 1;
|
|
|
- if ($area >= 40 && $area < 51) {
|
|
|
- $requireArea = 2;
|
|
|
- } else if ($area >= 51 && $area < 61) {
|
|
|
- $requireArea = 3;
|
|
|
- } else if ($area >= 61 && $area < 81) {
|
|
|
- $requireArea = 4;
|
|
|
- } else if ($area >= 81 && $area < 101) {
|
|
|
- $requireArea = 5;
|
|
|
- } else if ($area >= 101 && $area < 121) {
|
|
|
- $requireArea = 6;
|
|
|
- } else if ($area >= 121 && $area < 151) {
|
|
|
- $requireArea = 7;
|
|
|
- } else if ($area >= 151) {
|
|
|
- $requireArea = 8;
|
|
|
- }
|
|
|
- $data['require_area'] = $requireArea;
|
|
|
- $data['age_range'] = null;
|
|
|
+
|
|
|
+ if (!empty($params['name'])) {
|
|
|
+ $keywords = $params['name'];
|
|
|
+ $where[] = [function($query) use ($keywords) {
|
|
|
+ $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($params['report_status'])) {
|
|
|
+ if ($params['report_status'] == -1) {
|
|
|
+ //待审核
|
|
|
+ $where[] = ['check_status', '=', 1];
|
|
|
} else {
|
|
|
- $data['area'] = null;
|
|
|
+ $where[] = ['check_status', '=', 2];
|
|
|
}
|
|
|
- } else {
|
|
|
- //来访
|
|
|
- $data['area'] = null;
|
|
|
+ if ($params['report_status'] == 1) {
|
|
|
+ //已报备
|
|
|
+ $where[] = [function($query) use ($diffNums, $currentTime) {
|
|
|
+ $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1);
|
|
|
+ }];
|
|
|
+ } else if ($params['report_status'] == 3) {
|
|
|
+ //已锁定
|
|
|
+ $where[] = [function($query) {
|
|
|
+ $query->whereIn('current_status', [2,3,4]);
|
|
|
+ }];
|
|
|
+ } else if ($params['report_status'] == 2) {
|
|
|
+ //已失效
|
|
|
+ $where[] = [function($query) use ($diffNums, $currentTime){
|
|
|
+ $query->orWhere('current_status', '=', -1)->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 1 AND check_status = 2)");
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!empty($params['consultant_name'])) {
|
|
|
+ $consultantIds = Consultant::where('name', 'like', "%{$params['consultant_name']}%")->pluck('id');
|
|
|
+ $where[] = [function($query) use ($consultantIds) {
|
|
|
+ $query->whereIn('consultant_id', $consultantIds);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($params['type'])) {
|
|
|
+ $where[] = ['type', '=', $params['type']];
|
|
|
}
|
|
|
- if (!empty($data['visit_time'])) {
|
|
|
- $data['visit_time'] = strtotime($data['visit_time']);
|
|
|
+ if (!empty($params['visit_time'])) {
|
|
|
+ $datetime = $params['visit_time'];
|
|
|
+ $datetime[0] = strtotime($datetime[0]);
|
|
|
+ $datetime[1] = strtotime($datetime[1]);
|
|
|
+ $where[] = [function($query) use ($datetime) {
|
|
|
+ $query->whereBetween('visit_time', $datetime);
|
|
|
+ }];
|
|
|
}
|
|
|
- if (empty($data['visit_type'])) {
|
|
|
- $data['visit_type'] = null;
|
|
|
+ if (!empty($params['visit_date'])) {
|
|
|
+ $visitDate = $params['visit_date'];
|
|
|
+ $visitDate[0] = strtotime($visitDate[0].' 00:00:00');
|
|
|
+ $visitDate[1] = strtotime($visitDate[1].' 23:59:59');
|
|
|
+ $where[] = [function($query) use ($visitDate) {
|
|
|
+ $query->whereBetween('visit_time', $visitDate);
|
|
|
+ }];
|
|
|
}
|
|
|
- if (empty($data['level'])) {
|
|
|
- $data['level'] = null;
|
|
|
+ if (!empty($params['created_date'])) {
|
|
|
+ $createdDate = $params['created_date'];
|
|
|
+ $createdDate[0] = strtotime($createdDate[0].' 00:00:00');
|
|
|
+ $createdDate[1] = strtotime($createdDate[1].' 23:59:59');
|
|
|
+ $where[] = [function($query) use ($createdDate) {
|
|
|
+ $query->whereBetween('created_at', $createdDate);
|
|
|
+ }];
|
|
|
}
|
|
|
- if (empty($data['purpose'])) {
|
|
|
- $data['purpose'] = null;
|
|
|
+
|
|
|
+ if (!empty($params['current_status'])) {
|
|
|
+ $currentStatus = $params['current_status'];
|
|
|
+ $where[] = [function($query) use ($currentStatus){
|
|
|
+ $query->where('current_status', '=', $currentStatus)->where('check_status', '=', 2);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($params['consultant_id'])) {
|
|
|
+ $where[] = ['consultant_id', '=', $params['consultant_id']];
|
|
|
+ }
|
|
|
+ if (!empty($params['custom'])) {
|
|
|
+ $keywords = $params['custom'];
|
|
|
+ $where[] = [function($query) use ($keywords) {
|
|
|
+ $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($params['check_status'])) {
|
|
|
+ $where[] = ['check_status', '=', $params['check_status']];
|
|
|
+ }
|
|
|
+ if (!empty($params['stat_report_status'])) {
|
|
|
+ //已报备 1
|
|
|
+ $statReportStatus = $params['stat_report_status'];
|
|
|
+ if ($statReportStatus == 1) {
|
|
|
+ $where[] = [function($query) use ($diffNums, $currentTime) {
|
|
|
+ $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0)")->where('current_status', '=', 1)->where('check_status', '=', 2);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ //已锁定 2
|
|
|
+ if ($statReportStatus == 2) {
|
|
|
+ $where[] = [function($query) {
|
|
|
+ $query->whereIn('current_status', [2,3,4])->where('check_status', '=', 2);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ //已失效 -1
|
|
|
+ if ($statReportStatus == -1) {
|
|
|
+ $where[] = [function($query) use ($diffNums, $currentTime){
|
|
|
+ $query->orWhere('current_status', '=', -1)->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND current_status = 1 AND check_status = 2)")->orWhere('check_status', '=', -1);
|
|
|
+ }];
|
|
|
+ }
|
|
|
}
|
|
|
- return $data;
|
|
|
+ return $where;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * Notes: 返回选项配置信息
|
|
|
+ * Notes: 列表
|
|
|
* User: yb
|
|
|
- * Date: 2024/8/5
|
|
|
- * Time: 16:05
|
|
|
- * @return array
|
|
|
+ * Date: 2024/8/6
|
|
|
+ * Time: 15:07
|
|
|
+ * @param Request $request
|
|
|
*/
|
|
|
- public static function config()
|
|
|
+ public static function index(Request $request)
|
|
|
{
|
|
|
- $config = [
|
|
|
- 'requirement' => ['康养公寓', '颐养公寓', '商业', '其他'],
|
|
|
- 'require_area' => ['40㎡以下', '40㎡-50㎡', '51㎡-60㎡', '61㎡-80㎡', '81㎡-100㎡', '101㎡-120㎡', '121㎡-150㎡', '150㎡以上'],
|
|
|
- 'age_range' => ['40岁以下', '21岁-30岁', '31岁-40岁', '41岁-50岁', '51岁-60岁', '61岁-80岁', '80岁以上'],
|
|
|
- 'visit_type' => [
|
|
|
- 'common' => ['', '路过', '分销', '朋友介绍', '安居客、房天下', '微信朋友圈', '户外', '微信公众号', '工地围挡', '老带新', '圈层营销', '自拓'],
|
|
|
- 'type1' => ['100' => '派单', '101' => '城市展厅', '102' => '活动'],
|
|
|
- 'type2' => ['200' => '内渠', '201' => '自渠']
|
|
|
- ],
|
|
|
- 'focus' => ['位置', '交通', '配套', '价格', '品牌', '适老化', '物业', '医疗', '运营'],
|
|
|
- 'region' => ['唐冶', '历城', '历下', '高新', '天桥', '槐荫', '市中', '其他'],
|
|
|
- 'purpose' => ['自买自用', '自买他用'],
|
|
|
- 'level' => ['A类', 'B类', 'C类', 'D类'],
|
|
|
- 'current_status' => ['首次到访', '已到访', '无效客户', '已成交'],
|
|
|
+ $format = $request->get('format', 'normal');
|
|
|
+ $limit = (int)$request->get('pageSize', $format === 'tree' ? 1000 : 10);
|
|
|
+ $limit = $limit <= 0 ? 10 : $limit;
|
|
|
+ $params = $request->get();
|
|
|
+ $page = (int)$request->get('page');
|
|
|
+ $page = $page > 0 ? $page : 1;
|
|
|
+ $currentTime = time();
|
|
|
+ $diffNums = self::DIFF_TIME;
|
|
|
+ $where = self::commonSearch($params);
|
|
|
+ $paginator = MarketCustomer::where($where)->orderBy('visit_time', 'desc')->paginate($limit, '*', 'page', $page);
|
|
|
+ $total = $paginator->total();
|
|
|
+ $items = $paginator->items();
|
|
|
+ if (!empty($items)) {
|
|
|
+ $consultantKeys = [];
|
|
|
+ $consultantIds = $paginator->pluck('consultant_id')->toArray();
|
|
|
+ if (!empty($consultantIds)) {
|
|
|
+ //去重
|
|
|
+ $consultantIds = array_unique($consultantIds);
|
|
|
+ //排序
|
|
|
+ $consultantIds = array_values($consultantIds);
|
|
|
+ //查询顾问信息
|
|
|
+ $consultantList = Consultant::whereIn('id', $consultantIds)->select(['id', 'name', 'mobile'])->get();
|
|
|
+ if (!$consultantList->isEmpty()) {
|
|
|
+ foreach ($consultantList->toArray() as $consultantItem) {
|
|
|
+ $consultantKeys[$consultantItem['id']] = $consultantItem;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $teamKeys = TeamService::getItemKeys();
|
|
|
+ foreach ($items as &$item) {
|
|
|
+ $visitTime = $item->visit_time;
|
|
|
+ $visitTimeStamp = strtotime($visitTime);
|
|
|
+ $diff = ($visitTimeStamp + $diffNums) - $currentTime;
|
|
|
+ $item->diff_nums = ($diff > 0) ? $diff : 0;
|
|
|
+ $item->consultant_name = $consultantKeys[$item->consultant_id]['name'] ?? '';
|
|
|
+ $item->consultant_mobile = $consultantKeys[$item->consultant_id]['mobile'] ?? '';
|
|
|
+ $item->team_name = TeamService::getTeamName($teamKeys, $item->dept_id);
|
|
|
+ $item->mask_mobile = self::handlePhone($item->mobile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $data = [
|
|
|
+ 'total' => $total,
|
|
|
+ 'rows' => $items
|
|
|
];
|
|
|
- //处理config中的visit_type
|
|
|
- $visitType = [];
|
|
|
- $visitTypeCommon = $config['visit_type']['common'];
|
|
|
- $visitType1 = $config['visit_type']['type1'];
|
|
|
- $visitType2 = $config['visit_type']['type2'];
|
|
|
- foreach ($visitTypeCommon as $key => $item) {
|
|
|
- if (!empty($item)) {
|
|
|
- $visitType['type1']["{$key}"] = $item;
|
|
|
- $visitType['type2']["{$key}"] = $item;
|
|
|
- }
|
|
|
- }
|
|
|
- $visitType['type1'] += $visitType1;
|
|
|
- $visitType['type2'] += $visitType2;
|
|
|
- $config['visit_type'] = $visitType;
|
|
|
- return $config;
|
|
|
+ return json_success('success', $data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -124,10 +229,10 @@ class CustomService
|
|
|
*/
|
|
|
public static function add($params)
|
|
|
{
|
|
|
- $params = self::handleNumParams($params);
|
|
|
+ $params = MarketCustomer::handleNumParams($params);
|
|
|
$mobile = $params['mobile'];
|
|
|
//查询客户手机号是否已经存在
|
|
|
- if (MarketCustomer::where('mobile', $mobile)->exists()) {
|
|
|
+ if (MarketCustomer::checkCustomExists($mobile)) {
|
|
|
return json_fail('客户已经登记过了');
|
|
|
}
|
|
|
//查询顾问信息
|
|
@@ -153,8 +258,11 @@ class CustomService
|
|
|
'purpose' => $params['purpose'] ?? null,
|
|
|
'level' => $params['level'] ?? null,
|
|
|
'type' => $params['type'] ?? null,
|
|
|
+ 'check_status' => 1,
|
|
|
'visit_time' => $params['visit_time'] ?? null,
|
|
|
'note' => $params['note'] ?? '',
|
|
|
+ 'current_status' => $params['current_status'] ?? null,
|
|
|
+ 'created_at' => time()
|
|
|
];
|
|
|
|
|
|
Db::beginTransaction();
|
|
@@ -162,7 +270,27 @@ class CustomService
|
|
|
//查询会员表中是否存在该客户
|
|
|
// $memberId = Member::where('member_mobile', $insertData['mobile'])->value('member_id');
|
|
|
// if (empty($memberId)) {
|
|
|
-// //新增会员信息
|
|
|
+// $result = MemberService::add([
|
|
|
+// 'account_name'=> $insertData['name'],
|
|
|
+// 'member_category' => '售房客户',
|
|
|
+// 'mobile' => $insertData['mobile'],
|
|
|
+// 'source' => 'HOUSE']);
|
|
|
+// $code = $result->getStatusCode();
|
|
|
+// if ($code == 200) {
|
|
|
+// $content = $result->rawBody();
|
|
|
+// if (is_json($content)) {
|
|
|
+// $content = json_decode($content, 1);
|
|
|
+// if ($content['code'] == 200) {
|
|
|
+// $memberId = $content['data']['member_id'];
|
|
|
+// } else {
|
|
|
+// throw new BusinessException('新增会员失败');
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// throw new BusinessException('新增会员失败');
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// throw new BusinessException('新增会员失败');
|
|
|
+// }
|
|
|
// }
|
|
|
// $insertData['member_id'] = $memberId;
|
|
|
$customId = MarketCustomer::insertGetId($insertData);
|
|
@@ -177,4 +305,564 @@ class CustomService
|
|
|
return json_fail('添加失败');
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 编辑客户
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/6
|
|
|
+ * Time: 16:53
|
|
|
+ * @param $params
|
|
|
+ */
|
|
|
+ public static function update($params)
|
|
|
+ {
|
|
|
+ $params = MarketCustomer::handleNumParams($params);
|
|
|
+ $mobile = $params['mobile'];
|
|
|
+ //查询客户手机号是否已经存在
|
|
|
+ if (MarketCustomer::checkCustomExists($mobile, $params['id'])) {
|
|
|
+ return json_fail('客户已经登记过了');
|
|
|
+ }
|
|
|
+ $updateData = [
|
|
|
+ 'name' => $params['name'],
|
|
|
+ 'mobile' => $params['mobile'],
|
|
|
+ 'gender' => $params['gender'] ?? null,
|
|
|
+ 'visit_type' => $params['visit_type'] ?? null,
|
|
|
+ 'require_area' => $params['require_area'] ?? null,
|
|
|
+ 'area' => $params['area'] ?? null,
|
|
|
+ 'requirement' => $params['requirement'] ?? null,
|
|
|
+ 'age_range' => $params['age_range'] ?? null,
|
|
|
+ 'focus' => $params['focus'] ?? null,
|
|
|
+ 'region' => $params['region'] ?? null,
|
|
|
+ 'purpose' => $params['purpose'] ?? null,
|
|
|
+ 'level' => $params['level'] ?? null,
|
|
|
+ 'type' => $params['type'] ?? null,
|
|
|
+ 'note' => $params['note'] ?? '',
|
|
|
+ 'current_status' => $params['current_status'] ?? null,
|
|
|
+ 'updated_at' => time()
|
|
|
+ ];
|
|
|
+ if (!empty($params['update_visit_time'])) {
|
|
|
+ if ($params['update_visit_time'] == 1) {
|
|
|
+ $updateData['visit_time'] = $params['visit_time'] ?? null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $result = MarketCustomer::where('id', $params['id'])->update($updateData);
|
|
|
+ if ($result) {
|
|
|
+ return json_success('编辑成功');
|
|
|
+ } else {
|
|
|
+ return json_fail('编辑失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 删除客户
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/2
|
|
|
+ * Time: 13:33
|
|
|
+ * @param $ids
|
|
|
+ * @return \support\Response
|
|
|
+ */
|
|
|
+ public static function delete($ids)
|
|
|
+ {
|
|
|
+ if (!$ids) {
|
|
|
+ return json_fail("数据错误");
|
|
|
+ }
|
|
|
+ if (!is_array($ids)) {
|
|
|
+ $ids = [$ids];
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (is_array($ids)) {
|
|
|
+ MarketCustomer::whereIn('id', $ids)->delete();
|
|
|
+ } else {
|
|
|
+ MarketCustomer::where('id', $ids)->delete();
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return json_fail('删除失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_success('删除成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 跟进记录
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/7
|
|
|
+ * Time: 9:00
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public static function follow(Request $request)
|
|
|
+ {
|
|
|
+ $format = $request->get('format', 'normal');
|
|
|
+ $limit = (int)$request->get('pageSize', $format === 'tree' ? 1000 : 10);
|
|
|
+ $limit = $limit <= 0 ? 10 : $limit;
|
|
|
+ $params = $request->get();
|
|
|
+ $page = (int)$request->get('page');
|
|
|
+ $page = $page > 0 ? $page : 1;
|
|
|
+ $where = [];
|
|
|
+ $whereCustom = [];
|
|
|
+ $whereConsultant = [];
|
|
|
+ if (!empty($params['market_customer_id'])) {
|
|
|
+ $marketCustomerIds = $params['market_customer_id'];
|
|
|
+ $where[] = [function($query) use ($marketCustomerIds) {
|
|
|
+ $query->whereIn('market_customer_id', $marketCustomerIds);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($params['consultant_id'])) {
|
|
|
+ $consultantId = $params['consultant_id'];
|
|
|
+ $where[] = [function($query) use ($consultantId) {
|
|
|
+ $query->whereIn('consultant_id', $consultantId);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($params['follow_way'])) {
|
|
|
+ $where[] = ['follow_way', '=', $params['follow_way']];
|
|
|
+ }
|
|
|
+ if (!empty($params['custom'])) {
|
|
|
+ $custom = $params['custom'];
|
|
|
+ $whereCustom[] = [function($query) use ($custom) {
|
|
|
+ $query->orWhere('name', 'like', "%{$custom}%")->orWhere('mobile', 'like', "%{$custom}%");
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($params['consultant'])) {
|
|
|
+ $consultant = $params['consultant'];
|
|
|
+ $whereConsultant[] = [function($query) use ($consultant) {
|
|
|
+ $query->orWhere('name', 'like', "%{$consultant}%")->orWhere('mobile', 'like', "%{$consultant}%");
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ $paginator = MarketCustomerFollow::with(['custom', 'consultant'])->whereHas('custom', function($query) use ($whereCustom){
|
|
|
+ $query->where($whereCustom);
|
|
|
+ })->whereHas('consultant', function($query) use ($whereConsultant) {
|
|
|
+ $query->where($whereConsultant);
|
|
|
+ })->where($where)->orderBy('follow_time', 'desc')->paginate($limit, '*', 'page', $page);
|
|
|
+ $total = $paginator->total();
|
|
|
+ $items = $paginator->items();
|
|
|
+ foreach ($items as &$item) {
|
|
|
+ if (!empty($item->custom)) {
|
|
|
+ $item->custom->mask_mobile = self::handlePhone($item->custom->mobile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $data = [
|
|
|
+ 'total' => $total,
|
|
|
+ 'rows' => $items
|
|
|
+ ];
|
|
|
+ return json_success('success', $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 删除跟进记录
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/7
|
|
|
+ * Time: 11:25
|
|
|
+ * @param $ids
|
|
|
+ */
|
|
|
+ public static function deleteFollow($ids)
|
|
|
+ {
|
|
|
+ if (!$ids) {
|
|
|
+ return json_fail("数据错误");
|
|
|
+ }
|
|
|
+ if (!is_array($ids)) {
|
|
|
+ $ids = [$ids];
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (is_array($ids)) {
|
|
|
+ MarketCustomerFollow::whereIn('id', $ids)->delete();
|
|
|
+ } else {
|
|
|
+ MarketCustomerFollow::where('id', $ids)->delete();
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ return json_fail('删除失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_success('删除成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 转移客户
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/7
|
|
|
+ * Time: 14:27
|
|
|
+ * @param $params
|
|
|
+ */
|
|
|
+ public static function moveCustom($params)
|
|
|
+ {
|
|
|
+ $userId = JwtToken::getCurrentId();
|
|
|
+ $currentConsultantId = $params['consultant_id']; //当前顾问
|
|
|
+ $consultantIds = $params['move_consultant_id'] ?? [];
|
|
|
+ $customIds = $params['move_market_customer_id'] ?? [];
|
|
|
+ $note = $params['note'] ?? '';
|
|
|
+ $consultantInfo = Consultant::firstWhere(['id' => $currentConsultantId]);
|
|
|
+ if (empty($consultantInfo)) {
|
|
|
+ return json_fail('顾问不存在');
|
|
|
+ }
|
|
|
+ $currentDeptId = $consultantInfo->dept_id; //当前部门
|
|
|
+ $logsInertData = [];
|
|
|
+ $now = time();
|
|
|
+ $whereCustom = [];
|
|
|
+ if (!empty($customIds)) {
|
|
|
+ $whereCustom[] = [function($query) use ($customIds) {
|
|
|
+ $query->whereIn('id', $customIds);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($consultantIds)) {
|
|
|
+ $whereCustom[] = [function($query) use ($consultantIds) {
|
|
|
+ $query->whereIn('consultant_id', $consultantIds);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ $customList = MarketCustomer::where($whereCustom)->select(['id','consultant_id','dept_id'])->get();
|
|
|
+ if (!$customList->isEmpty()) {
|
|
|
+ foreach ($customList as$item) {
|
|
|
+ $logsInertData[] = [
|
|
|
+ 'market_customer_id' => $item->id,
|
|
|
+ 'consultant_id' => $currentConsultantId,
|
|
|
+ 'dept_id' => $currentDeptId,
|
|
|
+ 'before_consultant_id' => $item->consultant_id,
|
|
|
+ 'before_dept_id' => $item->dept_id,
|
|
|
+ 'note' => $note,
|
|
|
+ 'change_user_id' => $userId,
|
|
|
+ 'created_at' => $now
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($customList->isEmpty()) {
|
|
|
+ return json_fail('移交的客户信息不存在');
|
|
|
+ }
|
|
|
+ if (empty($logsInertData)) {
|
|
|
+ return json_fail('移交记录不能为空');
|
|
|
+ }
|
|
|
+ Db::beginTransaction();
|
|
|
+ $result = false;
|
|
|
+ try {
|
|
|
+ foreach ($customList as $item) {
|
|
|
+ $item->consultant_id = $currentConsultantId;
|
|
|
+ $item->dept_id = $currentDeptId;
|
|
|
+ $item->save();
|
|
|
+ }
|
|
|
+ $result = MarketCustomerLogs::insert($logsInertData);
|
|
|
+ Db::commit();
|
|
|
+ }catch (BusinessException|\Exception $e) {
|
|
|
+ Db::rollBack();
|
|
|
+ return json_fail($e->getMessage());
|
|
|
+ }
|
|
|
+ if ($result) {
|
|
|
+ return json_success('移交成功');
|
|
|
+ } else {
|
|
|
+ return json_fail('移交失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 转移记录
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/7
|
|
|
+ * Time: 15:33
|
|
|
+ * @param Request $request
|
|
|
+ */
|
|
|
+ public static function moveLogs(Request $request)
|
|
|
+ {
|
|
|
+ $format = $request->get('format', 'normal');
|
|
|
+ $limit = (int)$request->get('pageSize', $format === 'tree' ? 1000 : 10);
|
|
|
+ $limit = $limit <= 0 ? 10 : $limit;
|
|
|
+ $params = $request->get();
|
|
|
+ $page = (int)$request->get('page');
|
|
|
+ $page = $page > 0 ? $page : 1;
|
|
|
+ $platform = 0;
|
|
|
+ $where = [];
|
|
|
+ $whereCustom = [];
|
|
|
+ $whereCurrent = [];
|
|
|
+ $whereBefore = [];
|
|
|
+ $whereOpBack = [];
|
|
|
+ $whereOpFront = [];
|
|
|
+ if (!empty($params['market_customer_id'])) {
|
|
|
+ $where[] = ['market_customer_id', '=', $params['market_customer_id']];
|
|
|
+ }
|
|
|
+ if (!empty($params['consultant_id'])) {
|
|
|
+ $consultantIds = $params['consultant_id'];
|
|
|
+ $where[] = [function($query) use ($consultantIds) {
|
|
|
+ $query->orWhereIn('consultant_id', $consultantIds)->orWhereIn('before_consultant_id', $consultantIds);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ if (!empty($params['custom'])) {
|
|
|
+ //客户信息
|
|
|
+ $custom = $params['custom'];
|
|
|
+ $whereCustom[] = ['name', 'like', "%{$custom}%"];
|
|
|
+ }
|
|
|
+ if (!empty($params['current_consultant'])) {
|
|
|
+ //当前员工信息
|
|
|
+ $current = $params['current_consultant'];
|
|
|
+ $whereCurrent[] = ['name', 'like', "%{$current}%"];
|
|
|
+ }
|
|
|
+ if (!empty($params['before_consultant'])) {
|
|
|
+ //转移前员工信息
|
|
|
+ $before = $params['before_consultant'];
|
|
|
+ $whereBefore[] = ['name', 'like', "%{$before}%"];
|
|
|
+ }
|
|
|
+ if (!empty($params['platform'])) {
|
|
|
+ $platform = $params['platform'];
|
|
|
+ if ($platform == 1) {
|
|
|
+ //后台
|
|
|
+ $where[] = ['change_user_id', '<>', null];
|
|
|
+ $where[] = ['change_user_id', '=', null];
|
|
|
+ } else {
|
|
|
+ //小程序
|
|
|
+ $where[] = ['change_user_id', '=', null];
|
|
|
+ $where[] = ['change_user_id', '<>', null];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!empty($params['op_name'])) {
|
|
|
+ $opName = $params['op_name'];
|
|
|
+ if (!empty($platform)) {
|
|
|
+ if ($platform == 1) {
|
|
|
+ $whereOpBack[] = ['user_name', 'like', "%{$opName}%"];
|
|
|
+ } else {
|
|
|
+ $whereOpFront[] = ['name', 'like', "%{$opName}%"];
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ $whereOpBack[] = [function($query) use ($opName) {
|
|
|
+ $query->orWhere('user_name', 'like', "%{$opName}%");
|
|
|
+ }];
|
|
|
+ $whereOpFront[] = [function($query) use ($opName) {
|
|
|
+ $query->orWhere('name', 'like', "%{$opName}%");
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $paginator = MarketCustomerLogs::with(['custom', 'beforeMan', 'currentMan', 'opBackPerson', 'opFrontPerson'])
|
|
|
+ ->whereHas('custom', function ($query) use ($whereCustom) {
|
|
|
+ $query->where($whereCustom);
|
|
|
+ })
|
|
|
+ ->whereHas('currentMan', function ($query) use ($whereCurrent) {
|
|
|
+ $query->where($whereCurrent);
|
|
|
+ })
|
|
|
+ ->whereHas('beforeMan', function ($query) use ($whereBefore) {
|
|
|
+ $query->where($whereBefore);
|
|
|
+ })
|
|
|
+ ->where($where)
|
|
|
+ ->orderBy('created_at', 'desc')
|
|
|
+ ->paginate($limit, '*', 'page', $page);
|
|
|
+ $total = $paginator->total();
|
|
|
+ $items = $paginator->items();
|
|
|
+ foreach ($items as &$item) {
|
|
|
+ if (!empty($item->custom)) {
|
|
|
+ $item->custom->mask_mobile = self::handlePhone($item->custom->mobile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $data = [
|
|
|
+ 'total' => $total,
|
|
|
+ 'rows' => $items
|
|
|
+ ];
|
|
|
+ return json_success('success', $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 审核客户
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/16
|
|
|
+ * Time: 11:21
|
|
|
+ * @param $params
|
|
|
+ */
|
|
|
+ public static function checkCustom($params)
|
|
|
+ {
|
|
|
+ $adminId = JwtToken::getCurrentId();
|
|
|
+ //查询客户是否已经存在
|
|
|
+ $customId = $params['id'];
|
|
|
+ $checkStatus = $params['check_status'];
|
|
|
+ if (!in_array($checkStatus, [-1,2])) {
|
|
|
+ return json_fail('状态值非法');
|
|
|
+ }
|
|
|
+ $info = MarketCustomer::firstWhere(['id' => $customId]);
|
|
|
+ if (empty($info)) {
|
|
|
+ return json_fail('客户不存在');
|
|
|
+ }
|
|
|
+ $status = $info->check_status;
|
|
|
+ if ($status != 1) {
|
|
|
+ return json_fail('客户不是待审核状态');
|
|
|
+ }
|
|
|
+ $mobile = $info->mobile;
|
|
|
+ if ($checkStatus == 2) {
|
|
|
+ if (MarketCustomer::checkCustomExists($mobile)) {
|
|
|
+ return json_fail('客户已经存在');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $result = false;
|
|
|
+ Db::beginTransaction();
|
|
|
+ try {
|
|
|
+ $info->check_status = $checkStatus;
|
|
|
+ if ($checkStatus == -1) {
|
|
|
+ //拒绝录入拒绝理由
|
|
|
+ $info->check_note = $params['note'] ?? '无拒绝理由';
|
|
|
+ }
|
|
|
+ $info->check_time = time();
|
|
|
+ $info->check_admin_id = $adminId;
|
|
|
+ $result = $info->save();
|
|
|
+ if ($checkStatus == 2) {
|
|
|
+ //将其他待审的相同手机号的改拒绝
|
|
|
+ $where = [
|
|
|
+ ['mobile' , '=', $mobile],
|
|
|
+ ['check_status', '=', 1],
|
|
|
+ ['id', '<>', $customId]
|
|
|
+ ];
|
|
|
+ MarketCustomer::where($where)->update(['check_time' => time(),'check_status' => -1,'check_note' => '客户已经被其他顾问报备']);
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ }catch (BusinessException|\Exception $e){
|
|
|
+ Db::rollBack();
|
|
|
+ return json_fail($e->getMessage());
|
|
|
+ }
|
|
|
+ if ($result) {
|
|
|
+ return json_success('审核成功');
|
|
|
+ } else {
|
|
|
+ return json_fail('审核失败');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 数据统计分析
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/17
|
|
|
+ * Time: 11:27
|
|
|
+ * @param $params
|
|
|
+ */
|
|
|
+ public static function statisticsIndex(Request $request)
|
|
|
+ {
|
|
|
+ $params = $request->get();
|
|
|
+ $where = self::commonSearch($params);
|
|
|
+ //客户总数
|
|
|
+ $total = MarketCustomer::where($where)->count();
|
|
|
+ //待审核
|
|
|
+ $checkTotal = MarketCustomer::where($where)->where('check_status', 1)->count();
|
|
|
+ //已报备
|
|
|
+ $reportTotal = MarketCustomer::where($where)->where(
|
|
|
+ [[function($query) {
|
|
|
+ $currentTime = time();
|
|
|
+ $diffNums = self::DIFF_TIME;
|
|
|
+ $query->whereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0) AND current_status = 1 AND check_status = 2");
|
|
|
+ }]]
|
|
|
+ )->count();
|
|
|
+ //已来电
|
|
|
+ $mobileTotal = MarketCustomer::where($where)->where([[function($query) {
|
|
|
+ $query->where('check_status', '=', 2)->where('current_status', '=', 1);
|
|
|
+ }]])->count();
|
|
|
+ //已到访
|
|
|
+ $visitTotal = MarketCustomer::where($where)->where([[function($query) {
|
|
|
+ $query->where('check_status', '=', 2)->where('current_status', '=', 2);
|
|
|
+ }]])->count();
|
|
|
+ //已缴费
|
|
|
+ $payTotal = MarketCustomer::where($where)->where([[function($query) {
|
|
|
+ $query->where('check_status', '=', 2)->where('current_status', '=', 3);
|
|
|
+ }]])->count();
|
|
|
+ //已成交
|
|
|
+ $dealTotal = MarketCustomer::where($where)->where([[function($query) {
|
|
|
+ $query->where('check_status', '=', 2)->where('current_status', '=', 4);
|
|
|
+ }]])->count();
|
|
|
+ //已到访 + 已缴费 + 已成交 / 客户总数
|
|
|
+ $rant = 0;
|
|
|
+ if ($total > 0) {
|
|
|
+ $rant = $payTotal + $dealTotal / $total;
|
|
|
+ }
|
|
|
+ $rant = sprintf('%.2f', $rant);
|
|
|
+ $data = [
|
|
|
+ 'total' => $total,
|
|
|
+ 'check_total' => $checkTotal,
|
|
|
+ 'report_total' => $reportTotal,
|
|
|
+ 'mobile_total' => $mobileTotal,
|
|
|
+ 'visit_total' => $visitTotal,
|
|
|
+ 'pay_total' => $payTotal,
|
|
|
+ 'deal_total' => $dealTotal,
|
|
|
+ 'rant' => $rant,
|
|
|
+ ];
|
|
|
+ return json_success('请求成功', $data);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Notes: 导出数据
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/17
|
|
|
+ * Time: 13:01
|
|
|
+ */
|
|
|
+ public static function exportData(Request $request)
|
|
|
+ {
|
|
|
+ $currentTime = time();
|
|
|
+ $diffNums = self::DIFF_TIME;
|
|
|
+ $params = $request->get();
|
|
|
+ $where = self::commonSearch($params);
|
|
|
+ if (!empty($params['custom_ids'])) {
|
|
|
+ $customIds = $params['custom_ids'];
|
|
|
+ $where[] = [function($query) use ($customIds){
|
|
|
+ $query->whereIn('id', $customIds);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ $list = MarketCustomer::where($where)->orderBy('visit_time', 'desc')->get();
|
|
|
+ if (!$list->isEmpty()) {
|
|
|
+ $consultantKeys = [];
|
|
|
+ //查询顾问信息
|
|
|
+ $consultantList = Consultant::select(['id', 'name', 'mobile'])->get();
|
|
|
+ if (!$consultantList->isEmpty()) {
|
|
|
+ foreach ($consultantList->toArray() as $consultantItem) {
|
|
|
+ $consultantKeys[$consultantItem['id']] = $consultantItem;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $teamKeys = TeamService::getItemKeys();
|
|
|
+ $returnData = [];
|
|
|
+ $genderData = ['', '男', '女'];
|
|
|
+ $typeData = ['', '来电', '来访'];
|
|
|
+ $currentStatusData = [-1 => '无效客户', 1 => '已来电', 2 => '已到访', 3 => '已缴费', 4 => '已成交'];
|
|
|
+ foreach ($list as &$item) {
|
|
|
+ $visitTime = $item->visit_time;
|
|
|
+ $visitTimeStamp = strtotime($visitTime);
|
|
|
+ $diff = ($visitTimeStamp + $diffNums) - $currentTime;
|
|
|
+ $currentStatus = $item->current_status;//当前状态
|
|
|
+ $checkStatus = $item->check_status;//审核状态
|
|
|
+ if ($checkStatus == 1) {
|
|
|
+ $currentStatusText = '待审核';
|
|
|
+ }
|
|
|
+ if ($checkStatus == -1) {
|
|
|
+ $currentStatusText = '已失效';
|
|
|
+ }
|
|
|
+ if ($checkStatus == 2) {
|
|
|
+ //审核通过
|
|
|
+ if ($currentStatus > 1) {
|
|
|
+ $currentStatusText = $currentStatusData[$currentStatus] ?? '';
|
|
|
+ }
|
|
|
+ if ($currentStatus == 1) {
|
|
|
+ if ($diff > 0) {
|
|
|
+ $currentStatusText = $currentStatusData[$currentStatus] ?? '';
|
|
|
+ } else {
|
|
|
+ $currentStatusText = '已失效';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($currentStatus == -1) {
|
|
|
+ $currentStatusText = '无效客户';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $returnData[] = [
|
|
|
+ 'name' => $item->name,
|
|
|
+ 'mobile' => $item->mobile,
|
|
|
+ 'mask_mobile' => self::handlePhone($item->mobile),
|
|
|
+ 'gender' => $genderData[$item->gender] ?? '',
|
|
|
+ 'type' => $typeData[$item->type] ?? '',
|
|
|
+ 'consultant_name' => $consultantKeys[$item->consultant_id]['name'] ?? '',
|
|
|
+ 'consultant_mobile' => $consultantKeys[$item->consultant_id]['mobile'] ?? '',
|
|
|
+ 'team_name' => TeamService::getTeamName($teamKeys, $item->dept_id),
|
|
|
+ 'current_status' => $currentStatusText, //当前状态
|
|
|
+ 'visit_time' => $item->visit_time,
|
|
|
+ 'created_at' => date('Y-m-d H:i:s',strtotime($item->created_at))
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return json_success('请求成功', $returnData);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * Notes: 对手机号加密处理
|
|
|
+ * User: yb
|
|
|
+ * Date: 2024/8/8
|
|
|
+ * Time: 15:41
|
|
|
+ * @param $val
|
|
|
+ * @return string|string[]
|
|
|
+ */
|
|
|
+ public static function handlePhone($val)
|
|
|
+ {
|
|
|
+ return substr_replace($val, '****', 3, 4);
|
|
|
+ }
|
|
|
}
|