chart_info_elastic.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package data
  2. import (
  3. "eta/eta_mobile/models/data_manage"
  4. "eta/eta_mobile/services/elastic"
  5. "eta/eta_mobile/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. )
  10. //func CreateChartInfoIndex() {
  11. // indexName := utils.CHART_INDEX_NAME
  12. // mappingJson := `{
  13. // "mappings": {
  14. // "dynamic": true,
  15. // "properties": {
  16. // "ChartInfoId": {
  17. // "type": "integer"
  18. // },
  19. // "ChartName": {
  20. // "type": "text",
  21. // "term_vector": "with_positions_offsets",
  22. // "analyzer": "ik_smart"
  23. // },
  24. // "ChartClassifyId": {
  25. // "type": "integer"
  26. // },
  27. // "SysUserId": {
  28. // "type": "integer"
  29. // },
  30. // "SysUserRealName": {
  31. // "type": "text",
  32. // "term_vector": "with_positions_offsets",
  33. // "analyzer": "ik_smart"
  34. // },
  35. // "UniqueCode": {
  36. // "type": "text",
  37. // "term_vector": "with_positions_offsets"
  38. // },
  39. // "Unit": {
  40. // "type": "text",
  41. // "term_vector": "with_positions_offsets"
  42. // },
  43. // "CreateTime": {
  44. // "type": "date"
  45. // },
  46. // "ModifyTime": {
  47. // "type": "date"
  48. // },
  49. // "DateType": {
  50. // "type": "integer"
  51. // },
  52. // "StartDate": {
  53. // "type": "text",
  54. // "term_vector": "with_positions_offsets"
  55. // },
  56. // "EndDate": {
  57. // "type": "text",
  58. // "term_vector": "with_positions_offsets"
  59. // },
  60. // "IsSetName": {
  61. // "type": "integer"
  62. // },
  63. // "EdbInfoIds": {
  64. // "type": "text",
  65. // "term_vector": "with_positions_offsets"
  66. // },
  67. // "ChartType": {
  68. // "type": "integer"
  69. // },
  70. // "Calendar": {
  71. // "type": "text",
  72. // "term_vector": "with_positions_offsets"
  73. // },
  74. // "ChartImage": {
  75. // "type": "text",
  76. // "term_vector": "with_positions_offsets"
  77. // }
  78. // }
  79. // }
  80. //}`
  81. // EsCreateIndex(indexName, mappingJson)
  82. //}
  83. func AddAllChartInfo() {
  84. allList, err := data_manage.GetChartInfoAllList()
  85. if err != nil {
  86. fmt.Println("GetArticleAll Err:", err.Error())
  87. return
  88. }
  89. total := len(allList)
  90. for k, v := range allList {
  91. EsAddOrEditChartInfo(v.ChartInfoId)
  92. //fmt.Println(v.ChartInfoId)
  93. fmt.Println("剩余", total-k-1, "条数据,当前图表id:", v.ChartInfoId)
  94. }
  95. }
  96. // EsAddOrEditChartInfo 新增和修改ES中的图表数据
  97. func EsAddOrEditChartInfo(chartInfoId int) {
  98. var err error
  99. defer func() {
  100. if err != nil {
  101. fmt.Println("新增和修改ES中的图表数据失败:", err.Error())
  102. }
  103. }()
  104. itemInfo, _ := data_manage.GetChartInfoById(chartInfoId)
  105. //添加es
  106. err = elastic.EsAddOrEditChartInfoData(utils.CHART_INDEX_NAME, strconv.Itoa(itemInfo.ChartInfoId), itemInfo)
  107. return
  108. }
  109. // EsDeleteChartInfo 删除ES中的图表数据
  110. func EsDeleteChartInfo(chartInfoId int) {
  111. var err error
  112. defer func() {
  113. if err != nil {
  114. fmt.Println("删除ES中的图表数据失败:", err.Error())
  115. }
  116. }()
  117. //添加es
  118. err = elastic.EsDeleteDataV2(utils.CHART_INDEX_NAME, strconv.Itoa(chartInfoId))
  119. return
  120. }
  121. // EsSearchChartInfo 搜索图表信息
  122. func EsSearchChartInfo(keyword string, showSysId int, sourceList []int, noPermissionChartIdList []int, startSize, pageSize int) (list []*data_manage.ChartInfo, total int64, err error) {
  123. list, total, err = elastic.SearchChartInfoData(utils.CHART_INDEX_NAME, keyword, showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  124. return
  125. }
  126. // AddAllMyChartInfo 添加所有的我的图库数据
  127. func AddAllMyChartInfo() {
  128. errMsg := make([]string, 0)
  129. defer func() {
  130. if len(errMsg) > 0 {
  131. fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n"))
  132. }
  133. }()
  134. var condition string
  135. var pars []interface{}
  136. allList, err := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition, pars)
  137. if err != nil {
  138. fmt.Println("AddAllMyChartInfo Err:", err.Error())
  139. return
  140. }
  141. for _, v := range allList {
  142. //添加es
  143. err = elastic.EsAddOrEditDataInterface(utils.MY_CHART_INDEX_NAME, strconv.Itoa(v.MyChartId), v)
  144. if err != nil {
  145. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,添加Es失败,err:%s", v.MyChartId, err.Error()))
  146. }
  147. }
  148. }
  149. // EsAddOrEditMyChartInfoByChartInfoId 根据图表id 新增和修改ES中的图表数据
  150. func EsAddOrEditMyChartInfoByChartInfoId(chartInfoId int) {
  151. errMsg := make([]string, 0)
  152. defer func() {
  153. if len(errMsg) > 0 {
  154. fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n"))
  155. }
  156. }()
  157. var condition string
  158. var pars []interface{}
  159. condition += " and a.chart_info_id = ? "
  160. pars = append(pars, chartInfoId)
  161. allList, err := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition, pars)
  162. if err != nil {
  163. errMsg = append(errMsg, fmt.Sprintf("EsAddOrEditMyChartInfoByChartInfoId Err:%s", err.Error()))
  164. return
  165. }
  166. for _, v := range allList {
  167. //添加es
  168. err = elastic.EsAddOrEditDataInterface(utils.MY_CHART_INDEX_NAME, strconv.Itoa(v.MyChartId), v)
  169. if err != nil {
  170. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,添加Es失败,err:%s", v.MyChartId, err.Error()))
  171. }
  172. }
  173. return
  174. }
  175. // EsDeleteMyChartInfoByChartInfoId 根据图表id 删除ES中的图表数据
  176. func EsDeleteMyChartInfoByChartInfoId(chartInfoId int) {
  177. errMsg := make([]string, 0)
  178. defer func() {
  179. if len(errMsg) > 0 {
  180. fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n"))
  181. }
  182. }()
  183. var condition string
  184. var pars []interface{}
  185. condition += ` AND a.chart_info_id = ? `
  186. pars = append(pars, chartInfoId)
  187. allList, err := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition, pars)
  188. if err != nil {
  189. errMsg = append(errMsg, fmt.Sprintf("GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition Err:%s", err.Error()))
  190. return
  191. }
  192. for _, v := range allList {
  193. //删除es
  194. err = elastic.EsDeleteDataV2(utils.MY_CHART_INDEX_NAME, strconv.Itoa(v.MyChartId))
  195. if err != nil {
  196. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,删除Es失败,err:%s", v.MyChartId, err.Error()))
  197. }
  198. }
  199. return
  200. }
  201. // EsAddOrEditMyChartInfoByMyChartId 根据我的图库id 新增和修改ES中的图表数据
  202. func EsAddOrEditMyChartInfoByMyChartId(myChartId int) {
  203. errMsg := make([]string, 0)
  204. defer func() {
  205. if len(errMsg) > 0 {
  206. fmt.Println("新增和修改ES中的图表数据失败:", strings.Join(errMsg, "\n"))
  207. }
  208. }()
  209. var condition string
  210. var pars []interface{}
  211. condition += " AND a.my_chart_id = ? "
  212. pars = append(pars, myChartId)
  213. allList, err := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(condition, pars)
  214. if err != nil {
  215. errMsg = append(errMsg, fmt.Sprintf("EsAddOrEditMyChartInfoByMyChartId Err:%s", err.Error()))
  216. return
  217. }
  218. for _, v := range allList {
  219. //添加es
  220. err = elastic.EsAddOrEditDataInterface(utils.MY_CHART_INDEX_NAME, strconv.Itoa(v.MyChartId), v)
  221. if err != nil {
  222. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,添加Es失败,err:%s", v.MyChartId, err.Error()))
  223. }
  224. }
  225. return
  226. }
  227. // EsDeleteMyChartInfoByMyChartId 根据我的图库id 删除ES中的图表数据
  228. func EsDeleteMyChartInfoByMyChartId(myChartId int) {
  229. var err error
  230. defer func() {
  231. if err != nil {
  232. fmt.Println("删除ES中的图表数据失败:", err.Error())
  233. }
  234. }()
  235. //添加es
  236. err = elastic.EsDeleteDataV2(utils.MY_CHART_INDEX_NAME, strconv.Itoa(myChartId))
  237. return
  238. }
  239. // EsSearchMyChartInfo 搜索图表信息
  240. func EsSearchMyChartInfo(keyword string, adminId int, noPermissionChartIdList []int, startSize, pageSize int) (list []*data_manage.MyChartList, total int64, err error) {
  241. list, total, err = elastic.SearchMyChartInfoData(utils.MY_CHART_INDEX_NAME, keyword, adminId, noPermissionChartIdList, startSize, pageSize)
  242. return
  243. }
  244. // EsDeleteMyChartInfoByMyChartIds 根据我的图表IDs删除ES中的我的图表
  245. func EsDeleteMyChartInfoByMyChartIds(ids []int) {
  246. if len(ids) == 0 {
  247. return
  248. }
  249. errMsg := make([]string, 0)
  250. defer func() {
  251. if len(errMsg) > 0 {
  252. fmt.Println("EsDeleteMyChartInfoByMyChartIds, 删除ES中的我的图表数据失败:", strings.Join(errMsg, "\n"))
  253. }
  254. }()
  255. for _, v := range ids {
  256. id := strconv.Itoa(v)
  257. if e := elastic.EsDeleteDataV2(utils.MY_CHART_INDEX_NAME, id); e != nil {
  258. errMsg = append(errMsg, fmt.Sprintf("myChartId:%d,删除Es失败,err:%s", v, e.Error()))
  259. }
  260. }
  261. return
  262. }