package com.happy.dao.impl;

import com.happy.Model.Admin;
import com.happy.Model.AdminManager;
import com.happy.dao.AppDao;
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("AppDao")
public class AppImplDao implements AppDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

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

    // 登录
    public Admin login(String admin_name,String password) {
        String sql = "select * from `admin` where admin_name=:admin_name and password=:password ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("admin_name",admin_name);
        sps.addValue("password",password);
        List<Admin> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Admin.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public Admin queryByOpenid(String openid) {
        String sql = "select * from `admin` where openid=:openid ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("openid",openid);
        List<Admin> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Admin.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public Admin queryByNameAndOpenid(String admin_name,String openid) {
        String sql = "select * from `admin` where admin_name=:admin_name and openid=:openid ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("admin_name",admin_name);
        sps.addValue("openid",openid);
        List<Admin> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Admin.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public int updateOpenid(String openid,String admin_name){
        String sql = "update `admin` set openid=:openid where admin_name=:admin_name ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("openid",openid);
        sps.addValue("admin_name",admin_name);
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    public int updateOpenidNull(String openid){
        String sql = "update `admin` set openid=null where openid=:openid ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("openid",openid);
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**********************
     * ********商户绑定**********
     * ********************/
    public AdminManager login_ma(String admin_name, String password) {
        String sql = "select * from `admin_manager` where admin_name=:admin_name and password=:password ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("admin_name",admin_name);
        sps.addValue("password",password);
        List<AdminManager> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(AdminManager.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public AdminManager queryMaByOpenid(String openid) {
        String sql = "select * from `admin_manager` where openid=:openid ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("openid",openid);
        List<AdminManager> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(AdminManager.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public AdminManager queryMaByNameAndOpenid(String admin_name,String openid) {
        String sql = "select * from `admin_manager` where admin_name=:admin_name and openid=:openid ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("admin_name",admin_name);
        sps.addValue("openid",openid);
        List<AdminManager> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(AdminManager.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public int updateMaOpenid(String openid,String admin_name){
        String sql = "update `admin_manager` set openid=:openid where admin_name=:admin_name ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("openid",openid);
        sps.addValue("admin_name",admin_name);
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    public int updateMaOpenidNull(String openid){
        String sql = "update `admin_manager` set openid=null where openid=:openid ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("openid",openid);
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

}
