package services import ( "eta/eta_forum_admin/models/chart_collect" "eta/eta_forum_admin/services/elastic" "eta/eta_forum_admin/utils" "fmt" "strconv" "strings" ) // EsAddOrEditChartCollectByChartInfoId 新增和修改ES中的收藏图表数据 func EsAddOrEditChartCollectByChartInfoId(chartInfoId int) { errMsg := make([]string, 0) defer func() { if len(errMsg) > 0 { fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n")) } }() var condition string var pars []interface{} condition += " and a.chart_info_id = ? " pars = append(pars, chartInfoId) allList, err := chart_collect.GetChartCollectByChartInfoIdAndUserIdByCondition(condition, pars) if err != nil { errMsg = append(errMsg, fmt.Sprintf("EsAddOrEditChartCollectByChartInfoId Err:%s", err.Error())) return } for _, v := range allList { //添加es v.CollectClassifyIds += "," err = elastic.EsAddOrEditDataInterface(utils.CHART_COLLECT_INDEX_NAME, v.UserChartInfoId, v) if err != nil { errMsg = append(errMsg, fmt.Sprintf("图表收藏:%s,添加Es失败,err:%s", v.UserChartInfoId, err.Error())) } } return } // EsAddOrEditChartCollectByChartInfoIdUserId 新增和修改ES中的收藏图表数据 func EsAddOrEditChartCollectByChartInfoIdUserId(chartInfoId, userId int) { errMsg := make([]string, 0) defer func() { if len(errMsg) > 0 { fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n")) } }() var condition string var pars []interface{} condition += " and a.chart_info_id = ? and a.user_id = ? " pars = append(pars, chartInfoId, userId) allList, err := chart_collect.GetChartCollectByChartInfoIdAndUserIdByCondition(condition, pars) if err != nil { errMsg = append(errMsg, fmt.Sprintf("EsAddOrEditChartCollectByChartInfoId Err:%s", err.Error())) return } for _, v := range allList { //添加es v.CollectClassifyIds += "," fmt.Println(v) err = elastic.EsAddOrEditDataInterface(utils.CHART_COLLECT_INDEX_NAME, v.UserChartInfoId, v) if err != nil { errMsg = append(errMsg, fmt.Sprintf("图表收藏:%s,添加Es失败,err:%s", v.UserChartInfoId, err.Error())) } } return } // EsDeleteChartCollectByChartInfoIdUserId 取消收藏 func EsDeleteChartCollectByChartInfoIdUserId(chartInfoId, userId int) { var err error defer func() { if err != nil { fmt.Println("删除ES中的图表数据失败:", err.Error()) } }() //添加es // 先删除,再新增 userChartInfoId := strconv.Itoa(userId) + "_" + strconv.Itoa(chartInfoId) err = elastic.EsDeleteDataV2(utils.CHART_COLLECT_INDEX_NAME, userChartInfoId) if err != nil { err = fmt.Errorf(fmt.Sprintf("图表收藏:%s,删除Es失败,err:%s", userChartInfoId, err.Error())) return } var condition string var pars []interface{} condition += " and a.chart_info_id = ? and a.user_id = ? " pars = append(pars, chartInfoId, userId) allList, err := chart_collect.GetChartCollectByChartInfoIdAndUserIdByCondition(condition, pars) if err != nil { return } for _, v := range allList { //添加es v.CollectClassifyIds += "," err = elastic.EsAddOrEditDataInterface(utils.CHART_COLLECT_INDEX_NAME, v.UserChartInfoId, v) if err != nil { err = fmt.Errorf("图表收藏:%s,添加Es失败,err:%s", v.UserChartInfoId, err.Error()) return } } return } // EsDeleteChartCollectByChartInfoIdUserIds 删除图表时,删除图表的收藏记录 func EsDeleteChartCollectByChartInfoIdUserIds(userChartInfoIds []string) { if len(userChartInfoIds) == 0 { return } errMsg := make([]string, 0) defer func() { if len(errMsg) > 0 { fmt.Println("EsDeleteMyChartInfoByMyChartIds, 删除ES中的我的图表数据失败:", strings.Join(errMsg, "\n")) } }() for _, v := range userChartInfoIds { id := v if e := elastic.EsDeleteDataV2(utils.CHART_COLLECT_INDEX_NAME, id); e != nil { errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,删除Es失败,err:%s", v, e.Error())) } } return } func AddAllChartCollectData() { var condition string var pars []interface{} allList, err := chart_collect.GetChartCollectByChartInfoIdAndUserIdByCondition(condition, pars) if err != nil { return } for _, v := range allList { //添加es v.CollectClassifyIds += "," err = elastic.EsAddOrEditDataInterface(utils.CHART_COLLECT_INDEX_NAME, v.UserChartInfoId, v) if err != nil { err = fmt.Errorf("图表收藏:%s,添加Es失败,err:%s", v.UserChartInfoId, err.Error()) return } } }