diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/domian/DmpMonitorNumDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/domian/DmpMonitorNumDto.java new file mode 100644 index 0000000..c04108e --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/domian/DmpMonitorNumDto.java @@ -0,0 +1,23 @@ +package me.zhengjie.modules.dmpMonitor.domian; + +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.util.Date; +import java.util.List; + +/** + * @author wujingtao + * @date 2022/03/01 + */ +@Data +public class DmpMonitorNumDto { + @NotNull + private Date startTime; + @NotNull + private Date endTime; + @NotNull + private List tags; + + private String mail; +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/request/FtpRequest.java b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/request/FtpRequest.java new file mode 100644 index 0000000..cf2e49f --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/request/FtpRequest.java @@ -0,0 +1,63 @@ +package me.zhengjie.modules.dmpMonitor.request; + +import cn.hutool.extra.ssh.JschUtil; +import com.jcraft.jsch.ChannelSftp; +import com.jcraft.jsch.Session; +import com.jcraft.jsch.SftpException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.exception.BadRequestException; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * @author wujingtao + * @date 2022/03/01 + */ +@Slf4j +@Component +public class FtpRequest { + + @Value("${remote.link.file-base-path-linux}") + private String fileBasePathLinux; + + public String ftpUpload(String path) { + String linePath = ""; + try { + Session session = JschUtil.getSession("162.62.197.152", 22, "root", "yuyou@ECS2020"); + ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp"); + sftp.connect(); + log.info("连接成功"); + try { + sftp.cd(fileBasePathLinux); + } catch (SftpException sft) { + log.error("大数据平台目录不存在,{}", fileBasePathLinux); + throw new BadRequestException("大数据平台目录不存在"); + } + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd"); + String timeForMate = LocalDateTime.now().format(dtf); + try { + sftp.cd(timeForMate); + } catch (SftpException sft) { + sftp.mkdir(timeForMate); + sftp.cd(timeForMate); + } + File file = new File(path); + InputStream in = new FileInputStream(file); + sftp.put(in, file.getName()); + linePath = fileBasePathLinux + timeForMate + File.separator + file.getName(); + in.close(); + JschUtil.close(sftp); + JschUtil.close(session); + } catch (Exception e) { + throw new BadRequestException("文件上传到大数据平台失败"); + } + return linePath; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/request/HttpRequestRds.java b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/request/HttpRequestRds.java new file mode 100644 index 0000000..0275691 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/request/HttpRequestRds.java @@ -0,0 +1,60 @@ +package me.zhengjie.modules.dmpMonitor.request; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.modules.dmpMonitor.domian.DmpMonitorNumDto; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/** + * @author wujingtao + * @date 2022/03/01 + */ +@Slf4j +@Component +public class HttpRequestRds { + + /** + * 请求大数据平台 + * + * @param filePath + * @return + */ + public String rdsRequest(String filePath) { + String link = ""; + HttpResponse response = getRequest("http://162.62.197.152:8000/api/temp/file/link/nofile", filePath); + if (response.isOk()) { + JSONObject jsonObject = JSONUtil.parseObj(response.body()); + link = jsonObject.getStr("data"); + } + return link; + } + + public Map callBackNumByTags(DmpMonitorNumDto dmp) { + JSONObject jsonObject = new JSONObject(); + HttpResponse response = getRequest("http://47.110.11.213:9191/api/dmp/imeiNumber", JSONUtil.toJsonStr(dmp)); + if (response.isOk()) { + jsonObject = JSONUtil.parseObj(response.body()); + } + return jsonObject; + } + + public Map callBackImeiLink(DmpMonitorNumDto dmp) { + JSONObject jsonObject = new JSONObject(); +// http://47.110.11.213:9191/api/dmp/imeiLink + HttpResponse response = getRequest("http://47.110.11.213:9191/api/dmp/imeiLink", JSONUtil.toJsonStr(dmp)); + if (response.isOk()) { + jsonObject = JSONUtil.parseObj(response.body()); + } + return jsonObject; + } + + private HttpResponse getRequest(String url, String body) { + return HttpRequest.post(url) + .body(body).execute(); + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/rest/StatisticsDmpController.java b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/rest/StatisticsDmpController.java new file mode 100644 index 0000000..5e2b075 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/rest/StatisticsDmpController.java @@ -0,0 +1,43 @@ +package me.zhengjie.modules.dmpMonitor.rest; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import me.zhengjie.annotation.Log; +import me.zhengjie.modules.dmpMonitor.domian.DmpMonitorNumDto; +import me.zhengjie.modules.dmpMonitor.service.StatisticsDmpService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + + +/** + * @author wujingtao + * @date 2022/03/01 + */ +@RestController +@RequiredArgsConstructor +@Api(tags = "dmp监测") +@RequestMapping("/api/dmp") +public class StatisticsDmpController { + private final StatisticsDmpService statisticsDmpService; + + @Log("获取数目") + @ApiOperation("导出数据") + @PostMapping(value = "/number") + @PreAuthorize("@el.check('Dmp:list')") + public ResponseEntity getDmpNumByTags(DmpMonitorNumDto conditionDto) { + return new ResponseEntity<>(statisticsDmpService.queryNumByTags(conditionDto), HttpStatus.OK); + } + + @Log("发送邮件") + @ApiOperation("发送邮件") + @PostMapping(value = "/sendMail") + public ResponseEntity sendEmail(DmpMonitorNumDto conditionDto) { + statisticsDmpService.sendEmail(conditionDto); + return new ResponseEntity<>(HttpStatus.OK); + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/service/StatisticsDmpService.java b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/service/StatisticsDmpService.java new file mode 100644 index 0000000..7019ebe --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/service/StatisticsDmpService.java @@ -0,0 +1,16 @@ +package me.zhengjie.modules.dmpMonitor.service; + +import me.zhengjie.modules.dmpMonitor.domian.DmpMonitorNumDto; + +import java.util.Map; + +/** + * @author wujingtao + * @date 2022/03/01 + */ +public interface StatisticsDmpService { + + Map queryNumByTags(DmpMonitorNumDto conditionDto); + + void sendEmail(DmpMonitorNumDto conditionDto); +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/service/impl/StatisticsDmpServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/service/impl/StatisticsDmpServiceImpl.java new file mode 100644 index 0000000..43ded33 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/dmpMonitor/service/impl/StatisticsDmpServiceImpl.java @@ -0,0 +1,90 @@ +package me.zhengjie.modules.dmpMonitor.service.impl; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.ZipUtil; +import cn.hutool.poi.excel.ExcelUtil; +import cn.hutool.poi.excel.ExcelWriter; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.domain.vo.EmailVo; +import me.zhengjie.exception.BadRequestException; +import me.zhengjie.modules.dmpMonitor.domian.DmpMonitorNumDto; +import me.zhengjie.modules.dmpMonitor.request.FtpRequest; +import me.zhengjie.modules.dmpMonitor.request.HttpRequestRds; +import me.zhengjie.modules.dmpMonitor.service.StatisticsDmpService; +import me.zhengjie.modules.uploadnew.service.impl.BuildPathUtils; +import me.zhengjie.service.EmailService; +import me.zhengjie.utils.FileUtil; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @author wujingtao + * @date 2022/03/01 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class StatisticsDmpServiceImpl implements StatisticsDmpService { + private final BuildPathUtils buildPathUtils; + private final FtpRequest ftpRequest; + private final HttpRequestRds httpRequestRds; + private final EmailService emailService; + + @Override + public Map queryNumByTags(DmpMonitorNumDto conditionDto) { + return httpRequestRds.callBackNumByTags(conditionDto); + } + + @Override + public void sendEmail(DmpMonitorNumDto conditionDto) { + if (StrUtil.isBlank(conditionDto.getMail())) { + throw new BadRequestException("邮箱地址不能为空"); + } + Map map = httpRequestRds.callBackImeiLink(conditionDto); + map.forEach((k, v) -> { + //生成文件 + String path = buildPathUtils.buildFileDmpPath(k, conditionDto.getStartTime(), conditionDto.getEndTime()); + ExcelWriter writer = ExcelUtil.getWriter(path + ".xlsx"); + writer.write(getData(v), true); + writer.close(); + }); + String contentPath = buildPathUtils.buildFileDmpZipPath(); + String zipPath = buildPathUtils.buildFilePath() + ".zip"; + File zip = ZipUtil.zip(contentPath, zipPath); + //上传文件到大数据平台 + String s = ftpRequest.ftpUpload(zipPath); + + //远程调用生成短链 + String link = httpRequestRds.rdsRequest(s); + + //发送邮件 + EmailVo emailVo = new EmailVo(); + List list = new ArrayList<>(); + list.add(conditionDto.getMail()); + emailVo.setTos(list); + emailVo.setSubject("一次性短链"); + emailVo.setContent(link); + emailService.send(emailVo, emailService.find()); + System.out.println(link); + + //删除临时文件 + FileUtil.del(contentPath); + FileUtil.del(zip); + } + + private List> getData(Object v) { + List> data = new ArrayList<>(); + List list = (List) v; + for (String info : list) { + List rows = new ArrayList<>(); + rows.add(info); + data.add(rows); + } + return data; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/BuildPathUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/BuildPathUtils.java index 9d2b3ff..6667536 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/BuildPathUtils.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/BuildPathUtils.java @@ -1,5 +1,6 @@ package me.zhengjie.modules.uploadnew.service.impl; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.system.OsInfo; import cn.hutool.system.SystemUtil; @@ -11,6 +12,7 @@ import org.springframework.stereotype.Service; import java.io.File; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.Date; /** @@ -102,4 +104,58 @@ public class BuildPathUtils { return ""; } } + + /** + * 构建文件上传保存路径 + */ + public String buildFileDmpPath(String fileName, Date startTime, Date endTime) { + // 获取环境配置信息 + OsInfo osInfo = SystemUtil.getOsInfo(); + String timeForMate = fileName + " " + DateUtil.format(startTime, "MM.dd") + "-" + DateUtil.format(endTime, "MM.dd"); + String dirPath; + if (osInfo.isWindows()) { + dirPath = fileBasePathWindows + "dmp" + File.separator + timeForMate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else if (osInfo.isLinux()) { + dirPath = fileBasePathLinux + "dmp" + File.separator + timeForMate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else if (osInfo.isMac()) { + + dirPath = fileBasePathMac + "dmp" + File.separator + timeForMate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else { + return ""; + } + } + + public String buildFileDmpZipPath() { + // 获取环境配置信息 + OsInfo osInfo = SystemUtil.getOsInfo(); + String dirPath; + if (osInfo.isWindows()) { + dirPath = fileBasePathWindows + "dmp"; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else if (osInfo.isLinux()) { + dirPath = fileBasePathLinux + "dmp"; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else if (osInfo.isMac()) { + + dirPath = fileBasePathMac + "dmp"; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else { + return ""; + } + } }