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 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 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 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> groupByCourse(Integer userId, Collection courseIds){ Map> 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 nos = select(userNoteCourseMapper, example); Collection 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 select(int page, int pageSize){ return select(userNoteCourseMapper, page, pageSize); } public Page select(Integer[] ids){ return page(()->select(userNoteCourseMapper, ids), 1, ids.length); } public List select(Collection ids){ return select(userNoteCourseMapper, ids); } }