123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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)
- }
|