|
@@ -4,23 +4,19 @@ import cn.rankin.common.utils.api.model.APICodeManager;
|
|
import cn.rankin.common.utils.api.model.APIResult;
|
|
import cn.rankin.common.utils.api.model.APIResult;
|
|
import cn.rankin.common.utils.api.page.Page;
|
|
import cn.rankin.common.utils.api.page.Page;
|
|
import cn.rankin.common.utils.dto.product.CourseDTO;
|
|
import cn.rankin.common.utils.dto.product.CourseDTO;
|
|
-import cn.rankin.common.utils.dto.product.SubRelationDTO;
|
|
|
|
|
|
+import cn.rankin.common.utils.enums.BaseOrderEnum;
|
|
import cn.rankin.common.utils.enums.BaseStatusEnum;
|
|
import cn.rankin.common.utils.enums.BaseStatusEnum;
|
|
-import cn.rankin.common.utils.enums.CourseSubTypeEnum;
|
|
|
|
-import cn.rankin.common.utils.util.ListToMapUtil;
|
|
|
|
|
|
+import cn.rankin.common.utils.util.JpaSortUtil;
|
|
import cn.rankin.productservice.entity.*;
|
|
import cn.rankin.productservice.entity.*;
|
|
import cn.rankin.productservice.repository.*;
|
|
import cn.rankin.productservice.repository.*;
|
|
import cn.rankin.productservice.utils.DTOConverter;
|
|
import cn.rankin.productservice.utils.DTOConverter;
|
|
-import cn.rankin.productservice.vo.CourseSubItemVo;
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
|
+import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-import java.util.Map;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@Service
|
|
@Service
|
|
@@ -29,64 +25,12 @@ public class CourseService {
|
|
@Autowired
|
|
@Autowired
|
|
private CourseRepository courseRepository;
|
|
private CourseRepository courseRepository;
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private LessonRepository lessonRepository;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- private CourseSubRelationRepository subRelationRepository;
|
|
|
|
-
|
|
|
|
public boolean exists(String code) {
|
|
public boolean exists(String code) {
|
|
Long count = courseRepository.countByCode(code);
|
|
Long count = courseRepository.countByCode(code);
|
|
return count > 0L;
|
|
return count > 0L;
|
|
}
|
|
}
|
|
|
|
|
|
- public boolean checkSubItemIds(List<SubRelationDTO> subRelationDTOList) {
|
|
|
|
- // TODO: 目前只包含课
|
|
|
|
- if (subRelationDTOList == null || subRelationDTOList.size() == 0) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- List<String> lessonIdList = new ArrayList<>();
|
|
|
|
- subRelationDTOList.forEach( r -> {
|
|
|
|
- String subId = r.getId();
|
|
|
|
- CourseSubTypeEnum type = r.getType();
|
|
|
|
- if (type.equals(CourseSubTypeEnum.LESSON)) {
|
|
|
|
- lessonIdList.add(subId);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- Long count = lessonRepository.countByIdIn(lessonIdList);
|
|
|
|
- if (count < lessonIdList.size()) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Transactional
|
|
|
|
- public List<CourseSubRelation> updateSubRelation(String courseId, List<SubRelationDTO> subRelationDTOList) {
|
|
|
|
- // TODO: 暂时不考虑单元
|
|
|
|
- if (subRelationDTOList == null) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Long count = subRelationRepository.deleteByCourseId(courseId);
|
|
|
|
- log.info("delete course sub item, code={}, num={}", courseId, String.valueOf(count));
|
|
|
|
-
|
|
|
|
- List<CourseSubRelation> subRelationList = new ArrayList<>();
|
|
|
|
- for (int i = 0; i < subRelationDTOList.size(); i++) {
|
|
|
|
- SubRelationDTO relationDTO = subRelationDTOList.get(i);
|
|
|
|
- CourseSubRelation relation = new CourseSubRelation();
|
|
|
|
- relation.setCourseId(courseId);
|
|
|
|
- relation.setSubId(relationDTO.getId());
|
|
|
|
- relation.setType(relationDTO.getType());
|
|
|
|
- relation.setSort(i);
|
|
|
|
- subRelationList.add(relation);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- List<CourseSubRelation> result = subRelationRepository.save(subRelationList);
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public APIResult<Page<Course>> search(Course course, Integer pageNo, Integer pageSize) {
|
|
|
|
|
|
+ public APIResult<Page<Course>> search(Course course, Integer pageNo, Integer pageSize, LinkedHashMap<String, BaseOrderEnum> sort) {
|
|
Long count = courseRepository.count(course);
|
|
Long count = courseRepository.count(course);
|
|
Page<Course> page = new Page(count, pageNo, pageSize);
|
|
Page<Course> page = new Page(count, pageNo, pageSize);
|
|
|
|
|
|
@@ -94,7 +38,7 @@ public class CourseService {
|
|
return APIResult.ok(page);
|
|
return APIResult.ok(page);
|
|
}
|
|
}
|
|
|
|
|
|
- List<Course> wareList = courseRepository.find(course, page.getStart(), pageSize);
|
|
|
|
|
|
+ List<Course> wareList = courseRepository.find(course, page.getStart(), pageSize, JpaSortUtil.sort(sort));
|
|
page.setList(wareList);
|
|
page.setList(wareList);
|
|
|
|
|
|
return APIResult.ok(page);
|
|
return APIResult.ok(page);
|
|
@@ -107,41 +51,24 @@ public class CourseService {
|
|
return APIResult.error(APICodeManager.ALREADY_EXISTS);
|
|
return APIResult.error(APICodeManager.ALREADY_EXISTS);
|
|
}
|
|
}
|
|
|
|
|
|
- // sub item check
|
|
|
|
- List<SubRelationDTO> subRelationDTOList = courseDTO.getSubList();
|
|
|
|
- if (!checkSubItemIds(subRelationDTOList)) {
|
|
|
|
- return APIResult.error(APICodeManager.error("包含错误课编码"));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
Course course = DTOConverter.convert(courseDTO);
|
|
Course course = DTOConverter.convert(courseDTO);
|
|
Course result = courseRepository.save(course);
|
|
Course result = courseRepository.save(course);
|
|
|
|
|
|
- String courseId = result.getId();
|
|
|
|
- updateSubRelation(courseId, subRelationDTOList);
|
|
|
|
-
|
|
|
|
return APIResult.ok(result);
|
|
return APIResult.ok(result);
|
|
}
|
|
}
|
|
|
|
|
|
@Transactional
|
|
@Transactional
|
|
public APIResult<Course> update(CourseDTO courseDTO) {
|
|
public APIResult<Course> update(CourseDTO courseDTO) {
|
|
- List<SubRelationDTO> subRelationDTOList = courseDTO.getSubList();
|
|
|
|
- if (!checkSubItemIds(subRelationDTOList)) {
|
|
|
|
- return APIResult.error(APICodeManager.error("包含错误课编码"));
|
|
|
|
- }
|
|
|
|
|
|
|
|
Course course = DTOConverter.convert(courseDTO);
|
|
Course course = DTOConverter.convert(courseDTO);
|
|
- Course result = courseRepository.save(course);
|
|
|
|
-
|
|
|
|
- String courseId = result.getId();
|
|
|
|
- updateSubRelation(courseId, subRelationDTOList);
|
|
|
|
|
|
+ Course result = courseRepository.update(course);
|
|
|
|
|
|
return APIResult.ok(result);
|
|
return APIResult.ok(result);
|
|
}
|
|
}
|
|
|
|
|
|
@Transactional
|
|
@Transactional
|
|
public APIResult<Boolean> delete(String id) {
|
|
public APIResult<Boolean> delete(String id) {
|
|
- Integer count = courseRepository.updateStatusById(id, BaseStatusEnum.DEL);
|
|
|
|
|
|
+ Integer count = courseRepository.deleteById(id, BaseStatusEnum.DEL);
|
|
if (count > 0) {
|
|
if (count > 0) {
|
|
return APIResult.ok(true);
|
|
return APIResult.ok(true);
|
|
}
|
|
}
|
|
@@ -149,46 +76,17 @@ public class CourseService {
|
|
}
|
|
}
|
|
|
|
|
|
public APIResult<Course> findById(String id) {
|
|
public APIResult<Course> findById(String id) {
|
|
- //TODO: 目前只加载课, 不加载单元
|
|
|
|
Course course = courseRepository.findById(id);
|
|
Course course = courseRepository.findById(id);
|
|
if (course == null) {
|
|
if (course == null) {
|
|
return APIResult.error(APICodeManager.NOT_EXISTS);
|
|
return APIResult.error(APICodeManager.NOT_EXISTS);
|
|
}
|
|
}
|
|
|
|
|
|
- // 你瞅瞅, 加上单元难不难?
|
|
|
|
- List<CourseSubRelation> subRelationList = subRelationRepository.findByCourseId(id);
|
|
|
|
- if (subRelationList == null || subRelationList.size() == 0) {
|
|
|
|
- return APIResult.ok(course);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- List<CourseSubItemVo> subItemVoList = new ArrayList<>();
|
|
|
|
-
|
|
|
|
- List<String> lessonIdList = new ArrayList<>();
|
|
|
|
- subRelationList.forEach( rel -> {
|
|
|
|
- CourseSubTypeEnum type = rel.getType();
|
|
|
|
- String subId = rel.getSubId();
|
|
|
|
- if (type.equals(CourseSubTypeEnum.LESSON)) {
|
|
|
|
- lessonIdList.add(subId);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- List<Lesson> lessonList = lessonRepository.findByIds(lessonIdList);
|
|
|
|
- log.info(JSON.toJSONString(lessonList));
|
|
|
|
- Map<String, Lesson> lessonMap = new ListToMapUtil<String, Lesson>(lessonList).parse("id");
|
|
|
|
-
|
|
|
|
- for(CourseSubRelation rel: subRelationList) {
|
|
|
|
- String subCode = rel.getSubId();
|
|
|
|
- CourseSubTypeEnum type = rel.getType();
|
|
|
|
- if (type.equals(CourseSubTypeEnum.LESSON)) {
|
|
|
|
- Lesson lesson = lessonMap.get(subCode);
|
|
|
|
- CourseSubItemVo vo = CourseSubItemVo.parseToVo(lesson);
|
|
|
|
- subItemVoList.add(vo);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- course.setSubItemList(subItemVoList);
|
|
|
|
-
|
|
|
|
return APIResult.ok(course);
|
|
return APIResult.ok(course);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public APIResult<List<Course>> findAllByIds(List<String> courseIdList) {
|
|
|
|
+// List<Course> courseList = courseRepository.findByIdIn(courseIdList, BaseStatusEnum.NORMAL);
|
|
|
|
+ List<Course> courseList = courseRepository.findByIds(courseIdList);
|
|
|
|
+ return APIResult.ok(courseIdList);
|
|
|
|
+ }
|
|
}
|
|
}
|