wangwei 7 年 前
コミット
56f560ed2b

+ 8 - 1
examcloud-core-basic-dao/src/main/java/cn/com/qmth/examcloud/core/basic/dao/OrgRepo.java

@@ -4,6 +4,8 @@ import java.util.List;
 
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
 import org.springframework.data.repository.query.QueryByExampleExecutor;
 
 import cn.com.qmth.examcloud.core.basic.dao.entity.Org;
@@ -21,8 +23,13 @@ public interface OrgRepo
 
 	List<Org> findByParentIdAndNameLikeAndEnable(Long parentId, String name, Boolean enable);
 
-	Org findByParentIdAndCodeAndEnable(Long parentId, String code, Boolean enable);
+	Org findByParentIdAndCode(Long parentId, String code);
+
+	Org findByRootIdAndCode(Long rootId, String code);
 
 	List<Org> findByParentIdAndEnable(Long parentId, Boolean enable);
 
+	@Query(nativeQuery = true, value = "select * from ecs_core_org t where t.parent_id is null and t.code=:code ")
+	Org findRootOrg(@Param("code") String code);
+
 }

+ 59 - 27
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/OrgService.java

@@ -19,6 +19,7 @@ import org.springframework.data.jpa.domain.Specification;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.commons.base.util.excel.ExcelError;
 import cn.com.qmth.examcloud.commons.base.util.excel.ExcelReader;
 import cn.com.qmth.examcloud.commons.base.util.excel.ExcelReaderHandle;
@@ -79,8 +80,7 @@ public class OrgService {
 	 * @param orgDto
 	 */
 	private void saveOrgAndExamSite(OrgDto orgDto) {
-		Org org = orgRepo.findByParentIdAndCodeAndEnable(orgDto.getParentId(), orgDto.getCode(),
-				true);
+		Org org = orgRepo.findByParentIdAndCode(orgDto.getParentId(), orgDto.getCode());
 		if (org == null) {
 
 			Org tempOrg = orgRepo.save(orgAssembler(orgDto));
@@ -233,33 +233,69 @@ public class OrgService {
 		return orgRepo.findAll(examExample);
 	}
 
+	/**
+	 * 强校验
+	 *
+	 * @author WANGWEI
+	 * @param org
+	 * @return
+	 */
 	public Org save(Org org) {
-		checkCode(org.getParentId(), org.getCode());
-		Org reOrg = orgRepo.save(org);
-		dataSendService.sendOrg(reOrg);
-		// 删除缓存
-		orgMemRepo.remove(reOrg.getId());
-		return reOrg;
-	}
 
-	private void checkCode(Long parentId, String code) {
-		Org old = orgRepo.findFirstByParentIdAndCode(parentId, code);
-		if (old != null) {
-			throw new RuntimeException("代码已存在");
+		String code = org.getCode();
+		if (StringUtils.isBlank(code)) {
+			throw new StatusException("B-150000", "code is blank");
 		}
-	}
 
-	public Org update(Long id, Org org) {
-		Org old = orgRepo.findOne(id);
-		if (!old.getCode().equals(org.getCode())) {
-			checkCode(org.getParentId(), org.getCode());
+		// 顶级机构
+		if (null == org.getParentId()) {
+
+			// 校验顶级机构编码已存在
+			Org rootOrg = orgRepo.findRootOrg(code);
+			if (null != rootOrg) {
+				if (!(null != org.getId() && org.getId().equals(rootOrg.getId()))) {
+					throw new StatusException("B-150001", "顶级机构编码已存在");
+				}
+			}
+
+			// 新增
+			if (null == org.getId()) {
+				Org saved = orgRepo.save(org);
+				org.setId(saved.getId());
+				org.setRootId(saved.getId());
+			}
 		}
-		org.setUpdateTime(new Date());
-		Org reOrg = orgRepo.save(org);
-		dataSendService.sendOrg(reOrg);
+		// 子机构
+		else {
+			Long rootId = org.getRootId();
+			if (null == rootId) {
+				throw new StatusException("B-150002", "rootId is null");
+			}
+			Org rootOrg = orgRepo.findOne(rootId);
+			if (null == rootOrg || null != rootOrg.getParentId()) {
+				throw new StatusException("B-150003", "rootId is wrong");
+			}
+			Long parentId = org.getParentId();
+
+			Org parentOrg = orgRepo.findOne(parentId);
+			if (!parentOrg.getRootId().equals(rootId)) {
+				throw new StatusException("B-150004", "parentId is wrong");
+			}
+
+			Org subOrg = orgRepo.findByRootIdAndCode(rootId, code);
+			if (null != subOrg) {
+				if (!(null != org.getId() && org.getId().equals(subOrg.getId()))) {
+					throw new StatusException("B-150005", "机构编码已存在");
+				}
+			}
+
+		}
+
+		Org saved = orgRepo.save(org);
+		dataSendService.sendOrg(saved);
 		// 删除缓存
-		orgMemRepo.remove(reOrg.getId());
-		return reOrg;
+		orgMemRepo.remove(saved.getId());
+		return saved;
 	}
 
 	public Org findOne(Long id) {
@@ -275,8 +311,4 @@ public class OrgService {
 		}
 	}
 
-	public Org findFirstByParentIdAndCode(Long parentId, String code) {
-		return orgRepo.findFirstByParentIdAndCode(parentId, code);
-	}
-
 }