package main import ( "encoding/json" "fmt" "go.mongodb.org/mongo-driver/bson" "go.uber.org/zap" util "jygit.jydev.jianyu360.cn/data_processing/common_utils" "jygit.jydev.jianyu360.cn/data_processing/common_utils/log" "net/http" "tieta_data/config" "time" ) type Req struct { Ids []string `json:"ids"` } type Resp struct { Status int `json:"status"` Message string `json:"message"` Data string `json:"data"` } func MergeProject(w http.ResponseWriter, req *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") var res Req if err := json.NewDecoder(req.Body).Decode(&res); err != nil { _ = req.Body.Close() } log.Info("MergeProject", zap.Any("req", res)) //var result Resp var result Resp if len(res.Ids) > 1 { // 重新捏合字段 pid := MergeMethod(res.Ids) if pid != "" { result = Resp{Status: 1, Message: "重新合并成功", Data: pid} } else { result = Resp{Status: -1, Message: "重新合并失败", Data: pid} } } else { result = Resp{Status: -1, Message: "参数有误"} } if err := json.NewEncoder(w).Encode(result); err != nil { log.Error("MergeProject", zap.Error(err)) } } func MergeMethod(ids []string) string { newId := "" for i, id := range ids { log.Debug("项目id,", zap.String("id: ", id)) // 1、删除旧有项目 AllIdsMapLock.Lock() delete(AllIdsMap, id) AllIdsMapLock.Unlock() b := MysqlTool.Delete(config.Conf.DB.Mysql.Pcoll, bson.M{"projectId": id}) if !b { return "" } binfo := MysqlTool.Find(config.Conf.DB.Mysql.Coll, bson.M{"projectId": id}, "", "", -1, -1) // 2、id标讯数据重新合并 for j, m := range *binfo { info := ParseInfo(m) if i == 0 && j == 0 { var p1 *Project newId, p1 = NewProject(m, info) AllIdsMapLock.Lock() AllIdsMap[newId] = &ID{Id: newId, P: p1} AllIdsMapLock.Unlock() } else { AllIdsMapLock.Lock() res := AllIdsMap[newId] AllIdsMapLock.Unlock() if res != nil { comparePro := res.P UpdateProject(m, info, comparePro, -1, "AAAAAAAAAA") } } } } log.Debug(fmt.Sprintf("newId: %s", newId)) return newId } type UpReq struct { Nid string `json:"nid"` Type int `json:"type"` // 0:修改;1:删除 } type UpResp struct { Status int `json:"status"` Message string `json:"message"` } func UpdateInfo(w http.ResponseWriter, req *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") var res UpReq if err := json.NewDecoder(req.Body).Decode(&res); err != nil { _ = req.Body.Close() } log.Info("UpdateInfo", zap.Any("req", res)) var result UpResp if res.Nid != "" { if res.Type == 1 { start := time.Now().Unix() log.Info("删除操作", zap.Int64("start", start)) MysqlTool.Delete(config.Conf.DB.Mysql.Pcoll, bson.M{"projectId": res.Nid}) log.Info("删除操作", zap.Int64("end", time.Now().Unix()-start)) result = UpResp{Status: 1, Message: "删除成功"} } else { if info := MysqlTool.FindOne(config.Conf.DB.Mysql.Coll, bson.M{"id_new": res.Nid}, "", ""); info != nil && len(*info) > 0 { if pid := util.ObjToString((*info)["projectId"]); pid != "" { if pinfo := MysqlTool.Find(config.Conf.DB.Mysql.Pcoll, bson.M{"projectId": pid}, "", "", -1, -1); pinfo != nil && len(*pinfo) > 0 { info1 := ParseInfo(*info) if len(*pinfo) > 1 { AllIdsMapLock.Lock() res := AllIdsMap[pid] AllIdsMapLock.Unlock() if res != nil { comparePro := res.P UpdateProject(*info, info1, comparePro, -1, "AAAAAAAAAA") } } else { // 删除旧有项目 AllIdsMapLock.Lock() delete(AllIdsMap, pid) AllIdsMapLock.Unlock() MysqlTool.Delete(config.Conf.DB.Mysql.Pcoll, bson.M{"projectId": pid}) startProjectMerge(info1, *info) } } } result = UpResp{Status: 1, Message: "重新合并成功"} } else { result = UpResp{Status: -1, Message: "未查询到项目信息"} } } } else { result = UpResp{Status: -1, Message: "参数有误"} } if err := json.NewEncoder(w).Encode(result); err != nil { log.Error("UpdateInfo", zap.Error(err)) } } func StartMerge(w http.ResponseWriter, req *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") log.Info("StartMerge ---") if !IsProject { go taskProject() } result := UpResp{Status: 1, Message: "执行项目合并成功"} if err := json.NewEncoder(w).Encode(result); err != nil { log.Error("UpdateInfo", zap.Error(err)) } }