package com.roma.romaapi.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

@Repository
public class ApiDao {
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    // 通过code,获取要执行的sql
    public Map<String, Object> adminInfoQueryForMap(String adminName) {
        try {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("admin_name", adminName);
            Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(
                    "SELECT * FROM `admin_user` where admin_name = :admin_name AND is_enable=1", paramMap);
            return  resMap2;
        } catch (EmptyResultDataAccessException e) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("sysErrorCode", "500");

            return resultMap;
        }
    }

    // 通过用户id,获取用户信息
    public Map<String, Object> userInfoByIdQueryForMap(String userID) {
        try {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("admin_id", userID);
            Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(
                    "SELECT * FROM `admin_user` where id = :admin_id AND is_enable=1", paramMap);
            return  resMap2;
        } catch (EmptyResultDataAccessException e) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("sysErrorCode", "500");

            return resultMap;
        }
    }
}
