From 3573217507615d54c3c201f04ad39a5e6f63b1af Mon Sep 17 00:00:00 2001 From: maimaishu <14610861+maimaishu@user.noreply.gitee.com> Date: Wed, 31 Dec 2025 16:55:20 +0800 Subject: [PATCH] =?UTF-8?q?[+]=E5=A2=9E=E5=8A=A0=E9=83=A8=E9=97=A8?= =?UTF-8?q?=E6=8E=A8=E5=8A=A8=E6=B6=88=E6=81=AF=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zt/plat/module/system/api/esp/EspApi.java | 94 +++++++ .../module/system/api/esp/dto/EspDto.java | 27 ++ .../controller/admin/dept/EspController.java | 117 ++++++++ .../dept/vo/depexternalcode/EspPageReqVO.java | 25 ++ .../vo/depexternalcode/EspSaveRespVo.java | 28 ++ .../dal/dataobject/dept/DeptPushMsgDO.java | 58 ++++ .../system/dal/mysql/dept/EspMapper.java | 57 ++++ .../system/service/dept/EspServiceImpl.java | 256 ++++++++++++++++++ .../system/service/dept/IEspService.java | 79 ++++++ 9 files changed, 741 insertions(+) create mode 100644 zt-module-system/zt-module-system-api/src/main/java/com/zt/plat/module/system/api/esp/EspApi.java create mode 100644 zt-module-system/zt-module-system-api/src/main/java/com/zt/plat/module/system/api/esp/dto/EspDto.java create mode 100644 zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/EspController.java create mode 100644 zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/vo/depexternalcode/EspPageReqVO.java create mode 100644 zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/vo/depexternalcode/EspSaveRespVo.java create mode 100644 zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/dal/dataobject/dept/DeptPushMsgDO.java create mode 100644 zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/dal/mysql/dept/EspMapper.java create mode 100644 zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/service/dept/EspServiceImpl.java create mode 100644 zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/service/dept/IEspService.java diff --git a/zt-module-system/zt-module-system-api/src/main/java/com/zt/plat/module/system/api/esp/EspApi.java b/zt-module-system/zt-module-system-api/src/main/java/com/zt/plat/module/system/api/esp/EspApi.java new file mode 100644 index 00000000..a7c338a5 --- /dev/null +++ b/zt-module-system/zt-module-system-api/src/main/java/com/zt/plat/module/system/api/esp/EspApi.java @@ -0,0 +1,94 @@ +package com.zt.plat.module.system.api.esp; + +import com.zt.plat.framework.common.pojo.CommonResult; +import com.zt.plat.framework.common.util.collection.CollectionUtils; +import com.zt.plat.module.system.api.dept.dto.*; +import com.zt.plat.module.system.enums.ApiConstants; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.*; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +@FeignClient(name = ApiConstants.NAME) +@Tag(name = "RPC 服务 - 部门") +public interface EspApi { + + String PREFIX = ApiConstants.PREFIX + "/dept"; + + // === 以下为补全的接口方法 === + @PostMapping(PREFIX + "/create") + @Operation(summary = "新增部门") + CommonResult createDept(@RequestBody DeptSaveReqDTO createReqVO); + + @PutMapping(PREFIX + "/update") + @Operation(summary = "修改部门") + CommonResult updateDept(@RequestBody DeptSaveReqDTO updateReqVO); + + @DeleteMapping(PREFIX + "/delete") + @Operation(summary = "删除部门") + CommonResult deleteDept(@RequestParam("id") Long id); + + @PostMapping(PREFIX + "/list-all") + @Operation(summary = "获得部门列表") + CommonResult> getDeptList(@RequestBody DeptListReqDTO reqVO); + + @GetMapping(PREFIX + "/simple-list") + @Operation(summary = "获得部门精简信息列表") + CommonResult> getSimpleDeptList(); + + @GetMapping(PREFIX + "/simple-company-list") + @Operation(summary = "获得公司精简信息列表") + CommonResult> getSimpleCompanyList(); + + @GetMapping(PREFIX + "/all-company-list") + @Operation(summary = "获得所有公司精简信息列表") + CommonResult> getAllCompanyList(); + + @GetMapping(PREFIX + "/get") + @Operation(summary = "获得部门信息") + @Parameter(name = "id", description = "部门编号", example = "1024", required = true) + CommonResult getDept(@RequestParam("id") Long id); + + @GetMapping(PREFIX + "/list") + @Operation(summary = "获得部门信息数组") + @Parameter(name = "ids", description = "部门编号数组", example = "1,2", required = true) + CommonResult> getDeptList(@RequestParam("ids") Collection ids); + + @GetMapping(PREFIX + "/valid") + @Operation(summary = "校验部门是否合法") + @Parameter(name = "ids", description = "部门编号数组", example = "1,2", required = true) + CommonResult validateDeptList(@RequestParam("ids") Collection ids); + + /** + * 获得指定编号的部门 Map + * + * @param ids 部门编号数组 + * @return 部门 Map + */ + default Map getDeptMap(Collection ids) { + List list = getDeptList(ids).getCheckedData(); + return CollectionUtils.convertMap(list, DeptRespDTO::getId); + } + + @GetMapping(PREFIX + "/list-child") + @Operation(summary = "获得指定部门的所有子部门") + @Parameter(name = "id", description = "部门编号", example = "1024", required = true) + CommonResult> getChildDeptList(@RequestParam("id") Long id); + + @GetMapping(PREFIX + "/company-dept-info") + @Operation(summary = "获得指定用户的公司部门信息") + @Parameter(name = "userId", description = "用户编号", example = "1", required = true) + CommonResult> getCompanyDeptInfoListByUserId(@RequestParam("userId") Long userId); + + // ========== 数据同步专用接口 ========== + + @PostMapping(PREFIX + "/sync") + @Operation(summary = "同步部门") + CommonResult syncDept(@RequestBody DeptSaveReqDTO syncReqDTO); + +} diff --git a/zt-module-system/zt-module-system-api/src/main/java/com/zt/plat/module/system/api/esp/dto/EspDto.java b/zt-module-system/zt-module-system-api/src/main/java/com/zt/plat/module/system/api/esp/dto/EspDto.java new file mode 100644 index 00000000..d69e1ab2 --- /dev/null +++ b/zt-module-system/zt-module-system-api/src/main/java/com/zt/plat/module/system/api/esp/dto/EspDto.java @@ -0,0 +1,27 @@ +package com.zt.plat.module.system.api.esp.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import java.util.List; + +@Schema(description = "RPC 服务 - 推送外部系统配置信息 Response DTO") +@Data +public class EspDto { + + @Schema(description = "部门名称,模糊匹配", example = "ZT") + private String name; + + @Schema(description = "展示状态,参见 CommonStatusEnum 枚举类", example = "1") + private Integer status; + + @Schema(description = "是否公司", example = "false") + private Boolean isCompany; + + @Schema(description = "是否集团", example = "false") + private Boolean isGroup; + + @Schema(description = "部门编号集合,支持多部门查询", example = "[\"1001\", \"1002\"]") + private List ids; + + +} diff --git a/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/EspController.java b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/EspController.java new file mode 100644 index 00000000..8ae5afa5 --- /dev/null +++ b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/EspController.java @@ -0,0 +1,117 @@ +package com.zt.plat.module.system.controller.admin.dept; + +import com.zt.plat.framework.common.pojo.CommonResult; +import com.zt.plat.framework.common.pojo.PageResult; +import com.zt.plat.framework.common.util.object.BeanUtils; +import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.EspPageReqVO; +import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.EspSaveRespVo; +import com.zt.plat.module.system.dal.dataobject.dept.DeptDO; +import com.zt.plat.module.system.dal.dataobject.dept.DeptPushMsgDO; +import com.zt.plat.module.system.service.dept.DeptService; +import com.zt.plat.module.system.service.dept.IEspService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import static com.zt.plat.framework.common.pojo.CommonResult.success; + +@Tag(name = "管理后台 - 部门推送消息") +@RestController +@RequestMapping("/system/esp") +@Validated +public class EspController +{ + + @Resource + private IEspService espService; + @Resource + private DeptService deptService; + + @PostMapping("/create") + @Operation(summary = "创建部门推送消息") + @PreAuthorize("@ss.hasPermission('system:esp-external-code:create')") + public CommonResult create(@Valid @RequestBody EspSaveRespVo createReqVO) { + Long id = espService.createDeptPushMsg(createReqVO); + return success(id); + } + + @PutMapping("/update") + @Operation(summary = "修改部门推送消息") + @PreAuthorize("@ss.hasPermission('system:esp-external-code:update')") + public CommonResult update(@Valid @RequestBody EspSaveRespVo updateReqVO) { + espService.updateDeptPushMsg(updateReqVO); + return success(true); + } + + @DeleteMapping("/delete") + @Operation(summary = "删除部门推送消息") + @PreAuthorize("@ss.hasPermission('system:esp-external-code:delete')") + public CommonResult delete(@RequestParam("id") Long id) { + espService.deleteDeptPushMsg(id); + return success(true); + } + + @GetMapping("/get") + @Operation(summary = "获取部门推送消息详情") + @PreAuthorize("@ss.hasPermission('system:esp-external-code:query')") + public CommonResult get(@RequestParam("id") Long id) { + DeptPushMsgDO entity = espService.getDeptPushMsgDetails(id); + EspSaveRespVo respVO = BeanUtils.toBean(entity, EspSaveRespVo.class); + fillDeptInfo(List.of(respVO)); + return success(respVO); + } + + @GetMapping("/page") + @Operation(summary = "分页查询部门推送消息") + @PreAuthorize("@ss.hasPermission('system:esp-external-code:query')") + public CommonResult> page(@Valid EspPageReqVO reqVO) { + PageResult pageResult = espService.getDeptExternalCodePage(reqVO); + PageResult result = BeanUtils.toBean(pageResult, EspSaveRespVo.class); + fillDeptInfo(result.getList()); + return success(result); + } + + @GetMapping("/list-by-dept") + @Operation(summary = "根据部门部门推送消息") + @Parameter(name = "deptId", description = "部门编号", required = true, example = "1024") + @PreAuthorize("@ss.hasPermission('system:esp-external-code:query')") + public CommonResult> listByDept(@RequestParam("deptId") Long deptId) { + List list = espService.getPushMsgByDeptId(deptId); + List respList = BeanUtils.toBean(list, EspSaveRespVo.class); + fillDeptInfo(respList); + return success(respList); + } + + + + private void fillDeptInfo(List list) { + if (list == null || list.isEmpty()) { + return; + } + Set deptIds = list.stream() + .map(EspSaveRespVo::getDeptId) + .collect(Collectors.toCollection(HashSet::new)); + if (deptIds == null || deptIds.isEmpty()) { + return; + } + Map deptMap = deptService.getDeptList(deptIds).stream() + .collect(Collectors.toMap(DeptDO::getId, dept -> dept, (left, right) -> left)); + list.forEach(item -> { + DeptDO dept = deptMap.get(item.getDeptId()); + if (dept != null) { + item.setDeptName(dept.getName()); + item.setDeptCode(dept.getCode()); + } + }); + } + +} diff --git a/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/vo/depexternalcode/EspPageReqVO.java b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/vo/depexternalcode/EspPageReqVO.java new file mode 100644 index 00000000..10e0ab8b --- /dev/null +++ b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/vo/depexternalcode/EspPageReqVO.java @@ -0,0 +1,25 @@ +package com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode; + +import com.zt.plat.framework.common.pojo.PageParam; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Schema(description = "管理后台 - 部门外部组织编码映射分页 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +public class EspPageReqVO extends PageParam { + + @Schema(description = "部门编号", example = "1024") + private Long deptId; + + @Schema(description = "外部系统标识", example = "ERP") + private String systemCode; + + @Schema(description = "外部组织编码", example = "100200") + private String externalDeptCode; + + @Schema(description = "状态", example = "0") + private Integer status; + +} diff --git a/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/vo/depexternalcode/EspSaveRespVo.java b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/vo/depexternalcode/EspSaveRespVo.java new file mode 100644 index 00000000..859ba772 --- /dev/null +++ b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/controller/admin/dept/vo/depexternalcode/EspSaveRespVo.java @@ -0,0 +1,28 @@ +package com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; +import java.time.LocalDateTime; + +@Schema(description = "管理后台 - 部门外消息推送创建/修改 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +public class EspSaveRespVo extends DeptExternalCodeBaseVO { + + @Schema(description = "映射编号", example = "1024") + private Long id; + + @Schema(description = "所属部门名称", example = "技术部") + private String deptName; + + @Schema(description = "所属部门编码", example = "DEPT_001") + private String deptCode; + + @Schema(description = "创建时间") + private LocalDateTime createTime; + + @Schema(description = "最后更新时间") + private LocalDateTime updateTime; + +} diff --git a/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/dal/dataobject/dept/DeptPushMsgDO.java b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/dal/dataobject/dept/DeptPushMsgDO.java new file mode 100644 index 00000000..237c28bc --- /dev/null +++ b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/dal/dataobject/dept/DeptPushMsgDO.java @@ -0,0 +1,58 @@ +package com.zt.plat.module.system.dal.dataobject.dept; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.KeySequence; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.zt.plat.framework.common.enums.CommonStatusEnum; +import com.zt.plat.framework.tenant.core.db.TenantBaseDO; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * 部门推送消息 DO + */ +@TableName("system_dept_push_msg") +@KeySequence("system_dept_push_msg_seq") +@Data +@EqualsAndHashCode(callSuper = true) +public class DeptPushMsgDO extends TenantBaseDO { + + /** + * 主键编号 + */ + @TableId(type = IdType.ASSIGN_ID) + private Long id; + + /** + * 本系统部门 ID + */ + private Long deptId; + + /** + * 外部系统标识 + */ + private String systemCode; + + /** + * 外部系统组织编码 + */ + private String externalDeptCode; + + /** + * 外部系统组织名称 + */ + private String externalDeptName; + + /** + * 映射状态 + * 枚举 {@link CommonStatusEnum} + */ + private Integer status; + + /** + * 备注 + */ + private String remark; + +} diff --git a/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/dal/mysql/dept/EspMapper.java b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/dal/mysql/dept/EspMapper.java new file mode 100644 index 00000000..7f4cc147 --- /dev/null +++ b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/dal/mysql/dept/EspMapper.java @@ -0,0 +1,57 @@ +package com.zt.plat.module.system.dal.mysql.dept; + +import com.zt.plat.framework.common.pojo.PageResult; +import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX; +import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX; +import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.EspPageReqVO; +import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.EspSaveRespVo; +import com.zt.plat.module.system.dal.dataobject.dept.DeptPushMsgDO; +import org.apache.ibatis.annotations.Mapper; +import java.util.List; +/** + * 部门推送消息接口Mapper + */ +@Mapper +public interface EspMapper extends BaseMapperX { + + + /** + * 分页查询 + * @param reqVO 消息推送VO + * @return PageResult + */ + default PageResult selectPage(EspPageReqVO reqVO) { + return selectPage(reqVO, new LambdaQueryWrapperX() + .eqIfPresent(DeptPushMsgDO::getDeptId,reqVO.getDeptId()) + .eqIfPresent(DeptPushMsgDO::getSystemCode, reqVO.getSystemCode()) + .likeIfPresent(DeptPushMsgDO::getExternalDeptCode, reqVO.getExternalDeptCode()) + .eqIfPresent(DeptPushMsgDO::getStatus, reqVO.getStatus()) + .orderByDesc(DeptPushMsgDO::getId)); + } + + + default DeptPushMsgDO selectBySystemCodeAndDeptId(String systemCode, Long deptId) { + return selectOne(new LambdaQueryWrapperX() + .eq(DeptPushMsgDO::getSystemCode, systemCode) + .eq(DeptPushMsgDO::getDeptId, deptId)); + } + + default DeptPushMsgDO selectBySystemCodeAndExternalCode(String systemCode, String externalDeptCode) { + return selectOne(new LambdaQueryWrapperX() + .eq(DeptPushMsgDO::getSystemCode, systemCode) + .eq(DeptPushMsgDO::getExternalDeptCode, externalDeptCode)); + } + + default List selectListByDeptId(Long deptId) { + return selectList(DeptPushMsgDO::getDeptId, deptId); + } + + default int deleteByDeptId(Long deptId) { + return delete(DeptPushMsgDO::getDeptId, deptId); + } + + default List selectListBySystemCode(String systemCode) { + return selectList(DeptPushMsgDO::getSystemCode, systemCode); + } + +} diff --git a/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/service/dept/EspServiceImpl.java b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/service/dept/EspServiceImpl.java new file mode 100644 index 00000000..bd746bfb --- /dev/null +++ b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/service/dept/EspServiceImpl.java @@ -0,0 +1,256 @@ +package com.zt.plat.module.system.service.dept; + +import cn.hutool.core.util.StrUtil; +import com.zt.plat.framework.common.enums.CommonStatusEnum; +import com.zt.plat.framework.common.pojo.PageResult; +import com.zt.plat.framework.common.util.object.BeanUtils; +import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.EspPageReqVO; +import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.EspSaveRespVo; +import com.zt.plat.module.system.dal.dataobject.dept.DeptDO; +import com.zt.plat.module.system.dal.dataobject.dept.DeptPushMsgDO; +import com.zt.plat.module.system.dal.mysql.dept.DeptMapper; +import com.zt.plat.module.system.dal.mysql.dept.EspMapper; +import com.zt.plat.module.system.dal.redis.RedisKeyConstants; +import jakarta.annotation.Resource; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; +import java.util.List; +import java.util.Objects; +import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception; +import static com.zt.plat.module.system.enums.ErrorCodeConstants.*; + +/** + * 部门推送消息接口ServiceImpl实现类 + */ +@Service +@Validated +public class EspServiceImpl implements IEspService { + + @Resource + private EspMapper espMapper; + @Resource + private DeptMapper deptMapper; + @Resource + private CacheManager cacheManager; + + @Override + @CacheEvict(cacheNames = RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST, key = "#createReqVO.deptId", beforeInvocation = false) + public Long createDeptPushMsg(EspSaveRespVo createReqVO) { + + //请求校验 + normalizeRequest(createReqVO); + //冲突禁用-映射 + disableActiveMappingIfConflict(createReqVO.getDeptId(), createReqVO.getSystemCode(), createReqVO.getExternalDeptCode()); + validateForCreateOrUpdate(null, createReqVO.getDeptId(), createReqVO.getSystemCode(), + createReqVO.getExternalDeptCode()); + + DeptPushMsgDO entity = BeanUtils.toBean(createReqVO, DeptPushMsgDO.class); + if (entity.getStatus() == null) { + entity.setStatus(CommonStatusEnum.ENABLE.getStatus()); + } + espMapper.insert(entity); + return entity.getId(); + } + + @Override + public void updateDeptPushMsg(EspSaveRespVo updateReqVO) { + normalizeRequest(updateReqVO); + DeptPushMsgDO exists = validateExists(updateReqVO.getId()); + disableActiveMappingIfConflict(updateReqVO.getDeptId(), updateReqVO.getSystemCode(), updateReqVO.getExternalDeptCode()); + validateForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getDeptId(), updateReqVO.getSystemCode(), + updateReqVO.getExternalDeptCode()); + + DeptPushMsgDO updateObj = BeanUtils.toBean(updateReqVO, DeptPushMsgDO.class); + // 保持原有的状态默认值逻辑 + if (updateObj.getStatus() == null) { + updateObj.setStatus(exists.getStatus() == null ? CommonStatusEnum.ENABLE.getStatus() : exists.getStatus()); + } + espMapper.updateById(updateObj); + evictCacheSafely(exists.getDeptId()); + evictCacheSafely(updateObj.getDeptId()); + } + + @Override + public void deleteDeptPushMsg(Long id) { + DeptPushMsgDO exists = validateExists(id); + espMapper.deleteById(id); + evictCacheSafely(exists.getDeptId()); + } + + @Override + public DeptPushMsgDO getDeptPushMsgDetails(Long id) { + return espMapper.selectById(id); + } + + @Override + public PageResult getDeptExternalCodePage(EspPageReqVO reqVO) { + + return espMapper.selectPage(reqVO); + } + + @Override + @Cacheable(cacheNames = RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST, key = "#deptId") + public List getPushMsgByDeptId(Long deptId) { + return espMapper.selectListByDeptId(deptId); + } + +/* @Override + public DeptExternalCodeDO getBySystemCodeAndExternalCode(String systemCode, String externalDeptCode) { + if (StrUtil.hasEmpty(systemCode, externalDeptCode)) { + return null; + } + return espMapper.selectBySystemCodeAndExternalCode(systemCode.trim(), externalDeptCode.trim()); + }*/ + +/* @Override + public DeptExternalCodeDO getBySystemCodeAndDeptId(String systemCode, Long deptId) { + if (StrUtil.isBlank(systemCode) || deptId == null) { + return null; + } + return espMapper.selectBySystemCodeAndDeptId(systemCode.trim(), deptId); + }*/ + +/* @Override + public Long getDeptPushMsgDetails(Long deptId, String systemCode, String externalDeptCode, + String externalDeptName, Integer status) { + + if (StrUtil.hasEmpty(systemCode, externalDeptCode) || deptId == null) { + return null; + } + String normalizedSystemCode = systemCode.trim(); + String normalizedExternalCode = externalDeptCode.trim(); + String normalizedExternalName = StrUtil.blankToDefault(StrUtil.trimToNull(externalDeptName), null); + + disableActiveMappingIfConflict(deptId, normalizedSystemCode, normalizedExternalCode); + + // 如果存在则更新,否则创建 + DeptExternalCodeDO exists = espMapper.selectBySystemCodeAndDeptId(normalizedSystemCode, deptId); + if (exists != null) { + DeptExternalCodeSaveReqVO updateReqVO = new DeptExternalCodeSaveReqVO(); + updateReqVO.setId(exists.getId()); + updateReqVO.setDeptId(deptId); + updateReqVO.setSystemCode(normalizedSystemCode); + updateReqVO.setExternalDeptCode(normalizedExternalCode); + updateReqVO.setExternalDeptName(normalizedExternalName); + updateReqVO.setStatus(status == null ? exists.getStatus() : status); + + //TODO + //getDeptPushMsgDetails(updateReqVO); + return exists.getId(); + } + + DeptExternalCodeSaveReqVO createReqVO = new DeptExternalCodeSaveReqVO(); + createReqVO.setDeptId(deptId); + createReqVO.setSystemCode(normalizedSystemCode); + createReqVO.setExternalDeptCode(normalizedExternalCode); + createReqVO.setExternalDeptName(normalizedExternalName); + createReqVO.setStatus(status == null ? CommonStatusEnum.ENABLE.getStatus() : status); + return getDeptPushMsgDetails(createReqVO); + }*/ + +/* @Override + public void deleteDeptExternalCodesByDeptId(Long deptId) { + if (deptId == null) { + return; + } + espMapper.deleteByDeptId(deptId); + evictCacheSafely(deptId); + }*/ + + private DeptPushMsgDO validateExists(Long id) { + if (id == null) { + throw exception(DEPT_EXTERNAL_RELATION_NOT_EXISTS); + } + DeptPushMsgDO entity = espMapper.selectById(id); + if (entity == null) { + throw exception(DEPT_EXTERNAL_RELATION_NOT_EXISTS); + } + return entity; + } + + private void validateForCreateOrUpdate(Long id, Long deptId, String systemCode, String externalDeptCode) { + + // 校验部门存在 + DeptDO dept = deptMapper.selectById(deptId); + if (dept == null) { + throw exception(DEPT_NOT_FOUND); + } + String normalizedSystemCode = StrUtil.blankToDefault(systemCode, null); + String normalizedExternalCode = StrUtil.blankToDefault(externalDeptCode, null); + + // 校验同一系统下部门唯一 + if (StrUtil.isNotBlank(normalizedSystemCode)) { + DeptPushMsgDO sameDept = espMapper + .selectBySystemCodeAndDeptId(normalizedSystemCode, deptId); + if (sameDept != null && (id == null || !sameDept.getId().equals(id))) { + throw exception(DEPT_EXTERNAL_RELATION_EXISTS, normalizedSystemCode); + } + } + // 校验同一系统下外部编码唯一 + if (StrUtil.isNotBlank(normalizedSystemCode) && StrUtil.isNotBlank(normalizedExternalCode)) { + DeptPushMsgDO sameExternal = espMapper + .selectBySystemCodeAndExternalCode(normalizedSystemCode, normalizedExternalCode); + if (sameExternal != null && (id == null || !sameExternal.getId().equals(id))) { + boolean sameDept = Objects.equals(deptId, sameExternal.getDeptId()); + boolean activeConflict = !sameDept && CommonStatusEnum.isEnable(sameExternal.getStatus()); + if (activeConflict) { + throw exception(DEPT_EXTERNAL_CODE_DUPLICATE, normalizedSystemCode, normalizedExternalCode); + } + } + } + } + + private void normalizeRequest(EspSaveRespVo reqVO) { + if (reqVO == null) { + return; + } + if (StrUtil.isNotBlank(reqVO.getSystemCode())) { + reqVO.setSystemCode(reqVO.getSystemCode().trim()); + } + if (StrUtil.isNotBlank(reqVO.getExternalDeptCode())) { + reqVO.setExternalDeptCode(reqVO.getExternalDeptCode().trim()); + } + if (reqVO.getStatus() == null) { + reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); + } + } + + private void disableActiveMappingIfConflict(Long targetDeptId, String systemCode, String externalDeptCode) { + String normalizedSystem = StrUtil.trimToNull(systemCode); + String normalizedExternal = StrUtil.trimToNull(externalDeptCode); + if (StrUtil.hasEmpty(normalizedSystem, normalizedExternal) || targetDeptId == null) { + return; + } + DeptPushMsgDO existing = espMapper.selectBySystemCodeAndExternalCode(normalizedSystem, normalizedExternal); + if (existing == null) { + return; + } + if (Objects.equals(existing.getDeptId(), targetDeptId)) { + return; + } + if (CommonStatusEnum.isEnable(existing.getStatus())) { + DeptPushMsgDO update = new DeptPushMsgDO(); + update.setId(existing.getId()); + update.setStatus(CommonStatusEnum.DISABLE.getStatus()); + espMapper.updateById(update); + evictCacheSafely(existing.getDeptId()); + } + } + + private void evictCacheSafely(Long deptId) { + + if (deptId == null || cacheManager == null) { + return; + } + try { + if (cacheManager.getCache(RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST) != null) { + cacheManager.getCache(RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST).evict(deptId); + } + } catch (Exception ignore) { + // 缓存失效失败不影响主流程 + } + } +} diff --git a/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/service/dept/IEspService.java b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/service/dept/IEspService.java new file mode 100644 index 00000000..97c83454 --- /dev/null +++ b/zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/service/dept/IEspService.java @@ -0,0 +1,79 @@ +package com.zt.plat.module.system.service.dept; + +import com.zt.plat.framework.common.pojo.PageResult; +import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.EspPageReqVO; +import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.EspSaveRespVo; +import com.zt.plat.module.system.dal.dataobject.dept.DeptPushMsgDO; + +import java.util.List; + +/** + * 部门推送消息 Service 接口 + */ +public interface IEspService { + + /** + * 创建映射关系 + * @param createReqVO 创建请求 + * @return 新增记录编号 + */ + Long createDeptPushMsg(EspSaveRespVo createReqVO); + + /** + * 更新映射关系 + * @param updateReqVO 更新请求 + */ + void updateDeptPushMsg(EspSaveRespVo updateReqVO); + + /** + * 删除映射关系 + * + * @param id 记录编号 + */ + void deleteDeptPushMsg(Long id); + + /** + * 获取映射详情 + */ + DeptPushMsgDO getDeptPushMsgDetails(Long id); + + /** + * 分页查询映射 + */ + PageResult getDeptExternalCodePage(EspPageReqVO reqVO); + + /** + * 根据部门推送消息 + */ + List getPushMsgByDeptId(Long deptId); + + /** + * 根据部门与外部系统保存/更新映射(存在则更新,不存在则创建) + * @param deptId 本系统部门 ID + * @param systemCode 外部系统标识 + * @param externalDeptCode 外部系统组织编码 + * @param externalDeptName 外部系统组织名称(可选) + * @param status 状态,默认启用 + * @return 映射记录 ID + */ + /* Long getDeptPushMsgDetails(Long deptId, String systemCode, String externalDeptCode, String externalDeptName, + Integer status);*/ + + /** + * 根据部门删除全部外部编码映射 + * + * @param deptId 部门编号 + */ + //void deleteDeptExternalCodesByDeptId(Long deptId); + + /** + * 根据外部系统与外部组织编码查询映射 + */ + //DeptExternalCodeDO getBySystemCodeAndExternalCode(String systemCode, String externalDeptCode); + + /** + * 根据外部系统与部门编号查询映射 + */ + //DeptExternalCodeDO getBySystemCodeAndDeptId(String systemCode, Long deptId); + +}