|  |  | 
 |  |  | package com.doumee.service.business.impl; | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.model.LoginUserInfo; | 
 |  |  | import com.google.common.collect.Lists; | 
 |  |  |  | 
 |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
 |  |  | import com.doumee.core.constants.Constants; | 
 |  |  | import com.doumee.core.model.PageData; | 
 |  |  | import com.doumee.core.model.PageWrap; | 
 |  |  | import com.doumee.core.utils.Utils; | 
 |  |  | import com.doumee.core.wx.WxMiniUtilService; | 
 |  |  | import com.doumee.dao.business.LocksMapper; | 
 |  |  | import com.doumee.dao.business.join.LocksJoinMapper; | 
 |  |  | import com.doumee.dao.business.model.BaseParam; | 
 |  |  | import com.doumee.dao.business.model.Bikes; | 
 |  |  | import com.doumee.dao.business.model.Locks; | 
 |  |  | import com.doumee.dao.business.model.Sites; | 
 |  |  | import com.doumee.dao.system.model.SystemDictData; | 
 |  |  | import com.doumee.service.business.LocksService; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | import com.doumee.service.business.SitesService; | 
 |  |  | import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
 |  |  | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; | 
 |  |  | import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.apache.shiro.SecurityUtils; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.util.CollectionUtils; | 
 |  |  |  | 
 |  |  | import javax.servlet.ServletOutputStream; | 
 |  |  | import javax.servlet.http.HttpServletResponse; | 
 |  |  | import java.io.File; | 
 |  |  | import java.io.FileInputStream; | 
 |  |  | import java.io.FileOutputStream; | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.net.URLEncoder; | 
 |  |  | import java.nio.charset.Charset; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 锁头信息表Service实现 | 
 |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private LocksMapper locksMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private LocksJoinMapper locksJoinMapper; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private WxMiniUtilService wxMiniUtilService; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SystemDictDataBiz systemDictDataBiz; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public String create(Locks locks) { | 
 |  |  | 
 |  |  |         QueryWrapper<Locks> wrapper = new QueryWrapper<>(locks); | 
 |  |  |         return locksMapper.selectList(wrapper); | 
 |  |  |     } | 
 |  |  |    | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<Locks> findLockBase64List(Locks locks) { | 
 |  |  |         LoginUserInfo loginUserInfo =  (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
 |  |  |         String fullPath = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode() + | 
 |  |  |                 systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode() ; | 
 |  |  |  | 
 |  |  |         QueryWrapper<Locks> wrapper = new QueryWrapper<>(locks); | 
 |  |  |         wrapper.lambda().orderByAsc(Locks::getCode); | 
 |  |  |         List<Locks> locksList = locksMapper.selectList(wrapper); | 
 |  |  |         SystemDictData systemDictData = systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.ACCESS_TOKEN); | 
 |  |  |         String code = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.PROJECTS).getCode(); | 
 |  |  |         String prePath = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.IMG_DIR).getCode(); | 
 |  |  |         if (!CollectionUtils.isEmpty(locksList)){ | 
 |  |  |             // 检查是否有图片信息没有更新保存图片 | 
 |  |  |             locksList.forEach(s-> { | 
 |  |  |                 if (StringUtils.isBlank(s.getInfo())){ | 
 |  |  |                     wxMiniUtilService.generateWXMiniCode(s,systemDictData,prePath,code); | 
 |  |  |                     s.setEditor(loginUserInfo.getId()); | 
 |  |  |                     s.setEditDate(new Date()); | 
 |  |  |                     updateById(s); | 
 |  |  |                 } | 
 |  |  |                 s.setImgfullurl(fullPath+s.getInfo()); | 
 |  |  |             }); | 
 |  |  |             return locksList; | 
 |  |  |         } | 
 |  |  |         return new ArrayList<>(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public PageData<Locks> findPage(PageWrap<Locks> pageWrap) { | 
 |  |  |         IPage<Locks> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
 |  |  |         QueryWrapper<Locks> queryWrapper = new QueryWrapper<>(); | 
 |  |  |         MPJLambdaWrapper<Locks> queryWrapper = new MPJLambdaWrapper<>(); | 
 |  |  |         Utils.MP.blankToNull(pageWrap.getModel()); | 
 |  |  |         if (pageWrap.getModel().getId() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getId, pageWrap.getModel().getId()); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getCreateDate() != null) { | 
 |  |  |             queryWrapper.lambda().ge(Locks::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate())); | 
 |  |  |             queryWrapper.lambda().le(Locks::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate())); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getCreator() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getCreator, pageWrap.getModel().getCreator()); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getEditDate() != null) { | 
 |  |  |             queryWrapper.lambda().ge(Locks::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate())); | 
 |  |  |             queryWrapper.lambda().le(Locks::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate())); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getEditor() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getEditor, pageWrap.getModel().getEditor()); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getIsdeleted() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getIsdeleted, pageWrap.getModel().getIsdeleted()); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getInfo() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getInfo, pageWrap.getModel().getInfo()); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getName() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getName, pageWrap.getModel().getName()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if (pageWrap.getModel().getCode() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getCode, pageWrap.getModel().getCode()); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getStatus() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getStatus, pageWrap.getModel().getStatus()); | 
 |  |  |             queryWrapper.like(Locks::getCode, pageWrap.getModel().getCode()); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getSiteId() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getSiteId, pageWrap.getModel().getSiteId()); | 
 |  |  |             queryWrapper.like(Locks::getSiteId, pageWrap.getModel().getSiteId()); | 
 |  |  |         } | 
 |  |  |         if (pageWrap.getModel().getBikeCode() != null) { | 
 |  |  |             queryWrapper.lambda().eq(Locks::getBikeCode, pageWrap.getModel().getBikeCode()); | 
 |  |  |         if (pageWrap.getModel().getSiteName() != null) { | 
 |  |  |             queryWrapper.like(Sites::getName, pageWrap.getModel().getSiteName()); | 
 |  |  |         } | 
 |  |  |         for(PageWrap.SortData sortData: pageWrap.getSorts()) { | 
 |  |  |             if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { | 
 |  |  |                 queryWrapper.orderByDesc(sortData.getProperty()); | 
 |  |  |             } else { | 
 |  |  |                 queryWrapper.orderByAsc(sortData.getProperty()); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return PageData.from(locksMapper.selectPage(page, queryWrapper)); | 
 |  |  |         queryWrapper.leftJoin(Bikes.class,Bikes::getCode,Locks::getBikeCode) | 
 |  |  |                     .leftJoin(BaseParam.class,BaseParam::getId,Bikes::getParamId) | 
 |  |  |                     .leftJoin(Sites.class,Sites::getCode,Locks::getSiteId); | 
 |  |  |         queryWrapper.orderByDesc(Locks::getBikeCode); | 
 |  |  |         queryWrapper.selectAll(Locks.class) | 
 |  |  |                     .selectAs(Sites::getName,Locks::getSiteName) | 
 |  |  |                     .selectAs(BaseParam::getName,Locks::getBikeType); | 
 |  |  |         return PageData.from(locksJoinMapper.selectJoinPage(page, Locks.class,queryWrapper)); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  | 
 |  |  |         QueryWrapper<Locks> wrapper = new QueryWrapper<>(locks); | 
 |  |  |         return locksMapper.selectCount(wrapper); | 
 |  |  |     } | 
 |  |  |     public static void packFilesToZip(List<File> files,    ServletOutputStream os) throws IOException { | 
 |  |  |         try (ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(os)) { | 
 |  |  |             for (File file : files) { | 
 |  |  |                 ZipArchiveEntry entry = new ZipArchiveEntry(file.getName()); | 
 |  |  |                 zipOutputStream.putArchiveEntry(entry); | 
 |  |  |                 try (FileInputStream fileInputStream = new FileInputStream(file)) { | 
 |  |  |                     byte[] buffer = new byte[1024]; | 
 |  |  |                     int length; | 
 |  |  |                     while ((length = fileInputStream.read(buffer)) > 0) { | 
 |  |  |                         zipOutputStream.write(buffer, 0, length); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 zipOutputStream.closeArchiveEntry(); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     @Override | 
 |  |  |     public void  exportImages(String siteId, HttpServletResponse response) { | 
 |  |  |         try { | 
 |  |  |             List<File> fileList = new ArrayList<>(); | 
 |  |  |             List<Locks> locks = locksJoinMapper.selectList(new QueryWrapper<Locks>().lambda().eq(Locks::getSiteId,siteId).isNotNull(Locks::getInfo)); | 
 |  |  |             if(locks== null || locks.size() == 0){ | 
 |  |  |                 throw  new BusinessException(ResponseStatus.DATA_EMPTY); | 
 |  |  |             } | 
 |  |  |             String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.IMG_DIR).getCode()+systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode(); | 
 |  |  |             for(Locks l : locks){ | 
 |  |  |                 if(StringUtils.isNotBlank(l.getInfo())){ | 
 |  |  |                     File file = new File(path + l.getInfo()); | 
 |  |  |                     if(file!=null && file.isFile()){ | 
 |  |  |                         fileList.add(file); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             if(fileList == null || fileList.size() == 0){ | 
 |  |  |                 throw  new BusinessException(ResponseStatus.DATA_EMPTY); | 
 |  |  |             } | 
 |  |  |             String fileName =  "站点【"+siteId+"】小程序码批量导出_"+System.currentTimeMillis()+".zip" ; | 
 |  |  |             String encodeFileName = URLEncoder.encode(fileName); | 
 |  |  |             response.setHeader("Content-Disposition","attachment;filename=" + encodeFileName); | 
 |  |  |             response.setContentType("application/octet-stream"); | 
 |  |  |             response.setHeader("eva-opera-type", "download"); | 
 |  |  |             response.setHeader("eva-download-filename", encodeFileName); | 
 |  |  |             packFilesToZip(fileList,response.getOutputStream()); | 
 |  |  |         } catch (IOException e) { | 
 |  |  |             throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |