zhanghua
2023-12-26 0cd9d911b8e8634ec04cb82ea07696bb09260b03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.ycl.service.auth.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ycl.common.dingding.DingCommon;
import com.ycl.entity.depart.UmsDepart;
import com.ycl.entity.dingding.DingUserInfo;
import com.ycl.entity.user.UmsAdmin;
import com.ycl.entity.user.UmsDepartManage;
import com.ycl.exception.ApiException;
import com.ycl.service.depart.UmsDepartService;
import com.ycl.service.user.UmsAdminService;
import com.ycl.service.user.UmsDepartManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Objects;
 
@Slf4j
@Service
public class AuthService implements com.ycl.service.auth.AuthService {
 
    @Autowired
    private UmsAdminService adminService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UmsDepartService departService;
    @Autowired
    private DingCommon dingCommon;
    @Autowired
    private UmsDepartManageService departManageService;
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String dingLogin(DingUserInfo dingUserInfo) {
        String openid = dingUserInfo.getOpenid();   //openid
        UmsAdmin dingUser = adminService.getByOpenid(openid); //根据openid查询
        UmsAdmin byId = adminService.getById(dingUserInfo.getUserId());
        String token = null;
        if (ObjectUtil.isNotEmpty(dingUser)) {       //钉钉已绑定
            token = adminService.getOAuthToken(dingUser.getUsername()); //调用系统登录接口
        } else if (Objects.nonNull(byId)) {
            byId.setOpenid(openid);
            adminService.updateById(byId);
            token = adminService.getOAuthToken(byId.getUsername()); //调用系统登录接口
        } else { //钉钉未绑定
            //按照id查询
            UmsAdmin userParam = new UmsAdmin();
            userParam.setId(dingUserInfo.getUserId());
            userParam.setNickName(dingUserInfo.getNickNameCn()); //昵称
            userParam.setUsername(dingUserInfo.getEmployeeName());  //姓名
            userParam.setSex(dingUserInfo.getEmpGender().byteValue()); //性别
            userParam.setOpenid(openid); //openid
            userParam.setIsDy(Byte.parseByte("0"));
            userParam.setMobile("");
            userParam.setAccountId(dingUserInfo.getAccountId());
            String encodePassword = passwordEncoder.encode("123456");
            userParam.setPassword(encodePassword); //密码
            userParam.setStatus(1); //状态
            adminService.save(userParam);
            DingUserInfo empInfoByCode = dingCommon.getEmpInfoByCode(dingUserInfo.getEmployeeCode());
            //查询部门是否存在,
            UmsDepart depart = departService.getById(empInfoByCode.getOrgId());
            log.info("部门---------------->{}",depart);
            //员工信息,关联组织
            if (Objects.nonNull(depart)) {
                UmsDepartManage departManage = new UmsDepartManage();
                departManage.setDepartId(depart.getId());
                departManage.setUserId(dingUserInfo.getUserId());
                departManage.setIsLeader(0);
                departManageService.save(departManage);
            }
            //根据openid查询
            dingUser = adminService.getByOpenid(openid);
            if (ObjectUtil.isEmpty(dingUser)) {
                log.info("【auth用户认证:】_AUTH_DING_USER_" + JSONObject.toJSONString(userParam));
                throw new ApiException("auth用户认证失败");
            }
            token = null;
            //钉钉已绑定
            if (ObjectUtil.isNotEmpty(dingUser)) {
                token = adminService.getOAuthToken(dingUser.getUsername()); //调用系统登录接口
            }
        }
        return token;
    }
}