package com.happy.dao.impl;

import com.happy.Model.Admin;
import com.happy.Model.Config;
import com.happy.dao.ConfigDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("ConfigDao")
public class ConfigImplDao implements ConfigDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

    public void setNamedParameterJdbcTemplate(
            NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    // 修改配置
    public int update(Config config){
        String sql = "update `config` set free_cancel=:free_cancel,stop_book=:stop_book where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("free_cancel", config.getFree_cancel());
        sps.addValue("stop_book", config.getStop_book());
        sps.addValue("id",config.getId());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    // 查看配置
    public Config queryConfig(){
        String sql = "select * from `config` where id=1 ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        List<Config> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Config.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
