5720e123c86304bb7c08aa70360ecb299b5f9561.svn-base 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. package cn.com.goldenwater.dcproj.service.impl.general;
  2. import cn.com.goldenwater.core.service.AbstractCrudService;
  3. import cn.com.goldenwater.dcproj.constValue.SplitValue;
  4. import cn.com.goldenwater.dcproj.dao.BisInspPblmDao;
  5. import cn.com.goldenwater.dcproj.dao.GwComFileDao;
  6. import cn.com.goldenwater.dcproj.model.BisInspPblm;
  7. import cn.com.goldenwater.dcproj.model.GwComFile;
  8. import cn.com.goldenwater.dcproj.param.GwComFileParam;
  9. import cn.com.goldenwater.dcproj.service.GwComFileService;
  10. import cn.com.goldenwater.dcproj.utils.Builder;
  11. import cn.com.goldenwater.dcproj.utils.HttpWSDLUtils;
  12. import cn.com.goldenwater.dcproj.utils.InspUtils;
  13. import cn.com.goldenwater.target.CheckException;
  14. import com.github.pagehelper.PageHelper;
  15. import com.github.pagehelper.PageInfo;
  16. import net.coobird.thumbnailator.Thumbnails;
  17. import org.apache.commons.collections.MapUtils;
  18. import org.apache.commons.io.FileUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import org.springframework.web.multipart.MultipartFile;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.*;
  29. import java.util.*;
  30. /**
  31. * @author zhaohg
  32. * @date 2019-2-18
  33. */
  34. @Service
  35. @Transactional
  36. public class GwComFileServiceImpl extends AbstractCrudService<GwComFile, GwComFileParam> implements GwComFileService {
  37. @Autowired
  38. private GwComFileDao gwComFileDao;
  39. @Value("${notFileAllow}")
  40. private String notFileAllow;
  41. @Autowired
  42. private BisInspPblmDao bisInspPblmDao;
  43. private Logger logger = LoggerFactory.getLogger(getClass());
  44. public GwComFileServiceImpl(GwComFileDao gwComFileDao) {
  45. super(gwComFileDao);
  46. this.gwComFileDao = gwComFileDao;
  47. }
  48. @Value("${web.upload-path}")
  49. public String fileDir;
  50. @Value("${small.upload-path}")
  51. public String smallFileDir;
  52. @Override
  53. public void updatePblmVedio(String pblmId) {
  54. BisInspPblm bisInspPblm = Builder.of(BisInspPblm::new)
  55. .with(BisInspPblm::setPblmId, pblmId)
  56. .with(BisInspPblm::setHasVedio, "0")
  57. .build();
  58. List<GwComFile> gwComFileList = gwComFileDao.findFileByBiz(pblmId);
  59. if (gwComFileList != null && gwComFileList.size() > 0) {
  60. bisInspPblm.setHasVedio("1");
  61. }
  62. bisInspPblmDao.update(bisInspPblm);
  63. }
  64. @Override
  65. public int countByPblmId(String pblmId) {
  66. return this.gwComFileDao.countByPblmId(pblmId);
  67. }
  68. @Override
  69. public boolean writeFile(GwComFile gwComFile, MultipartFile file, String filePath) {
  70. boolean succcess = false;
  71. String smallFilePath = smallFileDir + filePath;
  72. filePath = fileDir + filePath;
  73. File pathFile = new File(filePath);
  74. try {
  75. if (!pathFile.exists()) {
  76. pathFile.mkdirs();
  77. }
  78. File newFile = null;
  79. if (gwComFile.getFileExt().length() > 12) {
  80. gwComFile.setFileExt("png");
  81. newFile = new File(pathFile, gwComFile.getId() + ".png");
  82. FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
  83. } else {
  84. newFile = new File(pathFile, gwComFile.getId() + "." + gwComFile.getFileExt());
  85. FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
  86. }
  87. /**
  88. * 缩略图begin
  89. */
  90. String fileExt = gwComFile.getFileExt();
  91. if (StringUtils.isBlank(fileExt)) {
  92. String fileName = file.getName();
  93. fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
  94. }
  95. fileExt = fileExt.toUpperCase();
  96. checkFileMethod(fileExt, newFile);
  97. if ("JPEG".equals(fileExt) || "JPG".equals(fileExt) || "PNG".equals(fileExt)) {
  98. if (!InspUtils.isImage(newFile)) {
  99. //如果不是图片则删除
  100. newFile.delete();
  101. throw new CheckException("当前上传文件不符合规范,请重新上传!");
  102. }
  103. File smallPathFile = new File(smallFilePath);
  104. if (!smallPathFile.exists()) {
  105. smallPathFile.mkdirs();
  106. }
  107. String fileName = pathFile + File.separator + gwComFile.getId() + "." + gwComFile.getFileExt();
  108. //拼接后台文件名称
  109. String thumbnailPathName = smallFilePath + File.separator + gwComFile.getId() + "." + gwComFile.getFileExt();
  110. thumbnailPathName = thumbnailPathName.replace(gwComFile.getFileExt(), "jpg");
  111. long size = file.getSize();
  112. double scale = 1.0d;
  113. if (size >= 300 * 1024) {
  114. if (size > 0) {
  115. scale = (300 * 1024f) / size;
  116. }
  117. if (scale < 0.25) {
  118. scale = 0.25;
  119. }
  120. }
  121. if (size < 300 * 1024) {
  122. Thumbnails.of(fileName).scale(1f).outputFormat("jpg").toFile(thumbnailPathName);
  123. } else {
  124. Thumbnails.of(fileName).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(thumbnailPathName);
  125. }
  126. }
  127. succcess = true;
  128. } catch (IOException e) {
  129. e.printStackTrace();
  130. logger.error(e.getMessage());
  131. }
  132. return succcess;
  133. }
  134. private void checkFileMethod(String fileExt, File newFile) {
  135. if (StringUtils.isNotBlank(notFileAllow)) {
  136. String[] notArrays = notFileAllow.split(SplitValue.FENHAO_SPLIT);
  137. for (String str : notArrays) {
  138. if (str.contains(fileExt)) {
  139. newFile.delete();
  140. throw new CheckException("文件异常,请仔细核实上传文件类型!");
  141. }
  142. }
  143. }
  144. }
  145. @Override
  146. public boolean writeFile(String id, String ext, MultipartFile file, String filePath) {
  147. boolean succcess = false;
  148. File pathFile = new File(filePath);
  149. try {
  150. if (!pathFile.exists()) {
  151. pathFile.mkdirs();
  152. }
  153. File newFile = new File(pathFile, id + "." + ext);
  154. FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
  155. checkFileMethod(ext, newFile);
  156. succcess = true;
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. }
  160. return succcess;
  161. }
  162. @Override
  163. public List<GwComFile> findFileByBiz(String bizId) {
  164. return this.gwComFileDao.findFileByBiz(bizId);
  165. }
  166. @Override
  167. public void updateBiz(List<GwComFile> gwComFiles, String bizId) {
  168. List<String> idList = new ArrayList<>();
  169. if (gwComFiles != null && !gwComFiles.isEmpty()) {
  170. int i = 1;
  171. for (GwComFile comFile : gwComFiles) {
  172. if (comFile != null) {
  173. if (StringUtils.isNotBlank(comFile.getId())) {
  174. comFile.setSn(i++);
  175. comFile.setBizId(bizId);
  176. gwComFileDao.update(comFile);
  177. idList.add(comFile.getId());
  178. }
  179. }
  180. }
  181. }
  182. List<GwComFile> comFiles = gwComFileDao.findFileByBiz(bizId);
  183. if (comFiles != null && !comFiles.isEmpty()) {
  184. for (GwComFile comFile : comFiles) {
  185. if (!idList.contains(comFile.getId())) {
  186. gwComFileDao.delete(comFile.getId());
  187. }
  188. }
  189. }
  190. }
  191. @Override
  192. public void updateTopById(String bizId) {
  193. GwComFile gwComFile = gwComFileDao.get(bizId);
  194. Optional.ofNullable(gwComFile).ifPresent(u -> {
  195. // 判断是否为置顶
  196. if (null != u.getTopValue() && u.getTopValue() == 1) {
  197. // 设为不置顶。 top=0, topDate=null(这是为了排序用的)
  198. gwComFile.setTopValue(0);
  199. gwComFile.setTopDate(null);
  200. gwComFileDao.updateTop(gwComFile);
  201. } else {
  202. // 设为置顶。 top=1
  203. gwComFile.setTopValue(1);
  204. gwComFile.setTopDate(new Date());
  205. gwComFileDao.updateTop(gwComFile);
  206. }
  207. });
  208. }
  209. @Override
  210. public PageInfo<GwComFile> findFilePageByBiz(GwComFileParam comFileParam) {
  211. PageHelper.startPage(comFileParam);
  212. // PageHelper.orderBy("");
  213. List<GwComFile> list = gwComFileDao.listByCon(comFileParam.getFileName(), comFileParam.getBizId(), comFileParam.getBizType(), comFileParam.getFileIds());
  214. PageInfo<GwComFile> pageInfo = new PageInfo(list);
  215. return pageInfo;
  216. }
  217. @Override
  218. public PageInfo<GwComFile> findImgPage(GwComFileParam comFileParam) {
  219. PageHelper.startPage(comFileParam);
  220. List<GwComFile> gwComFileList = gwComFileDao.findImgPage(comFileParam);
  221. return new PageInfo<>(gwComFileList);
  222. }
  223. @Override
  224. public void changeSmallImg(String year, String mnth) {
  225. String path = fileDir + year + File.separator + mnth;
  226. String smallPath = smallFileDir + year + File.separator + mnth;
  227. // 获取路径下的所有文件
  228. File file = new File(path);
  229. File[] files = file.listFiles();
  230. for (int i = 0; i < files.length; i++) {
  231. File fl = files[i];
  232. // 如果还是文件夹 递归获取里面的文件 文件夹
  233. if (files[i].isDirectory()) {
  234. File children = files[i];
  235. String day = children.getName();
  236. String childrenSmallPath = smallPath + File.separator + day;
  237. //smallPath = smallPath + File.separator + day;
  238. File[] childrenFiles = children.listFiles();
  239. for (File wenjian : childrenFiles) {
  240. String wenjianPath = wenjian.getPath();
  241. String fileName = wenjian.getName();
  242. String fileExt = wenjianPath.substring(wenjianPath.lastIndexOf(".") + 1);
  243. fileName = fileName.replace(fileExt, "jpg");
  244. if (StringUtils.isNotBlank(fileExt)) {
  245. fileExt = fileExt.toUpperCase();
  246. }
  247. checkFileMethod(fileExt, wenjian);
  248. if ("JPEG".equals(fileExt) || "JPG".equals(fileExt) || "PNG".equals(fileExt)) {
  249. try {
  250. File smallPathFile = new File(childrenSmallPath);
  251. if (!smallPathFile.exists()) {
  252. smallPathFile.mkdirs();
  253. }
  254. //拼接后台文件名称
  255. String thumbnailPathName = childrenSmallPath + File.separator + fileName;
  256. long size = wenjian.length();
  257. double scale = 1.0d;
  258. if (size >= 300 * 1024) {
  259. if (size > 0) {
  260. scale = (300 * 1024f) / size;
  261. }
  262. if (scale < 0.25) {
  263. scale = 0.25;
  264. }
  265. }
  266. if (size < 300 * 1024) {
  267. Thumbnails.of(wenjianPath).scale(1f).outputFormat("jpg").toFile(thumbnailPathName);
  268. } else {
  269. Thumbnails.of(wenjianPath).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(thumbnailPathName);
  270. }
  271. } catch (Exception e) {
  272. e.printStackTrace();
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. @Override
  280. public int downFile(GwComFile gwComFile, HttpServletResponse response) {
  281. String filePath = fileDir + gwComFile.getFilePath().replaceFirst("/upload", "");
  282. FileInputStream fis = null;
  283. BufferedInputStream bufferedIntputStream = null;
  284. try {
  285. File file = new File(filePath);
  286. if (!file.exists()) {
  287. return 0;
  288. }
  289. fis = new FileInputStream(file);
  290. // 设置response参数,可以打开下载页面
  291. response.reset();
  292. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
  293. response.setHeader("Content-Disposition", "attachment;filename=" + new String((gwComFile.getFileName()).getBytes(), "iso-8859-1"));
  294. response.setContentLength(bufferedIntputStream.available());
  295. OutputStream out = response.getOutputStream();
  296. bufferedIntputStream = new BufferedInputStream(fis);
  297. int i = 0;
  298. while ((i = bufferedIntputStream.read()) != -1) {
  299. out.write(bufferedIntputStream.read());
  300. }
  301. out.flush();
  302. } catch (Exception ex) {
  303. if (fis != null) {
  304. try {
  305. fis.close();
  306. } catch (IOException e) {
  307. e.printStackTrace();
  308. }
  309. }
  310. if (bufferedIntputStream != null) {
  311. try {
  312. bufferedIntputStream.close();
  313. } catch (IOException e) {
  314. e.printStackTrace();
  315. }
  316. }
  317. }
  318. return 1;
  319. }
  320. @Override
  321. public String getFilePath(String filePath) {
  322. if (StringUtils.isBlank(filePath)) {
  323. return null;
  324. }
  325. return fileDir + filePath.replaceFirst("/upload", "");
  326. }
  327. @Override
  328. public List<GwComFile> findLongFileExt() {
  329. return gwComFileDao.findLongFileExt();
  330. }
  331. @Override
  332. public List<GwComFile> getImgByBizId(String bizId, int n) {
  333. return gwComFileDao.getImgByBizId(bizId, n);
  334. }
  335. @Override
  336. public List<GwComFile> getTopNFace(String n, String adCode, String pType, String currentAdCode) {
  337. if (adCode.equals("43")) {
  338. return gwComFileDao.getTopHnNFace(n, adCode, pType, currentAdCode);
  339. } else {
  340. return gwComFileDao.getTopNFace(n, adCode, pType, currentAdCode);
  341. }
  342. }
  343. @Override
  344. public List<GwComFile> getTopNPblm(String n, String adCode, String pType, String currentAdCode) {
  345. return gwComFileDao.getTopNPblm(n, adCode, pType, currentAdCode);
  346. }
  347. @Override
  348. public List<GwComFile> getTopNPblmVido(String n, String adCode, String pType, String currentAdCode) {
  349. return gwComFileDao.getTopNPblmVido(n, adCode, pType, currentAdCode);
  350. }
  351. @Override
  352. public List<GwComFile> getTopTacPblm(String n, String province) {
  353. return gwComFileDao.getTopTacPlam(n, province);
  354. }
  355. @Override
  356. public List<GwComFile> getTopTacPblmVideo(String n, String province) {
  357. return gwComFileDao.getTopTacPlamVideo(n, province);
  358. }
  359. @Override
  360. public List<GwComFile> findAllFile(GwComFileParam gwComFileParam) {
  361. List<GwComFile> list = gwComFileDao.listByCon(gwComFileParam.getFileName(), gwComFileParam.getBizId(), gwComFileParam.getBizType(), gwComFileParam.getFileIds());
  362. return list;
  363. }
  364. @Override
  365. public void getpicInfo() throws IOException {
  366. //获取所有的工地标准化项目,获取项目下的所有图片,拷贝到新的目录
  367. List<Map<String, Object>> prjctList = gwComFileDao.getPrjectInfo();
  368. for (int i = 0; i < prjctList.size(); i++) {
  369. //登记表ID
  370. String id = MapUtils.getString(prjctList.get(i), "ID");
  371. String nm = MapUtils.getString(prjctList.get(i), "NM");
  372. //获取登记表id下的所有附件
  373. List<GwComFile> list = gwComFileDao.findFileByRgId(id);
  374. //附件迁移到新文件夹
  375. for (GwComFile item : list) {
  376. //获取文件路径
  377. fileDir = fileDir.replace("upload/", "");
  378. File source = new File(fileDir + File.separator + item.getFilePath());
  379. logger.info("------------------------------------source:" + source + "-------------------------------------------");
  380. File dest = new File(fileDir + "/fujian/" + nm + File.separator + item.getFileName());
  381. logger.info("------------------------------------dest:" + dest + "-------------------------------------------");
  382. if (!dest.getParentFile().exists()) {
  383. dest.getParentFile().mkdirs();
  384. }
  385. if (source.exists()) {
  386. FileUtils.copyFile(source, dest);
  387. }
  388. }
  389. }
  390. }
  391. @Override
  392. public String toWsdlPushData(GwComFileParam gwComFileParam) {
  393. // 一共有多少条数
  394. int total = gwComFileDao.selectCountByYear(gwComFileParam);
  395. logger.info(gwComFileParam.getCreateDateStr() + "共有" + total + "条");
  396. System.out.println(gwComFileParam.getCreateDateStr() + "共有" + total + "条。");
  397. String result = "";
  398. if (total > 0) {
  399. int pageSize = 100;
  400. int pageIndexTotal = total / pageSize + 1;
  401. gwComFileParam.setPageSize(pageSize);
  402. int startIndex = 1;
  403. if (1 != gwComFileParam.getPageNum()) {
  404. startIndex = gwComFileParam.getPageNum();
  405. }
  406. for (int index = startIndex; index < pageIndexTotal; index++) {
  407. gwComFileParam.setPageNum(index);
  408. logger.info(gwComFileParam.getCreateDateStr() + "分页查询 :index=" + index + ";pageSize=" + pageSize + " 。");
  409. System.out.println(gwComFileParam.getCreateDateStr() + "分页查询 :index=" + index + ";pageSize=" + pageSize + " 。");
  410. PageHelper.startPage(gwComFileParam);
  411. // 查询要推送的数据
  412. List<GwComFile> comFileList = gwComFileDao.selectListByYear(gwComFileParam);
  413. int realSize = comFileList.size();
  414. // 获取身份令牌
  415. String guid = HttpWSDLUtils.getUserToken();
  416. logger.info("GwComFileServiceImpl 454 toWsdlPushData " + index + ";pageSize=" + realSize);
  417. System.out.println("GwComFileServiceImpl 455 toWsdlPushData " + index + ";pageSize=" + realSize);
  418. if ("1".equals(gwComFileParam.getIsDelete())) {
  419. // 先删除
  420. String xmlDeleteStr = HttpWSDLUtils.getDeleteXmlStr(comFileList);
  421. String deleteResult = HttpWSDLUtils.pushData(guid, xmlDeleteStr);
  422. logger.info("GwComFileServiceImpl 460 toWsdlPushData delete result " + deleteResult);
  423. System.out.println("GwComFileServiceImpl 461 toWsdlPushData delete result " + deleteResult);
  424. }
  425. String xmlAddStr = HttpWSDLUtils.getAddXmlStr(comFileList);
  426. String pushResult = HttpWSDLUtils.pushData(guid, xmlAddStr);
  427. logger.info("GwComFileServiceImpl 465 toWsdlPushData pushData result " + pushResult);
  428. System.out.println("GwComFileServiceImpl 466 toWsdlPushData pushData result " + pushResult);
  429. if ("推送成功".equals(pushResult)) {
  430. result = result + " index=" + index + "推送成功";
  431. } else {
  432. result = result + " index=" + index + "推送失败";
  433. }
  434. }
  435. }
  436. return result;
  437. }
  438. }