|
@@ -3,8 +3,10 @@ package com.fuint.common.service.impl;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fuint.common.dto.ext.CountryDto;
|
|
|
import com.fuint.framework.annoation.OperationServiceLog;
|
|
|
import com.fuint.framework.exception.BusinessCheckException;
|
|
|
+import com.fuint.framework.exception.BusinessRuntimeException;
|
|
|
import com.fuint.framework.pagination.PaginationRequest;
|
|
|
import com.fuint.framework.pagination.PaginationResponse;
|
|
|
import com.fuint.repository.model.MtCountry;
|
|
@@ -17,6 +19,7 @@ import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import com.github.pagehelper.Page;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -114,24 +117,57 @@ public class CountryServiceImpl extends ServiceImpl<MtCountryMapper, MtCountry>
|
|
|
/**
|
|
|
* 修改国家表数据
|
|
|
*
|
|
|
- * @param mtCountry
|
|
|
+ * @param countryDto
|
|
|
* @throws BusinessCheckException
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@OperationServiceLog(description = "更新国家表")
|
|
|
- public MtCountry updateCountry(MtCountry mtCountry) throws BusinessCheckException {
|
|
|
- mtCountry = queryCountryById(mtCountry.getId());
|
|
|
- if (mtCountry == null) {
|
|
|
- throw new BusinessCheckException("该国家表状态异常");
|
|
|
+ public boolean updateCountry(CountryDto countryDto, Long accountId) throws BusinessCheckException {
|
|
|
+ // 1. 获取原数据
|
|
|
+ MtCountry mtCountry = Optional.ofNullable(this.getById(countryDto.getId())).orElseThrow(() ->
|
|
|
+ new BusinessRuntimeException("目标记录不存在"));
|
|
|
+ // 2. 校验父子关系变更
|
|
|
+ validateParentChange(countryDto.getId(), countryDto.getParentId());
|
|
|
+
|
|
|
+ // 3. 构建更新对象
|
|
|
+ MtCountry country = new MtCountry();
|
|
|
+ BeanUtils.copyProperties(countryDto, country);
|
|
|
+ return this.updateById(country);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateParentChange(Long currentId, Long newParentId) throws BusinessCheckException {
|
|
|
+ // 禁止设置自己为父节点
|
|
|
+ if (currentId.equals(newParentId)) {
|
|
|
+ throw new BusinessCheckException("禁止设置自己为父节点");
|
|
|
}
|
|
|
- mtCountry.setUpdateTime(new Date());
|
|
|
- mtCountryMapper.updateById(mtCountry);
|
|
|
- return mtCountry;
|
|
|
+
|
|
|
+ // 检查新父节点是否存在
|
|
|
+ if (newParentId != 0L) { // 0表示根节点
|
|
|
+ int parentCount = this.baseMapper.checkParentExist(newParentId);
|
|
|
+ if (parentCount == 0) {
|
|
|
+ throw new BusinessCheckException("新父节点不存在或已被删除");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 检查是否形成循环依赖(递归校验)
|
|
|
+ checkCircularDependency(currentId, newParentId);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
+ private void checkCircularDependency(Long currentId, Long checkParentId) throws BusinessCheckException {
|
|
|
+ if (checkParentId == 0L) return; // 根节点无需检查
|
|
|
+
|
|
|
+ MtCountry parent = this.getById(checkParentId);
|
|
|
+ if (parent == null) return;
|
|
|
+
|
|
|
+ if (currentId.equals(parent.getParentId())) {
|
|
|
+ throw new BusinessCheckException("检测到循环依赖");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 递归检查父节点的父节点
|
|
|
+ checkCircularDependency(currentId, parent.getParentId());
|
|
|
+ }
|
|
|
+ /**
|
|
|
* 根据条件搜索国家表
|
|
|
*
|
|
|
* @param params 查询参数
|
|
@@ -150,4 +186,22 @@ public class CountryServiceImpl extends ServiceImpl<MtCountryMapper, MtCountry>
|
|
|
List<MtCountry> dataList = mtCountryMapper.selectList(lambdaQueryWrapper);
|
|
|
return dataList;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean saveCountry(CountryDto countryDto, Long accountInfoId) throws BusinessCheckException {
|
|
|
+ // 1. 校验父节点
|
|
|
+ if (countryDto.getParentId() != null && countryDto.getParentId() != 0L) {
|
|
|
+ int parentCount = this.baseMapper.checkParentExist(countryDto.getParentId());
|
|
|
+ if (parentCount == 0) {
|
|
|
+ throw new BusinessCheckException("父节点不存在或已被删除");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 2. 构建实体
|
|
|
+ MtCountry country = new MtCountry();
|
|
|
+ BeanUtils.copyProperties(countryDto, country);
|
|
|
+ country.setServeState(countryDto.getServeState() != null ? countryDto.getServeState() : 0);
|
|
|
+ country.setDeleteFlag(0);
|
|
|
+ //保存数据
|
|
|
+ return this.save(country);
|
|
|
+ }
|
|
|
}
|