package com.happy.dao.impl;

import com.happy.Model.Booking;
import com.happy.Model.BookingComplaint;
import com.happy.Model.HotelCoupon;
import com.happy.Model.House;
import com.happy.Until.SqlUtil;
import com.happy.Until.UUIDUtil;
import com.happy.dao.BookingComplainDao;
import com.happy.dto.IPage;
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("BookingComplainDao")
public class BookingComplainImplDao implements BookingComplainDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

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

    private String selectSql = "select " +
            "u.user_phone as complainPhone,m.corpn_phone as hotelPhone,a.id,a.booking_id,a.hotel_id,a.house_id,a.title,a.content,a.create_id,a.create_date,a.modify_date,a.status,h.h_name as house_name,h2.hname as hotel_name" +
            " from booking_complaint a " +
            " left join house h on a.house_id = h.id " +
            " left join hotel h2 on a.hotel_id = h2.id "+
            " left join admin_manager m on h2.manager_id = m.id " +
            " left join users u on a.create_id = u.id " +
            " where a.status != 0 ";

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

    @Override
    public List<BookingComplaint> toComplainExcel(String sqlx) {
        SqlUtil.filterKeyword(sqlx);
        String sql = selectSql + sqlx + " and a.status != 0"  +" ORDER BY id DESC ";
        List<BookingComplaint> list = namedParameterJdbcTemplate.query(sql,
                new BeanPropertyRowMapper<>(BookingComplaint.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 ("+ selectSql +") a where status != 0 "+sqlx;
        MapSqlParameterSource sps = new MapSqlParameterSource();
        return namedParameterJdbcTemplate.queryForInt(sql, sps);
    }

    @Override
    public BookingComplaint getById(String id) {
        String sql = selectSql + " and a.id = :id";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id", id);
        List<BookingComplaint> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(BookingComplaint.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list.get(0);
        return null;
    }

    @Override
    public String insert(String bookingId, String hotelId, String houseId, String title, String content, String createId, String createDate, String modifyDate) {
        String sql = "insert into `booking_complaint` (id,booking_id,hotel_id,house_id,title,content,create_id,create_date,modify_date,status)" +
                " values (:id,:booking_id,:hotel_id,:house_id,:title,:content,:create_id,:create_date,:modify_date,:status)";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("booking_id",bookingId);
        sps.addValue("hotel_id",hotelId);
        sps.addValue("house_id",houseId);
        sps.addValue("title",title);
        sps.addValue("content",content);
        sps.addValue("create_id",createId);
        sps.addValue("create_date",createDate);
        sps.addValue("modify_date",modifyDate);
        sps.addValue("status",1);
        String id ="TS"+ UUIDUtil.getNewDate();
        sps.addValue("id", id);

        int num = 0;
        try {
            num = namedParameterJdbcTemplate.update(sql, sps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (num > 0) {
            return id;
        } else {
            id = num + "";
        }
        return id;

    }
}
