Commit ad34aaab authored by hujun's avatar hujun

经纪人转勤优化

parent 59890e5d
...@@ -516,9 +516,6 @@ class Broker extends Basic ...@@ -516,9 +516,6 @@ class Broker extends Basic
* 新增or修改or查看 * 新增or修改or查看
* *
* @return \think\Response * @return \think\Response
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/ */
public function saveAgentV2() { public function saveAgentV2() {
$result['code'] = 200; $result['code'] = 200;
......
...@@ -32,19 +32,16 @@ class BrokerService ...@@ -32,19 +32,16 @@ class BrokerService
} }
/** /**
* 新增编辑经纪人 * 新增编辑经纪人
* *
* @param array $data * @param array $data
* @param int $operation_id * @param int $operation_id
* @return mixed * @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/ */
public function saveAgentData(array $data, int $operation_id) public function saveAgentData(array $data, int $operation_id)
{ {
$is_new_id = 1;
$result['status'] = 'fail'; $result['status'] = 'fail';
$is_new_id = 1;
$validate = new AAgentValidate(); $validate = new AAgentValidate();
$m_district = new ADistrict(); $m_district = new ADistrict();
$data['site_id'] = $m_district->getDistrictKeyById('site_id', ['id' => $data['district_id']]); $data['site_id'] = $m_district->getDistrictKeyById('site_id', ['id' => $data['district_id']]);
...@@ -59,163 +56,92 @@ class BrokerService ...@@ -59,163 +56,92 @@ class BrokerService
$result['msg'] = $validate->getError(); $result['msg'] = $validate->getError();
return $result; return $result;
} }
$field = 'id,name,phone,sex,img,status,inviter_id,admin_off,quit_time,password,site_id,district_id,store_id,auth_group_id,level';
$agent_old = $this->m_agent->getAgentById($field, ['phone' => $data['phone'], 'status' => 0]);
if (empty($data['id'])) {
$where['phone'] = $data['phone'];
$where['site_id'] = $data['site_id'];
$agent_data = $this->m_agent->verifyUser($field, '', $where);
if (!empty($agent_data) && $agent_data['status'] == 0) { try {
$result['msg'] = '该用户已存在'; $field = 'id,name,phone,sex,img,status,inviter_id,admin_off,quit_time,password,site_id,district_id,store_id,auth_group_id,level';
return $result; $agent_old = $this->m_agent->getAgentById($field, ['phone' => $data['phone'], 'status' => 0]);
} if (empty($data['id'])) {
$where['phone'] = $data['phone'];
$where['site_id'] = $data['site_id'];
$agent_data = $this->m_agent->verifyUser($field, '', $where);
if (!empty($agent_data) && $agent_data['status'] == 0) {
$result['msg'] = '该用户已存在';
return $result;
}
if (empty($data['confirm_old']) && (!empty($agent_old[0]['id']) || $agent_data['status'] == 3)) { if (empty($data['confirm_old']) && (!empty($agent_old[0]['id']) || $agent_data['status'] == 3)) {
$result['msg'] = '转勤'; $result['msg'] = '转勤';
return $result; return $result;
} else { } else {
$agent_num = count($agent_old); $agent_num = count($agent_old);
if ($agent_num > 0) { if ($agent_num > 0) {
//根据是否带看过判断是否换id //根据是否带看过判断是否换id
$m_march_in = new OMarchInModel(); $m_march_in = new OMarchInModel();
$march_data = $m_march_in->selectMarchInOne('a.id', ['reception_id'=>$agent_old['id'],'site_id'=>$data['site_id']]); $march_data = $m_march_in->selectMarchInOne('a.id', ['reception_id' => $agent_old[0]['id'], 'site_id' => $data['site_id']]);
if ($march_data['id']) { if (empty($march_data['id'])) {
$is_new_id = 0; $is_new_id = 0;
}
} }
} }
}
$msg = '新增成功'; $msg = '新增成功';
$data['status'] = 0; $data['status'] = 0;
$data['level'] = 10; $data['level'] = 10;
if (empty($data['password'])) { if (empty($data['password'])) {
$data['password'] = substr($data['phone'], -6); //默认密码 $data['password'] = substr($data['phone'], -6); //默认密码
}
} else {
$where['id'] = $data['id'];
$agent_data = $this->m_agent->verifyUser($field, '', $where);
if (empty($agent_data)) {
$result['msg'] = '没有该用户';
return $result;
}
$old_site_id = $m_district->getDistrictKeyById('site_id', ['id'=>$agent_data['district_id']]);
$new_site_id = $m_district->getDistrictKeyById('site_id', ['id'=>$data['district_id']]);
if ($old_site_id != $new_site_id && empty($data['confirm_old'])) {
$result['msg'] = '编辑部门,导致所属城市不一致';
return $result;
}
$msg = '编辑成功';
if (!empty($data['password'])) {
if (($agent_data['password'] == $data['password'])) {
unset($data['password']);
} }
}
}
if ($is_new_id && $data['confirm_old']) {
if ($old_site_id == $new_site_id) {
$result['msg'] = '同城市不能转勤';
return $result;
}
$change_old_id = $agent_old[0]['id'];
if (!empty($agent_old[0]['id'])) {
$this->m_agent->saveAgent(['id'=>$agent_old[0]['id'],'status'=>3]);
}
unset($data['id']); $new_site_id = $m_district->getDistrictKeyById('site_id', ['id' => $data['district_id']]);
$data['password_md5'] = $agent_old[0]['password']; } else {
$id = $this->m_agent->saveAgent($data); $where['id'] = $data['id'];
} elseif ($agent_data['status'] == 3) { $agent_data = $this->m_agent->verifyUser($field, '', $where);
$change_old_id = $agent_old[0]['id'];
$id = $this->m_agent->saveAgent(['id'=>$agent_data['id'],'status'=>0]);
if (!empty($agent_old[0]['id'])) {
$this->m_agent->saveAgent(['id'=>$agent_old[0]['id'],'status'=>3]);
}
} elseif (empty($data['confirm_old'])){
$id = $this->m_agent->saveAgent($data); //编辑
$change_old_id = 0;
}else{
//新增一条转勤状态
$new_agent['name'] = $agent_old[0]['name'];
$new_agent['phone'] = $agent_old[0]['phone'];
$new_agent['sex'] = $agent_old[0]['sex'];
$new_agent['img'] = $agent_old[0]['img'];
$new_agent['status'] = 3;
$new_agent['inviter_id'] = $agent_old[0]['inviter_id'];
$new_agent['admin_off'] = $agent_old[0]['admin_off'];
$new_agent['quit_time'] = $agent_old[0]['quit_time'];
$new_agent['password_md5'] = $agent_old[0]['password'];
$new_agent['site_id'] = $agent_old[0]['site_id'];
$new_agent['district_id'] = $agent_old[0]['district_id'];
$new_agent['store_id'] = $agent_old[0]['store_id'];
$new_agent['auth_group_id'] = $agent_old[0]['auth_group_id'];
$new_agent['level'] = $agent_old[0]['level'];
$this->m_agent->saveAgent($new_agent);
$change_old_id = $agent_old[0]['id'];
//将原来的id作为新的
$data['id'] = $agent_old[0]['id'];
$id = $this->m_agent->saveAgent($data);
}
if ($id > 0) { if (empty($agent_data)) {
$result['status'] = 'successful'; $result['msg'] = '没有该用户';
$result['data'] = ['id' => $id]; return $result;
$result['msg'] = $msg;
if ($change_old_id) {
$m_user = new Users();
$update_user = $update_house = [];
$user_id_arr = $m_user->getUserByWhereColumn(['agent_id' => $change_old_id], 'id');
foreach ($user_id_arr as $k=>$v) {
$update_user[$k]['id'] = $v;
$update_user[$k]['agent_id'] = $id;
} }
$record_data['user_id'] = empty($user_id_arr) ? "" : implode(',', $user_id_arr); $old_site_id = $m_district->getDistrictKeyById('site_id', ['id' => $agent_data['district_id']]);
$new_site_id = $m_district->getDistrictKeyById('site_id', ['id' => $data['district_id']]);
$m_house = new GHousesToAgents(); if ($old_site_id != $new_site_id && empty($data['confirm_old'])) {
$house_where['is_del'] = 0; $result['msg'] = '编辑部门,导致所属城市不一致';
$house_where['agents_id'] = $change_old_id; return $result;
$house_where['type'] = ['in', '2,3'];
$house_id_arr = $m_house->getAgentHouseOne('id', $house_where);
foreach ($house_id_arr as $k=>$v) {
$update_house[$k]['id'] = $v;
$update_house[$k]['agents_id'] = $id;
} }
$house_id_arr = array_unique($house_id_arr); if ($old_site_id == $new_site_id) {
$record_data['house_id'] = empty($house_id_arr) ? "" : '关系id'.implode(',', $house_id_arr); $result['msg'] = '相同部门不需要转勤';
return $result;
if (!empty($update_user)) {
$m_user->updateUserAll($update_user);
} }
if (!empty($update_house)) { $msg = '编辑成功';
$m_house->updateUserAll($update_house); if (!empty($data['password'])) {
if (($agent_data['password'] == $data['password'])) {
unset($data['password']);
}
} }
} }
if ($data['confirm_old'] == 1) { if (empty($data['confirm_old'])) {
$m_records = new AOperatingRecords(); $id = $this->m_agent->saveAgent($data); //正常新增或编辑
$record_data['remark'] = '转勤,旧id:'.$change_old_id; } else {
$record_data['type'] = 0; $data['site_id'] = $new_site_id;
$record_data['operation_id'] = $operation_id; $id = $this->transferAgent($data, $agent_old, $is_new_id, $operation_id); //转勤
$record_data['agent_id'] = $id;
$m_records->insertData($record_data);
} }
} else { if ($id > 0) {
$result['msg'] = '新增或编辑失败'; $result['msg'] = $msg;
} $result['status'] = 'successful';
} else {
$result['msg'] = $msg;
$result['status'] = 'fail';
}
} catch (\Exception $e) {
$result['msg'] = '内部错误:'.$e->getMessage();
$result['status'] = 'fail';
}
return $result; return $result;
} }
...@@ -228,16 +154,10 @@ class BrokerService ...@@ -228,16 +154,10 @@ class BrokerService
*/ */
public function updateStatus(array $data, int $operation_id) public function updateStatus(array $data, int $operation_id)
{ {
$result['status'] = 'successful';
$save_data['status'] = $data['status']; $save_data['status'] = $data['status'];
$save_data['id'] = $data['id']; $save_data['id'] = $data['id'];
$id = $this->m_agent->saveAgent($save_data); $id = $this->m_agent->saveAgent($save_data);
if ($id < 0) {
$result['status'] = 'fail';
$result['msg'] = '修改失败';
return $result;
}
//同时修改设备绑定表 //同时修改设备绑定表
try { try {
...@@ -271,7 +191,7 @@ class BrokerService ...@@ -271,7 +191,7 @@ class BrokerService
$push->record(4, 0, [$data['id']], $operation_id, $message_data); $push->record(4, 0, [$data['id']], $operation_id, $message_data);
} }
return $result; return ;
} }
/** /**
...@@ -284,7 +204,8 @@ class BrokerService ...@@ -284,7 +204,8 @@ class BrokerService
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException * @throws \think\exception\DbException
*/ */
public function checkSite($agent_id, $site_id = 0) { public function checkSite($agent_id, $site_id = 0)
{
$city = []; $city = [];
$m_group_site = new AuthGroupSite(); $m_group_site = new AuthGroupSite();
$m_site = new ASite(); $m_site = new ASite();
...@@ -296,7 +217,7 @@ class BrokerService ...@@ -296,7 +217,7 @@ class BrokerService
$where['group_id'] = $auth_group_id; $where['group_id'] = $auth_group_id;
$where['status'] = 0; $where['status'] = 0;
if ($site_id) { if ($site_id) {
$where[] = ['EXP', 'find_in_set('.$site_id.', site_id)']; $where[] = ['EXP', 'find_in_set(' . $site_id . ', site_id)'];
} }
$site_id = $m_group_site->getGroupSite('site_id', $where); $site_id = $m_group_site->getGroupSite('site_id', $where);
...@@ -314,4 +235,98 @@ class BrokerService ...@@ -314,4 +235,98 @@ class BrokerService
} }
return $result; return $result;
} }
/**
* 处理转勤
*
* @param $data
* @param $agent_old
* @param $is_new_id
* @param $operation_id
* @return int|string
*/
private function transferAgent($data, $agent_old, $is_new_id, $operation_id)
{
try {
$new_where['phone'] = $data['phone'];
$new_where['status'] = 3;
$new_where['site_id'] = $data['site_id'];
$transfer_agent = $this->m_agent->getAgentsByWhere($new_where, 'id');
if (empty($transfer_agent)) {
if ($is_new_id) {
$agent_old[0]['status'] = 3;
$this->m_agent->saveAgent($agent_old[0]); //新增一条转勤状态
$data['id'] = $agent_old[0]['id']; //将原来的id作为新的
$id = $this->m_agent->saveAgent($data);
} else {
$this->m_agent->saveAgent(['id'=>$agent_old[0]['id'], 'status'=>3]); //将原来的设置为转勤状态
unset($data['id']);
$id = $this->m_agent->saveAgent($data); //普通转勤
$this->transferUserHouse($id, $agent_old[0]['id'], $operation_id);
}
} else {
$this->m_agent->saveAgent(['id'=>$agent_old[0]['id'], 'status'=>3]); //将原来的设置为转勤状态
$this->m_agent->saveAgent(['id'=>$transfer_agent, 'status'=>0]); //将原来转勤状态处理为正常
$this->transferUserHouse($transfer_agent, $agent_old[0]['id'], $operation_id);
$id = $transfer_agent;
}
} catch (\Exception $e) {
$id = 0;
}
return $id;
}
/**
* 转换客户和商铺到转勤后的id
*
* @param $id
* @param $change_old_id
* @param $operation_id
* @throws \Exception
*/
private function transferUserHouse($id, $change_old_id, $operation_id)
{
$m_user = new Users();
$update_user = $update_house = [];
$user_id_arr = $m_user->getUserByWhereColumn(['agent_id' => $change_old_id], 'id');
foreach ($user_id_arr as $k => $v) {
$update_user[$k]['id'] = $v;
$update_user[$k]['agent_id'] = $id;
}
$record_data['user_id'] = empty($user_id_arr) ? "" : implode(',', $user_id_arr);
$m_house = new GHousesToAgents();
$house_where['is_del'] = 0;
$house_where['agents_id'] = $change_old_id;
$house_where['type'] = ['in', '2,3'];
$house_id_arr = $m_house->getAgentHouseOne('id', $house_where);
foreach ($house_id_arr as $k => $v) {
$update_house[$k]['id'] = $v;
$update_house[$k]['agents_id'] = $id;
}
$house_id_arr = array_unique($house_id_arr);
$record_data['house_id'] = empty($house_id_arr) ? "" : '关系id' . implode(',', $house_id_arr);
if (!empty($update_user)) {
$m_user->updateUserAll($update_user);
}
if (!empty($update_house)) {
$m_house->updateUserAll($update_house);
}
$m_records = new AOperatingRecords();
$record_data['remark'] = '转勤,旧id:' . $change_old_id;
$record_data['type'] = 0;
$record_data['operation_id'] = $operation_id;
$record_data['agent_id'] = $id;
$m_records->insertData($record_data);
}
} }
\ No newline at end of file
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