//package org.jk.web.service;
//
//import cn.dev33.satoken.stp.StpUtil;
//import cn.dev33.satoken.stp.parameter.SaLoginParameter;
//import cn.hutool.crypto.digest.BCrypt;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.jk.common.core.config.JkConfig;
//import org.jk.common.core.constant.SystemConstants;
//import org.jk.common.core.domain.model.LoginUser;
//import org.jk.common.core.enums.LoginType;
//import org.jk.common.core.exception.user.UserException;
//import org.jk.common.core.utils.DateUtils;
//import org.jk.common.core.utils.ServletUtils;
//import org.jk.common.satoken.utils.LoginHelper;
//import org.jk.common.tenant.helper.TenantHelper;
//import org.jk.system.domain.SysUser;
//import org.jk.system.domain.vo.SysClientVo;
//import org.jk.system.domain.vo.SysUserVo;
////import org.jk.rag.service.IJkApiService;
//import org.jk.system.service.ISysClientService;
//import org.jk.system.service.ISysConfigService;
//import org.jk.system.service.ISysUserService;
//import org.jk.web.domain.vo.LoginVo;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.io.IOException;
//import java.util.Map;
//
///**
// * 建科登录校验方法
// *
// * @author takai
// */
//@Slf4j
//@Service
//@RequiredArgsConstructor
//public class JkLoginService
//{
//    private final ISysUserService userService;
//
//    private final ISysConfigService configService;
//
//    //private final IJkApiService jkApiService;
//
//    private final SysLoginService loginService;
//
//    private final ISysClientService clientService;
//
//    @Autowired
//    JkConfig jkConfig;
//
//
//    /**
//     * 记录登录信息
//     *
//     * @param userId 用户ID
//     */
//    public void recordLoginInfo(String userId)
//    {
//        SysUser sysUser = new SysUser();
//        sysUser.setUserId(Long.parseLong(userId));
//        sysUser.setLoginIp(ServletUtils.getClientIP());
//        sysUser.setLoginDate(DateUtils.getNowDate());
//        userService.updateUserProfile(sysUser);
//    }
//
//
//    public LoginVo login(String code,String redirectUrl) throws IOException {
//        log.info("登录code:{}", code);
//        log.info("登录redirectUrl:{}", redirectUrl);
//        String username;
//        // 用户验证
//        Map<String, Object> resJson = jkApiService.getJkToken(code,redirectUrl);
//        username = String.valueOf(resJson.get("userId"));
//        SysUserVo sysUserVo = userService.selectUserByUserName(username);
//        SysClientVo client = clientService.queryByClientId("e5cd7e4891bf95d1d19206ce24a7b32e");
//        String password = sysUserVo.getPassword();
//        LoginUser loginUser = TenantHelper.dynamic(sysUserVo.getTenantId(), () -> {
//            if (SystemConstants.DISABLE.equals(sysUserVo.getStatus())) {
//                log.info("登录用户：{} 已被停用.", username);
//                throw new UserException("user.blocked", username);
//            }
//            loginService.checkLogin(LoginType.PASSWORD, sysUserVo.getTenantId(), username, () -> !BCrypt.checkpw(password, password));
//            // 此处可根据登录用户的数据不同 自行创建 loginUser
//            return loginService.buildLoginUser(sysUserVo);
//        });
//        loginUser.setClientKey(client.getClientKey());
//        loginUser.setDeviceType(client.getDeviceType());
//        SaLoginParameter model = new SaLoginParameter();
//        model.setDeviceType(client.getDeviceType());
//        // 自定义分配 不同用户体系 不同 token 授权时间 不设置默认走全局 yml 配置
//        // 例如: 后台用户30分钟过期 app用户1天过期
//        model.setTimeout(client.getTimeout());
//        model.setActiveTimeout(client.getActiveTimeout());
//        model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId());
//        // 生成token
//        LoginHelper.login(loginUser, model);
//
//        LoginVo loginVo = new LoginVo();
//        loginVo.setAccessToken(StpUtil.getTokenValue());
//        loginVo.setExpireIn(StpUtil.getTokenTimeout());
//        loginVo.setClientId(client.getClientId());
//        return loginVo;
//    }
//
//}
