package com.qxgmat.service; 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.dao.UserNoteQuestionMapper; import com.qxgmat.data.dao.entity.UserNoteQuestion; import com.qxgmat.data.relation.UserNoteQuestionRelationMapper; 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.ArrayList; import java.util.Collection; import java.util.Date; import java.util.List; @Service public class UserNoteQuestionService extends AbstractService { private static final Logger logger = LoggerFactory.getLogger(UserNoteQuestionService.class); @Resource private UserNoteQuestionMapper userNoteQuestionMapper; @Resource private UserNoteQuestionRelationMapper userNoteQuestionRelationMapper; /** * 获取用户笔记列表 * @param page * @param size * @param userId * @param questionTypes * @param structIds * @param startTime * @param endTime * @param order * @return */ public Page listExercise(int page, int size, Integer userId, String keyword, String[] questionTypes, String[] courseModule, Integer[] structIds, Date startTime, Date endTime, String order){ Page p = page(()->{ userNoteQuestionRelationMapper.listExercise(userId, keyword, questionTypes, courseModule, structIds, startTime, endTime, order); }, page, size); Collection ids = Transform.getIds(p, UserNoteQuestion.class, "id"); // 获取详细数据 List list = select(ids); Transform.replace(p, list, UserNoteQuestion.class, "id"); return p; } /** * 获取用户笔记列表 * @param page * @param size * @param userId * @param questionTypes * @param structIds * @param startTime * @param endTime * @param order * @return */ public Page listExamination(int page, int size, Integer userId, String keyword, String[] questionTypes, Integer[] structIds, Integer libraryId, String year, Date startTime, Date endTime, String order){ Page p = page(()->{ userNoteQuestionRelationMapper.listExamination(userId, keyword, questionTypes, structIds, libraryId, year, startTime, endTime, order); }, page, size); Collection ids = Transform.getIds(p, UserNoteQuestion.class, "id"); // 获取详细数据 List list = select(ids); Transform.replace(p, list, UserNoteQuestion.class, "id"); return p; } /** * 查询用户笔记情况 * @param userId * @param questionId * @return */ public UserNoteQuestion getByUserAndQuestion(Integer userId, Integer questionId){ Example example = new Example(UserNoteQuestion.class); example.and( example.createCriteria() .andEqualTo("userId", userId) .andEqualTo("questionId", questionId) ); UserNoteQuestion in = one(userNoteQuestionMapper, example); return in; } /** * 查询用户笔记情况:批量 * @param userId * @param ids * @return */ public List listByUserAndQuestions(Integer userId, Collection ids){ if(ids==null || ids.size() == 0) return new ArrayList<>(); Example example = new Example(UserNoteQuestion.class); example.and( example.createCriteria() .andEqualTo("userId", userId) .andIn("questionId", ids) ); return select(userNoteQuestionMapper, example); } /** * 更新用户笔记:没有则添加 * @param note * @return */ @Transactional public UserNoteQuestion updateNote(UserNoteQuestion note){ Example example = new Example(UserNoteQuestion.class); example.and( example.createCriteria() .andEqualTo("userId", note.getUserId()) .andEqualTo("questionModule", note.getQuestionModule()) ); example.and( example.createCriteria() .orEqualTo("questionId", note.getQuestionId()) .orEqualTo("questionNoId", note.getQuestionNoId()) ); UserNoteQuestion in = one(userNoteQuestionMapper, example); Date now = new Date(); if(in == null){ // 按实际更新更改更新时间 if (note.getQuestionContent() != null && !note.getQuestionContent().isEmpty()){ note.setQuestionTime(now); } if (note.getOfficialContent() != null && !note.getOfficialContent().isEmpty()){ note.setOfficialTime(now); } if (note.getQxContent() != null && !note.getQxContent().isEmpty()){ note.setQxTime(now); } if (note.getAssociationContent() != null && !note.getAssociationContent().isEmpty()){ note.setAssociationTime(now); } if (note.getQaContent() != null && !note.getQaContent().isEmpty()){ note.setQaTime(now); } return add(note); }else{ note.setId(in.getId()); // 按实际更新更改更新时间 if (note.getQuestionContent() != null && !note.getQuestionContent().equals(in.getQuestionContent())){ note.setQuestionTime(now); } if (note.getOfficialContent() != null && !note.getOfficialContent().equals(in.getOfficialContent())){ note.setOfficialTime(now); } if (note.getQxContent() != null && !note.getQxContent().equals(in.getQxContent())){ note.setQxTime(now); } if (note.getAssociationContent() != null && !note.getAssociationContent().equals(in.getAssociationContent())){ note.setAssociationTime(now); } if (note.getQaContent() != null && !note.getQaContent().equals(in.getQaContent())){ note.setQaTime(now); } // 如果所有内容为空,则删除 return edit(note); } } /** * 删除笔记 * @param userId * @param questionId * @return */ @Transactional public boolean deleteNote(Integer userId, Integer questionId){ Example example = new Example(UserNoteQuestion.class); example.and( example.createCriteria() .andEqualTo("userId", userId) .andEqualTo("questionId", questionId) ); UserNoteQuestion in = one(userNoteQuestionMapper, example); if (in == null){ return true; } return delete(in.getId()); } public UserNoteQuestion add(UserNoteQuestion note){ int result = insert(userNoteQuestionMapper, note); note = one(userNoteQuestionMapper, note.getId()); if(note == null){ throw new SystemException("笔记添加失败"); } return note; } public UserNoteQuestion edit(UserNoteQuestion note){ UserNoteQuestion in = one(userNoteQuestionMapper, note.getId()); if(in == null){ throw new ParameterException("笔记不存在"); } int result = update(userNoteQuestionMapper, note); return note; } public boolean delete(Number id){ UserNoteQuestion in = one(userNoteQuestionMapper, id); if(in == null){ throw new ParameterException("笔记不存在"); } int result = delete(userNoteQuestionMapper, id); return result > 0; } public UserNoteQuestion get(Number id){ UserNoteQuestion in = one(userNoteQuestionMapper, id); if(in == null){ throw new ParameterException("笔记不存在"); } return in; } public Page select(int page, int pageSize){ return select(userNoteQuestionMapper, page, pageSize); } public Page select(Integer[] ids){ return page(()->select(userNoteQuestionMapper, ids), 1, ids.length); } public List select(Collection ids){ return select(userNoteQuestionMapper, ids); } }