diff --git a/src/main/java/weaver/cssc/workflow/voucher/action/VoucherPushAction.java b/src/main/java/weaver/cssc/workflow/voucher/action/VoucherPushAction.java index 3f7f944..777a4ac 100644 --- a/src/main/java/weaver/cssc/workflow/voucher/action/VoucherPushAction.java +++ b/src/main/java/weaver/cssc/workflow/voucher/action/VoucherPushAction.java @@ -29,7 +29,7 @@ import java.util.Map; * 中国船舶工业 * 流程数据推送NC生成凭证Action * @author bleach - * @Date 2023-06-19 + * @version 2023-06-19 */ public class VoucherPushAction extends SafeCusBaseAction { @@ -69,148 +69,156 @@ public class VoucherPushAction extends SafeCusBaseAction { interfaceLoggerDao.setOperateDateTime(TimeUtil.getCurrentTimeString()); 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(); Map configMap = voucherUtil.getConfigurationByWorkflowId(String.valueOf(workflowId),cusParamValue); - 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> fieldList = (List>) configMap.get("fieldList"); - List> detailList = (List>) 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 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 { + if(configMap == null || configMap.size() == 0){ interfaceLoggerDao.setDealStatus(VoucherConstants.DEAL_STATUS_NO_DEAL); interfaceLoggerDao.setDealMessage("该流程自定义参数值[" + 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> fieldList = (List>) configMap.get("fieldList"); + List> detailList = (List>) 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 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); @@ -224,4 +232,4 @@ public class VoucherPushAction extends SafeCusBaseAction { public void setCusParamValue(String cusParamValue) { this.cusParamValue = cusParamValue; } -} +} \ No newline at end of file diff --git a/src/main/java/weaver/weilin/zhu/xyzq/scheduled/entity/SyncConfigDao.java b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/entity/SyncConfigDao.java new file mode 100644 index 0000000..cc17841 --- /dev/null +++ b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/entity/SyncConfigDao.java @@ -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 fieldDataList; + +} diff --git a/src/main/java/weaver/weilin/zhu/xyzq/scheduled/entity/SyncConfigDetailDao.java b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/entity/SyncConfigDetailDao.java new file mode 100644 index 0000000..0a8a8ba --- /dev/null +++ b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/entity/SyncConfigDetailDao.java @@ -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; +} diff --git a/src/main/java/weaver/weilin/zhu/xyzq/scheduled/sqlmapper/OrganizationSyncSqlMapper.java b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/sqlmapper/OrganizationSyncSqlMapper.java new file mode 100644 index 0000000..b75d680 --- /dev/null +++ b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/sqlmapper/OrganizationSyncSqlMapper.java @@ -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 getConfigurationDetailByMainKeyId(@ParamMapper("mainId") int mainId); + + /** + * 获取当前模块中已存在记录集合 + * @param foreignKeyFieldName 外键字段名称 + * @param modeTable 模块表名称 + * @return 数据集合 + */ + @Select("select $t{foreignKeyFieldName},id from $t{modeTable}") + Map getModeExistData(@ParamMapper("foreignKeyFieldName") String foreignKeyFieldName,@ParamMapper("modeTable") String modeTable); + + /** + * 更新建模数据 + * @param updateSql 更新的SQL语句 + * @param params 参数集合 + * @return 执行结果 + */ + @Update(custom = true) + boolean updateModeData(@SqlString String updateSql, Map params); + + /** + * 删除插入失败的冗余数据 + * @param modeTable 删除的表名称 + * @param id 数据ID + */ + @Delete("delete from $t{modeTable} where id = #{id}") + void deleteRedundancyData(@ParamMapper("modeTable") String modeTable,@ParamMapper("id") int id); +} diff --git a/src/main/java/weaver/weilin/zhu/xyzq/scheduled/task/SyncOrgDataTask.java b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/task/SyncOrgDataTask.java new file mode 100644 index 0000000..a6e2c5a --- /dev/null +++ b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/task/SyncOrgDataTask.java @@ -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; + } +} diff --git a/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/ChangeRuleMethod.java b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/ChangeRuleMethod.java new file mode 100644 index 0000000..b4e9c41 --- /dev/null +++ b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/ChangeRuleMethod.java @@ -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, Object>> VALUE_RULE_FUNCTION = new HashMap<>(); + + static { + Class 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 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 map) { + return TimeUtil.getCurrentDateString(); + } + + /** + * 系统日期 + * @param dao 明细配置 + * @param map 接口数据集合 + * @return 返回转换后的值 + */ + @MethodRuleNo(value = 2, desc = "系统日期时间") + public Object systemDateTime(SyncConfigDetailDao dao,Map map) { + return TimeUtil.getCurrentTimeString(); + } + + /** + * 固定值 + * @param dao 明细配置 + * @param map 接口数据集合 + * @return 返回转换后的值 + */ + @MethodRuleNo(value = 3, desc = "固定值") + public Object fixValue(SyncConfigDetailDao dao,Map map) { + return Util.null2String(dao.getCusSQL()); + } + + /** + * 固定值 + * @param dao 明细配置 + * @param map 接口数据集合 + * @return 返回转换后的值 + */ + @MethodRuleNo(value = 4, desc = "自定义SQL") + public Object customizeSQL(SyncConfigDetailDao dao,Map 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 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 pathParam = Util.parseCusInterfacePathParam(cusText); + return o.cusConvert(dao, map, pathParam); + }catch (Exception e){ + logger.error("getCusConvertInterface error! " + e.getMessage()); + return null; + } + } +} diff --git a/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/CusAsyncConvert.java b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/CusAsyncConvert.java new file mode 100644 index 0000000..2d40145 --- /dev/null +++ b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/CusAsyncConvert.java @@ -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; + +/** + *

自定义转换接口

+ * + * @author bleach + */ +public interface CusAsyncConvert { + Logger log = Util.getLogger(); + Object cusConvert(SyncConfigDetailDao dao, Map maps, Map pathParam); +} diff --git a/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/OrganizationSyncUtil.java b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/OrganizationSyncUtil.java new file mode 100644 index 0000000..274c1e8 --- /dev/null +++ b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/OrganizationSyncUtil.java @@ -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 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> dataList = restApiUtil.getOrganizationList(lastSyncTs, syncConfigDao.getDataType()); + + + // 使用 Stream 对接口数据进行去除重复记录 + List> 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> dataList, Map oaDataMap, List detailDaoList){ + + String currentDateTime = TimeUtil.getCurrentTimeString(); + + for(Map 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 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 主键值 + */ + private Predicate distinctByKey(Function keyExtractor) { + Map seen = new HashMap<>(); + return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; + } +} diff --git a/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/RestApiUtil.java b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/RestApiUtil.java new file mode 100644 index 0000000..1e01c03 --- /dev/null +++ b/src/main/java/weaver/weilin/zhu/xyzq/scheduled/util/RestApiUtil.java @@ -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 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> getOrganizationList(String lastSyncDateTime,int dataType){ + String requestBaseURL = Util.null2String(paramMap.get("Organization_BaseURL")); + + Map 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> 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> dataList = (List>) Util.getValueByKeyStr("respbody.data.list",responseDataObj); + + mergedDataList.addAll((Collection>) 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_"); + } +} diff --git a/src/main/java/weaver/zwl/common/ToolUtilNew.java b/src/main/java/weaver/zwl/common/ToolUtilNew.java index 6b74c17..05c0ee2 100644 --- a/src/main/java/weaver/zwl/common/ToolUtilNew.java +++ b/src/main/java/weaver/zwl/common/ToolUtilNew.java @@ -423,4 +423,17 @@ public class ToolUtilNew extends ToolUtil{ 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 ""; + } }