|  | @@ -5,10 +5,12 @@ import cn.hutool.core.util.ObjectUtil;
 | 
	
		
			
				|  |  |  import com.alibaba.excel.EasyExcel;
 | 
	
		
			
				|  |  |  import com.alibaba.excel.context.AnalysisContext;
 | 
	
		
			
				|  |  |  import com.alibaba.excel.event.AnalysisEventListener;
 | 
	
		
			
				|  |  | +import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  | +import com.google.common.collect.Lists;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.constant.UserConstants;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.domain.OrgTreeReq;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.domain.OrgTreeResp;
 | 
	
	
		
			
				|  | @@ -25,10 +27,6 @@ import com.xunmei.system.api.domain.SysOrg;
 | 
	
		
			
				|  |  |  import com.xunmei.system.api.domain.SysRole;
 | 
	
		
			
				|  |  |  import com.xunmei.system.api.domain.SysUser;
 | 
	
		
			
				|  |  |  import com.xunmei.system.api.dto.SysPlanOrgDTO;
 | 
	
		
			
				|  |  | -import com.xunmei.system.domain.SysPost;
 | 
	
		
			
				|  |  | -import com.xunmei.system.domain.SysRoleMenu;
 | 
	
		
			
				|  |  | -import com.xunmei.system.domain.SysUserPost;
 | 
	
		
			
				|  |  | -import com.xunmei.system.domain.SysUserRole;
 | 
	
		
			
				|  |  |  import com.xunmei.system.domain.*;
 | 
	
		
			
				|  |  |  import com.xunmei.system.domain.vo.SysMasterSubUserVO;
 | 
	
		
			
				|  |  |  import com.xunmei.system.domain.vo.SysUserListVo;
 | 
	
	
		
			
				|  | @@ -41,6 +39,7 @@ import com.xunmei.system.service.ISysUserService;
 | 
	
		
			
				|  |  |  import com.xunmei.system.util.SecurityUserExport;
 | 
	
		
			
				|  |  |  import com.xunmei.system.util.UserExport;
 | 
	
		
			
				|  |  |  import com.xunmei.system.util.UserImport;
 | 
	
		
			
				|  |  | +import com.xunmei.system.util.UserImportErrorMsg;
 | 
	
		
			
				|  |  |  import org.slf4j.Logger;
 | 
	
		
			
				|  |  |  import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
	
		
			
				|  | @@ -52,10 +51,8 @@ import org.springframework.web.multipart.MultipartFile;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import javax.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  |  import javax.validation.Validator;
 | 
	
		
			
				|  |  | -import java.util.ArrayList;
 | 
	
		
			
				|  |  | -import java.util.Date;
 | 
	
		
			
				|  |  | -import java.util.List;
 | 
	
		
			
				|  |  | -import java.util.Optional;
 | 
	
		
			
				|  |  | +import java.net.URLEncoder;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  |  import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import static com.xunmei.system.controller.SysUserController.multipartFileToFile;
 | 
	
	
		
			
				|  | @@ -816,23 +813,20 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public List<SysMasterSubUserVO> selectSwitchUserList(Long masterUserId,Long subUserId) {
 | 
	
		
			
				|  |  | -        List<SysMasterSubUserVO> result=new ArrayList<>();
 | 
	
		
			
				|  |  | -        if(ObjectUtil.isNotNull(masterUserId))
 | 
	
		
			
				|  |  | -        {
 | 
	
		
			
				|  |  | +    public List<SysMasterSubUserVO> selectSwitchUserList(Long masterUserId, Long subUserId) {
 | 
	
		
			
				|  |  | +        List<SysMasterSubUserVO> result = new ArrayList<>();
 | 
	
		
			
				|  |  | +        if (ObjectUtil.isNotNull(masterUserId)) {
 | 
	
		
			
				|  |  |              result = baseMapper.selectSwitchUserList(masterUserId);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          // 子账号 可以切回 主账号
 | 
	
		
			
				|  |  | -        if(ObjectUtil.isNotEmpty(subUserId) && !result.isEmpty())
 | 
	
		
			
				|  |  | -        {
 | 
	
		
			
				|  |  | +        if (ObjectUtil.isNotEmpty(subUserId) && !result.isEmpty()) {
 | 
	
		
			
				|  |  |              Optional<SysMasterSubUserVO> first = result.stream().filter(x -> ObjectUtil.equal(x.getSubUserId(), subUserId)).findFirst();
 | 
	
		
			
				|  |  | -            if(first.isPresent())
 | 
	
		
			
				|  |  | -            {
 | 
	
		
			
				|  |  | +            if (first.isPresent()) {
 | 
	
		
			
				|  |  |                  result.remove(first.get());
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              // 手动将主账号加入
 | 
	
		
			
				|  |  | -            SysMasterSubUserVO temp =new SysMasterSubUserVO();
 | 
	
		
			
				|  |  | +            SysMasterSubUserVO temp = new SysMasterSubUserVO();
 | 
	
		
			
				|  |  |              temp.setMasterUserId(masterUserId);
 | 
	
		
			
				|  |  |              SysUser masterSysUser = selectUserById(masterUserId);
 | 
	
		
			
				|  |  |              temp.setSubUserId(masterUserId);
 | 
	
	
		
			
				|  | @@ -842,7 +836,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              SysOrg org = sysOrgMapper.selectOne(new LambdaQueryWrapper<SysOrg>()
 | 
	
		
			
				|  |  |                      .eq(SysOrg::getId, masterSysUser.getOrgId())
 | 
	
		
			
				|  |  | -                    .select(SysOrg::getType, SysOrg::getShortName,SysOrg::getPath));
 | 
	
		
			
				|  |  | +                    .select(SysOrg::getType, SysOrg::getShortName, SysOrg::getPath));
 | 
	
		
			
				|  |  |              if (ObjectUtil.isNotEmpty(org)) {
 | 
	
		
			
				|  |  |                  temp.setSubUserOrgName(org.getShortName());
 | 
	
		
			
				|  |  |              }
 | 
	
	
		
			
				|  | @@ -873,29 +867,128 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public void importRole(MultipartFile file, HttpServletResponse response) {
 | 
	
		
			
				|  |  | -        List<UserImport> list = new ArrayList<>();
 | 
	
		
			
				|  |  | +        List<UserImportErrorMsg> errorList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        Map<String, Long> roleMap = new HashMap<>();
 | 
	
		
			
				|  |  | +        Map<Long, List<Long>> userMap = new HashMap<>();
 | 
	
		
			
				|  |  |          EasyExcel.read(multipartFileToFile(file), UserImport.class, new AnalysisEventListener<UserImport>() {
 | 
	
		
			
				|  |  |              // 每解析一行数据,该方法会被调用一次
 | 
	
		
			
				|  |  |              @Override
 | 
	
		
			
				|  |  |              public void invoke(UserImport user, AnalysisContext analysisContext) {
 | 
	
		
			
				|  |  | -                if (user != null) {
 | 
	
		
			
				|  |  | -                    list.add(user);
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                validatorParam(user, errorList, roleMap, userMap);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              // 全部解析完成被调用
 | 
	
		
			
				|  |  |              @Override
 | 
	
		
			
				|  |  |              public void doAfterAllAnalysed(AnalysisContext analysisContext) {
 | 
	
		
			
				|  |  | -                if (list.isEmpty()){
 | 
	
		
			
				|  |  | +                if (userMap.isEmpty()) {
 | 
	
		
			
				|  |  |                      return;
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | +                for (Map.Entry<Long, List<Long>> entry : userMap.entrySet()) {
 | 
	
		
			
				|  |  | +                    Long userId = entry.getKey();
 | 
	
		
			
				|  |  | +                    List<Long> roleIdList = entry.getValue();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +                    userRoleMapper.deleteUserRoleByUserId(userId);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +                    saveUserRole(userId, roleIdList);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }).sheet().doRead();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (errorList.isEmpty()){
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            // 设置响应头
 | 
	
		
			
				|  |  | +            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户数据.xlsx", "utf-8"));
 | 
	
		
			
				|  |  | +            response.setContentType("application/octet-stream;charset=UTF-8");
 | 
	
		
			
				|  |  | +            response.setCharacterEncoding("utf-8");
 | 
	
		
			
				|  |  | +            // 数据导出
 | 
	
		
			
				|  |  | +            EasyExcel.write(response.getOutputStream(), UserImportErrorMsg.class)
 | 
	
		
			
				|  |  | +                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("用户数据.xlsx").doWrite(errorList);
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            // 重置response
 | 
	
		
			
				|  |  | +            response.reset();
 | 
	
		
			
				|  |  | +            response.setContentType("application/json");
 | 
	
		
			
				|  |  | +            response.setCharacterEncoding("utf-8");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void saveUserRole(Long userId, List<Long> roleIdList) {
 | 
	
		
			
				|  |  | +        List<SysUserRole> list = new ArrayList<>();
 | 
	
		
			
				|  |  | +        for (Long roleId : roleIdList) {
 | 
	
		
			
				|  |  | +            SysUserRole userRole = new SysUserRole();
 | 
	
		
			
				|  |  | +            userRole.setRoleId(roleId);
 | 
	
		
			
				|  |  | +            userRole.setUserId(userId);
 | 
	
		
			
				|  |  | +            userRole.setCreateTime(new Date());
 | 
	
		
			
				|  |  | +            userRole.setCreateBy(SecurityUtils.getLoginUser().getUsername());
 | 
	
		
			
				|  |  | +            list.add(userRole);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (!list.isEmpty()) {
 | 
	
		
			
				|  |  | +            userRoleMapper.batchUserRole(list);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void validatorParam(UserImport user, List<UserImportErrorMsg> errorList, Map<String, Long> roleMap, Map<Long, List<Long>> userMap) {
 | 
	
		
			
				|  |  | +        String username = user.getUsername();
 | 
	
		
			
				|  |  | +        if (StringUtils.isEmpty(username)) {
 | 
	
		
			
				|  |  | +            UserImportErrorMsg errorMsg = BeanHelper.copyProperties(user, UserImportErrorMsg.class);
 | 
	
		
			
				|  |  | +            errorMsg.setErrorMsg("账户名称为空;");
 | 
	
		
			
				|  |  | +            errorList.add(errorMsg);
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        SysUser sysUser = baseMapper.selectUserByUserName(username);
 | 
	
		
			
				|  |  | +        if (sysUser == null) {
 | 
	
		
			
				|  |  | +            UserImportErrorMsg errorMsg = BeanHelper.copyProperties(user, UserImportErrorMsg.class);
 | 
	
		
			
				|  |  | +            errorMsg.setErrorMsg("用户不存在;");
 | 
	
		
			
				|  |  | +            errorList.add(errorMsg);
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (userMap.containsKey(sysUser.getId())) {
 | 
	
		
			
				|  |  | +            UserImportErrorMsg errorMsg = BeanHelper.copyProperties(user, UserImportErrorMsg.class);
 | 
	
		
			
				|  |  | +            errorMsg.setErrorMsg("账号 " + username + " 重复导入,请检查;");
 | 
	
		
			
				|  |  | +            errorList.add(errorMsg);
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        String roleNames = user.getRoleNames();
 | 
	
		
			
				|  |  | +        if (StringUtils.isEmpty(roleNames)) {
 | 
	
		
			
				|  |  | +            UserImportErrorMsg errorMsg = BeanHelper.copyProperties(user, UserImportErrorMsg.class);
 | 
	
		
			
				|  |  | +            errorMsg.setErrorMsg("角色名称为空;");
 | 
	
		
			
				|  |  | +            errorList.add(errorMsg);
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        String[] names = roleNames.split(",");
 | 
	
		
			
				|  |  | +        List<String> arrayList = Lists.newArrayList(names);
 | 
	
		
			
				|  |  | +        List<SysRole> roleList = roleMapper.selectByRoleNames(arrayList);
 | 
	
		
			
				|  |  | +        if (roleList.isEmpty()) {
 | 
	
		
			
				|  |  | +            UserImportErrorMsg errorMsg = BeanHelper.copyProperties(user, UserImportErrorMsg.class);
 | 
	
		
			
				|  |  | +            errorMsg.setErrorMsg("角色 " + roleNames + " 不存在;");
 | 
	
		
			
				|  |  | +            errorList.add(errorMsg);
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        List<String> notExitRoleNameList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        List<Long> roleIdList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        for (String roleName : arrayList) {
 | 
	
		
			
				|  |  | +            Optional<SysRole> optional = roleList.stream().filter(r -> ObjectUtil.equal(r.getRoleName(), roleName)).findAny();
 | 
	
		
			
				|  |  | +            if (optional.isPresent()) {
 | 
	
		
			
				|  |  | +                roleMap.putIfAbsent(roleName, optional.get().getId());
 | 
	
		
			
				|  |  | +                roleIdList.add(optional.get().getId());
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                notExitRoleNameList.add(roleName);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (!notExitRoleNameList.isEmpty()) {
 | 
	
		
			
				|  |  | +            UserImportErrorMsg errorMsg = BeanHelper.copyProperties(user, UserImportErrorMsg.class);
 | 
	
		
			
				|  |  | +            errorMsg.setErrorMsg("角色 " + String.join(",", notExitRoleNameList) + " 不存在;");
 | 
	
		
			
				|  |  | +            errorList.add(errorMsg);
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        userMap.put(sysUser.getId(), roleIdList);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |