package org.example.service;

import org.example.entity.Users;
import org.example.entity.Yonghu;
import org.example.mapper.UsersMapper;
import org.example.mapper.YonghuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录服务
 */
@Service
public class LoginService {

    @Autowired
    private YonghuMapper yonghuMapper;

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 用户登录
     */
    public Map<String, Object> userLogin(String username, String password) {
        Map<String, Object> result = new HashMap<>();
        Yonghu yonghu = yonghuMapper.findByUsernameAndPassword(username, password);
        if (yonghu != null) {
            result.put("code", 200);
            result.put("message", "登录成功");
            result.put("data", yonghu);
            result.put("userType", "user");
        } else {
            result.put("code", 500);
            result.put("message", "用户名或密码错误");
        }
        return result;
    }

    /**
     * 管理员登录
     */
    public Map<String, Object> adminLogin(String username, String password) {
        Map<String, Object> result = new HashMap<>();
        Users admin = usersMapper.findByUsernameAndPassword(username, password);
        if (admin != null) {
            result.put("code", 200);
            result.put("message", "登录成功");
            result.put("data", admin);
            result.put("userType", "admin");
        } else {
            result.put("code", 500);
            result.put("message", "管理员账号或密码错误");
        }
        return result;
    }
}
