package com.happy.dao.impl;

import com.happy.Model.HotelCoupon;
import com.happy.Model.HotelCouponStatus;
import com.happy.Until.UUIDUtil;
import com.happy.dao.HotelCoupomStatusDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.EmptySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Repository("HotelCoupomStatusDao")
public class HotelCoupomStatusImplDao implements HotelCoupomStatusDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

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

    @Override
    public int updateLapse(List<String> id) {
        StringBuffer stringBuffer =  new StringBuffer(" update `hotel_coupon_status` set  ");
        MapSqlParameterSource sps = new MapSqlParameterSource();
        stringBuffer.append(" status = 3 ");
        stringBuffer.append(", modify_date=:modify_date ");
        sps.addValue("modify_date", UUIDUtil.getNewDate());
        stringBuffer.append("  where complaint_id in (:id)  ");
        sps.addValue("id", id);
        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(stringBuffer.toString(), sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public int updateExpir(String dateTime) {
        String sql = "SELECT `id` FROM `hotel_coupon_status` where `lapse_date` < :now and `status` = 1";
        int num = 0;
        try{
            MapSqlParameterSource sps2 = new MapSqlParameterSource();
            sps2.addValue("now",dateTime);
            List<String> ids = namedParameterJdbcTemplate.queryForList(sql, sps2, String.class);
            if (ids.isEmpty()) return 0;
            //修改
            StringBuffer stringBuffer =  new StringBuffer(" update `hotel_coupon_status` set  ");
            MapSqlParameterSource sps = new MapSqlParameterSource();
            stringBuffer.append(" status = 4 ");
            stringBuffer.append(", modify_date=:modify_date ");
            sps.addValue("modify_date", UUIDUtil.getNewDate());
            stringBuffer.append("  where  id in (:id)");
            sps.addValue("id", ids);
            num = namedParameterJdbcTemplate.update(stringBuffer.toString(), sps);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }


    @Override
    public int insert(HotelCouponStatus hotelCouponStatus) {
        String sql="insert into hotel_coupon_status (id,complaint_id,user_id,create_id,create_date,modify_date,lapse_date,status) values (:id,:complaint_id,:user_id,:create_id,:create_date,:modify_date,:lapse_date,:status)";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("complaint_id", hotelCouponStatus.getComplaintId());
        sps.addValue("user_id",hotelCouponStatus.getUserId());
        sps.addValue("create_id", hotelCouponStatus.getCreateId());
        sps.addValue("create_date", hotelCouponStatus.getCreateDate());
        sps.addValue("modify_date", hotelCouponStatus.getModifyDate());
        sps.addValue("lapse_date", hotelCouponStatus.getLapseDate());
        sps.addValue("status", 1);
        String id = "";
        if (hotelCouponStatus.getId() == null) {
            id = UUID.randomUUID() + "";
            sps.addValue("id", id);
        } else {
            id = hotelCouponStatus.getId() + "";
            sps.addValue("id", id);
        }
        int num = 0;
        try {
            num = namedParameterJdbcTemplate.update(sql, sps);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return num;
    }

    @Override
    public HotelCouponStatus getById(String id) {
        String sql = "select * from hotel_coupon_status where id = :id and status=1";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id",id);
        List<HotelCouponStatus> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(HotelCouponStatus.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list.get(0);
        return null;
    }

    @Override
    public int update(HotelCouponStatus hotelCouponStatus) {
        String sql="update hotel_coupon_status SET complaint_id=:complaint_id,booking_id=:booking_id,discount_amount=:discount_amount,user_id=:user_id,create_id=:create_id,create_date=:create_date,modify_date=:modify_date,lapse_date=:lapse_date,status=:status where id=:id";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("id",hotelCouponStatus.getId());
        sps.addValue("complaint_id",hotelCouponStatus.getComplaintId());
        sps.addValue("booking_id",hotelCouponStatus.getBookingId());
        sps.addValue("discount_amount",hotelCouponStatus.getDiscountAmount());
        sps.addValue("user_id",hotelCouponStatus.getUserId());
        sps.addValue("create_id",hotelCouponStatus.getCreateId());
        sps.addValue("create_date",hotelCouponStatus.getCreateDate());
        sps.addValue("modify_date",hotelCouponStatus.getModifyDate());
        sps.addValue("lapse_date",hotelCouponStatus.getLapseDate());
        sps.addValue("status",hotelCouponStatus.getStatus());

        int num = 0;
        try{
            num = namedParameterJdbcTemplate.update(sql, sps);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return num;

    }

    @Override
    public List<HotelCouponStatus> getBookingId(String bookingId) {
        String sql="select * from hotel_coupon_status where booking_id=:bookingId  ";
        MapSqlParameterSource sps = new MapSqlParameterSource();
        sps.addValue("bookingId",bookingId);
        List<HotelCouponStatus> list = null;
        try{
            list = namedParameterJdbcTemplate.query(sql, sps,
                    new BeanPropertyRowMapper<>(HotelCouponStatus.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        if(list != null && list.size()>0) return list;
        return null;
    }
}
