Browse Source

学生帐号关联

Eric 2 years ago
parent
commit
0be664ba33

+ 1 - 1
cooleshow-auth/auth-server/src/main/java/com/yonge/cooleshow/auth/core/provider/PhoneAuthenticationProvider.java

@@ -74,7 +74,7 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
                 //获取jwt原始内容
                 String claims = jwt.getClaims();
                 if (StringUtils.isEmpty(claims)) {
-                    throw new BizException("三方授权登录失败");
+                    throw new BizException("三方授权校验失败");
                 }
                 log.info("retrieveUser claims={}", claims);
             } catch (Exception e) {

+ 33 - 0
cooleshow-user/user-admin/src/main/java/com/yonge/cooleshow/admin/controller/open/AdminClient.java

@@ -1,5 +1,6 @@
 package com.yonge.cooleshow.admin.controller.open;
 
+import com.microsvc.toolkit.config.jwt.utils.RsaKeyHelper;
 import com.yonge.cooleshow.admin.io.request.coupon.CouponOrderVO;
 import com.yonge.cooleshow.api.feign.dto.CouponInfoApi;
 import com.yonge.cooleshow.api.feign.dto.EmployeeApi;
@@ -17,24 +18,32 @@ import com.yonge.cooleshow.biz.dal.service.EmployeeService;
 import com.yonge.cooleshow.biz.dal.service.StudentService;
 import com.yonge.cooleshow.biz.dal.service.TeacherService;
 import com.yonge.cooleshow.biz.dal.service.UserFirstTimeService;
+import com.yonge.cooleshow.biz.dal.wrapper.StudentWrapper;
 import com.yonge.cooleshow.biz.dal.wrapper.coupon.CouponOrderWrapper;
 import com.yonge.cooleshow.common.controller.BaseController;
 import com.yonge.cooleshow.common.entity.HttpResponseResult;
 import com.yonge.cooleshow.common.enums.UserFirstTimeTypeEnum;
+import com.yonge.toolset.base.exception.BizException;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.joda.time.DateTime;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.jwt.Jwt;
+import org.springframework.security.jwt.JwtHelper;
+import org.springframework.security.jwt.crypto.sign.RsaVerifier;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.math.BigDecimal;
+import java.security.interfaces.RSAPublicKey;
 import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -88,6 +97,30 @@ public class AdminClient extends BaseController {
         return succeed(studentApi);
     }
 
+    @ApiOperation(value = "学生帐号关联Id", notes = "CouponOrderVO.PageRequest")
+    @PostMapping("/unionStudent")
+    public HttpResponseResult<StudentWrapper.UnionStudentResp> unionStudent(@RequestBody StudentWrapper.UnionStudent info) {
+
+        if (StringUtils.isAnyBlank(info.getToken(), info.getMobile())) {
+            throw new BizException("请求参数错误");
+        }
+
+        // 校验三方帐号关联请求合法性
+        RSAPublicKey rsaPublicKey = RsaKeyHelper.getRSAPublicKey("jmedu", "dayaedu", "jmedu.jks", "dayaedu");
+        Jwt jwt = JwtHelper.decodeAndVerify(info.getToken(), new RsaVerifier(rsaPublicKey));
+
+        //获取jwt原始内容
+        String claims = jwt.getClaims();
+        if (StringUtils.isEmpty(claims)) {
+            throw new BizException("三方授权校验失败");
+        }
+
+        // 学生帐号关联
+        StudentWrapper.UnionStudentResp studentResp = studentService.unionStudent(info);
+
+        return succeed(studentResp);
+    }
+
     @GetMapping("/getEmployee")
     public HttpResponseResult<EmployeeApi> getEmployee(@RequestParam("userId") Long userId) {
         Employee employee = employeeService.get(userId);

+ 10 - 4
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/dao/SubjectDao.java

@@ -1,11 +1,10 @@
 package com.yonge.cooleshow.biz.dal.dao;
 
-import java.util.List;
-
-import org.apache.ibatis.annotations.Param;
-
 import com.yonge.cooleshow.biz.dal.entity.Subject;
 import com.yonge.toolset.mybatis.dal.BaseDAO;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 public interface SubjectDao extends BaseDAO<Long, Subject> {
     /***
@@ -55,4 +54,11 @@ public interface SubjectDao extends BaseDAO<Long, Subject> {
     Subject queryByName(@Param("name") String name);
 
     List<Subject> subjectSelect(@Param("type") String type);
+
+    /**
+     * 声部模糊匹配
+     * @param subjectName 声部匹配
+     * @return Subject
+     */
+    Subject getSubjectMatchByName(@Param("name") String subjectName);
 }

+ 12 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/entity/Student.java

@@ -75,6 +75,10 @@ public class Student implements Serializable {
     @TableField(value = "evaluate_time_")
     private Long evaluateTime;
 
+    @ApiModelProperty("帐号关联Id ")
+    @TableField(value = "union_id_")
+    private Long unionId;
+
 	@TableField(value = "create_time_")
     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
@@ -179,4 +183,12 @@ public class Student implements Serializable {
     public void setEvaluateTime(Long evaluateTime) {
         this.evaluateTime = evaluateTime;
     }
+
+    public Long getUnionId() {
+        return unionId;
+    }
+
+    public void setUnionId(Long unionId) {
+        this.unionId = unionId;
+    }
 }

+ 12 - 1
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/StudentService.java

@@ -7,7 +7,11 @@ import com.yonge.cooleshow.biz.dal.dto.search.QueryMyFollowSearch;
 import com.yonge.cooleshow.biz.dal.dto.search.StudentSearch;
 import com.yonge.cooleshow.biz.dal.entity.Student;
 import com.yonge.cooleshow.biz.dal.entity.Subject;
-import com.yonge.cooleshow.biz.dal.vo.*;
+import com.yonge.cooleshow.biz.dal.vo.MyFollow;
+import com.yonge.cooleshow.biz.dal.vo.StudentHomeVo;
+import com.yonge.cooleshow.biz.dal.vo.StudentVo;
+import com.yonge.cooleshow.biz.dal.vo.TeacherVo;
+import com.yonge.cooleshow.biz.dal.wrapper.StudentWrapper;
 import com.yonge.cooleshow.common.entity.HttpResponseResult;
 
 import java.util.List;
@@ -93,4 +97,11 @@ public interface StudentService extends IService<Student> {
      * @return
      */
     HttpResponseResult<Map<String, TeacherVo>> bindTeacher(String phone, Long userId, Boolean isUpdate);
+
+    /**
+     * 学生帐号关联
+     * @param info StudentWrapper.UnionStudent
+     * @return StudentWrapper.UnionStudentResp
+     */
+    StudentWrapper.UnionStudentResp unionStudent(StudentWrapper.UnionStudent info);
 }

+ 132 - 10
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/StudentServiceImpl.java

@@ -5,20 +5,28 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.yonge.cooleshow.auth.api.entity.SysUser;
+import com.yonge.cooleshow.biz.dal.dao.StudentDao;
+import com.yonge.cooleshow.biz.dal.dao.SubjectDao;
 import com.yonge.cooleshow.biz.dal.dao.TeacherDao;
 import com.yonge.cooleshow.biz.dal.dao.UserBindingTeacherDao;
 import com.yonge.cooleshow.biz.dal.dto.search.QueryMyFollowSearch;
 import com.yonge.cooleshow.biz.dal.dto.search.StudentSearch;
+import com.yonge.cooleshow.biz.dal.entity.Student;
 import com.yonge.cooleshow.biz.dal.entity.StudentTotal;
 import com.yonge.cooleshow.biz.dal.entity.Subject;
 import com.yonge.cooleshow.biz.dal.entity.UserBindingTeacher;
 import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
+import com.yonge.cooleshow.biz.dal.mapper.SysUserMapper;
 import com.yonge.cooleshow.biz.dal.service.ImUserFriendService;
-import com.yonge.cooleshow.biz.dal.service.TeacherService;
-import com.yonge.cooleshow.common.enums.CacheNameEnum;
+import com.yonge.cooleshow.biz.dal.service.StudentService;
 import com.yonge.cooleshow.biz.dal.service.StudentTotalService;
-import com.yonge.cooleshow.biz.dal.vo.*;
+import com.yonge.cooleshow.biz.dal.vo.MyFollow;
+import com.yonge.cooleshow.biz.dal.vo.StudentHomeVo;
+import com.yonge.cooleshow.biz.dal.vo.StudentVo;
+import com.yonge.cooleshow.biz.dal.vo.TeacherVo;
+import com.yonge.cooleshow.biz.dal.wrapper.StudentWrapper;
 import com.yonge.cooleshow.common.entity.HttpResponseResult;
+import com.yonge.cooleshow.common.enums.CacheNameEnum;
 import com.yonge.cooleshow.common.enums.UserLockFlag;
 import com.yonge.toolset.base.util.StringUtil;
 import com.yonge.toolset.utils.date.DateUtil;
@@ -28,13 +36,17 @@ import org.redisson.api.RedissonClient;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import com.yonge.cooleshow.biz.dal.entity.Student;
-import com.yonge.cooleshow.biz.dal.dao.StudentDao;
-import com.yonge.cooleshow.biz.dal.service.StudentService;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.text.MessageFormat;
-import java.util.*;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
 
@@ -48,10 +60,12 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> impleme
     private ImUserFriendService imUserFriendService;
     @Resource
     private UserBindingTeacherDao userBindingTeacherDao;
-    @Autowired
-    private StudentService studentService;
     @Resource
     private TeacherDao teacherDao;
+    @Autowired
+    private SysUserMapper sysUserMapper;
+    @Autowired
+    private SubjectDao subjectDao;
 
     @Override
     public StudentVo detail(Long userId) {
@@ -165,7 +179,7 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> impleme
         }
         resMap.put("old", old);
 
-        StudentVo studentVo = studentService.detailByPhone(phone);
+        StudentVo studentVo = detailByPhone(phone);
 
         if (null == studentVo) {
             return HttpResponseResult.failed("未找到用户信息");
@@ -202,4 +216,112 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> impleme
         return HttpResponseResult.succeed(resMap);
     }
 
+    /**
+     * 学生帐号关联
+     *
+     * @param info StudentWrapper.UnionStudent
+     * @return StudentWrapper.UnionStudentResp
+     */
+    @Transactional
+    @Override
+    public StudentWrapper.UnionStudentResp unionStudent(StudentWrapper.UnionStudent info) {
+
+        // 帐号关联响应数据
+        StudentWrapper.UnionStudentResp studentResp = StudentWrapper.UnionStudentResp
+                .builder()
+                .unionId(info.getUnionId())
+                .mobile(info.getMobile())
+                .updateFlag(false)
+                .build();
+
+        // 手机号查询用户信息
+        StudentVo studentVo = detailByPhone(info.getMobile());
+        if (Objects.isNull(studentVo)) {
+
+            com.yonge.cooleshow.biz.dal.entity.SysUser sysUser = sysUserMapper.selectOne(Wrappers.<com.yonge.cooleshow.biz.dal.entity.SysUser>lambdaQuery()
+                    .eq(com.yonge.cooleshow.biz.dal.entity.SysUser::getPhone, info.getMobile()));
+
+            if (Objects.isNull(sysUser)) {
+
+                // 自动注册创建用户
+                com.yonge.cooleshow.biz.dal.entity.SysUser user = new com.yonge.cooleshow.biz.dal.entity.SysUser();
+                user.setUsername(info.getUsername());
+                user.setPhone(info.getMobile());
+                user.setLockFlag(0);
+                user.setGender(info.getGender());
+                sysUserMapper.insert(user);
+
+                String subjectId = null;
+                // 学生声部
+                Subject subject = subjectDao.getSubjectMatchByName(info.getSubjectName());
+                if (Objects.nonNull(subject)) {
+                    subjectId = String.valueOf(subject.getId());
+                }
+
+                // 添加学生帐号
+                Student student = new Student();
+                student.setUserId(user.getId());
+                student.setSubjectId(subjectId);
+                student.setUnionId(info.getUnionId());
+                save(student);
+
+                // 更新标识
+                studentResp.setUpdateFlag(true);
+            } else {
+
+                com.yonge.cooleshow.biz.dal.entity.SysUser updateEntity = new com.yonge.cooleshow.biz.dal.entity.SysUser();
+                updateEntity.setId(sysUser.getId());
+                updateEntity.setDelFlag(0); // 激活帐号
+                // 激活帐号
+                sysUserMapper.updateById(updateEntity);
+
+                studentResp.username(sysUser.getUsername()).userId(sysUser.getId());
+
+                Student student = lambdaQuery().eq(Student::getUserId, sysUser.getId()).one();
+
+                // 更新学生关联帐号
+                updateStudentUnionStatus(info, studentResp, student);
+
+            }
+
+        } else {
+
+            studentResp.username(studentVo.getUsername()).userId(studentVo.getUserId());
+
+            Student student = lambdaQuery().eq(Student::getUserId, studentVo.getUserId()).one();
+
+            // 更新学生关联帐号
+            updateStudentUnionStatus(info, studentResp, student);
+        }
+
+        return studentResp;
+    }
+
+    /**
+     * 更新学生关联帐号
+     * @param info StudentWrapper.UnionStudent
+     * @param studentResp StudentWrapper.UnionStudentResp
+     * @param student Student
+     */
+    private void updateStudentUnionStatus(StudentWrapper.UnionStudent info,
+                                          StudentWrapper.UnionStudentResp studentResp, Student student) {
+
+        if (Objects.nonNull(student)) {
+
+            if (Objects.isNull(student.getUnionId())) {
+
+                // 更新学生关联Id
+                lambdaUpdate()
+                        .eq(Student::getUserId, student.getUserId())
+                        .set(Student::getUnionId, info.getUnionId())
+                        .update();
+            } else {
+                studentResp.setUnionId(student.getUnionId());
+            }
+
+            // 更新标识
+            studentResp.setUpdateFlag(true);
+        }
+    }
+
 }

+ 77 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/wrapper/StudentWrapper.java

@@ -0,0 +1,77 @@
+package com.yonge.cooleshow.biz.dal.wrapper;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+/**
+ * 学生封装类
+ * Created by Eric.Shang on 2023/1/6.
+ */
+public class StudentWrapper {
+
+    @Data
+    @Builder
+    @NoArgsConstructor
+    @AllArgsConstructor
+    @ApiModel("学生帐号关联")
+    public static class UnionStudent implements Serializable {
+
+        @ApiModelProperty("帐号关联Id")
+        private Long unionId;
+
+        @ApiModelProperty("授权Token")
+        private String token;
+
+        @ApiModelProperty("联系方式")
+        private String mobile;
+
+        @ApiModelProperty("声部名称")
+        private String subjectName;
+
+        @ApiModelProperty("用户名")
+        private String username;
+
+        @ApiModelProperty("性别(0,女  1,男)")
+        private Integer gender;
+
+    }
+
+    @Data
+    @Builder
+    @NoArgsConstructor
+    @AllArgsConstructor
+    @ApiModel("帐号关联响应")
+    public static class UnionStudentResp implements Serializable {
+
+        @ApiModelProperty("帐号关联Id")
+        private Long unionId;
+
+        @ApiModelProperty("联系方式")
+        private String mobile;
+
+        @ApiModelProperty("用户Id")
+        private Long userId;
+
+        @ApiModelProperty("用户名")
+        private String username;
+
+        @ApiModelProperty("更新标识")
+        private Boolean updateFlag;
+
+        public UnionStudentResp userId(Long userId) {
+            this.userId = userId;
+            return this;
+        }
+
+        public UnionStudentResp username(String username) {
+            this.username = username;
+            return this;
+        }
+    }
+}

+ 5 - 1
cooleshow-user/user-biz/src/main/resources/config/mybatis/SubjectMapper.xml

@@ -200,6 +200,10 @@
         where  t.id_ = #{id}
     </select>
 
-
+    <!--声部模糊匹配-->
+    <select id="getSubjectMatchByName" resultType="com.yonge.cooleshow.biz.dal.entity.Subject">
+        SELECT * FROM subject WHERE del_flag_ = 0 and name_ LIKE CONCAT('%', #{name}, '%') LIMIT 1
+    </select>
+    <!--声部模糊匹配-->
 
 </mapper>