package com.happy.dao.impl;

import com.happy.Until.UUIDUtil;
import com.happy.dao.HotelCoupomStatusDao;
import org.springframework.beans.factory.annotation.Autowired;
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;

@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 sql = "SELECT `id` FROM `hotel_coupon_status` where DATE_FORMAT(`lapse_date`, '%Y-%m-%d') < CURDATE() and `status` = 1";
        int num = 0;
        try{
            List<String> ids = namedParameterJdbcTemplate.queryForList(sql, EmptySqlParameterSource.INSTANCE, 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;
    }
}
