From 1b414a66acf92e649539270d945d43bdfa14eb94 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Thu, 2 Jun 2022 22:43:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B1=B1=E4=B8=9C=E4=B8=B4=E6=97=B6=E7=81=AB?= =?UTF-8?q?=E7=82=B9=E8=A1=A8=E5=88=B0=E5=85=A8=E5=9B=BD=E7=81=AB=E7=82=B9?= =?UTF-8?q?=E8=A1=A8=E7=9A=84=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/controller/FirePointController.java | 7 + src/main/java/com/xkrs/dao/FirePointDao.java | 4 + .../com/xkrs/service/FirePointService.java | 10 ++ .../service/impl/FirePointServiceImpl.java | 61 +++++-- src/main/java/com/xkrs/utils/XlsxUtil.java | 152 ++++++++++++++++++ 5 files changed, 218 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/xkrs/utils/XlsxUtil.java diff --git a/src/main/java/com/xkrs/controller/FirePointController.java b/src/main/java/com/xkrs/controller/FirePointController.java index 1b3f33b..53a7aec 100644 --- a/src/main/java/com/xkrs/controller/FirePointController.java +++ b/src/main/java/com/xkrs/controller/FirePointController.java @@ -25,6 +25,8 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.persistence.criteria.Predicate; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.transaction.Transactional; import java.io.IOException; import java.util.ArrayList; @@ -160,6 +162,11 @@ public class FirePointController { return outputEncapsulationObject(PromptMessageEnum.SUCCESS, shanDongFirePointList, locale); } + @RequestMapping(value = "/download/vipuserfirepoint", method = RequestMethod.GET) + public String downloadVipUserFirePoint(HttpServletResponse response, HttpServletRequest request) { + return firePointService.downloadVipUserFilePoint(request, response); + } + /** * 更新火点的街道编号数据 * diff --git a/src/main/java/com/xkrs/dao/FirePointDao.java b/src/main/java/com/xkrs/dao/FirePointDao.java index e33a2fa..0e09c4a 100644 --- a/src/main/java/com/xkrs/dao/FirePointDao.java +++ b/src/main/java/com/xkrs/dao/FirePointDao.java @@ -67,6 +67,7 @@ public interface FirePointDao extends JpaRepository, JpaS /** * 根据火点编码修改审核火点状态 + * * @param fireCode * @param auditFireType */ @@ -142,4 +143,7 @@ public interface FirePointDao extends JpaRepository, JpaS @Query(value = "UPDATE fire_point SET street_code = ?2 WHERE id = ?1", nativeQuery = true) void updateStreetCodeById(Integer id, String streetCode); + @Query(value = "select a.really_name 姓名, a.county_name 所在市, a.telephone 手机号, a.vip_level vip, b.satellite_time 扫描时间, " + "round(cast(b.longitude as numeric) ,6) 经度, round(cast(b.latitude as numeric) ,6) 纬度, b.fire_point_address 详细地址 from sys_user a left join fire_point b on " + "case when left(a.county_code, 4) = '0000' then left(b.county_code, 4) = left(a.county_code, 4) " + "when left(a.county_code, 2) = '00' then left(b.county_code, 2) = left(a.county_code, 2) " + "else left(b.county_code, 2) = left(a.county_code, 2) end where a.vip_level > 0 and satellite_time > ?1", nativeQuery = true) + List> downloadFirePointByVip(String yesterday); + } diff --git a/src/main/java/com/xkrs/service/FirePointService.java b/src/main/java/com/xkrs/service/FirePointService.java index c5e53c1..8b0379f 100644 --- a/src/main/java/com/xkrs/service/FirePointService.java +++ b/src/main/java/com/xkrs/service/FirePointService.java @@ -7,6 +7,8 @@ import com.xkrs.model.qo.FirePointQo; import com.xkrs.model.vo.AppTaskBodyVo; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.util.Map; @@ -209,4 +211,12 @@ public interface FirePointService { * 操作2:将山东火点表中的这个火点的审核状态更新 */ String updateVerifyStateByFireCode(String fireCode, String verifyState); + + /** + * download vip user's fire point + * + * @param request + * @param response + */ + String downloadVipUserFilePoint(HttpServletRequest request, HttpServletResponse response); } diff --git a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java index deb51c6..b735444 100644 --- a/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/FirePointServiceImpl.java @@ -21,15 +21,22 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.transaction.Transactional; import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.*; import java.util.stream.Collectors; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; +import static com.xkrs.utils.DateTimeUtil.dateTimeToString; +import static com.xkrs.utils.XlsxUtil.downloadXlsx; /** * @author XinYi Song @@ -117,7 +124,7 @@ public class FirePointServiceImpl implements FirePointService { firePointEntity.setSatelliteType(firePointQo.getSatelliteType()); firePointEntity.setLandType(firePointQo.getLandtype()); firePointEntity.setConfidence(firePointQo.getConfidence()); - firePointEntity.setAddTime(DateTimeUtil.dateTimeToString(LocalDateTime.now())); + firePointEntity.setAddTime(dateTimeToString(LocalDateTime.now())); firePointEntity.setFireType("0"); firePointEntity.setFireImage(firePointQo.getFireImage()); firePointEntity.setSatelliteImage(firePointQo.getSatelliteImage()); @@ -176,7 +183,7 @@ public class FirePointServiceImpl implements FirePointService { firePointEntity.setSatelliteType(firePointQo.getSatelliteType()); firePointEntity.setLandType(firePointQo.getLandtype()); firePointEntity.setConfidence(firePointQo.getConfidence()); - firePointEntity.setAddTime(DateTimeUtil.dateTimeToString(LocalDateTime.now())); + firePointEntity.setAddTime(dateTimeToString(LocalDateTime.now())); firePointEntity.setFireType("0"); firePointEntity.setFireImage(firePointQo.getFireImage()); firePointEntity.setSatelliteImage(firePointQo.getSatelliteImage()); @@ -399,6 +406,28 @@ public class FirePointServiceImpl implements FirePointService { return null; } + @Override + public String downloadVipUserFilePoint(HttpServletRequest request, HttpServletResponse response) { + LocalDate now = LocalDate.now(); + List> list = firePointDao.downloadFirePointByVip(dateTimeToString(LocalDateTime.of(now.minusDays(1), LocalTime.MIN))); + if (list.size() == 0) { + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "无数据!", LocaleContextHolder.getLocale()); + } + int year = now.getYear(); + int month = now.getMonth().getValue(); + int day = now.getDayOfMonth(); + String fileName = String.format("%s_%s_%s_VIP用户火点信息列表.xlsx", year, month, day); + response.setContentType("application/octet-stream; charset=UTF8"); + response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8)); + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); + try { + downloadXlsx(list, response.getOutputStream()); + } catch (IOException e) { + log.error("download vip user's fire point fail: {}", e.getMessage()); + } + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "ok", LocaleContextHolder.getLocale()); + } + /** * 查询今天的火点信息 */ @@ -544,7 +573,7 @@ public class FirePointServiceImpl implements FirePointService { FireAndRanger byFireCode1 = fireAndRangerDao.findByFireCode(fireCode); if (byFireCode1 == null) { FirePointEntity byFireCode4 = firePointDao.findByFireCode(fireCode); - String time = DateTimeUtil.dateTimeToString(LocalDateTime.now()); + String time = dateTimeToString(LocalDateTime.now()); FireAndRanger fireAndRanger = new FireAndRanger(); fireAndRanger.setFireCode(fireCode); // 由于测试阶段先用用户账号代替,后期换成getReallyName(用户真实姓名) @@ -556,7 +585,7 @@ public class FirePointServiceImpl implements FirePointService { } // 火点结案,将护林员的状态修改为0 表示该护林员没有在执行任务,处于空闲状态 forestRangerDao.updateRangerTypeByPhone(byFireCode1.getRangerName(), rangerType); - String time = DateTimeUtil.dateTimeToString(LocalDateTime.now()); + String time = dateTimeToString(LocalDateTime.now()); fireAndRangerDao.updateEndTimeByFireCode(fireCode, time); fireAndRangerDao.updateProgressTypeByFireCode(fireCode, "0"); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "结案", locale); @@ -583,7 +612,7 @@ public class FirePointServiceImpl implements FirePointService { if (byTaskFireCode != null) { return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "该火点的任务已经提交,请勿重复提交", locale); } - String s = DateTimeUtil.dateTimeToString(LocalDateTime.now()); + String s = dateTimeToString(LocalDateTime.now()); FireTask fireTask = new FireTask(); fireTask.setTaskFireCode(appTaskBodyVo.getFireCode()); fireTask.setTaskInformation(appTaskBodyVo.getTaskInformation()); @@ -636,10 +665,10 @@ public class FirePointServiceImpl implements FirePointService { @Override public List selectFirePointBetweenSeven() { LocalDateTime localDateTime = LocalDateTime.now(); - String endTime = DateTimeUtil.dateTimeToString(localDateTime); + String endTime = dateTimeToString(localDateTime); // 当前时间减7 LocalDateTime localDateTime1 = localDateTime.minusDays(7); - String startTime = DateTimeUtil.dateTimeToString(localDateTime1); + String startTime = dateTimeToString(localDateTime1); return query.selectFirePointBetweenThree(startTime, endTime); } @@ -652,10 +681,10 @@ public class FirePointServiceImpl implements FirePointService { @Override public List selectFirePointByMonth(String countyCode) { LocalDateTime localDateTime = LocalDateTime.now(); - String endTime = DateTimeUtil.dateTimeToString(localDateTime); + String endTime = dateTimeToString(localDateTime); // 当前时间减31 LocalDateTime localDateTime1 = localDateTime.minusDays(31); - String startTime = DateTimeUtil.dateTimeToString(localDateTime1); + String startTime = dateTimeToString(localDateTime1); if (9 == countyCode.length()) { return query.selectFirePointByMonthStreet(countyCode, startTime, endTime); } else if ("0000".equals(countyCode.substring(2))) { @@ -685,10 +714,10 @@ public class FirePointServiceImpl implements FirePointService { Map map = new HashMap(3); LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime2 = localDateTime.plusDays(1); - String endTime = DateTimeUtil.dateTimeToString(localDateTime2); + String endTime = dateTimeToString(localDateTime2); // 当前时间减30 LocalDateTime localDateTime1 = localDateTime.minusDays(30); - String startTime = DateTimeUtil.dateTimeToString(localDateTime1); + String startTime = dateTimeToString(localDateTime1); // 查询近一个月各植被类型的火点数量 List> maps = firePointDao.selectNumByLandType(startTime, endTime); map.put("land", maps); @@ -712,10 +741,10 @@ public class FirePointServiceImpl implements FirePointService { Map map = new HashMap(3); LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime2 = localDateTime.plusDays(1); - String endTime = DateTimeUtil.dateTimeToString(localDateTime2); + String endTime = dateTimeToString(localDateTime2); // 当前时间减1 LocalDateTime localDateTime1 = localDateTime.minusDays(1); - String startTime = DateTimeUtil.dateTimeToString(localDateTime1); + String startTime = dateTimeToString(localDateTime1); // 查询近一个月各植被类型的火点数量 List> maps = firePointDao.selectNumByLandType(startTime, endTime); map.put("land", maps); @@ -739,10 +768,10 @@ public class FirePointServiceImpl implements FirePointService { Map map = new HashMap(3); LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime2 = localDateTime.plusDays(1); - String endTime = DateTimeUtil.dateTimeToString(localDateTime2); + String endTime = dateTimeToString(localDateTime2); // 当前时间减7 LocalDateTime localDateTime1 = localDateTime.minusDays(7); - String startTime = DateTimeUtil.dateTimeToString(localDateTime1); + String startTime = dateTimeToString(localDateTime1); // 查询近一个月各植被类型的火点数量 List> maps = firePointDao.selectNumByLandType(startTime, endTime); map.put("land", maps); @@ -794,7 +823,7 @@ public class FirePointServiceImpl implements FirePointService { firePoint.setSatelliteType(firePointQo.getSatelliteType()); firePoint.setLandType(firePointQo.getLandtype()); firePoint.setConfidence(firePointQo.getConfidence()); - firePoint.setAddTime(DateTimeUtil.dateTimeToString(LocalDateTime.now())); + firePoint.setAddTime(dateTimeToString(LocalDateTime.now())); firePoint.setFireType("0"); firePoint.setFireImage(firePointQo.getFireImage()); firePoint.setSatelliteImage(firePointQo.getSatelliteImage()); diff --git a/src/main/java/com/xkrs/utils/XlsxUtil.java b/src/main/java/com/xkrs/utils/XlsxUtil.java new file mode 100644 index 0000000..992d19e --- /dev/null +++ b/src/main/java/com/xkrs/utils/XlsxUtil.java @@ -0,0 +1,152 @@ +package com.xkrs.utils; + +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.*; + + +/** + * @author wudong + * @date 2021/12/22 17:47 + * @description + */ +public class XlsxUtil { + private static final Logger log = LoggerFactory.getLogger(XlsxUtil.class); + + /** + * 获取表格内容, 第一行为表头,第二行为数据,最少两行 + * + * @param is + * @return + */ + public static List> getXlsxData(InputStream is) { + List> list = new ArrayList<>(); + Workbook wb = null; + try { + wb = new XSSFWorkbook(is); + } catch (IOException e) { + log.error("xlsx,读取数据失败"); + return list; + } + Sheet sheet = wb.getSheetAt(0); + int physicalNumberOfRows = sheet.getPhysicalNumberOfRows(); + if (physicalNumberOfRows < 2) { + return list; + } + Row row = sheet.getRow(0); + int titNums = row.getPhysicalNumberOfCells(); + Object[] tit = new String[titNums]; + for (int j = 0; j < titNums; j++) { + Cell cell = row.getCell(j); + Object value = getValue(cell); + tit[j] = value; + } + for (int i = 1; i < physicalNumberOfRows; i++) { + Row contentRow = sheet.getRow(i); + int physicalNumberOfCells = contentRow.getPhysicalNumberOfCells(); + Map map = new HashMap<>(physicalNumberOfCells); + for (int j = 0; j < physicalNumberOfCells; j++) { + Cell cell = contentRow.getCell(j); + Object value = getValue(cell); + map.put(tit[j], value); + } + list.add(map); + } + return list; + } + + /** + * 获取值 + * + * @param cell + * @return + */ + private static Object getValue(Cell cell) { + CellType cellType = cell.getCellType(); + Object value; + switch (cellType) { + case STRING: + value = cell.getStringCellValue().trim(); + break; + case NUMERIC: + // 获取时间数据 + if (DateUtil.isCellDateFormatted(cell)) { + Date tempValue = cell.getDateCellValue(); + SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd"); + value = simpleFormat.format(tempValue); + } else { + // 防止出现科学计数法 + DecimalFormat df = new DecimalFormat("0"); + value = df.format(cell.getNumericCellValue()); + } + break; + case BOOLEAN: + value = cell.getBooleanCellValue(); + break; + default: + value = null; + } + return value; + } + + /** + * 按内容生成xlsx, 不关闭输出流 + * + * @param contextList 参数 + * @param os + */ + public static void createXml(List> contextList, OutputStream os) { + // 提取表头 + Set strings = contextList.get(0).keySet(); + Workbook wb = new XSSFWorkbook(); + // 创建一页sheet + Sheet sheet = wb.createSheet(); + // 创建表头 + Row row = sheet.createRow(0); + int i = 0; + for (String key : strings) { + row.createCell(i++).setCellValue(key); + } + // 填写内容 + for (int j = 0; j < contextList.size(); j++) { + Row ct = sheet.createRow(j + 1); + ct.setHeight((short) 1200); + Map map = contextList.get(j); + for (int k = 0; k < map.size(); k++) { + ct.createCell(k).setCellValue(String.valueOf(map.get(row.getCell(k).getStringCellValue()))); + } + } + try { + // 可以直接写入到zipOutputStream嘛,需要转一下,转为输入流,再进行写入,这样的话就需要把流输出,不能在该地方关闭 + wb.write(os); + wb.close(); + System.out.println("finish"); + } catch (IOException e) { + log.error("生成Xlsx失败,IO操作失败, 输出到IO失败", e); + } + } + + + /** + * 下载文件 单个 + * + * @param contextList + * @param os + */ + public static void downloadXlsx(List> contextList, OutputStream os) { + createXml(contextList, os); + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +}