package controllers import ( "database/sql" models "devel.mephi.ru/dyokunev/cps-models" helpers "devel.mephi.ru/dyokunev/go-helpers" "encoding/json" "fmt" "github.com/revel/revel" "time" ) type Passes struct { Controller } func (c Passes) Index(pass models.Pass, updatePersonIds bool, startDate, endDate time.Time) revel.Result { if r := c.Init(); r != nil { return r } me := c.GetMe() scope := c.PassSQL if (models.Pass{}) != pass { scope = scope.Where(pass) } if (time.Time{}) != startDate || (time.Time{}) != endDate { scope = scope.SetTableQuery("`events_users_view` JOIN `activity_userids` USING (`UserID`)").Group("UserId") if (time.Time{}) != startDate { scope = scope.Where("date >= ?", startDate) } if (time.Time{}) != endDate { scope = scope.Where("date < ?", endDate) } } passes, err := scope.Select() if r := c.ConsiderErr(err); r != nil { return r } if !me.Permissions.IsFixOk { return c.Render(passes) } if updatePersonIds { var err error apiKey, _ := revel.Config.String("fixOk.ApiKey") params := map[string]interface{}{"apiKey": apiKey} if pass.Id != 0 { params["id"] = pass.Id } fixokJsonPasses := helpers.HttpGetJson("http://10.10.177.2/passes/index.json", params).([]interface{}) var currentFixokPasses models.FixOkRows currentFixokPasses, err = models.FixOkRowSQL.Select(*pass.FixOkRow()) if err != nil && err != sql.ErrNoRows { return c.RenderError(err) } passesMap := currentFixokPasses.ToMap() for _, fixokJsonPassI := range fixokJsonPasses { fixokJsonPass := fixokJsonPassI.(map[string]interface{}) fixokJsonPassId64, err := fixokJsonPass["id"].(json.Number).Int64() if err != nil { revel.ERROR.Printf("Got error: %v", err.Error()) continue } fixokJsonPassId := int(fixokJsonPassId64) pass := passesMap[fixokJsonPassId] if pass == nil { // New record revel.TRACE.Printf("new pass from fixok: %v", fixokJsonPass) pass = &models.FixOkRow{} pass.ParseFixokJson(fixokJsonPass) if pass.EmpGUID == 0 { revel.ERROR.Printf("pass.EmpGUID == 0") continue } pass.Insert() } else { updatedPass := models.FixOkRow{} updatedPass.ParseFixokJson(fixokJsonPass) pass.EmpGUID = updatedPass.EmpGUID if pass.EmpGUID == 0 { pass.Delete() } else { pass.Update() } } } } return c.Render(passes) } func (c Passes) renderNoPhoto() revel.Result { return c.Redirect("https://voip.mephi.ru/public/photos/no_photo.png") } func (c Passes) renderPhotoOf(passId int) revel.Result { panic("Not implemented, yet") return nil } func (c Passes) GetPhoto(passId int) revel.Result { if r := c.Init(); r != nil { return r } me := c.GetMe() if !me.Permissions.IsFixOk { return c.RenderError(fmt.Errorf("No permission")) } return c.renderPhotoOf(passId) }