package com.bonus.auth.service;

import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.domain.R;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.config.SystemConfig;
import com.bonus.system.api.RemoteUserService;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.api.model.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author bonus
 */
@Service
public class KitchenPhonePasswordLoginStrategy implements LoginStrategy {

    @Resource
    private SystemConfig systemConfig;

    @Resource
    private RemoteUserService remoteUserService;

    @Resource
    private PasswordValidatorService passwordValidatorService;

    @Resource
    private SysPasswordService passwordService;

    @Override
    public LoginUser login(String phone, String password) {
        if (!systemConfig.getLoginConfig().isPhonePassword()) {
            throw new ServiceException("用户不存在/密码错误");
        }
        //通过手机号获取用户信息
        R<LoginUser> userResult = remoteUserService.getKitchenStaffInfoByPhone(phone, SecurityConstants.INNER);
        //验证用户是否存在
        passwordValidatorService.validateUserResult(phone, userResult);
        //获取用户信息
        LoginUser userInfo = userResult.getData();
        SysUser user = userInfo.getSysUser();
        //校验用户审批状态
        //passwordValidatorService.validateApprovalStatus(user.getUserName(), user);
        // 处理IP校验
        //passwordValidatorService.validateIpBlacklist(user.getUserName());
        // 验证密码
        passwordService.validate(user, password, System.currentTimeMillis());
        //校验用户启用状态
        //passwordValidatorService.validateUserStatus(user.getUserName(), user);

        //passwordValidatorService.processLoginBlackList(user);
        //返回信息
        return userInfo;
    }
}


