Prechádzať zdrojové kódy

token失效更新提示错误信息,优化房源save接口逻辑

houweiyu 5 rokov pred
rodič
commit
7bb4092827

+ 139 - 87
fdkanfang-web/src/main/java/com/fdkanfang/web/backend/HouseController.java

@@ -153,52 +153,18 @@ public class HouseController extends BaseController {
             return new R(MsgCode.e_COMMON_3001, "楼层/户型/面积/朝向不能为空");
         }
 
-        boolean isNewAdd = false;
-        HouseEntity house = null;
-        if (param.getId() == null) {
-            house = new HouseEntity();
-            UserEntity user  = userUtils.getUserByToken(getToken());
-            house.setUserId(user.getId());
-            param.setId(null);
-            BeanUtils.copyProperties(param, house);
-            Integer byMaxNum = houseService2.findByMaxNum();
-            // 用于第一条数据
-            byMaxNum = (byMaxNum == null)? 10000:byMaxNum;
-            String sceneCode = getSceneCode();
-            house.setNum(byMaxNum + 1);
-            house.setSceneCode(sceneCode);
-            house.setWebSite(domain+"?m="+sceneCode);
-            house.setFilePath(OUTPATH + sceneCode);
-            houseService2.save(house);
-            SceneProEntity sceneProEntity = new SceneProEntity();
-            sceneProEntity.setNum(house.getSceneCode());
-            sceneProService.save(sceneProEntity);
-            SceneProEditEntity sceneProEditEntity = new SceneProEditEntity();
-            sceneProEditEntity.setProId(sceneProEntity.getId());
-            // 默认是1
-            sceneProEditEntity.setPanoVisi(1);
-            sceneProEditEntity.setM2dVisi(1);
-            sceneProEditEntity.setM3dVisi(1);
-            sceneProEditEntity.setMeasureVisi(1);
-            sceneProEditService.save(sceneProEditEntity);
-            isNewAdd = true;
-        } else {
-            house = houseService2.findById(param.getId());
-            if (house == null) {
-                log.error("房源不存在: {}", param.getId());
-                return new R(MsgCode.e_COMMON_3002, MsgCode.msg_COMMON_3002);
-            }
-            BeanUtils.copyProperties(param, house);
-            house.setUpdateTime(new Date());
-            houseService2.update(house);
-        }
         // 处理上传图片
         MultipartFile[] files = param.getFiles();
         if (files == null || files.length <= 0) {
             log.info("文件为空");
             return new R(MsgCode.e_COMMON_3100, MsgCode.msg_COMMON_3100);
         }
-
+        boolean isNewAdd = false;
+        HouseEntity house = null;
+        if (param.getId() == null) {
+            isNewAdd = true;
+        }
+        house = updateOrInsertHouse(param);
         // 只有新增是才保存图片,修改时另外处理
         if (isNewAdd) {
             //场景码
@@ -210,67 +176,30 @@ public class HouseController extends BaseController {
             HashMap<String, String> ossVerticalImageLowMap = new HashMap<>();
             String imageHighPath = null;
             String imageLowPath = null;
-            //照片分辨率,以最高的为准
             ImageResolutionRate maxResolutionRate = null;
-            int imageResolutionRateSort = 0;
             for (MultipartFile file : files) {
-                ImageEntity image = new ImageEntity();
                 String filename = file.getOriginalFilename();
-                if(StringUtils.isBlank(filename)){
-                    throw new CommonBaseException(ResultCodeEnum.D100 , "文件名不能为空");
-                }
-                if(filename.contains(".JPG")){
-                    int index = filename.lastIndexOf(".");
-                    String fileNameWithoutPostfix = filename.substring(0 , index);
-                    filename = fileNameWithoutPostfix + ".jpg";
-                    log.info("照片的格式写成了大写,需要转成小写:{}" , filename);
-                }
-                // 检查一下,测试时,可能会没有"_"
-                if (!filename.contains("_")) {
-                    log.error("filename: {}", filename);
-                    throw new BaseRuntimeException(MsgCode.e_COMMON_3003, "图片命名不符合要求,需要类型+下划线");
-                }
-                if (RegexpUtils.isContainChinese(filename)) {
-                    log.error("图片名称不允许中文字符: {}", filename);
-                    throw new BaseRuntimeException(MsgCode.e_COMMON_3003, "图片名称不允许中文字符");
-                }
-
-                // 获取图片房间类型
-                String imageType = StringUtils.substringBefore(filename, "_");
-
-                // 本地图片保存位置: /root/data/kanfang/场景码/input_img/xxxx.jpg
-                String fileFullPath = directoryName + filename;
-
+                filename = checkAndChangeFileName(filename);
                 imageHighPath = OUTPATH + sceneCode + "/output_img/" + filename;
                 imageLowPath = OUTPATH + sceneCode + "/output_img_low/" + filename;
                 // 提前把算法的图片路径存到数据库,但不代表算法运行成功,最后好是要看house的状态;
                 String ossVerticalHighPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/high/"+ filename;
                 String ossVerticalLowPath = ConstantFilePath.OSS_IMAGE_PATH+ sceneCode+"/pan/low/"+ filename;
-                image.setVerticalPath(ossVerticalHighPath);
-                image.setFileName(filename);
-                // 这个参数给前端用,只要有文件名就可以了
-                image.setPath(fileFullPath);
-                image.setLocalPath(fileFullPath);
-                image.setHouseId(house.getId());
-                // 默认所有图片都是一楼
-                image.setFloor(1);
-                image.setType(imageType);
+                // 封装垂直校验后的图片到信息到oss
+                ossVerticalImageHighMap.put(imageHighPath, ossVerticalHighPath);
+                ossVerticalImageLowMap.put(imageLowPath, ossVerticalLowPath);
                 try {
-                    //将图片保存到本地指定目录filePath eg: /root/data/kanfang/d_9iRDUgn3l/input_img/xxx.jpg
-                    int  resolutionRate = FileUtils.downloanAndGetResolutionRate(file.getInputStream(), fileFullPath , false);
-                    maxResolutionRate = ImageResolutionRate.getResolutionRateByRate(resolutionRate);
-                    image.setResolutionRate(null != maxResolutionRate ? maxResolutionRate.name() : "TWO_K");
-                    imageService2.save(image);
-                    // 封装垂直校验后的图片到信息到oss
-                    ossVerticalImageHighMap.put(imageHighPath, ossVerticalHighPath);
-                    ossVerticalImageLowMap.put(imageLowPath, ossVerticalLowPath);
+                    ImageResolutionRate tmpResolutionRate = addAndGetResolutionRate(file , directoryName , ossVerticalHighPath , house.getId());
+                    if(ImageResolutionRate.FOUR_K.equals(tmpResolutionRate)){
+                        maxResolutionRate = tmpResolutionRate;
+                    }
                     needSendMqMsg = true;
                 } catch (IOException e) {
                     needSendMqMsg = false;
                     log.error("保存照片到本地和持久化image对象出现异常:{}" , e);
                 }
             }
-
+            insertScene(maxResolutionRate , house);
             if (needSendMqMsg) {
                 HashMap<Object, Object> mqMap = new HashMap<>();
                 mqMap.put("ossImageHigh", ossVerticalImageHighMap);
@@ -284,9 +213,132 @@ public class HouseController extends BaseController {
                 rabbitTemplate.convertAndSend(RabbitConfig.VERTICAL_EXCHANGE, RabbitConfig.VERTICAL_QUEUE_ROUTING, mqMap);
             }
         }
-
         return new R(MsgCode.SUCCESS_CODE, house);
     }
+    @Transactional(rollbackFor = Exception.class)
+    public void insertScene(ImageResolutionRate maxResolutionRate , HouseEntity house){
+        SceneProEntity sceneProEntity = new SceneProEntity();
+        SceneProEditEntity sceneProEditEntity = new SceneProEditEntity();
+        if(ImageResolutionRate.TWO_K.equals(maxResolutionRate)){
+            //2k
+            sceneProEntity.setSceneScheme(11);
+        }else{
+            //4k
+            sceneProEntity.setSceneScheme(10);
+        }
+        sceneProEntity.setNum(house.getSceneCode());
+        //TODO:不知道为何数据库设置的默认不生效
+        sceneProEditEntity.setM2dVisi(1);
+        sceneProEditEntity.setM3dVisi(1);
+        sceneProEditEntity.setPanoVisi(1);
+        sceneProEditEntity.setMapVisi(1);
+        int insertOne = sceneProService.save(sceneProEntity);
+        if(insertOne != 1){
+            throw  new CommonBaseException(ResultCodeEnum.D101, "新增scenePro记录失败");
+        }
+        sceneProEditEntity.setProId(sceneProEntity.getId());
+        int insertTwo = sceneProEditService.save(sceneProEditEntity);
+        if(insertTwo != 1){
+            throw  new CommonBaseException(ResultCodeEnum.D101, "新增sceneProEdit记录失败");
+        }
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    public ImageResolutionRate addAndGetResolutionRate(MultipartFile file , String directoryName ,
+                                                        String ossVerticalHighPath , Long houseId) throws IOException {
+        if(null == file || null == houseId){
+            log.error("房源id或者文件为空,无法生成image记录");
+            return null;
+        }
+        String filename = file.getOriginalFilename();
+        filename = checkAndChangeFileName(filename);
+
+        // 获取图片房间类型
+        String imageType = StringUtils.substringBefore(filename, "_");
+
+        // 本地图片保存位置: /root/data/kanfang/场景码/input_img/xxxx.jpg
+        String fileFullPath = directoryName + filename;
+        ImageEntity image = new ImageEntity();
+        image.setVerticalPath(ossVerticalHighPath);
+        image.setFileName(filename);
+        // 这个参数给前端用,只要有文件名就可以了
+        image.setPath(fileFullPath);
+        image.setLocalPath(fileFullPath);
+
+        image.setHouseId(houseId);
+        // 默认所有图片都是一楼
+        image.setFloor(1);
+        image.setType(imageType);
+        //将图片保存到本地指定目录filePath eg: /root/data/kanfang/d_9iRDUgn3l/input_img/xxx.jpg
+        int  resolutionRate = FileUtils.downloanAndGetResolutionRate(file.getInputStream(), fileFullPath , false);
+        //照片分辨率
+        ImageResolutionRate maxResolutionRate = ImageResolutionRate.getResolutionRateByRate(resolutionRate);
+        image.setResolutionRate(null != maxResolutionRate ? maxResolutionRate.name() : "TWO_K");
+        int insert = imageService2.save(image);
+        if(insert != 1){
+            throw  new CommonBaseException(ResultCodeEnum.D101, "新增image记录失败");
+        }
+        return maxResolutionRate;
+    }
+
+    private String checkAndChangeFileName(String filename){
+
+        if(StringUtils.isBlank(filename)){
+            throw new CommonBaseException(ResultCodeEnum.D100 , "文件名不能为空");
+        }
+        if(filename.contains(".JPG")){
+            int index = filename.lastIndexOf(".");
+            String fileNameWithoutPostfix = filename.substring(0 , index);
+            filename = fileNameWithoutPostfix + ".jpg";
+            log.info("照片的格式写成了大写,需要转成小写:{}" , filename);
+        }
+        // 检查一下,测试时,可能会没有"_"
+        if (!filename.contains("_")) {
+            log.error("filename: {}", filename);
+            throw new BaseRuntimeException(MsgCode.e_COMMON_3003, "图片命名不符合要求,需要类型+下划线");
+        }
+        if (RegexpUtils.isContainChinese(filename)) {
+            log.error("图片名称不允许中文字符: {}", filename);
+            throw new BaseRuntimeException(MsgCode.e_COMMON_3003, "图片名称不允许中文字符");
+        }
+        return filename;
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    public HouseEntity updateOrInsertHouse(HouseDto param){
+        if(null == param){
+            return null;
+        }
+        HouseEntity house = null;
+        if (param.getId() == null) {
+            house = new HouseEntity();
+            UserEntity user  = userUtils.getUserByToken(getToken());
+            house.setUserId(user.getId());
+            param.setId(null);
+            BeanUtils.copyProperties(param, house);
+            Integer byMaxNum = houseService2.findByMaxNum();
+            // 用于第一条数据
+            byMaxNum = (byMaxNum == null)? 10000:byMaxNum;
+            String sceneCode = getSceneCode();
+            house.setNum(byMaxNum + 1);
+            house.setSceneCode(sceneCode);
+            house.setWebSite(domain+"?m="+sceneCode);
+            house.setFilePath(OUTPATH + sceneCode);
+            houseService2.save(house);
+        } else {
+            house = houseService2.findById(param.getId());
+            if (house == null) {
+                log.error("房源不存在: {}", param.getId());
+                 throw new CommonBaseException(ResultCodeEnum.D101 , "房源不存在");
+            }
+            BeanUtils.copyProperties(param, house);
+            house.setUpdateTime(new Date());
+            houseService2.update(house);
+        }
+        return house;
+    }
+
+
 
 
     @ApiOperation("预审")

+ 1 - 1
fdkanfang-web/src/main/java/com/fdkanfang/web/shiro/MyRealm.java

@@ -95,7 +95,7 @@ public class MyRealm extends AuthorizingRealm {
         String username = JWTUtil.getUserName(token);
         if (username == null) {
             log.error("error token username");
-            throw new JwtAuthenticationException(5001, "token invalid");
+            throw new JwtAuthenticationException(5001, "账户登录已过期,请重新登录");
 
         }