123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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
- }
- }
- }
|