123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package com.qxgmat.service;
- import com.github.pagehelper.Page;
- import com.nuliji.tools.AbstractService;
- 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.dao.UserNoteCourseMapper;
- import com.qxgmat.data.dao.entity.UserNoteCourse;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.*;
- @Service
- public class UserNoteCourseService extends AbstractService {
- private static final Logger logger = LoggerFactory.getLogger(UserNoteCourseService.class);
- @Resource
- private UserNoteCourseMapper userNoteCourseMapper;
- public Page<UserNoteCourse> listByCourse(int page, int size, String keyword, Integer userId, Integer courseId, String order, DirectionStatus direction){
- Example example = new Example(UserNoteCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andEqualTo("courseId", courseId)
- );
- if (keyword != null)
- example.and(
- example.createCriteria()
- .orLike("content", "%"+keyword+"%")
- );
- 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(userNoteCourseMapper, example, page, size);
- }
- public List<UserNoteCourse> listByCourse(Number courseId){
- Example example = new Example(UserNoteCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("courseId", courseId)
- );
- example.orderBy("id").asc();
- return select(userNoteCourseMapper, example);
- }
- /**
- * 更新用户笔记:没有则添加
- * @param note
- * @return
- */
- @Transactional
- public UserNoteCourse update(UserNoteCourse note){
- Example example = new Example(UserNoteCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", note.getUserId())
- .andEqualTo("courseId", note.getCourseId())
- .andEqualTo("courseNoId", note.getCourseNoId())
- );
- UserNoteCourse in = one(userNoteCourseMapper, example);
- Date now = new Date();
- if(in == null){
- // 按实际更新更改更新时间
- return add(note);
- }else{
- note.setId(in.getId());
- return edit(note);
- }
- }
- public List<UserNoteCourse> getByCourse(Integer userId, Integer courseId){
- Example example = new Example(UserNoteCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andEqualTo("courseId", courseId)
- );
- return select(userNoteCourseMapper, example);
- }
- /**
- * 获取课程记录分组列表
- * @param courseIds
- * @return
- */
- public Map<Object, Collection<UserNoteCourse>> groupByCourse(Integer userId, Collection courseIds){
- Map<Object, Collection<UserNoteCourse>> relationMap = new HashMap<>();
- if(courseIds == null || courseIds.size() == 0) return relationMap;
- Example example = new Example(UserNoteCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andIn("courseId", courseIds)
- );
- List<UserNoteCourse> nos = select(userNoteCourseMapper, example);
- Collection<UserNoteCourse> list;
- for(UserNoteCourse no : nos){
- if (!relationMap.containsKey(no.getCourseId())){
- list = new ArrayList<>();
- relationMap.put(no.getCourseId(), list);
- }else{
- list = relationMap.get(no.getCourseId());
- }
- list.add(no);
- }
- return relationMap;
- }
- /**
- * 删除笔记
- * @param userId
- * @param courseNoId
- * @return
- */
- @Transactional
- public boolean deleteNote(Integer userId, Integer courseNoId){
- Example example = new Example(UserNoteCourse.class);
- example.and(
- example.createCriteria()
- .andEqualTo("userId", userId)
- .andEqualTo("courseNoId", courseNoId)
- );
- UserNoteCourse in = one(userNoteCourseMapper, example);
- if (in == null){
- return true;
- }
- return delete(in.getId());
- }
- public UserNoteCourse add(UserNoteCourse message){
- int result = insert(userNoteCourseMapper, message);
- message = one(userNoteCourseMapper, message.getId());
- if(message == null){
- throw new SystemException("笔记添加失败");
- }
- return message;
- }
- public UserNoteCourse edit(UserNoteCourse message){
- UserNoteCourse in = one(userNoteCourseMapper, message.getId());
- if(in == null){
- throw new ParameterException("笔记不存在");
- }
- int result = update(userNoteCourseMapper, message);
- return message;
- }
- public boolean delete(Number id){
- UserNoteCourse in = one(userNoteCourseMapper, id);
- if(in == null){
- throw new ParameterException("笔记不存在");
- }
- int result = delete(userNoteCourseMapper, id);
- return result > 0;
- }
- public UserNoteCourse get(Number id){
- UserNoteCourse in = one(userNoteCourseMapper, id);
- if(in == null){
- throw new ParameterException("笔记不存在");
- }
- return in;
- }
- public Page<UserNoteCourse> select(int page, int pageSize){
- return select(userNoteCourseMapper, page, pageSize);
- }
- public Page<UserNoteCourse> select(Integer[] ids){
- return page(()->select(userNoteCourseMapper, ids), 1, ids.length);
- }
- public List<UserNoteCourse> select(Collection ids){
- return select(userNoteCourseMapper, ids);
- }
- }
|