123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package com.qxgmat.service.inline;
- import com.github.pagehelper.Page;
- import com.nuliji.tools.AbstractService;
- import com.nuliji.tools.Transform;
- import com.nuliji.tools.exception.ParameterException;
- import com.nuliji.tools.exception.SystemException;
- import com.nuliji.tools.mybatis.Example;
- import com.qxgmat.data.constants.enums.module.CourseModule;
- import com.qxgmat.data.constants.enums.status.DirectionStatus;
- import com.qxgmat.data.dao.CourseMapper;
- import com.qxgmat.data.dao.entity.Course;
- import com.qxgmat.data.relation.CourseRelationMapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- public class CourseService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(CourseService.class);
- @Resource
- private CourseMapper courseMapper;
- @Resource
- private CourseRelationMapper courseRelationMapper;
- public Page<Course> listAdmin(int page, int size, String keyword, CourseModule module, Integer structId, Boolean excludeVs, Boolean excludeOnline, String order, DirectionStatus direction){
- Example example = new Example(Course.class);
- if (keyword != null) {
- example.and(
- example.createCriteria()
- .andLike("title", "%"+keyword+"%")
- );
- }
- if(module != null){
- example.and(
- example.createCriteria()
- .andEqualTo("courseModule", module.key)
- );
- }
- if(structId != null){
- example.and(
- example.createCriteria()
- .orEqualTo("structId", structId)
- .orEqualTo("parentStructId", structId)
- );
- }
- if (excludeVs != null) {
- example.and(
- example.createCriteria()
- .andNotEqualTo("courseModule", CourseModule.VS.key)
- );
- }
- if (excludeVs != null) {
- example.and(
- example.createCriteria()
- .andNotEqualTo("courseModule", CourseModule.ONLINE.key)
- );
- }
- if(order == null || order.isEmpty()) order = "id";
- switch(direction){
- case ASC:
- example.orderBy(order).asc();
- break;
- case DESC:
- default:
- example.orderBy(order).desc();
- }
- return select(courseMapper, example, page, size);
- }
- public Page<Course> list(int page, int size, CourseModule module, Integer structId, String order, DirectionStatus direction){
- Example example = new Example(Course.class);
- if(module != null){
- example.and(
- example.createCriteria()
- .andEqualTo("courseModule", module.key)
- );
- }
- if(structId != null){
- example.and(
- example.createCriteria()
- .orEqualTo("structId", structId)
- .orEqualTo("parentStructId", structId)
- );
- }
- if(order == null || order.isEmpty()) order = "id";
- if (direction != null){
- switch(direction){
- case ASC:
- example.orderBy(order).asc();
- break;
- case DESC:
- default:
- example.orderBy(order).desc();
- }
- } else {
- example.orderBy(order).desc();
- }
- return select(courseMapper, example, page, size);
- }
- public Map<Integer, List<Course>> groupByMap(Map<Integer, Integer[]> courseIdsMap){
- Map<Integer, List<Course>> relationMap = new HashMap<>();
- List<Integer> ids = new ArrayList<>();
- for(Integer[] courseIds : courseIdsMap.values()){
- ids.addAll(Arrays.stream(courseIds).collect(Collectors.toList()));
- }
- List<Course> courseList = select(ids);
- Map courseMap = Transform.getMap(courseList, Course.class, "id");
- for(Integer k: courseIdsMap.keySet()){
- List<Course> l = new ArrayList<>();
- for (Integer courseId : courseIdsMap.get(k)){
- l.add((Course)courseMap.get(courseId));
- }
- relationMap.put(k, l);
- }
- return relationMap;
- }
- public List<Course> all(CourseModule module){
- Example example = new Example(Course.class);
- example.and(
- example.createCriteria()
- .andEqualTo("courseModule", module.key)
- );
- return select(courseMapper, example);
- }
- public List<Course> listByExtend(String questionType, String questionSubject, String videoType){
- Example example = new Example(Course.class);
- example.and(
- example.createCriteria()
- .orEqualTo("extend", questionType)
- .orEqualTo("extend", questionSubject)
- );
- if (videoType != null){
- example.and(
- example.createCriteria()
- .andEqualTo("videoType", videoType)
- );
- }
- return select(courseMapper, example);
- }
- /**
- * 累加购买记录,访问记录
- * @param sale
- */
- public void accumulation(Integer courseId, int trail, int sale, int packageSale){
- courseRelationMapper.accumulation(courseId, trail, sale, packageSale);
- }
- public Course add(Course course){
- int result = insert(courseMapper, course);
- course = one(courseMapper, course.getId());
- if(course == null){
- throw new SystemException("课程添加失败");
- }
- return course;
- }
- public Course edit(Course course){
- Course in = one(courseMapper, course.getId());
- if(in == null){
- throw new ParameterException("课程不存在");
- }
- int result = update(courseMapper, course);
- return course;
- }
- public boolean delete(Number id){
- Course in = one(courseMapper, id);
- if(in == null){
- throw new ParameterException("课程不存在");
- }
- int result = delete(courseMapper, id);
- return result > 0;
- }
- public Course get(Number id){
- Course in = one(courseMapper, id);
- if(in == null){
- throw new ParameterException("课程不存在");
- }
- return in;
- }
- public Page<Course> select(int page, int pageSize){
- return select(courseMapper, page, pageSize);
- }
- public Page<Course> select(Integer[] ids){
- return page(()->select(courseMapper, ids), 1, ids.length);
- }
- public List<Course> select(Collection ids){
- return select(courseMapper, ids);
- }
- }
|