package com.happy.dao.impl;

import com.happy.Model.Booking;
import com.happy.Model.weixin.Users;
import com.happy.Until.Func;
import com.happy.Until.UUIDUtil;
import com.happy.dao.UserDao;
import com.happy.vo.UnevaluatedOrder;
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.ArrayList;
import java.util.List;

@Repository("UserDao")
public class UserDaoImpl implements UserDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

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

    public int insert(Users users){
        String sql = "insert into `users`(id,card_number,user_name,user_phone,user_zz,openid,create_time,remark,identity_type,contact_id,contact_name,data_type,head_photo,collect_hotel,live_hotel)" +
                " values(:id,:card_number,:user_name,:user_phone,:user_zz,:openid,:create_time,:remark,:identity_type,:contact_id,:contact_name,:data_type,:head_photo,:collect_hotel,:live_hotel) ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("card_number", users.getCard_number());
        sps.addValue("user_name", users.getUser_name());
        sps.addValue("user_phone", users.getUser_phone());
        sps.addValue("user_zz", users.getUser_zz());
        sps.addValue("openid", users.getOpenid());
        sps.addValue("create_time", UUIDUtil.getNewDate());
        sps.addValue("remark", users.getRemark());
        sps.addValue("identity_type", users.getIdentity_type());
        sps.addValue("contact_id", users.getContactId());
        sps.addValue("contact_name", users.getContactName());
        sps.addValue("data_type", users.getDataType());
        sps.addValue("head_photo", users.getHeadPhoto());
        sps.addValue("collect_hotel", users.getCollect_hotel());
        sps.addValue("live_hotel", users.getLive_hotel());
        if(users.getId()==0){
            sps.addValue("id", UUIDUtil.generateID());
        }else{
            sps.addValue("id", users.getId());
        }
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    // 修改信息
    public int update(Users users){
        String sql = "update `users` set user_name=:user_name,user_phone=:user_phone,user_zz=:user_zz,remark=:remark, head_photo=:head_photo,card_number=:card_number,collect_hotel=:collect_hotel,live_hotel=:live_hotel" +
                "  where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", users.getId());
        sps.addValue("user_name", users.getUser_name());
        sps.addValue("user_phone", users.getUser_phone());
        sps.addValue("user_zz", users.getUser_zz());
        sps.addValue("remark", users.getRemark());
        sps.addValue("head_photo", users.getHeadPhoto());
        sps.addValue("card_number", users.getCard_number());
        sps.addValue("collect_hotel", users.getCollect_hotel());
        sps.addValue("live_hotel", users.getLive_hotel());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    // 修改信息
    public int updateUserByParam(Users users){
        StringBuffer stringBuffer =  new StringBuffer(" update `users` set  ");
        MapSqlParameterSource msp = new MapSqlParameterSource();
        // 将要修改的数据填充到查询语句中
        appendValue(users,stringBuffer,msp);

        stringBuffer.append("  where id=:id  ");
        msp.addValue("id", users.getId());
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(stringBuffer.toString(), msp);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    public void appendValue(Users users, StringBuffer stringBuffer,MapSqlParameterSource msp)
    {
        if (!Func.checkNull(users.getUser_name()))
        {
            stringBuffer.append(" user_name=:user_name, ");
            msp.addValue("user_name", users.getUser_name());
        }
        if (!Func.checkNull(users.getCard_number()))
        {
            stringBuffer.append(" card_number=:card_number, ");
            msp.addValue("card_number", users.getCard_number());
        }
        if (!Func.checkNull(users.getUser_phone()))
        {
            stringBuffer.append(" user_phone=:user_phone ,");
            msp.addValue("user_phone", users.getUser_phone());
        }
        if (!Func.checkNull(users.getUser_zz()))
        {
            stringBuffer.append(" user_zz=:user_zz ,");
            msp.addValue("user_zz", users.getUser_zz());
        }
        if (!Func.checkNull(users.getOpenid()))
        {
            stringBuffer.append(" openid=:openid ,");
            msp.addValue("openid", users.getOpenid());
        }
        if (!Func.checkNull(users.getRemark()))
        {
            stringBuffer.append(" remark=:remark ,");
            msp.addValue("remark", users.getRemark());
        }
        if (!Func.checkDblZero(users.getIdentity_type()))
        {
            stringBuffer.append(" identity_type=:identity_type ,");
            msp.addValue("identity_type", users.getIdentity_type());
        }
        if (!Func.checkNull(users.getContactId()))
        {
            stringBuffer.append(" contact_id=:contact_id ,");
            msp.addValue("contact_id", users.getContactId());
        }
        if (!Func.checkNull(users.getContactName()))
        {
            stringBuffer.append(" contact_id=:contact_id ,");
            msp.addValue("contact_id", users.getContactId());
        }
        if (!Func.checkNull(users.getDataType()))
        {
            stringBuffer.append(" data_type=:data_type ,");
            msp.addValue("data_type", users.getDataType());
        }
        if (!Func.checkNull(users.getHeadPhoto()))
        {
            stringBuffer.append(" head_photo=:head_photo ,");
            msp.addValue("head_photo", users.getHeadPhoto());
        }
        if (!Func.checkNull(users.getCollect_hotel()))
        {
            stringBuffer.append(" collect_hotel=:collect_hotel ,");
            msp.addValue("collect_hotel", users.getCollect_hotel());
        }
        if (!Func.checkNull(users.getLive_hotel()))
        {
            stringBuffer.append(" live_hotel=:live_hotel ,");
            msp.addValue("live_hotel", users.getLive_hotel());
        }

        stringBuffer.append(" remark=''");
    }


    public int del(int id){
        String sql = "delete from `users` 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;
    }

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

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

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

    public Users queryByUserId(String userId){
        String sql = "select * from users where id=:id ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", userId);
        List<Users> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(Users.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public int queryUserTotal(String sqlStr) {
        String sql = "select count(*) from `users` where 1=1 "+sqlStr;
        MapSqlParameterSource sps = new MapSqlParameterSource();
        return namedParameterJdbcTemplate.queryForInt(sql, sps);
    }

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

    @Override
    public UnevaluatedOrder collect(String hotelId, Integer createId) {
        String sql = "SELECT\n" +
                "\t( SELECT h.cover_img FROM hotel h WHERE h.id = :hotelId AND h.`status` = 1 ) as url,\n" +
                "\t( SELECT hname FROM hotel  WHERE id = :hotelId AND `status` = 1 ) as name,\n" +
                "\tCOUNT( * ) as count \n" +
                "FROM\n" +
                "\t`users` u \n" +
                "WHERE\n" +
                "\tu.id = :createId \n" +
                "\tAND FIND_IN_SET( :hotelId, u.collect_hotel )";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("hotelId", hotelId);
        sps.addValue("createId", createId);
        List<UnevaluatedOrder> list = namedParameterJdbcTemplate.query(sql, sps,
                new BeanPropertyRowMapper<>(UnevaluatedOrder.class));
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

}
