package com.changhu.enums.handler;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.common.db.enums.IsEnable;
import com.changhu.common.db.enums.UserType;
import com.changhu.common.enums.ResultCode;
import com.changhu.common.exception.MessageException;
import com.changhu.common.pojo.vo.TokenInfo;
import com.changhu.common.utils.RsaUtil;
import com.changhu.common.utils.UserUtil;
import com.changhu.common.utils.ValidatorUtil;
import com.changhu.module.management.pojo.entity.ManagementSecurityUnitUser;
import com.changhu.module.management.pojo.entity.SecurityUnit;
import com.changhu.pojo.params.ManagementSecurityUnitLoginParams;
import com.changhu.support.mybatisplus.pojo.entity.BaseEntity;

/**
 * @author 20252
 * @createTime 2024/9/2 下午5:48
 * @desc ManagementSecurityUnitLogin...
 */
public class ManagementSecurityUnitLogin extends AbstractLoginHandler {

    public static final ManagementSecurityUnitLogin instance = new ManagementSecurityUnitLogin();

    private ManagementSecurityUnitLogin() {
    }

    @Override
    public TokenInfo login(JSONObject jsonObject) {
        ManagementSecurityUnitLoginParams loginParams = jsonObject.to(ManagementSecurityUnitLoginParams.class);
        ValidatorUtil.manual(loginParams);

        String accountOrTelephone = loginParams.getAccountOrTelephone();
        String password = RsaUtil.decrypt(loginParams.getPassword());

        //查看 账号/手机号 是否存在
        ManagementSecurityUnitUser managementSecurityUnitUser = Db.lambdaQuery(ManagementSecurityUnitUser.class)
                .eq(ManagementSecurityUnitUser::getAccount, accountOrTelephone)
                .or()
                .eq(ManagementSecurityUnitUser::getTelephone, accountOrTelephone)
                .oneOpt()
                .orElseThrow(() -> new MessageException(ResultCode.USER_NOT_FOUND));

        //判断用户是否禁用
        if (managementSecurityUnitUser.getIsEnable().equals(IsEnable.FALSE)) {
            throw new MessageException(ResultCode.USER_IS_DISABLE);
        }

        //判断单位是否禁用
        IsEnable unitEnable = Db.lambdaQuery(SecurityUnit.class)
                .eq(BaseEntity::getSnowFlakeId, managementSecurityUnitUser.getSecurityUnitId())
                .oneOpt()
                .map(SecurityUnit::getIsEnable)
                .orElseThrow(() -> new MessageException("单位不存在"));
        if (unitEnable.equals(IsEnable.FALSE)) {
            throw new MessageException("单位被禁用");
        }

        //判断密码是否正确
        if (!UserUtil.verifyPassWord(password, managementSecurityUnitUser.getSalt(), managementSecurityUnitUser.getPassword())) {
            throw new MessageException(ResultCode.PASSWORD_ERROR);
        }

        //登录
        SaTokenInfo saTokenInfo = UserUtil.loginAndTokenInfo(
                managementSecurityUnitUser.getSnowFlakeId(),
                UserType.MANAGEMENT_SECURITY,
                managementSecurityUnitUser.getSecurityUnitId()
        );
        //返回token
        return new TokenInfo(saTokenInfo.getTokenName(), saTokenInfo.getTokenValue());
    }
}
