package com.zoniot.ccrc.service.impl; import com.alibaba.fastjson.JSON; import com.github.pagehelper.PageHelper; import com.zoniot.ccrc.commom.exception.ServiceException; import com.zoniot.ccrc.commom.model.Pagination; import com.zoniot.ccrc.commom.utils.JwtTokenUtil; import com.zoniot.ccrc.commom.utils.RedisUtil; import com.zoniot.ccrc.commom.utils.UserUtil; import com.zoniot.ccrc.dao.SiteMapper; import com.zoniot.ccrc.dao.SiteUserMapper; import com.zoniot.ccrc.dao.UserMapper; import com.zoniot.ccrc.dao.UserRoleMapper; import com.zoniot.ccrc.dto.DeleteUserDto; import com.zoniot.ccrc.dto.LoginUser; import com.zoniot.ccrc.dto.UserDto; import com.zoniot.ccrc.dto.UserRoleDto; import com.zoniot.ccrc.entity.Site; import com.zoniot.ccrc.entity.SiteUser; import com.zoniot.ccrc.entity.User; import com.zoniot.ccrc.entity.UserRole; import com.zoniot.ccrc.security.mobile.MobileLoginUserDetailService; import com.zoniot.ccrc.service.IotService; import com.zoniot.ccrc.service.TokenService; import com.zoniot.ccrc.service.UserService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigInteger; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import static com.google.common.collect.Lists.newArrayList; @Slf4j @Service public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; //@Resource //private AuthenticationManager authenticationManager; @Resource private UserRoleMapper userRoleMapper; @Autowired private MobileLoginUserDetailService userDetailsService; @Autowired private JwtTokenUtil jwtTokenUtil; @Autowired private TokenService tokenService; @Autowired private IotService iotService; @Resource private SiteUserMapper siteUserMapper; @Resource private SiteMapper siteMapper; @Autowired private RedisUtil redisUtil; @Override public int insertSelective(User user) { return userMapper.insertSelective(user); } @Override public int update(User user) { return userMapper.update(user); } @Override public User findUserById(Integer id) { return userMapper.findUserById(id); } @Override public String login(String username, String password) { log.info("begin login username=" + username + ",password=" + password); /*LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(username); BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); if (!encoder.matches(password, loginUser.getPassword())) { throw new BadCredentialsException("密码不正确"); } UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(loginUser, password, loginUser.getAuthorities()); Authentication authentication = authenticationManager.authenticate(upToken); SecurityContextHolder.getContext().setAuthentication(authentication); return tokenService.saveToken(loginUser);*/ return null; } @Override public String mobileLogin(String mobile, String password) { log.info("begin login username=" + mobile + ",password=" + password); /*LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(mobile); BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); if (!encoder.matches(password, loginUser.getPassword())) { throw new BadCredentialsException("密码不正确"); } MobileLoginAuthenticationToken upToken = new MobileLoginAuthenticationToken(loginUser, loginUser.getAuthorities()); Authentication authentication = authenticationManager.authenticate(upToken); SecurityContextHolder.getContext().setAuthentication(authentication);*/ return null; } @Override public String wechatLogin(User user) { log.info("begin wechatLogin user:" + JSON.toJSONString(user)); /*LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(user.getUsername()); BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities()); Authentication authentication = authenticationManager.authenticate(upToken); SecurityContextHolder.getContext().setAuthentication(authentication); return tokenService.saveToken(loginUser);*/ return null; } @Override public void logout() { LoginUser loginUser = UserUtil.getCurrentUser(); if (loginUser != null) { tokenService.deleteToken(loginUser.getLoginToken()); if (loginUser.getIotToken() != null) { iotService.logout(loginUser.getIotToken()); } } } @Override @Transactional public void addUser(String username, String name, String mobilePhone, Integer roleId, Integer orgId) { log.info("begin addUser username={},name={},roleId={},mobilePhone={}", username,name,roleId,mobilePhone); LoginUser loginUser = UserUtil.getCurrentUser(); /*User u = userMapper.findUserByUsername(username); if (u != null) { throw new ServiceException(-900, "用户名已存在"); } int result = userMapper.findUserByMobilePhoneUnique(null, mobilePhone); if (result > 0) { throw new ServiceException(-900, "该手机号码已存在"); }*/ if (loginUser.getType() == 1) { int result = userMapper.findUserByMobilePhoneUnique(null, mobilePhone); if (result > 0) { throw new ServiceException(-900, "该手机号码已存在"); } }else { int result = userMapper.findUserByMobilePhoneSiteUnique(null,loginUser.getSiteId(), mobilePhone); if (result > 0) { throw new ServiceException(-900, "该站点已存在该手机号码"); } } User u = userMapper.findUserByMobile(mobilePhone); if (u != null) { if (loginUser.getIsSuperAdmin() == 1) { throw new ServiceException(-900,"该账号是超级管理员,无法添加"); } if(!StringUtils.equals(username,u.getUsername()) || !StringUtils.equals(name,u.getName())){ u.setUsername(username); u.setName(name); userMapper.update(u); } }else { //添加用户 User user = new User(); user.setUsername(username); //user.setPassword(encode); user.setMobilePhone(mobilePhone); user.setName(name); user.setStatus(1); //user.setOrganId(organId != null ? organId : -1); user.setCreateBy(UserUtil.getCurrentUser().getUsername()); user.setCreateDate(LocalDateTime.now()); user.setUpdateBy(UserUtil.getCurrentUser().getUsername()); user.setUpdateDate(LocalDateTime.now()); user.setIsSuperAdmin(0); //user.setType(loginUser.getType() == 1 ? 2 : 3); userMapper.insertSelective(user); u = user; } if (loginUser.getType() != 1) { SiteUser siteUser = new SiteUser(); siteUser.setSiteId(loginUser.getSiteId()); siteUser.setUserId(u.getId()); siteUser.setStatus(1); siteUser.setIsAdmin(0); siteUser.setOrganId(orgId); //siteUser.setOrganId(organId != null ? organId : -1); siteUser.setType(3); siteUser.setCreateBy(loginUser.getUsername()); siteUser.setCreateDate(LocalDateTime.now()); siteUser.setUpdateBy(loginUser.getUsername()); siteUser.setUpdateDate(LocalDateTime.now()); siteUserMapper.insert(siteUser); //添加用户角色关系 UserRole userRole = new UserRole(); userRole.setUid(u.getId()); userRole.setRid(roleId); userRole.setStatus(1); userRole.setCreateBy(loginUser.getUsername()); userRole.setCreateDate(LocalDateTime.now()); userRole.setUpdateBy(loginUser.getUsername()); userRole.setUpdateDate(LocalDateTime.now()); userRoleMapper.insert(userRole); } log.info("end addUser"); } @Override @Transactional public void editUser(Integer userId, String name, String mobilePhone, Integer roleId, Integer orgId) { log.info("begin addUser userId=" + userId + ",name=" + name + ",roleId=" + roleId + ",mobilePhone=" + mobilePhone); LoginUser loginUser = UserUtil.getCurrentUser(); /*int mobilePhoneUniqueResult = userMapper.findUserByMobilePhoneUnique(userId, mobilePhone); if (mobilePhoneUniqueResult > 0) { throw new ServiceException(-900, "该手机号码已存在"); }*/ if (loginUser.getType() == 1) { int result = userMapper.findUserByMobilePhoneUnique(userId, mobilePhone); if (result > 0) { throw new ServiceException(-900, "该手机号码已存在"); } }else { int result = userMapper.findUserByMobilePhoneSiteUnique(userId,loginUser.getSiteId(), mobilePhone); if (result > 0) { throw new ServiceException(-900, "该站点已存在该手机号码"); } } User editUser = userMapper.findUserById(userId); User user = new User(); user.setId(userId); user.setName(name); user.setMobilePhone(mobilePhone); user.setUpdateBy(loginUser.getUsername()); user.setUpdateDate(LocalDateTime.now()); userMapper.update(user); //更新用户角色关系 if (loginUser.getType() != 1) { //先删除关系 userRoleMapper.deleteBySiteIdAndUserId(loginUser.getSiteId(), userId, loginUser.getUsername()); //添加用户角色关系 UserRole userRole = new UserRole(); userRole.setUid(user.getId()); userRole.setRid(roleId); userRole.setStatus(1); userRole.setCreateBy(loginUser.getUsername()); userRole.setCreateDate(LocalDateTime.now()); userRole.setUpdateBy(loginUser.getUsername()); userRole.setUpdateDate(LocalDateTime.now()); userRoleMapper.insert(userRole); } siteMapper.updateOrgId(userId,orgId); log.info("end editUser"); } @Override public void updateStatus(Integer id) { log.info("begin updateStatus userId " + id); LoginUser loginUser = UserUtil.getCurrentUser(); if (loginUser.getType() == 3) { throw new ServiceException(-900, "普通用户无法启用、禁用用户"); } User selectedUser = userMapper.findUserById(id); if(selectedUser != null) { selectedUser.setId(id); if(selectedUser.getEnable() == 1) { selectedUser.setEnable(0); } else { selectedUser.setEnable(1); } selectedUser.setUpdateBy(UserUtil.getUsername()); selectedUser.setUpdateDate(LocalDateTime.now()); this.update(selectedUser); log.info("end updateStatus User"); } else { log.info("用户不存在"); } } @Override @Transactional public void delectUser(Integer id) { log.info("begin delectUser userId=" + id); LoginUser loginUser = UserUtil.getCurrentUser(); User delectUser = userMapper.findUserById(id); User user = new User(); user.setId(id); user.setStatus(0); user.setUpdateBy(UserUtil.getUsername()); user.setUpdateDate(LocalDateTime.now()); int i = this.update(user); //删除站点用户关系 int j = userMapper.deleteByUserId(id, UserUtil.getUsername()); //删除用户角色 userRoleMapper.deleteByUserId(id, UserUtil.getUsername()); log.info("end delectUser"); } @Override public Pagination getUserList(String username, String name, String mobilePhone, Integer organId, Integer roleId, Integer programId, int pageNum, int pageSize) { LoginUser loginUser = UserUtil.getCurrentUser(); List list = new ArrayList<>(); if (loginUser.getIsSuperAdmin() == 1) { PageHelper.startPage(pageNum, pageSize); list = userMapper.findUserList(username, name, mobilePhone); } else { PageHelper.startPage(pageNum, pageSize); list = userMapper.findBySiteId(loginUser.getSiteId(), username, name, mobilePhone, organId, roleId, programId); } Pagination pageInfo = new Pagination<>(list); return pageInfo; } @Override @Transactional public void batchDelectUser(DeleteUserDto deleteUserDto) { log.info("begin batchDelectUser deleteUserDto:" + JSON.toJSONString(deleteUserDto)); /*int i = userMapper.batchDelectUser(deleteUserDto.getUserIds(), UserUtil.getCurrentUser().getUsername()); //删除站点用户关系 int j = userMapper.batchDeleteByUserIds(deleteUserDto.getUserIds(), UserUtil.getCurrentUser().getUsername()); //删除用户数据项关系 userRoleProgramService.batchDelByUserId(deleteUserDto.getUserIds(), UserUtil.getCurrentUser().getUsername()); //删除用户角色 userRoleMapper.batchDeleteByUserId(deleteUserDto.getUserIds(), UserUtil.getCurrentUser().getUsername());*/ if (deleteUserDto.getUserIds() != null && deleteUserDto.getUserIds().size() > 0) { for (Integer userId : deleteUserDto.getUserIds()) { this.delectUser(userId); } } log.info("end batchDelectUser"); } @Override public void changePassword(String oldPassword, String newPassword) { log.info("begin changePassword oldPassword=" + oldPassword + ",newPassword=" + newPassword); User user = UserUtil.getCurrentUser(); BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); if (!encoder.matches(oldPassword, user.getPassword())) { throw new ServiceException("密码不正确"); } String str = encoder.encode(newPassword); User newUser = new User(); newUser.setId(user.getId()); newUser.setPassword(str); user.setUpdateBy(UserUtil.getCurrentUser().getUsername()); user.setUpdateDate(LocalDateTime.now()); int result = userMapper.update(newUser); log.info("end changePassword result=" + result); } @Override public List unbindUser(Integer siteId) { return siteUserMapper.unbindUser(siteId); } @Override public List deleteQuery(List userIds) { LoginUser loginUser = UserUtil.getCurrentUser(); List list = newArrayList(); if (loginUser != null && loginUser.getIsSuperAdmin() == 1) { list = siteUserMapper.deleteQuery(userIds); } else { } return list; } @Override public User findUserByOpenid(String openid) { return userMapper.findUserByOpenid(openid); } @Override public List getAllUserList(Integer organId, String taskId) { LoginUser loginUser = UserUtil.getCurrentUser(); List list = newArrayList(); Integer siteId = null; if (loginUser != null && loginUser.getIsSuperAdmin() != 1) { siteId = loginUser.getSiteId(); } list = siteUserMapper.getAllUserList(siteId, organId); return list; } @Override public void changeSite(Integer siteId) { } //判断siteList中是否包含siteId private boolean checkSite(List siteList, Integer siteId) { return siteList.parallelStream().anyMatch(site -> site.getId().equals(siteId)); } @Override public void bindOpenId(String openId) { LoginUser loginUser = UserUtil.getCurrentUser(); User user = userMapper.findUserByOpenid(openId); if (user == null) { User userUpdate = new User(); userUpdate.setId(loginUser.getId()); userUpdate.setOpenid(openId); this.update(userUpdate); } } @Override public User findUserByName(String name, BigInteger sitId) { return userMapper.findUserByName(name,sitId); } @Override public void addIntegrationUser(String username, String mobilePhone) { User userTemp = userMapper.findUserByMobile(mobilePhone); if (userTemp != null) { User user = new User(); user.setId(userTemp.getId()); user.setMobilePhone(mobilePhone); user.setUsername(username); user.setUpdateBy("admin"); user.setUpdateDate(LocalDateTime.now()); userMapper.update(user); }else { //添加用户 User user = new User(); user.setUsername(username); user.setPassword(""); user.setMobilePhone(mobilePhone); //user.setName(name); user.setStatus(1); //user.setOrganId(organId != null ? organId : -1); user.setCreateBy("admin"); user.setCreateDate(LocalDateTime.now()); user.setUpdateBy("admin"); user.setUpdateDate(LocalDateTime.now()); user.setIsSuperAdmin(0); //user.setType(loginUser.getType() == 1 ? 2 : 3); userMapper.insertSelective(user); SiteUser siteUser = new SiteUser(); siteUser.setSiteId(1); siteUser.setUserId(user.getId()); siteUser.setStatus(1); siteUser.setIsAdmin(0); siteUser.setOrganId(-1); siteUser.setType(3); siteUser.setCreateBy("admin"); siteUser.setCreateDate(LocalDateTime.now()); siteUser.setUpdateBy("admin"); siteUser.setUpdateDate(LocalDateTime.now()); siteUserMapper.insert(siteUser); //添加用户角色关系 UserRole userRole = new UserRole(); userRole.setUid(user.getId()); userRole.setRid(4); userRole.setStatus(1); userRole.setCreateBy("admin"); userRole.setCreateDate(LocalDateTime.now()); userRole.setUpdateBy("admin"); userRole.setUpdateDate(LocalDateTime.now()); userRoleMapper.insert(userRole); } } @Override public void delectIntegrationUser(String mobilePhone) { User userTemp = userMapper.findUserByMobile(mobilePhone); if (userTemp != null) { Integer id = userTemp.getId(); User user = new User(); user.setId(id); user.setStatus(0); user.setUpdateBy("system"); user.setUpdateDate(LocalDateTime.now()); int i = this.update(user); //删除站点用户关系 int j = userMapper.deleteByUserId(id, "system"); //删除用户角色 userRoleMapper.deleteByUserId(id, "system"); } } }