package com.zksy.framework.security.moblie;

import com.zksy.common.core.domain.entity.SysUser;
import com.zksy.common.core.domain.model.LoginUser;
import com.zksy.common.enums.UserStatus;
import com.zksy.common.exception.ServiceException;
import com.zksy.common.utils.StringUtils;
import com.zksy.framework.web.service.SysPermissionService;
import com.zksy.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author ：BIN.LI
 * @date ：2022/4/27 10:36
 * @description：
 */
@Component
@Slf4j
public class UserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    private SysPermissionService permissionService;

    LoginUser findUserByMobile(SysUser mobile) {

        SysUser user = sysUserMapper.selectUserByMobile(mobile.getPhonenumber());
        if (StringUtils.isNull(user)) {
            log.info("登录用户手机：{} 不存在.", mobile.getPhonenumber());
            throw new ServiceException("登录用户：" + mobile.getPhonenumber() + " 不存在");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            log.info("登录用户手机：{} 已被删除.", mobile.getPhonenumber());
            throw new ServiceException("对不起，您的账号：" + mobile.getPhonenumber() + " 已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            log.info("登录用户手机：{} 已被停用.", mobile.getPhonenumber());
            throw new ServiceException("对不起，您的账号：" + mobile.getPhonenumber() + " 已停用");
        }
        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
    }
}
