change.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "strconv"
  15. "time"
  16. )
  17. var projectFieldMap = map[string]string{
  18. "safety_record_no":"安监备案号",
  19. "project_no":"项目编号",
  20. "name":"项目名称",
  21. "short_name":"项目简称",
  22. "project_function":"项目功能",
  23. "project_type":"项目类型",
  24. "price":"项目造价",
  25. "desc":"项目介绍",
  26. "project_leader":"项目负责人",
  27. "covered_area":"建筑面积",
  28. "phone":"项目负责人电话",
  29. "total_level":"总层数",
  30. "building_storey":"建筑层数",
  31. "construction":"施工单位",
  32. "development":"建设单位",
  33. "oversee":"监理单位",
  34. "design":"设计单位",
  35. "exploration":"勘察单位",
  36. "location":"地址",
  37. "lon":"经度",
  38. "lat":"纬度",
  39. "is_finish":"是否完工",
  40. "project_effect_pic":"项目效果图",
  41. "project_plan":"项目平面图",
  42. }
  43. var projectFieldTypeMap = map[string]string{
  44. "id": "int",
  45. "cid": "int",
  46. "safety_record_no": "string",
  47. "project_no": "string",
  48. "name": "string",
  49. "short_name": "string",
  50. "project_function": "string",
  51. "project_type": "string",
  52. "price": "float",
  53. "desc": "string",
  54. "project_leader": "string",
  55. "covered_area": "string",
  56. "phone": "string",
  57. "total_level": "string",
  58. "building_storey": "string",
  59. "construction": "string",
  60. "development": "string",
  61. "oversee": "string",
  62. "design": "string",
  63. "exploration": "string",
  64. "location": "string",
  65. "lon": "float",
  66. "lat": "float",
  67. "is_finish": "int",
  68. "project_effect_pic":"string",
  69. "project_plan":"string",
  70. }
  71. var layout = "2006-01-02 15:04:05"
  72. func ProjectChange(ctx context.Context, req *v1.ProjectChangeRequest) (reply *v1.ProjectChangeReply, err error) {
  73. // 构造更新map
  74. dstm := map[string]interface{}{}
  75. for _, v := range req.List {
  76. ftype, ok := projectFieldTypeMap[v.Name]
  77. if !ok {
  78. logger.Error("ProjectChange",
  79. zap.String("err", fmt.Sprintf("字段不存在%s", v.Name)))
  80. return nil, errors.ParamsError
  81. }
  82. switch ftype {
  83. case "int":
  84. value, _ := strconv.ParseInt(v.Value, 10, 64)
  85. dstm[v.Name] = value
  86. case "float":
  87. value, _ := strconv.ParseFloat(v.Value, 64)
  88. dstm[v.Name] = value
  89. default:
  90. dstm[v.Name] = v.Value
  91. }
  92. }
  93. // 获取原始数据
  94. origin := &model.TProject{}
  95. where := map[string]interface{}{"id":req.Id}
  96. err = origin.Find(database.DB(), where)
  97. if err != nil {
  98. logger.Error("ProjectChange",
  99. zap.String("err", err.Error()))
  100. return nil, errors.DataBaseError
  101. }
  102. if origin.Id == 0 {
  103. return nil, errors.NoRecordError
  104. }
  105. // 构造更新字段的json字符串
  106. originFull := map[string]interface{}{}
  107. bytes, _ := json.Marshal(origin)
  108. json.Unmarshal([]byte(bytes), &originFull)
  109. originChoose := map[string]interface{}{}
  110. for k, _ := range dstm {
  111. if _, ok := originFull[k];!ok {
  112. return nil, errors.SystemError
  113. }
  114. originChoose[k] = originFull[k]
  115. }
  116. dstBytes, _ := json.Marshal(dstm)
  117. originBytes, _ := json.Marshal(originChoose)
  118. // 提工单
  119. job := model.TProjectJob{}
  120. now := time.Now()
  121. job.CreatedAt = now
  122. job.UpdatedAt = now
  123. job.Type = model.ProjectJobTypeUpdateProject
  124. job.ProjectId = origin.Id
  125. job.Origin = string(originBytes)
  126. job.Content = string(dstBytes)
  127. err = job.Insert(database.DB())
  128. if err != nil {
  129. logger.Error("ProjectChange",
  130. zap.String("err", err.Error()))
  131. return nil, errors.DataBaseError
  132. }
  133. return &v1.ProjectChangeReply{}, nil
  134. }