|
|
@@ -0,0 +1,143 @@
|
|
|
+package org.dromara.server.sync.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.dromara.common.core.domain.R;
|
|
|
+import org.dromara.common.core.utils.MapstructUtils;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.common.message.kafka.constant.EventTypeConstants;
|
|
|
+import org.dromara.common.message.kafka.constant.KafkaTopicConstants;
|
|
|
+import org.dromara.common.message.kafka.enums.EventSenderEnum;
|
|
|
+import org.dromara.common.tenant.helper.TenantHelper;
|
|
|
+import org.dromara.server.sync.domain.SysDept;
|
|
|
+import org.dromara.server.sync.domain.bo.SysDeptBo;
|
|
|
+import org.dromara.server.sync.domain.vo.SysDeptVo;
|
|
|
+import org.dromara.server.sync.mapper.SysDeptMapper;
|
|
|
+import org.dromara.server.sync.mq.PushKafkaData;
|
|
|
+import org.dromara.server.sync.service.IDeptService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName DeptServiceImpl
|
|
|
+ * @Description 部门服务接口实现
|
|
|
+ * @Author luoyibo
|
|
|
+ * @Date 2025-01-23 21:33
|
|
|
+ * @Version 1.0
|
|
|
+ * @since jdk17
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class DeptServiceImpl implements IDeptService {
|
|
|
+ private final SysDeptMapper deptMapper;
|
|
|
+ private final PushKafkaData kafkaNormalProducer;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SysDeptVo selectByOtherId(String otherId, String tenantId) {
|
|
|
+ return TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectVoOne(new LambdaQueryWrapper<SysDept>().eq(SysDept::getOtherId, otherId).eq(SysDept::getTenantId, tenantId)));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SysDeptVo selectByRootId(String tenantId) {
|
|
|
+ return TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectVoOne(new LambdaQueryWrapper<SysDept>().eq(SysDept::getParentId, 0L).eq(SysDept::getTenantId, tenantId)));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean insertDept(SysDeptBo bo) {
|
|
|
+ SysDept entity = MapstructUtils.convert(bo, SysDept.class);
|
|
|
+ if (bo.getOrderNum() == null || bo.getOrderNum() == 0) {
|
|
|
+ Objects.requireNonNull(entity).setOrderNum(this.getMaxLevelOrderNumb(bo.getParentId()));
|
|
|
+ }
|
|
|
+ Objects.requireNonNull(entity).setAncestors(getAncestors(bo.getParentId()));
|
|
|
+ int iCount = TenantHelper.ignore(
|
|
|
+ () -> deptMapper.insert(entity));
|
|
|
+ if (iCount > 0) {
|
|
|
+ bo.setDeptId(entity.getDeptId());
|
|
|
+ SysDeptVo vo = TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectVoById(bo.getDeptId()));
|
|
|
+ kafkaNormalProducer.sendKafkaMessage(KafkaTopicConstants.SYNC_DATA_TOPIC, EventTypeConstants.DEPT, EventSenderEnum.SYSTEM.code(), vo);
|
|
|
+ }
|
|
|
+ return iCount > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateDept(SysDeptBo bo) {
|
|
|
+ SysDept entity = MapstructUtils.convert(bo, SysDept.class);
|
|
|
+ SysDept old = TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectById(bo.getDeptId()));
|
|
|
+ if (entity != null && ObjectUtil.notEqual(entity.getParentId(), old.getParentId())) {
|
|
|
+ // 上级部门变了,要更新祖级列表
|
|
|
+ entity.setAncestors(getAncestors(entity.getParentId()));
|
|
|
+ }
|
|
|
+ int iCount = TenantHelper.ignore(
|
|
|
+ () -> deptMapper.updateById(entity));
|
|
|
+ if (iCount > 0) {
|
|
|
+ SysDeptVo vo = TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectVoById(bo.getDeptId()));
|
|
|
+ kafkaNormalProducer.sendKafkaMessage(KafkaTopicConstants.SYNC_DATA_TOPIC, EventTypeConstants.DEPT, EventSenderEnum.SYSTEM.code(), vo);
|
|
|
+ }
|
|
|
+ return iCount > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<SysDeptVo> insertReturnDept(SysDeptBo bo) {
|
|
|
+ Boolean result = this.insertDept(bo);
|
|
|
+ if (result) {
|
|
|
+ return R.ok(TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectVoById(bo.getDeptId())));
|
|
|
+ }
|
|
|
+ return R.fail();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<SysDeptVo> updateReturnDept(SysDeptBo bo) {
|
|
|
+ Boolean result = this.updateDept(bo);
|
|
|
+ if (result) {
|
|
|
+ return R.ok(TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectVoById(bo.getDeptId())));
|
|
|
+ }
|
|
|
+ return R.fail();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean deleteByOtherId(String otherId, String tenantId) {
|
|
|
+ // TODO 2025-01-24 10:41:56 luoyibo 删除前的校验
|
|
|
+ int iCount = TenantHelper.ignore(
|
|
|
+ () -> deptMapper.delete(new LambdaQueryWrapper<SysDept>().eq(SysDept::getOtherId, otherId).eq(SysDept::getTenantId, tenantId)));
|
|
|
+ if (iCount > 0) {
|
|
|
+ SysDeptVo vo = TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectVoOne(new LambdaQueryWrapper<SysDept>().eq(SysDept::getOtherId, otherId).eq(SysDept::getTenantId, tenantId)));
|
|
|
+ kafkaNormalProducer.sendKafkaMessage(KafkaTopicConstants.SYNC_DATA_TOPIC, EventTypeConstants.DEPT, EventSenderEnum.SYSTEM.code(), vo);
|
|
|
+ }
|
|
|
+ return iCount > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getAncestors(Long parentId) {
|
|
|
+ if (ObjectUtil.equals(parentId, 0L)) {
|
|
|
+ return "0";
|
|
|
+ }
|
|
|
+ SysDept parent = TenantHelper.ignore(
|
|
|
+ () -> deptMapper.selectById(parentId));
|
|
|
+
|
|
|
+ return parent.getAncestors() + StringUtils.SEPARATOR + parentId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer getMaxLevelOrderNumb(Long deptId) {
|
|
|
+ QueryWrapper<SysDept> qw = new QueryWrapper<>();
|
|
|
+ qw.select("max(order_num) as order_num");
|
|
|
+ qw.eq("parent_id", deptId);
|
|
|
+ qw.lt("order_num", 999);
|
|
|
+
|
|
|
+ SysDept dept = TenantHelper.ignore(() -> deptMapper.selectOne(qw));
|
|
|
+ if (dept == null) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return dept.getOrderNum() + 1;
|
|
|
+ }
|
|
|
+}
|