|
@ -1,10 +1,23 @@
|
|
|
package com.yihu.jw.base.service.user;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.jw.base.dao.role.BaseRoleMenuDao;
|
|
|
import com.yihu.jw.base.dao.role.RoleDao;
|
|
|
import com.yihu.jw.base.dao.saas.SaasDao;
|
|
|
import com.yihu.jw.base.dao.user.UserDao;
|
|
|
import com.yihu.jw.base.service.org.BaseOrgSaasService;
|
|
|
import com.yihu.jw.base.service.org.BaseOrgUserService;
|
|
|
import com.yihu.jw.base.service.org.OrgTree;
|
|
|
import com.yihu.jw.base.service.org.OrgTreeService;
|
|
|
import com.yihu.jw.base.util.ConstantUtils;
|
|
|
import com.yihu.jw.base.util.JavaBeanUtils;
|
|
|
import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
|
|
|
import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
|
|
|
import com.yihu.jw.entity.base.doctor.BaseDoctorRoleDO;
|
|
|
import com.yihu.jw.entity.base.org.BaseOrgDO;
|
|
|
import com.yihu.jw.entity.base.org.BaseOrgUserDO;
|
|
|
import com.yihu.jw.entity.base.role.RoleDO;
|
|
|
import com.yihu.jw.entity.base.saas.SaasDO;
|
|
|
import com.yihu.jw.entity.base.user.UserDO;
|
|
@ -12,13 +25,16 @@ import com.yihu.mysql.query.BaseJpaService;
|
|
|
import com.yihu.utils.security.MD5;
|
|
|
import org.apache.commons.collections.map.HashedMap;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Service - 后台管理员
|
|
@ -38,6 +54,22 @@ public class UserService extends BaseJpaService<UserDO, UserDao> {
|
|
|
@Autowired
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
|
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private BaseOrgUserService baseOrgUserService;
|
|
|
|
|
|
@Autowired
|
|
|
private OrgTreeService orgTreeService;
|
|
|
|
|
|
@Autowired
|
|
|
private BaseOrgSaasService baseOrgSaasService;
|
|
|
|
|
|
@Value("${configDefault.saasId}")
|
|
|
private String defaultSaasId;
|
|
|
|
|
|
|
|
|
public UserDO registerWithIdcard(UserDO userDO) {
|
|
|
userDO.setSalt(randomString(5));
|
|
|
userDO.setEnabled(true);
|
|
@ -53,6 +85,7 @@ public class UserService extends BaseJpaService<UserDO, UserDao> {
|
|
|
|
|
|
/**
|
|
|
* 手机号作为账号,初始密码为手机号后6位
|
|
|
*
|
|
|
* @param userDO
|
|
|
* @return
|
|
|
*/
|
|
@ -63,7 +96,7 @@ public class UserService extends BaseJpaService<UserDO, UserDao> {
|
|
|
userDO.setLoginFailureCount(0);
|
|
|
String password = userDO.getPassword();
|
|
|
if (StringUtils.isEmpty(password)) {
|
|
|
password = userDO.getMobile().substring(0, 5);
|
|
|
password = userDO.getMobile().substring(0, 5);
|
|
|
}
|
|
|
userDO.setPassword(MD5.md5Hex(password + "{" + userDO.getSalt() + "}"));
|
|
|
return userDao.save(userDO);
|
|
@ -72,6 +105,7 @@ public class UserService extends BaseJpaService<UserDO, UserDao> {
|
|
|
|
|
|
/**
|
|
|
* 根据用户手机号查找(手机号为登录账号)
|
|
|
*
|
|
|
* @param mobile
|
|
|
* @return
|
|
|
*/
|
|
@ -81,6 +115,7 @@ public class UserService extends BaseJpaService<UserDO, UserDao> {
|
|
|
|
|
|
/**
|
|
|
* 根据id查找用户
|
|
|
*
|
|
|
* @param id
|
|
|
* @return
|
|
|
*/
|
|
@ -90,67 +125,57 @@ public class UserService extends BaseJpaService<UserDO, UserDao> {
|
|
|
|
|
|
/**
|
|
|
* 判断手机号是否存在
|
|
|
*
|
|
|
* @param mobile
|
|
|
* @return
|
|
|
*/
|
|
|
public Boolean existMobile(String mobile){
|
|
|
if(StringUtils.isEmpty(mobile)) {
|
|
|
public Boolean existMobile(String mobile) {
|
|
|
if (StringUtils.isEmpty(mobile)) {
|
|
|
return null;
|
|
|
}
|
|
|
return userDao.existsByMobile(mobile);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断用户名是否存在
|
|
|
* @param username
|
|
|
* @return
|
|
|
*/
|
|
|
public Boolean existUserName(String username){
|
|
|
if(StringUtils.isEmpty(username)) {
|
|
|
return null;
|
|
|
}
|
|
|
return userDao.existsByUsername(username);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用户管理,获取用户基本信息列表
|
|
|
* @param name 模糊查询
|
|
|
*
|
|
|
* @param name 模糊查询
|
|
|
* @param saasId 精准匹配,为空查全部
|
|
|
* @param roleId 精准匹配,为空查全部
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String,Object>> queryBaseUserInfoList(String name,String saasId,String roleId){
|
|
|
List<Map<String,Object>> result = new ArrayList<>();
|
|
|
public List<Map<String, Object>> queryBaseUserInfoList(String name, String saasId, String roleId) {
|
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public Map<String,Object> findUserBaseInfo(String id){
|
|
|
public Map<String, Object> findUserBaseInfo(String id) {
|
|
|
|
|
|
Map<String,Object> userinfo = new HashedMap();
|
|
|
Map<String, Object> userinfo = new HashedMap();
|
|
|
|
|
|
UserDO user = userDao.findOne(id);
|
|
|
RoleDO role = roleDao.findByCode(user.getRoleCode());
|
|
|
|
|
|
userinfo.put("id",user.getId());
|
|
|
userinfo.put("name",user.getName());
|
|
|
userinfo.put("role",role.getName());
|
|
|
userinfo.put("roleCode",role.getCode());
|
|
|
userinfo.put("system",role.getSystem());
|
|
|
userinfo.put("id", user.getId());
|
|
|
userinfo.put("name", user.getName());
|
|
|
userinfo.put("role", role.getName());
|
|
|
userinfo.put("roleCode", role.getCode());
|
|
|
userinfo.put("system", role.getSystem());
|
|
|
|
|
|
if("admin".equals(role.getCode())){
|
|
|
if ("admin".equals(role.getCode())) {
|
|
|
|
|
|
}else if("saasAdmin".equals(role.getCode())){
|
|
|
} else if ("saasAdmin".equals(role.getCode())) {
|
|
|
|
|
|
if(org.apache.commons.lang3.StringUtils.isNotBlank(user.getId())){
|
|
|
if (org.apache.commons.lang3.StringUtils.isNotBlank(user.getId())) {
|
|
|
SaasDO saas = saasDao.findOne(user.getSaasId());
|
|
|
Map<String,Object> ss = new HashedMap();
|
|
|
ss.put("id",saas.getId());
|
|
|
ss.put("name",saas.getName());
|
|
|
userinfo.put("saas",ss);
|
|
|
Map<String, Object> ss = new HashedMap();
|
|
|
ss.put("id", saas.getId());
|
|
|
ss.put("name", saas.getName());
|
|
|
userinfo.put("saas", ss);
|
|
|
}
|
|
|
|
|
|
|
|
|
}else if("hosAdmin".equals(role.getCode())){
|
|
|
} else if ("hosAdmin".equals(role.getCode())) {
|
|
|
String sql = "SELECT " +
|
|
|
" g.code AS orgCode, " +
|
|
|
" g.province_code AS provinceCode, " +
|
|
@ -167,16 +192,273 @@ public class UserService extends BaseJpaService<UserDO, UserDao> {
|
|
|
" base_org g " +
|
|
|
" JOIN base_org_user u ON g.`code` = u.org_code " +
|
|
|
" WHERE " +
|
|
|
" u.user_id = '"+id+"'";
|
|
|
List<Map<String,Object>> org = jdbcTemplate.queryForList(sql);
|
|
|
userinfo.put("org",org.get(0));
|
|
|
" u.user_id = '" + id + "'";
|
|
|
List<Map<String, Object>> org = jdbcTemplate.queryForList(sql);
|
|
|
userinfo.put("org", org.get(0));
|
|
|
}
|
|
|
|
|
|
return userinfo;
|
|
|
}
|
|
|
|
|
|
// public Map<String,Object> findUserMenu(String id){
|
|
|
//
|
|
|
// }
|
|
|
/**
|
|
|
* 创建用户
|
|
|
*
|
|
|
* @param jsonData
|
|
|
* @return
|
|
|
*/
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public JSONObject createUser(String jsonData) {
|
|
|
JSONObject result = new JSONObject();
|
|
|
if (StringUtils.isEmpty(jsonData)) {
|
|
|
result.put("msg", "parameter jsonData is null");
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
JSONObject jsonObject = JSONObject.parseObject(jsonData);
|
|
|
JSONObject user = jsonObject.getJSONObject("user");
|
|
|
JSONArray org = jsonObject.getJSONArray("org");
|
|
|
if (null == user || CollectionUtils.isEmpty(org)) {
|
|
|
result.put("msg", "parameter user or org of jsonData is null");
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
//组装用户信息
|
|
|
UserDO userDO = null;
|
|
|
try {
|
|
|
userDO = objectMapper.readValue(user.toJSONString(), UserDO.class);
|
|
|
} catch (IOException e) {
|
|
|
result.put("msg", "convert user jsonObject to UserDO failed," + e.getCause());
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
registerWithIdcard(userDO);
|
|
|
|
|
|
//组装用户与机构的关联关系
|
|
|
List<BaseOrgUserDO> orgList = new ArrayList<>();
|
|
|
BaseOrgUserDO orgUserDO = null;
|
|
|
for (Object obj : org) {
|
|
|
try {
|
|
|
orgUserDO = objectMapper.readValue(obj.toString(), BaseOrgUserDO.class);
|
|
|
orgUserDO.setUserId(userDO.getId());
|
|
|
orgList.add(orgUserDO);
|
|
|
} catch (IOException e) {
|
|
|
result.put("msg", "convert org jsonObject to BaseOrgUserDO failed," + e.getCause());
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
|
baseOrgUserService.batchInsert(orgList);
|
|
|
JSONObject returnMsg = new JSONObject();
|
|
|
returnMsg.put("username",userDO.getUsername());
|
|
|
returnMsg.put("password",userDO.getSalt());
|
|
|
result.put("response", ConstantUtils.SUCCESS);
|
|
|
result.put("msg", returnMsg);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改用户
|
|
|
*
|
|
|
* @param jsonData
|
|
|
* @return
|
|
|
*/
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public JSONObject updateUser(String jsonData) {
|
|
|
JSONObject result = new JSONObject();
|
|
|
if (StringUtils.isEmpty(jsonData)) {
|
|
|
result.put("msg", "parameter jsonData is null");
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
JSONObject jsonObject = JSONObject.parseObject(jsonData);
|
|
|
JSONObject user = jsonObject.getJSONObject("user");
|
|
|
JSONArray org = jsonObject.getJSONArray("org");
|
|
|
if (null == user || CollectionUtils.isEmpty(org)) {
|
|
|
result.put("msg", "parameter user or org of jsonData is null");
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
//修改用户信息
|
|
|
UserDO userDO = null;
|
|
|
try {
|
|
|
userDO = objectMapper.readValue(user.toJSONString(), UserDO.class);
|
|
|
UserDO oldUser = userDao.findOne(userDO.getId());
|
|
|
userDO.setPassword(oldUser.getPassword());
|
|
|
} catch (IOException e) {
|
|
|
result.put("msg", "convert user jsonObject to UserDO failed," + e.getCause());
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
this.save(userDO);
|
|
|
|
|
|
//修改用户机构关联关系
|
|
|
Set<Object> roleIdList = baseOrgUserService.findIdListByUserId(userDO.getId());
|
|
|
BaseOrgUserDO orgUserDO = null;
|
|
|
try {
|
|
|
for (Object obj : org) {
|
|
|
orgUserDO = objectMapper.readValue(obj.toString(), BaseOrgUserDO.class);
|
|
|
orgUserDO.setUserId(userDO.getId());
|
|
|
if(roleIdList.contains(orgUserDO.getId())){
|
|
|
roleIdList.remove(orgUserDO.getId());
|
|
|
}
|
|
|
orgUserDO.setUserId(userDO.getId());
|
|
|
baseOrgUserService.save(orgUserDO);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
result.put("msg", "convert org jsonObject to BaseOrgUserDO failed," + e.getCause());
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
// 表示没有删除,只有修改
|
|
|
if (roleIdList.size() > 0) {
|
|
|
baseOrgUserService.delete(roleIdList.toArray());
|
|
|
}
|
|
|
|
|
|
result.put("response", ConstantUtils.SUCCESS);
|
|
|
result.put("msg", userDO);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取单个用户信息
|
|
|
* @param userId 用户id
|
|
|
* @return
|
|
|
*/
|
|
|
public JSONObject getOneUserInfo(String userId) throws Exception{
|
|
|
JSONObject result = new JSONObject();
|
|
|
if(StringUtils.isEmpty(userId)){
|
|
|
result.put("msg","parameter userId is null ");
|
|
|
result.put("response",ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
//用户基本信息
|
|
|
UserDO user = userDao.findOne(userId);
|
|
|
if(null == user){
|
|
|
result.put("msg","User not exist for id:" + userId);
|
|
|
result.put("response",ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
result.put("response",ConstantUtils.SUCCESS);
|
|
|
result.put("msg",user);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 租户只能给用户归属到自己租户底下的机构
|
|
|
* 机构管理员只能属于一个租户
|
|
|
* @param saasid
|
|
|
* @param userId
|
|
|
* @return
|
|
|
*/
|
|
|
public JSONObject generateUserOrgTree(String saasid,String userId,String roleCode) throws IOException {
|
|
|
JSONObject result = new JSONObject();
|
|
|
List<OrgTree> orgList = new ArrayList<>();
|
|
|
// 有归属租户加载saas底下的所有机构树列表,角色为机构管理员时不需要选管理机构
|
|
|
if(!StringUtils.isEmpty(saasid) && !StringUtils.endsWithIgnoreCase(RoleDO.BaseRoleType.admin.toString(),roleCode)){
|
|
|
StringBuilder sql = new StringBuilder("SELECT " +
|
|
|
" tree.* " +
|
|
|
" FROM " +
|
|
|
" org_tree tree, " +
|
|
|
" base_org org, " +
|
|
|
" base_org_saas saas " +
|
|
|
" WHERE " +
|
|
|
" org.code = saas.org_code " +
|
|
|
" and saas.saasid = '{saasid}' " +
|
|
|
" and (tree.code = org.code or tree.code = org.province_code or tree.code = org.city_code or tree.code = town_code) " +
|
|
|
" ORDER BY tree.name;");
|
|
|
orgList = jdbcTemplate.query(sql.toString().replace("{saasid}",saasid),new BeanPropertyRowMapper(OrgTree.class));
|
|
|
}
|
|
|
// 用户不为空表示修改,需要筛选出用户已经选择过的机构,设置checked为true
|
|
|
else if(!StringUtils.isEmpty(userId)){
|
|
|
Set<String> codeSet = baseOrgUserService.findorgCodeListByUserId(userId);
|
|
|
orgList.forEach( one -> {
|
|
|
if(codeSet.contains(one.getCode())){
|
|
|
one.setChecked(true);
|
|
|
}
|
|
|
});
|
|
|
/* StringBuilder sql = new StringBuilder("SELECT tree.* " +
|
|
|
"FROM " +
|
|
|
" org_tree tree, " +
|
|
|
" base_org org, " +
|
|
|
" base_org_user user " +
|
|
|
"WHERE " +
|
|
|
" org.code = user.org_code " +
|
|
|
" AND user.user_id = '{userId}' " +
|
|
|
" and (tree.code = org.code OR tree.code = org.province_code OR tree.code = org.city_code OR tree.code = town_code) " +
|
|
|
"ORDER BY tree.name;");
|
|
|
orgList = jdbcTemplate.query(sql.toString().replace("{userId}",userId),new BeanPropertyRowMapper(OrgTree.class));*/
|
|
|
}
|
|
|
|
|
|
if(CollectionUtils.isEmpty(orgList)){
|
|
|
result.put("msg","no org under this saasid:" + saasid);
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
String tree = orgTreeService.makeTree(orgList,false);
|
|
|
result.put("response", ConstantUtils.SUCCESS);
|
|
|
result.put("msg",objectMapper.readValue(tree,JSONArray.class));
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用户重置密码
|
|
|
* @param userId
|
|
|
* @return
|
|
|
*/
|
|
|
public JSONObject resetPassword(String userId){
|
|
|
JSONObject result = new JSONObject();
|
|
|
UserDO user = userDao.findOne(userId);
|
|
|
if(null == user){
|
|
|
result.put("response", ConstantUtils.FAIL);
|
|
|
result.put("msg","user not exist for id : " + userId);
|
|
|
return result;
|
|
|
}
|
|
|
registerWithIdcard(user);
|
|
|
result.put("response",ConstantUtils.SUCCESS);
|
|
|
result.put("msg", user.getSalt());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 判断用户账号是否存在
|
|
|
* @param code
|
|
|
* @return
|
|
|
*/
|
|
|
public Boolean existUsername(String code){
|
|
|
if(StringUtils.isEmpty(code)) {
|
|
|
return null;
|
|
|
}
|
|
|
return userDao.existsByUsername(code);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 生效或失效单个用户
|
|
|
* @param userId
|
|
|
* @param lock
|
|
|
* @return
|
|
|
*/
|
|
|
public JSONObject lockOrUnlocakUser(String userId,boolean lock){
|
|
|
JSONObject result = new JSONObject();
|
|
|
if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(lock)){
|
|
|
result.put("msg","parameter id or del is null");
|
|
|
result.put("response",ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
UserDO user = userDao.findOne(userId);
|
|
|
if( null == user ){
|
|
|
result.put("msg","user not exist for id:" + userId);
|
|
|
result.put("response",ConstantUtils.FAIL);
|
|
|
return result;
|
|
|
}
|
|
|
user.setLocked(lock);
|
|
|
user.setLockedDate(new Date());
|
|
|
this.save(user);
|
|
|
result.put("response",ConstantUtils.SUCCESS);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
}
|