合并优化
dev
youhong.ai 2023-07-10 14:53:41 +08:00
commit 486ba3d592
10 changed files with 820 additions and 137 deletions

View File

@ -29,7 +29,7 @@ import java.util.Map;
* *
* NCAction * NCAction
* @author bleach * @author bleach
* @Date 2023-06-19 * @version 2023-06-19
*/ */
public class VoucherPushAction extends SafeCusBaseAction { public class VoucherPushAction extends SafeCusBaseAction {
@ -69,148 +69,156 @@ public class VoucherPushAction extends SafeCusBaseAction {
interfaceLoggerDao.setOperateDateTime(TimeUtil.getCurrentTimeString()); interfaceLoggerDao.setOperateDateTime(TimeUtil.getCurrentTimeString());
logger.info("workflowId:[" + workflowId + "],requestId:[" + requestId + "],billTable:[" + billTable + "]"); logger.info("workflowId:[" + workflowId + "],requestId:[" + requestId + "],billTable:[" + billTable + "]");
//流程请求标题
String requestName = "";
//流程请求编号
String requestMark = "";
//获取流程基础信息
RecordSetTrans rsts = requestInfo.getRsTrans();
if(rsts == null){
rsts = new RecordSetTrans();
}
try {
if(rsts.executeQuery("select * from workflow_requestBase where requestId = ?",requestId)){
if(rsts.next()){
requestName = Util.null2String(rsts.getString("requestName"));
requestMark = Util.null2String(rsts.getString("requestMark"));
}
}
} catch (Exception e) {
Util.actionFailException(requestInfo.getRequestManager(),"获取流程基本信息异常,异常信息:[" + e.getMessage() + "]");
throw new RuntimeException(e);
}
interfaceLoggerDao.setRequestNo(requestMark);
interfaceLoggerDao.setOperator(user.getUID());
VoucherUtil voucherUtil = new VoucherUtil(); VoucherUtil voucherUtil = new VoucherUtil();
Map<String,Object> configMap = voucherUtil.getConfigurationByWorkflowId(String.valueOf(workflowId),cusParamValue); Map<String,Object> configMap = voucherUtil.getConfigurationByWorkflowId(String.valueOf(workflowId),cusParamValue);
if(configMap != null && configMap.size() > 0){ if(configMap == null || configMap.size() == 0){
//获取数据条件
String dataCondition = (String) configMap.get("dataCondition");
RecordSet rs ;
if(StringUtils.isNotBlank(dataCondition)){
dataCondition = ToolUtilNew.staticToDBC(dataCondition);
rs = sqlMapper.getWorkflowMainInfoAndCusWhere(billTable,requestId,dataCondition);
} else {
rs = sqlMapper.getWorkflowMainInfo(billTable,requestId);
}
int mainId = -1;
if(rs.next()){
mainId = Util.getIntValue(rs.getString("id"),-1);
}
if(mainId > -1){
//流程请求标题
String requestName = "";
//流程请求编号
String requestMark = "";
//获取流程基础信息
RecordSetTrans rsts = requestInfo.getRsTrans();
if(rsts == null){
rsts = new RecordSetTrans();
}
try {
if(rsts.executeQuery("select * from workflow_requestBase where requestId = ?",requestId)){
if(rsts.next()){
requestName = Util.null2String(rsts.getString("requestName"));
requestMark = Util.null2String(rsts.getString("requestMark"));
}
}
} catch (Exception e) {
Util.actionFailException(requestInfo.getRequestManager(),"获取流程基本信息异常,异常信息:[" + e.getMessage() + "]");
throw new RuntimeException(e);
}
interfaceLoggerDao.setRequestNo(requestMark);
interfaceLoggerDao.setOperator(user.getUID());
List<Map<String,Object>> fieldList = (List<Map<String, Object>>) configMap.get("fieldList");
List<Map<String,Object>> detailList = (List<Map<String, Object>>) configMap.get("detailList");
voucherUtil.setDetailList(detailList);
voucherUtil.setFieldList(fieldList);
//流程基础信息集合[流程请求ID,流程请求标题,流程请求编号,流程表单名称主表数据ID]
String[] baseArray = new String[]{requestId,requestName,requestMark,billTable,String.valueOf(mainId)};
//根据配置生成JSON对象
JSONObject jsonObject = voucherUtil.recursionGenerateJSONObject(baseArray,rs,null, 0,"",0,0);
logger.info("生成JSON字符串为:[" + jsonObject.toJSONString() + "]");
interfaceLoggerDao.setRequestBody(jsonObject.toJSONString());
//NC凭证接口地址
String voucherRequestURL = ToolUtilNew.getStaticSystemParamValue("NC_VoucherRequestURL");
if(StringUtils.isNotBlank(voucherRequestURL)){
HttpUtils httpUtils = new HttpUtils();
try {
Map<String,String> headerMap = new HashMap<>();
headerMap.put("Content-Type", HttpArgsType.APPLICATION_JSON);
ResponeVo responeVo = httpUtils.apiPostObject(voucherRequestURL,jsonObject,headerMap);
if(responeVo.getCode() == VoucherConstants.REQUEST_SUCCESS_CODE){
//获取返回信息
String result = responeVo.getEntityString();
interfaceLoggerDao.setResponseBody(result);
JSONObject resultObj = JSONObject.parseObject(result);
if(resultObj.containsKey("code")){
int code = resultObj.getIntValue("code");
if(code == 0){
String pk = resultObj.getString("pk");
String backFieldName = (String) configMap.get("backFieldName");
if(StringUtils.isNotBlank(backFieldName)){
sqlMapper.backVoucherNoToBill(billTable,backFieldName,pk,requestId);
}
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_SUCCESS);
interfaceLoggerDao.setDealMessage("接口调用成功!");
} else {
String message = resultObj.getString("msg");
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_FAILURE);
interfaceLoggerDao.setDealMessage("调用NC凭证接口失败失败状态码:[" + message + "]");
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
//接口调用失败
Util.actionFailException(requestInfo.getRequestManager(),"调用NC凭证接口失败失败状态码:[" + message + "]");
}
} else {
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_FAILURE);
interfaceLoggerDao.setDealMessage("调用NC凭证接口失败接口返回信息为空");
}
} else {
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_FAILURE);
interfaceLoggerDao.setDealMessage("调用NC凭证接口失败失败状态码:[" + responeVo.getCode() + "]");
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
Util.actionFailException(requestInfo.getRequestManager(),"调用NC凭证接口失败失败状态码:[" + responeVo.getCode() + "]");
}
} catch (IOException e) {
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_FAILURE);
interfaceLoggerDao.setDealMessage("调用NC凭证接口异常异常信息:" + e.getMessage());
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
logger.error(Util.getErrString(e));
Util.actionFailException(requestInfo.getRequestManager(),"调用NC凭证接口异常");
//throw new RuntimeException(e);
}
} else {
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_NO_DEAL);
interfaceLoggerDao.setDealMessage("NC凭证接口地址未配置");
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
Util.actionFailException(requestInfo.getRequestManager(),"NC凭证接口地址未配置");
}
} else {
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_NO_DEAL);
interfaceLoggerDao.setDealMessage("当前流程不满足自定义条件:[" + dataCondition + "]");
logger.info("当前流程不满足自定义条件:[" + dataCondition + "]");
}
} else {
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_NO_DEAL); interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_NO_DEAL);
interfaceLoggerDao.setDealMessage("该流程自定义参数值[" + cusParamValue + "]对应的配置不存在!"); interfaceLoggerDao.setDealMessage("该流程自定义参数值[" + cusParamValue + "]对应的配置不存在!");
logger.info("该流程自定义参数值[" + cusParamValue + "]对应的配置不存在!"); logger.info("该流程自定义参数值[" + cusParamValue + "]对应的配置不存在!");
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
return ;
}
//获取数据条件
String dataCondition = (String) configMap.get("dataCondition");
RecordSet rs ;
if(StringUtils.isNotBlank(dataCondition)){
dataCondition = ToolUtilNew.staticToDBC(dataCondition);
rs = sqlMapper.getWorkflowMainInfoAndCusWhere(billTable,requestId,dataCondition);
} else {
rs = sqlMapper.getWorkflowMainInfo(billTable,requestId);
}
int mainId = -1;
if(rs.next()){
mainId = Util.getIntValue(rs.getString("id"),-1);
}
if(mainId <= 0){
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_NO_DEAL);
interfaceLoggerDao.setDealMessage("当前流程不满足自定义条件:[" + dataCondition + "]");
logger.info("当前流程不满足自定义条件:[" + dataCondition + "]");
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
return ;
}
List<Map<String,Object>> fieldList = (List<Map<String, Object>>) configMap.get("fieldList");
List<Map<String,Object>> detailList = (List<Map<String, Object>>) configMap.get("detailList");
voucherUtil.setDetailList(detailList);
voucherUtil.setFieldList(fieldList);
//流程基础信息集合[流程请求ID,流程请求标题,流程请求编号,流程表单名称主表数据ID]
String[] baseArray = new String[]{requestId,requestName,requestMark,billTable,String.valueOf(mainId)};
//根据配置生成JSON对象
JSONObject jsonObject = voucherUtil.recursionGenerateJSONObject(baseArray,rs,null, 0,"",0,0);
logger.info("生成JSON字符串为:[" + jsonObject.toJSONString() + "]");
interfaceLoggerDao.setRequestBody(jsonObject.toJSONString());
//NC凭证接口地址
String voucherRequestURL = ToolUtilNew.getStaticSystemParamValue("NC_VoucherRequestURL");
if(StringUtils.isBlank(voucherRequestURL)){
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_NO_DEAL);
interfaceLoggerDao.setDealMessage("NC凭证接口地址未配置");
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
Util.actionFailException(requestInfo.getRequestManager(),"NC凭证接口地址未配置");
}
HttpUtils httpUtils = new HttpUtils();
try {
Map<String,String> headerMap = new HashMap<>();
headerMap.put("Content-Type", HttpArgsType.APPLICATION_JSON);
ResponeVo responeVo = httpUtils.apiPostObject(voucherRequestURL,jsonObject,headerMap);
if(responeVo.getCode() != VoucherConstants.REQUEST_SUCCESS_CODE){
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_FAILURE);
interfaceLoggerDao.setDealMessage("调用NC凭证接口失败失败状态码:[" + responeVo.getCode() + "]");
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
Util.actionFailException(requestInfo.getRequestManager(),"调用NC凭证接口失败失败状态码:[" + responeVo.getCode() + "]");
}
//获取返回信息
String result = responeVo.getEntityString();
interfaceLoggerDao.setResponseBody(result);
JSONObject resultObj = JSONObject.parseObject(result);
if(resultObj.containsKey("code")){
int code = resultObj.getIntValue("code");
if(code == 0){
String pk = resultObj.getString("pk");
String backFieldName = (String) configMap.get("backFieldName");
if(StringUtils.isNotBlank(backFieldName)){
sqlMapper.backVoucherNoToBill(billTable,backFieldName,pk,requestId);
}
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_SUCCESS);
interfaceLoggerDao.setDealMessage("接口调用成功!");
} else {
String message = resultObj.getString("msg");
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_FAILURE);
interfaceLoggerDao.setDealMessage("调用NC凭证接口失败失败状态码:[" + message + "]");
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
//接口调用失败
Util.actionFailException(requestInfo.getRequestManager(),"调用NC凭证接口失败失败状态码:[" + message + "]");
}
} else {
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_FAILURE);
interfaceLoggerDao.setDealMessage("调用NC凭证接口失败接口返回信息为空");
}
} catch (IOException e) {
interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_FAILURE);
interfaceLoggerDao.setDealMessage("调用NC凭证接口异常异常信息:" + e.getMessage());
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
logger.error(Util.getErrString(e));
Util.actionFailException(requestInfo.getRequestManager(),"调用NC凭证接口异常");
//throw new RuntimeException(e);
} }
interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao); interfaceLoggerDao.insertInterfaceLog(interfaceLoggerDao);
@ -224,4 +232,4 @@ public class VoucherPushAction extends SafeCusBaseAction {
public void setCusParamValue(String cusParamValue) { public void setCusParamValue(String cusParamValue) {
this.cusParamValue = cusParamValue; this.cusParamValue = cusParamValue;
} }
} }

View File

@ -0,0 +1,51 @@
package weaver.weilin.zhu.xyzq.scheduled.entity;
import aiyh.utils.entity.FieldViewInfo;
import lombok.Data;
import java.util.List;
/**
*
*
* @author bleach
* @version 2023-07-07
*/
@Data
public class SyncConfigDao {
/**
* ID
*/
private int id;
/**
* ID
*/
private int modeId;
/**
* 0- 1-
*/
private int dataType;
/**
*
*/
private FieldViewInfo foreignKeyField;
/**
*
*/
private String keyNodeName;
/**
*
*/
private String lastSyncTs;
/**
*
*/
List<SyncConfigDetailDao> fieldDataList;
}

View File

@ -0,0 +1,34 @@
package weaver.weilin.zhu.xyzq.scheduled.entity;
import aiyh.utils.entity.FieldViewInfo;
import lombok.Data;
/**
*
*
* @author bleach
* @version 2023-07-07
*/
@Data
public class SyncConfigDetailDao {
/**
*
*/
private FieldViewInfo modeField;
/**
*
*/
private String nodeName;
/**
* 0- 1- 2- 3- 4-SQL 5-
*/
private int changeRule;
/**
*
*/
private String cusSQL;
}

View File

@ -0,0 +1,83 @@
package weaver.weilin.zhu.xyzq.scheduled.sqlmapper;
import aiyh.utils.annotation.recordset.*;
import weaver.weilin.zhu.xyzq.scheduled.entity.SyncConfigDao;
import weaver.weilin.zhu.xyzq.scheduled.entity.SyncConfigDetailDao;
import java.util.List;
import java.util.Map;
/**
*
*
* @author bleach
* @version 2023-07-07
*/
@SqlMapper
public interface OrganizationSyncSqlMapper {
/**
* ID
* @param keyId ID
* @return
*/
@Select("select * from uf_orgSyncConfig where id = #{keyId}")
@CollectionMappings({
@CollectionMapping(
property = "fieldDataList",
column = "id",
id = @Id(value = Integer.class, methodId = 1)
)
})
@Associations({
@Association(
property = "foreignKeyField",
column = "foreignKeyField",
select = "weaver.common.util.CommonUtil.getFieldInfo",
id = @Id(Integer.class)
)
})
SyncConfigDao getConfigurationByKeyId(@ParamMapper("keyId") int keyId);
/**
*
* @param mainId ID
* @return
*/
@Select("select * from uf_orgSyncConfig_dt1 where mainId = #{mainId}")
@Associations({
@Association(
property = "modeField",
column = "modeField",
select = "weaver.common.util.CommonUtil.getFieldInfo",
id = @Id(Integer.class)
)
})
@CollectionMethod(value = 1,desc = "获取详细的字段配置信息")
List<SyncConfigDetailDao> getConfigurationDetailByMainKeyId(@ParamMapper("mainId") int mainId);
/**
*
* @param foreignKeyFieldName
* @param modeTable
* @return
*/
@Select("select $t{foreignKeyFieldName},id from $t{modeTable}")
Map<String,Integer> getModeExistData(@ParamMapper("foreignKeyFieldName") String foreignKeyFieldName,@ParamMapper("modeTable") String modeTable);
/**
*
* @param updateSql SQL
* @param params
* @return
*/
@Update(custom = true)
boolean updateModeData(@SqlString String updateSql, Map<String, Object> params);
/**
*
* @param modeTable
* @param id ID
*/
@Delete("delete from $t{modeTable} where id = #{id}")
void deleteRedundancyData(@ParamMapper("modeTable") String modeTable,@ParamMapper("id") int id);
}

View File

@ -0,0 +1,38 @@
package weaver.weilin.zhu.xyzq.scheduled.task;
import aiyh.utils.Util;
import org.apache.commons.lang3.StringUtils;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.weilin.zhu.xyzq.scheduled.util.OrganizationSyncUtil;
/**
*
*
* @author bleach
* @version 2023-07-07
*/
public class SyncOrgDataTask extends BaseCronJob {
/**
* ID
*/
private String keyId ;
@Override
public void execute() {
if(StringUtils.isNotBlank(keyId)){
OrganizationSyncUtil syncUtil = new OrganizationSyncUtil();
syncUtil.syncData(Util.getIntValue(keyId,0));
}
}
public String getKeyId() {
return keyId;
}
public void setKeyId(String keyId) {
this.keyId = keyId;
}
}

View File

@ -0,0 +1,151 @@
package weaver.weilin.zhu.xyzq.scheduled.util;
import aiyh.utils.Util;
import aiyh.utils.annotation.MethodRuleNo;
import aiyh.utils.excention.CustomerException;
import com.google.common.base.Strings;
import org.apache.log4j.Logger;
import weaver.general.TimeUtil;
import weaver.weilin.zhu.xyzq.scheduled.entity.SyncConfigDetailDao;
import weaver.zwl.common.ToolUtilNew;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
/**
*
* @author bleach
* @version 2023-07-01
*/
public class ChangeRuleMethod {
/**
*
*/
private final Logger logger = Util.getLogger();
public static final Map<Integer, BiFunction<SyncConfigDetailDao, Map<String, Object>, Object>> VALUE_RULE_FUNCTION = new HashMap<>();
static {
Class<ChangeRuleMethod> valueRuleMethodClass = ChangeRuleMethod.class;
Method[] methods = valueRuleMethodClass.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(MethodRuleNo.class)) {
MethodRuleNo annotation = method.getAnnotation(MethodRuleNo.class);
int value = annotation.value();//规则标识
VALUE_RULE_FUNCTION.put(value, (config, map) -> {
try {
ChangeRuleMethod valueRuleMethod = new ChangeRuleMethod();
return method.invoke(valueRuleMethod, config, map);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
}
}
}
/**
*
* @param dao
* @param map
* @return
*/
@MethodRuleNo(value = 0, desc = "不转换")
public Object noChange(SyncConfigDetailDao dao,Map<String, Object> map) {
String fieldName = dao.getNodeName();//字段名称
//根据字段名称,获取其对应的字段值
if(map.containsKey(fieldName)){
return Util.null2String(map.get(fieldName));
} else {
return null;
}
}
/**
*
* @param dao
* @param map
* @return
*/
@MethodRuleNo(value = 1, desc = "系统日期")
public Object systemDate(SyncConfigDetailDao dao,Map<String, Object> map) {
return TimeUtil.getCurrentDateString();
}
/**
*
* @param dao
* @param map
* @return
*/
@MethodRuleNo(value = 2, desc = "系统日期时间")
public Object systemDateTime(SyncConfigDetailDao dao,Map<String, Object> map) {
return TimeUtil.getCurrentTimeString();
}
/**
*
* @param dao
* @param map
* @return
*/
@MethodRuleNo(value = 3, desc = "固定值")
public Object fixValue(SyncConfigDetailDao dao,Map<String, Object> map) {
return Util.null2String(dao.getCusSQL());
}
/**
*
* @param dao
* @param map
* @return
*/
@MethodRuleNo(value = 4, desc = "自定义SQL")
public Object customizeSQL(SyncConfigDetailDao dao,Map<String, Object> map) {
String fieldName = dao.getNodeName();//字段名称
//字段值
String fieldValue = "";
//根据字段名称,获取其对应的字段值
if(map.containsKey(fieldName)) {
fieldValue = Util.null2String(map.get(fieldName));
}
fieldValue = ToolUtilNew.getStaticValueByChangeRule(dao.getCusSQL(),fieldValue);
return fieldValue;
}
/**
*
* @param dao
* @param map
* @return
*/
@MethodRuleNo(value = 5, desc = "自定义接口")
public Object getCusConvertInterface(SyncConfigDetailDao dao, Map<String, Object> map) {
String cusText = dao.getCusSQL();
if(Strings.isNullOrEmpty(cusText)){
return null;
}
try {
Class<?> clazz = Class.forName(cusText);
if(!CusAsyncConvert.class.isAssignableFrom(clazz)){
throw new CustomerException(cusText + " 接口不存在或者未实现 weaver.weilin.zhu.xyzq.scheduled.util.CusAsyncConvert类!");
}
CusAsyncConvert o = (CusAsyncConvert) clazz.newInstance();
Map<String, String> pathParam = Util.parseCusInterfacePathParam(cusText);
return o.cusConvert(dao, map, pathParam);
}catch (Exception e){
logger.error("getCusConvertInterface error! " + e.getMessage());
return null;
}
}
}

View File

@ -0,0 +1,17 @@
package weaver.weilin.zhu.xyzq.scheduled.util;
import aiyh.utils.Util;
import org.apache.log4j.Logger;
import weaver.weilin.zhu.xyzq.scheduled.entity.SyncConfigDetailDao;
import java.util.Map;
/**
* <h1></h1>
*
* @author bleach
*/
public interface CusAsyncConvert {
Logger log = Util.getLogger();
Object cusConvert(SyncConfigDetailDao dao, Map<String, Object> maps, Map<String, String> pathParam);
}

View File

@ -0,0 +1,159 @@
package weaver.weilin.zhu.xyzq.scheduled.util;
import aiyh.utils.Util;
import aiyh.utils.entity.FieldViewInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import weaver.general.TimeUtil;
import weaver.weilin.zhu.xyzq.scheduled.entity.SyncConfigDao;
import weaver.weilin.zhu.xyzq.scheduled.entity.SyncConfigDetailDao;
import weaver.weilin.zhu.xyzq.scheduled.sqlmapper.OrganizationSyncSqlMapper;
import weaver.zwl.common.ToolUtilNew;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
*
*
* @author bleach
* @version 2023-07-07
*/
public class OrganizationSyncUtil {
/**
*
*/
private final Logger logger = Util.getLogger();
/**
*
*/
private final OrganizationSyncSqlMapper sqlMapper = Util.getMapper(OrganizationSyncSqlMapper.class);
/**
*
* @param keyId ID
*/
public synchronized void syncData(int keyId){
SyncConfigDao syncConfigDao = sqlMapper.getConfigurationByKeyId(keyId);
assert syncConfigDao != null;
//同步至模块ID
int modeId = syncConfigDao.getModeId();
if(modeId <= 0){
logger.info("未配置对应的模块!");
return;
}
//获取模块对应的表名称
String modeTable = ToolUtilNew.getModeTableById(modeId);
if(StringUtils.isBlank(modeTable)){
logger.info("该模块对应的表名称不存在!");
return;
}
//模块的外键
FieldViewInfo foreignKeyField = syncConfigDao.getForeignKeyField();
if(foreignKeyField == null){
logger.info("外键字段为空!");
return;
}
//获取当前库中已存在的记录集合
Map<String,Integer> oaDataMap = sqlMapper.getModeExistData(foreignKeyField.getFieldName(), modeTable);
//上一次同步时间戳
String lastSyncTs = StringUtils.isNotBlank(syncConfigDao.getLastSyncTs()) ? syncConfigDao.getLastSyncTs() : "2000-01-01 00:00:01";
RestApiUtil restApiUtil = new RestApiUtil();
List<Map<String, Object>> dataList = restApiUtil.getOrganizationList(lastSyncTs, syncConfigDao.getDataType());
// 使用 Stream 对接口数据进行去除重复记录
List<Map<String, Object>> distinctList = dataList.stream()
.filter(distinctByKey(map -> map.get(syncConfigDao.getKeyNodeName())))
.collect(Collectors.toList());
dealDataOperate(modeTable,modeId,syncConfigDao.getKeyNodeName(),distinctList,oaDataMap,syncConfigDao.getFieldDataList());
}
/**
*
* @param billTable
* @param modeId ID
* @param keyNodeName
* @param dataList
* @param oaDataMap OA
* @param detailDaoList
*/
private void dealDataOperate(String billTable,int modeId,String keyNodeName,List<Map<String,Object>> dataList, Map<String,Integer> oaDataMap, List<SyncConfigDetailDao> detailDaoList){
String currentDateTime = TimeUtil.getCurrentTimeString();
for(Map<String,Object> interfaceMap : dataList){
//获取主键值
String keyNodeValue = Util.null2String(interfaceMap.get(keyNodeName));
if(StringUtils.isBlank(keyNodeValue)){
continue;
}
int dataId;
boolean isNewData = false;
if(oaDataMap.containsKey(keyNodeValue)){//该数据已在历史记录中存在
dataId = oaDataMap.get(keyNodeValue);
} else {//该记录不存在
dataId = Util.getModeDataId(billTable,modeId,1);
isNewData = true;
}
StringBuilder setColumns = new StringBuilder();
Map<String,Object> paramMap = new HashMap<>();
for(SyncConfigDetailDao dao : detailDaoList){
String modeFieldName = dao.getModeField().getFieldName();
setColumns.append(",").append(modeFieldName).append(" = #{").append(modeFieldName).append("}");
Object fieldValue = ChangeRuleMethod.VALUE_RULE_FUNCTION.get(dao.getChangeRule()).apply(dao, interfaceMap);
paramMap.put(modeFieldName,fieldValue);
}
String updateSQL = "update " + billTable + " set modeDataModifyDatetime = '" + currentDateTime + "'" + setColumns.toString() + " where id = #{id}";
paramMap.put("id",dataId);
if(sqlMapper.updateModeData(updateSQL,paramMap)){
if(isNewData){
oaDataMap.put(keyNodeName,dataId);
}
} else{
sqlMapper.deleteRedundancyData(billTable,dataId);
}
}
}
/**
*
* @param keyExtractor
* @return
* @param <T>
*/
private <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new HashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}

View File

@ -0,0 +1,129 @@
package weaver.weilin.zhu.xyzq.scheduled.util;
import aiyh.utils.Util;
import aiyh.utils.httpUtil.HttpArgsType;
import aiyh.utils.httpUtil.ResponeVo;
import aiyh.utils.httpUtil.util.HttpUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import weaver.zwl.common.ToolUtilNew;
import java.io.IOException;
import java.util.*;
/**
*
* API
* @author bleach
*/
public class RestApiUtil {
/**
*
*/
private Map<String,String> paramMap;
/**
*
*/
private final Logger logger = Util.getLogger();
/**
* HTTP
*/
private final HttpUtils httpUtils = new HttpUtils();
/**
*
*/
public RestApiUtil(){
initParam();
}
/**
*
* @param lastSyncDateTime
* @param dataType 0- 1-
* @return
*/
@SuppressWarnings("unchecked")
public List<Map<String,Object>> getOrganizationList(String lastSyncDateTime,int dataType){
String requestBaseURL = Util.null2String(paramMap.get("Organization_BaseURL"));
Map<String,String> headerMap = new HashMap<>();
headerMap.put("Content-Type", HttpArgsType.APPLICATION_JSON);
String actualRequestURL = "";
if(dataType == 0){
actualRequestURL = requestBaseURL + "RESTHRService/GetOrganizationList";
} else if (dataType == 1) {
actualRequestURL = requestBaseURL + "RESTHRService/GetEmployeeList";
}
JSONObject requestBody = new JSONObject();
requestBody.put("sysid",Util.null2String(paramMap.get("Organization_SysId")));
requestBody.put("data_upd_tm",lastSyncDateTime);
requestBody.put("size",100);
int page = 1;
boolean hasNextPage;
List<Map<String,Object>> mergedDataList = new ArrayList<>();
do {
requestBody.put("page",page);
try {
ResponeVo responeVo = httpUtils.apiPostObject(actualRequestURL, requestBody, headerMap);
if(responeVo != null && responeVo.getCode() == 200){
//获取接口返回信息
String entityString = responeVo.getEntityString();
if(StringUtils.isBlank(entityString)) {
logger.info("接口调用失败,返回信息为空!");
break;
}
JSONObject resultObj = JSONObject.parseObject(entityString);
String responseData = resultObj.getString("ResponseData");
JSONObject responseDataObj = JSONObject.parseObject(responseData);
boolean isSuccess = (boolean) Util.getValueByKeyStr("resphead.success",responseDataObj);
if(isSuccess){
hasNextPage = (boolean) Util.getValueByKeyStr("respbody.data.hasNextPage",responseDataObj);
//详细数据集合
List<Map<String,Object>> dataList = (List<Map<String, Object>>) Util.getValueByKeyStr("respbody.data.list",responseDataObj);
mergedDataList.addAll((Collection<? extends Map<String, Object>>) dataList);
page ++;
} else {
//接口获取失败
return null;
}
} else {
assert responeVo != null;
logger.info("接口调用失败,失败状态码:[" + responeVo.getCode() + "]");
break;
}
} catch (IOException e) {
logger.info(Util.getErrString(e));
throw new RuntimeException(e);
}
} while (hasNextPage);
return mergedDataList;
}
/**
*
*/
private void initParam(){
paramMap = ToolUtilNew.getSystemParamList("Organization_");
}
}

View File

@ -423,4 +423,17 @@ public class ToolUtilNew extends ToolUtil{
return returnString; return returnString;
} }
/**
* ID
* @param modeId ID
* @return
*/
public static String getModeTableById(int modeId){
RecordSet rs = new RecordSet();
if(rs.executeQuery("select wb.tablename from modeInfo m inner join workflow_bill wb on m.formid = wb.id where m.id = ?") && rs.next()){
return Util.null2String(rs.getString(1));
}
return "";
}
} }