|
@@ -0,0 +1,154 @@
|
|
|
|
+package com.jkcredit.sysnews.service.links;
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.jkcredit.sysnews.enums.PhotoTypeEnum;
|
|
|
|
+import com.jkcredit.sysnews.mapper.LinksMapper;
|
|
|
|
+import com.jkcredit.sysnews.model.dto.links.LinksDto;
|
|
|
|
+import com.jkcredit.sysnews.model.dto.photo.PhotoDto;
|
|
|
|
+import com.jkcredit.sysnews.model.po.links.LinksPo;
|
|
|
|
+import com.jkcredit.sysnews.model.po.newsArticle.NewsArticlePo;
|
|
|
|
+import com.jkcredit.sysnews.model.vo.links.LinksVo;
|
|
|
|
+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.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/11 15:42
|
|
|
|
+ * @version: V1.0
|
|
|
|
+ **/
|
|
|
|
+@Slf4j
|
|
|
|
+@Service
|
|
|
|
+@Transactional(readOnly = true)
|
|
|
|
+public class LinksServiceImpl extends BaseService implements LinksService {
|
|
|
|
+ @Resource
|
|
|
|
+ LinksMapper linksMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ PhotoService photoService;
|
|
|
|
+
|
|
|
|
+ @Value("${photo.accessPath}")
|
|
|
|
+ private String accessPath;
|
|
|
|
+ @Value("${photo.accessResource}")
|
|
|
|
+ private String accessResource;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = ServiceException.class)
|
|
|
|
+ public void saveLinks(LinksDto linksDto) {
|
|
|
|
+ validate(linksDto);
|
|
|
|
+ try {
|
|
|
|
+ PhotoDto photoDto = new PhotoDto();
|
|
|
|
+ photoDto.setPhoto(linksDto.getPhoto());
|
|
|
|
+ photoDto.setName(linksDto.getName());
|
|
|
|
+ photoDto.setType(PhotoTypeEnum.LINKS_PHOTO.getValue());
|
|
|
|
+ photoDto.setGoPageUrl(linksDto.getUrl());
|
|
|
|
+ PhotoVo photoVo = photoService.savePhoto(photoDto);
|
|
|
|
+
|
|
|
|
+ LinksPo linksPo = new LinksPo();
|
|
|
|
+ BeanUtil.copyProperties(linksPo, linksDto);
|
|
|
|
+ linksPo.setPhotoId(photoVo.getId());
|
|
|
|
+ linksPo.setCreateTime(new Date());
|
|
|
|
+ linksMapper.insert(linksPo);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("新增友情链接失败,失败原因:{}", e.getMessage());
|
|
|
|
+ throw new ServiceException("新增友情链接失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = ServiceException.class)
|
|
|
|
+ public void deleteLinks(Long id) {
|
|
|
|
+ AssertUtils.assertNotNull(id, "友情链接id不能为空");
|
|
|
|
+ try {
|
|
|
|
+ LinksPo linksPo = linksMapper.getLinkById(id);
|
|
|
|
+ linksPo.setActivated(CommonConstant.ACTIVATED_DELETED);
|
|
|
|
+ linksPo.setUpdateTime(new Date());
|
|
|
|
+ linksPo.setPhotoUrl(null);
|
|
|
|
+ linksMapper.updateById(linksPo);
|
|
|
|
+ photoService.deletePhoto(linksPo.getPhotoId());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("删除友情链接失败,失败原因:{}", e.getMessage());
|
|
|
|
+ throw new ServiceException("删除友情链接失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = ServiceException.class)
|
|
|
|
+ public void editLinks(LinksDto linksDto) {
|
|
|
|
+ validate(linksDto);
|
|
|
|
+ try {
|
|
|
|
+ LinksPo linksPo = linksMapper.getLinkById(linksDto.getId());
|
|
|
|
+ if (!StringUtils.isBlank(linksDto.getName())) {
|
|
|
|
+ linksPo.setName(linksDto.getName());
|
|
|
|
+ }
|
|
|
|
+ if (!StringUtils.isBlank(linksDto.getUrl())) {
|
|
|
|
+ linksPo.setUrl(linksDto.getUrl());
|
|
|
|
+ }
|
|
|
|
+ if (linksDto.getStatus() != null) {
|
|
|
|
+ linksPo.setStatus(linksDto.getStatus());
|
|
|
|
+ }
|
|
|
|
+ if (linksDto.getPhoto() != null) {
|
|
|
|
+ // 更换照片
|
|
|
|
+ PhotoDto photoDto = new PhotoDto();
|
|
|
|
+ photoDto.setPhoto(linksDto.getPhoto());
|
|
|
|
+ photoDto.setName(linksPo.getName());
|
|
|
|
+ photoDto.setType(PhotoTypeEnum.LINKS_PHOTO.getValue());
|
|
|
|
+ photoDto.setGoPageUrl(linksPo.getUrl());
|
|
|
|
+ PhotoVo photoVo = photoService.savePhoto(photoDto);
|
|
|
|
+ photoService.deletePhoto(linksPo.getPhotoId());
|
|
|
|
+ linksPo.setPhotoId(photoVo.getId());
|
|
|
|
+ }
|
|
|
|
+ linksPo.setUpdateTime(new Date());
|
|
|
|
+ linksPo.setPhotoUrl(null);
|
|
|
|
+ linksMapper.updateById(linksPo);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("编辑友情链接失败,失败原因:{}", e.getMessage());
|
|
|
|
+ throw new ServiceException("编辑友情链接失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public IPage<LinksVo> getLinksPage(Page page, LinksDto linksDto) {
|
|
|
|
+ IPage<LinksPo> linksPoIPage = linksMapper.getLinksPage(page, linksDto);
|
|
|
|
+
|
|
|
|
+ return linksPoIPage.convert(this::convert);
|
|
|
|
+ }
|
|
|
|
+ private LinksVo convert(LinksPo po) {
|
|
|
|
+ LinksVo vo = new LinksVo();
|
|
|
|
+ BeanUtils.copyProperties(po, vo);
|
|
|
|
+ if (po.getPhotoUrl() != null) {
|
|
|
|
+ String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
|
|
|
|
+ vo.setPhotoUrl(url);
|
|
|
|
+ }
|
|
|
|
+ return vo;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public LinksVo getLinkById(Long id) {
|
|
|
|
+ AssertUtils.assertNotNull(id, "友情链接id不能为空");
|
|
|
|
+ LinksPo linksPo = linksMapper.getLinkById(id);
|
|
|
|
+ LinksVo linksVo = new LinksVo();
|
|
|
|
+ BeanUtil.copyProperties(linksVo, linksPo);
|
|
|
|
+ if (linksPo.getPhotoUrl() != null) {
|
|
|
|
+ String url = accessPath + accessResource + linksPo.getPhotoUrl().replace("/","");
|
|
|
|
+ linksVo.setPhotoUrl(url);
|
|
|
|
+ }
|
|
|
|
+ return linksVo;
|
|
|
|
+ }
|
|
|
|
+}
|