3cda6d3680cbe3eac6945aee52f1133e85197bda.svn-base 19 KB

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