passes.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package controllers
  2. import (
  3. "database/sql"
  4. models "devel.mephi.ru/dyokunev/cps-models"
  5. helpers "devel.mephi.ru/dyokunev/go-helpers"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/revel/revel"
  9. "time"
  10. )
  11. type Passes struct {
  12. Controller
  13. }
  14. func (c Passes) Index(pass models.Pass, updatePersonIds bool, startDate, endDate time.Time) revel.Result {
  15. if r := c.Init(); r != nil {
  16. return r
  17. }
  18. me := c.GetMe()
  19. scope := c.PassSQL
  20. if (models.Pass{}) != pass {
  21. scope = scope.Where(pass)
  22. }
  23. if (time.Time{}) != startDate || (time.Time{}) != endDate {
  24. scope = scope.SetTableQuery("`events_users_view` JOIN `activity_userids` USING (`UserID`)").Group("UserId")
  25. if (time.Time{}) != startDate {
  26. scope = scope.Where("date >= ?", startDate)
  27. }
  28. if (time.Time{}) != endDate {
  29. scope = scope.Where("date < ?", endDate)
  30. }
  31. }
  32. passes, err := scope.Select()
  33. if r := c.ConsiderErr(err); r != nil {
  34. return r
  35. }
  36. if !me.Permissions.IsFixOk {
  37. return c.Render(passes)
  38. }
  39. if updatePersonIds {
  40. var err error
  41. apiKey, _ := revel.Config.String("fixOk.ApiKey")
  42. params := map[string]interface{}{"apiKey": apiKey}
  43. if pass.Id != 0 {
  44. params["id"] = pass.Id
  45. }
  46. fixokJsonPasses := helpers.HttpGetJson("http://10.10.177.2/passes/index.json", params).([]interface{})
  47. var currentFixokPasses models.FixOkRows
  48. currentFixokPasses, err = models.FixOkRowSQL.Select(*pass.FixOkRow())
  49. if err != nil && err != sql.ErrNoRows {
  50. return c.RenderError(err)
  51. }
  52. passesMap := currentFixokPasses.ToMap()
  53. for _, fixokJsonPassI := range fixokJsonPasses {
  54. fixokJsonPass := fixokJsonPassI.(map[string]interface{})
  55. fixokJsonPassId64, err := fixokJsonPass["id"].(json.Number).Int64()
  56. if err != nil {
  57. revel.ERROR.Printf("Got error: %v", err.Error())
  58. continue
  59. }
  60. fixokJsonPassId := int(fixokJsonPassId64)
  61. pass := passesMap[fixokJsonPassId]
  62. if pass == nil { // New record
  63. revel.TRACE.Printf("new pass from fixok: %v", fixokJsonPass)
  64. pass = &models.FixOkRow{}
  65. pass.ParseFixokJson(fixokJsonPass)
  66. if pass.EmpGUID == 0 {
  67. revel.ERROR.Printf("pass.EmpGUID == 0")
  68. continue
  69. }
  70. pass.Insert()
  71. } else {
  72. updatedPass := models.FixOkRow{}
  73. updatedPass.ParseFixokJson(fixokJsonPass)
  74. pass.EmpGUID = updatedPass.EmpGUID
  75. if pass.EmpGUID == 0 {
  76. pass.Delete()
  77. } else {
  78. pass.Update()
  79. }
  80. }
  81. }
  82. }
  83. return c.Render(passes)
  84. }
  85. func (c Passes) renderNoPhoto() revel.Result {
  86. return c.Redirect("https://voip.mephi.ru/public/photos/no_photo.png")
  87. }
  88. func (c Passes) renderPhotoOf(passId int) revel.Result {
  89. panic("Not implemented, yet")
  90. return nil
  91. }
  92. func (c Passes) GetPhoto(passId int) revel.Result {
  93. if r := c.Init(); r != nil {
  94. return r
  95. }
  96. me := c.GetMe()
  97. if !me.Permissions.IsFixOk {
  98. return c.RenderError(fmt.Errorf("No permission"))
  99. }
  100. return c.renderPhotoOf(passId)
  101. }