|  |  |  | 
|---|
|  |  |  | package com.doumee.biz.system.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
|---|
|  |  |  | import com.doumee.biz.system.SystemUserBiz; | 
|---|
|  |  |  | import com.doumee.config.jwt.JwtProperties; | 
|---|
|  |  |  | import com.doumee.core.annotation.excel.ExcelImporter; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.model.LoginUserInfo; | 
|---|
|  |  |  | import com.doumee.core.utils.Constants; | 
|---|
|  |  |  | import com.doumee.core.utils.PwdCheckUtil; | 
|---|
|  |  |  | import com.doumee.core.utils.Utils; | 
|---|
|  |  |  | import com.doumee.dao.system.SystemDepartmentUserMapper; | 
|---|
|  |  |  | import com.doumee.dao.system.dto.*; | 
|---|
|  |  |  | import com.doumee.dao.system.dto.UpdatePwdDto; | 
|---|
|  |  |  | import com.doumee.dao.system.model.*; | 
|---|
|  |  |  | import com.doumee.service.system.*; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang3.RandomStringUtils; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.shiro.SecurityUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  | import org.springframework.web.multipart.MultipartFile; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.concurrent.TimeUnit; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | public class SystemUserBizImpl implements SystemUserBiz { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisTemplate<String,Object> redisTemplate; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private JwtProperties jwtProperties; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemUserService systemUserService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updatePwd(UpdatePwdDto dto) { | 
|---|
|  |  |  | //登录密码复杂度校验:6-20个字符,至少包含字母、数字及特殊字符2种 | 
|---|
|  |  |  | if(StringUtils.isBlank(dto.getNewPwd()) | 
|---|
|  |  |  | ||dto.getNewPwd().length()>20 | 
|---|
|  |  |  | ||dto.getNewPwd().length()<6 | 
|---|
|  |  |  | ||!PwdCheckUtil.checkPassword(dto.getNewPwd())){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,密码复杂度不满足要求:6-20个字符,至少包含字母、数字及特殊字符2种"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | SystemUser user = systemUserService.findById(dto.getUserId()); | 
|---|
|  |  |  | if (user.getDeleted()) { | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(), "用户不存在或已被删除"); | 
|---|
|  |  |  | 
|---|
|  |  |  | SystemUser newUser = new SystemUser(); | 
|---|
|  |  |  | newUser.setId(dto.getUserId()); | 
|---|
|  |  |  | newUser.setPassword(Utils.Secure.encryptPassword(dto.getNewPwd(), user.getSalt())); | 
|---|
|  |  |  | newUser.setNeedChangePwd(Constants.ONE); | 
|---|
|  |  |  | systemUserService.updateById(newUser); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("===================更新信息"); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //            log.error("redisTemplate:["+redisTemplate+"]\njwtProperties=:["+jwtProperties+"]\n"+"token:["+dto.getToken()+"]\nuser:"+dto.getLoginUserInfo()); | 
|---|
|  |  |  | if(redisTemplate!=null&&dto.getToken() !=null &&dto.getLoginUserInfo()!=null && jwtProperties!=null){ | 
|---|
|  |  |  | dto.getLoginUserInfo().setNeedChangePwd(Constants.ONE); | 
|---|
|  |  |  | redisTemplate.opsForValue().set(Constants.REDIS_TOKEN_KEY+dto.getToken(),JSONObject.toJSONString(dto.getLoginUserInfo()),jwtProperties.getExpiration(), TimeUnit.MILLISECONDS); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | //            log.error("redisTemplate:["+redisTemplate+"]\njwtProperties=:["+jwtProperties+"]\n"+"token:["+dto.getToken()+"]\nuser:"+dto.getLoginUserInfo()); | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | updateUserDto.setId(dto.getId()); | 
|---|
|  |  |  | updateUserDto.setUpdateUser(dto.getOperaUserId()); | 
|---|
|  |  |  | updateUserDto.setPassword(Utils.Secure.encryptPassword(dto.getPassword(), systemUser.getSalt())); | 
|---|
|  |  |  | updateUserDto.setNeedChangePwd(Constants.ZERO); | 
|---|
|  |  |  | systemUserService.updateById(updateUserDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "工号【"+systemUser.getEmpNo()+"】已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //验证手机号 | 
|---|
|  |  |  | if (StringUtils.isNotBlank(systemUser.getMobile())) { | 
|---|
|  |  |  | queryUserDto = new SystemUser(); | 
|---|
|  |  |  | queryUserDto.setDeleted(Boolean.FALSE); | 
|---|
|  |  |  | queryUserDto.setMobile(systemUser.getMobile()); | 
|---|
|  |  |  | user = systemUserService.findOne(queryUserDto); | 
|---|
|  |  |  | if (user != null) { | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "手机号【"+systemUser.getMobile()+"】已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 生成密码盐 | 
|---|
|  |  |  | String salt = RandomStringUtils.randomAlphabetic(6); | 
|---|
|  |  |  | // 生成密码 | 
|---|
|  |  |  | systemUser.setPassword(Utils.Secure.encryptPassword(systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.INITIAL_PASSWORD).getCode(), salt)); | 
|---|
|  |  |  | systemUser.setSalt(salt); | 
|---|
|  |  |  | systemUser.setSource(Constants.ZERO); | 
|---|
|  |  |  | systemUser.setNeedChangePwd(Constants.ZERO); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 创建用户记录 | 
|---|
|  |  |  | systemUser.setType(Constants.ZERO); | 
|---|
|  |  |  | 
|---|
|  |  |  | user = systemUserService.findOne(queryUserDto); | 
|---|
|  |  |  | if (user != null && !user.getId().equals(systemUser.getId())) { | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "工号【"+systemUser.getEmpNo()+"】已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //验证手机号 | 
|---|
|  |  |  | if (StringUtils.isNotBlank(systemUser.getMobile())) { | 
|---|
|  |  |  | queryUserDto = new SystemUser(); | 
|---|
|  |  |  | queryUserDto.setMobile(systemUser.getMobile()); | 
|---|
|  |  |  | queryUserDto.setDeleted(Boolean.FALSE); | 
|---|
|  |  |  | user = systemUserService.findOne(queryUserDto); | 
|---|
|  |  |  | if (user != null && !user.getId().equals(systemUser.getId())) { | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(), "手机号【"+systemUser.getMobile()+"】已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 修改用户 | 
|---|
|  |  |  | 
|---|
|  |  |  | .filter(s->StringUtils.isNotBlank(s)) | 
|---|
|  |  |  | .distinct() | 
|---|
|  |  |  | .collect(Collectors.toList()); | 
|---|
|  |  |  | String pwd =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.INITIAL_PASSWORD).getCode(); | 
|---|
|  |  |  | if (!CollectionUtils.isEmpty(collect)){ | 
|---|
|  |  |  | QueryWrapper<SystemDepartment> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.lambda().in(SystemDepartment::getName,collect); | 
|---|
|  |  |  | List<SystemDepartment> systemDepartments = systemDepartmentService.findList(wrapper); | 
|---|
|  |  |  | Map<Integer, String> collect1 = systemDepartments.stream().collect(Collectors.toMap(s -> s.getId(), s -> s.getName())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 生成密码盐 | 
|---|
|  |  |  | dataList.forEach(s->{ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | CreateSystemUserDTO systemUser = new CreateSystemUserDTO(); | 
|---|
|  |  |  | systemUser.setDepartmentId(getKey(collect1,s.getDepartmentName())); | 
|---|
|  |  |  | systemUser.setUsername(s.getUsername()); | 
|---|
|  |  |  | systemUser.setRealname(s.getUsername()); | 
|---|
|  |  |  | systemUser.setEmpNo(s.getEmpNo()); | 
|---|
|  |  |  | systemUser.setMobile(s.getMobile()); | 
|---|
|  |  |  | systemUser.setPassword(s.getMobile()); | 
|---|
|  |  |  | String salt = RandomStringUtils.randomAlphabetic(6); | 
|---|
|  |  |  | //                    systemUser.setPassword(s.getMobile()); | 
|---|
|  |  |  | // 生成密码  String pwd =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.INITIAL_PASSWORD).getCode(); | 
|---|
|  |  |  | systemUser.setPassword(Utils.Secure.encryptPassword(pwd, salt)); | 
|---|
|  |  |  | systemUser.setNeedChangePwd(Constants.ZERO); | 
|---|
|  |  |  | systemUser.setSalt(salt); | 
|---|
|  |  |  | systemUser.setCreateUser(loginUserInfo.getId()); | 
|---|
|  |  |  | systemUser.setUpdateUser(loginUserInfo.getId()); | 
|---|
|  |  |  | systemUser.setType(Constants.ZERO); | 
|---|
|  |  |  | 
|---|
|  |  |  | systemUser.setRealname(s.getUsername()); | 
|---|
|  |  |  | systemUser.setEmpNo(s.getEmpNo()); | 
|---|
|  |  |  | systemUser.setMobile(s.getMobile()); | 
|---|
|  |  |  | systemUser.setPassword(s.getMobile()); | 
|---|
|  |  |  | // 生成密码             String pwd =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.INITIAL_PASSWORD).getCode(); | 
|---|
|  |  |  | String salt = RandomStringUtils.randomAlphabetic(6); | 
|---|
|  |  |  | systemUser.setPassword(Utils.Secure.encryptPassword(pwd, salt)); | 
|---|
|  |  |  | systemUser.setNeedChangePwd(Constants.ZERO); | 
|---|
|  |  |  | //                    systemUser.setPassword(s.getMobile()); | 
|---|
|  |  |  | systemUser.setCreateUser(loginUserInfo.getId()); | 
|---|
|  |  |  | systemUser.setUpdateUser(loginUserInfo.getId()); | 
|---|
|  |  |  | systemUser.setType(Constants.ZERO); | 
|---|