chart_info.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_forum_hub/models"
  6. "eta/eta_forum_hub/models/chart_collect"
  7. "eta/eta_forum_hub/models/system"
  8. "eta/eta_forum_hub/services/alarm_msg"
  9. "eta/eta_forum_hub/utils"
  10. "fmt"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // AddChartInfo 添加图表
  17. func AddChartInfo(req *models.AddChartReq, sysUserId int, sysUserRealName string) (chartInfo *models.ChartInfo, err error, errMsg string, isSendEmail bool) {
  18. defer func() {
  19. if err != nil {
  20. utils.FileLog.Info("添加图表失败, AddChartInfo Msg: " + errMsg + ", Err: " + err.Error())
  21. go alarm_msg.SendAlarmMsg("添加图表失败, AddChartInfo Msg: "+errMsg+", Err: "+err.Error(), 3)
  22. }
  23. }()
  24. isSendEmail = true // 默认错误的时候要发送邮件
  25. req.ChartInfo.ChartName = strings.Trim(req.ChartInfo.ChartName, " ")
  26. if req.ChartInfo.ChartName == "" {
  27. errMsg = "请填写图表名称!"
  28. err = errors.New(errMsg)
  29. isSendEmail = false
  30. return
  31. }
  32. //判断图表是否存在
  33. var condition string
  34. var pars []interface{}
  35. // todo 判断同一个平台上的图表是否重复
  36. condition += " AND chart_info_id=?"
  37. pars = append(pars, req.ChartInfo.ChartInfoId)
  38. count, err := models.GetChartInfoCountByCondition(condition, pars)
  39. if err != nil {
  40. errMsg = "判断图表名称是否存在失败"
  41. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  42. return
  43. }
  44. if count > 0 {
  45. errMsg = "图表已存在"
  46. err = errors.New("图表已存在")
  47. isSendEmail = false
  48. return
  49. }
  50. // 添加指标数据
  51. err = BatchAddOrUpdateEdbData(req.EdbInfoDataList)
  52. if err != nil {
  53. errMsg = "添加指标数据失败"
  54. err = errors.New("添加指标数据失败,Err:" + err.Error())
  55. return
  56. }
  57. // 查询指标ID和指标编码之间的关系
  58. originEdbInfoIdMap := make(map[int]string)
  59. for _, v := range req.EdbInfoList {
  60. originEdbInfoIdMap[v.EdbInfoId] = v.EdbCode
  61. }
  62. // 添加关联指标
  63. var edbInfoIdArr []int
  64. edbInfoList := make([]*models.EdbInfo, 0)
  65. edbInfoList, err, errMsg, isSendEmail = BatchAddOrUpdateEdbInfo(req.EdbInfoList, req.EdbInfoCalculateMapping, sysUserId, sysUserRealName)
  66. if err != nil {
  67. errMsg = "添加关联指标失败"
  68. err = errors.New("添加关联指标失败,Err:" + err.Error())
  69. return
  70. }
  71. edbCodeMap := make(map[string]*models.EdbInfo, 0)
  72. for _, v := range edbInfoList {
  73. edbCodeMap[v.EdbCode] = v
  74. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  75. }
  76. // todo 批量新增指标
  77. sort.Ints(edbInfoIdArr)
  78. var edbInfoIdArrStr []string
  79. for _, v := range edbInfoIdArr {
  80. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  81. }
  82. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  83. chartInfo = new(models.ChartInfo)
  84. chartInfo.SysUserId = req.ChartInfo.SysUserId
  85. chartInfo.SysUserRealName = req.ChartInfo.SysUserRealName
  86. // 查询创建者信息
  87. if req.CreatorInfo != nil {
  88. creator, _ := system.GetAdminByAdminName(req.CreatorInfo.AdminName)
  89. if creator != nil {
  90. chartInfo.SysUserId = creator.AdminId
  91. chartInfo.SysUserRealName = creator.RealName
  92. }
  93. }
  94. // 查询上传者信息
  95. if req.UploaderInfo != nil {
  96. uploader, _ := system.GetAdminByAdminName(req.UploaderInfo.AdminName)
  97. if uploader != nil {
  98. chartInfo.UploadUserId = uploader.AdminId
  99. chartInfo.UploadUserRealName = uploader.RealName
  100. }
  101. }
  102. chartInfo.ChartInfoId = req.ChartInfo.ChartInfoId
  103. chartInfo.ChartName = req.ChartInfo.ChartName
  104. chartInfo.ChartClassifyId = req.ChartInfo.ChartClassifyId
  105. chartInfo.EdbInfoIds = edbInfoIdStr
  106. chartInfo.CreateTime = time.Now()
  107. chartInfo.ModifyTime = time.Now()
  108. chartInfo.IsSetName = req.ChartInfo.IsSetName
  109. //timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  110. // todo 是否需要单独生成一个指标图表序列ID
  111. //chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  112. chartInfo.UniqueCode = req.ChartInfo.UniqueCode
  113. chartInfo.DateType = req.ChartInfo.DateType
  114. chartInfo.ChartType = req.ChartInfo.ChartType
  115. calendar := req.ChartInfo.Calendar
  116. chartInfo.Calendar = calendar
  117. chartInfo.StartDate = req.ChartInfo.StartDate
  118. chartInfo.EndDate = req.ChartInfo.EndDate
  119. chartInfo.SeasonStartDate = req.ChartInfo.StartDate
  120. chartInfo.SeasonEndDate = req.ChartInfo.EndDate
  121. chartInfo.LeftMin = req.ChartInfo.LeftMin
  122. chartInfo.LeftMax = req.ChartInfo.LeftMax
  123. chartInfo.RightMin = req.ChartInfo.RightMin
  124. chartInfo.RightMax = req.ChartInfo.RightMax
  125. chartInfo.Right2Min = req.ChartInfo.Right2Min
  126. chartInfo.Right2Max = req.ChartInfo.Right2Max
  127. chartInfo.MinMaxSave = req.ChartInfo.MinMaxSave
  128. chartInfo.Disabled = req.ChartInfo.Disabled
  129. chartInfo.BarConfig = req.ChartInfo.BarConfig
  130. chartInfo.ExtraConfig = req.ChartInfo.ExtraConfig
  131. chartInfo.SeasonExtraConfig = req.ChartInfo.SeasonExtraConfig
  132. chartInfo.StartYear = req.ChartInfo.StartYear
  133. chartInfo.Source = req.ChartInfo.Source
  134. chartInfo.ChartThemeId = req.ChartInfo.ChartThemeId
  135. chartInfo.SourcesFrom = req.ChartInfo.SourcesFrom
  136. chartInfo.Instructions = req.ChartInfo.Instructions
  137. chartInfo.MarkersLines = req.ChartInfo.MarkersLines
  138. chartInfo.MarkersAreas = req.ChartInfo.MarkersAreas
  139. chartInfo.Unit = req.ChartInfo.Unit
  140. chartInfo.UnitEn = req.ChartInfo.UnitEn
  141. chartInfo.ChartImage = req.ChartInfo.ChartImage
  142. chartInfo.Sort = req.ChartInfo.Sort
  143. /*newId, err := models.AddChartInfo(chartInfo)
  144. if err != nil {
  145. errMsg = `保存失败`
  146. err = errors.New("保存失败,Err:" + err.Error())
  147. return
  148. }
  149. chartInfo.ChartInfoId = int(newId)*/
  150. // todo 先整理好指标信息,生成指标ID,再将图表和指标ID进行绑定
  151. chartEdbInfoList := req.ChartEdbMapping
  152. mapList := make([]*models.ChartEdbMapping, 0)
  153. newEdbInfoIdMap := make(map[int]int) // ETA系统和ETA社区指标ID的映射关系
  154. for _, v := range chartEdbInfoList {
  155. mapItem := new(models.ChartEdbMapping)
  156. //mapItem.ChartInfoId = int(newId)
  157. edbCode, ok := originEdbInfoIdMap[v.EdbInfoId]
  158. if !ok {
  159. err = fmt.Errorf("指标代码不存在,EdbCode:%s", v.EdbCode)
  160. errMsg = "指标代码不存在"
  161. return
  162. }
  163. edbBase, ok := edbCodeMap[edbCode]
  164. if !ok {
  165. err = fmt.Errorf("指标代码不存在,EdbCode:%s", v.EdbCode)
  166. errMsg = "指标代码不存在"
  167. return
  168. }
  169. mapItem.EdbInfoId = edbBase.EdbInfoId
  170. mapItem.CreateTime = time.Now()
  171. mapItem.ModifyTime = time.Now()
  172. //timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  173. //mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", edbBase.EdbInfoId, "_", timestamp))
  174. mapItem.UniqueCode = v.UniqueCode
  175. mapItem.MaxData = v.MaxData
  176. mapItem.MinData = v.MinData
  177. mapItem.IsOrder = v.IsOrder
  178. mapItem.IsAxis = v.IsAxis
  179. mapItem.EdbInfoType = v.EdbInfoType
  180. mapItem.LeadValue = v.LeadValue
  181. mapItem.LeadUnit = v.LeadUnit
  182. mapItem.ChartStyle = v.ChartStyle
  183. mapItem.ChartColor = v.ChartColor
  184. mapItem.PredictChartColor = v.PredictChartColor
  185. mapItem.ChartWidth = v.ChartWidth
  186. mapItem.Source = v.Source
  187. mapItem.EdbAliasName = v.EdbAliasName
  188. mapItem.IsConvert = v.IsConvert
  189. mapItem.ConvertType = v.ConvertType
  190. mapItem.ConvertValue = v.ConvertValue
  191. mapItem.ConvertUnit = v.ConvertUnit
  192. mapItem.ConvertEnUnit = v.ConvertEnUnit
  193. mapItem.EdbCode = edbCode
  194. mapList = append(mapList, mapItem)
  195. newEdbInfoIdMap[v.EdbInfoId] = mapItem.EdbInfoId
  196. }
  197. if len(mapList) <= 0 {
  198. errMsg = `图表指标映射不能为空`
  199. err = errors.New("图表指标映射不能为空")
  200. return
  201. }
  202. /*err = models.AddChartEdbMapping(mapList)
  203. if err != nil {
  204. errMsg = `保存失败`
  205. err = errors.New("保存失败,Err:" + err.Error())
  206. return
  207. }*/
  208. extraConfig := chartInfo.ExtraConfig
  209. barConfig := chartInfo.BarConfig
  210. updateExtraConfigFlag := false
  211. //更新图表配置里的config信息
  212. e, _ := transferChartConfigEdbInfoId(chartInfo, newEdbInfoIdMap)
  213. if e == nil {
  214. //更新图表的特殊字段
  215. if chartInfo.ExtraConfig != extraConfig || chartInfo.BarConfig != barConfig {
  216. updateExtraConfigFlag = true
  217. /*err = chartInfo.Update([]string{"ExtraConfig", "BarConfig"})
  218. if err != nil {
  219. errMsg = `保存失败`
  220. err = errors.New("保存图表配置失败,Err:" + err.Error())
  221. return
  222. }*/
  223. }
  224. }
  225. err = models.AddChartInfoAndMapping(chartInfo, mapList, updateExtraConfigFlag)
  226. if err != nil {
  227. errMsg = `保存失败`
  228. err = errors.New("保存图表失败,Err:" + err.Error())
  229. return
  230. }
  231. if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  232. err = models.AddChartSeriesAndEdbMapping(req.ChartSeries, req.ChartSeriesEdbMapping, chartInfo.ChartInfoId, newEdbInfoIdMap)
  233. if err != nil {
  234. errMsg = `保存失败`
  235. err = errors.New("保存失败,Err:" + err.Error())
  236. return
  237. }
  238. }
  239. // 添加图表简介
  240. err, errMsg = addOrUpdateChartDescriptionList(chartInfo.ChartInfoId, req.Description)
  241. if err != nil {
  242. errMsg = "保存失败"
  243. err = errors.New("保存失败,Err:" + err.Error())
  244. return
  245. }
  246. //添加es数据
  247. go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  248. return
  249. }
  250. // UpdateChartInfoAndEdbInfo 刷新图表信息和指标信息
  251. func UpdateChartInfoAndEdbInfo(req *models.AddChartReq, sysUserId int, sysUserRealName string) (chartInfo *models.ChartInfo, err error, errMsg string, isSendEmail bool) {
  252. defer func() {
  253. if err != nil {
  254. utils.FileLog.Info("刷新图表信息和指标信息失败, AddChartInfo Msg: " + errMsg + ", Err: " + err.Error())
  255. go alarm_msg.SendAlarmMsg("刷新图表信息和指标信息失败, AddChartInfo Msg: "+errMsg+", Err: "+err.Error(), 3)
  256. }
  257. }()
  258. isSendEmail = true // 默认错误的时候要发送邮件
  259. req.ChartInfo.ChartName = strings.Trim(req.ChartInfo.ChartName, " ")
  260. if req.ChartInfo.ChartName == "" {
  261. errMsg = "请填写图表名称!"
  262. err = errors.New(errMsg)
  263. isSendEmail = false
  264. return
  265. }
  266. chartInfo, err = models.GetChartInfoById(req.ChartInfo.ChartInfoId)
  267. if err != nil {
  268. if err.Error() == utils.ErrNoRow() {
  269. errMsg = "图表已被删除,请刷新页面"
  270. err = errors.New(errMsg)
  271. isSendEmail = false
  272. return
  273. }
  274. errMsg = "获取图表信息失败"
  275. err = errors.New("获取图表信息失败,Err:" + err.Error())
  276. return
  277. }
  278. //判断图表是否存在
  279. var condition string
  280. var pars []interface{}
  281. // todo 判断同一个平台上的图表是否重复
  282. condition += " AND chart_name=? AND source = ? AND chart_info_id<>? "
  283. pars = append(pars, req.ChartInfo.ChartName, req.ChartInfo.Source, req.ChartInfo.ChartInfoId)
  284. count, err := models.GetChartInfoCountByCondition(condition, pars)
  285. if err != nil {
  286. errMsg = "判断图表名称是否存在失败"
  287. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  288. return
  289. }
  290. if count > 0 {
  291. errMsg = "图表已存在"
  292. err = errors.New("图表已存在")
  293. isSendEmail = false
  294. return
  295. }
  296. // 添加指标数据
  297. err = BatchAddOrUpdateEdbData(req.EdbInfoDataList)
  298. if err != nil {
  299. errMsg = "添加指标数据失败"
  300. err = errors.New("添加指标数据失败,Err:" + err.Error())
  301. return
  302. }
  303. // 查询指标ID和指标编码之间的关系
  304. originEdbInfoIdMap := make(map[int]string)
  305. for _, v := range req.EdbInfoList {
  306. originEdbInfoIdMap[v.EdbInfoId] = v.EdbCode
  307. }
  308. // 添加关联指标
  309. var edbInfoIdArr []int
  310. edbInfoList := make([]*models.EdbInfo, 0)
  311. edbInfoList, err, errMsg, isSendEmail = BatchAddOrUpdateEdbInfo(req.EdbInfoList, req.EdbInfoCalculateMapping, sysUserId, sysUserRealName)
  312. if err != nil {
  313. errMsg = "添加关联指标失败"
  314. err = errors.New("添加关联指标失败,Err:" + err.Error())
  315. return
  316. }
  317. edbCodeMap := make(map[string]*models.EdbInfo, 0)
  318. for _, v := range edbInfoList {
  319. edbCodeMap[v.EdbCode] = v
  320. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  321. }
  322. // todo 批量新增指标
  323. sort.Ints(edbInfoIdArr)
  324. var edbInfoIdArrStr []string
  325. for _, v := range edbInfoIdArr {
  326. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  327. }
  328. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  329. chartInfo.ChartName = req.ChartInfo.ChartName
  330. chartInfo.ChartClassifyId = req.ChartInfo.ChartClassifyId
  331. chartInfo.EdbInfoIds = edbInfoIdStr
  332. chartInfo.ModifyTime = time.Now()
  333. chartInfo.IsSetName = req.ChartInfo.IsSetName
  334. chartInfo.DateType = req.ChartInfo.DateType
  335. chartInfo.ChartType = req.ChartInfo.ChartType
  336. calendar := req.ChartInfo.Calendar
  337. chartInfo.Calendar = calendar
  338. chartInfo.StartDate = req.ChartInfo.StartDate
  339. chartInfo.EndDate = req.ChartInfo.EndDate
  340. chartInfo.SeasonStartDate = req.ChartInfo.StartDate
  341. chartInfo.SeasonEndDate = req.ChartInfo.EndDate
  342. chartInfo.LeftMin = req.ChartInfo.LeftMin
  343. chartInfo.LeftMax = req.ChartInfo.LeftMax
  344. chartInfo.RightMin = req.ChartInfo.RightMin
  345. chartInfo.RightMax = req.ChartInfo.RightMax
  346. chartInfo.Right2Min = req.ChartInfo.Right2Min
  347. chartInfo.Right2Max = req.ChartInfo.Right2Max
  348. chartInfo.MinMaxSave = req.ChartInfo.MinMaxSave
  349. chartInfo.Disabled = req.ChartInfo.Disabled
  350. chartInfo.BarConfig = req.ChartInfo.BarConfig
  351. chartInfo.ExtraConfig = req.ChartInfo.ExtraConfig
  352. chartInfo.SeasonExtraConfig = req.ChartInfo.SeasonExtraConfig
  353. chartInfo.StartYear = req.ChartInfo.StartYear
  354. chartInfo.Source = req.ChartInfo.Source
  355. chartInfo.ChartThemeId = req.ChartInfo.ChartThemeId
  356. chartInfo.SourcesFrom = req.ChartInfo.SourcesFrom
  357. chartInfo.Instructions = req.ChartInfo.Instructions
  358. chartInfo.MarkersLines = req.ChartInfo.MarkersLines
  359. chartInfo.MarkersAreas = req.ChartInfo.MarkersAreas
  360. chartInfo.Unit = req.ChartInfo.Unit
  361. chartInfo.UnitEn = req.ChartInfo.UnitEn
  362. chartInfo.ChartImage = req.ChartInfo.ChartImage
  363. chartInfo.SysUserId = req.ChartInfo.SysUserId
  364. chartInfo.SysUserRealName = req.ChartInfo.SysUserRealName
  365. chartInfo.Sort = req.ChartInfo.Sort
  366. // 查询创建者信息
  367. if req.CreatorInfo != nil {
  368. creator, _ := system.GetAdminByAdminName(req.CreatorInfo.AdminName)
  369. if creator != nil {
  370. chartInfo.SysUserId = creator.AdminId
  371. chartInfo.SysUserRealName = creator.RealName
  372. }
  373. }
  374. /*err = chartInfo.Update([]string{})
  375. if err != nil {
  376. errMsg = `更新失败`
  377. err = errors.New("更新失败,Err:" + err.Error())
  378. return
  379. }*/
  380. // 处理映射,已存在的映射直接更新,不存在的映射再新增,同时删除旧的映射关系
  381. oldChartEdbMappingList, err := models.GetChartEdbMappingListV2(chartInfo.ChartInfoId)
  382. oldMappingIdMap := make(map[string]*models.ChartEdbMapping)
  383. removeMapping := make(map[int]struct{})
  384. for _, v := range oldChartEdbMappingList {
  385. name := fmt.Sprintf("%s-%d", v.EdbCode, v.Source)
  386. oldMappingIdMap[name] = v
  387. removeMapping[v.ChartEdbMappingId] = struct{}{}
  388. }
  389. // todo 先整理好指标信息,生成指标ID,再将图表和指标ID进行绑定
  390. chartEdbInfoList := req.ChartEdbMapping
  391. addMappingList := make([]*models.ChartEdbMapping, 0)
  392. updateMappingList := make([]*models.ChartEdbMapping, 0)
  393. newEdbInfoIdMap := make(map[int]int) // ETA系统和ETA社区指标ID的映射关系
  394. for _, v := range chartEdbInfoList {
  395. mapItem := new(models.ChartEdbMapping)
  396. mapItem.ChartInfoId = chartInfo.ChartInfoId
  397. edbCode, ok := originEdbInfoIdMap[v.EdbInfoId]
  398. if !ok {
  399. err = fmt.Errorf("指标代码不存在,EdbCode:%s", v.EdbCode)
  400. errMsg = "指标代码不存在"
  401. return
  402. }
  403. edbBase, ok := edbCodeMap[edbCode]
  404. if !ok {
  405. err = fmt.Errorf("指标代码不存在,EdbCode:%s", v.EdbCode)
  406. errMsg = "指标代码不存在"
  407. return
  408. }
  409. // 判断是否已经存在映射关系
  410. existName := fmt.Sprintf("%s-%d", edbCode, v.Source)
  411. if oldMapping, ok1 := oldMappingIdMap[existName]; ok1 {
  412. //更新映射关系
  413. mapItem = oldMapping
  414. mapItem.ModifyTime = time.Now()
  415. mapItem.MaxData = v.MaxData
  416. mapItem.MinData = v.MinData
  417. mapItem.IsOrder = v.IsOrder
  418. mapItem.IsAxis = v.IsAxis
  419. mapItem.EdbInfoType = v.EdbInfoType
  420. mapItem.LeadValue = v.LeadValue
  421. mapItem.LeadUnit = v.LeadUnit
  422. mapItem.ChartStyle = v.ChartStyle
  423. mapItem.ChartColor = v.ChartColor
  424. mapItem.PredictChartColor = v.PredictChartColor
  425. mapItem.ChartWidth = v.ChartWidth
  426. mapItem.Source = v.Source
  427. mapItem.EdbAliasName = v.EdbAliasName
  428. mapItem.IsConvert = v.IsConvert
  429. mapItem.ConvertType = v.ConvertType
  430. mapItem.ConvertValue = v.ConvertValue
  431. mapItem.ConvertUnit = v.ConvertUnit
  432. mapItem.ConvertEnUnit = v.ConvertEnUnit
  433. mapItem.EdbCode = edbCode
  434. /*err = mapItem.Update([]string{})
  435. if err != nil {
  436. errMsg = `更新失败`
  437. err = errors.New("更新图表指标映射关系失败,Err:" + err.Error())
  438. return
  439. }*/
  440. updateMappingList = append(updateMappingList, mapItem)
  441. delete(removeMapping, mapItem.ChartEdbMappingId)
  442. } else {
  443. mapItem.EdbInfoId = edbBase.EdbInfoId
  444. mapItem.CreateTime = time.Now()
  445. mapItem.ModifyTime = time.Now()
  446. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  447. mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", edbBase.EdbInfoId, "_", timestamp))
  448. mapItem.MaxData = v.MaxData
  449. mapItem.MinData = v.MinData
  450. mapItem.IsOrder = v.IsOrder
  451. mapItem.IsAxis = v.IsAxis
  452. mapItem.EdbInfoType = v.EdbInfoType
  453. mapItem.LeadValue = v.LeadValue
  454. mapItem.LeadUnit = v.LeadUnit
  455. mapItem.ChartStyle = v.ChartStyle
  456. mapItem.ChartColor = v.ChartColor
  457. mapItem.PredictChartColor = v.PredictChartColor
  458. mapItem.ChartWidth = v.ChartWidth
  459. mapItem.Source = v.Source
  460. mapItem.EdbAliasName = v.EdbAliasName
  461. mapItem.IsConvert = v.IsConvert
  462. mapItem.ConvertType = v.ConvertType
  463. mapItem.ConvertValue = v.ConvertValue
  464. mapItem.ConvertUnit = v.ConvertUnit
  465. mapItem.ConvertEnUnit = v.ConvertEnUnit
  466. mapItem.EdbCode = edbCode
  467. addMappingList = append(addMappingList, mapItem)
  468. }
  469. newEdbInfoIdMap[v.EdbInfoId] = mapItem.EdbInfoId
  470. }
  471. /*if len(addMappingList) > 0 {
  472. err = models.AddChartEdbMapping(addMappingList)
  473. if err != nil {
  474. errMsg = `保存失败`
  475. err = errors.New("保存失败,Err:" + err.Error())
  476. return
  477. }
  478. }*/
  479. // 删除原先的绑定关系
  480. removeIdStr := ""
  481. if len(removeMapping) > 0 {
  482. removeIdList := make([]string, 0) //需要移除的日期
  483. for k, _ := range removeMapping {
  484. removeIdList = append(removeIdList, fmt.Sprint(k))
  485. }
  486. removeIdStr = strings.Join(removeIdList, `","`)
  487. removeIdStr = `"` + removeIdStr + `"`
  488. /*err = models.DeleteChartEdbMappingByChartEdbMappingId(removeIdStr)
  489. if err != nil {
  490. errMsg = `删除失败`
  491. err = errors.New("删除失败,Err:" + err.Error())
  492. return
  493. }*/
  494. }
  495. extraConfig := chartInfo.ExtraConfig
  496. barConfig := chartInfo.BarConfig
  497. updateExtraConfigFlag := false
  498. //更新图表配置里的config信息
  499. e, _ := transferChartConfigEdbInfoId(chartInfo, newEdbInfoIdMap)
  500. if e == nil {
  501. //更新图表的特殊字段
  502. if chartInfo.ExtraConfig != extraConfig || chartInfo.BarConfig != barConfig {
  503. updateExtraConfigFlag = true
  504. /*err = chartInfo.Update([]string{"ExtraConfig", "BarConfig"})
  505. if err != nil {
  506. errMsg = `保存失败`
  507. err = errors.New("保存图表配置失败,Err:" + err.Error())
  508. return
  509. }*/
  510. }
  511. }
  512. err = models.ModifyChartInfoAndMapping(chartInfo, addMappingList, updateMappingList, removeIdStr, updateExtraConfigFlag)
  513. if err != nil {
  514. errMsg = `保存失败`
  515. err = errors.New("保存图表配置失败,Err:" + err.Error())
  516. return
  517. }
  518. if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  519. err = models.EditChartSeriesAndEdbMapping(req.ChartSeries, req.ChartSeriesEdbMapping, chartInfo.ChartInfoId, newEdbInfoIdMap)
  520. if err != nil {
  521. errMsg = "保存失败"
  522. err = errors.New("保存失败,Err:" + err.Error())
  523. return
  524. }
  525. }
  526. // 更新图表简介
  527. err, errMsg = addOrUpdateChartDescriptionList(chartInfo.ChartInfoId, req.Description)
  528. if err != nil {
  529. errMsg = "保存失败"
  530. err = errors.New("保存失败,Err:" + err.Error())
  531. return
  532. }
  533. //添加es数据
  534. go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  535. go EsAddOrEditChartCollectByChartInfoId(chartInfo.ChartInfoId)
  536. return
  537. }
  538. // 删除图表信息
  539. func DeleteChart(chartInfoId int) (err error, errMsg string) {
  540. //删除图表
  541. chartInfo, err := models.GetChartInfoById(chartInfoId)
  542. if err != nil {
  543. if err.Error() == utils.ErrNoRow() {
  544. errMsg = "图表已删除,请刷新页面"
  545. err = fmt.Errorf("图表不存在,Err:" + err.Error())
  546. return
  547. } else {
  548. errMsg = "删除失败"
  549. err = fmt.Errorf("删除失败, 获取图表信息失败Err:" + err.Error())
  550. return
  551. }
  552. }
  553. if chartInfo == nil {
  554. errMsg = "图表已删除,请刷新页面"
  555. err = fmt.Errorf(errMsg)
  556. return
  557. }
  558. // 处理映射,已存在的映射直接更新,不存在的映射再新增,同时删除旧的映射关系
  559. oldChartEdbMappingList, err := models.GetChartEdbMappingListV2(chartInfo.ChartInfoId)
  560. edbInfoIds := make([]int, 0)
  561. for _, v := range oldChartEdbMappingList {
  562. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  563. }
  564. //删除图表及关联指标
  565. err = models.DeleteChartInfoAndData(chartInfo.ChartInfoId)
  566. if err != nil {
  567. errMsg = "删除失败"
  568. err = fmt.Errorf("删除图表数据失败, Err:" + err.Error())
  569. return
  570. }
  571. if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  572. err = models.DeleteChartSeriesAndEdbMapping(chartInfo.ChartInfoId)
  573. if err != nil {
  574. errMsg = "删除失败"
  575. err = fmt.Errorf("删除图表系列数据失败, Err:" + err.Error())
  576. return
  577. }
  578. }
  579. err, errMsg = BatchDeleteEdbInfo(edbInfoIds)
  580. if err != nil {
  581. errMsg = "删除失败"
  582. err = fmt.Errorf("删除指标数据失败, Err:" + err.Error())
  583. return
  584. }
  585. // 删除图表简介
  586. descriptionList := make([]*models.ChartDescriptionReq, 0)
  587. err, errMsg = addOrUpdateChartDescriptionList(chartInfo.ChartInfoId, descriptionList)
  588. if err != nil {
  589. errMsg = "删除失败"
  590. err = errors.New("删除图表简介失败,Err:" + err.Error())
  591. return
  592. }
  593. go EsDeleteChartInfo(chartInfo.ChartInfoId)
  594. go func() {
  595. var e error
  596. defer func() {
  597. if e != nil {
  598. utils.FileLog.Info("删除图表收藏数据失败, Err:" + e.Error())
  599. }
  600. }()
  601. // 查询所有的收藏图表数据
  602. userChartCollect := make([]string, 0)
  603. chartCollectList, e := chart_collect.GetChartCollectListByChartInfoId(chartInfo.ChartInfoId)
  604. if e != nil {
  605. e = fmt.Errorf("查询图表收藏数据失败, Err:" + e.Error())
  606. return
  607. }
  608. for _, v := range chartCollectList {
  609. userChartCollect = append(userChartCollect, fmt.Sprintf("%d_%d", v.UserId, v.ChartInfoId))
  610. }
  611. if len(userChartCollect) > 0 {
  612. EsDeleteChartCollectByChartInfoIdUserIds(userChartCollect)
  613. }
  614. }()
  615. return
  616. }
  617. func transferChartConfigEdbInfoId(chartInfo *models.ChartInfo, newEdbInfoIdMap map[int]int) (err error, errMsg string) {
  618. chartType := chartInfo.ChartType
  619. switch chartType {
  620. case 7: // 柱形图
  621. var barConfig models.BarChartInfoReq
  622. if chartInfo.BarConfig == `` {
  623. errMsg = "柱方图未配置"
  624. err = errors.New(errMsg)
  625. return
  626. }
  627. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  628. if err != nil {
  629. errMsg = "柱方图配置异常"
  630. err = errors.New(errMsg)
  631. return
  632. }
  633. //替换里面的edbInfoId
  634. for i := 0; i < len(barConfig.EdbInfoIdList); i++ {
  635. if newId, ok := newEdbInfoIdMap[barConfig.EdbInfoIdList[i].EdbInfoId]; ok {
  636. barConfig.EdbInfoIdList[i].EdbInfoId = newId
  637. }
  638. }
  639. for i := 0; i < len(barConfig.XEdbList); i++ {
  640. if newId, ok := newEdbInfoIdMap[barConfig.XEdbList[i].EdbInfoId]; ok {
  641. barConfig.XEdbList[i].EdbInfoId = newId
  642. }
  643. }
  644. for i := 0; i < len(barConfig.YEdbList); i++ {
  645. if newId, ok := newEdbInfoIdMap[barConfig.YEdbList[i].EdbInfoId]; ok {
  646. barConfig.YEdbList[i].EdbInfoId = newId
  647. }
  648. }
  649. configStr, e := json.Marshal(barConfig)
  650. if e != nil {
  651. errMsg = "柱方图配置异常"
  652. err = errors.New(errMsg + "json.Marshal 失败" + e.Error())
  653. return
  654. }
  655. chartInfo.BarConfig = string(configStr)
  656. return
  657. case 10: // 截面散点图
  658. var tmpExtraConfig models.SectionScatterReq
  659. if chartInfo.ExtraConfig == `` {
  660. errMsg = "截面散点图未配置"
  661. err = errors.New(errMsg)
  662. return
  663. }
  664. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &tmpExtraConfig)
  665. if err != nil {
  666. errMsg = "截面散点配置异常"
  667. err = errors.New(errMsg)
  668. return
  669. }
  670. //替换里面的edbInfoId
  671. for i := 0; i < len(tmpExtraConfig.SeriesList); i++ {
  672. for j := 0; j < len(tmpExtraConfig.SeriesList[i].EdbInfoList); j++ {
  673. if newId, ok := newEdbInfoIdMap[tmpExtraConfig.SeriesList[i].EdbInfoList[j].XEdbInfoId]; ok {
  674. tmpExtraConfig.SeriesList[i].EdbInfoList[j].XEdbInfoId = newId
  675. }
  676. if newId, ok := newEdbInfoIdMap[tmpExtraConfig.SeriesList[i].EdbInfoList[j].YEdbInfoId]; ok {
  677. tmpExtraConfig.SeriesList[i].EdbInfoList[j].YEdbInfoId = newId
  678. }
  679. }
  680. }
  681. configStr, e := json.Marshal(tmpExtraConfig)
  682. if e != nil {
  683. errMsg = "截面散点配置异常"
  684. err = errors.New(errMsg + "json.Marshal 失败" + e.Error())
  685. return
  686. }
  687. chartInfo.ExtraConfig = string(configStr)
  688. return
  689. case utils.CHART_TYPE_SECTION_COMBINE:
  690. // 预览和详情都走这个接口
  691. var sectionExtraConfig models.ChartSectionAllExtraConf
  692. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &sectionExtraConfig)
  693. if err != nil {
  694. errMsg = "截面组合图配置异常"
  695. err = errors.New(errMsg)
  696. return
  697. }
  698. //替换里面的edbInfoId
  699. for i := 0; i < len(sectionExtraConfig.DateConfList); i++ {
  700. if newId, ok := newEdbInfoIdMap[sectionExtraConfig.DateConfList[i].EdbInfoId]; ok {
  701. sectionExtraConfig.DateConfList[i].EdbInfoId = newId
  702. }
  703. }
  704. configStr, e := json.Marshal(sectionExtraConfig)
  705. if e != nil {
  706. errMsg = "截面组合图配置异常"
  707. err = errors.New(errMsg + "json.Marshal 失败" + e.Error())
  708. return
  709. }
  710. chartInfo.ExtraConfig = string(configStr)
  711. }
  712. return
  713. }
  714. func addOrUpdateChartDescriptionList(chartInfoId int, reqDescription []*models.ChartDescriptionReq) (err error, errMsg string) {
  715. descriptionList := make([]*models.ChartDescription, 0)
  716. if len(reqDescription) == 0 {
  717. err = models.BatchUpdateChartDescription(chartInfoId, descriptionList)
  718. if err != nil {
  719. errMsg = "保存失败"
  720. err = errors.New("保存失败,Err:" + err.Error())
  721. return
  722. }
  723. }
  724. // 更换成社区的创建人
  725. oldAdminIdMap := make(map[string]int)
  726. adminNames := make([]string, 0)
  727. for _, v := range reqDescription {
  728. adminNames = append(adminNames, v.AdminName)
  729. }
  730. if len(adminNames) > 0 {
  731. adminList, e := system.GetAdminListByAdminName(adminNames)
  732. if e != nil {
  733. errMsg = "获取创建人信息失败"
  734. err = errors.New("获取创建人信息失败,Err:" + e.Error())
  735. return
  736. }
  737. for _, v := range reqDescription {
  738. for _, admin := range adminList {
  739. oldAdminIdMap[v.AdminName] = admin.AdminId
  740. }
  741. }
  742. }
  743. for _, v := range reqDescription {
  744. if oldAdminId, ok := oldAdminIdMap[v.AdminName]; ok {
  745. v.SysUserId = oldAdminId
  746. }else{
  747. v.SysUserId = 0
  748. }
  749. descriptionList = append(descriptionList, &models.ChartDescription{
  750. ChartInfoId: chartInfoId,
  751. Description: v.Description,
  752. SysUserId: v.SysUserId,
  753. SysUserRealName: v.AdminName,
  754. ModifyTime: time.Now(),
  755. CreateTime: time.Now(),
  756. })
  757. }
  758. err = models.BatchUpdateChartDescription(chartInfoId, descriptionList)
  759. if err != nil {
  760. errMsg = "保存失败"
  761. err = errors.New("保存失败,Err:" + err.Error())
  762. return
  763. }
  764. return
  765. }