package com.madeu.common.dao;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.madeu.util.Snake2Camel;

import jakarta.annotation.PostConstruct;

@Repository
public class CommonDao extends SqlSessionDaoSupport{
	
	@Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @PostConstruct
    void init() {
        setSqlSessionTemplate(sqlSessionTemplate);
    }
    
    @Autowired
    private Snake2Camel s2c;
    
    /** 단건 조회: count를 반환 */
    public int selectCount(String sqlId, Object param) {
        return getSqlSession().selectOne(sqlId, param);
    }
    
    /** 단건 조회: 결과를 HashMap으로 반환 */
    @SuppressWarnings("static-access")
	public Map<String, Object> select(String sqlId, Object param) {
    	Map<String, Object> map = getSqlSession().selectOne(sqlId, param);
    	return s2c.convertKeysToCamelCase(map);
    }

    /** 다건 조회: 결과를 ArrayList<HashMap> 형태로 반환 */
    public List<Map<String, Object>> selectList(String sqlId, Object param) {
        List<Map<String, Object>> list = getSqlSession().selectList(sqlId, param);
        list = list.stream().map(Snake2Camel::convertKeysToCamelCase).collect(Collectors.toList());
    	return list;
    }

    /** insert 수행 후 영향건수 반환 */
    public int insert(String sqlId, Object param) {
        return getSqlSession().insert(sqlId, param);
    }

    /** update 수행 후 영향건수 반환 */
    public int update(String sqlId, Object param) {
        return getSqlSession().update(sqlId, param);
    }

    /** delete 수행 후 영향건수 반환 */
    public int delete(String sqlId, Object param) {
        return getSqlSession().delete(sqlId, param);
    }

}
