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.ManagementPoliceUnitUser;
import com.changhu.module.management.pojo.entity.PoliceUnit;
import com.changhu.pojo.params.ManagementPoliceUnitLoginParams;
import com.changhu.support.mybatisplus.pojo.entity.BaseEntity;

/**
 * @author 20252
 * @createTime 2024/9/2 下午4:52
 * @desc ManagementPoliceUnitLogin...
 */
public class ManagementPoliceUnitLogin extends AbstractLoginHandler {

    public static final ManagementPoliceUnitLogin instance = new ManagementPoliceUnitLogin();

    private ManagementPoliceUnitLogin() {
    }


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

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

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

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

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


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

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