chart_info.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. package line_equation
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/line_equation"
  7. "eta/eta_api/models/data_manage/line_equation/request"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "math"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/shopspring/decimal"
  18. )
  19. type LineEquationResp struct {
  20. AData LineEquationDataResp
  21. BData LineEquationDataResp
  22. R2Data LineEquationDataResp
  23. }
  24. type LineEquationDataResp struct {
  25. MaxData float64
  26. MinData float64
  27. LatestDate string `description:"真实数据的最后日期"`
  28. EdbInfoCategoryType int
  29. ChartColor string
  30. ChartStyle string
  31. PredictChartColor string
  32. ChartType int
  33. ChartWidth int
  34. EdbName string
  35. EdbNameEn string
  36. Unit string
  37. UnitEn string
  38. IsAxis int
  39. DataList []data_manage.EdbDataList
  40. }
  41. // GetChartEdbData 获取图表数据
  42. func GetChartEdbData(chartInfoId int, lineChartInfoConfig request.LineChartInfoReq, getAData, getBData, getR2Data bool) (edbList []*data_manage.ChartEdbInfoMapping, dataResp LineEquationResp, err error, errMsg string) {
  43. // 获取基本信息
  44. mappingList, startDate, endDate, err, errMsg := getConfigData(lineChartInfoConfig)
  45. if err != nil {
  46. return
  47. }
  48. xEdbInfoIdList := lineChartInfoConfig.XEdbInfoIdList
  49. yEdbInfoIdList := lineChartInfoConfig.YEdbInfoIdList
  50. aLineEquationDataResp := LineEquationDataResp{
  51. DataList: make([]data_manage.EdbDataList, 0),
  52. MaxData: 0,
  53. MinData: 0,
  54. ChartColor: "#00f",
  55. ChartStyle: `spline`,
  56. PredictChartColor: `#00f`,
  57. ChartType: 0,
  58. ChartWidth: 3,
  59. EdbName: `弹性系数`,
  60. EdbNameEn: `elastic coefficient`,
  61. IsAxis: 1,
  62. }
  63. bLineEquationDataResp := LineEquationDataResp{
  64. DataList: make([]data_manage.EdbDataList, 0),
  65. MaxData: 0,
  66. MinData: 0,
  67. ChartColor: "#00f",
  68. ChartStyle: `spline`,
  69. PredictChartColor: `#00f`,
  70. ChartType: 0,
  71. ChartWidth: 3,
  72. EdbName: `截距`,
  73. EdbNameEn: `intercept`,
  74. IsAxis: 1,
  75. }
  76. r2LineEquationDataResp := LineEquationDataResp{
  77. DataList: make([]data_manage.EdbDataList, 0),
  78. MaxData: 0,
  79. MinData: 0,
  80. ChartColor: "#00f",
  81. ChartStyle: `spline`,
  82. PredictChartColor: `#00f`,
  83. ChartType: 0,
  84. ChartWidth: 3,
  85. EdbName: `相关系数`,
  86. EdbNameEn: `coefficient of association`,
  87. IsAxis: 1,
  88. }
  89. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  90. dataResp = LineEquationResp{
  91. AData: aLineEquationDataResp,
  92. BData: bLineEquationDataResp,
  93. R2Data: r2LineEquationDataResp,
  94. }
  95. var baseEdbInfo *data_manage.ChartEdbInfoMapping
  96. // 获取确定以哪个指标的日期作为基准日期
  97. {
  98. var xEdbInfo, yEdbInfo *data_manage.ChartEdbInfoMapping
  99. for _, v := range mappingList {
  100. if v.EdbInfoId == xEdbInfoIdList[0] {
  101. xEdbInfo = v
  102. }
  103. if v.EdbInfoId == yEdbInfoIdList[0] {
  104. yEdbInfo = v
  105. }
  106. }
  107. if xEdbInfo == nil {
  108. errMsg = `X轴第一个指标异常`
  109. err = errors.New(errMsg)
  110. return
  111. }
  112. if yEdbInfo == nil {
  113. errMsg = `Y轴第一个指标异常`
  114. err = errors.New(errMsg)
  115. return
  116. }
  117. // 时间截面规则:按照什么频率来取不同的截面的问题。原则是:按照X轴和Y轴所选择的第一个指标(X和Y分别有一个第一个指标),两个指标中,以低频的那个作为基准。如果X轴和Y轴是同频的,那以Y轴第一个指标的为准。
  118. frequencyIntMap := map[string]int{
  119. "日度": 1,
  120. "周度": 2,
  121. "旬度": 3,
  122. "月度": 4,
  123. "季度": 5,
  124. "年度": 6,
  125. }
  126. // 如果x是高频 或者 x与y是同频的,那么就是Y轴的第一个指标为主
  127. if frequencyIntMap[xEdbInfo.Frequency] <= frequencyIntMap[yEdbInfo.Frequency] {
  128. baseEdbInfo = yEdbInfo
  129. } else {
  130. // 否则是X轴的第一个指标是低频
  131. baseEdbInfo = xEdbInfo
  132. }
  133. }
  134. // 指标对应的所有数据
  135. chartType := 1 //1:普通图,2:季节性图
  136. calendar := "公历"
  137. edbDataListMap, edbList, err := data.GetEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, "")
  138. if err != nil {
  139. return
  140. }
  141. // 获取所有的日期
  142. dateList := make([]string, 0)
  143. for _, v := range edbDataListMap[baseEdbInfo.EdbInfoId] {
  144. dateList = append(dateList, v.DataTime)
  145. }
  146. // 数据整理
  147. // [日期][A指标id:值,B指标id:值]
  148. dateEdbMap, err := handleData(baseEdbInfo.EdbInfoId, edbDataListMap)
  149. if err != nil {
  150. return
  151. }
  152. lenX := len(xEdbInfoIdList)
  153. var isNotAFirst, isNotBFirst, isNotR2First bool
  154. for i, date := range dateList {
  155. coordinateData := make([]utils.Coordinate, 0)
  156. for k := 0; k < lenX; k++ {
  157. xVal, ok1 := dateEdbMap[date][xEdbInfoIdList[k]]
  158. yVal, ok2 := dateEdbMap[date][yEdbInfoIdList[k]]
  159. if !ok1 || !ok2 {
  160. continue
  161. }
  162. tmpCoordinate1 := utils.Coordinate{
  163. X: xVal,
  164. Y: yVal,
  165. }
  166. coordinateData = append(coordinateData, tmpCoordinate1)
  167. }
  168. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  169. timestamp := dataTime.UnixNano() / 1e6
  170. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  171. if len(coordinateData) >= 2 {
  172. a, b := utils.GetLinearResult(coordinateData)
  173. if !math.IsNaN(a) && !math.IsNaN(b) && !math.IsInf(a, 0) && !math.IsInf(b, 0) {
  174. if getAData {
  175. a, _ = decimal.NewFromFloat(a).Round(4).Float64()
  176. dataResp.AData.DataList = append(dataResp.AData.DataList, data_manage.EdbDataList{
  177. EdbDataId: i,
  178. EdbInfoId: 0,
  179. DataTime: date,
  180. DataTimestamp: timestamp,
  181. Value: a,
  182. })
  183. if !isNotAFirst {
  184. dataResp.AData.MinData = a
  185. dataResp.AData.MaxData = a
  186. isNotAFirst = true
  187. }
  188. if dataResp.AData.MinData > a {
  189. dataResp.AData.MinData = a
  190. }
  191. if dataResp.AData.MaxData < a {
  192. dataResp.AData.MaxData = a
  193. }
  194. }
  195. if getBData {
  196. b, _ = decimal.NewFromFloat(b).Round(4).Float64()
  197. dataResp.BData.DataList = append(dataResp.BData.DataList, data_manage.EdbDataList{
  198. EdbDataId: i,
  199. EdbInfoId: 0,
  200. DataTime: date,
  201. DataTimestamp: timestamp,
  202. Value: b,
  203. })
  204. if !isNotBFirst {
  205. dataResp.BData.MinData = b
  206. dataResp.BData.MaxData = b
  207. isNotBFirst = true
  208. }
  209. if dataResp.BData.MinData > b {
  210. dataResp.BData.MinData = b
  211. }
  212. if dataResp.BData.MaxData < b {
  213. dataResp.BData.MaxData = b
  214. }
  215. }
  216. }
  217. // 计算R平方
  218. if getR2Data {
  219. tmpVal := utils.CalculationDecisive(coordinateData)
  220. if math.IsNaN(tmpVal) || math.IsInf(tmpVal, 0) {
  221. continue
  222. }
  223. tmpVal, _ = decimal.NewFromFloat(tmpVal).Round(4).Float64()
  224. dataResp.R2Data.DataList = append(dataResp.R2Data.DataList, data_manage.EdbDataList{
  225. EdbDataId: i,
  226. EdbInfoId: 0,
  227. DataTime: date,
  228. DataTimestamp: timestamp,
  229. Value: tmpVal,
  230. })
  231. if !isNotR2First {
  232. dataResp.R2Data.MinData = tmpVal
  233. dataResp.R2Data.MaxData = tmpVal
  234. isNotR2First = true
  235. }
  236. if dataResp.R2Data.MinData > tmpVal {
  237. dataResp.R2Data.MinData = tmpVal
  238. }
  239. if dataResp.R2Data.MaxData < tmpVal {
  240. dataResp.R2Data.MaxData = tmpVal
  241. }
  242. }
  243. }
  244. }
  245. dataResp.AData.LatestDate = baseEdbInfo.LatestDate
  246. dataResp.AData.EdbInfoCategoryType = baseEdbInfo.EdbInfoCategoryType
  247. dataResp.BData.LatestDate = baseEdbInfo.LatestDate
  248. dataResp.BData.EdbInfoCategoryType = baseEdbInfo.EdbInfoCategoryType
  249. dataResp.R2Data.LatestDate = baseEdbInfo.LatestDate
  250. dataResp.R2Data.EdbInfoCategoryType = baseEdbInfo.EdbInfoCategoryType
  251. return
  252. }
  253. // GetConfigData 获取配置数据
  254. func GetConfigData(lineChartInfoConfig request.LineChartInfoReq) (mappingList []*data_manage.ChartEdbInfoMapping, startDate, endDate string, err error, errMsg string) {
  255. return getConfigData(lineChartInfoConfig)
  256. }
  257. // getConfigData 获取配置数据
  258. func getConfigData(lineChartInfoConfig request.LineChartInfoReq) (mappingList []*data_manage.ChartEdbInfoMapping, startDate, endDate string, err error, errMsg string) {
  259. dateType := lineChartInfoConfig.DateType
  260. switch dateType {
  261. case 1:
  262. startDate = "2000-01-01"
  263. case 2:
  264. startDate = "2010-01-01"
  265. case 3:
  266. startDate = "2015-01-01"
  267. case 4:
  268. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  269. startDate = "2021-01-01"
  270. case 5:
  271. startDate = lineChartInfoConfig.StartDate + "-01"
  272. endDate = lineChartInfoConfig.EndDate + "-01"
  273. case 6:
  274. startDate = lineChartInfoConfig.StartDate + "-01"
  275. case 7:
  276. startDate = "2018-01-01"
  277. case 8:
  278. startDate = "2019-01-01"
  279. case 9:
  280. startDate = "2020-01-01"
  281. case 11:
  282. startDate = "2022-01-01"
  283. case 12:
  284. startDate = "2023-01-01"
  285. case 13:
  286. startDate = "2024-01-01"
  287. }
  288. //指标数据
  289. edbInfoIdList := make([]int, 0)
  290. {
  291. edbInfoIdMap := make(map[int]int, 0)
  292. for _, edbInfoId := range lineChartInfoConfig.XEdbInfoIdList {
  293. edbInfoIdMap[edbInfoId] = edbInfoId
  294. }
  295. for _, edbInfoId := range lineChartInfoConfig.YEdbInfoIdList {
  296. edbInfoIdMap[edbInfoId] = edbInfoId
  297. }
  298. for _, edbInfoId := range edbInfoIdMap {
  299. edbInfoIdList = append(edbInfoIdList, edbInfoId)
  300. }
  301. }
  302. mappingList, err = data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  303. if err != nil {
  304. errMsg = `获取失败`
  305. err = errors.New("获取图表,指标信息失败,Err:" + err.Error())
  306. return
  307. }
  308. return
  309. }
  310. // handleData 数据处理
  311. func handleData(baseEdbInfoId int, edbDataListMap map[int][]*data_manage.EdbDataList) (dateEdbMap map[string]map[int]float64, err error) {
  312. dateEdbMap = make(map[string]map[int]float64) // [日期][A指标id:值,B指标id:值]
  313. for edbInfoId, edbDataList := range edbDataListMap {
  314. if edbInfoId != baseEdbInfoId {
  315. // 用上期的数据补充当期的数据处理
  316. handleDataMap := make(map[string]float64)
  317. err = data_manage.HandleDataByPreviousData(edbDataList, handleDataMap)
  318. if err != nil {
  319. return
  320. }
  321. for date, val := range handleDataMap {
  322. item, ok := dateEdbMap[date]
  323. if ok {
  324. item[edbInfoId] = val
  325. } else {
  326. item = map[int]float64{
  327. edbInfoId: val,
  328. }
  329. }
  330. dateEdbMap[date] = item
  331. }
  332. } else {
  333. for _, edbData := range edbDataList {
  334. item, ok := dateEdbMap[edbData.DataTime]
  335. if ok {
  336. item[edbInfoId] = edbData.Value
  337. } else {
  338. item = map[int]float64{
  339. edbInfoId: edbData.Value,
  340. }
  341. }
  342. dateEdbMap[edbData.DataTime] = item
  343. }
  344. }
  345. }
  346. return
  347. }
  348. // BatchAddChartInfo 批量添加图表
  349. func BatchAddChartInfo(batchAddChartReq []request.AddChart, lineChartInfoConfig request.LineChartInfoReq, sysUser *system.Admin, lang string) (batchAddChartList []line_equation.BatchAddChart, err error, errMsg string, isSendEmail bool) {
  350. source := utils.CHART_SOURCE_LINE_EQUATION
  351. // 获取需要
  352. for k, v := range batchAddChartReq {
  353. v.ChartName = strings.Trim(v.ChartName, " ")
  354. batchAddChartReq[k] = v
  355. if v.ChartName == "" {
  356. errMsg = "请填写图表名称!"
  357. err = errors.New(errMsg)
  358. isSendEmail = false
  359. return
  360. }
  361. if v.ChartClassifyId <= 0 {
  362. errMsg = "请填写图表名称!"
  363. err = errors.New(errMsg)
  364. isSendEmail = false
  365. return
  366. }
  367. chartClassify, tmpErr := data_manage.GetChartClassifyById(v.ChartClassifyId)
  368. if tmpErr != nil {
  369. if tmpErr.Error() == utils.ErrNoRow() {
  370. errMsg = "分类不存在"
  371. err = errors.New(errMsg)
  372. isSendEmail = false
  373. return
  374. }
  375. errMsg = "获取分类信息失败"
  376. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  377. return
  378. }
  379. if chartClassify == nil {
  380. errMsg = "分类不存在"
  381. err = errors.New(errMsg)
  382. isSendEmail = false
  383. return
  384. }
  385. // 判断图表是否存在
  386. {
  387. var condition string
  388. var pars []interface{}
  389. switch lang {
  390. case utils.EnLangVersion:
  391. condition += " AND chart_name_en = ? AND source = ? "
  392. default:
  393. condition += " AND chart_name = ? AND source = ? "
  394. }
  395. pars = append(pars, v.ChartName, source)
  396. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  397. if tmpErr != nil {
  398. errMsg = "判断图表名称是否存在失败"
  399. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  400. return
  401. }
  402. if count > 0 {
  403. errMsg = "图表已存在,请重新填写"
  404. err = errors.New(errMsg)
  405. isSendEmail = false
  406. return
  407. }
  408. }
  409. }
  410. edbList, _, _, err, errMsg := getConfigData(lineChartInfoConfig)
  411. var edbInfoIdArr []int
  412. for _, v := range edbList {
  413. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  414. }
  415. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  416. sort.Ints(edbInfoIdArr)
  417. var edbInfoIdArrStr []string
  418. for _, v := range edbInfoIdArr {
  419. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  420. }
  421. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  422. // 批量配置
  423. lineChartInfoConfigByte, err := json.Marshal(lineChartInfoConfig)
  424. if err != nil {
  425. return
  426. }
  427. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  428. Curve: string(lineChartInfoConfigByte),
  429. SysUserId: sysUser.AdminId,
  430. SysUserRealName: sysUser.RealName,
  431. ModifyTime: time.Now(),
  432. CreateTime: time.Now(),
  433. }
  434. // 多图
  435. batchAddChartList = make([]line_equation.BatchAddChart, 0)
  436. for _, v := range batchAddChartReq {
  437. tmpLineChartInfoConfig := lineChartInfoConfig
  438. tmpLineChartInfoConfig.Source = v.Source
  439. tmpLineChartInfoConfigByte, tmpErr := json.Marshal(tmpLineChartInfoConfig)
  440. if tmpErr != nil {
  441. err = tmpErr
  442. return
  443. }
  444. chartInfo := &data_manage.ChartInfo{
  445. ChartInfoId: 0,
  446. ChartName: v.ChartName,
  447. ChartNameEn: v.ChartName,
  448. ChartClassifyId: v.ChartClassifyId,
  449. SysUserId: sysUser.AdminId,
  450. SysUserRealName: sysUser.RealName,
  451. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
  452. CreateTime: time.Now(),
  453. ModifyTime: time.Now(),
  454. DateType: lineChartInfoConfig.DateType,
  455. StartDate: lineChartInfoConfig.StartDate,
  456. EndDate: lineChartInfoConfig.EndDate,
  457. IsSetName: 0,
  458. EdbInfoIds: edbInfoIdStr,
  459. ChartType: 1,
  460. Calendar: "公历",
  461. SeasonStartDate: "",
  462. SeasonEndDate: "",
  463. ChartImage: v.ChartImage,
  464. Sort: 0,
  465. LeftMin: v.LeftMin,
  466. LeftMax: v.LeftMax,
  467. RightMin: "",
  468. RightMax: "",
  469. Disabled: disableVal,
  470. BarConfig: "",
  471. Source: source,
  472. ExtraConfig: string(tmpLineChartInfoConfigByte),
  473. ChartThemeId: v.ChartThemeId,
  474. SourcesFrom: v.SourcesFrom,
  475. Instructions: v.Instructions,
  476. MarkersLines: v.MarkersLines,
  477. MarkersAreas: v.MarkersAreas,
  478. }
  479. // 指标信息
  480. mapList := make([]*data_manage.ChartEdbMapping, 0)
  481. for _, edbInfo := range edbList {
  482. mapItem := new(data_manage.ChartEdbMapping)
  483. mapItem.EdbInfoId = edbInfo.EdbInfoId
  484. mapItem.CreateTime = time.Now()
  485. mapItem.ModifyTime = time.Now()
  486. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  487. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + timestamp + "_" + strconv.Itoa(edbInfo.EdbInfoId))
  488. mapItem.IsOrder = true
  489. mapItem.IsAxis = 1
  490. mapItem.EdbInfoType = 1
  491. mapItem.Source = source
  492. mapList = append(mapList, mapItem)
  493. }
  494. // 批量的关系图
  495. multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  496. //Id: 0,
  497. //MultipleGraphConfigId: 0,
  498. //ChartInfoId: 0,
  499. Source: v.Source,
  500. ModifyTime: time.Now(),
  501. CreateTime: time.Now(),
  502. }
  503. batchAddChartList = append(batchAddChartList, line_equation.BatchAddChart{
  504. ChartInfo: chartInfo,
  505. EdbMappingList: mapList,
  506. MultipleGraphConfigChartMapping: multipleGraphConfigChartMapping,
  507. })
  508. }
  509. // 批量创建
  510. err = line_equation.CreateLineEquationChartAndEdb(multipleGraphConfig, batchAddChartList)
  511. // 更改es
  512. for _, v := range batchAddChartList {
  513. // 添加指标引用记录
  514. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, v.ChartInfo)
  515. //添加es数据
  516. go data.EsAddOrEditChartInfo(v.ChartInfo.ChartInfoId)
  517. }
  518. return
  519. }
  520. // BatchSaveChartInfo 批量添加编辑图表
  521. func BatchSaveChartInfo(multipleGraphConfigId int, batchAddChartReq []request.AddChart, lineChartInfoConfig request.LineChartInfoReq, sysUser *system.Admin) (batchAddChartList []line_equation.BatchAddChart, err error, errMsg string, isSendEmail bool) {
  522. // 获取统一配置
  523. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(multipleGraphConfigId)
  524. if err != nil {
  525. errMsg = `获取失败`
  526. err = errors.New(`获取配置失败,ERR:` + err.Error())
  527. return
  528. }
  529. // 获取已经关联的图表
  530. chartMappingList, err := data_manage.GetMultipleGraphConfigChartMappingListById(multipleGraphConfig.MultipleGraphConfigId)
  531. if err != nil {
  532. errMsg = `获取失败`
  533. err = errors.New(`获取关联图表配置失败,ERR:` + err.Error())
  534. return
  535. }
  536. // 已经存在关联的图表map
  537. existSourceChartMap := make(map[int]*data_manage.MultipleGraphConfigChartMapping)
  538. // 已经存在的图表map
  539. existChartInfoMap := make(map[int]*data_manage.ChartInfo)
  540. for _, v := range chartMappingList {
  541. existSourceChartMap[v.Source] = v
  542. chartInfo, tmpErr := data_manage.GetChartInfoById(v.ChartInfoId)
  543. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  544. err = tmpErr
  545. return
  546. }
  547. if chartInfo != nil {
  548. existChartInfoMap[v.Source] = chartInfo
  549. }
  550. }
  551. source := utils.CHART_SOURCE_LINE_EQUATION
  552. // 获取需要
  553. for k, v := range batchAddChartReq {
  554. v.ChartName = strings.Trim(v.ChartName, " ")
  555. batchAddChartReq[k] = v
  556. if v.ChartName == "" {
  557. errMsg = "请填写图表名称!"
  558. err = errors.New(errMsg)
  559. isSendEmail = false
  560. return
  561. }
  562. if v.ChartClassifyId <= 0 {
  563. errMsg = "请填写图表名称!"
  564. err = errors.New(errMsg)
  565. isSendEmail = false
  566. return
  567. }
  568. chartClassify, tmpErr := data_manage.GetChartClassifyById(v.ChartClassifyId)
  569. if tmpErr != nil {
  570. if tmpErr.Error() == utils.ErrNoRow() {
  571. errMsg = "分类不存在"
  572. err = errors.New(errMsg)
  573. isSendEmail = false
  574. return
  575. }
  576. errMsg = "获取分类信息失败"
  577. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  578. return
  579. }
  580. if chartClassify == nil {
  581. errMsg = "分类不存在"
  582. err = errors.New(errMsg)
  583. isSendEmail = false
  584. return
  585. }
  586. // 判断图表是否存在
  587. var condition string
  588. var pars []interface{}
  589. condition += " AND chart_name=? AND source = ? "
  590. pars = append(pars, v.ChartName, source)
  591. // 已经创建了图表的类型
  592. if sourceChart, ok := existChartInfoMap[v.Source]; ok {
  593. condition += " AND chart_info_id != ? "
  594. pars = append(pars, sourceChart.ChartInfoId)
  595. }
  596. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  597. if tmpErr != nil {
  598. errMsg = "判断图表名称是否存在失败"
  599. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  600. return
  601. }
  602. if count > 0 {
  603. errMsg = "图表已存在,请重新填写"
  604. err = errors.New(errMsg)
  605. isSendEmail = false
  606. return
  607. }
  608. }
  609. edbList, _, _, err, errMsg := getConfigData(lineChartInfoConfig)
  610. var edbInfoIdArr []int
  611. for _, v := range edbList {
  612. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  613. }
  614. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  615. sort.Ints(edbInfoIdArr)
  616. var edbInfoIdArrStr []string
  617. for _, v := range edbInfoIdArr {
  618. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  619. }
  620. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  621. // 批量配置
  622. lineChartInfoConfigByte, err := json.Marshal(lineChartInfoConfig)
  623. if err != nil {
  624. return
  625. }
  626. multipleGraphConfig.Curve = string(lineChartInfoConfigByte)
  627. multipleGraphConfig.ModifyTime = time.Now()
  628. updateMultipleGraphConfigCols := []string{"Curve", "ModifyTime"}
  629. updateChartCols := []string{"ChartName", "DateType", "StartDate", "EndDate", "EdbInfoIds", "LeftMin", "LeftMax", "Disabled", "ExtraConfig"}
  630. // 多图
  631. batchAddChartList = make([]line_equation.BatchAddChart, 0)
  632. for _, v := range batchAddChartReq {
  633. tmpLineChartInfoConfig := lineChartInfoConfig
  634. tmpLineChartInfoConfig.Source = v.Source
  635. tmpLineChartInfoConfigByte, tmpErr := json.Marshal(tmpLineChartInfoConfig)
  636. if tmpErr != nil {
  637. err = tmpErr
  638. return
  639. }
  640. var multipleGraphConfigChartMapping *data_manage.MultipleGraphConfigChartMapping
  641. // 已经创建了图表的类型
  642. chartInfo, ok := existChartInfoMap[v.Source]
  643. if ok {
  644. chartInfo.ChartName = v.ChartName
  645. chartInfo.DateType = lineChartInfoConfig.DateType
  646. chartInfo.StartDate = lineChartInfoConfig.StartDate
  647. chartInfo.EndDate = lineChartInfoConfig.EndDate
  648. chartInfo.EdbInfoIds = edbInfoIdStr
  649. chartInfo.LeftMin = v.LeftMin
  650. chartInfo.LeftMax = v.LeftMax
  651. chartInfo.Disabled = disableVal
  652. chartInfo.ExtraConfig = string(tmpLineChartInfoConfigByte)
  653. //updateChartInfoList
  654. tmpMultipleGraphConfigChartMapping, ok2 := existSourceChartMap[v.Source]
  655. if !ok2 {
  656. err = errors.New("找不到对应的统一配置关系")
  657. return
  658. }
  659. multipleGraphConfigChartMapping = tmpMultipleGraphConfigChartMapping
  660. } else {
  661. chartInfo = &data_manage.ChartInfo{
  662. ChartInfoId: 0,
  663. ChartName: v.ChartName,
  664. //ChartNameEn: v.ChartName,
  665. ChartClassifyId: v.ChartClassifyId,
  666. SysUserId: sysUser.AdminId,
  667. SysUserRealName: sysUser.RealName,
  668. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
  669. CreateTime: time.Now(),
  670. ModifyTime: time.Now(),
  671. DateType: lineChartInfoConfig.DateType,
  672. StartDate: lineChartInfoConfig.StartDate,
  673. EndDate: lineChartInfoConfig.EndDate,
  674. IsSetName: 0,
  675. EdbInfoIds: edbInfoIdStr,
  676. ChartType: 1,
  677. Calendar: "公历",
  678. SeasonStartDate: "",
  679. SeasonEndDate: "",
  680. ChartImage: "",
  681. Sort: 0,
  682. LeftMin: v.LeftMin,
  683. LeftMax: v.LeftMax,
  684. RightMin: "",
  685. RightMax: "",
  686. Disabled: disableVal,
  687. BarConfig: "",
  688. Source: source,
  689. ExtraConfig: string(tmpLineChartInfoConfigByte),
  690. }
  691. // 批量配置的关系图
  692. if tmpMultipleGraphConfigChartMapping, ok2 := existSourceChartMap[v.Source]; ok2 {
  693. multipleGraphConfigChartMapping = tmpMultipleGraphConfigChartMapping
  694. } else {
  695. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  696. //Id: 0,
  697. //MultipleGraphConfigId: 0,
  698. //ChartInfoId: 0,
  699. Source: v.Source,
  700. ModifyTime: time.Now(),
  701. CreateTime: time.Now(),
  702. }
  703. }
  704. }
  705. // 指标信息
  706. mapList := make([]*data_manage.ChartEdbMapping, 0)
  707. for _, edbInfo := range edbList {
  708. mapItem := new(data_manage.ChartEdbMapping)
  709. mapItem.EdbInfoId = edbInfo.EdbInfoId
  710. mapItem.CreateTime = time.Now()
  711. mapItem.ModifyTime = time.Now()
  712. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  713. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + timestamp + "_" + strconv.Itoa(edbInfo.EdbInfoId))
  714. mapItem.IsOrder = true
  715. mapItem.IsAxis = 1
  716. mapItem.EdbInfoType = 1
  717. mapItem.Source = source
  718. mapList = append(mapList, mapItem)
  719. }
  720. batchAddChartList = append(batchAddChartList, line_equation.BatchAddChart{
  721. ChartInfo: chartInfo,
  722. EdbMappingList: mapList,
  723. MultipleGraphConfigChartMapping: multipleGraphConfigChartMapping,
  724. })
  725. }
  726. // 批量创建
  727. err = line_equation.CreateAndEditLineEquationChartAndEdb(multipleGraphConfig, batchAddChartList, updateMultipleGraphConfigCols, updateChartCols)
  728. // 更改es
  729. for _, v := range batchAddChartList {
  730. // 添加指标引用记录
  731. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, v.ChartInfo)
  732. //添加es数据
  733. go data.EsAddOrEditChartInfo(v.ChartInfo.ChartInfoId)
  734. }
  735. return
  736. }