package com.huimv.admin.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.system.entity.Account;
import com.huimv.admin.system.entity.SysAccountMultilevel;
import com.huimv.admin.system.mapper.AccountMapper;
import com.huimv.admin.system.mapper.SysAccountMultilevelMapper;
import com.huimv.admin.system.service.ILoginService;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Result login(String accountName, String password) {
        //Step1:判断账户状态
        //Step2:读取菜单组
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.eq("account_name",accountName).eq("password",password);
        Account accountEntity = accountMapper.selectOne(accountQueryWrapper);
        if (accountEntity == null) {
//            throw new PlatformException("The account or password is wrong.", "该账号不存在或密码错误.");
            return new Result(10002, "账号不存在或密码错误.", false);
        }
        int status = accountEntity.getAccountStatus();
        if (status == 0) {
            return new Result(10003, "该账号未启用.", false);
        }
        if (status == -1) {
            return new Result(10004, "该账号已暂停使用.", false);
        }
        int accountId = accountEntity.getId();
        System.out.println("accountId>>"+accountId);
        return new Result(ResultCode.SUCCESS,accountEntity);
    }

    @Override
    public Result logout() {
        return null;
    }

    @Autowired
    private SysAccountMultilevelMapper accountMultilevelMapper;

    @Override
    public Result loginMultilevel(String accountName, String password) {

        QueryWrapper<SysAccountMultilevel> wrapper = new QueryWrapper<>();
        wrapper.eq("account",accountName).eq("password",password);
        SysAccountMultilevel accountMultilevel = accountMultilevelMapper.selectOne(wrapper);

        if (ObjectUtil.isEmpty(accountMultilevel)){
            return new Result(10001, "账号不存在或密码错误.", false);
        }


        if (accountMultilevel.getAccountStatus() == 0){
            return new Result(10002, "账号未启用.", false);
        }
        return new Result(ResultCode.SUCCESS,accountMultilevel);
    }
}
