mirror of https://gitee.com/xiaonuobase/snowy
【更新】完善导入功能
parent
20c238f9cd
commit
2056089ed9
|
@ -93,6 +93,14 @@ public interface SysOrgService extends IService<SysOrg> {
|
||||||
**/
|
**/
|
||||||
List<SysOrg> getCachedAllOrgList();
|
List<SysOrg> getCachedAllOrgList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据机构全名称获取机构id,有则返回,无则创建
|
||||||
|
*
|
||||||
|
* @author xuyuxiang
|
||||||
|
* @date 2023/3/7 15:44
|
||||||
|
**/
|
||||||
|
String getOrgIdByOrgFullNameWithCreate(String orgFullName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id获取父子数据列表
|
* 根据id获取父子数据列表
|
||||||
*
|
*
|
||||||
|
@ -123,7 +131,7 @@ public interface SysOrgService extends IService<SysOrg> {
|
||||||
* @author xuyuxiang
|
* @author xuyuxiang
|
||||||
* @date 2022/8/15 14:55
|
* @date 2022/8/15 14:55
|
||||||
**/
|
**/
|
||||||
SysOrg getById(List<SysOrg> originDataList, String id) ;
|
SysOrg getById(List<SysOrg> originDataList, String id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id获取父数据
|
* 根据id获取父数据
|
||||||
|
@ -131,7 +139,7 @@ public interface SysOrgService extends IService<SysOrg> {
|
||||||
* @author xuyuxiang
|
* @author xuyuxiang
|
||||||
* @date 2022/8/15 14:55
|
* @date 2022/8/15 14:55
|
||||||
**/
|
**/
|
||||||
SysOrg getParentById(List<SysOrg> originDataList, String id) ;
|
SysOrg getParentById(List<SysOrg> originDataList, String id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id获取子数据
|
* 根据id获取子数据
|
||||||
|
@ -139,7 +147,7 @@ public interface SysOrgService extends IService<SysOrg> {
|
||||||
* @author xuyuxiang
|
* @author xuyuxiang
|
||||||
* @date 2022/8/15 14:55
|
* @date 2022/8/15 14:55
|
||||||
**/
|
**/
|
||||||
SysOrg getChildById(List<SysOrg> originDataList, String id) ;
|
SysOrg getChildById(List<SysOrg> originDataList, String id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取组织树选择器
|
* 获取组织树选择器
|
||||||
|
|
|
@ -48,6 +48,7 @@ import vip.xiaonuo.sys.modular.user.entity.SysUser;
|
||||||
import vip.xiaonuo.sys.modular.user.service.SysUserService;
|
import vip.xiaonuo.sys.modular.user.service.SysUserService;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -222,6 +223,50 @@ public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> impleme
|
||||||
return orgList;
|
return orgList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOrgIdByOrgFullNameWithCreate(String orgFullName) {
|
||||||
|
List<SysOrg> cachedAllOrgList = this.getCachedAllOrgList();
|
||||||
|
List<Tree<String>> treeList = TreeUtil.build(cachedAllOrgList.stream().map(sysOrg ->
|
||||||
|
new TreeNode<>(sysOrg.getId(), sysOrg.getParentId(), sysOrg.getName(), sysOrg.getSortCode()))
|
||||||
|
.collect(Collectors.toList()), "0");
|
||||||
|
return findOrgIdByOrgName("0", StrUtil.split(orgFullName, StrUtil.DASHED).iterator(), cachedAllOrgList, treeList);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String findOrgIdByOrgName(String parentId, Iterator<String> iterator, List<SysOrg> cachedAllOrgList, List<Tree<String>> treeList) {
|
||||||
|
String next = iterator.next();
|
||||||
|
List<Tree<String>> findList = treeList.stream().filter(tree -> tree.getName().equals(next)).collect(Collectors.toList());
|
||||||
|
if(ObjectUtil.isNotEmpty(findList)) {
|
||||||
|
if(iterator.hasNext()) {
|
||||||
|
return findOrgIdByOrgName(findList.get(0).getId(), iterator, cachedAllOrgList, findList.get(0).getChildren());
|
||||||
|
} else {
|
||||||
|
return findList.get(0).getId();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//创建该机构
|
||||||
|
SysOrg sysOrg = new SysOrg();
|
||||||
|
sysOrg.setName(next);
|
||||||
|
sysOrg.setCode(RandomUtil.randomString(10));
|
||||||
|
sysOrg.setParentId(parentId);
|
||||||
|
sysOrg.setCategory(parentId.equals("0")?SysOrgCategoryEnum.COMPANY.getValue():SysOrgCategoryEnum.DEPT.getValue());
|
||||||
|
sysOrg.setSortCode(99);
|
||||||
|
this.save(sysOrg);
|
||||||
|
// 发布增加事件
|
||||||
|
CommonDataChangeEventCenter.doAddWithData(SysDataTypeEnum.ORG.getValue(), JSONUtil.createArray().put(sysOrg));
|
||||||
|
// 将该机构加入缓存
|
||||||
|
cachedAllOrgList.add(sysOrg);
|
||||||
|
// 更新缓存
|
||||||
|
commonCacheOperator.put(ORG_CACHE_ALL_KEY, cachedAllOrgList);
|
||||||
|
// 继续查找
|
||||||
|
if(iterator.hasNext()) {
|
||||||
|
return findOrgIdByOrgName(sysOrg.getId(), iterator, cachedAllOrgList, CollectionUtil.newArrayList());
|
||||||
|
} else {
|
||||||
|
return sysOrg.getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* ====组织部分所需要用到的选择器==== */
|
/* ====组织部分所需要用到的选择器==== */
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -82,7 +82,15 @@ public interface SysPositionService extends IService<SysPosition> {
|
||||||
* @author xuyuxiang
|
* @author xuyuxiang
|
||||||
* @date 2022/8/15 14:55
|
* @date 2022/8/15 14:55
|
||||||
**/
|
**/
|
||||||
SysPosition getById(List<SysPosition> originDataList, String id) ;
|
SysPosition getById(List<SysPosition> originDataList, String id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据机构id和职位名称获取职位id,有则返回,无则创建
|
||||||
|
*
|
||||||
|
* @author xuyuxiang
|
||||||
|
* @date 2022/8/15 14:55
|
||||||
|
**/
|
||||||
|
String getPositionIdByPositionNameWithCreate(String orgId, String positionName);
|
||||||
|
|
||||||
/* ====职位部分所需要用到的选择器==== */
|
/* ====职位部分所需要用到的选择器==== */
|
||||||
|
|
||||||
|
|
|
@ -171,7 +171,26 @@ public class SysPositionServiceImpl extends ServiceImpl<SysPositionMapper, SysPo
|
||||||
int index = CollStreamUtil.toList(originDataList, SysPosition::getId).indexOf(id);
|
int index = CollStreamUtil.toList(originDataList, SysPosition::getId).indexOf(id);
|
||||||
return index == -1?null:originDataList.get(index);
|
return index == -1?null:originDataList.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPositionIdByPositionNameWithCreate(String orgId, String positionName) {
|
||||||
|
SysPosition sysPosition = this.getOne(new LambdaQueryWrapper<SysPosition>().eq(SysPosition::getOrgId, orgId).eq(SysPosition::getName, positionName));
|
||||||
|
if(ObjectUtil.isNotEmpty(sysPosition)) {
|
||||||
|
return sysPosition.getId();
|
||||||
|
} else {
|
||||||
|
sysPosition = new SysPosition();
|
||||||
|
sysPosition.setOrgId(orgId);
|
||||||
|
sysPosition.setName(positionName);
|
||||||
|
sysPosition.setCode(RandomUtil.randomString(10));
|
||||||
|
sysPosition.setCategory(SysPositionCategoryEnum.LOW.getValue());
|
||||||
|
sysPosition.setSortCode(99);
|
||||||
|
this.save(sysPosition);
|
||||||
|
// 发布增加事件
|
||||||
|
CommonDataChangeEventCenter.doAddWithData(SysDataTypeEnum.POSITION.getValue(), JSONUtil.createArray().put(sysPosition));
|
||||||
|
return sysPosition.getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* ====职位部分所需要用到的选择器==== */
|
/* ====职位部分所需要用到的选择器==== */
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
package vip.xiaonuo.sys.modular.user.controller;
|
package vip.xiaonuo.sys.modular.user.controller;
|
||||||
|
|
||||||
import cn.hutool.core.lang.tree.Tree;
|
import cn.hutool.core.lang.tree.Tree;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||||
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
|
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
|
||||||
|
@ -283,9 +284,8 @@ public class SysUserController {
|
||||||
@ApiOperation("用户导入")
|
@ApiOperation("用户导入")
|
||||||
@CommonLog("用户导入")
|
@CommonLog("用户导入")
|
||||||
@PostMapping("/sys/user/import")
|
@PostMapping("/sys/user/import")
|
||||||
public CommonResult<String> importUser(@RequestPart("file") @ApiParam(value="文件", required = true) MultipartFile file) {
|
public CommonResult<JSONObject> importUser(@RequestPart("file") @ApiParam(value="文件", required = true) MultipartFile file) {
|
||||||
sysUserService.importUser(file);
|
return CommonResult.data(sysUserService.importUser(file));
|
||||||
return CommonResult.ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -354,7 +354,7 @@ public interface SysUserService extends IService<SysUser> {
|
||||||
* @author xuyuxiang
|
* @author xuyuxiang
|
||||||
* @date 2022/8/8 13:16
|
* @date 2022/8/8 13:16
|
||||||
**/
|
**/
|
||||||
void importUser(MultipartFile file);
|
JSONObject importUser(MultipartFile file);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户导出
|
* 用户导出
|
||||||
|
|
|
@ -42,7 +42,6 @@ import cn.hutool.json.JSONObject;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.EasyExcel;
|
||||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||||
import com.alibaba.excel.read.listener.PageReadListener;
|
|
||||||
import com.alibaba.excel.write.handler.CellWriteHandler;
|
import com.alibaba.excel.write.handler.CellWriteHandler;
|
||||||
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
|
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
|
||||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
||||||
|
@ -980,21 +979,120 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
public void importUser(MultipartFile file) {
|
public JSONObject importUser(MultipartFile file) {
|
||||||
try {
|
try {
|
||||||
// TODO 导入
|
int successCount = 0;
|
||||||
EasyExcel.read("D://import.xlsx", SysUserImportParam.class, new PageReadListener<SysUserImportParam>(dataList -> {
|
int errorCount = 0;
|
||||||
for (SysUserImportParam sysUserImportParam : dataList) {
|
JSONArray errorDetail = JSONUtil.createArray();
|
||||||
System.out.println(sysUserImportParam);
|
List<SysUserImportParam> sysUserImportParamList = EasyExcel.read("D://import.xlsx")
|
||||||
|
.head(SysUserImportParam.class).sheet().headRowNumber(2).doReadSync();
|
||||||
|
for (int i = 0; i < sysUserImportParamList.size(); i++) {
|
||||||
|
JSONObject jsonObject = this.doImport(sysUserImportParamList.get(i), i);
|
||||||
|
if(jsonObject.getBool("success")) {
|
||||||
|
successCount += 1;
|
||||||
|
} else{
|
||||||
|
errorCount += 1;
|
||||||
|
errorDetail.add(jsonObject);
|
||||||
}
|
}
|
||||||
})).sheet().headRowNumber(2).doRead();
|
}
|
||||||
|
return JSONUtil.createObj()
|
||||||
|
.set("totalCount", sysUserImportParamList.size())
|
||||||
|
.set("successCount", successCount)
|
||||||
|
.set("errorCount", errorCount)
|
||||||
|
.set("errorDetail", errorDetail);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new CommonException("文件导入失败");
|
throw new CommonException("文件导入失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行导入
|
||||||
|
*
|
||||||
|
* @author xuyuxiang
|
||||||
|
* @date 2023/3/7 13:22
|
||||||
|
**/
|
||||||
|
public JSONObject doImport(SysUserImportParam sysUserImportParam, int i) {
|
||||||
|
String account = sysUserImportParam.getAccount();
|
||||||
|
String name = sysUserImportParam.getName();
|
||||||
|
String orgName = sysUserImportParam.getOrgName();
|
||||||
|
String positionName = sysUserImportParam.getPositionName();
|
||||||
|
if(ObjectUtil.hasEmpty(account, name, orgName, positionName)) {
|
||||||
|
return JSONUtil.createObj().set("index", i + 1).set("success", false).set("msg", "必填字段存在空值");
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
List<SysUser> cachedAllUserList = this.getCachedAllUserList();
|
||||||
|
String orgId = sysOrgService.getOrgIdByOrgFullNameWithCreate(sysUserImportParam.getOrgName());
|
||||||
|
String positionId = sysPositionService.getPositionIdByPositionNameWithCreate(orgId, sysUserImportParam.getPositionName());
|
||||||
|
SysUser sysUser = this.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getAccount, account));
|
||||||
|
boolean isAdd = false;
|
||||||
|
String existUserId = null;
|
||||||
|
if(ObjectUtil.isEmpty(sysUser)) {
|
||||||
|
sysUser = new SysUser();
|
||||||
|
isAdd = true;
|
||||||
|
} else {
|
||||||
|
existUserId = sysUser.getId();
|
||||||
|
}
|
||||||
|
String phone = sysUser.getPhone();
|
||||||
|
String email = sysUser.getEmail();
|
||||||
|
// 拷贝属性
|
||||||
|
BeanUtil.copyProperties(sysUserImportParam, sysUser);
|
||||||
|
sysUser.setOrgId(orgId);
|
||||||
|
sysUser.setPositionId(positionId);
|
||||||
|
// 判断手机号是否跟系统现有的重复
|
||||||
|
if(ObjectUtil.isNotEmpty(phone)) {
|
||||||
|
if(isAdd) {
|
||||||
|
boolean repeatPhone = cachedAllUserList.stream().anyMatch(tempSysUser -> ObjectUtil
|
||||||
|
.isNotEmpty(tempSysUser.getPhone()) && tempSysUser.getPhone().equals(phone));
|
||||||
|
if(repeatPhone) {
|
||||||
|
sysUser.setPhone(null);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
String finalExistUserId = existUserId;
|
||||||
|
boolean repeatPhone = cachedAllUserList.stream().anyMatch(tempSysUser -> ObjectUtil
|
||||||
|
.isNotEmpty(tempSysUser.getPhone()) && tempSysUser.getPhone()
|
||||||
|
.equals(phone) && !tempSysUser.getId().equals(finalExistUserId));
|
||||||
|
if(repeatPhone) {
|
||||||
|
sysUser.setPhone(phone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 判断邮箱是否跟系统现有的重复
|
||||||
|
if(ObjectUtil.isNotEmpty(email)) {
|
||||||
|
if(isAdd) {
|
||||||
|
boolean repeatPhone = cachedAllUserList.stream().anyMatch(tempSysUser -> ObjectUtil
|
||||||
|
.isNotEmpty(tempSysUser.getEmail()) && tempSysUser.getEmail().equals(email));
|
||||||
|
if(repeatPhone) {
|
||||||
|
sysUser.setPhone(null);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
String finalExistUserId = existUserId;
|
||||||
|
boolean repeatPhone = cachedAllUserList.stream().anyMatch(tempSysUser -> ObjectUtil
|
||||||
|
.isNotEmpty(tempSysUser.getEmail()) && tempSysUser.getEmail()
|
||||||
|
.equals(email) && !tempSysUser.getId().equals(finalExistUserId));
|
||||||
|
if(repeatPhone) {
|
||||||
|
sysUser.setPhone(email);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.saveOrUpdate(sysUser);
|
||||||
|
// 发布增加事件
|
||||||
|
CommonDataChangeEventCenter.doAddWithData(SysDataTypeEnum.ORG.getValue(), JSONUtil.createArray().put(sysUser));
|
||||||
|
// 将该用户加入缓存
|
||||||
|
cachedAllUserList.add(sysUser);
|
||||||
|
// 更新缓存
|
||||||
|
commonCacheOperator.put(USER_CACHE_ALL_KEY, cachedAllUserList);
|
||||||
|
// 返回成功
|
||||||
|
return JSONUtil.createObj().set("success", true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return JSONUtil.createObj().set("index", i + 1).set("success", false).set("msg", "数据导入异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exportUser(SysUserExportParam sysUserExportParam, HttpServletResponse response) throws IOException {
|
public void exportUser(SysUserExportParam sysUserExportParam, HttpServletResponse response) throws IOException {
|
||||||
File tempFile = null;
|
File tempFile = null;
|
||||||
|
@ -1143,7 +1241,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||||
if(ObjectUtil.isNotEmpty(sysUser.getAvatar())) {
|
if(ObjectUtil.isNotEmpty(sysUser.getAvatar())) {
|
||||||
avatarBase64 = sysUser.getAvatar();
|
avatarBase64 = sysUser.getAvatar();
|
||||||
} else {
|
} else {
|
||||||
avatarBase64 = CommonAvatarUtil.generateImg(sysUser.getAvatar());
|
avatarBase64 = CommonAvatarUtil.generateImg(sysUser.getName());
|
||||||
}
|
}
|
||||||
// 头像
|
// 头像
|
||||||
ImageEntity imageEntity = new ImageEntity(ImgUtil.toBytes(ImgUtil.toImage(StrUtil
|
ImageEntity imageEntity = new ImageEntity(ImgUtil.toBytes(ImgUtil.toImage(StrUtil
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue