|
|
@@ -1,27 +1,33 @@
|
|
|
package org.dromara.customer.service.impl;
|
|
|
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import org.dromara.common.core.constant.SystemConstants;
|
|
|
-import org.dromara.common.core.exception.ServiceException;
|
|
|
-import org.dromara.common.core.utils.MapstructUtils;
|
|
|
-import org.dromara.common.core.utils.StringUtils;
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.lang.tree.Tree;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.dromara.common.redis.utils.SequenceUtils;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.dromara.common.core.context.PlatformContext;
|
|
|
+import org.dromara.common.core.enums.IsDefault;
|
|
|
+import org.dromara.common.core.utils.MapstructUtils;
|
|
|
+import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.customer.domain.CustomerDept;
|
|
|
+import org.dromara.customer.domain.CustomerInfo;
|
|
|
import org.dromara.customer.domain.bo.CustomerDeptBo;
|
|
|
+import org.dromara.customer.domain.vo.CustomerDeptTreeVo;
|
|
|
import org.dromara.customer.domain.vo.CustomerDeptVo;
|
|
|
-import org.dromara.customer.domain.CustomerDept;
|
|
|
import org.dromara.customer.mapper.CustomerDeptMapper;
|
|
|
import org.dromara.customer.service.ICustomerDeptService;
|
|
|
+import org.dromara.system.api.RemoteDeptService;
|
|
|
+import org.dromara.system.api.domain.vo.RemoteDeptVo;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.time.Duration;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Collection;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 客户部门信息Service业务层处理
|
|
|
@@ -34,6 +40,9 @@ import java.util.Collection;
|
|
|
@Service
|
|
|
public class CustomerDeptServiceImpl extends ServiceImpl<CustomerDeptMapper, CustomerDept> implements ICustomerDeptService {
|
|
|
|
|
|
+ @DubboReference
|
|
|
+ private RemoteDeptService remoteDeptService;
|
|
|
+
|
|
|
private static final String DEPT_NO_KEY = "customer_dept:dept_no";
|
|
|
|
|
|
private final CustomerDeptMapper baseMapper;
|
|
|
@@ -62,16 +71,64 @@ public class CustomerDeptServiceImpl extends ServiceImpl<CustomerDeptMapper, Cus
|
|
|
return baseMapper.selectVoList(lqw);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<CustomerDeptTreeVo> getCustomerDeptTree(Long customerId) {
|
|
|
+ if (customerId == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 查 customer_dept
|
|
|
+ List<CustomerDept> customerDepts = baseMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<CustomerDept>()
|
|
|
+ .eq(CustomerDept::getCustomerId, customerId)
|
|
|
+ );
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(customerDepts)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 查 sys_dept
|
|
|
+ List<Long> deptIds = customerDepts.stream()
|
|
|
+ .map(CustomerDept::getDeptId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ List<RemoteDeptVo> sysDepts = remoteDeptService.selectDeptByIds(deptIds);
|
|
|
+
|
|
|
+ Map<Long, RemoteDeptVo> deptMap = sysDepts.stream()
|
|
|
+ .collect(Collectors.toMap(RemoteDeptVo::getDeptId, d -> d));
|
|
|
+
|
|
|
+ // 3. 合并 + 安全排序
|
|
|
+ return customerDepts.stream()
|
|
|
+ .filter(cd -> deptMap.containsKey(cd.getDeptId()))
|
|
|
+ .map(cd -> {
|
|
|
+ RemoteDeptVo sys = deptMap.get(cd.getDeptId());
|
|
|
+ CustomerDeptTreeVo vo = new CustomerDeptTreeVo();
|
|
|
+ vo.setDeptId(sys.getDeptId());
|
|
|
+ vo.setParentId(sys.getParentId());
|
|
|
+ vo.setDeptName(sys.getDeptName());
|
|
|
+ vo.setOrderNum(sys.getOrderNum()); // 可能为 null!
|
|
|
+ vo.setStatus(sys.getStatus());
|
|
|
+
|
|
|
+ vo.setYearlyBudget(cd.getYearlyBudget());
|
|
|
+ vo.setUsedBudget(cd.getUsedBudget());
|
|
|
+ vo.setMonthLimit(cd.getMonthLimit());
|
|
|
+ vo.setBindAddress(cd.getBindAddress());
|
|
|
+ vo.setBindStatus(cd.getBindStatus());
|
|
|
+
|
|
|
+ return vo;
|
|
|
+ })
|
|
|
+ .sorted(Comparator.comparing(
|
|
|
+ CustomerDeptTreeVo::getOrderNum,
|
|
|
+ Comparator.nullsLast(Comparator.naturalOrder()) // ✅ 安全处理 null
|
|
|
+ ))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
private LambdaQueryWrapper<CustomerDept> buildQueryWrapper(CustomerDeptBo bo) {
|
|
|
Map<String, Object> params = bo.getParams();
|
|
|
LambdaQueryWrapper<CustomerDept> lqw = Wrappers.lambdaQuery();
|
|
|
- lqw.orderByAsc(CustomerDept::getId);
|
|
|
- lqw.eq(StringUtils.isNotBlank(bo.getDeptNo()), CustomerDept::getDeptNo, bo.getDeptNo());
|
|
|
- lqw.like(StringUtils.isNotBlank(bo.getDeptName()), CustomerDept::getDeptName, bo.getDeptName());
|
|
|
- lqw.eq(bo.getParentId() != null, CustomerDept::getParentId, bo.getParentId());
|
|
|
+ lqw.orderByAsc(CustomerDept::getDeptId);
|
|
|
lqw.eq(bo.getCustomerId() != null, CustomerDept::getCustomerId, bo.getCustomerId());
|
|
|
- lqw.eq(StringUtils.isNotBlank(bo.getAncestors()), CustomerDept::getAncestors, bo.getAncestors());
|
|
|
- lqw.eq(bo.getDepartmentLevel() != null, CustomerDept::getDepartmentLevel, bo.getDepartmentLevel());
|
|
|
lqw.eq(bo.getYearlyBudget() != null, CustomerDept::getYearlyBudget, bo.getYearlyBudget());
|
|
|
lqw.eq(bo.getUsedBudget() != null, CustomerDept::getUsedBudget, bo.getUsedBudget());
|
|
|
lqw.eq(bo.getMonthLimit() != null, CustomerDept::getMonthLimit, bo.getMonthLimit());
|
|
|
@@ -81,74 +138,84 @@ public class CustomerDeptServiceImpl extends ServiceImpl<CustomerDeptMapper, Cus
|
|
|
lqw.eq(StringUtils.isNotBlank(bo.getDeptManage()), CustomerDept::getDeptManage, bo.getDeptManage());
|
|
|
lqw.eq(StringUtils.isNotBlank(bo.getIsLimit()), CustomerDept::getIsLimit, bo.getIsLimit());
|
|
|
lqw.eq(StringUtils.isNotBlank(bo.getSelectYear()), CustomerDept::getSelectYear, bo.getSelectYear());
|
|
|
- lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CustomerDept::getStatus, bo.getStatus());
|
|
|
lqw.eq(StringUtils.isNotBlank(bo.getPlatformCode()), CustomerDept::getPlatformCode, bo.getPlatformCode());
|
|
|
return lqw;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 新增客户部门信息
|
|
|
- *
|
|
|
- * @param bo 客户部门信息
|
|
|
- * @return 是否新增成功
|
|
|
- */
|
|
|
+
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public Boolean insertByBo(CustomerDeptBo bo) {
|
|
|
- bo.setDeptNo(SequenceUtils.nextPaddedIdStr(DEPT_NO_KEY, Duration.ofDays(3650), 5));
|
|
|
+ try {
|
|
|
+ // 1. 参数校验
|
|
|
+ if (bo == null || bo.getCustomerId() == null) {
|
|
|
+ throw new IllegalArgumentException("客户ID不能为空");
|
|
|
+ }
|
|
|
|
|
|
- CustomerDept info = baseMapper.selectById(bo.getParentId());
|
|
|
- // 如果父节点不为正常状态,则不允许新增子节点
|
|
|
- if (!SystemConstants.NORMAL.equals(info.getStatus())) {
|
|
|
- throw new ServiceException("部门停用,不允许新增");
|
|
|
- }
|
|
|
+ // 2. 构建并插入 sys_dept 部门结构
|
|
|
+ RemoteDeptVo remoteDeptVo = new RemoteDeptVo();
|
|
|
+ remoteDeptVo.setParentId(bo.getParentId());
|
|
|
+ remoteDeptVo.setDeptName(bo.getDeptName());
|
|
|
+ remoteDeptVo.setPlatformCode(PlatformContext.getPlatform());
|
|
|
+
|
|
|
+
|
|
|
+ RemoteDeptVo deptVo = remoteDeptService.insertDept(remoteDeptVo);
|
|
|
+
|
|
|
+ // 3. 校验 deptId 是否回填成功
|
|
|
+ if (deptVo == null || deptVo.getDeptId() == null) {
|
|
|
+ log.error("创建部门失败:未返回有效部门ID");
|
|
|
+ }
|
|
|
|
|
|
- CustomerDept dept = MapstructUtils.convert(bo, CustomerDept.class);
|
|
|
- dept.setAncestors(info.getAncestors() + StringUtils.SEPARATOR + dept.getParentId());
|
|
|
- validEntityBeforeSave(dept);
|
|
|
- boolean flag = baseMapper.insert(dept) > 0;
|
|
|
- if (flag) {
|
|
|
- bo.setId(dept.getId());
|
|
|
+ // 4. 插入 customer_dept 业务数据
|
|
|
+ CustomerDept dept = new CustomerDept();
|
|
|
+ dept.setDeptId(deptVo.getDeptId());
|
|
|
+ dept.setCustomerId(bo.getCustomerId());
|
|
|
+ dept.setYearlyBudget(bo.getYearlyBudget());
|
|
|
+ dept.setMonthLimit(bo.getMonthLimit());
|
|
|
+ dept.setUsedBudget(bo.getUsedBudget());
|
|
|
+ dept.setBindStatus(bo.getBindStatus());
|
|
|
+ dept.setBindAddress(bo.getBindAddress());
|
|
|
+
|
|
|
+ boolean success = baseMapper.insert(dept) > 0;
|
|
|
+ if (!success) {
|
|
|
+ log.error("创建部门失败:插入业务数据失败");
|
|
|
+ }
|
|
|
+ return success;
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ log.error("创建部门失败:{}", e.getMessage());
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- return flag;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 修改客户部门信息
|
|
|
- *
|
|
|
- * @param bo 客户部门信息
|
|
|
- * @return 是否修改成功
|
|
|
- */
|
|
|
@Override
|
|
|
public Boolean updateByBo(CustomerDeptBo bo) {
|
|
|
- CustomerDept update = MapstructUtils.convert(bo, CustomerDept.class);
|
|
|
- validEntityBeforeSave(update);
|
|
|
- return baseMapper.updateById(update) > 0;
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 保存前的数据校验
|
|
|
- */
|
|
|
- private boolean validEntityBeforeSave(CustomerDept dept) {
|
|
|
- boolean exist = baseMapper.exists(new LambdaQueryWrapper<CustomerDept>()
|
|
|
- .eq(CustomerDept::getDeptName, dept.getDeptName())
|
|
|
- .eq(CustomerDept::getParentId, dept.getParentId())
|
|
|
- .eq(CustomerDept::getPlatformCode, dept.getPlatformCode())
|
|
|
- .ne(ObjectUtil.isNotNull(dept.getId()), CustomerDept::getId, dept.getId()));
|
|
|
- return !exist;
|
|
|
+ if (bo == null || bo.getDeptId() == null || bo.getDeptId() <= 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ RemoteDeptVo remoteDeptVo = remoteDeptService.selectDeptById(bo.getDeptId());
|
|
|
+ if (remoteDeptVo == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ MapstructUtils.convert(bo, CustomerInfo.class);
|
|
|
+
|
|
|
+
|
|
|
+ // 1. 更新主表
|
|
|
+ CustomerInfo entity = MapstructUtils.convert(bo, CustomerInfo.class);
|
|
|
+// boolean mainUpdated = baseMapper.updateById(entity) > 0;
|
|
|
+// if (!mainUpdated) {
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+//
|
|
|
+// Long customerId = bo.getId();
|
|
|
+
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 校验并批量删除客户部门信息信息
|
|
|
- *
|
|
|
- * @param ids 待删除的主键集合
|
|
|
- * @param isValid 是否进行有效性校验
|
|
|
- * @return 是否删除成功
|
|
|
- */
|
|
|
@Override
|
|
|
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
|
|
- if (isValid) {
|
|
|
- //TODO 做一些业务上的校验,判断是否需要校验
|
|
|
- }
|
|
|
- return baseMapper.deleteByIds(ids) > 0;
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|