change_info_list.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package project
  4. import (
  5. "fmt"
  6. "context"
  7. "encoding/json"
  8. "github.com/jaryhe/gopkgs/database"
  9. "github.com/jinzhu/gorm"
  10. "smart-site-management/errors"
  11. "smart-site-management/model"
  12. "smart-site-management/pb/v1"
  13. "github.com/jaryhe/gopkgs/logger"
  14. "go.uber.org/zap"
  15. )
  16. func makeProjectChangeInfoContent(data []byte)([]*v1.ProjectInfoItem) {
  17. m := map[string]interface{}{}
  18. if len(data) == 0 {
  19. return nil
  20. }
  21. json.Unmarshal(data, &m)
  22. ret := make([]*v1.ProjectInfoItem, len(m))
  23. i := 0
  24. for k, v := range m {
  25. item := &v1.ProjectInfoItem{
  26. Name:k,
  27. Chinese:projectFieldMap[k],
  28. Value:fmt.Sprintf("%v", v),
  29. }
  30. ret[i] = item
  31. i++
  32. }
  33. return ret
  34. }
  35. func ProjectChangeInfoList(ctx context.Context, req *v1.ProjectChangeInfoListRequest) (reply *v1.ProjectChangeInfoListReply, err error) {
  36. p := model.TProjectJob{}
  37. where := map[string]interface{}{
  38. "project_id":req.Id,
  39. "type":model.ProjectJobTypeUpdateProject,
  40. }
  41. total, err := p.Count(database.DB(), where)
  42. if err != nil {
  43. logger.Error("ProjectChangeInfoList",
  44. zap.String("err", err.Error()))
  45. return nil, errors.DataBaseError
  46. }
  47. project := model.TProject{}
  48. err = project.Find(database.DB(), map[string]interface{}{"id":req.Id})
  49. if err != nil {
  50. logger.Error("ProjectChangeInfoList",
  51. zap.String("err", err.Error()))
  52. return nil, errors.DataBaseError
  53. }
  54. if project.Id == 0 {
  55. return nil, errors.NoRecordError
  56. }
  57. reply = &v1.ProjectChangeInfoListReply{
  58. Total:total,
  59. Page:req.Page,
  60. PageSize:int32(model.PageSize),
  61. }
  62. if total == 0 {
  63. return reply, nil
  64. }
  65. list, err := p.List(database.DB(), where, int(req.Page))
  66. if err != nil && err != gorm.ErrRecordNotFound {
  67. logger.Error("ProjectChangeInfoList",
  68. zap.String("err", err.Error()))
  69. return nil, errors.DataBaseError
  70. }
  71. reply.List = make([]*v1.ProjectChangeInfoItem, len(list))
  72. for i, v := range list {
  73. reply.List[i] = &v1.ProjectChangeInfoItem{}
  74. reply.List[i].Status = v.Status
  75. reply.List[i].ApplyTime = v.CreatedAt.Format(layout)
  76. if v.Status > 0 {
  77. reply.List[i].ApproveTime = v.UpdatedAt.Format(layout)
  78. }
  79. reply.List[i].Origin = makeProjectChangeInfoContent([]byte(v.Origin))
  80. reply.List[i].Dst = makeProjectChangeInfoContent([]byte(v.Content))
  81. reply.List[i].Name = project.Name
  82. reply.List[i].SafetyRecordNo = project.SafetyRecordNo
  83. reply.List[i].Addr = project.Location
  84. }
  85. return reply, nil
  86. }