|
@@ -0,0 +1,306 @@
|
|
|
|
+package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
|
|
+
|
|
|
|
+import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.contains;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+import javax.transaction.Transactional;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.data.domain.Example;
|
|
|
|
+import org.springframework.data.domain.ExampleMatcher;
|
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.ErrorMsg;
|
|
|
|
+import cn.com.qmth.examcloud.commons.web.security.AccessCtrlUtil;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.base.enums.UserType;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.StudentRepo;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.UserRepo;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.constants.Consts;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.Org;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.Student;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserEntity;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserRole;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.enums.LoginType;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.enums.UserScope;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.StudentService;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.bean.StudentInfo;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.bean.UserInfo;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 学生服务类 Created by songyue on 17/1/14.
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class StudentServiceImpl implements StudentService {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ StudentRepo studentRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ UserRepo userRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ UserServiceImpl userService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ OrgRepo orgRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ DataSendService dataSendService;
|
|
|
|
+
|
|
|
|
+ private static final String JPG = ".jpg";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取所有学生(分页)
|
|
|
|
+ *
|
|
|
|
+ * @param studentCriteria
|
|
|
|
+ * @param pageable
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public Page<Student> getAllStudent(Student studentCriteria, Pageable pageable) {
|
|
|
|
+ ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("name", contains())
|
|
|
|
+ .withMatcher("studentCode", contains()).withMatcher("identityNumber", contains())
|
|
|
|
+ .withIgnoreNullValues();
|
|
|
|
+ Example<Student> studentExample = Example.of(studentCriteria, exampleMatcher);
|
|
|
|
+ return studentRepo.findAll(studentExample, pageable);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取所有学生
|
|
|
|
+ *
|
|
|
|
+ * @param studentCriteria
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<Student> getAllStudent(Student studentCriteria) {
|
|
|
|
+ ExampleMatcher exampleMatcher = ExampleMatcher.matching();
|
|
|
|
+ Example<Student> studentExample = Example.of(studentCriteria, exampleMatcher);
|
|
|
|
+ return studentRepo.findAll(studentExample);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 绑定学生照片
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<ErrorMsg> photopBundled(Long rootOrgId, File directory) {
|
|
|
|
+ List<ErrorMsg> list = new ArrayList<ErrorMsg>();
|
|
|
|
+ if (directory.isDirectory()) {
|
|
|
|
+ for (File file : directory.listFiles()) {
|
|
|
|
+ if (!file.getName().endsWith(JPG)) {
|
|
|
|
+ list.add(new ErrorMsg(file.getName() + "格式错误,文件名格式必须是jpg(小写)"));
|
|
|
|
+ }
|
|
|
|
+ String fileName = file.getName().split(JPG)[0];
|
|
|
|
+ // 先查学号
|
|
|
|
+ Student student = studentRepo.findByStudentCodeAndRootOrgId(fileName, rootOrgId);
|
|
|
|
+ if (student == null) {// 学号不存在查身份证号
|
|
|
|
+ student = studentRepo.findByIdentityNumber(fileName);
|
|
|
|
+ }
|
|
|
|
+ if (student != null) {// 学生存在则绑定照片
|
|
|
|
+ student.setPhotoPath(student.getUser().getRootOrgId() + File.separator + "photo"
|
|
|
|
+ + File.separator + fileName);
|
|
|
|
+ studentRepo.save(student);
|
|
|
|
+ dataSendService.sendStudent(student);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ list.add(new ErrorMsg("文件路径不正确"));
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional
|
|
|
|
+ public Student save(Student student) {
|
|
|
|
+ // 判断是否有用户
|
|
|
|
+ if (student.getUser() == null || null == student.getUser().getId()) {
|
|
|
|
+ // 判断是否有该学生
|
|
|
|
+ if (!StringUtils.isEmpty(student.getStudentCode())) {
|
|
|
|
+ // 学号查找不为空,更新身份证号
|
|
|
|
+ Student s1 = studentRepo.findByRootOrgIdAndStudentCode(
|
|
|
|
+ student.getUser().getRootOrgId(), student.getStudentCode());
|
|
|
|
+ if (s1 != null) {
|
|
|
|
+ s1.setIdentityNumber(student.getIdentityNumber());
|
|
|
|
+ s1.setUpdateTime(new Date());
|
|
|
|
+ s1.setName(student.getName());
|
|
|
|
+ s1.setOrgId(student.getOrgId());
|
|
|
|
+ s1 = studentRepo.save(s1);
|
|
|
|
+ dataSendService.sendStudent(s1);
|
|
|
|
+ return s1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 身份证查找不为空,更新学号
|
|
|
|
+ Student s2 = studentRepo.findByIdentityNumberAndRootOrgId(student.getIdentityNumber(),
|
|
|
|
+ student.getRootOrgId());
|
|
|
|
+ if (s2 != null) {
|
|
|
|
+ s2.setStudentCode(student.getStudentCode());
|
|
|
|
+ s2.setUpdateTime(new Date());
|
|
|
|
+ s2.setName(student.getName());
|
|
|
|
+ s2.setOrgId(student.getOrgId());
|
|
|
|
+ s2 = studentRepo.save(s2);
|
|
|
|
+ dataSendService.sendStudent(s2);
|
|
|
|
+ return s2;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ student.setOrgId(student.getUser().getOrgId());
|
|
|
|
+ student.setRootOrgId(student.getUser().getRootOrgId());
|
|
|
|
+ // 新建用户和学生
|
|
|
|
+ UserEntity user = new UserEntity(student.getName(), UserScope.ORG,
|
|
|
|
+ student.getUser().getRootOrgId(), student.getUser().getOrgId(),
|
|
|
|
+ UserType.STUDENT);
|
|
|
|
+ user.setEnable(
|
|
|
|
+ student.getUser().getEnable() == null ? true : student.getUser().getEnable());
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isNotEmpty(student.getStudentCode())) {
|
|
|
|
+ user.setLoginName(student.getStudentCode());
|
|
|
|
+ } else {
|
|
|
|
+ user.setLoginName(student.getIdentityNumber());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String identityNumber = student.getIdentityNumber();
|
|
|
|
+ if (StringUtils.isNotEmpty(identityNumber)
|
|
|
|
+ && identityNumber.matches("[0-9a-zA-Z]{6,}")) {
|
|
|
|
+ user.setPassword(
|
|
|
|
+ StringUtils.substring(identityNumber, -6, identityNumber.length()));
|
|
|
|
+ } else {
|
|
|
|
+ user.setPassword(Consts.DEFAULT_PASSWORD);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<UserRole> userRoles = new ArrayList<UserRole>();
|
|
|
|
+ userRoles.add(new UserRole("STUDENT"));
|
|
|
|
+ user.setUserRoles(userRoles);
|
|
|
|
+ userRepo.save(user);
|
|
|
|
+ student.setUser(user);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ student = studentRepo.save(student);
|
|
|
|
+ dataSendService.sendStudent(student);
|
|
|
|
+ return student;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 二级登录
|
|
|
|
+ *
|
|
|
|
+ * @param orgId
|
|
|
|
+ * @param loginName
|
|
|
|
+ * @param password
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public UserInfo login(String orgId, String loginName, String password, LoginType loginType)
|
|
|
|
+ throws Exception {
|
|
|
|
+ Org org = orgRepo.findFirstByParentIdAndCode((long) 0, orgId);
|
|
|
|
+ if (org == null) {
|
|
|
|
+ throw new RuntimeException("学校不存在");
|
|
|
|
+ }
|
|
|
|
+ Student student = null;
|
|
|
|
+ if (LoginType.STUDENT_CODE.equals(loginType)) {
|
|
|
|
+ student = studentRepo.findByStudentCodeAndRootOrgId(loginName, org.getId());
|
|
|
|
+ } else if (LoginType.IDENTITY_NUMBER.equals(loginType)) {
|
|
|
|
+ student = studentRepo.findByIdentityNumberAndRootOrgId(loginName, org.getId());
|
|
|
|
+ }
|
|
|
|
+ if (student == null) {
|
|
|
|
+ throw new RuntimeException("该用户不存在");
|
|
|
|
+ }
|
|
|
|
+ return this.loginProcess(student, password);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private UserInfo loginProcess(Student student, String password) throws Exception {
|
|
|
|
+ if (student.getUser() == null) {
|
|
|
|
+ throw new RuntimeException("该用户不存在");
|
|
|
|
+ } else if (!student.getUser().getPassword().equals(password)) {
|
|
|
|
+ throw new RuntimeException("密码错误");
|
|
|
|
+ } else if (!student.getUser().getEnable()) {
|
|
|
|
+ throw new RuntimeException("该用户被禁用");
|
|
|
|
+ } else {
|
|
|
|
+ String token = AccessCtrlUtil.buildToken();
|
|
|
|
+ userService.initUserLogin(student.getUser());
|
|
|
|
+ userService.createAccessUser(token, student.getUser(), student.getId());
|
|
|
|
+ userService.createUserLogin(token, student.getUser());
|
|
|
|
+ UserInfo userInfo = userService.getUserInfo(student.getUser(), token);
|
|
|
|
+ userInfo.setStudentId(student.getId());
|
|
|
|
+ userInfo.setIdentityNumber(student.getIdentityNumber());
|
|
|
|
+ userInfo.setStudentCode(student.getStudentCode());
|
|
|
|
+ return userInfo;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Student update(Student student) {
|
|
|
|
+ student.setUpdateTime(new Date());
|
|
|
|
+ UserEntity user = userRepo.findOne(student.getUser().getId());
|
|
|
|
+ user.setName(student.getName());
|
|
|
|
+ student.setUser(user);
|
|
|
|
+ student = studentRepo.save(student);
|
|
|
|
+ dataSendService.sendStudent(student);
|
|
|
|
+ return student;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional
|
|
|
|
+ public Student insertOrUpdateStudent(StudentInfo studentInfo) {
|
|
|
|
+ Long rootOrgId = studentInfo.getRootOrgId();
|
|
|
|
+ Org rootOrg = orgRepo.findOne(rootOrgId);
|
|
|
|
+
|
|
|
|
+ if (null == rootOrg || (!rootOrg.getParentId().equals(0L))) {
|
|
|
|
+ throw new StatusException("B-160001", "顶级机构错误");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Long orgId = studentInfo.getOrgId();
|
|
|
|
+ String orgCode = studentInfo.getOrgCode();
|
|
|
|
+ String orgName = studentInfo.getOrgName();
|
|
|
|
+
|
|
|
|
+ if (null != orgId) {
|
|
|
|
+ Org org = orgRepo.findOne(orgId);
|
|
|
|
+ if (!org.getParentId().equals(rootOrgId)) {
|
|
|
|
+ throw new StatusException("B-160002", "机构错误");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (StringUtils.isBlank(orgName) || StringUtils.isBlank(orgCode)) {
|
|
|
|
+ Org org = orgRepo.findFirstByParentIdAndCode(rootOrgId, orgCode);
|
|
|
|
+ if (null == org) {
|
|
|
|
+ org = new Org();
|
|
|
|
+ org.setParentId(rootOrgId);
|
|
|
|
+ org.setCode(orgCode);
|
|
|
|
+ org.setName(orgName);
|
|
|
|
+ Org saved = orgRepo.save(org);
|
|
|
|
+ orgId = saved.getId();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String identityNumber = studentInfo.getIdentityNumber();
|
|
|
|
+ String studentCode = studentInfo.getStudentCode();
|
|
|
|
+
|
|
|
|
+ Student student = studentRepo.findByIdentityNumberAndStudentCodeAndRootOrgId(identityNumber,
|
|
|
|
+ studentCode, rootOrgId);
|
|
|
|
+
|
|
|
|
+ Student updatedStudent = new Student();
|
|
|
|
+ if (null != student) {
|
|
|
|
+ updatedStudent.setId(student.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ updatedStudent.setName(studentInfo.getName());
|
|
|
|
+ updatedStudent.setIdentityNumber(identityNumber);
|
|
|
|
+ updatedStudent.setStudentCode(studentCode);
|
|
|
|
+ updatedStudent.setOrgId(orgId);
|
|
|
|
+ if (null != studentInfo.getPhotoPath()) {
|
|
|
|
+ updatedStudent.setPhotoPath(studentInfo.getPhotoPath());
|
|
|
|
+ }
|
|
|
|
+ if (null != studentInfo.getPhoneNumber()) {
|
|
|
|
+ updatedStudent.setPhoneNumber(studentInfo.getPhoneNumber());
|
|
|
|
+ }
|
|
|
|
+ if (null != studentInfo.getRemark()) {
|
|
|
|
+ updatedStudent.setRemark(studentInfo.getRemark());
|
|
|
|
+ }
|
|
|
|
+ studentRepo.save(updatedStudent);
|
|
|
|
+
|
|
|
|
+ return updatedStudent;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|