先查询是否存在,存在则更新

This commit is contained in:
hecan 2024-08-28 14:06:50 +08:00
parent a7b25fa268
commit ef85056b11
1 changed files with 124 additions and 123 deletions

View File

@ -213,150 +213,151 @@ public class ComparisonServiceImpl extends BaseService<ComparisonEntity, String>
//根据data_id判断数据是否已经保存在相关表中,避免重复保存
List<String> fields = getFields(comparisonEntity);
Map<String,Object> mapDataId=new HashMap<>();
mapDataId.put("data_id",jsonObjectMain.getString("data_id"));
ComparisonEntity comparisonDataId=new ComparisonEntity();
Map<String, Object> mapDataId = new HashMap<>();
mapDataId.put("data_id", jsonObjectMain.getString("data_id"));
ComparisonEntity comparisonDataId = new ComparisonEntity();
comparisonDataId.setMap(mapDataId);
comparisonDataId.setDbName(comparisonEntity.getDbName());
comparisonDataId.setFields(fields);
List<Map<String, Object>> listDataId = comparisonDao.queryComparisonPage(comparisonDataId);
if(CollectionUtils.isNotEmpty(listDataId)){
return BaseResult.getFailureMessageEntity(comparisonEntity.getDbName()+"表中已经存在data_id为:"+jsonObjectMain.getString("data_id")+"的数据");
}
//判断输入的主表字段是否是必填项
for (ComparisonEntity entity : comparisonEntities) {
//如果必填字段不为空
if(entity.getRuleValue()!=null && entity.getDbType().equals("1")){
if (CollectionUtils.isNotEmpty(listDataId)) {
//return BaseResult.getFailureMessageEntity(comparisonEntity.getDbName()+"表中已经存在data_id为:"+jsonObjectMain.getString("data_id")+"的数据");
updateEntity(json);
} else {
//判断输入的主表字段是否是必填项
for (ComparisonEntity entity : comparisonEntities) {
//如果必填字段不为空
if (entity.getRuleValue() != null && entity.getDbType().equals("1")) {
//如果值为空说明没有传这个字段或者这个字段的值为空
if(jsonObjectMain.getString(entity.getEnName()) ==null || jsonObjectMain.getString(entity.getEnName()).equals("")){
if (jsonObjectMain.getString(entity.getEnName()) == null || jsonObjectMain.getString(entity.getEnName()).equals("")) {
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid,jsonObject.getString("appName"),jsonObject.getString("appCode"),jsonObject.toJSONString(),"接口新增",jsonObject.getString("optionName"),tableName+"_option_log","2");
return BaseResult.getFailureMessageEntity(entity.getDbName()+"表的"+entity.getEnName()+"字段不能为空");
controlsLogDaoimpl.saveControlsLog(uuid, jsonObject.getString("appName"), jsonObject.getString("appCode"), jsonObject.toJSONString(), "接口新增", jsonObject.getString("optionName"), tableName + "_option_log", "2");
return BaseResult.getFailureMessageEntity(entity.getDbName() + "表的" + entity.getEnName() + "字段不能为空");
}
}
}
map.put("id", uuid);
map.put("create_user_id", "c796fd9ba4c9f5ff3cc2fa41a040e443");
map.put("modify_user_id", "c796fd9ba4c9f5ff3cc2fa41a040e443");
map.put("org_id", "0");
map.put("create_time", new Date());
map.put("modify_time", new Date());
map.put("sts", "Y");
map.put("data_status", "Y");//新增状态
map.put("add_status", "0");//新增状态
map.put("update_status", "1");//修改状态
map.put("delete_status", "1");//删除状态
ComparisonEntity comparisonEntityRule = comparisonEntities.get(0);
//查询单据规则 只查询主表得明细表不用
try {
ComparisonEntity comparisonEntityRules = comparisonDao.queryComparisonById(comparisonEntityRule);
String documentRule = comparisonEntityRules.getDocumentRule();//"KH-2024-05-30-1";
logger.info("通用数据保存接口中根据mId查询出来的单据规则为:{}", documentRule);
if (documentRule != null) {
//获取规则前缀 CK
String rulePrefix = documentRule.substring(0, documentRule.indexOf("-"));
//获取规则中间日期 yyyy-MM-dd
String ruleDate = documentRule.substring(documentRule.indexOf("-") + 1, documentRule.lastIndexOf("-"));
String ruleMiddle = getRuleDate(ruleDate);
//获取规则后缀
String ruleSuffixLength = documentRule.substring(documentRule.lastIndexOf("-") + 1);
int ruleSuffix = ruleSuffixLength.length();
//生成单据规则
comparisonEntityRule.setRulePrefix(rulePrefix);
comparisonEntityRule.setRuleMiddle(ruleMiddle);
comparisonEntityRule.setRuleSuffix(ruleSuffix);
try {
//新生成单据规则保存到数据库
ComparisonEntity comparisonEntityRuleGz = comparisonDao.queryComparisonRule(comparisonEntityRule);
map.put("document_rule", comparisonEntityRuleGz.getDocumentRule());//单据规则
map.put("document_rule_num", comparisonEntityRuleGz.getDocumentRuleNum());//单据规则流水号
comparisonEntityRule.setMap(map);
} catch (Exception e) {
logger.info("保存通用数据时候生成单据规则错误:{}", e.getMessage());
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid,jsonObject.getString("appName"),jsonObject.getString("appCode"),jsonObject.toJSONString(),"接口新增",jsonObject.getString("optionName"),tableName+"_option_log","2");
return BaseResult.getFailureMessageEntity("保存失败");
}
}
} catch (Exception e) {
logger.info("通用数据保存接口中查询单据规则错误:{}", e.getMessage());
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid,jsonObject.getString("appName"),jsonObject.getString("appCode"),jsonObject.toJSONString(),"接口新增",jsonObject.getString("optionName"),tableName+"_option_log","2");
return BaseResult.getFailureMessageEntity("保存失败");
}
try {
List<JSONObject> list = new ArrayList<>();
JSONObject jsonObjectList = new JSONObject();
map.put("id", uuid);
map.put("create_user_id", "c796fd9ba4c9f5ff3cc2fa41a040e443");
map.put("modify_user_id", "c796fd9ba4c9f5ff3cc2fa41a040e443");
map.put("org_id", "0");
map.put("create_time", new Date());
map.put("modify_time", new Date());
map.put("sts", "Y");
map.put("data_status", "Y");//新增状态
map.put("add_status", "0");//新增状态
map.put("update_status", "1");//修改状态
map.put("delete_status", "1");//删除状态
ComparisonEntity comparisonEntityRule = comparisonEntities.get(0);
//查询单据规则 只查询主表得明细表不用
try {
ComparisonEntity comparisonEntityRules = comparisonDao.queryComparisonById(comparisonEntityRule);
String documentRule = comparisonEntityRules.getDocumentRule();//"KH-2024-05-30-1";
logger.info("通用数据保存接口中根据mId查询出来的单据规则为:{}", documentRule);
if (documentRule != null) {
//获取规则前缀 CK
String rulePrefix = documentRule.substring(0, documentRule.indexOf("-"));
//获取规则中间日期 yyyy-MM-dd
String ruleDate = documentRule.substring(documentRule.indexOf("-") + 1, documentRule.lastIndexOf("-"));
String ruleMiddle = getRuleDate(ruleDate);
//获取规则后缀
String ruleSuffixLength = documentRule.substring(documentRule.lastIndexOf("-") + 1);
int ruleSuffix = ruleSuffixLength.length();
//生成单据规则
comparisonEntityRule.setRulePrefix(rulePrefix);
comparisonEntityRule.setRuleMiddle(ruleMiddle);
comparisonEntityRule.setRuleSuffix(ruleSuffix);
try {
//新生成单据规则保存到数据库
ComparisonEntity comparisonEntityRuleGz = comparisonDao.queryComparisonRule(comparisonEntityRule);
map.put("document_rule", comparisonEntityRuleGz.getDocumentRule());//单据规则
map.put("document_rule_num", comparisonEntityRuleGz.getDocumentRuleNum());//单据规则流水号
comparisonEntityRule.setMap(map);
} catch (Exception e) {
logger.info("保存通用数据时候生成单据规则错误:{}", e.getMessage());
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid, jsonObject.getString("appName"), jsonObject.getString("appCode"), jsonObject.toJSONString(), "接口新增", jsonObject.getString("optionName"), tableName + "_option_log", "2");
//判断明细表字段是否是必填项
if (arrayList != null && arrayList.size() > 0) {
for (JSONArray jsonArrayDetails : arrayList) {
for (Object object : jsonArrayDetails) {
JSONObject jsonObjectDetails = (JSONObject) JSON.toJSON(object);
for (ComparisonEntity entity : comparisonEntities) {
if(entity.getRuleValue()!=null && entity.getDbType().equals("2")){
//如果值为空说明没有传这个字段或者这个字段的值为空
if(jsonObjectDetails.getString(entity.getEnName())==null || jsonObjectDetails.getString(entity.getEnName()).equals("")){
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid,jsonObject.getString("appName"),jsonObject.getString("appCode"),jsonObject.toJSONString(),"接口新增",jsonObject.getString("optionName"),tableName+"_option_log","2");
return BaseResult.getFailureMessageEntity(entity.getDbName()+"表的"+entity.getEnName()+"字段不能为空");
return BaseResult.getFailureMessageEntity("保存失败");
}
}
} catch (Exception e) {
logger.info("通用数据保存接口中查询单据规则错误:{}", e.getMessage());
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid, jsonObject.getString("appName"), jsonObject.getString("appCode"), jsonObject.toJSONString(), "接口新增", jsonObject.getString("optionName"), tableName + "_option_log", "2");
return BaseResult.getFailureMessageEntity("保存失败");
}
try {
List<JSONObject> list = new ArrayList<>();
JSONObject jsonObjectList = new JSONObject();
//判断明细表字段是否是必填项
if (arrayList != null && arrayList.size() > 0) {
for (JSONArray jsonArrayDetails : arrayList) {
for (Object object : jsonArrayDetails) {
JSONObject jsonObjectDetails = (JSONObject) JSON.toJSON(object);
for (ComparisonEntity entity : comparisonEntities) {
if (entity.getRuleValue() != null && entity.getDbType().equals("2")) {
//如果值为空说明没有传这个字段或者这个字段的值为空
if (jsonObjectDetails.getString(entity.getEnName()) == null || jsonObjectDetails.getString(entity.getEnName()).equals("")) {
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid, jsonObject.getString("appName"), jsonObject.getString("appCode"), jsonObject.toJSONString(), "接口新增", jsonObject.getString("optionName"), tableName + "_option_log", "2");
return BaseResult.getFailureMessageEntity(entity.getDbName() + "表的" + entity.getEnName() + "字段不能为空");
}
}
}
}
}
}
}
//保存主表数据
ComparisonEntity comparisonEntityOne = comparisonDao.saveComparison(comparisonEntityRule);
jsonObjectList.put(comparisonEntityOne.getDbName(),map);
//保存子表数据
if (arrayList != null && arrayList.size() > 0) {
mapDetails = spliceDetailMap(comparisonEntities, mapDetails, arrayList);
if(mapDetails.size()>0 && mapDetails!=null ){
for (List<Map<String, Object>> mapDetail : mapDetails) {
for (Map<String, Object> stringObjectMap : mapDetail) {
stringObjectMap.put("id", UUIDLong.longUUID());
stringObjectMap.put("formmain_id", uuid);
stringObjectMap.put("create_user_id", "c796fd9ba4c9f5ff3cc2fa41a040e443");
stringObjectMap.put("modify_user_id", "c796fd9ba4c9f5ff3cc2fa41a040e443");
stringObjectMap.put("org_id", "0");
stringObjectMap.put("create_time", new Date());
stringObjectMap.put("modify_time", new Date());
stringObjectMap.put("sts", "Y");
stringObjectMap.put("data_status", "Y");//新增状态
comparisonDetailsEntity.setMapDetails(stringObjectMap);
comparisonDetailsEntity.setDbName(String.valueOf(stringObjectMap.get("table_name")));
stringObjectMap.remove("table_name");
ComparisonDetailsEntity comparisonDetailsEntityOne = comparisonDetailsDao.saveComparisonDetails(comparisonDetailsEntity);
comparisonEntityOne.setComparisonDetailsEntity(comparisonDetailsEntityOne);
//保存操作日志
//controlsLogDaoimpl.saveControlsLog(uuidDetail,jsonObject.getString("appName"),jsonObject.getString("appCode"),jsonObject.toJSONString(),"接口新增",jsonObject.getString("optionName"),comparisonDetailsEntity.getDbName()+"_option_log");
//保存主表数据
ComparisonEntity comparisonEntityOne = comparisonDao.saveComparison(comparisonEntityRule);
jsonObjectList.put(comparisonEntityOne.getDbName(), map);
//保存子表数据
if (arrayList != null && arrayList.size() > 0) {
mapDetails = spliceDetailMap(comparisonEntities, mapDetails, arrayList);
if (mapDetails.size() > 0 && mapDetails != null) {
for (List<Map<String, Object>> mapDetail : mapDetails) {
for (Map<String, Object> stringObjectMap : mapDetail) {
stringObjectMap.put("id", UUIDLong.longUUID());
stringObjectMap.put("formmain_id", uuid);
stringObjectMap.put("create_user_id", "c796fd9ba4c9f5ff3cc2fa41a040e443");
stringObjectMap.put("modify_user_id", "c796fd9ba4c9f5ff3cc2fa41a040e443");
stringObjectMap.put("org_id", "0");
stringObjectMap.put("create_time", new Date());
stringObjectMap.put("modify_time", new Date());
stringObjectMap.put("sts", "Y");
stringObjectMap.put("data_status", "Y");//新增状态
comparisonDetailsEntity.setMapDetails(stringObjectMap);
comparisonDetailsEntity.setDbName(String.valueOf(stringObjectMap.get("table_name")));
stringObjectMap.remove("table_name");
ComparisonDetailsEntity comparisonDetailsEntityOne = comparisonDetailsDao.saveComparisonDetails(comparisonDetailsEntity);
comparisonEntityOne.setComparisonDetailsEntity(comparisonDetailsEntityOne);
//保存操作日志
//controlsLogDaoimpl.saveControlsLog(uuidDetail,jsonObject.getString("appName"),jsonObject.getString("appCode"),jsonObject.toJSONString(),"接口新增",jsonObject.getString("optionName"),comparisonDetailsEntity.getDbName()+"_option_log");
}
}
jsonObjectList.put(comparisonDetailsEntity.getDbName(), mapDetails);
}
jsonObjectList.put(comparisonDetailsEntity.getDbName(), mapDetails);
} else {
jsonObjectList.put(comparisonDetailsEntity.getDbName(), new ArrayList<>());
}
} else {
jsonObjectList.put(comparisonDetailsEntity.getDbName(), new ArrayList<>());
}
jsonObjectList.remove(null);
list.add(jsonObjectList);
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid,jsonObject.getString("appName"),jsonObject.getString("appCode"),jsonObject.toJSONString(),"接口新增",jsonObject.getString("optionName"),tableName+"_option_log","1");
JSONObject json1 = new JSONObject();
json1.put("status","200");
json1.put("list",list);
return json1;
} catch (Exception e) {
logger.info("保存通用数据时候错误:{}", e.getMessage());
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid,jsonObject.getString("appName"),jsonObject.getString("appCode"),jsonObject.toJSONString(),"接口新增",jsonObject.getString("optionName"),tableName+"_option_log","2");
jsonObjectList.remove(null);
list.add(jsonObjectList);
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid, jsonObject.getString("appName"), jsonObject.getString("appCode"), jsonObject.toJSONString(), "接口新增", jsonObject.getString("optionName"), tableName + "_option_log", "1");
JSONObject json1 = new JSONObject();
json1.put("status", "200");
json1.put("list", list);
return json1;
} catch (Exception e) {
logger.info("保存通用数据时候错误:{}", e.getMessage());
//保存操作日志
controlsLogDaoimpl.saveControlsLog(uuid, jsonObject.getString("appName"), jsonObject.getString("appCode"), jsonObject.toJSONString(), "接口新增", jsonObject.getString("optionName"), tableName + "_option_log", "2");
return BaseResult.getFailureMessageEntity("保存失败");
return BaseResult.getFailureMessageEntity("保存失败");
}
}
}
}