123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981 |
- package com.jsh.erp.service.impl;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.jsh.erp.datasource.entities.*;
- import com.jsh.erp.datasource.mappers.TenantMapper;
- import com.jsh.erp.datasource.vo.SpinnerVO;
- import com.jsh.erp.exception.BusinessParamCheckingException;
- import com.jsh.erp.service.*;
- import com.jsh.erp.utils.*;
- import org.springframework.util.StringUtils;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.jsh.erp.constants.BusinessConstants;
- import com.jsh.erp.constants.ExceptionConstants;
- import com.jsh.erp.datasource.mappers.UserMapper;
- import com.jsh.erp.datasource.mappers.UserMapperEx;
- import com.jsh.erp.datasource.vo.TreeNodeEx;
- import com.jsh.erp.exception.BusinessRunTimeException;
- import com.jsh.erp.exception.JshException;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import java.security.NoSuchAlgorithmException;
- import java.util.*;
- @Service
- public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
- private Logger logger = LoggerFactory.getLogger(UserService.class);
- @Resource
- private UserMapper userMapper;
- @Resource
- private TenantMapper tenantMapper;
- @Resource
- private UserMapperEx userMapperEx;
- @Resource
- private OrgaUserRelService orgaUserRelService;
- @Resource
- private LogService logService;
- @Resource
- private UserService userService;
- @Resource
- private TenantService tenantService;
- @Resource
- private UserBusinessService userBusinessService;
- @Resource
- private RoleService roleService;
- @Resource
- private FunctionService functionService;
- @Resource
- private PlatformConfigService platformConfigService;
- @Resource
- private RedisService redisService;
- @Resource
- private OrganizationService organizationService;
- @Value("${tenant.userNumLimit}")
- private Integer userNumLimit;
- @Value("${tenant.tryDayLimit}")
- private Integer tryDayLimit;
- @Override
- public User getUser(long id) {
- User result=null;
- try{
- result=userMapper.selectByPrimaryKey(id);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- /**
- * 获取用户下拉框数据
- * @return
- */
- @Override
- public List<SpinnerVO> creatorSpinnerList() {
- return userMapper.creatorSpinnerList();
- }
- @Override
- public List<User> getUserListByIds(String ids)throws Exception {
- List<Long> idList = StringUtil.strToLongList(ids);
- List<User> list = new ArrayList<>();
- try{
- UserExample example = new UserExample();
- example.createCriteria().andIdIn(idList);
- list = userMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public List<User> getUser(HttpServletRequest request) throws Exception {
- List<User> list=null;
- try{
- //先校验是否登录,然后才能查询用户数据
- Long userId = this.getUserId(request);
- if(userId!=null) {
- UserExample example = new UserExample();
- example.createCriteria().andStatusEqualTo(BusinessConstants.USER_STATUS_NORMAL).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- list = userMapper.selectByExample(example);
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public List<UserEx> select(String userName, String loginName)throws Exception {
- List<UserEx> list=null;
- try {
- //先校验是否登录,然后才能查询用户数据
- HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
- Long userId = this.getUserId(request);
- if(userId!=null) {
- PageUtils.startPage();
- list = userMapperEx.selectByConditionUser(userName, loginName);
- for (UserEx ue : list) {
- String userType = "";
- if (ue.getId().equals(ue.getTenantId())) {
- userType = "租户";
- } else if (ue.getTenantId() == null) {
- userType = "超管";
- } else {
- userType = "普通";
- }
- ue.setUserType(userType);
- //是否经理
- String leaderFlagStr = "";
- if ("1".equals(ue.getLeaderFlag())) {
- leaderFlagStr = "是";
- } else {
- leaderFlagStr = "否";
- }
- ue.setLeaderFlagStr(leaderFlagStr);
- }
- }
- } catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public Long countUser(String userName, String loginName)throws Exception {
- Long result=null;
- try{
- result=userMapperEx.countsByUser(userName, loginName);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int insertUser(JSONObject obj, HttpServletRequest request)throws Exception {
- User user = JSONObject.parseObject(obj.toJSONString(), User.class);
- String password = "123456";
- //因密码用MD5加密,需要对密码进行转化
- try {
- password = Tools.md5Encryp(password);
- user.setPassword(password);
- } catch (NoSuchAlgorithmException e) {
- logger.error(">>>>>>>>>>>>>>转化MD5字符串错误 :" + e.getMessage());
- }
- int result=0;
- try{
- result=userMapper.insertSelective(user);
- logService.insertLog("用户",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(user.getLoginName()).toString(), request);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int updateUser(JSONObject obj, HttpServletRequest request) throws Exception{
- User user = JSONObject.parseObject(obj.toJSONString(), User.class);
- int result=0;
- try{
- result=userMapper.updateByPrimaryKeySelective(user);
- logService.insertLog("用户",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(user.getLoginName()).toString(), request);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int updateUserByObj(User user) throws Exception{
- logService.insertLog("用户",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(user.getId()).toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- int result=0;
- try{
- result=userMapper.updateByPrimaryKeySelective(user);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int resetPwd(String md5Pwd, Long id) throws Exception{
- int result=0;
- logService.insertLog("用户",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(id).toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- User u = getUser(id);
- String loginName = u.getLoginName();
- if("admin".equals(loginName)){
- logger.info("禁止重置超管密码");
- } else {
- User user = new User();
- user.setId(id);
- user.setPassword(md5Pwd);
- try{
- result=userMapper.updateByPrimaryKeySelective(user);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int deleteUser(Long id, HttpServletRequest request)throws Exception {
- return batDeleteUser(id.toString());
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batchDeleteUser(String ids, HttpServletRequest request)throws Exception {
- return batDeleteUser(ids);
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batDeleteUser(String ids) throws Exception{
- int result=0;
- StringBuffer sb = new StringBuffer();
- sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
- List<User> list = getUserListByIds(ids);
- for(User user: list){
- if(user.getId().equals(user.getTenantId())) {
- logger.error("异常码[{}],异常提示[{}],参数,ids:[{}]",
- ExceptionConstants.USER_LIMIT_TENANT_DELETE_CODE,ExceptionConstants.USER_LIMIT_TENANT_DELETE_MSG,ids);
- throw new BusinessRunTimeException(ExceptionConstants.USER_LIMIT_TENANT_DELETE_CODE,
- ExceptionConstants.USER_LIMIT_TENANT_DELETE_MSG);
- }
- sb.append("[").append(user.getLoginName()).append("]");
- }
- logService.insertLog("用户", sb.toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- String[] idsArray =ids.split(",");
- try{
- result=userMapperEx.batDeleteOrUpdateUser(idsArray);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- if(result<1){
- logger.error("异常码[{}],异常提示[{}],参数,ids:[{}]",
- ExceptionConstants.USER_DELETE_FAILED_CODE,ExceptionConstants.USER_DELETE_FAILED_MSG,ids);
- throw new BusinessRunTimeException(ExceptionConstants.USER_DELETE_FAILED_CODE,
- ExceptionConstants.USER_DELETE_FAILED_MSG);
- }
- return result;
- }
- /**
- * 校验验证码
- * @param code 验证码
- * @param uuid 唯一标识
- * @return 结果
- */
- @Override
- public void validateCaptcha(String code, String uuid) {
- if(StringUtil.isNotEmpty(code) && StringUtil.isNotEmpty(uuid)) {
- code = code.trim();
- uuid = uuid.trim();
- String verifyKey = BusinessConstants.CAPTCHA_CODE_KEY + uuid;
- String captcha = redisService.getCacheObject(verifyKey);
- redisService.deleteObject(verifyKey);
- if (captcha == null) {
- logger.error("异常码[{}],异常提示[{}]", ExceptionConstants.USER_JCAPTCHA_EXPIRE_CODE, ExceptionConstants.USER_JCAPTCHA_EXPIRE_MSG);
- throw new BusinessRunTimeException(ExceptionConstants.USER_JCAPTCHA_EXPIRE_CODE, ExceptionConstants.USER_JCAPTCHA_EXPIRE_MSG);
- }
- if (!code.equalsIgnoreCase(captcha)) {
- logger.error("异常码[{}],异常提示[{}]", ExceptionConstants.USER_JCAPTCHA_ERROR_CODE, ExceptionConstants.USER_JCAPTCHA_ERROR_MSG);
- throw new BusinessRunTimeException(ExceptionConstants.USER_JCAPTCHA_ERROR_CODE, ExceptionConstants.USER_JCAPTCHA_ERROR_MSG);
- }
- } else {
- logger.error("异常码[{}],异常提示[{}]", ExceptionConstants.USER_JCAPTCHA_EMPTY_CODE, ExceptionConstants.USER_JCAPTCHA_EMPTY_MSG);
- throw new BusinessRunTimeException(ExceptionConstants.USER_JCAPTCHA_EMPTY_CODE, ExceptionConstants.USER_JCAPTCHA_EMPTY_MSG);
- }
- }
- /**
- * 用户登录
- * @param loginName
- * @param password
- * @param request
- * @return
- * @throws Exception
- */
- @Override
- public Map<String, Object> login(String loginName, String password, HttpServletRequest request) throws Exception {
- Map<String, Object> data = new HashMap<>();
- String msgTip = "";
- User user = null;
- //判断用户是否已经登录过,登录过不再处理
- Object userId = redisService.getObjectFromSessionByKey(request,"userId");
- if (userId != null) {
- logger.info("====用户已经登录过, login 方法调用结束====");
- msgTip = "user already login";
- }
- //获取用户状态
- int userStatus = -1;
- try {
- redisService.deleteObjectBySession(request,"userId");
- userStatus = validateUser(loginName, password);
- } catch (Exception e) {
- logger.error(">>>>>>>>>>>>>用户 " + loginName + " 登录 login 方法 访问服务层异常====", e);
- msgTip = "access service exception";
- }
- String token = UUID.randomUUID().toString().replaceAll("-", "") + "";
- switch (userStatus) {
- case ExceptionCodeConstants.UserExceptionCode.USER_NOT_EXIST:
- msgTip = "user is not exist";
- break;
- case ExceptionCodeConstants.UserExceptionCode.USER_PASSWORD_ERROR:
- msgTip = "user password error";
- break;
- case ExceptionCodeConstants.UserExceptionCode.BLACK_USER:
- msgTip = "user is black";
- break;
- case ExceptionCodeConstants.UserExceptionCode.USER_ACCESS_EXCEPTION:
- msgTip = "access service error";
- break;
- case ExceptionCodeConstants.UserExceptionCode.BLACK_TENANT:
- msgTip = "tenant is black";
- break;
- case ExceptionCodeConstants.UserExceptionCode.EXPIRE_TENANT:
- msgTip = "tenant is expire";
- break;
- case ExceptionCodeConstants.UserExceptionCode.USER_CONDITION_FIT:
- msgTip = "user can login";
- //验证通过 ,可以登录,放入session,记录登录日志
- user = getUserByLoginName(loginName);
- if(user.getTenantId()!=null) {
- token = token + "_" + user.getTenantId();
- }
- redisService.storageObjectBySession(token,"userId",user.getId());
- break;
- default:
- break;
- }
- data.put("msgTip", msgTip);
- if(user!=null){
- //校验下密码是不是过于简单
- boolean pwdSimple = false;
- if(user.getPassword().equals(Tools.md5Encryp(BusinessConstants.USER_DEFAULT_PASSWORD))) {
- pwdSimple = true;
- }
- user.setPassword(null);
- redisService.storageObjectBySession(token,"clientIp", Tools.getLocalIp(request));
- logService.insertLogWithUserId(user.getId(), user.getTenantId(), "用户",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_LOGIN).append(user.getLoginName()).toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- data.put("token", token);
- data.put("user", user);
- data.put("roleName",userService.getRoleTypeByUserId(user.getId()).getName());
- data.put("pwdSimple", pwdSimple);
- }
- return data;
- }
- @Override
- public int validateUser(String loginName, String password) throws Exception {
- /**默认是可以登录的*/
- List<User> list = null;
- try {
- UserExample example = new UserExample();
- example.createCriteria().andLoginNameEqualTo(loginName).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- list = userMapper.selectByExample(example);
- if (null != list && list.size() == 0) {
- return ExceptionCodeConstants.UserExceptionCode.USER_NOT_EXIST;
- } else if(list.size() ==1) {
- if(list.get(0).getStatus()!=0) {
- return ExceptionCodeConstants.UserExceptionCode.BLACK_USER;
- }
- }
- } catch (Exception e) {
- logger.error(">>>>>>>>访问验证用户姓名是否存在后台信息异常", e);
- return ExceptionCodeConstants.UserExceptionCode.USER_ACCESS_EXCEPTION;
- }
- try {
- UserExample example = new UserExample();
- example.createCriteria().andLoginNameEqualTo(loginName).andPasswordEqualTo(password)
- .andStatusEqualTo(BusinessConstants.USER_STATUS_NORMAL).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- list = userMapper.selectByExample(example);
- if (null != list && list.size() == 0) {
- return ExceptionCodeConstants.UserExceptionCode.USER_PASSWORD_ERROR;
- }
- } catch (Exception e) {
- logger.error(">>>>>>>>>>访问验证用户密码后台信息异常", e);
- return ExceptionCodeConstants.UserExceptionCode.USER_ACCESS_EXCEPTION;
- }
- return ExceptionCodeConstants.UserExceptionCode.USER_CONDITION_FIT;
- }
- @Override
- public User getUserByLoginName(String loginName)throws Exception {
- UserExample example = new UserExample();
- example.createCriteria().andLoginNameEqualTo(loginName).andStatusEqualTo(BusinessConstants.USER_STATUS_NORMAL)
- .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<User> list=null;
- try{
- list= userMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- User user =null;
- if(list!=null&&list.size()>0){
- user = list.get(0);
- }
- return user;
- }
- @Override
- public int checkIsNameExist(Long id, String name)throws Exception {
- UserExample example = new UserExample();
- List<Byte> userStatus = new ArrayList<>();
- userStatus.add(BusinessConstants.USER_STATUS_NORMAL);
- example.createCriteria().andIdNotEqualTo(id).andLoginNameEqualTo(name).andStatusIn(userStatus)
- .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<User> list=null;
- try{
- list= userMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list==null?0:list.size();
- }
- /**
- * create by: cjl
- * description:
- * 获取当前用户信息
- * create time: 2019/1/24 10:01
- * @Param:
- * @return com.jsh.erp.datasource.entities.User
- */
- @Override
- public User getCurrentUser(){
- HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
- Long userId = Long.parseLong(redisService.getObjectFromSessionByKey(request,"userId").toString());
- return getUser(userId);
- }
- /**
- * 根据用户名查询id
- * @param loginName
- * @return
- */
- @Override
- public Long getIdByLoginName(String loginName) {
- Long userId = 0L;
- UserExample example = new UserExample();
- example.createCriteria().andLoginNameEqualTo(loginName).andStatusEqualTo(BusinessConstants.USER_STATUS_NORMAL)
- .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<User> list = userMapper.selectByExample(example);
- if(list!=null) {
- userId = list.get(0).getId();
- }
- return userId;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void addUserAndOrgUserRel(UserEx ue, HttpServletRequest request) throws Exception{
- if(BusinessConstants.DEFAULT_MANAGER.equals(ue.getLoginName())) {
- throw new BusinessRunTimeException(ExceptionConstants.USER_NAME_LIMIT_USE_CODE,
- ExceptionConstants.USER_NAME_LIMIT_USE_MSG);
- } else {
- logService.insertLog("用户",
- BusinessConstants.LOG_OPERATION_TYPE_ADD,
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- //检查用户名和登录名
- checkLoginName(ue);
- //新增用户信息
- ue= this.addUser(ue);
- if(ue==null){
- logger.error("异常码[{}],异常提示[{}],参数,[{}]",
- ExceptionConstants.USER_ADD_FAILED_CODE,ExceptionConstants.USER_ADD_FAILED_MSG);
- throw new BusinessRunTimeException(ExceptionConstants.USER_ADD_FAILED_CODE,
- ExceptionConstants.USER_ADD_FAILED_MSG);
- }
- //用户id,根据用户名查询id
- Long userId = getIdByLoginName(ue.getLoginName());
- if(ue.getRoleId()!=null){
- JSONObject ubObj = new JSONObject();
- ubObj.put("type", "UserRole");
- ubObj.put("keyid", userId);
- ubObj.put("value", "[" + ue.getRoleId() + "]");
- userBusinessService.insertUserBusiness(ubObj, request);
- }
- if(ue.getOrgaId()==null){
- //如果没有选择机构,就不建机构和用户的关联关系
- return;
- }
- if(ue.getOrgaId()!=null && "1".equals(ue.getLeaderFlag())){
- //检查当前机构是否存在经理
- List<User> checkList = userMapperEx.getListByOrgaId(ue.getId(), ue.getOrgaId());
- if(checkList.size()>0) {
- throw new BusinessRunTimeException(ExceptionConstants.USER_LEADER_IS_EXIST_CODE,
- ExceptionConstants.USER_LEADER_IS_EXIST_MSG);
- }
- }
- //新增用户和机构关联关系
- OrgaUserRel oul=new OrgaUserRel();
- //机构id
- oul.setOrgaId(ue.getOrgaId());
- oul.setUserId(userId);
- //用户在机构中的排序
- oul.setUserBlngOrgaDsplSeq(ue.getUserBlngOrgaDsplSeq());
- oul=orgaUserRelService.addOrgaUserRel(oul);
- if(oul==null){
- logger.error("异常码[{}],异常提示[{}],参数,[{}]",
- ExceptionConstants.ORGA_USER_REL_ADD_FAILED_CODE,ExceptionConstants.ORGA_USER_REL_ADD_FAILED_MSG);
- throw new BusinessRunTimeException(ExceptionConstants.ORGA_USER_REL_ADD_FAILED_CODE,
- ExceptionConstants.ORGA_USER_REL_ADD_FAILED_MSG);
- }
- }
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public UserEx addUser(UserEx ue) throws Exception{
- /**
- * 新增用户默认设置
- * 1、密码默认123456
- * 2是否系统自带默认为非系统自带
- * 3是否管理者默认为员工
- * 4默认用户状态为正常
- * */
- ue.setPassword(Tools.md5Encryp(BusinessConstants.USER_DEFAULT_PASSWORD));
- ue.setIsystem(BusinessConstants.USER_NOT_SYSTEM);
- if(ue.getIsmanager()==null){
- ue.setIsmanager(BusinessConstants.USER_NOT_MANAGER);
- }
- ue.setStatus(BusinessConstants.USER_STATUS_NORMAL);
- int result=0;
- try{
- result= userMapper.insertSelective(ue);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- if(result>0){
- return ue;
- }
- return null;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void registerUser(UserEx ue, Integer manageRoleId, HttpServletRequest request) throws Exception{
- /**
- * 多次创建事务,事物之间无法协同,应该在入口处创建一个事务以做协调
- */
- if(BusinessConstants.DEFAULT_MANAGER.equals(ue.getLoginName())) {
- throw new BusinessRunTimeException(ExceptionConstants.USER_NAME_LIMIT_USE_CODE,
- ExceptionConstants.USER_NAME_LIMIT_USE_MSG);
- } else {
- ue.setPassword(ue.getPassword());
- ue.setIsystem(BusinessConstants.USER_NOT_SYSTEM);
- if (ue.getIsmanager() == null) {
- ue.setIsmanager(BusinessConstants.USER_NOT_MANAGER);
- }
- ue.setStatus(BusinessConstants.USER_STATUS_NORMAL);
- try{
- userMapper.insertSelective(ue);
- Long userId = getIdByLoginName(ue.getLoginName());
- ue.setId(userId);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- //更新租户id
- User user = new User();
- user.setId(ue.getId());
- user.setTenantId(ue.getId());
- userService.updateUserTenant(user);
- //新增用户与角色的关系
- JSONObject ubObj = new JSONObject();
- ubObj.put("type", "UserRole");
- ubObj.put("keyid", ue.getId());
- JSONArray ubArr = new JSONArray();
- ubArr.add(manageRoleId);
- ubObj.put("value", ubArr.toString());
- ubObj.put("tenantId", ue.getId());
- userBusinessService.insertUserBusiness(ubObj, null);
- //创建租户信息
- JSONObject tenantObj = new JSONObject();
- tenantObj.put("tenantId", ue.getId());
- tenantObj.put("loginName",ue.getLoginName());
- tenantObj.put("userNumLimit", ue.getUserNumLimit());
- tenantObj.put("expireTime", ue.getExpireTime());
- tenantObj.put("remark", ue.getRemark());
- Tenant tenant = JSONObject.parseObject(tenantObj.toJSONString(), Tenant.class);
- tenant.setCreateTime(new Date());
- if(tenant.getUserNumLimit()==null) {
- tenant.setUserNumLimit(userNumLimit); //默认用户限制数量
- }
- if(tenant.getExpireTime()==null) {
- tenant.setExpireTime(Tools.addDays(new Date(), tryDayLimit)); //租户允许试用的天数
- }
- tenantMapper.insertSelective(tenant);
- logger.info("===============创建租户信息完成===============");
- }
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void updateUserTenant(User user) throws Exception{
- UserExample example = new UserExample();
- example.createCriteria().andIdEqualTo(user.getId());
- try{
- userMapper.updateByPrimaryKeySelective(user);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void updateUserAndOrgUserRel(UserEx ue, HttpServletRequest request) throws Exception{
- if(BusinessConstants.DEFAULT_MANAGER.equals(ue.getLoginName())) {
- throw new BusinessRunTimeException(ExceptionConstants.USER_NAME_LIMIT_USE_CODE,
- ExceptionConstants.USER_NAME_LIMIT_USE_MSG);
- } else {
- logService.insertLog("用户",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(ue.getId()).toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- //检查用户名和登录名
- checkLoginName(ue);
- //更新用户信息
- ue = this.updateUser(ue);
- if (ue == null) {
- logger.error("异常码[{}],异常提示[{}],参数,[{}]",
- ExceptionConstants.USER_EDIT_FAILED_CODE, ExceptionConstants.USER_EDIT_FAILED_MSG);
- throw new BusinessRunTimeException(ExceptionConstants.USER_EDIT_FAILED_CODE,
- ExceptionConstants.USER_EDIT_FAILED_MSG);
- }
- if(ue.getRoleId()!=null){
- JSONObject ubObj = new JSONObject();
- ubObj.put("type", "UserRole");
- ubObj.put("keyid", ue.getId());
- ubObj.put("value", "[" + ue.getRoleId() + "]");
- Long ubId = userBusinessService.checkIsValueExist("UserRole", ue.getId().toString());
- if(ubId!=null) {
- ubObj.put("id", ubId);
- userBusinessService.updateUserBusiness(ubObj, request);
- } else {
- userBusinessService.insertUserBusiness(ubObj, request);
- }
- }
- if (ue.getOrgaId() == null) {
- //如果没有选择机构,就不建机构和用户的关联关系
- return;
- }
- if(ue.getOrgaId()!=null && "1".equals(ue.getLeaderFlag())){
- //检查当前机构是否存在经理
- List<User> checkList = userMapperEx.getListByOrgaId(ue.getId(), ue.getOrgaId());
- if(checkList.size()>0) {
- throw new BusinessRunTimeException(ExceptionConstants.USER_LEADER_IS_EXIST_CODE,
- ExceptionConstants.USER_LEADER_IS_EXIST_MSG);
- }
- }
- //更新用户和机构关联关系
- OrgaUserRel oul = new OrgaUserRel();
- //机构和用户关联关系id
- oul.setId(ue.getOrgaUserRelId());
- //机构id
- oul.setOrgaId(ue.getOrgaId());
- //用户id
- oul.setUserId(ue.getId());
- //用户在机构中的排序
- oul.setUserBlngOrgaDsplSeq(ue.getUserBlngOrgaDsplSeq());
- if (oul.getId() != null) {
- //已存在机构和用户的关联关系,更新
- oul = orgaUserRelService.updateOrgaUserRel(oul);
- } else {
- //不存在机构和用户的关联关系,新建
- oul = orgaUserRelService.addOrgaUserRel(oul);
- }
- if (oul == null) {
- logger.error("异常码[{}],异常提示[{}],参数,[{}]",
- ExceptionConstants.ORGA_USER_REL_EDIT_FAILED_CODE, ExceptionConstants.ORGA_USER_REL_EDIT_FAILED_MSG);
- throw new BusinessRunTimeException(ExceptionConstants.ORGA_USER_REL_EDIT_FAILED_CODE,
- ExceptionConstants.ORGA_USER_REL_EDIT_FAILED_MSG);
- }
- }
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public UserEx updateUser(UserEx ue)throws Exception{
- int result =0;
- try{
- result=userMapper.updateByPrimaryKeySelective(ue);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- if(result>0){
- return ue;
- }
- return null;
- }
- /**
- * 检查登录名不能重复
- * create time: 2019/3/12 11:36
- * @Param: userEx
- * @return void
- */
- @Override
- public void checkLoginName(UserEx userEx)throws Exception{
- List<User> list=null;
- if(userEx==null){
- return;
- }
- Long userId=userEx.getId();
- //检查登录名
- if(!StringUtils.isEmpty(userEx.getLoginName())){
- String loginName=userEx.getLoginName();
- list=this.getUserListByloginName(loginName);
- if(list!=null&&list.size()>0){
- if(list.size()>1){
- //超过一条数据存在,该登录名已存在
- logger.error("异常码[{}],异常提示[{}],参数,loginName:[{}]",
- ExceptionConstants.USER_LOGIN_NAME_ALREADY_EXISTS_CODE,ExceptionConstants.USER_LOGIN_NAME_ALREADY_EXISTS_MSG,loginName);
- throw new BusinessRunTimeException(ExceptionConstants.USER_LOGIN_NAME_ALREADY_EXISTS_CODE,
- ExceptionConstants.USER_LOGIN_NAME_ALREADY_EXISTS_MSG);
- }
- //一条数据,新增时抛出异常,修改时和当前的id不同时抛出异常
- if(list.size()==1){
- if(userId==null||(userId!=null&&!userId.equals(list.get(0).getId()))){
- logger.error("异常码[{}],异常提示[{}],参数,loginName:[{}]",
- ExceptionConstants.USER_LOGIN_NAME_ALREADY_EXISTS_CODE,ExceptionConstants.USER_LOGIN_NAME_ALREADY_EXISTS_MSG,loginName);
- throw new BusinessRunTimeException(ExceptionConstants.USER_LOGIN_NAME_ALREADY_EXISTS_CODE,
- ExceptionConstants.USER_LOGIN_NAME_ALREADY_EXISTS_MSG);
- }
- }
- }
- }
- }
- /**
- * 通过登录名获取用户列表
- * */
- @Override
- public List<User> getUserListByloginName(String loginName){
- List<User> list =null;
- try{
- list=userMapperEx.getUserListByUserNameOrLoginName(null,loginName);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public List<TreeNodeEx> getOrganizationUserTree()throws Exception {
- List<TreeNodeEx> list =null;
- try{
- list=userMapperEx.getNodeTree();
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- /**
- * 根据用户id查询角色信息
- * @param userId
- * @return
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public Role getRoleTypeByUserId(long userId) throws Exception {
- Role role = new Role();
- List<UserBusiness> list = userBusinessService.getBasicData(String.valueOf(userId), "UserRole");
- UserBusiness ub = null;
- if(list.size() > 0) {
- ub = list.get(0);
- String values = ub.getValue();
- String roleId = null;
- if(values!=null) {
- values = values.replaceAll("\\[\\]",",").replace("[","").replace("]","");
- }
- String [] valueArray=values.split(",");
- if(valueArray.length>0) {
- roleId = valueArray[0];
- }
- role = roleService.getRoleWithoutTenant(Long.parseLong(roleId));
- }
- return role;
- }
- /**
- * 获取用户id
- * @param request
- * @return
- */
- @Override
- public Long getUserId(HttpServletRequest request) throws Exception{
- Object userIdObj = redisService.getObjectFromSessionByKey(request,"userId");
- Long userId = null;
- if(userIdObj != null) {
- userId = Long.parseLong(userIdObj.toString());
- }
- return userId;
- }
- /**
- * 用户的按钮权限
- * @param userId
- * @return
- * @throws Exception
- */
- @Override
- public JSONArray getBtnStrArrById(Long userId) throws Exception {
- JSONArray btnStrArr = new JSONArray();
- List<UserBusiness> userRoleList = userBusinessService.getBasicData(userId.toString(), "UserRole");
- if(userRoleList!=null && userRoleList.size()>0) {
- String roleValue = userRoleList.get(0).getValue();
- if(StringUtil.isNotEmpty(roleValue) && roleValue.indexOf("[")>-1 && roleValue.indexOf("]")>-1){
- roleValue = roleValue.replace("[", "").replace("]", ""); //角色id-单个
- List<UserBusiness> roleFunctionsList = userBusinessService.getBasicData(roleValue, "RoleFunctions");
- if(roleFunctionsList!=null && roleFunctionsList.size()>0) {
- String btnStr = roleFunctionsList.get(0).getBtnStr();
- if(StringUtil.isNotEmpty(btnStr)){
- btnStrArr = JSONArray.parseArray(btnStr);
- }
- }
- }
- }
- //将数组中的funId转为url
- JSONArray btnStrWithUrlArr = new JSONArray();
- if(btnStrArr.size()>0) {
- List<Function> functionList = functionService.getFunction();
- Map<Long, String> functionMap = new HashMap<>();
- for (Function function: functionList) {
- functionMap.put(function.getId(), function.getUrl());
- }
- for (Object obj : btnStrArr) {
- JSONObject btnStrObj = JSONObject.parseObject(obj.toString());
- Long funId = btnStrObj.getLong("funId");
- JSONObject btnStrWithUrlObj = new JSONObject();
- btnStrWithUrlObj.put("url", functionMap.get(funId));
- btnStrWithUrlObj.put("btnStr", btnStrObj.getString("btnStr"));
- btnStrWithUrlArr.add(btnStrWithUrlObj);
- }
- }
- return btnStrWithUrlArr;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batchSetStatus(Byte status, String ids, HttpServletRequest request)throws Exception {
- int result=0;
- List<User> list = getUserListByIds(ids);
- //选中的用户的数量
- int selectUserSize = list.size();
- //查询启用状态的用户的数量
- int enableUserSize = getUser(request).size();
- User userInfo = userService.getCurrentUser();
- Tenant tenant = tenantService.getTenantByTenantId(userInfo.getTenantId());
- if(tenant!=null) {
- if (selectUserSize + enableUserSize > tenant.getUserNumLimit() && status == 0) {
- throw new BusinessParamCheckingException(ExceptionConstants.USER_ENABLE_OVER_LIMIT_FAILED_CODE,
- ExceptionConstants.USER_ENABLE_OVER_LIMIT_FAILED_MSG);
- }
- }
- StringBuilder userStr = new StringBuilder();
- List<Long> idList = new ArrayList<>();
- for(User user: list) {
- if(user.getId().equals(user.getTenantId())) {
- //租户不能进行禁用
- } else {
- idList.add(user.getId());
- userStr.append(user.getLoginName()).append(" ");
- }
- }
- String statusStr ="";
- if(status == 0) {
- statusStr ="批量启用";
- } else if(status == 2) {
- statusStr ="批量禁用";
- }
- if(idList.size()>0) {
- User user = new User();
- user.setStatus(status);
- UserExample example = new UserExample();
- example.createCriteria().andIdIn(idList);
- result = userMapper.updateByExampleSelective(user, example);
- logService.insertLog("用户",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(userStr).append("-").append(statusStr).toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- } else {
- result = 1;
- }
- return result;
- }
- @Override
- public User getUserByWeixinCode(String weixinCode) throws Exception {
- String weixinUrl = platformConfigService.getPlatformConfigByKey("weixinUrl").getPlatformValue();
- String weixinAppid = platformConfigService.getPlatformConfigByKey("weixinAppid").getPlatformValue();
- String weixinSecret = platformConfigService.getPlatformConfigByKey("weixinSecret").getPlatformValue();
- String url = weixinUrl + "?appid=" + weixinAppid + "&secret=" + weixinSecret + "&js_code=" + weixinCode
- + "&grant_type=authorization_code";
- JSONObject jsonObject = HttpClient.httpGet(url);
- if(jsonObject!=null) {
- String weixinOpenId = jsonObject.getString("openid");
- if(StringUtil.isNotEmpty(weixinOpenId)) {
- return userMapperEx.getUserByWeixinOpenId(weixinOpenId);
- }
- }
- return null;
- }
- @Override
- public int weixinBind(String loginName, String password, String weixinCode) throws Exception {
- String weixinUrl = platformConfigService.getPlatformConfigByKey("weixinUrl").getPlatformValue();
- String weixinAppid = platformConfigService.getPlatformConfigByKey("weixinAppid").getPlatformValue();
- String weixinSecret = platformConfigService.getPlatformConfigByKey("weixinSecret").getPlatformValue();
- String url = weixinUrl + "?appid=" + weixinAppid + "&secret=" + weixinSecret + "&js_code=" + weixinCode
- + "&grant_type=authorization_code";
- JSONObject jsonObject = HttpClient.httpGet(url);
- if(jsonObject!=null) {
- String weixinOpenId = jsonObject.getString("openid");
- if(StringUtil.isNotEmpty(weixinOpenId)) {
- return userMapperEx.updateUserWithWeixinOpenId(loginName, password, weixinOpenId);
- }
- }
- return 0;
- }
- /**
- * 根据用户id回去审批用户id
- * @param uid 用户id
- * @param isParent 是否查询父级 true查询,false不查询
- * @return
- */
- @Override
- public Long getAuditUserIds(Long uid, boolean isParent) {
- Organization organization = orgaUserRelService.getOrganizationByUid(uid);
- Long orgaId = organization.getId();
- if (isParent && organization.getParentId() != null){
- orgaId = organization.getParentId();
- }
- return userMapper.getAuditUserIds(orgaId);
- }
- }
|