package com.icloud.server.payutils.goods;

import com.icloud.server.scala.engine.service.Goodspaye;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;

public class GoodsPayDaoImpl implements GoodsPayDao{
    @PersistenceContext
    private EntityManager em;
    Logger log = LoggerFactory.getLogger(GoodsPayDaoImpl.class);

    @Override
    public List<Goodspaye> getGoodsPaysByHour(DateTime now) {
        DateTime hour= now.withSecondOfMinute(0).withMillisOfSecond(0).withMinuteOfHour(0);
        DateTime nextHour = hour.plusHours(1);
        String curDay = hour.withMillisOfDay(0).toString("yyyy-MM-dd HH:mm:ss");
        String nextDay = hour.withMillisOfDay(0).plusDays(1).toString("yyyy-MM-dd HH:mm:ss");
        StringBuffer query = new StringBuffer();
        query.append(" select distinct p from Goodspaye p");
        query.append(" where starttime  <= '").append(now.toString("yyyy-MM-dd HH:mm:ss")).append("'");
        query.append(" and expire_endtime  > '").append(now.toString("yyyy-MM-dd HH:mm:ss")).append("'");
        log.info(query.toString());
        List<Goodspaye> Goodspaye = this.em.createQuery(query.toString(), Goodspaye.class).getResultList();
        List<Goodspaye> res = new ArrayList<Goodspaye>();
        for (Goodspaye gps : Goodspaye) {
                res.add(gps);
        }
        return res;
    }
}
