解决map中值为null转成''的问题

dev
wangxuanran 2023-06-30 15:14:57 +08:00
parent d5d32720b9
commit a461733216
11 changed files with 99 additions and 145 deletions

View File

@ -268,7 +268,8 @@ public class SqlHandler {
}
Object o = ((Map<?, ?>) arg).get(key);
if (null == o) {
return "";
// 如果值是null 则直接返回hull
return null;
}
if (o instanceof Character || o instanceof String) {
// 处理字符类型

View File

@ -10,15 +10,13 @@ import org.apache.axis2.databinding.types.xsd._float;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.tools.ant.taskdefs.Pack;
import weaver.formmode.data.ModeDataIdUpdate;
import weaver.formmode.setup.ModeRightInfo;
import weaver.xuanran.wang.common.mapper.CommonMapper;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
@ -102,7 +100,10 @@ public class CusData2OA {
return modelDataList;
}
public static void baseInsertAndUpdate(String modelId, List<Map<String, Object>> params, CountDownLatch latch){
public static boolean baseInsertAndUpdate(String modelId, List<Map<String, Object>> params, CountDownLatch latch){
if(CollectionUtils.isEmpty(params)){
return true;
}
int modelIdInt = Util.getIntValue(modelId, -1);
String tableName = CommonUtil.checkModelId(modelIdInt);
if(modelIdInt < 0 || StringUtils.isBlank(tableName)){
@ -127,21 +128,23 @@ public class CusData2OA {
updateSql = buildUpdateSql(tableName, param);
}
if(StringUtils.isBlank(insertSql)){
insertSql = buildInsertSql(tableName, modelId, param);
insertSql = buildInsertSql(tableName, param);
}
}
try {
if(CollectionUtils.isNotEmpty(updateParams)){
if (!commonMapper.updateModelInfoList(updateSql, params)) {
if (!commonMapper.updateModelInfoList(updateSql, updateParams)) {
throw new CustomerException("update model data sql execute error!");
}
}
if(CollectionUtils.isNotEmpty(insertParams)){
if (!commonMapper.batchInsertModel(insertSql, params)) {
if (!commonMapper.batchInsertModel(insertSql, insertParams)) {
throw new CustomerException("insert model data sql execute error!");
}
}
return true;
}catch (Exception e){
log.error("更新数据失败! : " + e.getMessage());
throw new CustomerException(e);
}finally {
if(latch != null){
@ -153,10 +156,12 @@ public class CusData2OA {
}
public static String buildUpdateSql(String tableName, Map<String, Object> params) {
Map<String, Object> copy = new HashMap<>(params);
copy.remove("id");
StringBuilder sqlSb = new StringBuilder("update ")
.append(tableName)
.append(" set ");
for (Map.Entry<String, Object> entry : params.entrySet()) {
for (Map.Entry<String, Object> entry : copy.entrySet()) {
sqlSb.append(entry.getKey())
.append(" = #{item.")
.append(entry.getKey())
@ -167,13 +172,15 @@ public class CusData2OA {
return sqlSb.toString();
}
public static String buildInsertSql(String tableName, String modelId,Map<String, Object> params) {
public static String buildInsertSql(String tableName, Map<String, Object> params) {
Map<String, Object> copy = new HashMap<>(params);
copy.remove("id");
StringBuilder sqlSb = new StringBuilder("insert into ")
.append(tableName)
.append(" (");
StringBuilder fields = new StringBuilder();
StringBuilder values = new StringBuilder();
for (Map.Entry<String, Object> entry : params.entrySet()) {
for (Map.Entry<String, Object> entry : copy.entrySet()) {
fields.append(entry.getKey()).append(",");
values.append("#{item.")
.append(entry.getKey())

View File

@ -6,6 +6,7 @@ import aiyh.utils.annotation.ActionDesc;
import aiyh.utils.annotation.ActionOptionalParam;
import aiyh.utils.annotation.PrintParamMark;
import aiyh.utils.annotation.RequiredMark;
import com.alibaba.fastjson.JSONObject;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;
import weaver.xuanran.wang.cssc.cms.entity.CusSuccess;
@ -59,7 +60,7 @@ public class WorkflowToCms extends SafeCusBaseAction {
.errorMsg(Util.null2DefaultStr(msg, "error"))
.dataKey(Util.null2DefaultStr(dataKey, ""))
.build();
log.info("");
log.info("cmsResponseVoField : " + JSONObject.toJSONString(cmsResponseVoField));
CusSuccess tokenSuccess = CusSuccess
.builder()
@ -68,6 +69,7 @@ public class WorkflowToCms extends SafeCusBaseAction {
.errorMsg(Util.null2DefaultStr(tokenMsg, "error"))
.dataKey(Util.null2DefaultStr(tokenDataKey,""))
.build();
log.info("tokenSuccess : " + JSONObject.toJSONString(tokenSuccess));
workflowToCmsService.workflowToCms(onlyMark,tokenOnlyMark, billTable, requestId,cmsResponseVoField, tokenSuccess);
}

View File

@ -4,7 +4,6 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import weaver.xuanran.wang.sh_bigdata.common.service.CusDataDecipher;
/**
@ -23,5 +22,4 @@ public class CusSuccess {
private String errorMsg;
private String dataKey;
private Object response;
private CusDataDecipher cusDataDecipher;
}

View File

@ -1,17 +1,24 @@
package weaver.xuanran.wang.cssc.cms.service.impl;
import aiyh.utils.Util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.xiao.commons.config.entity.RequestMappingConfig;
import weaver.xiao.commons.config.entity.ResponseMapping;
import weaver.xiao.commons.config.service.DealWithMapping;
import weaver.xiao.commons.utils.SqlUtil;
import weaver.xuanran.wang.common.util.CommonUtil;
import weaver.xuanran.wang.cssc.cms.entity.CusSuccess;
import weaver.xuanran.wang.cssc.cms.service.WorkflowToCmsService;
import weaver.xuanran.wang.cssc.cms.util.RequestMasterPlate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -24,6 +31,7 @@ public class WorkFlowToCmsServiceImpl implements WorkflowToCmsService {
private final DealWithMapping dealWithMapping = new DealWithMapping();
private final Logger log = Util.getLogger(); // 获取日志对象
private final RequestMasterPlate requestMasterPlate = new RequestMasterPlate();
private final SqlUtil sqlUtil = new SqlUtil();
@Override
public String getToken(String onlyMark,String billTable,
String requestId, CusSuccess cusSuccess) {
@ -57,7 +65,20 @@ public class WorkFlowToCmsServiceImpl implements WorkflowToCmsService {
String url = requestMappingConfig.getRequestUrl();
dealWithMapping.setMainTable(billTable);
Map<String, Object> param = dealWithMapping.getRequestParam(recordSet, requestMappingConfig);
requestMasterPlate.apiPost(url, param, headers, cusSuccess);
Map<String, Object> result = requestMasterPlate.apiPost(url, param, headers, cusSuccess);
List<ResponseMapping> responseMappingList = requestMappingConfig.getResponseMappingList();
if(CollectionUtils.isEmpty(responseMappingList) || MapUtils.isEmpty(result)){
return;
}
Map<String, Map<String, Object>> writeBackMessage = dealWithMapping.dealResponse(responseMappingList, result);
log.info("回写信息 writeBackMessage ==>"+ JSON.toJSONString(writeBackMessage));
Map<String, Object> updateMsg = writeBackMessage.get(billTable);
Map<String,Object> whereParam = new HashMap<>();
whereParam.put("requestid",requestId);
boolean success = sqlUtil.updateMode(billTable, updateMsg, whereParam);
if(!success){
log.error("数据回写表单失败!");
}
}
public RecordSet initRs( RequestMappingConfig requestMappingConfig, String billTable, String requestId){

View File

@ -53,12 +53,7 @@ public class RequestMasterPlate {
responseVo.getEntityString())); // 相应内容
throw new CustomerException(Util.logStr("can not fetch [{}]", url)); // 自定义异常类 create 2022/3/9 2:20 PM 构建日志字符串
}
Map<String, Object> response;
if(cusSuccess.getCusDataDecipher() != null){
response = cusSuccess.getCusDataDecipher().decoder(responseVo);
}else {
response = responseVo.getResponseMap(); // 根据相应结果转化为map集合
}
Map<String, Object> response = responseVo.getResponseMap(); // 根据相应结果转化为map集合
cusSuccess.setResponse(response);
String responseValue = Util.null2DefaultStr(response.get(cusSuccess.getSuccessField()), "");
if (!cusSuccess.getSuccessValue().equals(responseValue)) {

View File

@ -55,4 +55,8 @@ public interface DataAsyncMapper {
@Select(custom = true)
String selectCustomerSql(@SqlString String sql, Map<String, Object> map);
@Select("select id from $t{tableName} where modedatacreatedate = #{date}")
List<Integer> selectDataIdListByCreateDate(@ParamMapper("tableName") String tableName,
@ParamMapper("date") String date);
}

View File

@ -4,17 +4,14 @@ import aiyh.utils.ThreadPoolConfig;
import aiyh.utils.Util;
import aiyh.utils.excention.CustomerException;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.cube.service.ModeAppService;
import com.engine.cube.service.impl.ModeAppServiceImpl;
import io.swagger.models.auth.In;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import weaver.formmode.setup.ModeRightInfoThread;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.hrm.User;
import weaver.xuanran.wang.common.entity.CusSuccess;
import weaver.xuanran.wang.common.util.CommonUtil;
import weaver.xuanran.wang.common.util.CusData2OA;
@ -24,10 +21,6 @@ import weaver.xuanran.wang.eny.data_async.entity.DataAsyncConfigMain;
import weaver.xuanran.wang.eny.data_async.mapper.DataAsyncMapper;
import weaver.xuanran.wang.eny.data_async.util.EyDataAsyncTokenUtil;
import weaver.xuanran.wang.eny.data_async.util.ValueRuleMethod;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
@ -56,6 +49,8 @@ public class DataAsyncServiceImpl {
.errorMsg("msg")
.build();
private static final ModeRightInfo moderightinfo = new ModeRightInfo();
private int pageSize = 100;
private int pageNo = 1;
@ -155,7 +150,8 @@ public class DataAsyncServiceImpl {
threadPoolInstance.execute(()->{
log.info("=======================================================================================");
log.info(Thread.currentThread().getName() + " 入库之前信号数量 : " + finalLatch.getCount());
CusData2OA.baseInsertAndUpdate(config.getModel_id(), maps, finalLatch);
boolean success = CusData2OA.baseInsertAndUpdate(config.getModel_id(), maps, finalLatch);
log.info("数据库更新标识 : " + success);
log.info(Thread.currentThread().getName() + " 入库之后信号数量 : " + finalLatch.getCount());
log.info("=======================================================================================");
});
@ -171,9 +167,17 @@ public class DataAsyncServiceImpl {
if(!await){
throw new CustomerException("线程等待时间超过最大时间限制!");
}
long endTime = System.currentTimeMillis();
// 查询当天同步的数据
// 应该在上面 每一批数据处理完直接权限重构
List<Integer> dataIdList = asyncMapper.selectDataIdListByCreateDate(config.getTable_name(), TimeUtil.getCurrentDateString());
if(CollectionUtils.isNotEmpty(dataIdList)){
log.info("需要权限重构的数据条数 : " + dataIdList.size());
for (Integer id : dataIdList) {
moderightinfo.rebuildModeDataShareByEdit(1, Util.getIntValue(config.getModel_id(),-1), id);
}
}
log.info("数据结束同步时间 : " + TimeUtil.getCurrentTimeString());
rebuildRight(Util.getIntValue(config.getModel_id()));
long endTime = System.currentTimeMillis();
log.info("同步耗时时间 " + (endTime - startTime) / 1000 + " s");// 等待所有转换操作完成
}
} catch (Exception e) {
@ -194,24 +198,24 @@ public class DataAsyncServiceImpl {
List<Map<String, Object>> maps = new ArrayList<>();
if(CollectionUtils.isNotEmpty(primaryKey)){
// 模版-接口外键
String modelFieldName = primaryKey.get(0).getModel_field_name();
String primaryKeyModelFieldName = primaryKey.get(0).getModel_field_name();
maps = data.stream().map(item -> convert(item, primaryKey.get(0).getInterface_field(), configDetailList)).filter(MapUtils::isNotEmpty).collect(Collectors.toList());
// 进行排序
Optional<Integer> minClassId = maps.stream()
.map(map -> Util.getIntValue(Util.null2DefaultStr(map.get(modelFieldName),""),-1))
.map(map -> Util.getIntValue(Util.null2DefaultStr(map.get(primaryKeyModelFieldName),""),-1))
.min(Comparator.naturalOrder());
Optional<Integer> maxClassId = maps.stream()
.map(map -> Util.getIntValue(Util.null2DefaultStr(map.get(modelFieldName),""),-1))
.map(map -> Util.getIntValue(Util.null2DefaultStr(map.get(primaryKeyModelFieldName),""),-1))
.max(Comparator.naturalOrder());
Integer min = minClassId.orElse(0);
Integer max = maxClassId.orElse(0);
// 按照外键排序并且在oa中范围查询出外键与oa数据的对应关系
List<Map<String, String>> dataIdList = asyncMapper.selectDataIds(modelFieldName, config.getTable_name(), min, max);
List<Map<String, String>> dataIdList = asyncMapper.selectDataIds(primaryKeyModelFieldName, config.getTable_name(), min, max);
if("1".equals(debug)){
log.info("dataIdList : " + JSONObject.toJSONString(dataIdList));
}
if(CollectionUtils.isNotEmpty(dataIdList)){
HashMap<String, String> idMap = parseListMap2Map(modelFieldName, "id", dataIdList);
HashMap<String, String> idMap = parseListMap2Map(primaryKeyModelFieldName, "id", dataIdList);
if("1".equals(debug)){
log.info("idMap : " + JSONObject.toJSONString(idMap));
}
@ -219,7 +223,7 @@ public class DataAsyncServiceImpl {
if("1".equals(debug)){
log.info("item : " + JSONObject.toJSONString(item));
}
String id = Util.null2DefaultStr(item.get(modelFieldName),"");
String id = Util.null2DefaultStr(item.get(primaryKeyModelFieldName),"");
if("1".equals(debug)){
log.info("id : " + id);
}
@ -233,6 +237,24 @@ public class DataAsyncServiceImpl {
return maps;
}
public Map<String, Integer> parseMaxAndMin(List<Map<String, Object>> maps, DataAsyncConfigDetail primaryKey){
String primaryKeyModelFieldName = primaryKey.getModel_field_name();
// 进行排序
Optional<Integer> minClassId = maps.stream()
.map(map -> Util.getIntValue(Util.null2DefaultStr(map.get(primaryKeyModelFieldName),""),-1))
.min(Comparator.naturalOrder());
Optional<Integer> maxClassId = maps.stream()
.map(map -> Util.getIntValue(Util.null2DefaultStr(map.get(primaryKeyModelFieldName),""),-1))
.max(Comparator.naturalOrder());
Integer min = minClassId.orElse(0);
Integer max = maxClassId.orElse(0);
Map<String, Integer> map = new HashMap<>();
map.put("max", max);
map.put("min", min);
return map;
}
/**
* <h1></h1>
* @author xuanran.wang
@ -277,108 +299,6 @@ public class DataAsyncServiceImpl {
return res;
}
public static ModeAppService modeAppService = ServiceUtil.getService(ModeAppServiceImpl.class, new User(1));
/**
* <h1></h1>
* @author xuanran.wang
* @dateTime 2023/6/9 13:28
* @param modeId id
**/
public static void rebuildRight(int modeId){
ModeRightInfoThread var5 = new ModeRightInfoThread();
var5.setModeId(modeId);
var5.setRebulidFlag("1");
var5.setSession(new HttpSession() {
@Override
public String getId() {
return null;
}
@Override
public boolean isNew() {
return false;
}
@Override
public long getCreationTime() {
return 0;
}
@Override
public long getLastAccessedTime() {
return 0;
}
@Override
public void setMaxInactiveInterval(int i) {
}
@Override
public int getMaxInactiveInterval() {
return 0;
}
@Override
public Object getAttribute(String s) {
return null;
}
@Override
public Enumeration getAttributeNames() {
return null;
}
@Override
public void setAttribute(String s, Object o) {
}
@Override
public void removeAttribute(String s) {
}
@Override
public void invalidate() {
}
@Override
public HttpSessionContext getSessionContext() {
return null;
}
@Override
public ServletContext getServletContext() {
return null;
}
@Override
public Object getValue(String s) {
return null;
}
@Override
public String[] getValueNames() {
return new String[0];
}
@Override
public void putValue(String s, Object o) {
}
@Override
public void removeValue(String s) {
}
});
var5.setUser(new User(1));
var5.resetModeRight();
}

View File

@ -52,7 +52,12 @@ public class ValueRuleMethod {
@ValueRuleMethodNo(value = 0, desc = "不转换")
public Object getFixValue(DataAsyncConfigDetail configDetail,Map<String, Object> map) {
return Util.null2DefaultStr(map.get(configDetail.getInterface_field()),"");
String interfaceField = configDetail.getInterface_field();
String value = Util.null2DefaultStr(map.get(interfaceField),"");
if(StringUtils.isBlank(value)){
return null;
}
return map.get(interfaceField);
}
@ -90,7 +95,7 @@ public class ValueRuleMethod {
try {
Class<?> clazz = Class.forName(cusText);
if(!CusAsyncConvert.class.isAssignableFrom(clazz)){
throw new CustomerException(cusText + " not implements weaver.xuanran.wang.sh_bigdata.org_hrm_async.annotations.CusOrgHrmAsyncConvert");
throw new CustomerException(cusText + " not implements weaver.xuanran.wang.eny.data_async.service.convert.CusAsyncConvert!");
}
CusAsyncConvert o = (CusAsyncConvert) clazz.newInstance();
Map<String, String> pathParam = Util.parseCusInterfacePathParam(cusText);

View File

@ -72,7 +72,7 @@ public class ValueRuleMethod {
}
if (!sapConfigDetail.getCustomerValue().trim().split(" ")[0].equalsIgnoreCase("select")) {
log.error(Util.logStr("intercept danger customer sql in " +
"[weaver.youhong.ai.haripijiu.action.sapdocking.config.util.ValueRuleMethod]. " +
"[weaver.youhong.ai.haripijiu.action.sapdocking.config.util.EyCusModelFieldValueRuleMethod]. " +
"sql [{}]", sapConfigDetail.getCustomerValue()));
return "";
}

View File

@ -55,6 +55,7 @@ public class DataAsyncTest extends BaseTest {
@Test
public void testb(){
String json = "[{\"interfaceForeignKey\":\"12001\",\"id\":386455},{\"interfaceForeignKey\":\"12002\",\"id\":386457},{\"interfaceForeignKey\":\"12003\",\"id\":386459},{\"interfaceForeignKey\":\"12004\",\"id\":386460},{\"interfaceForeignKey\":\"12005\",\"id\":386462},{\"interfaceForeignKey\":\"12006\",\"id\":386464},{\"interfaceForeignKey\":\"12007\",\"id\":386466},{\"interfaceForeignKey\":\"12008\",\"id\":386468},{\"interfaceForeignKey\":\"12009\",\"id\":386469},{\"interfaceForeignKey\":\"12010\",\"id\":386471}]";
// List<Map<String, String>> maps = asyncMapper.selectDataIds("classId", "uf_class", 1, 10);
List<Map<String, String>> list = JSONObject.parseObject(json, List.class);