api_task.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go.mongodb.org/mongo-driver/bson"
  6. "go.uber.org/zap"
  7. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  8. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  9. "net/http"
  10. "tieta_data/config"
  11. )
  12. type Req struct {
  13. Ids []string `json:"ids"`
  14. }
  15. type Resp struct {
  16. Status int `json:"status"`
  17. Message string `json:"message"`
  18. Data string `json:"data"`
  19. }
  20. func MergeProject(w http.ResponseWriter, req *http.Request) {
  21. w.Header().Set("Access-Control-Allow-Origin", "*")
  22. var res Req
  23. if err := json.NewDecoder(req.Body).Decode(&res); err != nil {
  24. _ = req.Body.Close()
  25. }
  26. log.Info("MergeProject", zap.Any("req", res))
  27. //var result Resp
  28. var result Resp
  29. if len(res.Ids) > 1 {
  30. // 重新捏合字段
  31. pid := MergeMethod(res.Ids)
  32. if pid != "" {
  33. result = Resp{Status: 1, Message: "重新合并成功", Data: pid}
  34. } else {
  35. result = Resp{Status: -1, Message: "重新合并失败", Data: pid}
  36. }
  37. } else {
  38. result = Resp{Status: -1, Message: "参数有误"}
  39. }
  40. if err := json.NewEncoder(w).Encode(result); err != nil {
  41. log.Error("MergeProject", zap.Error(err))
  42. }
  43. }
  44. func MergeMethod(ids []string) string {
  45. newId := ""
  46. for i, id := range ids {
  47. log.Debug("项目id,", zap.String("id: ", id))
  48. // 1、删除旧有项目
  49. AllIdsMapLock.Lock()
  50. delete(AllIdsMap, id)
  51. AllIdsMapLock.Unlock()
  52. b := MysqlTool.Delete(config.Conf.DB.Mysql.Pcoll, bson.M{"projectId": id})
  53. if !b {
  54. return ""
  55. }
  56. binfo := MysqlTool.Find(config.Conf.DB.Mysql.Coll, bson.M{"projectId": id}, "", "", -1, -1)
  57. // 2、id标讯数据重新合并
  58. for j, m := range *binfo {
  59. info := ParseInfo(m)
  60. if i == 0 && j == 0 {
  61. var p1 *Project
  62. newId, p1 = NewProject(m, info)
  63. AllIdsMapLock.Lock()
  64. AllIdsMap[newId] = &ID{Id: newId, P: p1}
  65. AllIdsMapLock.Unlock()
  66. } else {
  67. AllIdsMapLock.Lock()
  68. res := AllIdsMap[newId]
  69. AllIdsMapLock.Unlock()
  70. if res != nil {
  71. comparePro := res.P
  72. UpdateProject(m, info, comparePro, -1, "AAAAAAAAAA")
  73. }
  74. }
  75. }
  76. }
  77. log.Debug(fmt.Sprintf("newId: %s", newId))
  78. return newId
  79. }
  80. type UpReq struct {
  81. Nid string `json:"nid"`
  82. Type int `json:"type"` // 0:修改;1:删除
  83. }
  84. type UpResp struct {
  85. Status int `json:"status"`
  86. Message string `json:"message"`
  87. }
  88. func UpdateInfo(w http.ResponseWriter, req *http.Request) {
  89. w.Header().Set("Access-Control-Allow-Origin", "*")
  90. var res UpReq
  91. if err := json.NewDecoder(req.Body).Decode(&res); err != nil {
  92. _ = req.Body.Close()
  93. }
  94. log.Info("UpdateInfo", zap.Any("req", res))
  95. var result UpResp
  96. if res.Nid != "" {
  97. if res.Type == 1 {
  98. MysqlTool.Delete(config.Conf.DB.Mysql.Pcoll, bson.M{"projectId": res.Nid})
  99. result = UpResp{Status: 1, Message: "删除成功"}
  100. } else {
  101. if info := MysqlTool.FindOne(config.Conf.DB.Mysql.Coll, bson.M{"id_new": res.Nid}, "", ""); info != nil && len(*info) > 0 {
  102. if pid := util.ObjToString((*info)["projectId"]); pid != "" {
  103. if pinfo := MysqlTool.Find(config.Conf.DB.Mysql.Pcoll, bson.M{"projectId": pid}, "", "", -1, -1); pinfo != nil && len(*pinfo) > 0 {
  104. info1 := ParseInfo(*info)
  105. if len(*pinfo) > 1 {
  106. AllIdsMapLock.Lock()
  107. res := AllIdsMap[pid]
  108. AllIdsMapLock.Unlock()
  109. if res != nil {
  110. comparePro := res.P
  111. UpdateProject(*info, info1, comparePro, -1, "AAAAAAAAAA")
  112. }
  113. } else {
  114. // 删除旧有项目
  115. AllIdsMapLock.Lock()
  116. delete(AllIdsMap, pid)
  117. AllIdsMapLock.Unlock()
  118. MysqlTool.Delete(config.Conf.DB.Mysql.Pcoll, bson.M{"projectId": pid})
  119. startProjectMerge(info1, *info)
  120. }
  121. }
  122. }
  123. result = UpResp{Status: 1, Message: "重新合并成功"}
  124. } else {
  125. result = UpResp{Status: -1, Message: "未查询到项目信息"}
  126. }
  127. }
  128. } else {
  129. result = UpResp{Status: -1, Message: "参数有误"}
  130. }
  131. if err := json.NewEncoder(w).Encode(result); err != nil {
  132. log.Error("UpdateInfo", zap.Error(err))
  133. }
  134. }