package com.xkrs.service.impl; import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.dao.*; import com.xkrs.model.entity.*; import com.xkrs.model.qo.SysUserQo; import com.xkrs.model.vo.SysUserVo; import com.xkrs.service.RedisService; import com.xkrs.service.SysUserService; import com.xkrs.utils.DateTimeUtil; import org.apache.http.util.TextUtils; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.security.crypto.keygen.KeyGenerators; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Optional; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; import static com.xkrs.utils.DateTimeUtil.dateTimeToString; import static com.xkrs.utils.EncryptDecryptUtil.encry256; /** * 系统用户服务实现 * * @author tajochen */ @Service public class SysUserServiceImpl implements SysUserService { @Resource private SysUserDao sysUserDao; @Resource private SysRoleDao sysRoleDao; @Resource private RelUserRoleDao relUserRoleDao; @Resource private RelRoleAuthorityDao relRoleAuthorityDao; @Resource private RedisService redisService; @Resource private NationwideDao nationwideDao; @Resource private CityDao cityDao; @Resource private CountyDao countyDao; @Resource private StreetDao streetDao; /** * 检查用户名是否存在 * * @param userName * @return */ @Override public boolean checkUserName(String userName) { int num = sysUserDao.checkUserName(userName); return (num == 0); } /** * 新增用户 * * @param sysUserQo */ @Transactional(rollbackFor = Exception.class) @Override public String addUser(SysUserQo sysUserQo) { // 获取区域信息 Locale locale = LocaleContextHolder.getLocale(); String salt = KeyGenerators.string().generateKey(); SysUserEntity sysUserEntity = new SysUserEntity(); sysUserEntity.setUserName(sysUserQo.getUserName()); String o = (String) redisService.get(sysUserQo.getUserName()); if ("".equals(o) || o == null) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "请先发送验证码!", locale); } if (!redisService.get(sysUserQo.getUserName()).equals(sysUserQo.getVerificationCode())) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "验证码错误,请重新输入!", locale); } if ("".equals(sysUserQo.getReallyName()) || sysUserQo.getReallyName() == null) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "真实姓名不能为空!", locale); } sysUserEntity.setReallyName(sysUserQo.getReallyName()); sysUserEntity.setSalt(salt); sysUserEntity.setPassword(encry256(sysUserQo.getPassword() + salt)); sysUserEntity.setTelephone(sysUserQo.getUserName()); String countyCode = sysUserQo.getCountyCode(); String countyName = sysUserQo.getCountyName(); if (TextUtils.isEmpty(countyCode)) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "省市区街道编码不能为空!", locale); } if (TextUtils.isEmpty(countyName)) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "省市区街道名称不能为空!", locale); } if (9 == countyCode.length()) { Optional byStreetCodeOptional = streetDao.findByStreetCode(countyCode); if (byStreetCodeOptional.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "街道编码不存在!", locale); } Optional byStreetNameOptional = streetDao.findByStreetName(countyName); if (byStreetNameOptional.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale); } sysUserEntity.setActiveFlag(0); sysUserEntity.setAccountType("街道级"); } else if ("0000".equals(countyCode.substring(2))) { List byProCode = cityDao.findByProCode(countyCode); if (byProCode == null || byProCode.size() == 0) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "省编码不存在!", locale); } List byProName = cityDao.findByProName(countyName); if (byProName == null || byProName.size() == 0) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale); } sysUserEntity.setActiveFlag(1); sysUserEntity.setAccountType("省级"); } else if ("00".equals(countyCode.substring(4)) && !"0000".equals(countyCode.substring(2))) { List byCityCode = countyDao.findByCityCode(countyCode); if (byCityCode == null || byCityCode.size() == 0) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "市编码不存在!", locale); } List byCityName = countyDao.findByCityName(countyName); if (byCityName == null || byCityName.size() == 0) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale); } sysUserEntity.setActiveFlag(1); sysUserEntity.setAccountType("市级"); } else { List byCountyCode = countyDao.findByCountyCode(countyCode); if (byCountyCode == null || byCountyCode.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "区县编码不存在!", locale); } List byCountyName = countyDao.findByCountyName(countyName); if (byCountyName == null || byCountyName.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale); } sysUserEntity.setActiveFlag(0); sysUserEntity.setAccountType("县级"); } if (9 == countyCode.length()) {//街道账号5天后过期 sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(5L))); } else {//省市区县账号3天后过期 sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(3L))); } sysUserEntity.setStatusCode(0); sysUserEntity.setAddTime(dateTimeToString(LocalDateTime.now())); sysUserEntity.setDeleteFlag(0); sysUserEntity.setDayNum(7); sysUserEntity.setLoginNum(0); sysUserEntity.setCountyCode(countyCode); sysUserEntity.setCountyName(countyName); sysUserDao.save(sysUserEntity); RelUserRoleEntity relUserRoleEntity = new RelUserRoleEntity(); relUserRoleEntity.setUserId(sysUserEntity.getId().longValue()); relUserRoleEntity.setRoleId(2); RelRoleAuthorityEntity relRoleAuthorityEntity = new RelRoleAuthorityEntity(); relRoleAuthorityEntity.setRoleId(2); relRoleAuthorityEntity.setAuthorityId(2); relRoleAuthorityEntity.setUserId(sysUserEntity.getId()); relUserRoleDao.save(relUserRoleEntity); relRoleAuthorityDao.save(relRoleAuthorityEntity); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "注册成功!", locale); } /** * 查询所有用户Vo * * @return */ /*@Transactional(rollbackFor=Exception.class) @Override public Iterable getAllSysUser() { Iterable ls = sysUserDao.selectAllSysUser(); return ls; }*/ @Transactional(rollbackFor = Exception.class) @Override public SysUserEntity getSysUserByUserName(String userName) { return sysUserDao.selectByUserName(userName); } @Transactional(rollbackFor = Exception.class) @Override public int updateSysUserLogin(String userName, String ipAddress) { return sysUserDao.updateSysUserLogin(userName, ipAddress); } /** * 更新用户 * * @param sysUserQo * @return */ @Transactional(rollbackFor = Exception.class) @Override public int updateSysUser(SysUserQo sysUserQo) { SysUserEntity sysUserEntity = new SysUserEntity(); sysUserEntity.setReallyName(sysUserQo.getReallyName()); sysUserDao.save(sysUserEntity); return 1; } /** * 软删除普通用户 * * @param id * @return */ @Transactional(rollbackFor = Exception.class) @Override public int softDeleteGeneralUser(Integer id) { String adminRole = "role_administor"; String sysRole = "role_system_manager"; List list = sysRoleDao.selectByUserId(id); if (list.size() > 0) { SysRoleEntity sysRoleEntity = list.get(1); if (sysRole.equals(sysRoleEntity.getRoleName()) || adminRole.equals(sysRoleEntity.getRoleName())) { sysUserDao.softDeleteGeneralUserById(id); return 0; } } return 1; } /** * 查询用户的信息 * * @return */ @Override public List selectAllUser() { return sysUserDao.selectAll(); } /** * 启用 * * @param userId */ @Transactional(rollbackFor = Exception.class) @Override public void updateEnable(Integer userId) { sysUserDao.updateEnable(userId); } /** * 禁用 * * @param userId */ @Transactional(rollbackFor = Exception.class) @Override public void updateDisable(Integer userId) { sysUserDao.updateDisable(userId); } /** * 用户修改密码 * * @param oldPassword * @param newPassword * @param confirmPassword * @param sysUserEntity * @return */ @Transactional(rollbackFor = Exception.class) @Override public String updatePassword(String oldPassword, String newPassword, String confirmPassword, SysUserEntity sysUserEntity) { Locale locale = LocaleContextHolder.getLocale(); String oldFrontPassword = encry256(oldPassword + sysUserEntity.getSalt()); if (!oldFrontPassword.equals(sysUserEntity.getPassword())) { return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "旧密码输入错误", locale); } if (!newPassword.equals(confirmPassword)) { return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "两次密码输入不一致", locale); } String newAfterPassword = encry256(newPassword + sysUserEntity.getSalt()); sysUserDao.updatePassword(sysUserEntity.getId(), newAfterPassword); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "密码修改成功", locale); } /** * 管理员修改用户的密码 * * @param userId * @param newPassword * @param confirmPassword * @return */ @Transactional(rollbackFor = Exception.class) @Override public String adminUpdatePassword(Integer userId, String newPassword, String confirmPassword) { Locale locale = LocaleContextHolder.getLocale(); if (!newPassword.equals(confirmPassword)) { return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "两次密码输入不一致", locale); } SysUserEntity sysUserEntity = sysUserDao.selectByUserId(userId); String newAfterPassword = encry256(newPassword + sysUserEntity.getSalt()); sysUserDao.updatePassword(userId, newAfterPassword); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "密码修改成功", locale); } @Override public SysUserVo getUserByUserName(String userName) { return sysUserDao.selectUserByUserName(userName); } /** * 按天数修改账号到期时间 * * @param userId * @param dayNum * @return */ @Transactional(rollbackFor = Exception.class) @Override public String updateExpireDateDay(Integer userId, Integer dayNum) { Locale locale = LocaleContextHolder.getLocale(); long l = dayNum.longValue(); LocalDateTime now = LocalDateTime.now(); SysUserEntity sysUserEntity = sysUserDao.selectByUserId(userId); if (DateTimeUtil.dateTimeToString(now).compareTo(sysUserEntity.getOverTime()) > 0) { LocalDateTime localDateTime = now.plusDays(l); String time = dateTimeToString(localDateTime); sysUserDao.updateOverTime(userId, time); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } else { LocalDateTime localDateTime = DateTimeUtil.stringToDateTime(sysUserEntity.getOverTime()); LocalDateTime localDateTime1 = localDateTime.plusDays(l); String time = dateTimeToString(localDateTime1); sysUserDao.updateOverTime(userId, time); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } } /** * 按月份修改账号过期时间 * * @param userId * @param dayNum * @return */ @Transactional(rollbackFor = Exception.class) @Override public String updateExpireDateMonth(Integer userId, Integer dayNum) { Locale locale = LocaleContextHolder.getLocale(); long l = dayNum.longValue(); LocalDateTime now = LocalDateTime.now(); SysUserEntity sysUserEntity = sysUserDao.selectByUserId(userId); if (DateTimeUtil.dateTimeToString(now).compareTo(sysUserEntity.getOverTime()) > 0) { LocalDateTime localDateTime = now.plusMonths(l); String time = dateTimeToString(localDateTime); sysUserDao.updateOverTime(userId, time); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } else { LocalDateTime localDateTime = DateTimeUtil.stringToDateTime(sysUserEntity.getOverTime()); LocalDateTime localDateTime1 = localDateTime.plusMonths(l); String time = dateTimeToString(localDateTime1); sysUserDao.updateOverTime(userId, time); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } } /** * 按年份修改账号到期时间 * * @param userId * @param dayNum * @return */ @Transactional(rollbackFor = Exception.class) @Override public String updateExpireDateYear(Integer userId, Integer dayNum) { Locale locale = LocaleContextHolder.getLocale(); long l = dayNum.longValue(); LocalDateTime now = LocalDateTime.now(); SysUserEntity sysUserEntity = sysUserDao.selectByUserId(userId); if (DateTimeUtil.dateTimeToString(now).compareTo(sysUserEntity.getOverTime()) > 0) { LocalDateTime localDateTime = now.plusYears(l); String time = dateTimeToString(localDateTime); sysUserDao.updateOverTime(userId, time); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } else { LocalDateTime localDateTime = DateTimeUtil.stringToDateTime(sysUserEntity.getOverTime()); LocalDateTime localDateTime1 = localDateTime.plusYears(l); String time = dateTimeToString(localDateTime1); sysUserDao.updateOverTime(userId, time); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } } /** * 设置账号永久有效 * * @param userId * @return */ @Transactional(rollbackFor = Exception.class) @Override public String updatePermanent(Integer userId) { Locale locale = LocaleContextHolder.getLocale(); sysUserDao.updateDayNum(userId); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } /** * 修改账号到期时间 * * @param userId * @param overTime * @return */ @Transactional(rollbackFor = Exception.class) @Override public String updateOverTime(Integer userId, String overTime) { Locale locale = LocaleContextHolder.getLocale(); sysUserDao.updateOverTime(userId, overTime); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } /** * 记录用户登录次数 * * @param userId * @param loginNum */ @Transactional(rollbackFor = Exception.class) @Override public void updateLoginNum(Integer userId, Integer loginNum) { sysUserDao.updateLoginNum(userId, loginNum); } /** * 记录用户登录时间 * * @param userId * @param loginLastTime */ @Transactional(rollbackFor = Exception.class) @Override public void updateLoginLastTime(Integer userId, String loginLastTime) { sysUserDao.updateLoginLastTime(userId, loginLastTime); } @Override public List> findCityByProCode(String proCode) { return nationwideDao.findCityByProCode(proCode); } @Override public List> findCountyByProCode(String proCode) { return nationwideDao.findCountyByProCode(proCode); } @Override public List> findCountyByCityCode(String cityCode) { return nationwideDao.findCountyByCityCode(cityCode); } }