Explorar o código

友情链接增删改查

xusonglin %!s(int64=5) %!d(string=hai) anos
pai
achega
0da25e1ebb

+ 19 - 0
src/main/java/com/jkcredit/sysnews/mapper/LinksMapper.java

@@ -0,0 +1,19 @@
+package com.jkcredit.sysnews.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.jkcredit.sysnews.model.dto.links.LinksDto;
+import com.jkcredit.sysnews.model.po.links.LinksPo;
+import org.apache.ibatis.annotations.Param;
+
+/**
+ * @description:
+ * @author: xusonglin
+ * @create: 2020/3/11 15:52
+ * @version: V1.0
+ **/
+public interface LinksMapper extends BaseMapper<LinksPo> {
+    IPage<LinksPo> getLinksPage(Page page, @Param("query")LinksDto linksDto);
+    LinksPo getLinkById(Long id);
+}

+ 25 - 0
src/main/java/com/jkcredit/sysnews/model/dto/links/LinksDto.java

@@ -0,0 +1,25 @@
+package com.jkcredit.sysnews.model.dto.links;
+
+import lombok.Data;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @description:
+ * @author: xusonglin
+ * @create: 2020/3/11 15:43
+ * @version: V1.0
+ **/
+@Data
+public class LinksDto implements Serializable {
+    private static final long serialVersionUID = 2592366934118024980L;
+
+    private Long id;
+    private String name;
+    private String url;
+    private Long photoId;
+    private Integer status;
+    private MultipartFile photo;
+}

+ 1 - 0
src/main/java/com/jkcredit/sysnews/model/po/links/LinksPo.java

@@ -30,4 +30,5 @@ public class LinksPo implements Serializable {
     private Date createTime;
     private Date updateTime;
     private Integer status;
+    private String photoUrl;
 }

+ 23 - 0
src/main/java/com/jkcredit/sysnews/model/vo/links/LinksVo.java

@@ -0,0 +1,23 @@
+package com.jkcredit.sysnews.model.vo.links;
+
+import lombok.Data;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.Serializable;
+
+/**
+ * @description:
+ * @author: xusonglin
+ * @create: 2020/3/11 15:46
+ * @version: V1.0
+ **/
+@Data
+public class LinksVo implements Serializable {
+    private static final long serialVersionUID = 1432840354150943105L;
+    private Long id;
+    private String name;
+    private String url;
+    private Long photoId;
+    private Integer status;
+    private String photoUrl;
+}

+ 102 - 0
src/main/java/com/jkcredit/sysnews/resource/web/links/LinksResource.java

@@ -0,0 +1,102 @@
+package com.jkcredit.sysnews.resource.web.links;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.jkcredit.sysnews.model.dto.links.LinksDto;
+import com.jkcredit.sysnews.model.vo.links.LinksVo;
+import com.jkcredit.sysnews.resource.base.BaseResource;
+import com.jkcredit.sysnews.resource.base.WebResource;
+import com.jkcredit.sysnews.resource.web.links.fb.LinksAddFB;
+import com.jkcredit.sysnews.resource.web.links.fb.LinksEditFB;
+import com.jkcredit.sysnews.resource.web.links.fb.LinksQueryFB;
+import com.jkcredit.sysnews.service.links.LinksService;
+import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
+import com.jkcredit.sysnews.spi.web.data.ResponseData;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @description:
+ * @author: xusonglin
+ * @create: 2020/3/11 16:30
+ * @version: V1.0
+ **/
+@Slf4j
+@RestController
+public class LinksResource extends WebResource {
+    @Autowired
+    LinksService linksService;
+
+    @PostMapping("/link")
+    @PreAuthorize("hasPermission('link','edit')")
+    @ApiOperation(value = "新增友情链接")
+    public ResponseData saveLinks(LinksAddFB linksAddFB) {
+        try {
+            validate(linksAddFB);
+            LinksDto linksDto = mapper.map(linksAddFB, LinksDto.class);
+            linksService.saveLinks(linksDto);
+            return ResponseData.success("新增友情链接成功");
+        } catch (ServiceException e) {
+            log.error(e.getMessage());
+            return ResponseData.failed("新增友情链接失败, 失败原因:" + e.getMessage());
+        }
+    }
+
+    @PutMapping("/link/{id}")
+    @PreAuthorize("hasPermission('link','edit')")
+    @ApiOperation(value = "删除友情链接")
+    public ResponseData deleteLinks(@PathVariable Long id) {
+        try {
+            linksService.deleteLinks(id);
+            return ResponseData.success("删除友情链接成功");
+        } catch (ServiceException e) {
+            log.error(e.getMessage());
+            return ResponseData.failed("删除友情链接失败, 失败原因:" + e.getMessage());
+        }
+    }
+
+    @PutMapping("/link")
+    @PreAuthorize("hasPermission('link','edit')")
+    @ApiOperation(value = "修改友情链接")
+    public ResponseData editLinks(LinksEditFB linksEditFB) {
+        try {
+            validate(linksEditFB);
+            LinksDto linksDto = mapper.map(linksEditFB, LinksDto.class);
+            linksService.editLinks(linksDto);
+            return ResponseData.success("修改友情链接成功");
+        } catch (ServiceException e) {
+            log.error(e.getMessage());
+            return ResponseData.failed("修改友情链接失败, 失败原因:" + e.getMessage());
+        }
+    }
+
+    @GetMapping("/page/links")
+    @PreAuthorize("hasPermission('link','read')")
+    @ApiOperation(value = "分页查询友情链接")
+    public ResponseData getLinks(Page page, LinksQueryFB linksQueryFB) {
+        try {
+            LinksDto linksDto = mapper.map(linksQueryFB, LinksDto.class);
+            IPage<LinksVo> linksVoIPage = linksService.getLinksPage(page, linksDto);
+            return ResponseData.success(linksVoIPage);
+        } catch (ServiceException e) {
+            log.error(e.getMessage());
+            return ResponseData.failed("分页查询友情链接失败, 失败原因:" + e.getMessage());
+        }
+    }
+
+    @GetMapping("/link/{id}")
+    @PreAuthorize("hasPermission('link','read')")
+    @ApiOperation(value = "根据id获取友情链接详情")
+    public ResponseData getLink(@PathVariable Long id) {
+        try {
+             LinksVo linksVo = linksService.getLinkById(id);
+             return ResponseData.success(linksVo);
+        } catch (ServiceException e) {
+            log.error(e.getMessage());
+            return ResponseData.failed("查询友情链接失败, 失败原因:" + e.getMessage());
+        }
+    }
+}

+ 26 - 0
src/main/java/com/jkcredit/sysnews/resource/web/links/fb/LinksAddFB.java

@@ -0,0 +1,26 @@
+package com.jkcredit.sysnews.resource.web.links.fb;
+
+import lombok.Data;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.NotNull;
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @description:
+ * @author: xusonglin
+ * @create: 2020/3/11 16:32
+ * @version: V1.0
+ **/
+@Data
+public class LinksAddFB implements Serializable {
+    private static final long serialVersionUID = -866407909895418980L;
+    @NotEmpty(message = "友情链接名称不能为空")
+    private String name;
+    @NotEmpty(message = "友情链接地址不能为空")
+    private String url;
+    @NotNull(message = "友情链接图片不能为空")
+    private MultipartFile photo;
+}

+ 25 - 0
src/main/java/com/jkcredit/sysnews/resource/web/links/fb/LinksEditFB.java

@@ -0,0 +1,25 @@
+package com.jkcredit.sysnews.resource.web.links.fb;
+
+import lombok.Data;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.validation.constraints.NotNull;
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @description:
+ * @author: xusonglin
+ * @create: 2020/3/11 16:34
+ * @version: V1.0
+ **/
+@Data
+public class LinksEditFB implements Serializable {
+    private static final long serialVersionUID = 2826853908668786886L;
+    @NotNull(message = "id不能为空")
+    private Long id;
+    private String name;
+    private String url;
+    private Integer status;
+    private MultipartFile photo;
+}

+ 18 - 0
src/main/java/com/jkcredit/sysnews/resource/web/links/fb/LinksQueryFB.java

@@ -0,0 +1,18 @@
+package com.jkcredit.sysnews.resource.web.links.fb;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @description:
+ * @author: xusonglin
+ * @create: 2020/3/11 16:34
+ * @version: V1.0
+ **/
+@Data
+public class LinksQueryFB implements Serializable {
+    private static final long serialVersionUID = 588972209223439395L;
+    private String name;
+    private Integer status;
+}

+ 24 - 0
src/main/java/com/jkcredit/sysnews/service/links/LinksService.java

@@ -0,0 +1,24 @@
+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.model.dto.links.LinksDto;
+import com.jkcredit.sysnews.model.vo.links.LinksVo;
+
+/**
+ * @description:
+ * @author: xusonglin
+ * @create: 2020/3/11 15:41
+ * @version: V1.0
+ **/
+public interface LinksService {
+    void saveLinks(LinksDto linksDto);
+
+    void deleteLinks(Long id);
+
+    void editLinks(LinksDto linksDto);
+
+    IPage<LinksVo> getLinksPage(Page page, LinksDto linksDto);
+
+    LinksVo getLinkById(Long id);
+}

+ 154 - 0
src/main/java/com/jkcredit/sysnews/service/links/LinksServiceImpl.java

@@ -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;
+    }
+}

+ 56 - 0
src/main/resources/mapper/LinsMapper.xml

@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.jkcredit.sysnews.mapper.LinksMapper">
+    <resultMap id="baseResultMap" type="com.jkcredit.sysnews.model.po.links.LinksPo">
+        <id column="id" property="id"/>
+        <result column="name" property="name"/>
+        <result column="url" property="url"/>
+        <result column="photo_id" property="photoId"/>
+        <result column="status" property="status"/>
+        <result column="create_time" property="createTime"/>
+        <result column="update_time" property="updateTime"/>
+        <result column="activated" property="activated"/>
+        <result column="photoUrl" property="photoUrl"/>
+    </resultMap>
+
+    <sql id="link">
+        id,
+        name,
+        url,
+        photo_id,
+        status,
+        create_time,
+        update_time,
+        activated
+    </sql>
+
+    <select id="getLinksPage" resultMap="baseResultMap">
+        select
+            links.*,
+            photo.url as photoUrl
+        from
+            links
+        left join photo on links.photo_id = photo.id
+        <where>
+            and links.activated = 1
+            <if test="query.name != null and query.name != ''">
+                and links.name like CONCAT('%',#{query.name},'%')
+            </if>
+            <if test="query.status != null and query.status != ''">
+                and links.status = #{query.status}
+            </if>
+        </where>
+    </select>
+
+    <select id="getLinkById" resultMap="baseResultMap">
+        select
+            links.*,
+            photo.url as photoUrl
+        from
+            links
+        left join photo on links.photo_id = photo.id
+        where
+            links.activated = 1
+            and links.id = #{id}
+    </select>
+</mapper>