提交 | 用户 | age
|
739bf5
|
1 |
package com.hx.phip.his.task; |
C |
2 |
|
|
3 |
import com.alibaba.fastjson.JSONObject; |
|
4 |
import com.hx.common.service.CommonService; |
|
5 |
import com.hx.exception.TipsException; |
|
6 |
import com.hx.mp.util.MD5Util; |
|
7 |
import com.hx.mybatisTool.SqlSentence; |
|
8 |
import com.hx.phiappt.model.BaseEntity; |
|
9 |
import com.hx.phiappt.model.ComparePhotoRecordPictures; |
|
10 |
import com.hx.phip.his.config.BaseConfig; |
|
11 |
import com.hx.phip.his.config.SystemConfig; |
|
12 |
import com.hx.phiappt.dao.mapper.ComparePhotoRecordPicturesMapper; |
|
13 |
import com.hx.resultTool.Result; |
|
14 |
import com.hx.util.DateUtil; |
|
15 |
import com.hx.util.StringUtils; |
|
16 |
import com.hz.his.feign.service.phis.SFileService; |
|
17 |
import org.apache.commons.fileupload.FileItem; |
|
18 |
import org.apache.commons.fileupload.FileItemFactory; |
|
19 |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
20 |
import org.apache.http.entity.ContentType; |
|
21 |
import org.slf4j.Logger; |
|
22 |
import org.slf4j.LoggerFactory; |
|
23 |
import org.springframework.scheduling.annotation.Scheduled; |
|
24 |
import org.springframework.stereotype.Component; |
|
25 |
import org.springframework.web.multipart.MultipartFile; |
|
26 |
import org.springframework.web.multipart.commons.CommonsMultipartFile; |
|
27 |
|
|
28 |
import javax.annotation.Resource; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.OutputStream; |
|
32 |
import java.net.HttpURLConnection; |
|
33 |
import java.net.URL; |
|
34 |
import java.util.*; |
|
35 |
|
|
36 |
/** |
|
37 |
* @Author |
|
38 |
*/ |
|
39 |
@Component |
|
40 |
public class HisUserPicturesTask { |
|
41 |
|
|
42 |
// log4g |
|
43 |
private static final Logger logger = LoggerFactory.getLogger(HisUserPicturesTask.class); |
|
44 |
|
|
45 |
@Resource |
|
46 |
private BaseConfig baseConfig; |
|
47 |
@Resource |
|
48 |
private CommonService commonService; |
|
49 |
@Resource |
|
50 |
private SFileService sFileService; |
|
51 |
@Resource |
|
52 |
private SystemConfig systemConfig; |
|
53 |
|
|
54 |
//@Scheduled(cron="0 0/5 * * * ?") |
|
55 |
public void syncPictureTask() { |
|
56 |
this.handlerSyncPicture(null); |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* 处理上传用户对比照图片 |
|
61 |
* @param userId 用户id |
|
62 |
*/ |
|
63 |
public void handlerSyncPicture(String userId) { |
|
64 |
logger.info("开始执行his图片转换成本地服务器图片定时器"); |
|
65 |
SqlSentence sqlSentence = new SqlSentence(); |
|
66 |
Map<String, Object> map = new HashMap<>(); |
|
67 |
map.put("isDel", BaseEntity.NO); |
|
68 |
StringBuilder sql = new StringBuilder(); |
|
69 |
sql.append("SELECT * FROM compare_photo_record_pictures WHERE isDel=#{m.isDel} "); |
|
70 |
if (!StringUtils.isEmpty(userId)) { |
|
71 |
sql.append(" AND comparePhotoRecordId IN (SELECT id FROM compare_photo_record WHERE userId=#{m.userId} AND isDel=#{m.isDel}) "); |
|
72 |
map.put("userId", userId); |
|
73 |
} |
|
74 |
sql.append(" AND imgUrl IS NULL AND hisImgUrl IS NOT NULL ORDER BY createTime ASC LIMIT #{m.pageNum},#{m.pageSize}"); |
|
75 |
sqlSentence.sqlSentence(sql.toString(), map); |
|
76 |
List<ComparePhotoRecordPictures> list; |
|
77 |
// 翻页查询数据 |
|
78 |
int pageNum = 0; |
|
79 |
int pageSize = 200; |
|
80 |
// 数据处理 |
|
81 |
while (true) { |
|
82 |
map.put("pageNum", pageNum); |
|
83 |
map.put("pageSize", pageSize); |
|
84 |
list = commonService.selectList(ComparePhotoRecordPicturesMapper.class, sqlSentence); |
|
85 |
for (ComparePhotoRecordPictures comparePhotoRecordPictures : list) { |
|
86 |
try { |
|
87 |
// 处理上传图片转换 |
|
88 |
String imgUrl = this.downloadImg(comparePhotoRecordPictures.getHisImgUrl(), comparePhotoRecordPictures.getId(), comparePhotoRecordPictures.getCreateTime()); |
|
89 |
if (!StringUtils.isEmpty(imgUrl)) { |
|
90 |
comparePhotoRecordPictures.setImgUrl(imgUrl); |
|
91 |
commonService.updateAll(ComparePhotoRecordPicturesMapper.class, comparePhotoRecordPictures); |
|
92 |
} |
|
93 |
} catch (Exception e) { |
|
94 |
logger.error("数据id: {} 执行his图片转换成本地服务器图片出现异常:", comparePhotoRecordPictures.getId(), e); |
|
95 |
} |
|
96 |
} |
|
97 |
if (list.size() < pageSize) { |
|
98 |
break; |
|
99 |
} |
|
100 |
pageNum = pageNum + pageSize; |
|
101 |
} |
|
102 |
logger.info("完成执行his图片转换成本地服务器图片定时器"); |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* 上传图片方法 |
|
107 |
* @param url url |
|
108 |
* @param imgName 文件名称 |
|
109 |
* @param createTime 文件创建时间 |
|
110 |
* @return 返回 |
|
111 |
*/ |
|
112 |
public synchronized String downloadImg(String url, String imgName, Date createTime) { |
|
113 |
/* URL urlfile = new URL(url); |
|
114 |
InputStream inStream = urlfile.openStream(); |
|
115 |
|
|
116 |
File file = File.createTempFile("temp", ".JPG"); |
|
117 |
System.out.println(file.getPath()); |
|
118 |
File targetFile = new File(file.getPath()); |
|
119 |
org.apache.commons.io.FileUtils.copyInputStreamToFile(inStream, targetFile);*/ |
|
120 |
|
|
121 |
/*URL urlfile = new URL(url); |
|
122 |
InputStream inStream = urlfile.openStream();*/ |
|
123 |
|
|
124 |
// 创建文件名称 小写 |
|
125 |
String fileName = MD5Util.MD5Encode(imgName + DateUtil.formatDate_1(createTime), "UTF-8").toLowerCase().replace("-", "").replace(" ", ""); |
|
126 |
// 获取文件 |
|
127 |
MultipartFile file = createFileItem(url, imgName); |
|
128 |
Result result = sFileService.upload(file, "phi_appt/synchro_picture", systemConfig.getCosBucketName(), fileName + ".jpg"); |
|
129 |
if (!result.checkCode()) { |
|
130 |
logger.error("上传图片失败:" + JSONObject.toJSONString(result)); |
|
131 |
throw new TipsException("上传图片失败!"); |
|
132 |
} |
|
133 |
|
|
134 |
JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(result.getData())); |
|
135 |
String fileUrl = ""; |
|
136 |
if (object != null) { |
|
137 |
fileUrl = object.getString("fileUrl"); |
|
138 |
|
|
139 |
} |
|
140 |
return fileUrl; |
|
141 |
|
|
142 |
/* StringBuffer filePath = new StringBuffer(); |
|
143 |
filePath.append("phi_appt/synchro_his/"+imgName); |
|
144 |
try { synchro_contrast |
|
145 |
String imgUrl = OSSUtil.uploadImg(filePath.toString(), file, baseConfig.getOssKeyId(), baseConfig.getOssKeySecret(), baseConfig.getOssEndPoint(), baseConfig.getOssBucket()); |
|
146 |
return imgUrl; |
|
147 |
} catch (Exception e) { |
|
148 |
logger.info("上传图片失败:"+e.getMessage()); |
|
149 |
e.printStackTrace(); |
|
150 |
}*/ |
|
151 |
} |
|
152 |
|
|
153 |
/** |
|
154 |
* 创建文件 |
|
155 |
* @param url url |
|
156 |
* @param imgName 图片名称 |
|
157 |
* @return 返回 |
|
158 |
*/ |
|
159 |
private static MultipartFile createFileItem(String url,String imgName) { |
|
160 |
FileItem item = null; |
|
161 |
try { |
|
162 |
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); |
|
163 |
conn.setReadTimeout(60000); |
|
164 |
conn.setConnectTimeout(60000); |
|
165 |
//设置应用程序要从网络连接读取数据 |
|
166 |
conn.setDoInput(true); |
|
167 |
conn.setRequestMethod("GET"); |
|
168 |
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { |
|
169 |
InputStream is = conn.getInputStream(); |
|
170 |
// 创建工厂对象 |
|
171 |
FileItemFactory factory = new DiskFileItemFactory(16, null); |
|
172 |
// 通过工厂对象创建 item 对象 |
|
173 |
item = factory.createItem(imgName + ".jpg", ContentType.IMAGE_JPEG.toString(), false, imgName + ".jpg"); |
|
174 |
OutputStream os = item.getOutputStream(); |
|
175 |
int bytesRead = 0; |
|
176 |
byte[] buffer = new byte[8192]; |
|
177 |
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) { |
|
178 |
os.write(buffer, 0, bytesRead); |
|
179 |
} |
|
180 |
os.close(); |
|
181 |
is.close(); |
|
182 |
} |
|
183 |
} catch (IOException e) { |
|
184 |
throw new RuntimeException("文件下载失败", e); |
|
185 |
} |
|
186 |
|
|
187 |
return new CommonsMultipartFile(item); |
|
188 |
} |
|
189 |
} |