info_for_change.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. "context"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/jaryhe/gopkgs/database"
  9. "github.com/jaryhe/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "smart-site-management/errors"
  12. "smart-site-management/model"
  13. "smart-site-management/pb/v1"
  14. )
  15. func makeProjectInfoContent(data []byte)([]*v1.ProjectInfoItem) {
  16. m := map[string]interface{}{}
  17. if len(data) == 0 {
  18. return nil
  19. }
  20. json.Unmarshal(data, &m)
  21. ret := make([]*v1.ProjectInfoItem, 0)
  22. for k, v := range m {
  23. if _, ok := projectFieldMap[k]; !ok {
  24. continue
  25. }
  26. item := &v1.ProjectInfoItem{
  27. Name:k,
  28. Chinese:projectFieldMap[k],
  29. Value:fmt.Sprintf("%v", v),
  30. }
  31. ret = append(ret, item)
  32. }
  33. return ret
  34. }
  35. func ProjectInfoForChange(ctx context.Context, req *v1.ProjectInfoForChangeRequest) (reply *v1.ProjectInfoForChangeReply, err error) {
  36. project := model.TProject{}
  37. err = project.Find(database.DB(), map[string]interface{}{"id":req.Id})
  38. if err != nil {
  39. logger.Error("DeviceList",
  40. zap.String("err", err.Error()))
  41. return nil, errors.DataBaseError
  42. }
  43. if project.Id == 0 {
  44. return nil, errors.NoRecordError
  45. }
  46. bytes, _ := json.Marshal(project)
  47. list := makeProjectInfoContent(bytes)
  48. reply = &v1.ProjectInfoForChangeReply{
  49. List:list,
  50. }
  51. return reply, nil
  52. }