Kaynağa Gözat

优化小区配置功能

java110 4 yıl önce
ebeveyn
işleme
394e898ffe

+ 110 - 0
java110-core/src/main/java/com/java110/core/factory/CommunitySettingFactory.java

@@ -0,0 +1,110 @@
+package com.java110.core.factory;
+
+import com.java110.dto.communitySetting.CommunitySettingDto;
+import com.java110.intf.community.ICommunitySettingInnerServiceSMO;
+import com.java110.utils.cache.BaseCache;
+import com.java110.utils.factory.ApplicationContextFactory;
+import com.java110.utils.util.SerializeUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import redis.clients.jedis.Jedis;
+
+import java.util.List;
+
+public class CommunitySettingFactory extends BaseCache {
+
+    //日志
+    private static Logger logger = LoggerFactory.getLogger(CommunitySettingFactory.class);
+
+
+    /**
+     * 查询设置值
+     *
+     * @param communityId
+     * @param key
+     * @return
+     */
+    public static String getValue(String communityId, String key) {
+        Jedis redis = null;
+        CommunitySettingDto communitySettingDto = null;
+        try {
+            redis = getJedis();
+            Object object = SerializeUtil.unserialize(redis.get((communityId + "_" + key + "_community_setting").getBytes()));
+            if (object == null) {//这里存在并发问题,但是 等于查询了多次 然后多次写缓存,作者认为 这种应该比加全局锁效率高些
+                communitySettingDto = getCommunitySettingFromDb(communityId, key, redis);
+            } else {
+                communitySettingDto = (CommunitySettingDto) object;
+            }
+        } finally {
+            if (redis != null) {
+                redis.close();
+            }
+        }
+
+        if (communitySettingDto == null) {
+            return null;
+        }
+
+        return communitySettingDto.getSettingValue();
+    }
+
+    public static CommunitySettingDto getCommunitySettingFromDb(String communityId, String key) {
+        Jedis redis = null;
+        try {
+            redis = getJedis();
+            return getCommunitySettingFromDb(communityId, key, redis);
+        } finally {
+            if (redis != null) {
+                redis.close();
+            }
+        }
+
+
+    }
+
+    private static CommunitySettingDto getCommunitySettingFromDb(String communityId, String key, Jedis redis) {
+        ICommunitySettingInnerServiceSMO communitySettingInnerServiceSMOImpl
+                = ApplicationContextFactory.getBean(ICommunitySettingInnerServiceSMO.class.getName(), ICommunitySettingInnerServiceSMO.class);
+        CommunitySettingDto communitySettingDto = new CommunitySettingDto();
+        communitySettingDto.setCommunityId(communityId);
+        communitySettingDto.setSettingKey(key);
+        List<CommunitySettingDto> communitySettingDtos = communitySettingInnerServiceSMOImpl.queryCommunitySettings(communitySettingDto);
+        if (communitySettingDtos == null || communitySettingDtos.size() < 1) {
+            return null;
+        }
+
+        redis.set((communityId + "_" + key + "_community_setting").getBytes(), SerializeUtil.serialize(communitySettingDtos.get(0)));
+        return communitySettingDtos.get(0);
+    }
+
+    /**
+     * 查询设置值
+     *
+     * @param communityId
+     * @param key
+     * @return
+     */
+    public static CommunitySettingDto getCommunitySetting(String communityId, String key) {
+        Jedis redis = null;
+        CommunitySettingDto communitySettingDto = null;
+        try {
+            redis = getJedis();
+            Object object = SerializeUtil.unserialize(redis.get((communityId + "_" + key + "_community_setting").getBytes()));
+            if (object == null) { //这里存在并发问题,但是 等于查询了多次 然后多次写缓存,作者认为 这种应该比加全局锁效率高些
+                communitySettingDto = getCommunitySettingFromDb(communityId, key, redis);
+            } else {
+                communitySettingDto = (CommunitySettingDto) object;
+            }
+        } finally {
+            if (redis != null) {
+                redis.close();
+            }
+        }
+
+        if (communitySettingDto == null) {
+            return null;
+        }
+
+        return communitySettingDto;
+    }
+}

+ 3 - 3
service-common/src/main/java/com/java110/common/bmo/logSystemError/impl/SaveLogSystemErrorBMOImpl.java

@@ -28,11 +28,11 @@ public class SaveLogSystemErrorBMOImpl implements ISaveLogSystemErrorBMO {
         logSystemErrorPo.setErrId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_errId));
         logSystemErrorPo.setErrId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_errId));
         int flag = logSystemErrorInnerServiceSMOImpl.saveLogSystemError(logSystemErrorPo);
         int flag = logSystemErrorInnerServiceSMOImpl.saveLogSystemError(logSystemErrorPo);
 
 
-        if (flag > 0) {
-            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
+        if (flag < 1) {
+            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
         }
         }
 
 
-        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
+        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
     }
     }
 
 
 }
 }

+ 8 - 3
service-community/src/main/java/com/java110/community/bmo/communitySetting/impl/SaveCommunitySettingBMOImpl.java

@@ -2,6 +2,7 @@ package com.java110.community.bmo.communitySetting.impl;
 
 
 import com.java110.community.bmo.communitySetting.ISaveCommunitySettingBMO;
 import com.java110.community.bmo.communitySetting.ISaveCommunitySettingBMO;
 import com.java110.core.annotation.Java110Transactional;
 import com.java110.core.annotation.Java110Transactional;
+import com.java110.core.factory.CommunitySettingFactory;
 import com.java110.core.factory.GenerateCodeFactory;
 import com.java110.core.factory.GenerateCodeFactory;
 import com.java110.intf.community.ICommunitySettingInnerServiceSMO;
 import com.java110.intf.community.ICommunitySettingInnerServiceSMO;
 import com.java110.po.communitySetting.CommunitySettingPo;
 import com.java110.po.communitySetting.CommunitySettingPo;
@@ -29,11 +30,15 @@ public class SaveCommunitySettingBMOImpl implements ISaveCommunitySettingBMO {
         communitySettingPo.setCsId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_csId));
         communitySettingPo.setCsId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_csId));
         int flag = communitySettingInnerServiceSMOImpl.saveCommunitySetting(communitySettingPo);
         int flag = communitySettingInnerServiceSMOImpl.saveCommunitySetting(communitySettingPo);
 
 
-        if (flag > 0) {
-        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
+        if (flag < 1) {
+            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
         }
         }
 
 
-        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
+        //将结果写入缓存
+        CommunitySettingFactory.getCommunitySettingFromDb(communitySettingPo.getCommunityId(), communitySettingPo.getSettingKey());
+
+
+        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
     }
     }
 
 
 }
 }

+ 6 - 4
service-community/src/main/java/com/java110/community/bmo/communitySetting/impl/UpdateCommunitySettingBMOImpl.java

@@ -2,6 +2,7 @@ package com.java110.community.bmo.communitySetting.impl;
 
 
 import com.java110.community.bmo.communitySetting.IUpdateCommunitySettingBMO;
 import com.java110.community.bmo.communitySetting.IUpdateCommunitySettingBMO;
 import com.java110.core.annotation.Java110Transactional;
 import com.java110.core.annotation.Java110Transactional;
+import com.java110.core.factory.CommunitySettingFactory;
 import com.java110.intf.community.ICommunitySettingInnerServiceSMO;
 import com.java110.intf.community.ICommunitySettingInnerServiceSMO;
 import com.java110.po.communitySetting.CommunitySettingPo;
 import com.java110.po.communitySetting.CommunitySettingPo;
 import com.java110.vo.ResultVo;
 import com.java110.vo.ResultVo;
@@ -16,8 +17,6 @@ public class UpdateCommunitySettingBMOImpl implements IUpdateCommunitySettingBMO
     private ICommunitySettingInnerServiceSMO communitySettingInnerServiceSMOImpl;
     private ICommunitySettingInnerServiceSMO communitySettingInnerServiceSMOImpl;
 
 
     /**
     /**
-     *
-     *
      * @param communitySettingPo
      * @param communitySettingPo
      * @return 订单服务能够接受的报文
      * @return 订单服务能够接受的报文
      */
      */
@@ -27,10 +26,13 @@ public class UpdateCommunitySettingBMOImpl implements IUpdateCommunitySettingBMO
         int flag = communitySettingInnerServiceSMOImpl.updateCommunitySetting(communitySettingPo);
         int flag = communitySettingInnerServiceSMOImpl.updateCommunitySetting(communitySettingPo);
 
 
         if (flag > 0) {
         if (flag > 0) {
-        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
+            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
         }
         }
 
 
-        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
+        //将结果写入缓存
+        CommunitySettingFactory.getCommunitySettingFromDb(communitySettingPo.getCommunityId(), communitySettingPo.getSettingKey());
+        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
+
     }
     }
 
 
 }
 }