package com.qxgmat.service.inline; 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.UserCourseAppointmentMapper; import com.qxgmat.data.dao.entity.CourseNo; import com.qxgmat.data.dao.entity.UserCourseAppointment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; @Service public class UserCourseAppointmentService extends AbstractService { private static final Logger logger = LoggerFactory.getLogger(UserCourseAppointmentService.class); @Resource private UserCourseAppointmentMapper userCourseAppointmentMapper; public Page listAdmin(int page, int size, Integer recordId, Integer userId, Integer courseId, String startTime, String endTime, String order, DirectionStatus direction){ Example example = new Example(UserCourseAppointment.class); if(recordId != null){ example.and( example.createCriteria() .andEqualTo("recordId", recordId) ); } if (userId != null){ example.and( example.createCriteria() .andEqualTo("userId", userId) ); } if (courseId != null){ example.and( example.createCriteria() .andEqualTo("courseId", courseId) ); } // 在课程结束前完成预习作业逻辑 if (startTime != null){ example.and( example.createCriteria() .andGreaterThanOrEqualTo("endTime", startTime) ); } if (endTime != null){ example.and( example.createCriteria() .andLessThanOrEqualTo("endTime", endTime) ); } 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(userCourseAppointmentMapper, example, page, size); } /** * 获取课程课时分组列表 * @param recordIds * @return */ public Map> groupByRecordId(Collection recordIds){ Map> relationMap = new HashMap<>(); Example example = new Example(UserCourseAppointment.class); example.and( example.createCriteria() .andIn("recordId", recordIds) ); example.setOrderByClause("recordId asc, no asc"); List nos = select(userCourseAppointmentMapper, example); Collection list; for(UserCourseAppointment no : nos){ if (!relationMap.containsKey(no.getRecordId())){ list = new ArrayList<>(); relationMap.put(no.getRecordId(), list); }else{ list = relationMap.get(no.getRecordId()); } list.add(no); } return relationMap; } public List listByRecord(Integer recordId){ Example example = new Example(UserCourseAppointment.class); example.and( example.createCriteria() .andEqualTo("recordId", recordId) ); return select(userCourseAppointmentMapper, example); } public UserCourseAppointment addAppointment(UserCourseAppointment appointment){ List list = listByRecord(appointment.getRecordId()); Integer max = 0; for(UserCourseAppointment no:list){ if (max < no.getNo()) max = no.getNo(); } appointment.setNo(max + 1); return add(appointment); } public Boolean deleteAppointment(Number id){ UserCourseAppointment in = get(id); List nos = listByRecord(in.getRecordId()); for(UserCourseAppointment no : nos){ if (no.getNo() < in.getNo()){ continue; } edit(UserCourseAppointment.builder().id(no.getId()).no(no.getNo() - 1).build()); } return delete(id); } public UserCourseAppointment add(UserCourseAppointment appointment){ int result = insert(userCourseAppointmentMapper, appointment); appointment = one(userCourseAppointmentMapper, appointment.getId()); if(appointment == null){ throw new SystemException("预约添加失败"); } return appointment; } public UserCourseAppointment edit(UserCourseAppointment appointment){ UserCourseAppointment in = one(userCourseAppointmentMapper, appointment.getId()); if(in == null){ throw new ParameterException("预约不存在"); } int result = update(userCourseAppointmentMapper, appointment); return appointment; } public boolean delete(Number id){ UserCourseAppointment in = one(userCourseAppointmentMapper, id); if(in == null){ throw new ParameterException("预约不存在"); } int result = delete(userCourseAppointmentMapper, id); return result > 0; } public UserCourseAppointment get(Number id){ UserCourseAppointment in = one(userCourseAppointmentMapper, id); if(in == null){ throw new ParameterException("预约不存在"); } return in; } public Page select(int page, int pageSize){ return select(userCourseAppointmentMapper, page, pageSize); } public Page select(Integer[] ids){ return page(()->select(userCourseAppointmentMapper, ids), 1, ids.length); } public List select(Collection ids){ return select(userCourseAppointmentMapper, ids); } }