Commit bdcb861a by ThomasAdv

数据上报接口代码审查与增加注释

1 parent 1a17bba5
......@@ -42,7 +42,7 @@ public interface UserCenterFeignClient {
* 注意: @RequestParam("xx") 注解一定要写,而且属性名xx不能省略
*/
@GetMapping("/echo")
String echo(@RequestParam String message);
String echo(@RequestParam("message") String message);
@GetMapping(value = "/info/{id}")
UserDTO info(@PathVariable("id") Long id);
......
package cn.com.glsx.cdp.common.constant;
public class ModelConstants {
public final static String CUSTOMER_MODEL_NAME = "cdp_customer";
public final static String CAR_MODEL_NAME = "cdp_car";
}
......@@ -37,7 +37,7 @@ public class UserApiController implements UserCenterFeignClient {
@Operation(summary = "FeignClient调用")
@Override
@GetMapping("/echo")
public String echo(@RequestParam String message) {
public String echo(@RequestParam("message") String message) {
String echo = echoCenterFeignClient.echo(message);
return echo;
}
......
package cn.com.glsx.cdp.modules.enums;
import org.apache.commons.lang.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public enum OneIdBelongEnum {
CUSTOMER((byte)1, "oneId", "客户"),
CAR((byte)2, "carId", "车");
CUSTOMER((byte)1, "oneId", "cdp_customer", "客户"),
CAR((byte)2, "carId", "cdp_car", "车");
private final Byte value;
private final String oneIdFieldName;
private final String modelName;
private final String displayText;
OneIdBelongEnum(Byte value, String oneIdFieldName, String displayText){
OneIdBelongEnum(Byte value, String oneIdFieldName, String modelName, String displayText){
this.value = value;
this.oneIdFieldName = oneIdFieldName;
this.modelName = modelName;
this.displayText = displayText;
}
......@@ -22,6 +30,10 @@ public enum OneIdBelongEnum {
return oneIdFieldName;
}
public String getModelName() {
return modelName;
}
public String getDisplayText() {
return displayText;
}
......@@ -36,4 +48,19 @@ public enum OneIdBelongEnum {
}
return null;
}
public static List<String> getModelNames(){
return Arrays.stream(OneIdBelongEnum.values()).map(OneIdBelongEnum::getModelName).collect(Collectors.toList());
}
public static OneIdBelongEnum getByModelName(String modelName){
if (StringUtils.isNotBlank(modelName)) {
for (OneIdBelongEnum oneIdBelongEnum : OneIdBelongEnum.values()) {
if (oneIdBelongEnum.getModelName().equals(modelName)) {
return oneIdBelongEnum;
}
}
}
return null;
}
}
......@@ -20,7 +20,10 @@ import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
......@@ -44,6 +47,9 @@ public class DataReportRabbitMQConsumer {
private ModelService modelService;
@Autowired
private OneIdService oneIdService;
@Lazy
@Autowired
private DataReportRabbitMQConsumer thiz;
/**
* 处理数据上报
......@@ -67,9 +73,9 @@ public class DataReportRabbitMQConsumer {
Integer addWay = jsonData.getInteger(ModelFieldConstants.ADD_WAY);
if (orgId != null && addWay != null) {
List<ModelDTO> matchModels = matchModel(jsonData);
Map<OneIdBelongEnum, String> oneIdMap = getOneId(matchModels, jsonData);
Map<OneIdBelongEnum, String> oneIdMap = thiz.getOneId(matchModels, jsonData);
List<ModelDTO> valueModels = compileModelFieldsValue(matchModels, jsonData, oneIdMap);
valueModels.forEach(this::handleModel);
valueModels.forEach(thiz::handleModel);
} else {
log.error("data report consume error,orgId or addWay is null");
}
......@@ -93,7 +99,8 @@ public class DataReportRabbitMQConsumer {
* @author caiym
* @date 2023年09月15日 15:45:10
*/
private void handleModel(ModelDTO modelDTO) {
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public void handleModel(ModelDTO modelDTO) {
if (CollectionUtils.isNotEmpty(modelDTO.getModelFieldMappings())) {
Map<String, Object> existModelData = getExistModelData(modelDTO);
if (existModelData != null) {
......@@ -116,9 +123,11 @@ public class DataReportRabbitMQConsumer {
* @date 2023年09月15日 17:14:42
*/
private void updateModelData(ModelDTO modelDTO, Map<String, Object> existModelData) {
log.info("updateModelData, modelDTO:{}, existModelData:{}", modelDTO, existModelData);
if (CollectionUtils.isNotEmpty(modelDTO.getModelFieldMappings())) {
Map<String, Object> fieldMap = mergeModelData(modelDTO, existModelData);
String updateSql = buildUpdateSql(modelDTO, fieldMap);
log.debug("updateModelData, updateSql:{}", updateSql);
modelService.updateTable(updateSql);
}else{
log.warn("model field mapping is empty, model:{}", modelDTO);
......@@ -140,8 +149,13 @@ public class DataReportRabbitMQConsumer {
Map<String, Object> mergeResultMap = new HashMap<>();
for (Map.Entry<String, ModelFieldMappingDTO> newDataMapEntry : newDataMap.entrySet()) {
String fieldName = newDataMapEntry.getKey();
mergeResultMap.put(fieldName, mergeField(newDataMapEntry.getValue(), existModelData.get(fieldName)));
log.debug("mergeModelData, fieldName:{}, oldValue:{}, new Value:{}", fieldName, existModelData.get(fieldName), newDataMapEntry.getValue());
Object mergedField = mergeField(newDataMapEntry.getValue(), existModelData.get(fieldName));
log.debug("mergeModelData, fieldName:{}, oldValue:{}, new Value:{}, result:{}", fieldName, existModelData.get(fieldName),
newDataMapEntry.getValue(), mergedField);
mergeResultMap.put(fieldName, mergedField);
}
log.debug("mergeModelData, result:{}", mergeResultMap);
return mergeResultMap;
}
......@@ -270,6 +284,7 @@ public class DataReportRabbitMQConsumer {
* @date 2023年09月15日 17:14:29
*/
private void addModelData(ModelDTO modelDTO) {
log.info("addModelData, modelDTO:{}", modelDTO);
if (CollectionUtils.isNotEmpty(modelDTO.getModelFieldMappings())) {
JSONObject exJson = new JSONObject();
List<ModelFieldMappingDTO> normalFields = new ArrayList<>();
......@@ -295,6 +310,7 @@ public class DataReportRabbitMQConsumer {
valBuilder.append("now()").append(",now()").append(",0").append(")");
StringBuilder sqlBuilder = new StringBuilder();
String insertSql = sqlBuilder.append("insert into ").append(modelDTO.getModelName()).append(fieldBuilder).append(" values ").append(valBuilder).toString();
log.debug("addModelData, insertSql:{}", insertSql);
modelService.insertTable(insertSql);
}else{
log.warn("model field mapping is empty, model:{}", modelDTO);
......@@ -416,15 +432,23 @@ public class DataReportRabbitMQConsumer {
private List<ModelDTO> compileModelFieldsValue(List<ModelDTO> matchModels, JSONObject jsonData, Map<OneIdBelongEnum, String> oneIdMap) {
if (CollectionUtils.isNotEmpty(matchModels) && jsonData != null) {
for (ModelDTO model : matchModels) {
OneIdBelongEnum oneIdBelongEnum = OneIdBelongEnum.getByValue(model.getOneidBelong());
String oneId = oneIdMap.get(oneIdBelongEnum);
String oneId = null;
OneIdBelongEnum oneIdBelongEnum;
if (OneIdBelongEnum.getModelNames().contains(model.getModelName())) {
oneIdBelongEnum = OneIdBelongEnum.getByModelName(model.getModelName());
}else{
oneIdBelongEnum = OneIdBelongEnum.getByValue(model.getOneidBelong());
}
log.info("oneIdBelongEnum model:{}", oneIdBelongEnum.getModelName());
oneId = oneIdMap.get(oneIdBelongEnum);
if (StringUtils.isBlank(oneId)) {
log.error("oneId is blank, stop dealing model field value filling, model:{}, jsonData:{}", model.getModelName(), jsonData);
continue;
}
if (CollectionUtils.isNotEmpty(model.getModelFieldMappings())) {
for (ModelFieldMappingDTO modelFieldMapping : model.getModelFieldMappings()) {
if (oneIdBelongEnum != null && oneIdBelongEnum.getOneIdFieldName().equals(modelFieldMapping.getFieldName())) {
if (oneIdBelongEnum != null
&& oneIdBelongEnum.getOneIdFieldName().equals(modelFieldMapping.getFieldName())) {
modelFieldMapping.setFieldValue(oneId);
}
modelFieldMapping.setFieldValue(jsonData.get(modelFieldMapping.getFieldName()));
......@@ -432,6 +456,7 @@ public class DataReportRabbitMQConsumer {
}
}
}
log.debug("compileModelFieldsValue, return:{}", matchModels);
return matchModels;
}
......@@ -445,22 +470,22 @@ public class DataReportRabbitMQConsumer {
* @author caiym
* @date 2023年09月15日 15:00:07
*/
private Map<OneIdBelongEnum, String> getOneId(List<ModelDTO> matchModels, JSONObject jsonData) {
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public Map<OneIdBelongEnum, String> getOneId(List<ModelDTO> matchModels, JSONObject jsonData) {
if (CollectionUtils.isNotEmpty(matchModels) && jsonData != null) {
Map<OneIdBelongEnum, String> oneIdBelongEnumStringMap = new HashMap<>();
Map<Byte, ModelDTO> oneIdBelongGroupModelMap = matchModels.stream().filter(modelDTO -> modelDTO.getOneidBelong() != null).collect(Collectors.toMap(ModelDTO::getOneidBelong, o -> o, (o1, o2) -> o2));
for (Byte oneIdBelong : oneIdBelongGroupModelMap.keySet()) {
if (oneIdBelong != null) {
OneIdBelongEnum oneIdBelongEnum = OneIdBelongEnum.getByValue(oneIdBelong);
if (oneIdBelongEnum == null) {
throw new RpcServiceException("none or undefined oneIdBelong:"+oneIdBelong);
}
Map<String, Object> oneIdFieldMap = oneIdBelongGroupModelMap.get(oneIdBelong).getModelFieldMappings().stream()
.filter(o -> o.getOneidFlag() != null && o.getOneidFlag())
for (ModelDTO matchModel : matchModels) {
if (OneIdBelongEnum.getModelNames().contains(matchModel.getModelName())) {
log.info("get model:{} oneId", matchModel.getModelName());
Map<String, Object> oneIdFieldMap = matchModel.getModelFieldMappings().stream()
.filter(o -> o.getOneidFlag() != null && o.getOneidFlag() && jsonData.get(o.getFieldName()) != null)
.map(o->{o.setFieldValue(jsonData.get(o.getFieldName()));return o;})
.collect(
Collectors.toMap(ModelFieldMappingDTO::getFieldName, o -> o.getFieldValue(), (o1, o2) -> o2)
);
FindOrCreateOneIdParam param = FindOrCreateOneIdParam.builder().orgId(jsonData.getString(ModelFieldConstants.ORGID)).businessType(oneIdBelong).oneIdFieldJson(JSONObject.toJSONString(oneIdFieldMap)).build();
OneIdBelongEnum oneIdBelongEnum = OneIdBelongEnum.getByModelName(matchModel.getModelName());
FindOrCreateOneIdParam param = FindOrCreateOneIdParam.builder().orgId(jsonData.getString(ModelFieldConstants.ORGID))
.businessType(oneIdBelongEnum.getValue()).oneIdFieldJson(JSONObject.toJSONString(oneIdFieldMap)).build();
String oneId = oneIdService.findOrCreateOneId(param);
oneIdBelongEnumStringMap.put(oneIdBelongEnum, oneId);
}
......@@ -485,11 +510,15 @@ public class DataReportRabbitMQConsumer {
for (ModelDTO model : models) {
// 没有匹配的模型字段则跳过
if (hasMatchField(model, jsonData)) {
log.info("未匹配模型:{}任何字段,跳过", model.getModelName());
continue;
}
List<ModelFieldMappingDTO> modelFieldMappings = model.getModelFieldMappings();
if (CollectionUtils.isNotEmpty(modelFieldMappings)) {
List<ModelFieldMappingDTO> oneIdFields = modelFieldMappings.stream().filter(modelFieldMapping -> modelFieldMapping.getOneidFlag() != null && modelFieldMapping.getOneidFlag()).collect(Collectors.toList());
List<ModelFieldMappingDTO> oneIdFields = modelFieldMappings.stream()
.filter(modelFieldMapping -> modelFieldMapping.getOneidFlag() != null
&& modelFieldMapping.getOneidFlag())
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(oneIdFields)) {
// 匹配oneid字段
if (matchOneIdField(oneIdFields, jsonData)) {
......@@ -517,13 +546,19 @@ public class DataReportRabbitMQConsumer {
* @date 2023年09月15日 14:32:10
*/
private Boolean matchModelFlagField(List<ModelFieldMappingDTO> modelFieldMappings, JSONObject jsonData) {
List<ModelFieldMappingDTO> modelFlagFields = modelFieldMappings.stream().filter(modelFieldMapping -> modelFieldMapping.getModelFlag() != null && modelFieldMapping.getModelFlag()).collect(Collectors.toList());
List<ModelFieldMappingDTO> modelFlagFields = modelFieldMappings.stream()
.filter(modelFieldMapping -> modelFieldMapping.getModelFlag() != null
&& modelFieldMapping.getModelFlag())
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(modelFlagFields)) {
for (ModelFieldMappingDTO modelFieldMapping : modelFlagFields) {
String fieldName = modelFieldMapping.getFieldName();
Object fieldValue = jsonData.get(fieldName);
if (fieldValue instanceof String && StringUtils.isNotBlank((String) fieldValue)
|| fieldValue != null) {
if (fieldValue instanceof String) {
if (StringUtils.isNotBlank((String) fieldValue)) {
return true;
}
}else if(fieldValue != null){
return true;
}
}
......@@ -545,8 +580,11 @@ public class DataReportRabbitMQConsumer {
for (ModelFieldMappingDTO oneIdField : oneIdFields) {
String fieldName = oneIdField.getFieldName();
Object fieldValue = jsonData.get(fieldName);
if (fieldValue instanceof String && StringUtils.isNotBlank((String) fieldValue)
|| fieldValue != null) {
if (fieldValue instanceof String) {
if (StringUtils.isNotBlank((String) fieldValue)) {
return true;
}
}else if(fieldValue != null){
return true;
}
}
......
......@@ -44,7 +44,10 @@ public class OneIdMappingService {
return null;
}
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public void saveOneIdMapping(String oneId, OneIdFieldConfigDTO oneIdFieldConfigDTO){
log.info("saveOneIdMapping, oneId:{}, oneIdFieldConfigDTO:{}", oneId, oneIdFieldConfigDTO);
RpcAssert.assertNotNull(oneIdFieldConfigDTO, DefaultErrorEnum.DATA_NULL,"oneIdFieldConfigDTO not be null");
RpcAssert.assertNotNull(oneIdFieldConfigDTO.getOrgId(), DefaultErrorEnum.DATA_NULL,"oneIdFieldConfigDTO.getOrgId() not be null");
RpcAssert.assertNotNull(oneIdFieldConfigDTO.getBusinessType(), DefaultErrorEnum.DATA_NULL,"oneIdFieldConfigDTO.getBusinessType() not be null");
......
......@@ -11,6 +11,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
......@@ -28,6 +29,9 @@ public class OneIdService {
private OneIdMappingService oneIdMappingService;
@Autowired
private OneIdFieldConfigService oneIdFieldConfigService;
@Lazy
@Autowired
private OneIdService thiz;
/**
......@@ -38,7 +42,9 @@ public class OneIdService {
* @author caiym
* @date 2023年09月21日 16:07:51
*/
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public String findOrCreateOneId(FindOrCreateOneIdParam param){
log.info("findOrCreateOneId, param:{}", param);
RpcAssert.assertNotNull(param, DefaultErrorEnum.DATA_NULL,"param not be null");
RpcAssert.assertNotNull(param.getOrgId(), DefaultErrorEnum.DATA_NULL,"param.getOrgId() not be null");
RpcAssert.assertNotNull(param.getBusinessType(), DefaultErrorEnum.DATA_NULL,"param.getBusinessType() not be null");
......@@ -48,7 +54,7 @@ public class OneIdService {
String finalOneId = null;
if (CollectionUtils.isNotEmpty(readyMatchOneIdFieldConfigs)) {
for (OneIdFieldConfigDTO readyMatchOneIdFieldConfig : readyMatchOneIdFieldConfigs) {
finalOneId = findOrCreateOneId(finalOneId, readyMatchOneIdFieldConfig);
finalOneId = thiz.findOrCreateOneId(finalOneId, readyMatchOneIdFieldConfig);
}
}else{
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "none match oneId field config, param: " + JSONObject.toJSONString(param));
......@@ -66,11 +72,14 @@ public class OneIdService {
* @author caiym
* @date 2023年09月21日 16:53:44
*/
@Transactional(propagation= Propagation.REQUIRED,rollbackFor=RpcServiceException.class)
public String findOrCreateOneId(String finalOneId, OneIdFieldConfigDTO readyMatchOneIdFieldConfig){
log.info("findOrCreateOneId,finalOneId:{},fieldName:{}", readyMatchOneIdFieldConfig.getFieldName());
OneIdMappingDTO oneIdMapping = oneIdMappingService.getOneIdMapping(readyMatchOneIdFieldConfig.getOrgId(),
readyMatchOneIdFieldConfig.getBusinessType(), readyMatchOneIdFieldConfig.getFieldName());
if (oneIdMapping == null) {
if (StringUtils.isBlank(finalOneId)) {
log.info("generate new oneId,fieldName:{}", readyMatchOneIdFieldConfig.getFieldName());
//TODO 雪花算法
finalOneId = UUID.randomUUID().toString().replace("-", "");
}
......
......@@ -25,7 +25,7 @@
<result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime" />
<result column="del_flag" jdbcType="INTEGER" property="delFlag" />
</resultMap>
<resultMap id="BOResultMap" type="cn.com.glsx.cdp.modules.model.CustomerSopJourneyBO">
<resultMap id="BOResultMap" type="cn.com.glsx.cdp.modules.model.CustomerSopJourneyBO" extends="BaseResultMap">
<association property="customer" javaType="cn.com.glsx.cdp.modules.model.CustomerBO">
<result column="org_id" jdbcType="VARCHAR" property="orgId" />
<result column="one_id" jdbcType="VARCHAR" property="oneId" />
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!