package com.happy.dao.impl;

import com.happy.Model.Admin;
import com.happy.Model.AdminPower;
import com.happy.Model.weixin.Users;
import com.happy.Until.Func;
import com.happy.Until.SqlUtil;
import com.happy.Until.UUIDUtil;
import com.happy.dao.AdminDao;
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("AdminDao")
public class AdminImplDao implements AdminDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

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

    public String selectSql="select " +
            "id,admin_name,user_name,phone,level,admin_power,remark,create_id, DATE_FORMAT(create_date,'%Y-%m-%d %T') create_date,DATE_FORMAT(modify_date,'%Y-%m-%d %T') modify_date,status,openid" +
            " from `admin` ";

    @Override
    public int insertAdmin(Admin admin){
        String sql = "insert into admin(id,admin_name,user_name,phone,password,level,admin_power,remark, create_id, create_date, status ) values(:id,:admin_name,:user_name,:phone,:password,:level,:admin_power,:remark, :create_id, :create_date, :status) ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("admin_name", admin.getAdminName());
        sps.addValue("user_name",admin.getUserName());
        sps.addValue("phone", admin.getPhone());
        sps.addValue("password", admin.getPassword());
        sps.addValue("level", admin.getLevel());
        sps.addValue("admin_power",admin.getAdminPower());
        sps.addValue("remark", admin.getRemark());
        sps.addValue("create_id", admin.getCreateId());
        sps.addValue("create_date",admin.getCreateDate());
        sps.addValue("status", 1);
        if(admin.getId()==null){
            sps.addValue("id", UUIDUtil.generateID());
        }else{
            sps.addValue("id", admin.getId());
        }
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public int updateAdmin(Admin admin){
        StringBuffer stringBuffer =  new StringBuffer(" update `admin` set  ");
        MapSqlParameterSource sps = new MapSqlParameterSource();
        // 将要修改的数据填充到查询语句中
        appendValue(admin,stringBuffer,sps);
        stringBuffer.append("  where id=:id  ");
        sps.addValue("id", admin.getId());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(stringBuffer.toString(), sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

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

    @Override
    public Admin getById(int id) {
        String sql = selectSql + " where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        List<Admin> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Admin.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<Admin> queryPage(String sqlx, int page, int rows) {
        SqlUtil.filterKeyword(sqlx);
        int start = (page - 1) * rows;// 每页的起始下标
        String sql = selectSql + "where status=1 "+sqlx+" order by id desc limit :start,:rows ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("start", start);
        sps.addValue("rows", rows);
        List<Admin> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Admin.class));
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public int queryTotal(String sqlx) {
        SqlUtil.filterKeyword(sqlx);
        String sql = "select count(*) from `admin` where status=1 "+sqlx;
        MapSqlParameterSource sps = new MapSqlParameterSource();
        return namedParameterJdbcTemplate.queryForInt(sql, sps);
    }

    @Override
    public List<Admin> queryList(String sqlx) {
        SqlUtil.filterKeyword(sqlx);
        String sql = selectSql + " where status=1 "+sqlx;
        MapSqlParameterSource sps = new MapSqlParameterSource();
        List<Admin> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Admin.class));
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public List<AdminPower> queryTree(String sqlx){
        SqlUtil.filterKeyword(sqlx);
        String sql = "select * from `admin_power` where status=1 "+sqlx;
        MapSqlParameterSource sps = new MapSqlParameterSource();
        List<AdminPower> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(AdminPower.class));
        if (list != null && list.size() > 0) {
            return list;
        }
        return null;
    }

    public void appendValue(Admin admin, StringBuffer stringBuffer, MapSqlParameterSource sps){
        if (!Func.checkNull(admin.getAdminName())){
            stringBuffer.append(" user_name=:user_name ,");
            sps.addValue("user_name", admin.getAdminName());
        }
        if (!Func.checkNull(admin.getPassword())){
            stringBuffer.append(" password=:password ,");
            sps.addValue("password", admin.getPassword());
        }
        if (!Func.checkNull(admin.getUserName())){
            stringBuffer.append(" user_name=:user_name ,");
            sps.addValue("user_name", admin.getUserName());
        }
        if (!Func.checkNull(admin.getPhone())){
            stringBuffer.append(" phone=:phone ,");
            sps.addValue("phone", admin.getPhone());
        }
        if (!Func.checkNull(String.valueOf(admin.getLevel()))){
            stringBuffer.append(" level=:level ,");
            sps.addValue("level", admin.getLevel());
        }
        if (!Func.checkNull(admin.getAdminPower())){
            stringBuffer.append(" admin_power=:admin_power ,");
            sps.addValue("admin_power", admin.getAdminPower());
        }
        if (!Func.checkNull(admin.getRemark())){
            stringBuffer.append(" remark=:remark ,");
            sps.addValue("remark", admin.getRemark());
        }
        if (!Func.checkNull(String.valueOf(admin.getStatus()))){
            stringBuffer.append(" status=:status ,");
            sps.addValue("status", admin.getStatus());
        }
        stringBuffer.append(" modify_date=:modify_date ");
        sps.addValue("modify_date", UUIDUtil.getNewDate());
    }
}
