/ 維持 gmt 不變

func (r *StreamerProg) GetByNow(channelid string, now time.Time) *StreamerProg {

    //now := time.Now().UTC().Unix()

    //publish := (&dao.Streamer_program{}).GetByNow(channel_id, now)

    // now := time.Now().UTC()

    // publish := (&dao.StreamerProgram{}).GetByNow(channelid, now)

    items := r.GetAll(" WHERE channelid=? and starttime<=? ", "ORDER BY starttime DESC", 1, 0, channelid, now)

    //fmt.Println(items)

    if len(items) > 0 {

        if items[0].Endtime.Unix() >= now.Unix() {

            return items[0]

        }

    }

    return nil

}

func (r *StreamerProg) GetFirst(channelid string) *StreamerProg {

    items := r.GetAll(" WHERE channelid=?", "order by starttime ASC", 1, 0, channelid)

    //utils.Log(items)

    if len(items) > 0 {

        return items[0]

    }

    return nil

}

func (r *StreamerProg) ExistsByDay(channelid string, start time.Time) bool {

    items := r.GetAll(" WHERE channelid=? and starttime>=? and starttime<=? ", "order by starttime DESC", 1, 0, channelid, start, start.Add(time.Duration(24*time.Hour)))

    if items != nil {

        if items[0].Endtime.Unix() >= start.Unix()+int64(24*60*60) {

            return true

        }

    }

    return false

}

func (r *StreamerProg) GetByRange(channelid string, start time.Time, end time.Time) []*StreamerProg {

    items := r.GetAll(" WHERE channelid=? and starttime>=? and starttime<? ", "", 0, 0, channelid, start, end)

    //fmt.Println(items)

    if len(items) > 0 {

        return items

    }

    return nil

}

func (r *StreamerProg) GetByLastOneday(channelid string, now time.Time) []*StreamerProg {

    items := r.GetAll(" WHERE channelid=? ", "ORDER BY starttime DESC", 1, 0, channelid) //get last 1

    if len(items) > 0 {

        if items[0].Endtime.Unix() >= now.Unix() {

            return nil

        }

        base := items[0].Endtime.UTC()

        midnight := time.Date(base.Year(), base.Month(), base.Day(), 0, 0, 0, 0, time.UTC)

        prevday := midnight.Add(-24 * time.Hour)

        oneday := r.GetAll(" WHERE channelid=? and starttime>= ?  and starttime<? ", "ORDER BY starttime ASC", 0, 0, channelid, prevday, midnight)

        if oneday[0].Starttime.Unix()%(24*60*60) == 0 {

            return oneday

        } else {

            return nil

        }

    }

    return nil

}

===========================================

func ProgNow(c echo.Context) error {

    item := (&dao.StreamerProg{}).GetByNow("1gd3s2tnsbi43830KzC91TL4R1780c", time.Now())

    return c.JSON(http.StatusOK, item)

}

func ProgGetFirst(c echo.Context) error {

    //item := (&dao.StreamerProg{}).GetByNow("1gd3s2tnsbi43830KzC91TL4R1780c", time.Now())

    item := (&dao.StreamerProg{}).GetFirst("1gd3s2tnsbi43830KzC91TL4R1780c")

    return c.JSON(http.StatusOK, item)

}

func ProgGetByRange(c echo.Context) error {

    //item := (&dao.StreamerProg{}).GetByNow("1gd3s2tnsbi43830KzC91TL4R1780c", time.Now())

    //item := (&dao.StreamerProg{}).GetFirst("1gd3s2tnsbi43830KzC91TL4R1780c")

    //今天到明天

    now := time.Now().UTC()

    todayMidnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)

    nextday := todayMidnight.Add(24 * time.Hour)

    item := (&dao.StreamerProg{}).GetByRange("1gd3s2tnsbi43830KzC91TL4R1780c", todayMidnight, nextday)

    return c.JSON(http.StatusOK, item)

}