|
|
package com.yoho.ufo.order.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.redis.cluster.annotation.Redis;
|
|
|
import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
|
|
|
import com.yoho.order.annotation.MetaProperty;
|
|
|
import com.yoho.order.dal.MetaConfigMapper;
|
|
|
import com.yoho.order.model.MetaConfig;
|
...
|
...
|
@@ -8,6 +10,10 @@ import com.yoho.order.model.MetaConfigReq; |
|
|
import com.yoho.order.model.PropertyItem;
|
|
|
import com.yoho.service.model.order.response.PageResponse;
|
|
|
import com.yoho.ufo.order.service.IMetaConfigService;
|
|
|
import com.yoho.ufo.service.model.ApiResponse;
|
|
|
import com.yoho.ufo.util.DateUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationContext;
|
...
|
...
|
@@ -16,6 +22,8 @@ import org.springframework.stereotype.Service; |
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.util.*;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* @Author: 杨长江
|
...
|
...
|
@@ -29,6 +37,9 @@ public class MetaConfigServiceImpl implements IMetaConfigService, ApplicationCon |
|
|
@Autowired
|
|
|
private MetaConfigMapper metaConfigMapper;
|
|
|
|
|
|
@Redis("gwNoSyncRedis")
|
|
|
private YHRedisTemplate yhRedisTemplate;
|
|
|
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -36,6 +47,34 @@ public class MetaConfigServiceImpl implements IMetaConfigService, ApplicationCon |
|
|
*/
|
|
|
private StringBuffer collectionName;
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(MetaConfigServiceImpl.class);
|
|
|
|
|
|
/**
|
|
|
* SuperEntry SUPER_ENTRY;
|
|
|
*
|
|
|
* Common COMMON;
|
|
|
*
|
|
|
* LargeSettlement LARGE_SETTLEMENT;
|
|
|
*
|
|
|
* FastDeliverySuper FAST_DELIVERY_SUPER;
|
|
|
* 这里几个属性名称非要大写,无奈
|
|
|
*/
|
|
|
private static final String[] upperCaseClassNameArray= {"SuperEntry","Common","LargeSettlement","FastDeliverySuper"};
|
|
|
|
|
|
/**
|
|
|
* HkInStock hk_in_stock
|
|
|
*/
|
|
|
private static final String[] splitUnderLineClassNameArray = {"HkInStock"};
|
|
|
/**
|
|
|
* 上面的数组转成列表
|
|
|
*/
|
|
|
private static final List<String> upperCaseClassNameList = Arrays.asList(upperCaseClassNameArray);
|
|
|
|
|
|
/**
|
|
|
* 上面的数组转成列表
|
|
|
*/
|
|
|
private static final List<String> splitUnderLineClassNameList = Arrays.asList(splitUnderLineClassNameArray);
|
|
|
|
|
|
@Override
|
|
|
public MetaConfig selectByPrimaryKey(int id) {
|
|
|
return this.metaConfigMapper.selectByPrimaryKey(id);
|
...
|
...
|
@@ -71,8 +110,37 @@ public class MetaConfigServiceImpl implements IMetaConfigService, ApplicationCon |
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int updateByPrimaryKey(MetaConfig config) {
|
|
|
return this.metaConfigMapper.updateByPrimaryKey(config);
|
|
|
public ApiResponse updateByPrimaryKey(MetaConfig config) {
|
|
|
|
|
|
Object bean = applicationContext.getBean(config.getCode());
|
|
|
|
|
|
//如果根据code找不到bean,说明code被修改了,抛异常,直接返回
|
|
|
if(Objects.isNull(bean)){
|
|
|
return new ApiResponse.ApiResponseBuilder().code(500).message("数据校验出错,操作失败").build();
|
|
|
}
|
|
|
//验证json格式是否正确
|
|
|
Object object;
|
|
|
try {
|
|
|
object = JSONObject.parseObject(config.getValue(), bean.getClass());
|
|
|
}catch (Exception e){
|
|
|
LOGGER.error("JSON数据格式/类型错误",e);
|
|
|
return new ApiResponse.ApiResponseBuilder().code(500).message("数据校验出错,操作失败").build();
|
|
|
}
|
|
|
|
|
|
if(Objects.isNull(object)){
|
|
|
return new ApiResponse.ApiResponseBuilder().code(500).message("数据校验出错,操作失败").build();
|
|
|
}
|
|
|
config.setValue(JSONObject.toJSONString(object));
|
|
|
LOGGER.info("更新服务配置 ------------>" + config);
|
|
|
int n = this.metaConfigMapper.updateByPrimaryKey(config);
|
|
|
/**
|
|
|
* 清除缓存
|
|
|
*/
|
|
|
/*if(n > 0){
|
|
|
LOGGER.info("清除服务配置缓存(key) ------------->" + config.getCode());
|
|
|
yhRedisTemplate.delete(RedisKeyBuilder.newInstance().appendFixed(config.getCode()));
|
|
|
}*/
|
|
|
return new ApiResponse.ApiResponseBuilder().code(200).data(n).build();
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -80,10 +148,17 @@ public class MetaConfigServiceImpl implements IMetaConfigService, ApplicationCon |
|
|
|
|
|
int count = this.metaConfigMapper.countByCondition(metaConfigReq);
|
|
|
List<MetaConfig> list = null;
|
|
|
if(count >0){
|
|
|
if(count >=0){
|
|
|
list = this.metaConfigMapper.listByCondition(metaConfigReq);
|
|
|
}
|
|
|
|
|
|
if(null != list && list.size() > 0){
|
|
|
list.stream().forEach(item -> {
|
|
|
if(null != item.getCreateTime() && 0 != item.getCreateTime()){
|
|
|
item.setCreateTimeStr(DateUtil.int2DateStr(item.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
PageResponse<MetaConfig> pageResponse = new PageResponse();
|
|
|
pageResponse.setList(list);
|
|
|
pageResponse.setPageNo(metaConfigReq.getPage());
|
...
|
...
|
@@ -133,10 +208,23 @@ public class MetaConfigServiceImpl implements IMetaConfigService, ApplicationCon |
|
|
String[] clazzNames = clazzName.split("\\$");
|
|
|
StringBuffer prefix = new StringBuffer();
|
|
|
// 拼接该属性的完整限定名
|
|
|
Arrays.asList(clazzNames).stream().skip(1).forEach((s) ->
|
|
|
|
|
|
// 本不想代码耦合度太高,无奈之举
|
|
|
Arrays.asList(clazzNames).stream().skip(1).forEach((s) ->{
|
|
|
if(upperCaseClassNameList.contains(s)){
|
|
|
// 下划线分隔,字母全部大写
|
|
|
prefix.append(camelToUnderLine(s).substring(1).toUpperCase())
|
|
|
.append(".");
|
|
|
}else if(splitUnderLineClassNameList.contains(s)){
|
|
|
// 下划线分隔,字母全部小写
|
|
|
prefix.append(camelToUnderLine(s).substring(1))
|
|
|
.append(".");
|
|
|
}else{
|
|
|
prefix.append(Character.toLowerCase(s.charAt(0)))
|
|
|
.append(s.substring(1))
|
|
|
.append("."));
|
|
|
.append(".");
|
|
|
}
|
|
|
});
|
|
|
String pName = prefix.toString() + field.getName();
|
|
|
// 如果该属性完整的限定名包含CollectionName,那么则替换
|
|
|
if(collectionName != null){
|
...
|
...
|
@@ -192,4 +280,21 @@ public class MetaConfigServiceImpl implements IMetaConfigService, ApplicationCon |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 驼峰转下划线
|
|
|
* @param str
|
|
|
* @return
|
|
|
*/
|
|
|
public static String camelToUnderLine(String str) {
|
|
|
Pattern humpPattern = Pattern.compile("[A-Z]");
|
|
|
Matcher matcher = humpPattern.matcher(str);
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
while (matcher.find()) {
|
|
|
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
|
|
|
}
|
|
|
matcher.appendTail(sb);
|
|
|
return sb.toString();
|
|
|
}
|
|
|
} |
...
|
...
|
|