Преглед изворни кода

编辑器防止重复提交

wuweihao пре 3 година
родитељ
комит
fe539e0d1d

+ 3 - 0
720yun_fd_manage/gis_common/src/main/java/com/gis/common/constant/RedisConstant.java

@@ -7,4 +7,7 @@ public class RedisConstant {
 
     /**作品id*/
     public static final String WORK_ID = "workId:";
+
+    /**作品编辑*/
+    public static final String WORK_ID_EDIT = "workId:edit:";
 }

+ 11 - 0
720yun_fd_manage/gis_mapper/src/main/java/com/gis/mapper/WorkMapper.java

@@ -2,6 +2,7 @@ package com.gis.mapper;
 
 
 import com.gis.domain.dto.PageDto;
+import com.gis.domain.entity.SceneEntity;
 import com.gis.domain.entity.WorkEntity;
 import com.gis.mapper.provider.WorkProvider;
 import org.apache.ibatis.annotations.Mapper;
@@ -28,5 +29,15 @@ public interface WorkMapper extends IBaseMapper<WorkEntity, Long> {
     @Select("select id, name, scene_codes from tb_work where is_delete = 0 and user_id = #{userId} and scene_codes like #{sceneCode}")
     List<WorkEntity> likeSceneCode(String sceneCode, String userId);
 
+    /**
+     * for update 锁表更新完,才能进行更新,需要开启事务
+     * 并发量大作会出现脏数据
+     *
+     * @param id
+     * @return
+     */
+    @Select("select id, name, scene_codes, user_id from tb_work where is_delete = 0 and id = #{id} for update")
+    WorkEntity findByIdForUpdate(Long id);
+
 
 }

+ 3 - 0
720yun_fd_manage/gis_service/src/main/java/com/gis/service/WorkService.java

@@ -5,6 +5,7 @@ import com.gis.common.util.Result;
 import com.gis.domain.dto.PageDto;
 import com.gis.domain.dto.SomeDataDto;
 import com.gis.domain.dto.WorkDto;
+import com.gis.domain.entity.SceneEntity;
 import com.gis.domain.entity.WorkEntity;
 import com.gis.domain.vo.WorkInfoVo;
 import org.springframework.web.multipart.MultipartFile;
@@ -39,5 +40,7 @@ public interface WorkService extends IBaseService<WorkEntity, Long> {
 
     List<WorkEntity> likeSceneCode(String sceneCode);
 
+    WorkEntity findByIdForUpdate(Long id);
+
 
 }

+ 0 - 76
720yun_fd_manage/gis_service/src/main/java/com/gis/service/impl/SceneServiceImpl.java

@@ -252,32 +252,6 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, Long> implem
         return entity;
     }
 
-//    private Long createCatalog(CatalogSceneDto params) {
-//        Long parentId = params.getParentId();
-//        String parentName = params.getParentName();
-//        Long workId = params.getWorkId();
-//
-//        CatalogEntity entity = new CatalogEntity();
-//        entity.setName(params.getCatalogName());
-//        entity.setWorkId(workId);
-//        // 当父id不为空时
-//        if (parentId != null) {
-//            entity.setParentId(parentId);
-//        }
-//
-//        // 创建父目录
-//        if (parentId == null && parentName != null) {
-//            CatalogEntity parentEntity = new CatalogEntity();
-//            parentEntity.setWorkId(workId);
-//            parentEntity.setName(parentName);
-//            catalogService.save(parentEntity);
-//            entity.setParentId(parentEntity.getId());
-//
-//        }
-//        // 创建目录
-//        catalogService.save(entity);
-//        return entity.getId();
-//    }
 
 
 
@@ -293,39 +267,6 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, Long> implem
     }
 
 
-//    @Override
-//    public Result setIndex(String id, Long workId) {
-//        WorkEntity entity = workService.findById(workId);
-//
-//        if (entity == null) {
-//            log.error("作品不存在,id:  {}", workId);
-//            return Result.failure("作品不存在");
-//        }
-//        entity.setSceneIndex(id);
-//        entity.setUpdateTime(new Date());
-//        workService.update(entity);
-//
-//        return Result.success();
-//    }
-
-//    @Override
-//    public Result getIndex(Long workId) {
-//        WorkEntity entity = workService.findById(workId);
-//
-//        if (entity == null) {
-//            log.error("作品不存在,id:  {}", workId);
-//            return Result.failure("作品不存在");
-//        }
-//        String sceneIndex = entity.getSceneIndex();
-//        SceneEntity sceneEntity = null;
-//        // 字符串判断需要用isNotBlank
-//        if (StringUtils.isNotBlank(sceneIndex)) {
-//            sceneEntity = this.findById(Long.valueOf(sceneIndex));
-//        }
-//        return Result.success(sceneEntity);
-//    }
-
-
     @Override
     public Result setSort(Map<String, String> param) {
         log.info("sort size: " + param.size());
@@ -455,23 +396,6 @@ public class SceneServiceImpl extends IBaseServiceImpl<SceneEntity, Long> implem
         return entityMapper.findByWorkId(workId);
     }
 
-//    @Override
-//    public Result getVoIndex(Long workId) {
-//        WorkEntity entity = workService.findById(workId);
-//
-//        if (entity == null) {
-//            log.error("作品不存在,id:  {}", workId);
-//            return Result.failure("作品不存在");
-//        }
-//        String sceneIndex = entity.getSceneIndex();
-//        CatalogSceneVo vo = null;
-//        // 字符串判断需要用isNotBlank
-//        if (StringUtils.isNotBlank(sceneIndex)) {
-//            Long sceneId = Long.valueOf(sceneIndex);
-//            vo = entityMapper.findVoById(sceneId);
-//        }
-//        return Result.success(vo);
-//    }
 
     @Override
     public Result editEntity(BaseDto param) {

+ 28 - 3
720yun_fd_manage/gis_service/src/main/java/com/gis/service/impl/WorkServiceImpl.java

@@ -39,6 +39,7 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -48,7 +49,7 @@ import java.util.stream.Stream;
  */
 @Slf4j
 @Service
-@Transactional
+//@Transactional
 public class WorkServiceImpl extends IBaseServiceImpl<WorkEntity, Long> implements WorkService {
 
     @Autowired
@@ -83,6 +84,8 @@ public class WorkServiceImpl extends IBaseServiceImpl<WorkEntity, Long> implemen
     RedisUtil redisUtil;
 
 
+
+
     @Override
     public IBaseMapper<WorkEntity, Long> getBaseMapper() {
         return this.entityMapper;
@@ -242,16 +245,38 @@ public class WorkServiceImpl extends IBaseServiceImpl<WorkEntity, Long> implemen
     }
 
     @Override
+    public WorkEntity findByIdForUpdate(Long id) {
+        return entityMapper.findByIdForUpdate(id);
+    }
+
+    @Override
     public Result edit(SomeDataDto param) {
 
+
         // 检查是否是改用户的作品
         Long id = param.getId();
-        WorkEntity entity = this.findById(id);
+
+        // 防止重复提交
+        String editKey = RedisConstant.WORK_ID_EDIT + id + ":"+param.toString();
+        if (redisUtil.hasKey(editKey)){
+            log.info("编辑走缓存");
+            return Result.success();
+        }
+
+        // 3秒内防止重复提交
+        redisUtil.set(editKey, param.toString(), 3, TimeUnit.SECONDS);
+
+        WorkEntity entity = findByIdForUpdate(id);
         if (entity == null) {
             log.error("作品已被删除,无法编辑, id: " + id);
             throw new BaseRuntimeException(MsgCode.e3006, "作品已被删除,无法编辑, id: " + id);
         }
-
+        try {
+            // 休眠0.5秒
+            Thread.sleep(500);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
         String userNameForToken = getUserNameForToken();
         if (!userNameForToken.equals(entity.getUserId())) {
             return Result.failure(MsgCode.e3005, "当前无操作权限");

+ 2 - 1
720yun_fd_manage/gis_web/src/main/java/com/gis/web/controller/WorkController.java

@@ -18,6 +18,7 @@ import lombok.extern.log4j.Log4j2;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -83,7 +84,7 @@ public class WorkController extends BaseController {
         return workService.entitySave(param);
     }
 
-
+    @Transactional(rollbackFor = Exception.class)
     @ApiOperation(value = "修改someData", position = 1)
     @PostMapping("edit")
     public Result<WorkEntity> edit(@Valid @RequestBody SomeDataDto param) {

+ 1 - 1
startJar.sh

@@ -1,4 +1,4 @@
-APP_NAME=
+APP_NAME=720yun_fd_manage.jar
 date=`date`
 usage() {
     echo "case: sh run.sh [start|stop|restart|status|checkServer]"