package com.jkcredit.sysnews.service.newsArticle; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.jkcredit.sysnews.enums.PhotoStatusEnum; import com.jkcredit.sysnews.enums.PhotoTypeEnum; import com.jkcredit.sysnews.mapper.NewsArticleMapper; import com.jkcredit.sysnews.model.dto.newsArticle.NewsArticleDto; import com.jkcredit.sysnews.model.dto.photo.PhotoDto; import com.jkcredit.sysnews.model.po.newsArticle.NewsArticlePo; import com.jkcredit.sysnews.model.po.photo.PhotoPo; import com.jkcredit.sysnews.model.vo.newsArticle.NewsArticleVo; import com.jkcredit.sysnews.model.vo.photo.PhotoVo; import com.jkcredit.sysnews.service.base.BaseService; import com.jkcredit.sysnews.service.photo.PhotoService; import com.jkcredit.sysnews.spi.lang.constant.CommonConstant; import com.jkcredit.sysnews.spi.lang.exception.ServiceException; import com.jkcredit.sysnews.util.AssertUtils; import com.jkcredit.sysnews.util.BeanUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.enums.EnumUtils; import org.springframework.beans.BeanUtils; 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 javax.annotation.Resource; import java.util.Date; /** * @description: * @author: xusonglin * @create: 2020/3/1 22:46 * @version: V1.0 **/ @Slf4j @Service @Transactional(readOnly = true) public class NewsArticleServiceImpl extends BaseService implements NewsArticleService { @Value("${photo.relativePath}") private String relativePath; @Resource NewsArticleMapper mapper; @Autowired PhotoService photoService; @Override public IPage getNewsArticles(Page page, NewsArticleDto newsArticleDto) { IPage newsArticlePoIPage = mapper.getNewsArticles(page, newsArticleDto); return newsArticlePoIPage.convert(this::convert); } private NewsArticleVo convert(NewsArticlePo po) { NewsArticleVo vo = new NewsArticleVo(); BeanUtils.copyProperties(po, vo); return vo; } @Override public NewsArticleVo getNewsArticleById(Long id) { NewsArticlePo newsArticlePo = mapper.getNewsArticleById(id); NewsArticleVo newsArticleVo = new NewsArticleVo(); BeanUtil.copyProperties(newsArticleVo, newsArticlePo); return newsArticleVo; } @Override @Transactional(rollbackFor = ServiceException.class) public void saveNewsArticle(NewsArticleDto newsArticleDto) { validate(newsArticleDto); PhotoDto photoDto = new PhotoDto(); photoDto.setName(newsArticleDto.getTitle()); photoDto.setType(PhotoTypeEnum.NEWS_PHOTO.getValue()); photoDto.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_HAVE_USED.getValue()); // 更新新闻content中的图片 try { for (String url : newsArticleDto.getUrlList()) { PhotoPo photoPo = photoService.getPhotoByUrl(url); photoDto.setId(photoPo.getId()); photoService.editPhoto(photoDto); } } catch (ServiceException e) { log.error("新增新闻-更新图片失败,失败原因:{}", e.getMessage()); throw new ServiceException("新增新闻失败"); } // 保存新闻首图 PhotoVo photoVo; try { photoDto.setPhoto(newsArticleDto.getPhoto()); photoDto.setId(null); photoVo = photoService.savePhoto(photoDto); } catch (ServiceException e) { log.error("新增新闻-新增首图失败,失败原因:{}", e.getMessage()); throw new ServiceException("新增新闻失败"); } // 保存新闻 NewsArticlePo newsArticlePo = new NewsArticlePo(); BeanUtil.copyProperties(newsArticlePo, newsArticleDto); newsArticlePo.setCreateTime(new Date()); newsArticlePo.setPhotoId(photoVo.getId()); try { mapper.insert(newsArticlePo); } catch (Exception e) { log.error("新增新闻失败,失败原因:{}", e.getMessage()); throw new ServiceException("新增新闻失败"); } } @Override @Transactional(rollbackFor = ServiceException.class) public void editNewsArticle(NewsArticleDto newsArticleDto) { validate(newsArticleDto); NewsArticlePo newsArticlePo = mapper.getNewsArticleById(newsArticleDto.getId()); AssertUtils.assertNotNull(newsArticleDto, CommonConstant.ARTICLE_NOT_EXIST); try { if (!StringUtils.isBlank(newsArticleDto.getTitle())) { newsArticlePo.setTitle(newsArticleDto.getTitle()); } if (!StringUtils.isBlank(newsArticleDto.getNewsAbstract())) { newsArticlePo.setNewsAbstract(newsArticleDto.getNewsAbstract()); } if (!StringUtils.isBlank(newsArticleDto.getContent())) { newsArticlePo.setContent(newsArticleDto.getContent()); } if (newsArticleDto.getPhoto() != null) { try { } catch (ServiceException e) { } photoService.deletePhoto(newsArticlePo.getPhotoId()); newsArticlePo.setPhotoId(newsArticleDto.getPhotoId()); } mapper.updateById(newsArticlePo); } catch (Exception e) { log.error("编辑新闻失败,失败原因:{}", e.getMessage()); throw new ServiceException("编辑新闻失败"); } } @Override @Transactional(rollbackFor = ServiceException.class) public void deleteNewsArticle(Long id) { NewsArticlePo newsArticlePo = mapper.getNewsArticleById(id); newsArticlePo.setActivated(CommonConstant.ACTIVATED_DELETED); newsArticlePo.setUpdateTime(new Date()); try { mapper.updateById(newsArticlePo); } catch (Exception e) { log.error("删除新闻失败,失败原因:{}", e.getMessage()); throw new ServiceException("删除新闻失败"); } } }