ZbGalleryServiceImpl.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.ruoyi.system.service.impl;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.ruoyi.common.config.RuoYiConfig;
  6. import com.ruoyi.common.enums.CommentType;
  7. import com.ruoyi.common.exception.BaseException;
  8. import com.ruoyi.common.utils.DateUtils;
  9. import com.ruoyi.common.utils.SecurityUtils;
  10. import com.ruoyi.common.utils.bean.BeanUtils;
  11. import com.ruoyi.system.domain.ZbFile;
  12. import com.ruoyi.system.domain.grallery.ZbGallery;
  13. import com.ruoyi.system.domain.grallery.ZbGalleryCategory;
  14. import com.ruoyi.system.domain.grallery.ZbGalleryImg;
  15. import com.ruoyi.system.dto.GalleryCategoryDto;
  16. import com.ruoyi.system.dto.GalleryDto;
  17. import com.ruoyi.system.dto.GalleryImgDto;
  18. import com.ruoyi.system.mapper.ZbGalleryMapper;
  19. import com.ruoyi.system.service.IZbCommentService;
  20. import com.ruoyi.system.service.IZbGalleryCategoryService;
  21. import com.ruoyi.system.service.IZbGalleryImgService;
  22. import com.ruoyi.system.service.IZbGalleryService;
  23. import lombok.RequiredArgsConstructor;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.util.*;
  27. import java.util.stream.Collectors;
  28. /**
  29. * 【请填写功能名称】Service业务层处理
  30. *
  31. * @author ruoyi
  32. * @date 2020-11-20
  33. */
  34. @Service
  35. @RequiredArgsConstructor
  36. public class ZbGalleryServiceImpl extends ServiceImpl<ZbGalleryMapper, ZbGallery> implements IZbGalleryService {
  37. private final IZbGalleryImgService galleryImgService;
  38. private final RuoYiConfig ruoYiConfig;
  39. private final IZbCommentService commentService;
  40. private final IZbGalleryCategoryService galleryCategoryService;
  41. private final ZbFileServiceImpl zbFileService;
  42. /**
  43. * 查询【请填写功能名称】列表
  44. *
  45. * @param zbGallery 【请填写功能名称】
  46. * @return 【请填写功能名称】
  47. */
  48. @Override
  49. public List<GalleryDto> selectZbGalleryList(ZbGallery zbGallery, Page<ZbGallery> page) {
  50. return baseMapper.selectZbGalleryList(zbGallery, page);
  51. }
  52. /**
  53. * 新增【请填写功能名称】
  54. *
  55. * @param zbGallery 【请填写功能名称】
  56. * @return 结果
  57. */
  58. @Override
  59. @Transactional(rollbackFor = Exception.class)
  60. public boolean insertZbGallery(GalleryDto zbGallery) {
  61. ZbGallery gallery = new ZbGallery();
  62. BeanUtils.copyBeanProp(gallery, zbGallery);
  63. Date nowDate = DateUtils.getNowDate();
  64. gallery.setCreateTime(nowDate);
  65. String createBy = SecurityUtils.getUserId().toString();
  66. gallery.setCreateBy(createBy);
  67. int insert = baseMapper.insert(gallery);
  68. if (insert <= 0) {
  69. throw new BaseException("图库保存失败");
  70. }
  71. //判断是否新增图库时,新增了图片
  72. if (zbGallery.getImgInfos().size() > 0) {
  73. List<ZbGalleryImg> imgList = zbGallery.getImgInfos().stream().filter(e -> e.getId() != null).map(x -> {
  74. ZbGalleryImg galleryImg = new ZbGalleryImg();
  75. galleryImg.setAuthor(x.getAuthor());
  76. galleryImg.setLocation(x.getLocation());
  77. galleryImg.setGallertId(gallery.getId());
  78. galleryImg.setQrCodeId(x.getQrCode());
  79. galleryImg.setThumbId(x.getId());
  80. galleryImg.setWatermarkType(x.getWatermarkType());
  81. galleryImg.setWatermarkColor(x.getWatermarkColor());
  82. galleryImg.setCreateBy(createBy);
  83. galleryImg.setCreateTime(nowDate);
  84. galleryImg.setAuthorConcat(x.getConcat());
  85. galleryImg.setRemark(x.getRemark());
  86. return galleryImg;
  87. }).collect(Collectors.toList());
  88. if (imgList.size() > 0) {
  89. galleryImgService.saveBatch(imgList);
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * 修改【请填写功能名称】
  96. *
  97. * @param zbGallery 【请填写功能名称】
  98. * @return 结果
  99. */
  100. @Override
  101. @Transactional(rollbackFor = Exception.class)
  102. public boolean updateZbGallery(GalleryDto zbGallery) {
  103. ZbGallery gallery = new ZbGallery();
  104. BeanUtils.copyBeanProp(gallery, zbGallery);
  105. gallery.setCategoryId(zbGallery.getCategoryId());
  106. Date nowDate = DateUtils.getNowDate();
  107. gallery.setCreateTime(nowDate);
  108. String createBy = SecurityUtils.getUserId().toString();
  109. gallery.setCreateBy(createBy);
  110. int insert = baseMapper.updateById(gallery);
  111. if (insert <= 0) {
  112. throw new BaseException("图库保存失败");
  113. }
  114. Long galleryId = gallery.getId();
  115. //对应图片信息删除
  116. galleryImgService.removeByGalleryId(galleryId);
  117. //判断是否修改图库时,是否包含图片
  118. if (zbGallery.getImgInfos().size() > 0) {
  119. List<ZbGalleryImg> imgList = zbGallery.getImgInfos().stream().filter(e -> e.getId() != null).map(x -> {
  120. ZbGalleryImg galleryImg = new ZbGalleryImg();
  121. galleryImg.setAuthor(x.getAuthor());
  122. galleryImg.setLocation(x.getLocation());
  123. galleryImg.setGallertId(galleryId);
  124. galleryImg.setQrCodeId(x.getQrCode());
  125. galleryImg.setThumbId(x.getId());
  126. galleryImg.setWatermarkColor(x.getWatermarkColor());
  127. galleryImg.setWatermarkType(x.getWatermarkType());
  128. galleryImg.setCreateBy(createBy);
  129. galleryImg.setCreateTime(nowDate);
  130. galleryImg.setAuthorConcat(x.getConcat());
  131. galleryImg.setRemark(x.getRemark());
  132. return galleryImg;
  133. }).collect(Collectors.toList());
  134. if (imgList.size() > 0) {
  135. galleryImgService.saveBatch(imgList);
  136. }
  137. }
  138. return true;
  139. }
  140. @Override
  141. public GalleryDto getByGalleryId(Long id) {
  142. ZbGallery gallery = getById(id);
  143. if (gallery == null) {
  144. throw new BaseException("图库信息不存在");
  145. }
  146. GalleryDto galleryDto = new GalleryDto();
  147. BeanUtils.copyBeanProp(galleryDto, gallery);
  148. galleryDto.setId(gallery.getId());
  149. galleryDto.setSort(gallery.getSort());
  150. List<GalleryImgDto> galleryImg = galleryImgService.getByGalleryId(id);
  151. List<GalleryDto.ImgInfosDTO> infosDTOList = galleryImg.stream().map(x -> {
  152. GalleryDto.ImgInfosDTO imgInfosDTO = new GalleryDto.ImgInfosDTO();
  153. imgInfosDTO.setAuthor(x.getAuthor());
  154. imgInfosDTO.setId(x.getFileId());
  155. imgInfosDTO.setLocation(x.getLocation());
  156. imgInfosDTO.setQrCode(x.getQrCode());
  157. imgInfosDTO.setRemark(x.getRemark());
  158. imgInfosDTO.setConcat(x.getConcat());
  159. if (StrUtil.isNotBlank(x.getQrCodeUrl())) {
  160. imgInfosDTO.setQrCodeUrl(ruoYiConfig.getUrl() + x.getQrCodeUrl());
  161. }
  162. imgInfosDTO.setUrl(ruoYiConfig.getUrl() + x.getUrl());
  163. imgInfosDTO.setWatermarkType(x.getWatermarkType());
  164. imgInfosDTO.setWatermarkColor(x.getWatermarkColor());
  165. return imgInfosDTO;
  166. }).collect(Collectors.toList());
  167. galleryDto.setImgInfos(infosDTOList);
  168. return galleryDto;
  169. }
  170. /**
  171. * 删除图库
  172. * @param gallerys
  173. * @return
  174. */
  175. @Override
  176. @Transactional(rollbackFor = Exception.class)
  177. public boolean delGallery(List<Long> gallerys) {
  178. //删除图库表
  179. this.removeByIds(gallerys);
  180. //先删除附件表信息和服务器存储数据
  181. List<ZbGalleryImg> zbGalleryImgs = galleryImgService.selectByGalleryIds(gallerys);
  182. for (ZbGalleryImg zbGalleryImg : zbGalleryImgs) {
  183. ZbFile zbFile = new ZbFile();
  184. zbFile.setId(zbGalleryImg.getThumbId());
  185. zbFileService.deletePicture(zbFile);
  186. }
  187. //删除图库图片信息表
  188. galleryImgService.removeByGalleryIds(gallerys);
  189. //删除评论信息
  190. commentService.removeByTypeAndFk(CommentType.GALLERY, gallerys);
  191. return true;
  192. }
  193. @Override
  194. public Page<GalleryDto> selectGalleryDto(String categoryCode, Long categoryId, Page<GalleryDto> pageInfo) {
  195. List<Long> categorys = new ArrayList<>();
  196. if (StrUtil.isNotBlank(categoryCode) && categoryId == null) {
  197. ZbGalleryCategory category = galleryCategoryService.getByCode(categoryCode);
  198. if (category != null) {
  199. categoryId = category.getId();
  200. }
  201. }
  202. if (categoryId != null) {
  203. categorys = StrUtil.split(galleryCategoryService.getChildrens(categoryId), ',')
  204. .stream().map(Long::valueOf).collect(Collectors.toList());
  205. }
  206. String profile = ruoYiConfig.getUrl();
  207. /* List<GalleryDto> galleryDtos = baseMapper.selectGalleryDto(categorys, pageInfo)
  208. .stream().peek(
  209. x -> x.getImgInfos().forEach(i -> i.setUrl(profile + i.getUrl()))
  210. ).collect(Collectors.toList());*/
  211. //获取预览地址
  212. List<GalleryDto> galleryDtos = baseMapper.selectGalleryDtoThumbnail(categorys, pageInfo)
  213. .stream().peek(
  214. x -> x.getImgInfos().forEach(i -> i.setUrl(profile + i.getUrl()))
  215. ).collect(Collectors.toList());
  216. pageInfo.setRecords(galleryDtos);
  217. return pageInfo;
  218. }
  219. @Override
  220. public Integer checkCategoryHasValue(Long categoryId) {
  221. return baseMapper.checkCategoryHasValue(categoryId);
  222. }
  223. }