update:完善社团管理导入接口

This commit is contained in:
sunzheng
2024-09-11 15:26:12 +08:00
parent 15adf50290
commit 787534b5c7
7 changed files with 226 additions and 90 deletions
@@ -44,8 +44,8 @@ public class LawsClubPayment implements Serializable {
private String paymentAmount;
@ApiModelProperty(value = "缴费时间")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date paymentTime;
@ApiModelProperty(value = "备注")
@@ -16,10 +16,11 @@ import com.jero.modules.laws.club.mapper.LawsClubMapper;
import com.jero.modules.laws.club.service.*;
import com.jero.modules.laws.club.vo.LawsClubMeetingExcel;
import com.jero.modules.laws.club.vo.LawsClubTreeNodeModel;
import com.jero.modules.laws.club.vo.excel.LawsClubExcel;
import com.jero.modules.laws.club.vo.excel.LawsClubJoinTeamExcel;
import com.jero.modules.laws.club.vo.excel.LawsClubMeetingImportExcel;
import com.jero.modules.laws.club.vo.excel.LawsClubResearchInformationExcel;
import com.jero.modules.laws.club.vo.excel.*;
import com.jero.modules.system.entity.SysDepart;
import com.jero.modules.system.entity.SysUser;
import com.jero.modules.system.service.ISysDepartService;
import com.jero.modules.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
@@ -36,6 +37,8 @@ import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -66,6 +69,10 @@ public class LawsClubServiceImpl extends ServiceImpl<LawsClubMapper, LawsClub> i
private ILawsClubResearchInformationService lawsClubResearchInformationService;
@Resource
private RedisUtil redisUtil;
@Resource
private ISysUserService sysUserService;
@Resource
private ISysDepartService sysDepartService;
@Override
public List<LawsClubTreeNodeModel> getTreeStructure(String parentId, String nodeType) {
@@ -294,9 +301,13 @@ public class LawsClubServiceImpl extends ServiceImpl<LawsClubMapper, LawsClub> i
lawsClubPaymentService.saveBatch(lawsClubPayments);
}
if (!CollectionUtils.isEmpty(lawsClubMeetings)) {
// 为每个 LawsClubMeeting 设置 clubId
for (LawsClubMeeting lawsClubMeeting : lawsClubMeetings) {
lawsClubMeeting.setClubId(lawsClub.getId());
lawsClubMeetingService.saveBatch(lawsClubMeetings);
}
// 批量保存所有 LawsClubMeeting 对象
lawsClubMeetingService.saveBatch(lawsClubMeetings);
for (LawsClubMeeting lawsClubMeeting : lawsClubMeetings) {
// 会议成员
List<LawsClubMeetingMember> meetingMemberList = lawsClubMeeting.getMeetingMemberList();
if (!CollectionUtils.isEmpty(meetingMemberList)) {
@@ -415,7 +426,7 @@ public class LawsClubServiceImpl extends ServiceImpl<LawsClubMapper, LawsClub> i
for (LawsClub club : excelList) {
// 如果是根节点那么上级节点为空
if ("Part".equals(club.getParentId())) {
club.setParentId("");
club.setParentId("-");
}
// 解密联系方式
if (StringUtils.isNotBlank(club.getContactNumber())) {
@@ -550,68 +561,11 @@ public class LawsClubServiceImpl extends ServiceImpl<LawsClubMapper, LawsClub> i
if (CollectionUtils.isEmpty(excelList)) {
throw new JeroBootException("没有数据可导入");
}
StringBuilder msg = new StringBuilder();
for (LawsClubExcel lawsClubExcel : excelList) {
String parentId = lawsClubExcel.getParentId();
String nodeType = lawsClubExcel.getNodeType();
String clubName = lawsClubExcel.getClubName();
List<LawsClubMeetingImportExcel> lawsClubMeetings = lawsClubExcel.getLawsClubMeetings();
List<LawsClubJoinTeamExcel> lawsClubJoinTeamList = lawsClubExcel.getLawsClubJoinTeamList();
List<LawsClubResearchInformationExcel> lawsClubResearchInformations = lawsClubExcel.getLawsClubResearchInformations();
LawsClub club = this.getOne(new LambdaQueryWrapper<LawsClub>().eq(LawsClub::getNodeType, parentId));
StringBuilder msgSub = new StringBuilder();
if (StringUtils.isBlank(parentId)) {
// 如果上级节点为空,添加错误消息
msgSub.append("").append(excelList.indexOf(lawsClubExcel) + 4).append("行上级节点不能为空;");
} else {
// 如果上级节点不为空,检查是否存在对应的节点
if (Objects.isNull(club)) {
msgSub.append("").append(excelList.indexOf(lawsClubExcel) + 4).append("行上级节点不存在,请重新输入;");
}else {
lawsClubExcel.setParentId(club.getId());
}
}
if (StringUtils.isBlank(nodeType)) {
msgSub.append("").append(excelList.indexOf(lawsClubExcel) + 4).append("行节点类型不能为空;");
}
// 检查是否存在其他信息
boolean hasAdditionalInfo = !CollectionUtils.isEmpty(lawsClubMeetings) ||
!CollectionUtils.isEmpty(lawsClubJoinTeamList) ||
!CollectionUtils.isEmpty(lawsClubResearchInformations);
// 如果存在其他信息并且社团名称为空,则添加错误消息
if (hasAdditionalInfo && StringUtils.isBlank(clubName)) {
msgSub.append("").append(excelList.indexOf(lawsClubExcel) + 4).append("行社团名称不能为空;");
}
if (StringUtils.isNotBlank(lawsClubExcel.getContactNumber())) {
String contactNumber = lawsClubExcel.getContactNumber();
if (!contactNumber.matches("^(1[3-9]\\d{9})$|^(0\\d{2,3}-?\\d{7,8})$")) {
msgSub.append("").append(excelList.indexOf(lawsClubExcel) + 4).append("行联系方式格式不正确;");
}else {
// 联系方式如果不为空,且格式正确,进行加密
String encrypt = PasswordUtil.encrypt(contactNumber);
lawsClubExcel.setContactNumber(encrypt);
}
}
if(!StringUtils.isEmpty(msgSub.toString())){
msg.append(msgSub).append("</br>");
}
if(!StringUtils.isEmpty(msg.toString())){
return Result.error(msg.toString());
}
LawsClub lawsClub = new LawsClub();
BeanUtils.copyProperties(lawsClubExcel, lawsClub);
// 转换 LawsClubJoinTeamExcel -> LawsClubJoinTeam
List<LawsClubJoinTeam> lawsClubJoinTeamsList = Optional.ofNullable(lawsClubExcel.getLawsClubJoinTeamList())
.orElse(Collections.emptyList()).stream().map(this::convertToJoinTeam).collect(Collectors.toList());
// 转换 LawsClubMeetingImportExcel -> LawsClubMeetingImport
List<LawsClubMeeting> lawsClubMeetingsList = Optional.ofNullable(lawsClubExcel.getLawsClubMeetings())
.orElse(Collections.emptyList()).stream().map(this::convertToMeetingImport).collect(Collectors.toList());
// 转换 LawsClubResearchInformationExcel -> LawsClubResearchInformation
List<LawsClubResearchInformation> lawsClubResearchInformationsList = Optional.ofNullable(lawsClubExcel.getLawsClubResearchInformations())
.orElse(Collections.emptyList()).stream().map(this::convertToResearchInformation).collect(Collectors.toList());
save(lawsClub);
saveAssociationTable(lawsClub,lawsClubJoinTeamsList,null,lawsClubMeetingsList,lawsClubResearchInformationsList,null);
StringBuilder msg = validateExcelData(excelList); // 校验数据
if (StringUtils.isNotEmpty(msg.toString())) {
return Result.error(msg.toString());
}
saveExcelData(excelList); // 保存数据
}catch (Exception e) {
log.error(e.getMessage(),e);
return Result.error("文件导入失败:"+e.getMessage());
@@ -626,22 +580,192 @@ public class LawsClubServiceImpl extends ServiceImpl<LawsClubMapper, LawsClub> i
return Result.OK(ResultCommon.OK);
}
// 校验Excel数据
private StringBuilder validateExcelData(List<LawsClubExcel> excelList) {
StringBuilder msg = new StringBuilder();
for (LawsClubExcel lawsClubExcel : excelList) {
StringBuilder msgSub = new StringBuilder();
// 校验上级节点
validateParentNode(lawsClubExcel, msgSub, excelList);
// 校验基本信息
validateBasicInfo(lawsClubExcel, msgSub, excelList);
// 校验入团信息
validateTeamInfo(lawsClubExcel.getLawsClubJoinTeamList(), msgSub, lawsClubExcel, excelList);
// 校验参会信息
validateMeetingInfo(lawsClubExcel.getLawsClubMeetings(), msgSub, lawsClubExcel, excelList);
// 校验调研征集信息
validateResearchInfo(lawsClubExcel.getLawsClubResearchInformations(), msgSub, lawsClubExcel, excelList);
// 校验支付信息
validatePaymentInfo(lawsClubExcel.getLawsClubPayment(), msgSub, lawsClubExcel, excelList);
if (msgSub.length() > 0) {
msg.append(msgSub).append("</br>");
}
}
return msg;
}
private void validateParentNode(LawsClubExcel lawsClubExcel, StringBuilder msgSub, List<LawsClubExcel> excelList) {
String parentId = lawsClubExcel.getParentId();
if (StringUtils.isBlank(parentId)) {
msgSub.append(getErrorMsg(excelList, lawsClubExcel, "上级节点不能为空;"));
} else {
LawsClub club = getOne(new LambdaQueryWrapper<LawsClub>().eq(LawsClub::getNodeType, parentId));
if (club == null) {
msgSub.append(getErrorMsg(excelList, lawsClubExcel, "上级节点不存在,请重新输入;"));
} else {
lawsClubExcel.setParentId(club.getId());
}
}
}
private void validateBasicInfo(LawsClubExcel lawsClubExcel, StringBuilder msgSub, List<LawsClubExcel> excelList) {
if (StringUtils.isBlank(lawsClubExcel.getNodeType())) {
msgSub.append(getErrorMsg(excelList, lawsClubExcel, "节点类型不能为空;"));
}
if (hasAdditionalInfo(lawsClubExcel) && StringUtils.isBlank(lawsClubExcel.getClubName())) {
msgSub.append(getErrorMsg(excelList, lawsClubExcel, "社团名称不能为空;"));
}
String contactNumber = lawsClubExcel.getContactNumber();
if (StringUtils.isNotBlank(contactNumber) && !contactNumber.matches("^(?:(0\\d{2,3}-)?\\d{7,8}|1[3-9]\\d{9})$")) {
msgSub.append(getErrorMsg(excelList, lawsClubExcel, "联系方式格式不正确;"));
} else {
lawsClubExcel.setContactNumber(PasswordUtil.encrypt(contactNumber));
}
}
private void validateTeamInfo(List<LawsClubJoinTeamExcel> teamList, StringBuilder msgSub, LawsClubExcel lawsClubExcel, List<LawsClubExcel> excelList) {
if (CollectionUtils.isEmpty(teamList)) return;
for (LawsClubJoinTeamExcel team : teamList) {
String proposerId = team.getProposerId();
String departId = team.getDepartId();
if (StringUtils.isNotBlank(proposerId)) {
List<SysUser> sysUser = sysUserService.list(new LambdaQueryWrapper<SysUser>().eq(SysUser::getRealname, proposerId));
if (CollectionUtils.isEmpty(sysUser)) {
msgSub.append(getErrorMsg(excelList, lawsClubExcel, "入团信息的申请人不存在;"));
}else {
team.setProposerId(sysUser.get(0).getId());
}
}
if (StringUtils.isNotBlank(departId)) {
List<SysDepart> sysDepart = sysDepartService.list(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getDepartName, departId));
if (CollectionUtils.isEmpty(sysDepart)) {
msgSub.append(getErrorMsg(excelList, lawsClubExcel, "入团信息的部门名称不存在;"));
}else {
team.setDepartId(sysDepart.get(0).getId());
}
}
validateDateFormat(team.getJoinDate(), "入团信息的入团日期格式不正确;", msgSub, excelList, lawsClubExcel);
}
}
private void validateMeetingInfo(List<LawsClubMeetingImportExcel> meetings, StringBuilder msgSub, LawsClubExcel lawsClubExcel, List<LawsClubExcel> excelList) {
if (CollectionUtils.isEmpty(meetings)) return;
for (LawsClubMeetingImportExcel meeting : meetings) {
validateDateFormat(meeting.getMeetingDate(), "参会信息的会议日期格式不正确;", msgSub, excelList, lawsClubExcel);
}
}
private void validateResearchInfo(List<LawsClubResearchInformationExcel> researchList, StringBuilder msgSub, LawsClubExcel lawsClubExcel, List<LawsClubExcel> excelList) {
if (CollectionUtils.isEmpty(researchList)) return;
for (LawsClubResearchInformationExcel research : researchList) {
validateDateFormat(research.getReceiveDate(), "征集调研信息的接收日期格式不正确;", msgSub, excelList, lawsClubExcel);
validateDateFormat(research.getDeadlineDate(), "征集调研信息的截止日期格式不正确;", msgSub, excelList, lawsClubExcel);
}
}
private void validatePaymentInfo(List<LawsClubPaymentExcel> payments, StringBuilder msgSub, LawsClubExcel lawsClubExcel, List<LawsClubExcel> excelList) {
if (CollectionUtils.isEmpty(payments)) return;
for (LawsClubPaymentExcel payment : payments) {
validateDateFormat(payment.getPaymentTime(), "支付信息的缴费时间格式不正确;", msgSub, excelList, lawsClubExcel);
}
}
private void validateDateFormat(String dateStr, String errorMsg, StringBuilder msgSub, List<LawsClubExcel> excelList, LawsClubExcel lawsClubExcel) {
if (StringUtils.isNotBlank(dateStr)) {
try {
LocalDate.parse(dateStr);
} catch (Exception e) {
msgSub.append(getErrorMsg(excelList, lawsClubExcel, errorMsg));
}
}
}
// 保存Excel数据
private void saveExcelData(List<LawsClubExcel> excelList) {
for (LawsClubExcel lawsClubExcel : excelList) {
LawsClub lawsClub = new LawsClub();
BeanUtils.copyProperties(lawsClubExcel, lawsClub);
// 转换和保存数据
List<LawsClubJoinTeam> joinTeamList = convertList(lawsClubExcel.getLawsClubJoinTeamList(), this::convertToJoinTeam);
List<LawsClubPayment> paymentList = convertList(lawsClubExcel.getLawsClubPayment(), this::convertToPayment);
List<LawsClubMeeting> meetingList = convertList(lawsClubExcel.getLawsClubMeetings(), this::convertToMeeting);
List<LawsClubResearchInformation> researchInfoList = convertList(lawsClubExcel.getLawsClubResearchInformations(), this::convertToResearchInformation);
save(lawsClub);
saveAssociationTable(lawsClub, joinTeamList, paymentList, meetingList, researchInfoList, null);
}
}
// 错误消息格式化
private String getErrorMsg(List<LawsClubExcel> excelList, LawsClubExcel lawsClubExcel, String errorMsg) {
return "" + (excelList.indexOf(lawsClubExcel) + 4) + "" + errorMsg;
}
// 判断是否有额外信息
private boolean hasAdditionalInfo(LawsClubExcel lawsClubExcel) {
return !CollectionUtils.isEmpty(lawsClubExcel.getLawsClubMeetings()) ||
!CollectionUtils.isEmpty(lawsClubExcel.getLawsClubJoinTeamList()) ||
!CollectionUtils.isEmpty(lawsClubExcel.getLawsClubResearchInformations());
}
// 通用的转换方法
private <T, R> List<R> convertList(List<T> sourceList, Function<T, R> converter) {
return Optional.ofNullable(sourceList).orElse(Collections.emptyList()).stream()
.map(converter)
.collect(Collectors.toList());
}
// 通用的日期转换方法
private Date parseDate(String dateStr, String errorMessage) {
if (StringUtils.isNotBlank(dateStr)) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
} catch (Exception e) {
log.error(errorMessage);
}
}
return null;
}
// 转换方法
private LawsClubJoinTeam convertToJoinTeam(LawsClubJoinTeamExcel joinTeamExcel) {
LawsClubJoinTeam joinTeam = new LawsClubJoinTeam();
BeanUtils.copyProperties(joinTeamExcel, joinTeam);
joinTeam.setJoinDate(parseDate(joinTeamExcel.getJoinDate(), "入团日期转换异常"));
return joinTeam;
}
private LawsClubMeeting convertToMeetingImport(LawsClubMeetingImportExcel meetingImportExcel) {
LawsClubMeeting meetingImport = new LawsClubMeeting();
BeanUtils.copyProperties(meetingImportExcel, meetingImport);
return meetingImport;
private LawsClubPayment convertToPayment(LawsClubPaymentExcel paymentExcel) {
LawsClubPayment payment = new LawsClubPayment();
BeanUtils.copyProperties(paymentExcel, payment);
payment.setPaymentTime(parseDate(paymentExcel.getPaymentTime(), "缴费时间转换异常"));
return payment;
}
private LawsClubMeeting convertToMeeting(LawsClubMeetingImportExcel meetingImportExcel) {
LawsClubMeeting meeting = new LawsClubMeeting();
BeanUtils.copyProperties(meetingImportExcel, meeting);
meeting.setMeetingDate(parseDate(meetingImportExcel.getMeetingDate(), "会议日期转换异常"));
return meeting;
}
private LawsClubResearchInformation convertToResearchInformation(LawsClubResearchInformationExcel researchInformationExcel) {
LawsClubResearchInformation researchInformation = new LawsClubResearchInformation();
BeanUtils.copyProperties(researchInformationExcel, researchInformation);
return researchInformation;
LawsClubResearchInformation researchInfo = new LawsClubResearchInformation();
BeanUtils.copyProperties(researchInformationExcel, researchInfo);
researchInfo.setReceiveDate(parseDate(researchInformationExcel.getReceiveDate(), "接收日期转换异常"));
researchInfo.setDeadlineDate(parseDate(researchInformationExcel.getDeadlineDate(), "截止日期转换异常"));
return researchInfo;
}
}
@@ -1,6 +1,5 @@
package com.jero.modules.laws.club.vo.excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelCollection;
@@ -9,39 +8,33 @@ import java.util.List;
@Data
public class LawsClubExcel {
@ApiModelProperty(value = "上级节点id")
@Excel(name = "上级节点", width = 20)
private String parentId;
@ApiModelProperty(value = "节点类型")
@Excel(name = "节点类型", width = 20)
private String nodeType;
@ApiModelProperty(value = "社团名称")
@Excel(name = "社团名称", width = 20)
private String clubName;
@ApiModelProperty(value = "社团地址")
@Excel(name = "社团地址", width = 20)
private String clubAddress;
@ApiModelProperty(value = "社团联系人")
@Excel(name = "社团联系人", width = 20)
private String clubContact;
@ApiModelProperty(value = "联系电话")
@Excel(name = "联系电话", width = 20)
private String contactNumber;
@ApiModelProperty(value = "入团信息列表数据")
@ExcelCollection(name = "入团信息")
private List<LawsClubJoinTeamExcel> lawsClubJoinTeamList;
@ApiModelProperty(value = "参会信息列表数据")
@ExcelCollection(name = "参会信息")
private List<LawsClubMeetingImportExcel> lawsClubMeetings;
@ApiModelProperty(value = "征集调研信息列表数据")
@ExcelCollection(name = "支付信息")
private List<LawsClubPaymentExcel> lawsClubPayment;
@ExcelCollection(name = "征集调研信息")
private List<LawsClubResearchInformationExcel> lawsClubResearchInformations;
@@ -12,7 +12,7 @@ public class LawsClubJoinTeamExcel implements Serializable {
@ApiModelProperty(value = "入团日期")
@Excel(name = "入团日期", width = 20, format = "yyyy-MM-dd")
private Date joinDate;
private String joinDate;
@ApiModelProperty(value = "部门id")
@Excel(name = "部门名称", width = 20)
@@ -20,7 +20,7 @@ public class LawsClubMeetingImportExcel implements Serializable {
private String meetingName;
@Excel(name = "会议日期", width = 20, format = "yyyy-MM-dd")
private Date meetingDate;
private String meetingDate;
@Excel(name = "会议费用", width = 20)
private String meetingCost;
@@ -0,0 +1,19 @@
package com.jero.modules.laws.club.vo.excel;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
import java.io.Serializable;
@Data
public class LawsClubPaymentExcel implements Serializable {
@Excel(name = "缴费金额", width = 20)
private String paymentAmount;
@Excel(name = "缴费时间", width = 20)
private String paymentTime;
@Excel(name = "备注", width = 20)
private String remark;
}
@@ -21,8 +21,8 @@ public class LawsClubResearchInformationExcel implements Serializable {
private String researchContent;
@Excel(name = "接收日期", width = 20, format = "yyyy-MM-dd")
private Date receiveDate;
private String receiveDate;
@Excel(name = "截止日期", width = 20, format = "yyyy-MM-dd")
private Date deadlineDate;
private String deadlineDate;
}