package com.hzs.system.login.service.impl;

import com.hzs.common.core.constant.msg.SystemMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EStatus;
import com.hzs.common.domain.system.sys.SysLogininfor;
import com.hzs.common.domain.system.sys.SysUser;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.login.service.IUserLoginService;
import com.hzs.system.sys.ILogServiceApi;
import com.hzs.system.sys.IUserServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import com.hzs.common.core.constant.Constants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EUserStatus;
import com.hzs.common.core.exception.ServiceException;
import com.hzs.common.core.utils.ServletUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.ip.IpUtils;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.dto.LoginUser;
/**
 * 登录校验方法
 *
 * @author hzs
 */
@Component
public class UserLoginServiceImpl implements IUserLoginService {

    @DubboReference
    ILogServiceApi iLogServiceApi;
    @DubboReference
    IUserServiceApi iUserServiceApi;

    /**
     * 登录
     */
    @Override
    public LoginUser login(String username, String password) {
        if (StringUtils.isAnyBlank(username, password)) {
            // 用户/密码必须填写
            throw new ServiceException(TransactionUtils.getContent(SystemMsgConstants.USER_REQUIRED));
        }

        // 查询用户信息
        R<LoginUser> userResult = iUserServiceApi.getUserInfo(username);

        if (R.FAIL == userResult.getCode()) {
            throw new ServiceException(userResult.getMsg());
        }

        if (StringUtils.isNull(userResult.getData())) {
            recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
            // 登录用户: %s 不存在
            throw new ServiceException(TransactionUtils.getContent(SystemMsgConstants.USER_NOT_EXIST, username));
        }
        LoginUser loginUser = userResult.getData();
        SysUser user = userResult.getData().getLoginSysUser();
        if (EDelFlag.DELETE.getValue() == user.getDelFlag()) {
            recordLogininfor(username, Constants.LOGIN_FAIL, "您的账号已被删除");
            // 对不起，您的账号: %s 已被删除
            throw new ServiceException(TransactionUtils.getContent(SystemMsgConstants.ACCCOUNT_DELETE, username));
        }
        if (EUserStatus.DISABLE.getValue() == user.getStatus()) {
            recordLogininfor(username, Constants.LOGIN_FAIL, "您的账号已停用");
            // 对不起，您的账号: %s 已停用
            throw new ServiceException(TransactionUtils.getContent(SystemMsgConstants.ACCCOUNT_STOP, username));
        }
        if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
            recordLogininfor(username, Constants.LOGIN_FAIL, "用户不存在或密码错误");
            // 用户不存在/密码错误
            throw new ServiceException(TransactionUtils.getContent(SystemMsgConstants.USER_ERROR, username));
        }
        recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
        return loginUser;
    }


    @Override
    public LoginUser freeLogin(String userName) {
        // 查询用户信息
        R<LoginUser> userResult = iUserServiceApi.getUserInfo(userName);

        if (R.FAIL == userResult.getCode()) {
            throw new ServiceException(userResult.getMsg());
        }

        if (StringUtils.isNull(userResult.getData())) {
            recordLogininfor(userName, Constants.LOGIN_FAIL, "免密登录用户不存在");
            // 登录用户: %s 不存在
            throw new ServiceException(TransactionUtils.getContent(SystemMsgConstants.USER_NOT_EXIST, userName));
        }
        LoginUser loginUser = userResult.getData();
        SysUser user = userResult.getData().getLoginSysUser();

        if (EDelFlag.UN_DELETE.getValue() != user.getDelFlag()) {
            recordLogininfor(userName, Constants.LOGIN_FAIL, "您的账号已被删除");
            // 对不起，您的账号: %s 已被删除
            throw new ServiceException(TransactionUtils.getContent(SystemMsgConstants.ACCCOUNT_DELETE, userName));
        }
        if (EUserStatus.DISABLE.getValue() == user.getStatus()) {
            recordLogininfor(userName, Constants.LOGIN_FAIL, "您的账号已停用");
            // 对不起，您的账号: %s 已停用
            throw new ServiceException(TransactionUtils.getContent(SystemMsgConstants.ACCCOUNT_STOP, userName));
        }

        recordLogininfor(userName, Constants.LOGIN_SUCCESS, "免密登录成功");
        return loginUser;
    }


    /**
     * 登出
     */
    @Override
    public void logout(String loginName) {
        recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
    }

    /**
     * 记录登录信息
     *
     * @param username 用户名
     * @param status   状态
     * @param message  消息内容
     * @return
     */
    private void recordLogininfor(String username, String status, String message) {
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setUserName(username);
        logininfor.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
        logininfor.setMsg(message);
        // 日志状态
        if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
            logininfor.setStatus(EStatus.YES.getValue());
        } else if (Constants.LOGIN_FAIL.equals(status)) {
            logininfor.setStatus(EStatus.NO.getValue());
        }
        iLogServiceApi.saveLogininfor(logininfor);
    }

}

