Ver código fonte

作者:张哲
时间:2022/11/29
类型:开发
描述:新闻模块开发

ZizgZh 2 anos atrás
pai
commit
6094303d15

+ 16 - 1
src/main/java/com/redxun/knowledge/common/UserService.java

@@ -3,7 +3,9 @@ package com.redxun.knowledge.common;
 import com.redxun.common.model.JPaasUser;
 import com.redxun.common.utils.ExceptionUtil;
 import com.redxun.common.utils.SysUserUtil;
+import com.redxun.feign.org.UserClient;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.io.IOException;
@@ -21,8 +23,11 @@ import java.io.IOException;
 @Slf4j
 public class UserService {
 
+    @Autowired
+    private UserClient userClient;
+
     /**
-     * 获取当前系统登录用户
+     * 获取当前系统登录用户id
      * @return
      */
     public String queryLoginUser() {
@@ -39,4 +44,14 @@ public class UserService {
 
         return loginUserId;
     }
+
+    /**
+     * 获取当前系统登录用户姓名
+     * @return
+     */
+    public String queryLoginUserName() {
+        String userId = queryLoginUser();
+        String fullName = userClient.findByUserId(userId).getFullName();
+        return fullName;
+    }
 }

+ 8 - 18
src/main/java/com/redxun/knowledge/controller/NewsController.java

@@ -71,6 +71,14 @@ public class NewsController extends BaseController<News> {
         return jsonResult;
     }
 
+    @ApiOperation(value = "上升/下降")
+    @GetMapping("/goUpOrDownUp/{newsId}/{status}")
+    public JsonResult goUpOrDownUp(@PathVariable("newsId") String newsId,
+                                   @PathVariable("status") Boolean status){
+        newsService.goUpOrDownUp(newsId,status);
+        return JsonResult.Success("位置交换成功!");
+    }
+
     @ApiOperation(value = "新闻资讯(无分页,前台)")
     @GetMapping("getAllList")
     public JsonPageResult getAllList() {
@@ -129,23 +137,5 @@ public class NewsController extends BaseController<News> {
         return newsJsonResult;
     }
 
-    /**
-     * 获取登录员工信息
-     * @return
-     */
-    private String queryLoginUser() {
-        String loginUserId = null;
-        try {
-            JPaasUser jPaasUser = SysUserUtil.getLoginUser();
-            if (jPaasUser == null) {
-                return null;
-            }
-            loginUserId = jPaasUser.getUserId();
-        } catch (IOException e) {
-            log.error(ExceptionUtil.getExceptionMessage(e));
-        }
-        return loginUserId;
-    }
-
 }
 

+ 52 - 0
src/main/java/com/redxun/knowledge/controller/UserController.java

@@ -0,0 +1,52 @@
+package com.redxun.knowledge.controller;
+
+import com.redxun.common.annotation.ClassDefine;
+import com.redxun.common.base.entity.IUser;
+import com.redxun.common.base.entity.JsonResult;
+import com.redxun.common.utils.ContextUtil;
+import com.redxun.dto.user.OsUserDto;
+import com.redxun.feign.org.UserClient;
+import com.redxun.knowledge.common.UserService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 文件名: UserController
+ * 作者: Zizg
+ * 时间: 2022/11/29
+ * 描述:
+ * 修改人:
+ * 修改时间:
+ * 修改内容:
+ */
+@Slf4j
+@RestController
+@RequestMapping("/api-knowledge/knowledge/user")
+@Api(tags = "业务--用户信息")
+@ClassDefine(title = "业务--用户信息", alias = "UserController", path = "/api-knowledge/knowledge/user", packages = "", packageName = "子系统名称")
+public class UserController {
+
+    @Autowired
+    private UserClient userClient;
+
+    @Autowired
+    private UserService userService;
+
+    @GetMapping("findUserInfo")
+    public JsonResult findUserId(){
+        JsonResult jsonResult = JsonResult.getSuccessResult("获取成功");
+        String userId = userService.queryLoginUser();
+        String userName = userService.queryLoginUserName();
+        Map<String,Object> map = new HashMap<>();
+        map.put("userId",userId);
+        map.put("userName",userName);
+        return jsonResult;
+    }
+}

+ 3 - 5
src/main/java/com/redxun/knowledge/entity/dao/News.java

@@ -18,11 +18,6 @@ import com.redxun.common.base.entity.BaseExtEntity;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.experimental.Accessors;
-import oracle.sql.BLOB;
-
-import java.sql.Blob;
-import java.util.ArrayList;
-import java.util.List;
 
 
 @Setter
@@ -67,6 +62,9 @@ public class News  extends BaseExtEntity<java.lang.String> {
     @TableField(value = "IS_DEL")
     @TableLogic
     private Integer isDel;
+    //排序
+    @TableField(value = "SORT")
+    private Integer sort;
 
     @Override
     public String getPkId() {

+ 11 - 0
src/main/java/com/redxun/knowledge/mapper/NewsMapper.java

@@ -6,6 +6,7 @@ import org.apache.ibatis.annotations.Mapper;
 import com.redxun.common.base.db.BaseDao;
 import org.apache.ibatis.annotations.Param;
 
+import java.io.Serializable;
 import java.util.Map;
 
 /**
@@ -23,4 +24,14 @@ public interface NewsMapper extends BaseDao<News> {
     IPage findAllNews(IPage<News> page,@Param("params")Map<String, Object> params,@Param("w") Map<String, Object> order);
 
     IPage findAllPcNews(IPage page,@Param("params") Map<String,Object> params);
+
+    /**
+     * 新增/修改时候变更sort
+     * @param sort
+     */
+    void insertOrUpdateBySort(Integer sort);
+
+    void deleteNews(Serializable id);
+
+    void swapSort(String newsId, Integer sort);
 }

+ 98 - 0
src/main/java/com/redxun/knowledge/service/NewsServiceImpl.java

@@ -1,6 +1,7 @@
 
 package com.redxun.knowledge.service;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.redxun.common.base.db.BaseDao;
 import com.redxun.common.base.db.BaseService;
@@ -9,15 +10,23 @@ import com.redxun.common.base.entity.QueryData;
 import com.redxun.common.base.search.QueryFilter;
 import com.redxun.common.base.search.QueryFilterBuilder;
 import com.redxun.common.service.impl.SuperServiceImpl;
+import com.redxun.common.tool.BeanUtil;
+import com.redxun.common.tool.IdGenerator;
+import com.redxun.feign.org.UserClient;
+import com.redxun.knowledge.common.UserService;
+import com.redxun.knowledge.entity.dao.Banner;
 import com.redxun.knowledge.entity.dao.News;
 import com.redxun.knowledge.entity.vo.NewsAdminVo;
 import com.redxun.knowledge.entity.vo.NewsPcVo;
 import com.redxun.knowledge.mapper.NewsMapper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -34,6 +43,9 @@ public class NewsServiceImpl extends SuperServiceImpl<NewsMapper, News> implemen
     @Resource
     private NewsMapper newsMapper;
 
+    @Autowired
+    private UserService userService;
+
     @Override
     public BaseDao<News> getRepository() {
         return newsMapper;
@@ -58,6 +70,92 @@ public class NewsServiceImpl extends SuperServiceImpl<NewsMapper, News> implemen
         return page;
     }
 
+    /**
+     * 新增新闻
+     * @param entity
+     * @return
+     */
+    @Override
+    public int insert(News entity) {
+        if (BeanUtil.isEmpty(entity.getPkId())) {
+            entity.setPkId(IdGenerator.getIdStr());
+            // TODO: 2022/11/29  获取系统登录用户id(数据库中设置id和作者)
+            entity.setAuthorId(userService.queryLoginUser());
+            entity.setAuthor(userService.queryLoginUserName());
+            entity.setCreateBy(userService.queryLoginUser());
+            entity.setUpdateBy(userService.queryLoginUser());
+        }
+        if (entity.getSort() == null){
+            entity.setSort(0);
+        }
+        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("SORT",entity.getSort());
+        Integer integer = newsMapper.selectCount(queryWrapper);
+        if (integer > 0){
+            newsMapper.insertOrUpdateBySort(entity.getSort());
+        }
+        return this.getRepository().insert(entity);
+    }
+
+    /**
+     * 修改新闻
+     * @param entity
+     * @return
+     */
+    @Override
+    public int update(News entity) {
+        entity.setUpdateBy(userService.queryLoginUser());
+        if (entity.getSort() != null){
+            QueryWrapper<News> queryWrapper = new QueryWrapper<>();
+            queryWrapper.eq("SORT",entity.getSort());
+            Integer integer = newsMapper.selectCount(queryWrapper);
+            if (integer > 0){
+                newsMapper.insertOrUpdateBySort(entity.getSort());
+            }
+        }
+        return this.getRepository().updateById(entity);
+    }
+
+    @Override
+    public void delete(Serializable id) {
+        newsMapper.deleteNews(id);
+    }
+
+    /**
+     * 状态为true,上升;为false,下降
+     * @param newsId
+     * @param status
+     */
+    @Transactional(rollbackFor = Exception.class)
+    public void goUpOrDownUp(String newsId,Boolean status) {
+        News news = this.get(newsId);
+        Integer sort = news.getSort();
+        if (status){
+            // 上升
+            QueryWrapper<News> queryWrapper = new QueryWrapper<>();
+            queryWrapper.orderByAsc("SORT").orderByDesc("ENABLED");
+            List<News> newsList = newsMapper.selectList(queryWrapper);
+            List<News> collect = newsList.stream().filter(e -> e.getSort() < sort).collect(Collectors.toList());
+            News newsPre = collect.get(collect.size() - 1);
+            // 交换位置
+            int temp = sort;
+            newsMapper.swapSort(newsId,newsPre.getSort());
+            newsMapper.swapSort(newsPre.getPkId(),temp);
+        } else {
+            // 下降
+            QueryWrapper<News> queryWrapper = new QueryWrapper<>();
+            queryWrapper.orderByAsc("SORT").orderByDesc("ENABLED");
+            List<News> newsList = newsMapper.selectList(queryWrapper);
+            List<News> collect = newsList.stream().filter(e -> e.getSort() > sort).collect(Collectors.toList());
+            News newsNext = collect.get(0);
+            // 交换位置
+            int temp = sort;
+            newsMapper.swapSort(newsId,newsNext.getSort());
+            newsMapper.swapSort(newsNext.getPkId(),temp);
+        }
+    }
+
+
     /**
      * 首页新闻资讯 前台展示
      *

+ 29 - 8
src/main/resources/mapper/knowledge/NewsMapper.xml

@@ -13,6 +13,7 @@
                 <result property="views" column="VIEWS" jdbcType="INTEGER"/>
                 <result property="enabled" column="ENABLED" jdbcType="INTEGER"/>
                 <result property="isDel" column="IS_DEL" jdbcType="INTEGER"/>
+                <result property="sort" column="SORT" jdbcType="INTEGER"/>
                 <result property="companyId" column="COMPANY_ID_" jdbcType="VARCHAR"/>
                 <result property="createDepId" column="CREATE_DEP_ID_" jdbcType="VARCHAR"/>
                 <result property="tenantId" column="TENANT_ID_" jdbcType="VARCHAR"/>
@@ -23,7 +24,7 @@
     </resultMap>
 
     <sql id="columns">
-        PK_ID,TITLE,AUTHOR_ID,AUTHOR,SUMMARY,THUMBNAIL,CONTENT,VIEWS,ENABLED,IS_DEL,
+        PK_ID,TITLE,AUTHOR_ID,AUTHOR,SUMMARY,THUMBNAIL,CONTENT,VIEWS,ENABLED,IS_DEL,SORT,
         COMPANY_ID_,CREATE_DEP_ID_,TENANT_ID_,
         CREATE_BY_,CREATE_TIME_,UPDATE_BY_,UPDATE_TIME_
     </sql>
@@ -67,13 +68,13 @@
             </if>
             and IS_DEL = 0
         </where>
-        ORDER BY ENABLED DESC
-        <if test="@rx.Ognl@isNotEmpty(w.orderBySql)">
-            ,${w.orderBySql}
-        </if>
-        <if test="@rx.Ognl@isEmpty(w.orderBySql)">
-            ,CREATE_TIME_ DESC
-        </if>
+        ORDER BY SORT ASC,ENABLED DESC
+<!--        <if test="@rx.Ognl@isNotEmpty(w.orderBySql)">-->
+<!--            ,${w.orderBySql}-->
+<!--        </if>-->
+<!--        <if test="@rx.Ognl@isEmpty(w.orderBySql)">-->
+<!--            ,CREATE_TIME_ DESC-->
+<!--        </if>-->
     </select>
 
     <select id="findAllPcNews" resultType="com.redxun.knowledge.entity.vo.NewsAdminVo">
@@ -91,6 +92,26 @@
         order by VIEWS DESC
     </select>
 
+    <update id="insertOrUpdateBySort">
+        update KM_NEWS
+        set SORT = SORT + 1
+        where SORT >= #{sort}
+    </update>
+
+    <update id="swapSort">
+        update KM_NEWS
+        set
+            SORT = #{sort}
+        where PK_ID = #{bannerId}
+    </update>
+
+    <delete id="deleteNews">
+        update KM_NEWS
+        SET
+            IS_DEL = 1,UPDATE_TIME_ = sysdate
+        where PK_ID = #{id}
+    </delete>
+
 </mapper>