package com.qxueyou.scc.teach.student.service.impl; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.util.RandomUtil; import com.qxueyou.scc.admin.classes.model.ClsClass; import com.qxueyou.scc.exam.model.ExamResultV; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.qxueyou.scc.admin.classes.service.IClassService; import com.qxueyou.scc.admin.progress.model.view.QSubjectProgressTreeV; import com.qxueyou.scc.admin.progress.model.view.SubjectProgressTreeV; import com.qxueyou.scc.base.model.Pager; import com.qxueyou.scc.base.model.Result; import com.qxueyou.scc.base.service.impl.CommonAppService; import com.qxueyou.scc.base.util.ClientUtils; import com.qxueyou.scc.base.util.CollectionUtils; import com.qxueyou.scc.base.util.TraceUtils; import com.qxueyou.scc.teach.student.dao.StudentDAO; import com.qxueyou.scc.teach.student.model.QStuStudent; import com.qxueyou.scc.teach.student.model.StuStudent; import com.qxueyou.scc.teach.student.service.IStudentService; import com.qxueyou.scc.teach.subject.service.ISubjectService; import com.qxueyou.scc.user.model.QUser; import com.qxueyou.scc.user.model.User; import com.qxueyou.scc.user.service.IUserService; @Service public class StudentService extends CommonAppService implements IStudentService { @Autowired IClassService clsService; @Autowired IUserService userService; @Autowired ISubjectService subjectService; @Autowired StudentDAO dao; @Override public Result insertStudent(String classId, String name, String studentNo, boolean sex, String phoneNo, String orgId) { if (dao.exists(studentNo, orgId)) { return new Result(false, "学号已存在"); } User user = userService.insertUser(name, studentNo, phoneNo, "000000", sex, orgId); //ClsClass cls = clsService.read(classId); StuStudent student = new StuStudent(); TraceUtils.setCreateTrace(student); student.setStatus(StuStudent.STATUS_ACTIVE); student.setClassId(classId); //student.setSubjectId(cls.getSubjects().get(0).getSubjectId()); student.setName(name); student.setStudentNo(studentNo); student.setSex(sex); student.setUserId(user.getUserId()); student.setMobilePhone(phoneNo); if(StringUtils.isNoneBlank(classId)) { bulkUpdate("update ClsClass set studentCount = studentCount + 1 where deleteFlag is false and classId = ?", new Object[]{classId}); } save(student); return new Result(true, "success", CollectionUtils.newStringMap("studentId", student.getStudentId(), "password", user.getPassword())); } @Override public Result insertStudent(String classId, String name, String studentNo, boolean sex, String phoneNo, String orgId,String comName) { if (dao.exists(studentNo, orgId)) { return new Result(false, "身份证已存在"); } User user = userService.insertUser(name, studentNo, phoneNo, "000000", sex, orgId); //ClsClass cls = clsService.read(classId); StuStudent student = new StuStudent(); TraceUtils.setCreateTrace(student); String currentTime = LocalDateTimeUtil.format(LocalDateTime.now(), "yyMMddHHmmssSSS"); String examCardNo = currentTime.concat(RandomUtil.randomNumbers(1)); student.setStudentNumber(examCardNo); student.setStatus(StuStudent.STATUS_ACTIVE); student.setClassId(classId); //student.setSubjectId(cls.getSubjects().get(0).getSubjectId()); student.setName(name); student.setStudentNo(studentNo); student.setSex(sex); student.setUserId(user.getUserId()); student.setMobilePhone(phoneNo); //公司名称 student.setSubjectId(comName); if(StringUtils.isNoneBlank(classId)) { bulkUpdate("update ClsClass set studentCount = studentCount + 1 where deleteFlag is false and classId = ?", new Object[]{classId}); } save(student); return new Result(true, "success", CollectionUtils.newStringMap("studentId", student.getStudentId(), "password", user.getPassword())); } @Override public Result updateImgStudent(String studentId){ //根据学学员ID,获取用户信息 StuStudent stu = this.read(StuStudent.class, studentId); stu.setTenantId("1"); save(stu); return new Result(true, "success"); } @Override public Result updateStudent(String studentId, String name,String password, String studentNo, boolean sex, String phoneNo) { //根据学学员ID,获取用户信息 StuStudent stu = this.read(StuStudent.class, studentId); if (!stu.getStudentNo().equals(studentNo) && dao.exists(studentNo,ClientUtils.getOrgId())) { return new Result(false, "学号已存在"); } stu.setName(name); stu.setSex(sex); stu.setStudentNo(studentNo); stu.setMobilePhone(phoneNo); TraceUtils.setUpdateTrace(stu); save(stu); User user = this.read(User.class, stu.getUserId()); user.setAccount(studentNo); user.setSex(sex); user.setMobilePhone(phoneNo); if(!StringUtils.isEmpty(password)){ user.setPassword(password); } user.setName(name); save(user); return new Result(true, "success"); } @Override public List> getStudentLst(String classId, String keyword, Pager pager) { QUser qUser = QUser.user; QStuStudent qStuStudent = QStuStudent.stuStudent; return this.getQueryFactory() .select(qStuStudent.studentId, qStuStudent.name, qStuStudent.studentNo, qStuStudent.sex, qStuStudent.mobilePhone, qStuStudent.status, qStuStudent.studyDuration, qStuStudent.tenantId, qStuStudent.score, qUser.imgPath) .from(qUser, qStuStudent) .where(qUser.deleteFlag.isFalse().and(qUser.userId.eq(qStuStudent.userId)) .and(qStuStudent.classId.eq(classId)).and(qStuStudent.name.like("%" + keyword + "%")).and(qStuStudent.deleteFlag.isFalse())) .limit(pager.getPageSize()).offset(pager.getOffset()).orderBy(qStuStudent.createTime.desc()).fetch().stream().map(tuple -> { Map map = new HashMap(); map.put("studentId", tuple.get(qStuStudent.studentId)); map.put("name", tuple.get(qStuStudent.name)); map.put("studentNo", tuple.get(qStuStudent.studentNo)); map.put("studentNumber", tuple.get(qStuStudent.studentNo)); map.put("sex", tuple.get(qStuStudent.sex)); map.put("mobilePhone", tuple.get(qStuStudent.mobilePhone)); map.put("status", tuple.get(qStuStudent.status)); map.put("studyDuration", tuple.get(qStuStudent.studyDuration)); map.put("tenantId", tuple.get(qStuStudent.tenantId)); map.put("score", tuple.get(qStuStudent.score)); map.put("imgPath", tuple.get(qUser.imgPath)); return map; }).collect(Collectors.toList()); } @Override public int getStudentsCount(String classId, String keyword) { // //TODO 判断是否为补考分组 // ClsClass cls = clsService.read(classId); // String hql=null; // if(cls!=null && cls.getClassTypes()!=null && "1".equals(cls.getClassTypes())){ // //TODO 补考分组需要另一种查询学员 stu.tenantId = class.classId // hql = "from StuStudent where tenantId=:classId and name like :keyword and deleteFlag is false"; // }else { String hql = "from StuStudent where classId=:classId and name like :keyword and deleteFlag is false"; // } return findCountByComplexHql(hql, CollectionUtils.newObjectMap("classId", classId, "keyword", "%" + keyword + "%" )); } @Override public Result deleteStudent(String classId, String[] studentIds) { for (String studentId : studentIds) { Result result = deleteStudent(classId, studentId); if (!result.isSuccess()) { return result; } } return new Result(true); } private Result deleteStudent(String classId, String studentId) { StuStudent student = read(StuStudent.class, studentId); User user = read(User.class, student.getUserId()); if (StuStudent.STATUS_ACTIVE.equals(student.getStatus())) { return new Result(false, "已激活学员不能移除"); } TraceUtils.setUpdateTrace(student); student.setDeleteFlag(true); save(student); deleteTrace(user); return new Result(true); } @Override public Result doActivateStudent(String[] studentIds) { for (String studentId : studentIds) { activateStudent(studentId, StuStudent.STATUS_ACTIVE); } return new Result(true); } @Override public Result doDeActivateStudent(String[] studentIds) { for (String studentId : studentIds) { activateStudent(studentId, StuStudent.STATUS_DEACTIVE); } return new Result(true); } private Result activateStudent(String studentId, String status) { StuStudent student = read(StuStudent.class, studentId); if (StringUtils.equals(status, student.getStatus())) { return new Result(false); } TraceUtils.setUpdateTrace(student); String currentTime = LocalDateTimeUtil.format(LocalDateTime.now(), "yyMMddHHmmssSSS"); String examCardNo = currentTime.concat(RandomUtil.randomNumbers(1)); student.setStudentNumber(examCardNo); student.setStatus(status); save(student); clsService.addStudent(student.getClassId(), StuStudent.STATUS_DEACTIVE.equals(status) ? -1 : 1); return new Result(true); } @Override public Result queryStudent(String classId, String studentId) { StuStudent student = read(StuStudent.class, studentId); if(student == null) { return new Result(false, "未查询到学员信息"); } return new Result(true, "success", CollectionUtils.newObjectMap("studentId", student.getStudentId(), "name", student.getName(), "status", student.getStatus(), "studentNo", student.getStudentNo(), "sex", student.getSex(), "mobilePhone", student.getMobilePhone())); } @Override public StuStudent getStudentByUserId(String userId) { return this.findUnique("from StuStudent where userId=? ", CollectionUtils.newList(userId), StuStudent.class); } @Override public StuStudent getStudentByNo(String studentNo) { return this.findUnique("from StuStudent where studentNo=? ", CollectionUtils.newList(studentNo), StuStudent.class); } @Override public List getStudentByclassId(String classId) { QStuStudent qClassStudent=QStuStudent.stuStudent; return new ArrayList<>(this.getQueryFactory() .select(qClassStudent ) .from(qClassStudent) .where(qClassStudent.classId.eq(classId)) .fetch()); } @Override public Result studentProgress(String classId, String studentId) { StuStudent student = read(StuStudent.class, studentId); if(student == null) { return new Result(false, "未查询到学员信息"); } String userId = student.getUserId(); User u = this.read(User.class, userId); if(u == null) { return new Result(false, "未查询到用户信息"); } QSubjectProgressTreeV qSubjectProgressTreeV = QSubjectProgressTreeV.subjectProgressTreeV; //班级进度 SubjectProgressTreeV objSubjectProgressTreeV = this.getQueryFactory().selectFrom(qSubjectProgressTreeV).where(qSubjectProgressTreeV.id.nodeId.eq(classId).and(qSubjectProgressTreeV.id.userId.eq(userId))).fetchOne(); if(objSubjectProgressTreeV != null) { //学员总数 long studentCount = this.getQueryFactory().selectFrom(qSubjectProgressTreeV).where(qSubjectProgressTreeV.id.nodeId.eq(classId)).fetchCount(); // 当前学员排行 long studentIndex = this .getQueryFactory().selectFrom(qSubjectProgressTreeV).where(qSubjectProgressTreeV.id.nodeId .eq(classId).and(qSubjectProgressTreeV.percent.gt(objSubjectProgressTreeV.getPercent()) .or(qSubjectProgressTreeV.percent.eq(objSubjectProgressTreeV.getPercent())))) .fetchCount(); //学员的课程 List> lstSubject = this.subjectService.myClsSubjectlist(classId, userId, new Pager(Integer.MAX_VALUE, 0)); return new Result(true, "success", CollectionUtils.newObjectMap("studentId", student.getStudentId(), "name", student.getName(), "imgPath", u.getImgPath(), "status", student.getStatus(), "studentNumber", student.getStudentNo(), "sex", student.getSex(), "mobilePhone", student.getMobilePhone(), "subjectList", lstSubject, "studentCount", studentCount, "studentIndex", studentIndex, "studyDuration", objSubjectProgressTreeV.getProgressValue())); }{ return new Result(true, "success", CollectionUtils.newObjectMap("studentId", student.getStudentId(), "name", student.getName(), "imgPath", u.getImgPath(), "status", student.getStatus(), "studentNumber", student.getStudentNo(), "sex", student.getSex(), "mobilePhone", student.getMobilePhone(), "subjectList", new ArrayList>(), "studentCount", 0, "studentIndex", 1, "studyDuration", 0)); } } }