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.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; /** * 更新用户笔记:没有则添加 * @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); } } /** * 获取课程记录分组列表 * @param courseIds * @return */ public Map<Object, Collection<UserNoteCourse>> groupByCourse(Collection courseIds){ Map<Object, Collection<UserNoteCourse>> relationMap = new HashMap<>(); Example example = new Example(UserNoteCourse.class); example.and( example.createCriteria() .andIn("courseIds", 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; } 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); } }