From 346284ae265008cbe1130cd7f54f48fd0b4b6ed2 Mon Sep 17 00:00:00 2001 From: "QYTHINK\\qiaoy" Date: Mon, 16 Jan 2023 10:12:24 +0800 Subject: [PATCH] =?UTF-8?q?excel=E4=BC=9A=E8=AE=AE=E5=AE=A4=E7=94=B3?= =?UTF-8?q?=E8=AF=B7=E8=AE=B0=E5=BD=95=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Excel/MeetingExcelDataListener.java | 234 ++++++++++++++++++ .../controller/BookMeetingRoomController.java | 37 ++- .../modules/meeting/dto/MeetingExcelDTO.java | 29 +++ .../dto/MeetingExcelValidationDTO.java | 18 ++ 4 files changed, 315 insertions(+), 3 deletions(-) create mode 100644 renren-admin/src/main/java/io/renren/modules/meeting/Excel/MeetingExcelDataListener.java create mode 100644 renren-admin/src/main/java/io/renren/modules/meeting/dto/MeetingExcelDTO.java create mode 100644 renren-admin/src/main/java/io/renren/modules/meeting/dto/MeetingExcelValidationDTO.java diff --git a/renren-admin/src/main/java/io/renren/modules/meeting/Excel/MeetingExcelDataListener.java b/renren-admin/src/main/java/io/renren/modules/meeting/Excel/MeetingExcelDataListener.java new file mode 100644 index 00000000..f2fc1a30 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/meeting/Excel/MeetingExcelDataListener.java @@ -0,0 +1,234 @@ +package io.renren.modules.meeting.Excel; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import io.renren.modules.meeting.dao.TMeetingroomMapper; +import io.renren.modules.meeting.dto.MeetingExcelDTO; +import io.renren.modules.meeting.dto.MeetingExcelValidationDTO; +import io.renren.modules.meeting.dto.TMeetingroomBookDTO; +import io.renren.modules.meeting.dto.TMeetingroomDTO; +import io.renren.modules.meeting.entity.TMeetingroom; +import io.renren.modules.meeting.service.TMeetingroomBookService; +import io.renren.modules.sys.dao.SysUserDao; +import io.renren.modules.sys.entity.SysUserEntity; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Component; + +import java.text.ParseException; +import javax.annotation.Resource; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.concurrent.locks.ReentrantLock; + +@Component +public class MeetingExcelDataListener extends AnalysisEventListener { + + @Resource + private TMeetingroomMapper tMeetingroomMapper; + @Resource + private SysUserDao sysUserDao; + + @Resource + private TMeetingroomBookService tMeetingroomBookService; + /** + * 定义一个存储的界限,每读取5条数据就存储一次数据库,防止数据过多时发生溢出 + * 存储完成之后就清空list重新读取新的数据,方便内存回收 + */ + private static final int BATCH_COUNT = 5; + + /** + * 定义一个数据存储缓存,用于临时存储读取到的数据 + */ + private List cacheDataList = new ArrayList<>(); + public List badlist = new ArrayList<>(); + + private ReentrantLock badlistlock = new ReentrantLock(); + + public void lock (){ + badlistlock.lock(); + } + + public void unlock(){ + badlistlock.unlock(); + } + + @Override + public void invoke(MeetingExcelDTO item, AnalysisContext analysisContext) { + + cacheDataList.add(item); + /** + * 如果当前缓存列表中的数据等于指定值,就存储 + */ + if (cacheDataList.size() == BATCH_COUNT) { + //保存数据到数据库 + saveData(); + //清空缓存列表重新读取 + cacheDataList.clear(); + } + + } + + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + saveData(); + cacheDataList.clear(); + } + + private void saveData() { + System.out.println(Thread.currentThread().getName()+"----------------------------------"); + for (MeetingExcelDTO item : cacheDataList) { + String sportOriginalValue = item.getActivitySpot(); + if (StringUtils.isBlank(sportOriginalValue)) { + continue; + } + //一条数据里有多个会议室 + if (sportOriginalValue.contains("、")) { + String spots[] = sportOriginalValue.split("、"); + for (int i = 1; i < spots.length; i++) { + MeetingExcelDTO meetingExcelDTO = new MeetingExcelDTO(); + BeanUtils.copyProperties(item, meetingExcelDTO); + meetingExcelDTO.setActivitySpot(spots[i]); + MeetingExcelValidationDTO validationDTO = checkAndInsert(meetingExcelDTO); + if (!validationDTO.getValidated()) { + badlist.add(validationDTO); + } + } + item.setActivitySpot(spots[0]); + } + + MeetingExcelValidationDTO validationDTO = checkAndInsert(item); + if (!validationDTO.getValidated()) { + badlist.add(validationDTO); + } + } + } + + private MeetingExcelValidationDTO checkAndInsert(MeetingExcelDTO item) { + MeetingExcelValidationDTO result = new MeetingExcelValidationDTO(); + result.setMeetingExcelDTO(item); + + TMeetingroomBookDTO dto = new TMeetingroomBookDTO(); + //会议室处理 + QueryWrapper qw = new QueryWrapper(); + qw.eq("name", item.getActivitySpot().trim()); + qw.eq("del_flag", 0); + TMeetingroom tMeetingroom = tMeetingroomMapper.selectOne(qw); + if (tMeetingroom == null) { + result.setValidated(false); + result.getErrors().add("会议室找不到对应:" + item.getActivitySpot().trim()); + } else { + dto.setRoomId(tMeetingroom.getId()); + dto.setRoomName(tMeetingroom.getName()); + } + + //活动时间处理 + String activityTimeStr = item.getActivityTime(); + if (StringUtils.isNotBlank(activityTimeStr)) { + Date activityTimeDate = null; + Date startTime = null; + Date endTime = null; + Date bookTime = null; + SimpleDateFormat format0 = new SimpleDateFormat("yyyy/M/d"); + try { + activityTimeDate = format0.parse(activityTimeStr); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(activityTimeDate); + calendar.set(Calendar.HOUR, 9); + startTime = calendar.getTime(); + calendar.add(Calendar.HOUR, 1); + endTime = calendar.getTime(); + bookTime = startTime; + } catch (ParseException e) { + e.printStackTrace(); + } + if (startTime == null) { + SimpleDateFormat format1 = new SimpleDateFormat("yyyy/M/d HH:mm"); + try { + startTime = format0.parse(activityTimeStr); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(startTime); + calendar.add(Calendar.HOUR, 1); + endTime = calendar.getTime(); + bookTime = startTime; + } catch (ParseException e) { + e.printStackTrace(); + } + } + if (startTime == null) { + result.setValidated(false); + result.getErrors().add("活动时间异常:" + item.getActivityTime()); + } else { + //开始时间 + dto.setStartTime(startTime); + //结束时间 + dto.setEndTime(endTime); + //提交预约时间 + dto.setBookDate(bookTime); + } + } else { + result.setValidated(false); + result.getErrors().add("活动时间异常:" + item.getActivityTime()); + } + + //申请人处理 + String liaison = item.getLiaison(); + if (StringUtils.isNotBlank(liaison)) { + dto.setName(liaison);//申请人 + QueryWrapper qw1 = new QueryWrapper(); + qw1.eq("real_name", liaison); + qw1.eq("status", 1); + SysUserEntity user = sysUserDao.selectOne(qw1); + if (user == null) { + result.setValidated(false); + result.getErrors().add("联系人在系统中没有:" + liaison); + } else { + dto.setPhone(user.getMobile()); + } + } else { + result.setValidated(false); + result.getErrors().add("联系人异常:" + liaison); + } + if (result.getValidated()) { + StringBuffer matterBuff = new StringBuffer(); + if (StringUtils.isNotBlank(item.getActivityName())) { + matterBuff.append("活动名称:"); + matterBuff.append(item.getActivityName()); + matterBuff.append(" "); + } + if (StringUtils.isNotBlank(item.getParticipants())) { + matterBuff.append("参会人员:"); + matterBuff.append(item.getParticipants()); + matterBuff.append(" "); + } + if (StringUtils.isNotBlank(item.getActivityContents())) { + matterBuff.append("活动内容:"); + matterBuff.append(item.getActivityContents()); + matterBuff.append(" "); + } + dto.setMatter(matterBuff.toString()); + + dto.setState(2);//通过 + dto.setAuditViem("通过"); + dto.setDept(item.getDept());//部门 + + QueryWrapper qw2 = new QueryWrapper(); + qw2.eq("real_name", "城市云脑工作专班"); + qw2.eq("status", 1); + SysUserEntity user = sysUserDao.selectOne(qw2); + dto.setCreator(user.getId()); + dto.setCreateDate(dto.getBookDate()); + dto.setAuditor(user.getId()); + dto.setAuditTime(dto.getBookDate()); + + //插入数据 + tMeetingroomBookService.save(dto); + } + return result; + } + +} diff --git a/renren-admin/src/main/java/io/renren/modules/meeting/controller/BookMeetingRoomController.java b/renren-admin/src/main/java/io/renren/modules/meeting/controller/BookMeetingRoomController.java index 9013a5c3..1d054042 100644 --- a/renren-admin/src/main/java/io/renren/modules/meeting/controller/BookMeetingRoomController.java +++ b/renren-admin/src/main/java/io/renren/modules/meeting/controller/BookMeetingRoomController.java @@ -1,5 +1,6 @@ package io.renren.modules.meeting.controller; +import com.alibaba.excel.EasyExcel; import io.renren.common.annotation.LogOperation; import io.renren.common.constant.Constant; import io.renren.common.page.PageData; @@ -8,6 +9,9 @@ import io.renren.common.validator.ValidatorUtils; import io.renren.common.validator.group.AddGroup; import io.renren.common.validator.group.DefaultGroup; import io.renren.common.validator.group.UpdateGroup; +import io.renren.modules.meeting.Excel.MeetingExcelDataListener; +import io.renren.modules.meeting.dto.MeetingExcelDTO; +import io.renren.modules.meeting.dto.MeetingExcelValidationDTO; import io.renren.modules.meeting.dto.TMeetingroomBookDTO; import io.renren.modules.meeting.dto.TMeetingroomDTO; import io.renren.modules.meeting.service.TMeetingroomBookService; @@ -21,15 +25,16 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import springfox.documentation.annotations.ApiIgnore; import javax.annotation.Resource; import java.text.ParseException; -import java.util.Date; -import java.util.List; -import java.util.Map; +import java.util.*; @Api(tags = "预约会客厅") @RestController @@ -201,4 +206,30 @@ public class BookMeetingRoomController { return new Result(); } + + @Autowired + private MeetingExcelDataListener meetingExcelDataListener; + + + @PostMapping("importExcel") + @ApiOperation("导入Excel") + public Result importExcel(@RequestParam("excelfile") MultipartFile file) throws Exception { + meetingExcelDataListener.lock(); + //解析并保存到数据库 + EasyExcel.read(file.getInputStream(), MeetingExcelDTO.class, meetingExcelDataListener).sheet().doRead(); + Result r = new Result(); + + ArrayList clonedList = new ArrayList<>(); + Iterator it = meetingExcelDataListener.badlist.iterator(); + while (it.hasNext()) { + MeetingExcelValidationDTO s = it.next(); + MeetingExcelValidationDTO newS = new MeetingExcelValidationDTO(); + BeanUtils.copyProperties(s, newS); + clonedList.add(newS); + } + r.setData(clonedList); + meetingExcelDataListener.badlist.clear(); + meetingExcelDataListener.unlock(); + return r; + } } diff --git a/renren-admin/src/main/java/io/renren/modules/meeting/dto/MeetingExcelDTO.java b/renren-admin/src/main/java/io/renren/modules/meeting/dto/MeetingExcelDTO.java new file mode 100644 index 00000000..6aff54f9 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/meeting/dto/MeetingExcelDTO.java @@ -0,0 +1,29 @@ +package io.renren.modules.meeting.dto; + +import com.alibaba.excel.annotation.ExcelProperty; +import io.swagger.annotations.ApiModel; +import lombok.Data; + +@Data +@ApiModel(value = "excel导入") +public class MeetingExcelDTO { + + @ExcelProperty(value = "序号") + private int id; + @ExcelProperty(value = "活动名称") + private String activityName; + @ExcelProperty(value = "活动时间") + private String activityTime; + @ExcelProperty(value = "活动地点") + private String activitySpot; + @ExcelProperty(value = "参会人员") + private String participants; + @ExcelProperty(value = "活动内容") + private String activityContents; + @ExcelProperty(value = "组织单位") + private String dept; + @ExcelProperty(value = "联系人") + private String liaison; + + +} diff --git a/renren-admin/src/main/java/io/renren/modules/meeting/dto/MeetingExcelValidationDTO.java b/renren-admin/src/main/java/io/renren/modules/meeting/dto/MeetingExcelValidationDTO.java new file mode 100644 index 00000000..9f97f670 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/meeting/dto/MeetingExcelValidationDTO.java @@ -0,0 +1,18 @@ +package io.renren.modules.meeting.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.ArrayList; + +@Data +@ApiModel(value = "excel数据验证结果") +public class MeetingExcelValidationDTO { + @ApiModelProperty(value = "数据内容") + private MeetingExcelDTO meetingExcelDTO; + @ApiModelProperty(value = "验证结果") + private Boolean validated = true; + @ApiModelProperty(value = "错误内容") + private ArrayList errors = new ArrayList<>(); +}