package com.huimv.receive.service.impl;

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

@Service
public class LoginServiceImpl implements ILoginService {


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

    @Autowired
    private SysAccountMultilevelMapper accountMultilevelMapper;

    @Autowired
    private BaseJobMapper baseJobMapper;

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

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

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

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

        accountMultilevel.setJobName( baseJobMapper.selectById(accountMultilevel.getType()).getJobName());
        return new Result(ResultCode.SUCCESS,accountMultilevel);
    }

    @Override
    public Result loginMultilevelClient(String accountName, String password) {
        QueryWrapper<SysAccountMultilevel> wrapper = new QueryWrapper<>();
        wrapper.eq("account",accountName).eq("password",password).ne("type",7);
        SysAccountMultilevel accountMultilevel = accountMultilevelMapper.selectOne(wrapper);

        if (ObjectUtil.isEmpty(accountMultilevel)){
            return new Result(10001, "账号不存在或密码错误.", false);
        }
        if (accountMultilevel.getType()!=0){
            return new Result(10001, "用户类型不匹配.", false);
        }

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