package com.hdkj.lt.si.service.auth.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hdkj.lt.core.sys.model.po.SysUser;
import com.hdkj.lt.core.sys.service.ISysUserService;
import com.hdkj.lt.si.service.auth.IAuthenticationService;
import com.hdkj.lt.utils.security.JwtUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: wenhao
 * @Create: 2024/03/22 11:23
 * @Version: 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AuthenticationServiceImpl implements IAuthenticationService {

    private final ISysUserService sysUserService;

    @Value("${lt.jwt.config.ttl:1800000}")
    private long ttl; //token过期时间，默认半个小时

    @Override
    public String token(String userAccount, String password) {
        SysUser sysUser = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserAccount, userAccount).eq(SysUser::getPassword, password));
        Assert.notNull(sysUser, "用户不存在！");
        Map<String, Object> claims = MapUtil.builder(new HashMap<String, Object>()).put("id", sysUser.getUserId()).build();
        return JwtUtils.createJwtToken(claims, ttl);
    }

}
