Commit 2af3e36a authored by fangxinjiang's avatar fangxinjiang

权限批量导入

parent 213f97a6
package com.yifu.cloud.plus.v1.yifu.archives.vo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.ExcelAttribute;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.RowIndex;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import java.io.Serializable;
/**
* @Author fxj
* @Date 2023/10/17
* @Description
* @Version 1.0
*/
@Data
public class DataPermissionImportVo extends RowIndex implements Serializable {
@ExcelAttribute(name = "登录账号", maxLength = 30,isNotEmpty = true)
@Schema(description = "登录账号")
private String userLoginName;
@ExcelAttribute(name = "项目名称", maxLength = 32,isNotEmpty = false)
@Schema(description = "项目名称")
private String settleDomainName;
@Length(max = 32, message = "项目编码不能超过32个字符")
@ExcelAttribute(name = "项目编码", maxLength = 32,isNotEmpty = true)
@Schema(description = "项目编码")
private String settleDomainNo;
}
package com.yifu.cloud.plus.v1.yifu.archives.vo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.ExcelAttribute;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.RowIndex;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import java.io.Serializable;
/**
* @Author fxj
* @Date 2023/10/17
* @Description 社保公积金审核权限VO
* @Version 1.0
*/
@Data
public class SocialAuthHandleImportVo extends RowIndex implements Serializable {
@ExcelAttribute(name = "登录账号", maxLength = 30,isNotEmpty = true)
@Schema(description = "登录账号")
private String userLoginName;
@Length(max = 32, message = "户名称不能超过32个字符")
@ExcelAttribute(name = "户名称", maxLength = 32,isNotEmpty = true)
@Schema(description = "户名称")
private String houseHold;
}
package com.yifu.cloud.plus.v1.yifu.archives.vo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.ExcelAttribute;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.RowIndex;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import java.io.Serializable;
/**
* @Author fxj
* @Date 2023/10/17
* @Description 社保公积金审核权限VO
* @Version 1.0
*/
@Data
public class SocialAuthImportVo extends RowIndex implements Serializable {
@ExcelAttribute(name = "登录账号", maxLength = 30,isNotEmpty = true)
@Schema(description = "登录账号")
private String userLoginName;
/**
* 户类型:0社保 1公积金
*/
@Schema(description ="户类型:0社保 1公积金")
@ExcelAttribute(name = "户类型:0社保 1公积金", maxLength = 32,isNotEmpty = true,isDataId = true,readConverterExp = "0=社保,1=公积金")
private String type;
@Length(max = 32, message = "户名称不能超过32个字符")
@ExcelAttribute(name = "户名称", maxLength = 32,isNotEmpty = false)
@Schema(description = "户名称")
private String houseHold;
}
......@@ -24,9 +24,11 @@ import com.yifu.cloud.plus.v1.yifu.archives.entity.TCustomerInfo;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TSettleDomain;
import com.yifu.cloud.plus.v1.yifu.archives.vo.*;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.HashMap;
import java.util.List;
/**
......@@ -111,4 +113,6 @@ public interface TSettleDomainMapper extends BaseMapper<TSettleDomain> {
void changeIssueStatus(@Param("changeIssueStatusVo")ChangeIssueStatusVo changeIssueStatusVo);
List<TSettleDomain> getAllInfoMap();
}
......@@ -4,9 +4,11 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TCutsomerDataPermisson;
import com.yifu.cloud.plus.v1.yifu.common.core.util.ErrorMessage;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import java.io.InputStream;
import java.util.List;
/**
......@@ -43,4 +45,6 @@ public interface TCutsomerDataPermissonService extends IService<TCutsomerDataPer
* @return
**/
R<String> insertForCopyCutsomerDataPermisson(String[] srcUserList, String[] beUser, YifuUser user);
R<List<ErrorMessage>> importDiy(InputStream inputStream);
}
package com.yifu.cloud.plus.v1.yifu.archives.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUser;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.AllUserNaVo;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TCompanyInfo;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TCutsomerDataPermisson;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TSettleDomain;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TCutsomerDataPermissonMapper;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TSettleDomainMapper;
import com.yifu.cloud.plus.v1.yifu.archives.service.TCutsomerDataPermissonService;
import com.yifu.cloud.plus.v1.yifu.archives.service.TSettleDomainService;
import com.yifu.cloud.plus.v1.yifu.archives.vo.DataPermissionImportVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TCompanyInfoVo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.ErrorMessage;
import com.yifu.cloud.plus.v1.yifu.common.core.util.ExcelUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.dapr.config.DaprUpmsProperties;
import com.yifu.cloud.plus.v1.yifu.common.dapr.util.UpmsDaprUtils;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
......@@ -32,9 +55,13 @@ import java.util.*;
@Service
@RequiredArgsConstructor
@EnableConfigurationProperties(DaprUpmsProperties.class)
@AllArgsConstructor
@Slf4j
public class TCutsomerDataPermissonServiceImpl extends ServiceImpl<TCutsomerDataPermissonMapper, TCutsomerDataPermisson> implements TCutsomerDataPermissonService {
@Autowired
private UpmsDaprUtils upmsDaprUtils;
@Autowired
private TSettleDomainMapper domainMapper;
/**
* 客户数据权限表简单分页查询
*
......@@ -118,4 +145,172 @@ public class TCutsomerDataPermissonServiceImpl extends ServiceImpl<TCutsomerData
public boolean save(TCutsomerDataPermisson cutsomerDataPermisson){
return super.save(cutsomerDataPermisson);
}
/**
* @Author fxj
* @Description 批量导入项目权限
* @Date 16:59 2023/10/17
* @Param
* @return
**/
@Transactional(rollbackFor = Exception.class)
@ShardingTransactionType(TransactionType.BASE)
@Override
public R<List<ErrorMessage>> importDiy(InputStream inputStream) {
List<ErrorMessage> errorMessageList = new ArrayList<>();
ExcelUtil<DataPermissionImportVo> util1 = new ExcelUtil<>(DataPermissionImportVo.class);
List<TSettleDomain> settleDomains = domainMapper.getAllInfoMap();
Map<String, TSettleDomain> domainMap = null;
if (Common.isNotNull(settleDomains)){
domainMap = settleDomains.stream().collect(Collectors.toMap(k->k.getDepartNo(),k->k));
}
if (Common.isEmpty(domainMap)){
domainMap = new HashMap<>();
}
Map<String,SysUser> userMap = new HashMap<>();
Map<String,String> existMap = new HashMap<>();
// 写法2:
// 匿名内部类 不用额外写一个DemoDataListener
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
try {
Map<String, TSettleDomain> finalDomainMap = domainMap;
EasyExcel.read(inputStream, DataPermissionImportVo.class, new ReadListener<DataPermissionImportVo>() {
/**
* 单次缓存的数据量
*/
public static final int BATCH_COUNT = CommonConstants.BATCH_COUNT;
/**
*临时存储
*/
private List<DataPermissionImportVo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
@Override
public void invoke(DataPermissionImportVo data, AnalysisContext context) {
ReadRowHolder readRowHolder = context.readRowHolder();
Integer rowIndex = readRowHolder.getRowIndex();
data.setRowIndex(rowIndex+1);
ErrorMessage errorMessage = util1.checkEntity(data, data.getRowIndex());
if (Common.isNotNull(errorMessage)){
errorMessageList.add(errorMessage);
}else {
cachedDataList.add(data);
}
if (cachedDataList.size() >= BATCH_COUNT) {
saveData();
// 存储完成清理 list
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
saveData();
}
/**
* 加上存储数据库
*/
private void saveData() {
log.info("{}条数据,开始存储数据库!", cachedDataList.size());
importEntity(cachedDataList,errorMessageList, finalDomainMap,userMap,existMap);
log.info("存储数据库成功!");
}
}).sheet().doRead();
}catch (Exception e){
log.error(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR,e);
return R.failed(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR);
}finally {
// 清理
domainMap.clear();
}
return judgeAllMessage(errorMessageList);
}
/**
* @Author fxj
* @Description 判断所有返回数据,是否全部为正确的
* @Date 18:01 2023/10/18
* @Param
* @return
**/
private R<List<ErrorMessage>> judgeAllMessage(List<ErrorMessage> errorMessageList) {
if (!errorMessageList.isEmpty() && errorMessageList.stream().noneMatch(e-> e.getColor().equals(CommonConstants.RED))) {
return R.ok();
}
return R.ok(errorMessageList);
}
private void importEntity(List<DataPermissionImportVo> excelVOList,
List<ErrorMessage> errorMessageList,
Map<String,TSettleDomain> domainMap,
Map<String,SysUser> userMap,
Map<String,String> existMap) {
// 执行数据插入操作 组装
TCutsomerDataPermisson permission = null;
SysUser user;
R<SysUser> res;
TSettleDomain domain;
TCutsomerDataPermisson exist;
String temp;
DataPermissionImportVo excel;
for (int i = 0; i < excelVOList.size(); i++) {
excel = excelVOList.get(i);
// 数据合法情况
user = userMap.get(excel.getUserLoginName());
// 用户信息验证
if (Common.isEmpty(user)){
res = upmsDaprUtils.getSimpleUserByLoginName(excel.getUserLoginName());
if (Common.isNotNull(res) && Common.isNotNull(res.getData())){
user = res.getData();
userMap.put(user.getUsername(),user);
}else {
errorMessageList.add(new ErrorMessage(excel.getRowIndex(),CommonConstants.USER_FAIL));
continue;
}
}
if (Common.isEmpty(user.getDeptName())){
errorMessageList.add(new ErrorMessage(excel.getRowIndex(),"对应登录账号无部门信息,请完善后导入"));
continue;
}
// 项目信息验证
domain = domainMap.get(excel.getSettleDomainNo());
if (Common.isEmpty(domain)){
errorMessageList.add(new ErrorMessage(excel.getRowIndex(),CommonConstants.SETTLE_DOMAIN_NOT_EXIST));
continue;
}
// 重复校验
temp = existMap.get(excel.getUserLoginName()+CommonConstants.DOWN_LINE_STRING+excel.getSettleDomainNo());
if (Common.isNotNull(temp)){
errorMessageList.add(new ErrorMessage(excel.getRowIndex(),"对应账号的权限已存在,请勿重复导入"));
continue;
}else {
exist = baseMapper.selectOne(Wrappers.<TCutsomerDataPermisson>query().lambda()
.eq(TCutsomerDataPermisson::getSettleDomainNo,excel.getSettleDomainNo())
.eq(TCutsomerDataPermisson::getUserLoginName,excel.getUserLoginName())
.last(CommonConstants.LAST_ONE_SQL));
if (Common.isNotNull(exist)){
errorMessageList.add(new ErrorMessage(excel.getRowIndex(),"对应账号的权限已存在,请勿重复导入"));
continue;
}
}
// 初始化及新增数据
permission = new TCutsomerDataPermisson();
permission.setType(CommonConstants.ONE_STRING);
permission.setSettleDomainId(domain.getId());
permission.setRemark("批量授权");
permission.setSettleDomainName(domain.getDepartName());
permission.setSettleDomainNo(domain.getDepartNo());
permission.setStatus(CommonConstants.ZERO_STRING);
permission.setUserId(user.getUserId());
permission.setUserDeptName(user.getDeptName());
permission.setUserLoginName(user.getUsername());
permission.setUserNickName(user.getNickname());
insertExcel(permission);
// 加入map 验证当前表单数据是否重复
existMap.put(excel.getUserLoginName()+CommonConstants.DOWN_LINE_STRING+excel.getSettleDomainNo(),excel.getSettleDomainNo());
errorMessageList.add(new ErrorMessage(excel.getRowIndex(),CommonConstants.SAVE_SUCCESS,CommonConstants.GREEN));
}
}
/**
* 插入excel bad record
*/
private void insertExcel(TCutsomerDataPermisson entity) {
this.save(entity);
}
}
......@@ -510,4 +510,11 @@
set a.ISSUE_STATUS=#{changeIssueStatusVo.issueStatus}
WHERE a.ID=#{changeIssueStatusVo.id}
</update>
<select id="getAllInfoMap" resultMap="tSettleDomainMap">
SELECT
<include refid="Base_Column_List"/>
FROM
t_settle_domain a
where a.DELETE_FLAG = '0'
</select>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment