package com.yingpaipay.web.service.strategy;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.stp.parameter.SaLoginParameter;
import cn.hutool.core.lang.Dict;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yingpaipay.system.constant.UserAppletStatusConst;
import com.yingpaipay.web.domain.bo.AppletLoginBo;
import com.yingpaipay.web.domain.vo.AppletLoginVo;
import com.yingpaipay.web.service.IAppletAuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.BusinessException;
import org.dromara.common.core.utils.MessageUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.vo.SysClientVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.system.service.ISysClientService;
import org.dromara.web.service.SysLoginService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Huanyi
 * @CreateTime: 2025-12-04
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class PasswordAppletAuthService implements IAppletAuthService {

    private final SysUserMapper userMapper;

    private final SysLoginService loginService;
    private final ISysClientService clientService;

    @Override
    public AppletLoginVo login(AppletLoginBo bo) {
        Dict map = JsonUtils.parseMap(bo.getContent());
        List<SysUserVo> userList = userMapper.selectVoList(
            Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getAppletStatus, UserAppletStatusConst.NORMAL)
                .eq(SysUser::getPhonenumber, map.getStr("phoneNumber"))
        );
        if (userList.isEmpty()) {
            throw new BusinessException(MessageUtils.message("applet.auth.login.notexists"));
        }
        for (SysUserVo sysUser : userList) {
            if (BCrypt.checkpw(map.getStr("password"), sysUser.getPassword())) {
                LoginUser loginUser = loginService.buildLoginUser(sysUser);
                SysClientVo client = clientService.queryByClientId(bo.getClientId());
                loginUser.setClientKey(client.getClientKey());
                loginUser.setDeviceType(client.getDeviceType());
                SaLoginParameter parameter = new SaLoginParameter();
                parameter.setTimeout(client.getTimeout());
                parameter.setDeviceType(client.getDeviceType());
                parameter.setActiveTimeout(client.getActiveTimeout());
                parameter.setExtra(LoginHelper.CLIENT_KEY, client.getClientId());
                LoginHelper.login(loginUser, parameter);

                AppletLoginVo vo = new AppletLoginVo();
                vo.setToken(StpUtil.getTokenValue());
                return vo;
            }
        }
        throw new BusinessException(MessageUtils.message("applet.auth.login.passworderror"));
    }
}
