api_task.go 4.4 KB

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