|
@@ -1,98 +1,68 @@
|
|
package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
-import java.util.stream.Stream;
|
|
|
|
-
|
|
|
|
-import javax.persistence.criteria.Predicate;
|
|
|
|
-import javax.persistence.criteria.Root;
|
|
|
|
-import javax.persistence.criteria.Subquery;
|
|
|
|
-
|
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
-import org.springframework.data.domain.Page;
|
|
|
|
-import org.springframework.data.domain.Pageable;
|
|
|
|
-import org.springframework.data.jpa.domain.Specification;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
-import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
import cn.com.qmth.examcloud.core.basic.dao.SpecialtyRepo;
|
|
import cn.com.qmth.examcloud.core.basic.dao.SpecialtyRepo;
|
|
-import cn.com.qmth.examcloud.core.basic.dao.entity.CourseSpeciatlyRelationEntity;
|
|
|
|
import cn.com.qmth.examcloud.core.basic.dao.entity.SpecialtyEntity;
|
|
import cn.com.qmth.examcloud.core.basic.dao.entity.SpecialtyEntity;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.SpecialtyService;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.bean.SpecialtyInfo;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
-public class SpecialtyServiceImpl {
|
|
|
|
|
|
+public class SpecialtyServiceImpl implements SpecialtyService {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
SpecialtyRepo specialtyRepo;
|
|
SpecialtyRepo specialtyRepo;
|
|
|
|
|
|
- /**
|
|
|
|
- * 带分页查询专业
|
|
|
|
- *
|
|
|
|
- * @param specialty
|
|
|
|
- * @param pageable
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public Page<SpecialtyEntity> findAll(SpecialtyEntity specialty, Long courseId, Pageable pageable) {
|
|
|
|
- Specification<SpecialtyEntity> specification = getSpecification(specialty, courseId);
|
|
|
|
- Page<SpecialtyEntity> specialties = specialtyRepo.findAll(specification, pageable);
|
|
|
|
- return specialties;
|
|
|
|
- }
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public SpecialtyEntity saveSpecialty(SpecialtyInfo info) {
|
|
|
|
+ Long id = info.getId();
|
|
|
|
+ String code = info.getCode();
|
|
|
|
+ Boolean enable = info.getEnable();
|
|
|
|
+ Long rootOrgId = info.getRootOrgId();
|
|
|
|
+ String name = info.getName();
|
|
|
|
|
|
- public Specification<SpecialtyEntity> getSpecification(SpecialtyEntity specialty, Long courseId) {
|
|
|
|
- Specification<SpecialtyEntity> specification = (root, query, cb) -> {
|
|
|
|
- List<Predicate> predicates = new ArrayList<>();
|
|
|
|
- if (!StringUtils.isEmpty(specialty.getRootOrgId())) {
|
|
|
|
- predicates.add(cb.equal(root.get("orgId"), specialty.getRootOrgId()));
|
|
|
|
- }
|
|
|
|
- if (!StringUtils.isEmpty(specialty.getName())) {
|
|
|
|
- predicates.add(cb.like(root.get("name"), "%" + specialty.getName() + "%"));
|
|
|
|
- }
|
|
|
|
- if (!StringUtils.isEmpty(specialty.getCode())) {
|
|
|
|
- predicates.add(cb.like(root.get("code"), "%" + specialty.getCode() + "%"));
|
|
|
|
|
|
+ if (null == rootOrgId) {
|
|
|
|
+ throw new StatusException("B-620001", "rootOrgId is null");
|
|
|
|
+ }
|
|
|
|
+ SpecialtyEntity entity = null;
|
|
|
|
+
|
|
|
|
+ if (null != id) {
|
|
|
|
+ entity = specialtyRepo.findOne(id);
|
|
|
|
+ if (null == entity) {
|
|
|
|
+ throw new StatusException("B-620001", "id is wrong");
|
|
}
|
|
}
|
|
- if (!StringUtils.isEmpty(specialty.getEnable())) {
|
|
|
|
- predicates.add(cb.equal(root.get("enable"), specialty.getEnable()));
|
|
|
|
|
|
+ if (!entity.getRootOrgId().equals(rootOrgId)) {
|
|
|
|
+ throw new StatusException("B-620001", "rootOrgId or id is worng");
|
|
}
|
|
}
|
|
- if (courseId != null) {
|
|
|
|
- Subquery<CourseSpeciatlyRelationEntity> relationshipSubquery = query
|
|
|
|
- .subquery(CourseSpeciatlyRelationEntity.class);
|
|
|
|
- Root<CourseSpeciatlyRelationEntity> residencyRelationshipSubqueryRoot = relationshipSubquery
|
|
|
|
- .from(CourseSpeciatlyRelationEntity.class);
|
|
|
|
- relationshipSubquery.select(residencyRelationshipSubqueryRoot);
|
|
|
|
- Predicate predicate = cb.equal(residencyRelationshipSubqueryRoot.get("courseId"),
|
|
|
|
- courseId);
|
|
|
|
- Predicate predicate2 = cb.equal(
|
|
|
|
- residencyRelationshipSubqueryRoot.get("specialtyId"), root.get("id"));
|
|
|
|
- relationshipSubquery.where(cb.and(predicate, predicate2));
|
|
|
|
- predicates.add(cb.exists(relationshipSubquery));
|
|
|
|
|
|
+ } else if (StringUtils.isNotBlank(code)) {
|
|
|
|
+ entity = specialtyRepo.findByRootOrgIdAndCode(rootOrgId, code);
|
|
|
|
+ if (null == entity) {
|
|
|
|
+ if (StringUtils.isBlank(name)) {
|
|
|
|
+ throw new StatusException("B-620001", "name is blank");
|
|
|
|
+ }
|
|
|
|
+ entity = new SpecialtyEntity();
|
|
|
|
+ entity.setCode(code);
|
|
|
|
+ entity.setName(name);
|
|
|
|
+ entity.setRootOrgId(rootOrgId);
|
|
|
|
+ entity.setEnable(true);
|
|
}
|
|
}
|
|
- return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
|
- };
|
|
|
|
- return specification;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 查询专业不带分页
|
|
|
|
- *
|
|
|
|
- * @param specialty
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public List<SpecialtyEntity> findAll(SpecialtyEntity specialty) {
|
|
|
|
- Specification<SpecialtyEntity> specification = getSpecification(specialty, null);
|
|
|
|
- return specialtyRepo.findAll(specification);
|
|
|
|
- }
|
|
|
|
|
|
+ } else {
|
|
|
|
+ throw new StatusException("B-620001", "id and code can not be all null");
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- * 根据Id删除专业
|
|
|
|
- *
|
|
|
|
- * @param ids
|
|
|
|
- */
|
|
|
|
- public void deleteSpecialty(String ids) {
|
|
|
|
- List<Long> specialtyIds = Stream.of(ids.split(",")).map(s -> Long.parseLong(s.trim()))
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
- for (Long specialtyId : specialtyIds) {
|
|
|
|
- specialtyRepo.delete(specialtyId);
|
|
|
|
|
|
+ if (null != enable) {
|
|
|
|
+ entity.setEnable(enable);
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.isNotBlank(name)) {
|
|
|
|
+ entity.setName(name);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ SpecialtyEntity saved = specialtyRepo.save(entity);
|
|
|
|
+
|
|
|
|
+ return saved;
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|