Ejemplo n.º 1
0
func HandleIndex(c wine.Context) {
	date := c.Params().GetStr("date")
	_, e := time.Parse("2006-01-02", date)
	if e != nil {
		date = time.Now().In(time.FixedZone("", 8*3600)).Format("2006-01-02")
	}

	t, _ := time.Parse("2006-01-02", date)
	previousDate := t.Add(-24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02")
	nextDate := t.Add(24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02")
	params := gox.M{}
	params["previous_date"] = previousDate
	params["next_date"] = nextDate
	params["date"] = date
	params["user_start_date"] = t.Add(-10 * 24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02")
	params["user_end_date"] = date
	params["title"] = date + "(" + DaysOfWeek[t.Weekday()] + ")"

	//log.Println(params)
	resp, _ := http.Get("http://localhost:8001/daily-reports/" + date)
	data, _ := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	var respMap map[string]interface{}
	if err := json.Unmarshal(data, &respMap); err == nil {
		if data, ok := respMap["data"].(map[string]interface{}); ok {
			if reports, ok := data["reports"].([]interface{}); ok {
				params["reports"] = reports
				c.TemplateHTML("index.tmpl", params)
				return
			}
		}
	}

	log.Println(respMap)
	c.TemplateHTML("error.tmpl", nil)
	return
}
func HandleUserDailyReports(c wine.Context) {
	start := c.Params().GetStr("start")
	end := c.Params().GetStr("end")
	var st time.Time
	var et time.Time
	var e error
	if st, e = time.Parse("2006-01-02", start); e != nil {
		c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"})
		return
	}

	if et, e = time.Parse("2006-01-02", end); e != nil {
		c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"})
		return
	}

	userId, _ := strconv.ParseInt(c.Params().GetStr("user_id"), 10, 64)
	if userId <= 0 {
		c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"})
		return
	}

	resp, e := http.Get("http://localhost:8001/user/" + c.Params().GetStr("user_id") + "/daily-reports/" + start + "/" + end)
	if e != nil {
		c.TemplateHTML("error.tmpl", gox.M{"msg": "server error"})
		return
	}

	data, _ := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	params := gox.M{}

	previousStart := st.AddDate(0, 0, -10).Format("2006-01-02")
	nextEnd := et.AddDate(0, 0, 10).Format("2006-01-02")
	params["previous_start"] = previousStart
	params["next_end"] = nextEnd
	params["start"] = start
	params["end"] = end

	var respMap map[string]interface{}
	if err := json.Unmarshal(data, &respMap); err == nil {
		if data, ok := respMap["data"].(map[string]interface{}); ok {
			if reports, ok := data["reports"].([]interface{}); ok {
				for _, r := range reports {
					m := r.(map[string]interface{})
					t, _ := time.Parse("2006-01-02", m["date"].(string))
					m["date"] = m["date"].(string) + "(" + DaysOfWeek[t.Weekday()] + ")"
				}

				if user, ok := data["user"].(map[string]interface{}); ok {
					params["reports"] = reports
					params["user"] = user
					c.TemplateHTML("user_daily_reports.tmpl", params)
					return
				}
			}
		}
	}

	log.Println(respMap)
	c.TemplateHTML("error.tmpl", gox.M{"msg": "server error"})
}