| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- package cn.com.goldenwater.dcproj.service.impl.general;
- import cn.com.goldenwater.core.service.AbstractCrudService;
- import cn.com.goldenwater.dcproj.constValue.SplitValue;
- import cn.com.goldenwater.dcproj.dao.BisInspPblmDao;
- import cn.com.goldenwater.dcproj.dao.GwComFileDao;
- import cn.com.goldenwater.dcproj.model.BisInspPblm;
- import cn.com.goldenwater.dcproj.model.GwComFile;
- import cn.com.goldenwater.dcproj.param.GwComFileParam;
- import cn.com.goldenwater.dcproj.service.GwComFileService;
- import cn.com.goldenwater.dcproj.utils.Builder;
- import cn.com.goldenwater.dcproj.utils.HttpWSDLUtils;
- import cn.com.goldenwater.dcproj.utils.InspUtils;
- import cn.com.goldenwater.target.CheckException;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import net.coobird.thumbnailator.Thumbnails;
- import org.apache.commons.collections.MapUtils;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpServletResponse;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- /**
- * @author zhaohg
- * @date 2019-2-18
- */
- @Service
- @Transactional
- public class GwComFileServiceImpl extends AbstractCrudService<GwComFile, GwComFileParam> implements GwComFileService {
- @Autowired
- private GwComFileDao gwComFileDao;
- @Value("${notFileAllow}")
- private String notFileAllow;
- @Autowired
- private BisInspPblmDao bisInspPblmDao;
- private Logger logger = LoggerFactory.getLogger(getClass());
- public GwComFileServiceImpl(GwComFileDao gwComFileDao) {
- super(gwComFileDao);
- this.gwComFileDao = gwComFileDao;
- }
- @Value("${web.upload-path}")
- public String fileDir;
- @Value("${small.upload-path}")
- public String smallFileDir;
- @Override
- public void updatePblmVedio(String pblmId) {
- BisInspPblm bisInspPblm = Builder.of(BisInspPblm::new)
- .with(BisInspPblm::setPblmId, pblmId)
- .with(BisInspPblm::setHasVedio, "0")
- .build();
- List<GwComFile> gwComFileList = gwComFileDao.findFileByBiz(pblmId);
- if (gwComFileList != null && gwComFileList.size() > 0) {
- bisInspPblm.setHasVedio("1");
- }
- bisInspPblmDao.update(bisInspPblm);
- }
- @Override
- public int countByPblmId(String pblmId) {
- return this.gwComFileDao.countByPblmId(pblmId);
- }
- @Override
- public boolean writeFile(GwComFile gwComFile, MultipartFile file, String filePath) {
- boolean succcess = false;
- String smallFilePath = smallFileDir + filePath;
- filePath = fileDir + filePath;
- File pathFile = new File(filePath);
- try {
- if (!pathFile.exists()) {
- pathFile.mkdirs();
- }
- File newFile = null;
- if (gwComFile.getFileExt().length() > 12) {
- gwComFile.setFileExt("png");
- newFile = new File(pathFile, gwComFile.getId() + ".png");
- FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
- } else {
- newFile = new File(pathFile, gwComFile.getId() + "." + gwComFile.getFileExt());
- FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
- }
- /**
- * 缩略图begin
- */
- String fileExt = gwComFile.getFileExt();
- if (StringUtils.isBlank(fileExt)) {
- String fileName = file.getName();
- fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
- }
- fileExt = fileExt.toUpperCase();
- checkFileMethod(fileExt, newFile);
- if ("JPEG".equals(fileExt) || "JPG".equals(fileExt) || "PNG".equals(fileExt)) {
- if (!InspUtils.isImage(newFile)) {
- //如果不是图片则删除
- newFile.delete();
- throw new CheckException("当前上传文件不符合规范,请重新上传!");
- }
- File smallPathFile = new File(smallFilePath);
- if (!smallPathFile.exists()) {
- smallPathFile.mkdirs();
- }
- String fileName = pathFile + File.separator + gwComFile.getId() + "." + gwComFile.getFileExt();
- //拼接后台文件名称
- String thumbnailPathName = smallFilePath + File.separator + gwComFile.getId() + "." + gwComFile.getFileExt();
- thumbnailPathName = thumbnailPathName.replace(gwComFile.getFileExt(), "jpg");
- long size = file.getSize();
- double scale = 1.0d;
- if (size >= 300 * 1024) {
- if (size > 0) {
- scale = (300 * 1024f) / size;
- }
- if (scale < 0.25) {
- scale = 0.25;
- }
- }
- if (size < 300 * 1024) {
- Thumbnails.of(fileName).scale(1f).outputFormat("jpg").toFile(thumbnailPathName);
- } else {
- Thumbnails.of(fileName).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(thumbnailPathName);
- }
- }
- succcess = true;
- } catch (IOException e) {
- e.printStackTrace();
- logger.error(e.getMessage());
- }
- return succcess;
- }
- private void checkFileMethod(String fileExt, File newFile) {
- if (StringUtils.isNotBlank(notFileAllow)) {
- String[] notArrays = notFileAllow.split(SplitValue.FENHAO_SPLIT);
- for (String str : notArrays) {
- if (str.contains(fileExt)) {
- newFile.delete();
- throw new CheckException("文件异常,请仔细核实上传文件类型!");
- }
- }
- }
- }
- @Override
- public boolean writeFile(String id, String ext, MultipartFile file, String filePath) {
- boolean succcess = false;
- File pathFile = new File(filePath);
- try {
- if (!pathFile.exists()) {
- pathFile.mkdirs();
- }
- File newFile = new File(pathFile, id + "." + ext);
- FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
- checkFileMethod(ext, newFile);
- succcess = true;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return succcess;
- }
- @Override
- public List<GwComFile> findFileByBiz(String bizId) {
- return this.gwComFileDao.findFileByBiz(bizId);
- }
- @Override
- public void updateBiz(List<GwComFile> gwComFiles, String bizId) {
- List<String> idList = new ArrayList<>();
- if (gwComFiles != null && !gwComFiles.isEmpty()) {
- int i = 1;
- for (GwComFile comFile : gwComFiles) {
- if (comFile != null) {
- if (StringUtils.isNotBlank(comFile.getId())) {
- comFile.setSn(i++);
- comFile.setBizId(bizId);
- gwComFileDao.update(comFile);
- idList.add(comFile.getId());
- }
- }
- }
- }
- List<GwComFile> comFiles = gwComFileDao.findFileByBiz(bizId);
- if (comFiles != null && !comFiles.isEmpty()) {
- for (GwComFile comFile : comFiles) {
- if (!idList.contains(comFile.getId())) {
- gwComFileDao.delete(comFile.getId());
- }
- }
- }
- }
- @Override
- public void updateTopById(String bizId) {
- GwComFile gwComFile = gwComFileDao.get(bizId);
- Optional.ofNullable(gwComFile).ifPresent(u -> {
- // 判断是否为置顶
- if (null != u.getTopValue() && u.getTopValue() == 1) {
- // 设为不置顶。 top=0, topDate=null(这是为了排序用的)
- gwComFile.setTopValue(0);
- gwComFile.setTopDate(null);
- gwComFileDao.updateTop(gwComFile);
- } else {
- // 设为置顶。 top=1
- gwComFile.setTopValue(1);
- gwComFile.setTopDate(new Date());
- gwComFileDao.updateTop(gwComFile);
- }
- });
- }
- @Override
- public PageInfo<GwComFile> findFilePageByBiz(GwComFileParam comFileParam) {
- PageHelper.startPage(comFileParam);
- // PageHelper.orderBy("");
- List<GwComFile> list = gwComFileDao.listByCon(comFileParam.getFileName(), comFileParam.getBizId(), comFileParam.getBizType());
- PageInfo<GwComFile> pageInfo = new PageInfo(list);
- return pageInfo;
- }
- @Override
- public PageInfo<GwComFile> findImgPage(GwComFileParam comFileParam) {
- PageHelper.startPage(comFileParam);
- List<GwComFile> gwComFileList = gwComFileDao.findImgPage(comFileParam);
- return new PageInfo<>(gwComFileList);
- }
- @Override
- public void changeSmallImg(String year, String mnth) {
- String path = fileDir + year + File.separator + mnth;
- String smallPath = smallFileDir + year + File.separator + mnth;
- // 获取路径下的所有文件
- File file = new File(path);
- File[] files = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- File fl = files[i];
- // 如果还是文件夹 递归获取里面的文件 文件夹
- if (files[i].isDirectory()) {
- File children = files[i];
- String day = children.getName();
- String childrenSmallPath = smallPath + File.separator + day;
- //smallPath = smallPath + File.separator + day;
- File[] childrenFiles = children.listFiles();
- for (File wenjian : childrenFiles) {
- String wenjianPath = wenjian.getPath();
- String fileName = wenjian.getName();
- String fileExt = wenjianPath.substring(wenjianPath.lastIndexOf(".") + 1);
- fileName = fileName.replace(fileExt, "jpg");
- if (StringUtils.isNotBlank(fileExt)) {
- fileExt = fileExt.toUpperCase();
- }
- checkFileMethod(fileExt, wenjian);
- if ("JPEG".equals(fileExt) || "JPG".equals(fileExt) || "PNG".equals(fileExt)) {
- try {
- File smallPathFile = new File(childrenSmallPath);
- if (!smallPathFile.exists()) {
- smallPathFile.mkdirs();
- }
- //拼接后台文件名称
- String thumbnailPathName = childrenSmallPath + File.separator + fileName;
- long size = wenjian.length();
- double scale = 1.0d;
- if (size >= 300 * 1024) {
- if (size > 0) {
- scale = (300 * 1024f) / size;
- }
- if (scale < 0.25) {
- scale = 0.25;
- }
- }
- if (size < 300 * 1024) {
- Thumbnails.of(wenjianPath).scale(1f).outputFormat("jpg").toFile(thumbnailPathName);
- } else {
- Thumbnails.of(wenjianPath).scale(1f).outputQuality(scale).outputFormat("jpg").toFile(thumbnailPathName);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- @Override
- public int downFile(GwComFile gwComFile, HttpServletResponse response) {
- String filePath = fileDir + gwComFile.getFilePath();
- FileInputStream fis = null;
- BufferedInputStream bufferedIntputStream = null;
- try {
- File file = new File(filePath);
- if (!file.exists()) {
- return 0;
- }
- fis = new FileInputStream(file);
- // 设置response参数,可以打开下载页面
- response.reset();
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
- response.setHeader("Content-Disposition", "attachment;filename=" + new String((gwComFile.getFileName()).getBytes(), "iso-8859-1"));
- response.setContentLength(bufferedIntputStream.available());
- OutputStream out = response.getOutputStream();
- bufferedIntputStream = new BufferedInputStream(fis);
- int i = 0;
- while ((i = bufferedIntputStream.read()) != -1) {
- out.write(bufferedIntputStream.read());
- }
- out.flush();
- } catch (Exception ex) {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (bufferedIntputStream != null) {
- try {
- bufferedIntputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return 1;
- }
- @Override
- public List<GwComFile> findLongFileExt() {
- return gwComFileDao.findLongFileExt();
- }
- @Override
- public List<GwComFile> getImgByBizId(String bizId, int n) {
- return gwComFileDao.getImgByBizId(bizId, n);
- }
- @Override
- public List<GwComFile> getTopNFace(String n, String adCode, String pType, String currentAdCode) {
- if (adCode.equals("43")) {
- return gwComFileDao.getTopHnNFace(n, adCode, pType, currentAdCode);
- } else {
- return gwComFileDao.getTopNFace(n, adCode, pType, currentAdCode);
- }
- }
- @Override
- public List<GwComFile> getTopNPblm(String n, String adCode, String pType, String currentAdCode) {
- return gwComFileDao.getTopNPblm(n, adCode, pType, currentAdCode);
- }
- @Override
- public List<GwComFile> getTopNPblmVido(String n, String adCode, String pType, String currentAdCode) {
- return gwComFileDao.getTopNPblmVido(n, adCode, pType, currentAdCode);
- }
- @Override
- public List<GwComFile> getTopTacPblm(String n, String province) {
- return gwComFileDao.getTopTacPlam(n, province);
- }
- @Override
- public List<GwComFile> getTopTacPblmVideo(String n, String province) {
- return gwComFileDao.getTopTacPlamVideo(n, province);
- }
- @Override
- public List<GwComFile> findAllFile(GwComFileParam gwComFileParam) {
- List<GwComFile> list = gwComFileDao.listByCon(gwComFileParam.getFileName(), gwComFileParam.getBizId(), gwComFileParam.getBizType());
- return list;
- }
- @Override
- public void getpicInfo() throws IOException {
- //获取所有的工地标准化项目,获取项目下的所有图片,拷贝到新的目录
- List<Map<String, Object>> prjctList = gwComFileDao.getPrjectInfo();
- for (int i = 0; i < prjctList.size(); i++) {
- //登记表ID
- String id = MapUtils.getString(prjctList.get(i), "ID");
- String nm = MapUtils.getString(prjctList.get(i), "NM");
- //获取登记表id下的所有附件
- List<GwComFile> list = gwComFileDao.findFileByRgId(id);
- //附件迁移到新文件夹
- for (GwComFile item : list) {
- //获取文件路径
- fileDir = fileDir.replace("upload/", "");
- File source = new File(fileDir + File.separator + item.getFilePath());
- logger.info("------------------------------------source:" + source + "-------------------------------------------");
- File dest = new File(fileDir + "/fujian/" + nm + File.separator + item.getFileName());
- logger.info("------------------------------------dest:" + dest + "-------------------------------------------");
- if (!dest.getParentFile().exists()) {
- dest.getParentFile().mkdirs();
- }
- if (source.exists()) {
- FileUtils.copyFile(source, dest);
- }
- }
- }
- }
- @Override
- public String toWsdlPushData(GwComFileParam gwComFileParam){
- // 一共有多少条数
- int total = gwComFileDao.selectCountByYear(gwComFileParam);
- logger.info(gwComFileParam.getCreateDateStr()+"共有"+total+"条");
- System.out.println(gwComFileParam.getCreateDateStr()+"共有"+total+"条。");
- String result = "";
- if(total>0){
- int pageSize = 100;
- int pageIndexTotal = total/pageSize + 1 ;
- gwComFileParam.setPageSize(pageSize);
- int startIndex = 1;
- if(1 != gwComFileParam.getPageNum()){
- startIndex = gwComFileParam.getPageNum() ;
- }
- for(int index = startIndex; index < pageIndexTotal; index++){
- gwComFileParam.setPageNum(index);
- logger.info(gwComFileParam.getCreateDateStr()+"分页查询 :index="+index+";pageSize="+pageSize+" 。");
- System.out.println(gwComFileParam.getCreateDateStr()+"分页查询 :index="+index+";pageSize="+pageSize+" 。");
- PageHelper.startPage(gwComFileParam);
- // 查询要推送的数据
- List<GwComFile> comFileList = gwComFileDao.selectListByYear(gwComFileParam);
- int realSize = comFileList.size();
- // 获取身份令牌
- String guid = HttpWSDLUtils.getUserToken();
- logger.info("GwComFileServiceImpl 454 toWsdlPushData "+index+";pageSize="+realSize);
- System.out.println("GwComFileServiceImpl 455 toWsdlPushData "+index+";pageSize="+realSize);
- if("1".equals(gwComFileParam.getIsDelete())){
- // 先删除
- String xmlDeleteStr = HttpWSDLUtils.getDeleteXmlStr(comFileList);
- String deleteResult = HttpWSDLUtils.pushData(guid,xmlDeleteStr );
- logger.info("GwComFileServiceImpl 460 toWsdlPushData delete result "+deleteResult);
- System.out.println("GwComFileServiceImpl 461 toWsdlPushData delete result "+deleteResult);
- }
- String xmlAddStr = HttpWSDLUtils.getAddXmlStr(comFileList);
- String pushResult = HttpWSDLUtils.pushData(guid,xmlAddStr );
- logger.info("GwComFileServiceImpl 465 toWsdlPushData pushData result "+pushResult);
- System.out.println("GwComFileServiceImpl 466 toWsdlPushData pushData result "+pushResult);
- if("推送成功".equals(pushResult)){
- result = result + " index="+ index + "推送成功" ;
- }else {
- result = result + " index="+ index + "推送失败" ;
- }
- }
- }
- return result;
- }
- }
|