package com.happy.dao.impl;

import com.happy.Model.BookingComment;
import com.happy.Until.UUIDUtil;
import com.happy.dao.BookingCommentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("BookingCommentDao")
public class BookingCommentDaoImpl implements BookingCommentDao {
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

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

    /**
     * 添加评价，并返回主键
     * @param bookingComment
     * @return
     */
    @Override
    public String insertBookingComment(BookingComment bookingComment) {
        String sql = "insert into `booking_comment`(id,comment_id,booking_id,hotel_id,house_id,content,comment_status,score, score_ws,score_fw,score_ss,score_wz,create_id,create_username,create_date,modify_date,status) values(:id,:comment_id,:booking_id,:hotel_id,:house_id,:content,:comment_status,:score,:score_ws,:score_fw,:score_ss,:score_wz,:create_id,:create_username,:create_date,:modify_date,:status) ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("comment_id", bookingComment.getCommentId());
        sps.addValue("booking_id",bookingComment.getBookingId());
        sps.addValue("hotel_id", bookingComment.getHotelId());
        sps.addValue("house_id", bookingComment.getHouseId());
        sps.addValue("content", bookingComment.getContent());
        sps.addValue("comment_status",bookingComment.getCommentStatus());
        sps.addValue("score", bookingComment.getScore());
        sps.addValue("score_ws", bookingComment.getScoreWs());
        sps.addValue("score_fw", bookingComment.getScoreFw());
        sps.addValue("score_ss", bookingComment.getScoreSs());
        sps.addValue("score_wz", bookingComment.getScoreWz());
        sps.addValue("create_id", bookingComment.getCreateId());
        sps.addValue("create_username",bookingComment.getCreateUsername() );
        sps.addValue("create_date",bookingComment.getCreateDate());
        sps.addValue("modify_date",bookingComment.getModifyDate());
        sps.addValue("status", 1);

        String id ="";
        if(bookingComment.getId()==null){
            id = UUIDUtil.generateID()+"";
            sps.addValue("id", id);
        }else{
            id=bookingComment.getId()+"";
            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;
    }



}
