Commit 006e8c10 authored by hujun's avatar hujun

新增和编辑客户统一处理

parent 6a20635f
...@@ -3,13 +3,11 @@ ...@@ -3,13 +3,11 @@
namespace app\api_broker\controller; namespace app\api_broker\controller;
use app\api_broker\extend\Basic; use app\api_broker\extend\Basic;
use app\api_broker\service\PushMessageService; use app\index\service\UserService;
use app\model\AAgents; use app\model\AAgents;
use app\model\ULabels; use app\model\ULabels;
use app\model\Users; use app\model\Users;
use app\model\UPhoneFollowPp; use app\model\UPhoneFollowPp;
use app\model\AuthGroup;
use app\model\AuthRule;
use app\api_broker\service\VipService; use app\api_broker\service\VipService;
/** /**
...@@ -40,9 +38,6 @@ class Client extends Basic ...@@ -40,9 +38,6 @@ class Client extends Basic
* 新增或编辑客户 * 新增或编辑客户
* *
* @return \think\Response * @return \think\Response
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/ */
public function editClient() { public function editClient() {
header('Access-Control-Allow-Origin:*'); header('Access-Control-Allow-Origin:*');
...@@ -54,70 +49,16 @@ class Client extends Basic ...@@ -54,70 +49,16 @@ class Client extends Basic
$data = []; $data = [];
if (!empty($this->params['user_name']) || !empty($this->params['id'])) { if (!empty($this->params['user_name']) || !empty($this->params['id'])) {
$user_service = new UserService();
if ($this->request->isPost()) { if ($this->request->isPost()) {
if (!empty($this->params['id'])) { $result = $user_service->editUser($this->params['id'], $this->params, $this->agentId);
if ($result['status'] == 'fail') {
$user_agent_id = $this->user->getUserById('id,user_name,agent_id,referrer_id,referrer_source', $this->params['id']); //查询原始客方 $this->code = 101;
//编辑 $this->msg = $result['msg'];
$data['start'] = $this->user->edit($this->params, $this->params['id'], $this->agentId);
if (!empty($this->params['agents_id'])) {
//是否修改客方
if ($this->params['agents_id'] != $user_agent_id['agent_id']) {
$agent = new AAgents();
$agent_data = $agent->getAgentInfo('name,phone', $this->params['agents_id']);
$remark = '修改为'.$agent_data['name'].'-'.$agent_data['phone'];
$push = new PushMessageService();
$ccr_agent_data = $agent->getAgentInfo('name,phone', $this->params['ccr_id']);
$content = "【{$ccr_agent_data['name']}-{$ccr_agent_data['phone']}修改了{$user_agent_id['user_name']}(id:{$user_agent_id['id']})客方】";
$push->pushMessageById($user_agent_id['agent_id'], '客方修改', $content, 'user_info', $user_agent_id['id']);
$this->operating_records($this->params['ccr_id'], $type=3, $remark,0,$this->params['id']); //客方修改日志
}
if ($this->params['referrer_id'] != $user_agent_id['referrer_id']) {
if ($this->params['referrer_source'] == 20) {
$this->operating_records($this->params['referrer_id'], $type=4, $remark,0,$this->params['id']); //邀请人修改-经纪人
} else {
$this->operating_records($this->params['referrer_id'], $type=5, $remark,0,$this->params['id']); //邀请人修改-客户
}
}
}
} else {
$table = new AAgents();
//添加
$data['start'] = $this->user->edit($this->params, 0, $this->agentId);
if ($data['start'] > 0 && !empty($this->params['agents_id'])) {
$data['id']= $this->user->id;
$agent_data = $table->getAgentInfo('name,phone',$this->params['agents_id']);
$remark = '新增为'. $agent_data['name'].'-'.$agent_data['phone'];
$this->operating_records($this->params['ccr_id'], $type=3, $remark,0,$data['id']);
}
} }
switch ($data['start']) {
case -1 :
$this->code = 101;
$this->msg = '该用户已存在';break;
case -2 :
$this->code = 101;
$this->msg = '此号码为虚拟号码';break;
case -3 :
$this->code = 101;
$this->msg = '没有新增VIP权限';break;
case -4 :
$this->code = 101;
$this->msg = '客户信息错误,请检查!';break;
case -5 :
$this->code = 101;
$this->msg = '省或市或区未选择';break;
}
} else { } else {
$data['start'] = $this->user->getClient($this->params['id']); $data['start'] = $user_service->getUserData($this->params['id']);
} }
} else { } else {
$this->code = 101; $this->code = 101;
......
...@@ -4,6 +4,7 @@ namespace app\index\controller; ...@@ -4,6 +4,7 @@ namespace app\index\controller;
use app\api_broker\service\ClientService; use app\api_broker\service\ClientService;
use app\api_broker\service\UserExpiredTimeService; use app\api_broker\service\UserExpiredTimeService;
use app\index\extend\Basic; use app\index\extend\Basic;
use app\index\service\UserService;
use app\model\AAgents; use app\model\AAgents;
use app\model\ACollectUser; use app\model\ACollectUser;
use app\model\GOperatingRecords; use app\model\GOperatingRecords;
...@@ -356,98 +357,22 @@ class Member extends Basic{ ...@@ -356,98 +357,22 @@ class Member extends Basic{
* 查看或编辑客户 * 查看或编辑客户
* *
* @return \think\Response * @return \think\Response
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/ */
public function pcEditClient() { public function pcEditClient() {
$data = []; $data = [];
$user_service = new UserService();
if ($this->request->isPost()) { if ($this->request->isPost()) {
//编辑用户 $result = $user_service->editUser($this->params['id'], $this->params, $this->userId);
$result = ''; if ($result['status'] == 'fail') {
if (empty($this->params['id'])) { $this->code = 101;
//新增用户 $this->msg = $result['msg'];
$where['user_phone'] = $this->params['user_phone'];
$num = $this->user->all_user_count($where);
if ($num > 0) {
return $this->response(101, '该用户已经存在!');
}
$agent = new AAgents();
$agent_data = $agent->getAgentInfo('id,name,phone,store_id,district_id', $this->params['agents_id']);
$remark = '新增为' . $agent_data['name'] . '-' . $agent_data['phone'];
$result = $this->user->edit($this->params, $this->params['id'], $this->userId); //新增或编辑
//更新业绩统计
$sum = new ResultsSummaryNewTask();
$date = date('Y-m-d');
$sum->updateTotalByAgentId($agent_data['id'], $agent_data['store_id'], $agent_data['district_id'], $date);
$this->operating_records($this->userId, $type=3, $remark, $this->params['id']); //记录日志
} else {
$user_data = $this->user->getUserById('id,agent_id', $this->params['id']);
if (empty($user_data)) {
return $this->response(101, '没有用户信息');
}
if (!empty($this->params['agents_id'])) {
//是否修改客方
if ($this->params['agents_id'] != $user_data['agent_id']) {
$agent = new AAgents();
$agent_data = $agent->getAgentInfo('id,name,phone,store_id,district_id', $this->params['agents_id']);
$remark = '修改为' . $agent_data['name'] . '-' . $agent_data['phone'];
//更新业绩统计
$sum = new ResultsSummaryNewTask();
$date = date('Y-m-d');
$sum->updateTotalByAgentId($agent_data['id'], $agent_data['store_id'], $agent_data['district_id'], $date);
} else {
$remark = '用户编辑';
}
if ($this->params['referrer_id'] != $user_data['referrer_id']) {
if ($this->params['referrer_source'] == 20) {
$this->operating_records($this->params['referrer_id'], $type=4, $remark,0,$this->params['id']); //邀请人修改-经纪人
} else {
$this->operating_records($this->params['referrer_id'], $type=5, $remark,0,$this->params['id']); //邀请人修改-客户
}
}
$this->operating_records($this->userId, $type=3, $remark, $this->params['id']); //记录日志
}
$result = $this->user->edit($this->params, $this->params['id'], $this->userId); //新增或编辑
}
switch ($result) {
case -1 :
$this->code = 101;
$this->msg = '该用户已存在';break;
case -2 :
$this->code = 101;
$this->msg = '此号码为虚拟号码';break;
case -3 :
$this->code = 101;
$this->msg = '没有新增和编辑VIP权限';break;
case -4 :
$this->code = 101;
$this->msg = '客户信息错误,请检查!';break;
case -5 :
$this->code = 101;
$this->msg = '市或区未选择';break;
} }
} else { } else {
$data['list'] = $user_service->getUserData($this->params['id']);
//编辑获取用户信息
if (empty(($this->params['id']))) {
return $this->response(101, 'Id is null');
}
$data['list'] = $this->user->getClient($this->params['id']);
} }
return $this->response($this->code, $this->msg, $data); return $this->response($this->code, $this->msg, $data);
} }
/** /**
* 删除用户 * 删除用户
* @return \think\Response * @return \think\Response
......
<?php
/**
* Created by PhpStorm.
* User: fuju
* Date: 2018/8/27
* Time: 12:43
*/
namespace app\index\service;
use app\api_broker\service\PushMessageService;
use app\model\AAgents;
use app\model\GOperatingRecords;
use app\model\Users;
use app\task\controller\ResultsSummaryNewTask;
class UserService
{
protected $user;
protected $code = 200;
protected $msg = '';
protected $operation_id;
public function __construct()
{
$this->user = new Users();
}
/**
* 新增或编辑客户
*
* @param $id
* @param $data
* @param $operation_id
* @return array|int|mixed
*/
public function editUser($id, $data, $operation_id)
{
$this->operation_id = $operation_id;
$result = [];
$agent_id_old = $referrer_id_old = 0;
try {
if (empty($id)) {
//新增用户
$where['user_phone'] = $data['user_phone'];
$num = $this->user->all_user_count($where);
if ($num > 0) {
$result['status'] = 'fail';
$result['msg'] = '该用户已经存在';
return $result;
}
$type = 'add';
} else {
$user_data = $this->user->getUserById('id,agent_id,referrer_id,referrer_source,user_name', $id);
if (empty($user_data['id'])) {
$result['status'] = 'fail';
$result['msg'] = '没有该用户信息';
return $result;
}
$agent_id_old = empty($user_data['agent_id']) ? 0:$user_data['agent_id'];
$referrer_id_old = empty($user_data['referrer_id']) ? 0:$user_data['referrer_id'];
$user_name = empty($user_data['user_name']) ? '':$user_data['user_name'];
$type = 'edit';
}
$id = $this->user->edit($data, $id, $operation_id); //新增或编辑
if ($id <= 0) {
$msg = $this->getCodeMsg($id);
$result['status'] = 'fail';
$result['msg'] = $msg;
return $result;
} else {
$result['status'] = 'successful';
$result['msg'] = '';
}
//记录修改客方
$this->recordGuest($data['agents_id'], $agent_id_old, $id, $user_name,$type);
//记录邀请人修改记录
$this->recordReferrer($data['referrer_id'], $id, $referrer_id_old, $type);
//更新业绩统计
$agent = new AAgents();
$agent_data = $agent->getAgentInfo('id,name,phone,store_id,district_id', $data['agents_id']);
$sum = new ResultsSummaryNewTask();
$date = date('Y-m-d');
$sum->updateTotalByAgentId($agent_data['id'], $agent_data['store_id'], $agent_data['district_id'], $date);
} catch (\Exception $e) {
$result['status'] = 'fail';
$result['msg'] = $e->getMessage();
}
return $result;
}
/**
* 根据客户id获取客户信息
*
* @param int $user_id
* @return array
*/
public function getUserData(int $user_id) : array
{
if ($user_id) {
try {
$data['data'] = $this->user->getClient($user_id);
$data['msg'] = '';
$data['status'] = 'successful';
} catch (\Exception $e) {
$data['msg'] = $e->getMessage();
$data['status'] = 'fail';
}
} else {
$data['status'] = 'fail';
$data['msg'] = 'Id is null';
}
return $data;
}
/**
* 记录客方记录修改并推送消息
*
* @param int $agent_id
* @param int $agent_id_old
* @param int $user_id
* @param string $user_name
* @param string $type
* @return array
*/
public function recordGuest(int $agent_id,int $agent_id_old, int $user_id, string $user_name,string $type = 'add'): array
{
try {
$agent = new AAgents();
$agent_data = $agent->getAgentInfo('id,name,phone,store_id,district_id', $agent_id);
$remark = '';
if ($type == 'add') {
$remark = '新增为' . $agent_data['name'] . '-' . $agent_data['phone'];
} else {
if ($agent_id != $agent_id_old) {
$remark = '修改为' . $agent_data['name'] . '-' . $agent_data['phone'];
$this->pushMessage($agent_id, $this->operation_id, $user_name, $user_id);
}
}
if ($remark != '') {
$this->operating_records($this->operation_id, 3, $remark, $user_id); //记录日志
}
$result['status'] = 'successful';
$result['msg'] = '';
} catch (\Exception $e) {
$result['status'] = 'fail';
$result['msg'] = $e->getMessage();
}
return $result;
}
/**
* 记录邀请人修改记录
*
* @param $referrer_id
* @param $user_id
* @param $referrer_id_old
* @param string $type
* @return array
*/
public function recordReferrer($referrer_id, $user_id, $referrer_id_old, $type = 'add')
{
$result = [];
$remark = '';
try {
$user_data = $this->user->getUserById('user_name,user_phone', $referrer_id);
if ($type == 'add') {
$remark = '新增为' . $user_data['user_name'] . '-' . $user_data['user_phone'] . ',客户ID:' . $referrer_id;
} else {
if ($referrer_id != $referrer_id_old) {
$user_data = $this->user->getUserById('user_name,user_phone', $referrer_id);
$remark = '编辑为' . $user_data['user_name'] . '-' . $user_data['user_phone'] . ',客户ID:' . $referrer_id;
}
}
if ($remark != '') {
$this->operating_records($this->operation_id, 5, $remark, $user_id); //邀请人修改-客户
}
$result['status'] = 'successful';
$result['msg'] = "";
} catch (\Exception $e) {
$result['status'] = 'fail';
$result['msg'] = $e->getMessage();
}
return $result;
}
/**
* @param $code
* @return string
*/
public function getCodeMsg($code)
{
switch ($code) {
case -1 :
$msg = '该用户已存在';
break;
case -2 :
$msg = '此号码为虚拟号码';
break;
case -3 :
$msg = '没有新增和编辑VIP权限';
break;
case -4 :
$msg = '客户信息错误,请检查!';
break;
case -5 :
$msg = '市或区未选择';
break;
default :
$msg = '新增或编辑成功';
}
return $msg;
}
/**
* 记录操作
*
* @param int $agents_id
* @param string $type
* @param string $remark
* @param string $user_id
* @return bool|false|int
*/
public function operating_records($agents_id = 0, $type = '' ,$remark = '', $user_id = '') {
$records = new GOperatingRecords();
$result = $records->record($agents_id, $type, $remark, '',$user_id);
return $result;
}
/**
* 推送消息
*
* @param $agent_id
* @param $operation_id
* @param $user_name
* @param $user_id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function pushMessage($agent_id, $operation_id, $user_name, $user_id) {
$push = new PushMessageService();
$agent = new AAgents();
$ccr_agent_data = $agent->getAgentInfo('name,phone', $operation_id);
$content = "【{$ccr_agent_data['name']}-{$ccr_agent_data['phone']}修改了{$user_name}(id:{$user_id})客方】";
$push->pushMessageById($agent_id, '客方修改', $content, 'user_info', $user_id);
}
}
\ No newline at end of file
...@@ -310,7 +310,6 @@ class Users extends Model ...@@ -310,7 +310,6 @@ class Users extends Model
$insert_data['referrer_id'] = $data['referrer_id']; $insert_data['referrer_id'] = $data['referrer_id'];
$insert_data['referrer_source'] = $data['referrer_source']; $insert_data['referrer_source'] = $data['referrer_source'];
} }
if ($id && $id > 0) { if ($id && $id > 0) {
//修改 //修改
$insert_data['update_time'] = date('Y-m-d H:i:s'); $insert_data['update_time'] = date('Y-m-d H:i:s');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment