From 92503d432523416baa950f036d120cbe3851c76d Mon Sep 17 00:00:00 2001 From: wangxuanran <3055088966@qq.com> Date: Wed, 19 Jul 2023 13:28:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/aiyh/utils/Util.java | 20 +++- .../XkHospitalCommonDefinition.java | 105 ++++++++++++++++++ .../row/HrmDepartmentRowDefinition.java | 35 +++--- .../table/HrmDepartmentTableDefinition.java | 53 +++++++++ .../mapper/XkHospitalDataAsyncMapper.java | 24 ++++ .../XkHospitalCommonDataAsyncServiceImpl.java | 27 +++-- .../wang/xk_hospital/test/XkHospitalTest.java | 27 ++--- 7 files changed, 247 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/XkHospitalCommonDefinition.java create mode 100644 src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/table/HrmDepartmentTableDefinition.java create mode 100644 src/main/java/com/api/xuanran/wang/xk_hospital/data_async/mapper/XkHospitalDataAsyncMapper.java diff --git a/src/main/java/aiyh/utils/Util.java b/src/main/java/aiyh/utils/Util.java index 19081a2..136bd19 100644 --- a/src/main/java/aiyh/utils/Util.java +++ b/src/main/java/aiyh/utils/Util.java @@ -4094,16 +4094,28 @@ public class Util extends weaver.general.Util { } /** - *

获取下一个人员id

+ *

获取下一个部门id

* @author xuanran.wang * @dateTime 2023/7/19 11:24 * @return 部门id **/ public static int getNextDepartmentId(){ + return getNextDepartmentId(1); + } + + /** + *

获取下一个部门id

+ * @author xuanran.wang + * @dateTime 2023/7/19 11:24 + * @param subId 分部id + * @return 部门id + **/ + public static int getNextDepartmentId(int subId){ RecordSet insertRs = new RecordSet(); char separator = weaver.general.Util.getSeparator(); - String para = "" + separator + "" + separator + - "" + separator + "" + separator + "3" + separator + "" + separator + ""; + String uuid = UUID.randomUUID().toString(); + String para = uuid + separator +uuid + separator + + "" + separator + "" + separator + subId + separator + 1 + separator + ""; insertRs.executeProc("HrmDepartment_Insert", para); if(insertRs.next()){ return insertRs.getInt(1); @@ -4111,6 +4123,8 @@ public class Util extends weaver.general.Util { return -1; } + + /** *

获取下一个人员id

* @author xuanran.wang diff --git a/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/XkHospitalCommonDefinition.java b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/XkHospitalCommonDefinition.java new file mode 100644 index 0000000..477ed90 --- /dev/null +++ b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/XkHospitalCommonDefinition.java @@ -0,0 +1,105 @@ +package com.api.xuanran.wang.xk_hospital.data_async.definitions; + +import aiyh.utils.ThreadPoolConfig; +import aiyh.utils.Util; +import aiyh.utils.response_deal.mapper.ConfigMapper; +import com.api.xuanran.wang.xk_hospital.data_async.mapper.XkHospitalDataAsyncMapper; +import com.engine.common.service.impl.ThemeServiceImpl; +import org.apache.log4j.Logger; +import weaver.hrm.company.DepartmentComInfo; +import weaver.hrm.resource.ResourceComInfo; +import weaver.matrix.MatrixUtil; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/19 10:22 + */ +public class XkHospitalCommonDefinition { + /** + *

部门表名

+ **/ + public static final String DEPARTMENT = "hrmdepartment"; + /** + *

人员表名

+ **/ + public static final String HRM_RESOURCE = "hrmresource"; + /** + *

日志对象

+ **/ + protected static final Logger log = Util.getLogger(); + /** + *

数据同步mapper

+ **/ + protected final XkHospitalDataAsyncMapper dataAsyncMapper = Util.getMapper(XkHospitalDataAsyncMapper.class); + /** + *

执行自定义sql

+ **/ + protected final ConfigMapper configMapper = Util.getMapper(ConfigMapper.class); + /** + *

线程池对象

+ **/ + protected final ExecutorService threadPoolInstance = ThreadPoolConfig.createThreadPoolInstance(); + /** + *

部门缓存

+ **/ + protected static final DepartmentComInfo DEPARTMENT_COM_INFO = new DepartmentComInfo(); + /** + *

人员缓存

+ **/ + protected static ResourceComInfo RESOURCE_COM_INFO = null; + /** + *

系统主题接口

+ **/ + protected static final ThemeServiceImpl THEME_SERVICE = new ThemeServiceImpl(); + static { + try { + RESOURCE_COM_INFO = new ResourceComInfo(); + }catch (Exception e){ + log.error("人员缓存类初始化失败! " + e.getMessage()); + Util.logErrorStr(e); + } + } + + /** + * 同步系统缓存 + * @param table 表名 + * @param idList 数据id集合 + */ + protected void synCache(String table, List idList){ + if (DEPARTMENT.equals(table)) { + threadPoolInstance.execute(()->{ + try { + DEPARTMENT_COM_INFO.removeCache(); + MatrixUtil.sysDepartmentData(); + }catch (Exception e){ + log.error("同步系统部门缓存error : " + e.getMessage()); + Util.logErrorStr(e); + } + }); + }else if(HRM_RESOURCE.equals(table)){ + try { + List> hrmSubList = dataAsyncMapper.selectHrmSubByHrmIdList(idList); + hrmSubList.forEach(map->{ + THEME_SERVICE.createSubCompanyMenu(map.get("id"), map.get("sub")); + }); + RESOURCE_COM_INFO.removeCache(); + }catch (Exception e){ + log.error("同步系统人员缓存error : " + e.getMessage()); + Util.logErrorStr(e); + } + } + } + + + + + + + +} diff --git a/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/row/HrmDepartmentRowDefinition.java b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/row/HrmDepartmentRowDefinition.java index 8492899..fa1c7c8 100644 --- a/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/row/HrmDepartmentRowDefinition.java +++ b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/row/HrmDepartmentRowDefinition.java @@ -1,10 +1,14 @@ -package com.api.xuanran.wang.xk_hospital.data_async.definitions; +package com.api.xuanran.wang.xk_hospital.data_async.definitions.row; import aiyh.utils.Util; +import aiyh.utils.excention.CustomerException; import aiyh.utils.response_deal.entity.RowDefinition; import aiyh.utils.response_deal.intfaces.RowDefinitionCallback; import aiyh.utils.response_deal.mapper.ConfigMapper; import aiyh.utils.tool.cn.hutool.core.util.StrUtil; +import com.api.xuanran.wang.xk_hospital.data_async.definitions.XkHospitalCommonDefinition; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; import weaver.conn.RecordSet; import java.util.HashMap; @@ -15,9 +19,11 @@ import java.util.HashMap; * @author xuanran.wang * @date 2023/7/18 11:13 */ -public class HrmDepartmentDefinition implements RowDefinitionCallback { +@NoArgsConstructor +@AllArgsConstructor +public class HrmDepartmentRowDefinition extends XkHospitalCommonDefinition implements RowDefinitionCallback { - protected final ConfigMapper configMapper = Util.getMapper(ConfigMapper.class); + private String table; @Override public void rowHandle(RowDefinition rowDefinition) { @@ -27,19 +33,20 @@ public class HrmDepartmentDefinition implements RowDefinitionCallback { public boolean judgmentRepetition(RowDefinition rowDefinition) { String conditionScript = rowDefinition.getConditionScript(); String cusQuerySql = "select id from " + rowDefinition.getAssignTable() + " where " + conditionScript; - String dataId = this.configMapper.executeCusQuerySql(cusQuerySql, rowDefinition.getWhereParam(),new HashMap<>()); + String dataId = configMapper.executeCusQuerySql(cusQuerySql, rowDefinition.getWhereParam(),new HashMap<>()); if(StrUtil.isBlank(dataId)){ - RecordSet insertRs = new RecordSet(); - char separator = weaver.general.Util.getSeparator(); - String para = "" + separator + "" + separator + - "" + separator + "" + separator + "3" + separator + "" + separator + ""; - insertRs.executeProc("HrmDepartment_Insert", para); - if (insertRs.next()) { - int depId = insertRs.getInt(1); - rowDefinition.setDataId(depId); - rowDefinition.getWhereParam().put("id", depId); - rowDefinition.setConditionScript(" id = #{whereItem.id}"); + int nextId = -1; + if(HRM_RESOURCE.equals(table)){ + nextId = Util.getNextHrmId(); + }else if(DEPARTMENT.equals(table)){ + nextId = Util.getNextDepartmentId(); } + if(nextId < 0){ + throw new CustomerException("从 " + table + " 获取下一个id失败!"); + } + rowDefinition.setDataId(nextId); + rowDefinition.getWhereParam().put("id", nextId); + rowDefinition.setConditionScript(" id = #{whereItem.id}"); } return true; } diff --git a/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/table/HrmDepartmentTableDefinition.java b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/table/HrmDepartmentTableDefinition.java new file mode 100644 index 0000000..e36e021 --- /dev/null +++ b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/definitions/table/HrmDepartmentTableDefinition.java @@ -0,0 +1,53 @@ +package com.api.xuanran.wang.xk_hospital.data_async.definitions.table; + +import aiyh.utils.Util; +import aiyh.utils.excention.CustomerException; +import aiyh.utils.response_deal.entity.RowDefinition; +import aiyh.utils.response_deal.entity.TableDefinition; +import aiyh.utils.response_deal.intfaces.TableDefinitionCallback; +import com.api.xuanran.wang.xk_hospital.data_async.definitions.XkHospitalCommonDefinition; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import org.apache.commons.collections.CollectionUtils; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + *

部门/人员同步表回调

+ * + * @author xuanran.wang + * @date 2023/7/19 10:11 + */ +@AllArgsConstructor +@NoArgsConstructor +public class HrmDepartmentTableDefinition extends XkHospitalCommonDefinition implements TableDefinitionCallback { + + private String table; + + @Override + public void tableHandle(TableDefinition tableDefinition) { + + } + + @Override + public void afterHandle(TableDefinition tableDefinition, boolean flag) { + List idList = tableDefinition.getRowDefinitionList() + .stream() + .map(RowDefinition::getDataId) + .filter(dataId -> dataId > 0) + .collect(Collectors.toList()); + if(CollectionUtils.isEmpty(idList)){ + return; + } + if(!flag){ + if (!this.dataAsyncMapper.deleteHrmDepart(idList)) { + log.error("删除 " + table + " 表数据失败!"); + } + throw new CustomerException("同步 " + table + " 表数据失败!"); + }else { + synCache(table, idList); + } + } +} diff --git a/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/mapper/XkHospitalDataAsyncMapper.java b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/mapper/XkHospitalDataAsyncMapper.java new file mode 100644 index 0000000..4b59630 --- /dev/null +++ b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/mapper/XkHospitalDataAsyncMapper.java @@ -0,0 +1,24 @@ +package com.api.xuanran.wang.xk_hospital.data_async.mapper; + +import aiyh.utils.annotation.recordset.*; + +import java.util.List; +import java.util.Map; + +/** + *

胸科医院数据同步mapper

+ * + * @author xuanran.wang + * @date 2023/7/19 10:19 + */ +@SqlMapper +public interface XkHospitalDataAsyncMapper { + + @Delete("delete from hrmdepartment where id in $t{idList}") + boolean deleteHrmDepart(@ParamMapper("idList") List idList); + + @Select("select id, subcompanyid1 as sub from hrmresource where id in $t{idList}") + @ToLowerCase + List> selectHrmSubByHrmIdList(@ParamMapper("idList") List idList); + +} diff --git a/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/service/impl/XkHospitalCommonDataAsyncServiceImpl.java b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/service/impl/XkHospitalCommonDataAsyncServiceImpl.java index ff5f4ac..d8e16cc 100644 --- a/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/service/impl/XkHospitalCommonDataAsyncServiceImpl.java +++ b/src/main/java/com/api/xuanran/wang/xk_hospital/data_async/service/impl/XkHospitalCommonDataAsyncServiceImpl.java @@ -2,10 +2,12 @@ package com.api.xuanran.wang.xk_hospital.data_async.service.impl; import aiyh.utils.response_deal.ResponseMappingDeal; import aiyh.utils.response_deal.intfaces.RowDefinitionCallback; +import aiyh.utils.response_deal.intfaces.TableDefinitionCallback; import aiyh.utils.tool.cn.hutool.core.util.StrUtil; -import com.api.xuanran.wang.xk_hospital.data_async.definitions.HrmresourceDefinition; +import com.api.xuanran.wang.xk_hospital.data_async.definitions.XkHospitalCommonDefinition; +import com.api.xuanran.wang.xk_hospital.data_async.definitions.row.HrmDepartmentRowDefinition; +import com.api.xuanran.wang.xk_hospital.data_async.definitions.table.HrmDepartmentTableDefinition; import com.api.xuanran.wang.xk_hospital.data_async.service.XkHospitalCommonDataAsyncService; -import org.apache.commons.lang3.StringUtils; import java.util.HashMap; import java.util.Map; @@ -18,20 +20,29 @@ import java.util.Map; */ public class XkHospitalCommonDataAsyncServiceImpl implements XkHospitalCommonDataAsyncService { - private static final Map CUS_DEFINITIONS = new HashMap<>(); + private static final Map CUS_ROW_DEFINITIONS = new HashMap<>(); + + private static final Map CUS_TABLE_DEFINITIONS = new HashMap<>(); static { - CUS_DEFINITIONS.put("hrm", new HrmresourceDefinition()); - CUS_DEFINITIONS.put("dept", new HrmresourceDefinition()); + CUS_ROW_DEFINITIONS.put("dept", new HrmDepartmentRowDefinition(XkHospitalCommonDefinition.DEPARTMENT)); + CUS_ROW_DEFINITIONS.put("hrm", new HrmDepartmentRowDefinition(XkHospitalCommonDefinition.HRM_RESOURCE)); + + CUS_TABLE_DEFINITIONS.put("dept", new HrmDepartmentTableDefinition(XkHospitalCommonDefinition.DEPARTMENT)); + CUS_TABLE_DEFINITIONS.put("hrm", new HrmDepartmentTableDefinition(XkHospitalCommonDefinition.HRM_RESOURCE)); } @Override public void async(String configCode, String type, Map params){ ResponseMappingDeal deal = new ResponseMappingDeal(); if(StrUtil.isNotBlank(type)){ - RowDefinitionCallback callback = CUS_DEFINITIONS.get(type); - if(null != callback){ - deal.setRowCallback(callback); + RowDefinitionCallback rowDefinitionCallback = CUS_ROW_DEFINITIONS.get(type); + if(null != rowDefinitionCallback){ + deal.setRowCallback(rowDefinitionCallback); + } + TableDefinitionCallback tableDefinitionCallback = CUS_TABLE_DEFINITIONS.get(type); + if(null != tableDefinitionCallback){ + deal.setTableCallback(tableDefinitionCallback); } } deal.doResponseSync(configCode, params); diff --git a/src/test/java/xuanran/wang/xk_hospital/test/XkHospitalTest.java b/src/test/java/xuanran/wang/xk_hospital/test/XkHospitalTest.java index 0159c50..4f0f792 100644 --- a/src/test/java/xuanran/wang/xk_hospital/test/XkHospitalTest.java +++ b/src/test/java/xuanran/wang/xk_hospital/test/XkHospitalTest.java @@ -22,26 +22,15 @@ public class XkHospitalTest extends BaseTest { @Test public void testA(){ String json = "{\n" + - "\t\"ID\":\"主键\",\n" + - "\t\"DeptCode\":\"科室代码\",\n" + - "\t\"DeptName\":\"科室名称\",\n" + - "\t\"DeptLevel\": \"科室级别,int类型,可为null\", \n" + - "\t\"ParentDeptCode\":\"上级科室代码\",\n" + - "\t\"IsDelete\": \"是否停用,bool类型\", \n" + - "\t\"Remark\":\"备注\",\n" + - "\t\"Mapping\": [\n" + - "\t\t{\n" + - "\t\t\t\"ID\":\"主键\",\n" + - "\t\t\t\"DeptID\":\"源科室主键\",\n" + - "\t\t\t\"SourceDeptName\":\"源科室名称\",\n" + - "\t\t\t\"SystemID\":\"第三方系统主键\",\n" + - "\t\t\t\"SystemName\":\"第三方系统名称\",\n" + - "\t\t\t\"DeptCode\":\"科室代码\",\n" + - "\t\t\t\"DeptName\":\"科室名称\",\n" + - "\t\t\t\"Remark\":\"备注\",\n" + - "\t\t}\n" + - "\t]\n" + + "\t\"ID\":\"1\",\n" + + "\t\"DeptCode\":\"test3\",\n" + + "\t\"DeptName\":\"科室测试名称3\",\n" + + "\t\"DeptLevel\": null, \n" + + "\t\"ParentDeptCode\": \"test2\",\n" + + "\t\"IsDelete\": false, \n" + + "\t\"Remark\":\"备注\"\n" + "}\n"; + Map map = JSONObject.parseObject(json, Map.class); service.async("4bce0693734a","dept",map); }