chart_info.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. "github.com/shopspring/decimal"
  13. "math"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  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. }
  284. //指标数据
  285. edbInfoIdList := make([]int, 0)
  286. {
  287. edbInfoIdMap := make(map[int]int, 0)
  288. for _, edbInfoId := range lineChartInfoConfig.XEdbInfoIdList {
  289. edbInfoIdMap[edbInfoId] = edbInfoId
  290. }
  291. for _, edbInfoId := range lineChartInfoConfig.YEdbInfoIdList {
  292. edbInfoIdMap[edbInfoId] = edbInfoId
  293. }
  294. for _, edbInfoId := range edbInfoIdMap {
  295. edbInfoIdList = append(edbInfoIdList, edbInfoId)
  296. }
  297. }
  298. mappingList, err = data_manage.GetChartEdbMappingListByEdbInfoIdList(edbInfoIdList)
  299. if err != nil {
  300. errMsg = `获取失败`
  301. err = errors.New("获取图表,指标信息失败,Err:" + err.Error())
  302. return
  303. }
  304. return
  305. }
  306. // handleData 数据处理
  307. func handleData(baseEdbInfoId int, edbDataListMap map[int][]*data_manage.EdbDataList) (dateEdbMap map[string]map[int]float64, err error) {
  308. dateEdbMap = make(map[string]map[int]float64) // [日期][A指标id:值,B指标id:值]
  309. for edbInfoId, edbDataList := range edbDataListMap {
  310. if edbInfoId != baseEdbInfoId {
  311. // 用上期的数据补充当期的数据处理
  312. handleDataMap := make(map[string]float64)
  313. err = data_manage.HandleDataByPreviousData(edbDataList, handleDataMap)
  314. if err != nil {
  315. return
  316. }
  317. for date, val := range handleDataMap {
  318. item, ok := dateEdbMap[date]
  319. if ok {
  320. item[edbInfoId] = val
  321. } else {
  322. item = map[int]float64{
  323. edbInfoId: val,
  324. }
  325. }
  326. dateEdbMap[date] = item
  327. }
  328. } else {
  329. for _, edbData := range edbDataList {
  330. item, ok := dateEdbMap[edbData.DataTime]
  331. if ok {
  332. item[edbInfoId] = edbData.Value
  333. } else {
  334. item = map[int]float64{
  335. edbInfoId: edbData.Value,
  336. }
  337. }
  338. dateEdbMap[edbData.DataTime] = item
  339. }
  340. }
  341. }
  342. return
  343. }
  344. // BatchAddChartInfo 批量添加图表
  345. func BatchAddChartInfo(batchAddChartReq []request.AddChart, lineChartInfoConfig request.LineChartInfoReq, sysUser *system.Admin, lang string) (batchAddChartList []line_equation.BatchAddChart, err error, errMsg string, isSendEmail bool) {
  346. source := utils.CHART_SOURCE_LINE_EQUATION
  347. // 获取需要
  348. for k, v := range batchAddChartReq {
  349. v.ChartName = strings.Trim(v.ChartName, " ")
  350. batchAddChartReq[k] = v
  351. if v.ChartName == "" {
  352. errMsg = "请填写图表名称!"
  353. err = errors.New(errMsg)
  354. isSendEmail = false
  355. return
  356. }
  357. if v.ChartClassifyId <= 0 {
  358. errMsg = "请填写图表名称!"
  359. err = errors.New(errMsg)
  360. isSendEmail = false
  361. return
  362. }
  363. chartClassify, tmpErr := data_manage.GetChartClassifyById(v.ChartClassifyId)
  364. if tmpErr != nil {
  365. if tmpErr.Error() == utils.ErrNoRow() {
  366. errMsg = "分类不存在"
  367. err = errors.New(errMsg)
  368. isSendEmail = false
  369. return
  370. }
  371. errMsg = "获取分类信息失败"
  372. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  373. return
  374. }
  375. if chartClassify == nil {
  376. errMsg = "分类不存在"
  377. err = errors.New(errMsg)
  378. isSendEmail = false
  379. return
  380. }
  381. // 判断图表是否存在
  382. {
  383. var condition string
  384. var pars []interface{}
  385. switch lang {
  386. case utils.EnLangVersion:
  387. condition += " AND chart_name_en = ? AND source = ? "
  388. default:
  389. condition += " AND chart_name = ? AND source = ? "
  390. }
  391. pars = append(pars, v.ChartName, source)
  392. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  393. if tmpErr != nil {
  394. errMsg = "判断图表名称是否存在失败"
  395. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  396. return
  397. }
  398. if count > 0 {
  399. errMsg = "图表已存在,请重新填写"
  400. err = errors.New(errMsg)
  401. isSendEmail = false
  402. return
  403. }
  404. }
  405. }
  406. edbList, _, _, err, errMsg := getConfigData(lineChartInfoConfig)
  407. var edbInfoIdArr []int
  408. for _, v := range edbList {
  409. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  410. }
  411. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  412. sort.Ints(edbInfoIdArr)
  413. var edbInfoIdArrStr []string
  414. for _, v := range edbInfoIdArr {
  415. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  416. }
  417. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  418. // 批量配置
  419. lineChartInfoConfigByte, err := json.Marshal(lineChartInfoConfig)
  420. if err != nil {
  421. return
  422. }
  423. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  424. Curve: string(lineChartInfoConfigByte),
  425. SysUserId: sysUser.AdminId,
  426. SysUserRealName: sysUser.RealName,
  427. ModifyTime: time.Now(),
  428. CreateTime: time.Now(),
  429. }
  430. // 多图
  431. batchAddChartList = make([]line_equation.BatchAddChart, 0)
  432. for _, v := range batchAddChartReq {
  433. tmpLineChartInfoConfig := lineChartInfoConfig
  434. tmpLineChartInfoConfig.Source = v.Source
  435. tmpLineChartInfoConfigByte, tmpErr := json.Marshal(tmpLineChartInfoConfig)
  436. if tmpErr != nil {
  437. err = tmpErr
  438. return
  439. }
  440. chartInfo := &data_manage.ChartInfo{
  441. ChartInfoId: 0,
  442. ChartName: v.ChartName,
  443. ChartNameEn: v.ChartName,
  444. ChartClassifyId: v.ChartClassifyId,
  445. SysUserId: sysUser.AdminId,
  446. SysUserRealName: sysUser.RealName,
  447. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
  448. CreateTime: time.Now(),
  449. ModifyTime: time.Now(),
  450. DateType: lineChartInfoConfig.DateType,
  451. StartDate: lineChartInfoConfig.StartDate,
  452. EndDate: lineChartInfoConfig.EndDate,
  453. IsSetName: 0,
  454. EdbInfoIds: edbInfoIdStr,
  455. ChartType: 1,
  456. Calendar: "公历",
  457. SeasonStartDate: "",
  458. SeasonEndDate: "",
  459. ChartImage: v.ChartImage,
  460. Sort: 0,
  461. LeftMin: v.LeftMin,
  462. LeftMax: v.LeftMax,
  463. RightMin: "",
  464. RightMax: "",
  465. Disabled: disableVal,
  466. BarConfig: "",
  467. Source: source,
  468. ExtraConfig: string(tmpLineChartInfoConfigByte),
  469. ChartThemeId: v.ChartThemeId,
  470. SourcesFrom: v.SourcesFrom,
  471. Instructions: v.Instructions,
  472. MarkersLines: v.MarkersLines,
  473. MarkersAreas: v.MarkersAreas,
  474. }
  475. // 指标信息
  476. mapList := make([]*data_manage.ChartEdbMapping, 0)
  477. for _, edbInfo := range edbList {
  478. mapItem := new(data_manage.ChartEdbMapping)
  479. mapItem.EdbInfoId = edbInfo.EdbInfoId
  480. mapItem.CreateTime = time.Now()
  481. mapItem.ModifyTime = time.Now()
  482. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  483. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + timestamp + "_" + strconv.Itoa(edbInfo.EdbInfoId))
  484. mapItem.IsOrder = true
  485. mapItem.IsAxis = 1
  486. mapItem.EdbInfoType = 1
  487. mapItem.Source = source
  488. mapList = append(mapList, mapItem)
  489. }
  490. // 批量的关系图
  491. multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  492. //Id: 0,
  493. //MultipleGraphConfigId: 0,
  494. //ChartInfoId: 0,
  495. Source: v.Source,
  496. ModifyTime: time.Now(),
  497. CreateTime: time.Now(),
  498. }
  499. batchAddChartList = append(batchAddChartList, line_equation.BatchAddChart{
  500. ChartInfo: chartInfo,
  501. EdbMappingList: mapList,
  502. MultipleGraphConfigChartMapping: multipleGraphConfigChartMapping,
  503. })
  504. }
  505. // 批量创建
  506. err = line_equation.CreateLineEquationChartAndEdb(multipleGraphConfig, batchAddChartList)
  507. // 更改es
  508. for _, v := range batchAddChartList {
  509. // 添加指标引用记录
  510. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, v.ChartInfo)
  511. //添加es数据
  512. go data.EsAddOrEditChartInfo(v.ChartInfo.ChartInfoId)
  513. }
  514. return
  515. }
  516. // BatchSaveChartInfo 批量添加编辑图表
  517. func BatchSaveChartInfo(multipleGraphConfigId int, batchAddChartReq []request.AddChart, lineChartInfoConfig request.LineChartInfoReq, sysUser *system.Admin) (batchAddChartList []line_equation.BatchAddChart, err error, errMsg string, isSendEmail bool) {
  518. // 获取统一配置
  519. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(multipleGraphConfigId)
  520. if err != nil {
  521. errMsg = `获取失败`
  522. err = errors.New(`获取配置失败,ERR:` + err.Error())
  523. return
  524. }
  525. // 获取已经关联的图表
  526. chartMappingList, err := data_manage.GetMultipleGraphConfigChartMappingListById(multipleGraphConfig.MultipleGraphConfigId)
  527. if err != nil {
  528. errMsg = `获取失败`
  529. err = errors.New(`获取关联图表配置失败,ERR:` + err.Error())
  530. return
  531. }
  532. // 已经存在关联的图表map
  533. existSourceChartMap := make(map[int]*data_manage.MultipleGraphConfigChartMapping)
  534. // 已经存在的图表map
  535. existChartInfoMap := make(map[int]*data_manage.ChartInfo)
  536. for _, v := range chartMappingList {
  537. existSourceChartMap[v.Source] = v
  538. chartInfo, tmpErr := data_manage.GetChartInfoById(v.ChartInfoId)
  539. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  540. err = tmpErr
  541. return
  542. }
  543. if chartInfo != nil {
  544. existChartInfoMap[v.Source] = chartInfo
  545. }
  546. }
  547. source := utils.CHART_SOURCE_LINE_EQUATION
  548. // 获取需要
  549. for k, v := range batchAddChartReq {
  550. v.ChartName = strings.Trim(v.ChartName, " ")
  551. batchAddChartReq[k] = v
  552. if v.ChartName == "" {
  553. errMsg = "请填写图表名称!"
  554. err = errors.New(errMsg)
  555. isSendEmail = false
  556. return
  557. }
  558. if v.ChartClassifyId <= 0 {
  559. errMsg = "请填写图表名称!"
  560. err = errors.New(errMsg)
  561. isSendEmail = false
  562. return
  563. }
  564. chartClassify, tmpErr := data_manage.GetChartClassifyById(v.ChartClassifyId)
  565. if tmpErr != nil {
  566. if tmpErr.Error() == utils.ErrNoRow() {
  567. errMsg = "分类不存在"
  568. err = errors.New(errMsg)
  569. isSendEmail = false
  570. return
  571. }
  572. errMsg = "获取分类信息失败"
  573. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  574. return
  575. }
  576. if chartClassify == nil {
  577. errMsg = "分类不存在"
  578. err = errors.New(errMsg)
  579. isSendEmail = false
  580. return
  581. }
  582. // 判断图表是否存在
  583. var condition string
  584. var pars []interface{}
  585. condition += " AND chart_name=? AND source = ? "
  586. pars = append(pars, v.ChartName, source)
  587. // 已经创建了图表的类型
  588. if sourceChart, ok := existChartInfoMap[v.Source]; ok {
  589. condition += " AND chart_info_id != ? "
  590. pars = append(pars, sourceChart.ChartInfoId)
  591. }
  592. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  593. if tmpErr != nil {
  594. errMsg = "判断图表名称是否存在失败"
  595. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  596. return
  597. }
  598. if count > 0 {
  599. errMsg = "图表已存在,请重新填写"
  600. err = errors.New(errMsg)
  601. isSendEmail = false
  602. return
  603. }
  604. }
  605. edbList, _, _, err, errMsg := getConfigData(lineChartInfoConfig)
  606. var edbInfoIdArr []int
  607. for _, v := range edbList {
  608. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  609. }
  610. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  611. sort.Ints(edbInfoIdArr)
  612. var edbInfoIdArrStr []string
  613. for _, v := range edbInfoIdArr {
  614. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  615. }
  616. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  617. // 批量配置
  618. lineChartInfoConfigByte, err := json.Marshal(lineChartInfoConfig)
  619. if err != nil {
  620. return
  621. }
  622. multipleGraphConfig.Curve = string(lineChartInfoConfigByte)
  623. multipleGraphConfig.ModifyTime = time.Now()
  624. updateMultipleGraphConfigCols := []string{"Curve", "ModifyTime"}
  625. updateChartCols := []string{"ChartName", "DateType", "StartDate", "EndDate", "EdbInfoIds", "LeftMin", "LeftMax", "Disabled", "ExtraConfig"}
  626. // 多图
  627. batchAddChartList = make([]line_equation.BatchAddChart, 0)
  628. for _, v := range batchAddChartReq {
  629. tmpLineChartInfoConfig := lineChartInfoConfig
  630. tmpLineChartInfoConfig.Source = v.Source
  631. tmpLineChartInfoConfigByte, tmpErr := json.Marshal(tmpLineChartInfoConfig)
  632. if tmpErr != nil {
  633. err = tmpErr
  634. return
  635. }
  636. var multipleGraphConfigChartMapping *data_manage.MultipleGraphConfigChartMapping
  637. // 已经创建了图表的类型
  638. chartInfo, ok := existChartInfoMap[v.Source]
  639. if ok {
  640. chartInfo.ChartName = v.ChartName
  641. chartInfo.DateType = lineChartInfoConfig.DateType
  642. chartInfo.StartDate = lineChartInfoConfig.StartDate
  643. chartInfo.EndDate = lineChartInfoConfig.EndDate
  644. chartInfo.EdbInfoIds = edbInfoIdStr
  645. chartInfo.LeftMin = v.LeftMin
  646. chartInfo.LeftMax = v.LeftMax
  647. chartInfo.Disabled = disableVal
  648. chartInfo.ExtraConfig = string(tmpLineChartInfoConfigByte)
  649. //updateChartInfoList
  650. tmpMultipleGraphConfigChartMapping, ok2 := existSourceChartMap[v.Source]
  651. if !ok2 {
  652. err = errors.New("找不到对应的统一配置关系")
  653. return
  654. }
  655. multipleGraphConfigChartMapping = tmpMultipleGraphConfigChartMapping
  656. } else {
  657. chartInfo = &data_manage.ChartInfo{
  658. ChartInfoId: 0,
  659. ChartName: v.ChartName,
  660. //ChartNameEn: v.ChartName,
  661. ChartClassifyId: v.ChartClassifyId,
  662. SysUserId: sysUser.AdminId,
  663. SysUserRealName: sysUser.RealName,
  664. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
  665. CreateTime: time.Now(),
  666. ModifyTime: time.Now(),
  667. DateType: lineChartInfoConfig.DateType,
  668. StartDate: lineChartInfoConfig.StartDate,
  669. EndDate: lineChartInfoConfig.EndDate,
  670. IsSetName: 0,
  671. EdbInfoIds: edbInfoIdStr,
  672. ChartType: 1,
  673. Calendar: "公历",
  674. SeasonStartDate: "",
  675. SeasonEndDate: "",
  676. ChartImage: "",
  677. Sort: 0,
  678. LeftMin: v.LeftMin,
  679. LeftMax: v.LeftMax,
  680. RightMin: "",
  681. RightMax: "",
  682. Disabled: disableVal,
  683. BarConfig: "",
  684. Source: source,
  685. ExtraConfig: string(tmpLineChartInfoConfigByte),
  686. }
  687. // 批量配置的关系图
  688. if tmpMultipleGraphConfigChartMapping, ok2 := existSourceChartMap[v.Source]; ok2 {
  689. multipleGraphConfigChartMapping = tmpMultipleGraphConfigChartMapping
  690. } else {
  691. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  692. //Id: 0,
  693. //MultipleGraphConfigId: 0,
  694. //ChartInfoId: 0,
  695. Source: v.Source,
  696. ModifyTime: time.Now(),
  697. CreateTime: time.Now(),
  698. }
  699. }
  700. }
  701. // 指标信息
  702. mapList := make([]*data_manage.ChartEdbMapping, 0)
  703. for _, edbInfo := range edbList {
  704. mapItem := new(data_manage.ChartEdbMapping)
  705. mapItem.EdbInfoId = edbInfo.EdbInfoId
  706. mapItem.CreateTime = time.Now()
  707. mapItem.ModifyTime = time.Now()
  708. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  709. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + timestamp + "_" + strconv.Itoa(edbInfo.EdbInfoId))
  710. mapItem.IsOrder = true
  711. mapItem.IsAxis = 1
  712. mapItem.EdbInfoType = 1
  713. mapItem.Source = source
  714. mapList = append(mapList, mapItem)
  715. }
  716. batchAddChartList = append(batchAddChartList, line_equation.BatchAddChart{
  717. ChartInfo: chartInfo,
  718. EdbMappingList: mapList,
  719. MultipleGraphConfigChartMapping: multipleGraphConfigChartMapping,
  720. })
  721. }
  722. // 批量创建
  723. err = line_equation.CreateAndEditLineEquationChartAndEdb(multipleGraphConfig, batchAddChartList, updateMultipleGraphConfigCols, updateChartCols)
  724. // 更改es
  725. for _, v := range batchAddChartList {
  726. // 添加指标引用记录
  727. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, v.ChartInfo)
  728. //添加es数据
  729. go data.EsAddOrEditChartInfo(v.ChartInfo.ChartInfoId)
  730. }
  731. return
  732. }