package com.dgtlwks.ifl.user.service.impl;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dgtlwks.ifl.user.entity.ContractInfo;
import com.dgtlwks.ifl.user.mapper.ContractInfoMapper;
import com.dgtlwks.ifl.user.service.ContractInfoService;
import com.dgtlwks.ifl.user.util.DownloadPdf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
/**
* @author ruishan.chen
* @version f_1.7
* description: ContractInfoServiceImpl
* @date 2021/11/18 16:28
* Copyright (c) 2021/11/18,All Rights Reserved.
*/
@Service
@Slf4j
public class ContractInfoServiceImpl extends ServiceImpl<ContractInfoMapper, ContractInfo> implements ContractInfoService {
@Override
public void downloadAllContract() {
List<ContractInfo> allContractDTOList = this.list();
int i = 0;
for (ContractInfo allContractDTO : allContractDTOList) {
if (allContractDTO != null) {
if (StringUtils.hasText(allContractDTO.getUrl()) && StringUtils.hasText(allContractDTO.getName())) {
try {
log.info("开始下载。。。");
String fileLocal = "D:\\share\\" + allContractDTO.getName()
+"-" +getTypeName(allContractDTO.getType())+"~"+ DateUtil.format(allContractDTO.getSignTime(), "yyyy-MM-dd") + ".pdf";
DownloadPdf.downloadFile(allContractDTO.getUrl(), fileLocal);
log.info("下载数量:{}。。。", i++);
} catch (Exception e) {
log.error("合同下载失败");
e.printStackTrace();
}
}
}
}
}
public String getTypeName(int type) {
if (type == 1) {
return "某未知";
}
if (type == 2) {
return "实战班";
}
if (type == 3) {
return "镰刀班";
}
if (type == 4) {
return "合伙人合同";
}
return "定金合伙人合同";
}
}
package com.dgtlwks.ifl.user.util;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author ruishan.chen
* @version f_1.7
* description: DownloadPdf
* @date 2021/11/18 12:20
* Copyright (c) 2021/11/18,All Rights Reserved.
*/
public class DownloadPdf {
/**
* TODO 下载文件到本地
* @author nadim
* @date Sep 11, 2015 11:45:31 AM
* @param fileUrl 远程地址
* @param fileLocal 本地路径
* @throws Exception
*/
public static void downloadFile(String fileUrl,String fileLocal) throws Exception {
URL url = new URL(fileUrl);
HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
urlCon.setConnectTimeout(6000);
urlCon.setReadTimeout(6000);
int code = urlCon.getResponseCode();
if (code != HttpURLConnection.HTTP_OK) {
throw new Exception("文件读取失败");
}
//读文件流
DataInputStream in = new DataInputStream(urlCon.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));
byte[] buffer = new byte[2048];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
in.close();
}
}
package com.dgtlwks.ifl.user.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
/**
* @author ruishan.chen
* @version f_1.7
* description: ContractInfo
* @date 2021/11/18 16:22
* Copyright (c) 2021/11/18,All Rights Reserved.
*/
@Setter
@Getter
@Builder
@TableName("contract_info")
@ApiModel(value = "合同")
public class ContractInfo {
@ApiModelProperty(value = "id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField("name")
private String name;
@TableField("type")
private Integer type;
@TableField("sign_time")
private Date signTime;
@TableField("account")
private String account;
@TableField("status")
private Integer status;
@TableField("old")
private Integer old;
@TableField("url")
private String url;
}