Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of code #608

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@EnableCaching
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisTemplateConfiguration {
public class RedisTemplateConfiguration extends setOpTemplate {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KasaraneniKavya 这么做的的好处是啥?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lltx
分配过程的一部分


@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
Expand Down Expand Up @@ -60,14 +60,4 @@ public ListOperations<String, Object> listOperations(RedisTemplate<String, Objec
return redisTemplate.opsForList();
}

@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}

@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.pig4cloud.pig.common.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ZSetOperations;

public class setOpTemplate {
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}

@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public void insertFill(MetaObject metaObject) {
log.debug("mybatis plus start insert fill ....");
LocalDateTime now = LocalDateTime.now();

fillValIfNullByName("createTime", now, metaObject, false);
fillValIfNullByName("updateTime", now, metaObject, false);
fillValIfNullByName("createBy", getUserName(), metaObject, false);
fillValIfNullByName("updateBy", getUserName(), metaObject, false);
ReplaceNullValuesbyName("createTime", now, metaObject, false);
ReplaceNullValuesbyName("updateTime", now, metaObject, false);
ReplaceNullValuesbyName("createBy", getUserName(), metaObject, false);
ReplaceNullValuesbyName("updateBy", getUserName(), metaObject, false);
}

@Override
public void updateFill(MetaObject metaObject) {
log.debug("mybatis plus start update fill ....");
fillValIfNullByName("updateTime", LocalDateTime.now(), metaObject, true);
fillValIfNullByName("updateBy", getUserName(), metaObject, true);
ReplaceNullValuesbyName("updateTime", LocalDateTime.now(), metaObject, true);
ReplaceNullValuesbyName("updateBy", getUserName(), metaObject, true);
}

/**
Expand All @@ -45,7 +45,7 @@ public void updateFill(MetaObject metaObject) {
* @param metaObject MetaObject
* @param isCover 是否覆盖原有值,避免更新操作手动入参
*/
private static void fillValIfNullByName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) {
private static void ReplaceNullValuesbyName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) {
// 1. 没有 get 方法
if (!metaObject.hasSetter(fieldName)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pig.admin.api.dto.UserDTO;
import com.pig4cloud.pig.admin.api.dto.UserInfo;
import com.pig4cloud.pig.admin.api.entity.SysDept;
import com.pig4cloud.pig.admin.api.entity.SysPost;
import com.pig4cloud.pig.admin.api.entity.SysRole;
import com.pig4cloud.pig.admin.api.entity.SysUser;
import com.pig4cloud.pig.admin.api.vo.UserExcelVO;
import com.pig4cloud.pig.admin.api.vo.UserVO;
import com.pig4cloud.pig.common.core.util.R;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
Expand Down Expand Up @@ -115,4 +119,6 @@ public interface SysUserService extends IService<SysUser> {
*/
List<Long> listUserIdByDeptIds(Set<Long> deptIds);

void insertExcelUser(UserExcelVO excel, Optional<SysDept> deptOptional, List<SysRole> roleCollList,
List<SysPost> postCollList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ public List<Long> listUserIdByDeptIds(Set<Long> deptIds) {
/**
* 插入excel User
*/
private void insertExcelUser(UserExcelVO excel, Optional<SysDept> deptOptional, List<SysRole> roleCollList,
List<SysPost> postCollList) {
@Override
public void insertExcelUser(UserExcelVO excel, Optional<SysDept> deptOptional, List<SysRole> roleCollList,
List<SysPost> postCollList) {
UserDTO userDTO = new UserDTO();
userDTO.setUsername(excel.getUsername());
userDTO.setPhone(excel.getPhone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
@RequestMapping(value = "/gateway/flow")
public class GatewayFlowRuleController {

private final Logger logger = LoggerFactory.getLogger(GatewayFlowRuleController.class);
private final Logger factoryLogger = LoggerFactory.getLogger(GatewayFlowRuleController.class);

@Autowired
private InMemGatewayFlowRuleStore repository;
Expand All @@ -62,27 +62,33 @@ public class GatewayFlowRuleController {
@AuthAction(AuthService.PrivilegeType.READ_RULE)
public Result<List<GatewayFlowRuleEntity>> queryFlowRules(String app, String ip, Integer port) {

if (StringUtil.isEmpty(app)) {
return Result.ofFail(-1, "app can't be null or empty");
}
if (StringUtil.isEmpty(ip)) {
return Result.ofFail(-1, "ip can't be null or empty");
}
if (port == null) {
return Result.ofFail(-1, "port can't be null");
}
Result<List<GatewayFlowRuleEntity>> ofFail = getRequiredResult(app, ip, port);
if (ofFail != null) return ofFail;

try {
List<GatewayFlowRuleEntity> rules = sentinelApiClient.fetchGatewayFlowRules(app, ip, port).get();
repository.saveAll(rules);
return Result.ofSuccess(rules);
}
catch (Throwable throwable) {
logger.error("query gateway flow rules error:", throwable);
factoryLogger.error("query gateway flow rules error:", throwable);
return Result.ofThrowable(-1, throwable);
}
}

private Result<List<GatewayFlowRuleEntity>> getRequiredResult(String app, String ip, Integer port) {
if (StringUtil.isEmpty(app)) {
return Result.ofFail(-1, "app can't be null or empty");
}
if (StringUtil.isEmpty(ip)) {
return Result.ofFail(-1, "ip can't be null or empty");
}
if (port == null) {
return Result.ofFail(-1, "port can't be null");
}
return null;
}

@PostMapping("/new.json")
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
public Result<GatewayFlowRuleEntity> addFlowRule(@RequestBody AddFlowRuleReqVo reqVo) {
Expand Down Expand Up @@ -245,12 +251,12 @@ else if (CONTROL_BEHAVIOR_RATE_LIMITER == controlBehavior) {
entity = repository.save(entity);
}
catch (Throwable throwable) {
logger.error("add gateway flow rule error:", throwable);
factoryLogger.error("add gateway flow rule error:", throwable);
return Result.ofThrowable(-1, throwable);
}

if (!publishRules(app, ip, port)) {
logger.warn("publish gateway flow rules fail after add");
factoryLogger.warn("publish gateway flow rules fail after add");
}

return Result.ofSuccess(entity);
Expand Down Expand Up @@ -398,12 +404,12 @@ else if (CONTROL_BEHAVIOR_RATE_LIMITER == controlBehavior) {
entity = repository.save(entity);
}
catch (Throwable throwable) {
logger.error("update gateway flow rule error:", throwable);
factoryLogger.error("update gateway flow rule error:", throwable);
return Result.ofThrowable(-1, throwable);
}

if (!publishRules(app, entity.getIp(), entity.getPort())) {
logger.warn("publish gateway flow rules fail after update");
factoryLogger.warn("publish gateway flow rules fail after update");
}

return Result.ofSuccess(entity);
Expand All @@ -426,12 +432,12 @@ public Result<Long> deleteFlowRule(Long id) {
repository.delete(id);
}
catch (Throwable throwable) {
logger.error("delete gateway flow rule error:", throwable);
factoryLogger.error("delete gateway flow rule error:", throwable);
return Result.ofThrowable(-1, throwable);
}

if (!publishRules(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) {
logger.warn("publish gateway flow rules fail after delete");
factoryLogger.warn("publish gateway flow rules fail after delete");
}

return Result.ofSuccess(id);
Expand Down