123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- 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.status.DirectionStatus;
- import com.qxgmat.data.constants.enums.user.DataType;
- import com.qxgmat.data.dao.CourseDataMapper;
- import com.qxgmat.data.dao.CourseMapper;
- import com.qxgmat.data.dao.entity.Course;
- import com.qxgmat.data.dao.entity.CourseData;
- import com.qxgmat.data.relation.CourseDataRelationMapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.Collection;
- import java.util.List;
- @Service
- public class CourseDataService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(CourseDataService.class);
- @Resource
- private CourseDataMapper courseDataMapper;
- @Resource
- private CourseDataRelationMapper courseDataRelationMapper;
- public Page<CourseData> listAdmin(int page, int size,String keyword, Integer structId, DataType dataType, String order, DirectionStatus direction){
- Example example = new Example(CourseData.class);
- if(keyword != null){
- example.and(
- example.createCriteria()
- .andLike("title", "%"+keyword+"%")
- );
- }
- if(structId != null){
- example.and(
- example.createCriteria()
- .orEqualTo("structId", structId)
- .orEqualTo("parentStructId", structId)
- );
- }
- if(dataType != null){
- example.and(
- example.createCriteria()
- .andEqualTo("dataType", dataType.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(courseDataMapper, example, page, size);
- }
- public Page<CourseData> list(int page, int size, Integer structId, DataType dataType, Boolean isNovice, Boolean isOriginal, String order, DirectionStatus direction){
- Example example = new Example(CourseData.class);
- if(structId != null){
- example.and(
- example.createCriteria()
- .orEqualTo("structId", structId)
- .orEqualTo("parentStructId", structId)
- );
- }
- if (isNovice != null){
- example.and(
- example.createCriteria()
- .andEqualTo("isNovice", isNovice ? 1 : 0)
- );
- }
- if (isOriginal != null){
- example.and(
- example.createCriteria()
- .andEqualTo("isOriginal", isOriginal ? 1 : 0)
- );
- }
- if(dataType != null){
- example.and(
- example.createCriteria()
- .andEqualTo("dataType", dataType.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(courseDataMapper, example, page, size);
- }
- /**
- * 获取用户购买记录
- * @param page
- * @param size
- * @param userId
- * @return
- */
- public Page<CourseData> listByUser(int page, int size, Integer userId, Integer structId, DataType dataType, String order, DirectionStatus direction){
- Page<CourseData> p = page(()->{
- courseDataRelationMapper.listByUser(userId, structId, dataType != null ? dataType.key : null, order != null ? order : "id", direction != null ? direction.key : "desc");
- }, page, size);
- Collection ids = Transform.getIds(p, CourseData.class, "id");
- // 获取详细数据
- List<CourseData> list = select(ids);
- Transform.replace(p, list, CourseData.class, "id");
- return p;
- }
- /**
- * 累加购买记录,访问记录
- * @param sale
- */
- public void accumulation(Integer dataId, int sale, int view){
- courseDataRelationMapper.accumulation(dataId, sale, view);
- }
- /**
- * 获取长难句资料:添加时根据struct=sentence判断设置
- * @return
- */
- public CourseData getSentence(){
- Example example = new Example(CourseData.class);
- example.and(
- example.createCriteria().andEqualTo("isSentence", 1)
- );
- return one(courseDataMapper, example);
- }
- public CourseData add(CourseData courseData){
- int result = insert(courseDataMapper, courseData);
- courseData = one(courseDataMapper, courseData.getId());
- if(courseData == null){
- throw new SystemException("资料添加失败");
- }
- return courseData;
- }
- public CourseData edit(CourseData courseData){
- CourseData in = one(courseDataMapper, courseData.getId());
- if(in == null){
- throw new ParameterException("资料不存在");
- }
- int result = update(courseDataMapper, courseData);
- return courseData;
- }
- public boolean delete(Number id){
- CourseData in = one(courseDataMapper, id);
- if(in == null){
- throw new ParameterException("资料不存在");
- }
- int result = delete(courseDataMapper, id);
- return result > 0;
- }
- public CourseData get(Number id){
- CourseData in = one(courseDataMapper, id);
- if(in == null){
- throw new ParameterException("资料不存在");
- }
- return in;
- }
- public Page<CourseData> select(int page, int pageSize){
- return select(courseDataMapper, page, pageSize);
- }
- public Page<CourseData> select(Integer[] ids){
- return page(()->select(courseDataMapper, ids), 1, ids.length);
- }
- public List<CourseData> select(Collection ids){
- return select(courseDataMapper, ids);
- }
- }
|