package com.ruoyi.system.service.impl; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.enums.CommentType; import com.ruoyi.common.exception.BaseException; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.system.domain.ZbFile; import com.ruoyi.system.domain.grallery.ZbGallery; import com.ruoyi.system.domain.grallery.ZbGalleryCategory; import com.ruoyi.system.domain.grallery.ZbGalleryImg; import com.ruoyi.system.dto.GalleryCategoryDto; import com.ruoyi.system.dto.GalleryDto; import com.ruoyi.system.dto.GalleryImgDto; import com.ruoyi.system.mapper.ZbGalleryMapper; import com.ruoyi.system.service.IZbCommentService; import com.ruoyi.system.service.IZbGalleryCategoryService; import com.ruoyi.system.service.IZbGalleryImgService; import com.ruoyi.system.service.IZbGalleryService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; /** * 【请填写功能名称】Service业务层处理 * * @author ruoyi * @date 2020-11-20 */ @Service @RequiredArgsConstructor public class ZbGalleryServiceImpl extends ServiceImpl implements IZbGalleryService { private final IZbGalleryImgService galleryImgService; private final RuoYiConfig ruoYiConfig; private final IZbCommentService commentService; private final IZbGalleryCategoryService galleryCategoryService; private final ZbFileServiceImpl zbFileService; /** * 查询【请填写功能名称】列表 * * @param zbGallery 【请填写功能名称】 * @return 【请填写功能名称】 */ @Override public List selectZbGalleryList(ZbGallery zbGallery, Page page) { return baseMapper.selectZbGalleryList(zbGallery, page); } /** * 新增【请填写功能名称】 * * @param zbGallery 【请填写功能名称】 * @return 结果 */ @Override @Transactional(rollbackFor = Exception.class) public boolean insertZbGallery(GalleryDto zbGallery) { ZbGallery gallery = new ZbGallery(); BeanUtils.copyBeanProp(gallery, zbGallery); Date nowDate = DateUtils.getNowDate(); gallery.setCreateTime(nowDate); String createBy = SecurityUtils.getUserId().toString(); gallery.setCreateBy(createBy); int insert = baseMapper.insert(gallery); if (insert <= 0) { throw new BaseException("图库保存失败"); } //判断是否新增图库时,新增了图片 if (zbGallery.getImgInfos().size() > 0) { List imgList = zbGallery.getImgInfos().stream().filter(e -> e.getId() != null).map(x -> { ZbGalleryImg galleryImg = new ZbGalleryImg(); galleryImg.setAuthor(x.getAuthor()); galleryImg.setLocation(x.getLocation()); galleryImg.setGallertId(gallery.getId()); galleryImg.setQrCodeId(x.getQrCode()); galleryImg.setThumbId(x.getId()); galleryImg.setWatermarkType(x.getWatermarkType()); galleryImg.setWatermarkColor(x.getWatermarkColor()); galleryImg.setCreateBy(createBy); galleryImg.setCreateTime(nowDate); galleryImg.setAuthorConcat(x.getConcat()); galleryImg.setRemark(x.getRemark()); return galleryImg; }).collect(Collectors.toList()); if (imgList.size() > 0) { galleryImgService.saveBatch(imgList); } } return true; } /** * 修改【请填写功能名称】 * * @param zbGallery 【请填写功能名称】 * @return 结果 */ @Override @Transactional(rollbackFor = Exception.class) public boolean updateZbGallery(GalleryDto zbGallery) { ZbGallery gallery = new ZbGallery(); BeanUtils.copyBeanProp(gallery, zbGallery); gallery.setCategoryId(zbGallery.getCategoryId()); Date nowDate = DateUtils.getNowDate(); gallery.setCreateTime(nowDate); String createBy = SecurityUtils.getUserId().toString(); gallery.setCreateBy(createBy); int insert = baseMapper.updateById(gallery); if (insert <= 0) { throw new BaseException("图库保存失败"); } Long galleryId = gallery.getId(); //对应图片信息删除 galleryImgService.removeByGalleryId(galleryId); //判断是否修改图库时,是否包含图片 if (zbGallery.getImgInfos().size() > 0) { List imgList = zbGallery.getImgInfos().stream().filter(e -> e.getId() != null).map(x -> { ZbGalleryImg galleryImg = new ZbGalleryImg(); galleryImg.setAuthor(x.getAuthor()); galleryImg.setLocation(x.getLocation()); galleryImg.setGallertId(galleryId); galleryImg.setQrCodeId(x.getQrCode()); galleryImg.setThumbId(x.getId()); galleryImg.setWatermarkColor(x.getWatermarkColor()); galleryImg.setWatermarkType(x.getWatermarkType()); galleryImg.setCreateBy(createBy); galleryImg.setCreateTime(nowDate); galleryImg.setAuthorConcat(x.getConcat()); galleryImg.setRemark(x.getRemark()); return galleryImg; }).collect(Collectors.toList()); if (imgList.size() > 0) { galleryImgService.saveBatch(imgList); } } return true; } @Override public GalleryDto getByGalleryId(Long id) { ZbGallery gallery = getById(id); if (gallery == null) { throw new BaseException("图库信息不存在"); } GalleryDto galleryDto = new GalleryDto(); BeanUtils.copyBeanProp(galleryDto, gallery); galleryDto.setId(gallery.getId()); galleryDto.setSort(gallery.getSort()); List galleryImg = galleryImgService.getByGalleryId(id); List infosDTOList = galleryImg.stream().map(x -> { GalleryDto.ImgInfosDTO imgInfosDTO = new GalleryDto.ImgInfosDTO(); imgInfosDTO.setAuthor(x.getAuthor()); imgInfosDTO.setId(x.getFileId()); imgInfosDTO.setLocation(x.getLocation()); imgInfosDTO.setQrCode(x.getQrCode()); imgInfosDTO.setRemark(x.getRemark()); imgInfosDTO.setConcat(x.getConcat()); if (StrUtil.isNotBlank(x.getQrCodeUrl())) { imgInfosDTO.setQrCodeUrl(ruoYiConfig.getUrl() + x.getQrCodeUrl()); } imgInfosDTO.setUrl(ruoYiConfig.getUrl() + x.getUrl()); imgInfosDTO.setWatermarkType(x.getWatermarkType()); imgInfosDTO.setWatermarkColor(x.getWatermarkColor()); return imgInfosDTO; }).collect(Collectors.toList()); galleryDto.setImgInfos(infosDTOList); return galleryDto; } /** * 删除图库 * @param gallerys * @return */ @Override @Transactional(rollbackFor = Exception.class) public boolean delGallery(List gallerys) { //删除图库表 this.removeByIds(gallerys); //先删除附件表信息和服务器存储数据 List zbGalleryImgs = galleryImgService.selectByGalleryIds(gallerys); for (ZbGalleryImg zbGalleryImg : zbGalleryImgs) { ZbFile zbFile = new ZbFile(); zbFile.setId(zbGalleryImg.getThumbId()); zbFileService.deletePicture(zbFile); } //删除图库图片信息表 galleryImgService.removeByGalleryIds(gallerys); //删除评论信息 commentService.removeByTypeAndFk(CommentType.GALLERY, gallerys); return true; } @Override public Page selectGalleryDto(String categoryCode, Long categoryId, Page pageInfo) { List categorys = new ArrayList<>(); if (StrUtil.isNotBlank(categoryCode) && categoryId == null) { ZbGalleryCategory category = galleryCategoryService.getByCode(categoryCode); if (category != null) { categoryId = category.getId(); } } if (categoryId != null) { categorys = StrUtil.split(galleryCategoryService.getChildrens(categoryId), ',') .stream().map(Long::valueOf).collect(Collectors.toList()); } String profile = ruoYiConfig.getUrl(); /* List galleryDtos = baseMapper.selectGalleryDto(categorys, pageInfo) .stream().peek( x -> x.getImgInfos().forEach(i -> i.setUrl(profile + i.getUrl())) ).collect(Collectors.toList());*/ //获取预览地址 List galleryDtos = baseMapper.selectGalleryDtoThumbnail(categorys, pageInfo) .stream().peek( x -> x.getImgInfos().forEach(i -> i.setUrl(profile + i.getUrl())) ).collect(Collectors.toList()); pageInfo.setRecords(galleryDtos); return pageInfo; } @Override public Integer checkCategoryHasValue(Long categoryId) { return baseMapper.checkCategoryHasValue(categoryId); } }