chart_info.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. package line_equation
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/data_manage"
  6. "eta/eta_mobile/models/data_manage/line_equation"
  7. "eta/eta_mobile/models/data_manage/line_equation/request"
  8. "eta/eta_mobile/models/system"
  9. "eta/eta_mobile/services/data"
  10. "eta/eta_mobile/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. }
  470. // 指标信息
  471. mapList := make([]*data_manage.ChartEdbMapping, 0)
  472. for _, edbInfo := range edbList {
  473. mapItem := new(data_manage.ChartEdbMapping)
  474. mapItem.EdbInfoId = edbInfo.EdbInfoId
  475. mapItem.CreateTime = time.Now()
  476. mapItem.ModifyTime = time.Now()
  477. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  478. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + timestamp + "_" + strconv.Itoa(edbInfo.EdbInfoId))
  479. mapItem.IsOrder = true
  480. mapItem.IsAxis = 1
  481. mapItem.EdbInfoType = 1
  482. mapItem.Source = source
  483. mapList = append(mapList, mapItem)
  484. }
  485. // 批量的关系图
  486. multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  487. //Id: 0,
  488. //MultipleGraphConfigId: 0,
  489. //ChartInfoId: 0,
  490. Source: v.Source,
  491. ModifyTime: time.Now(),
  492. CreateTime: time.Now(),
  493. }
  494. batchAddChartList = append(batchAddChartList, line_equation.BatchAddChart{
  495. ChartInfo: chartInfo,
  496. EdbMappingList: mapList,
  497. MultipleGraphConfigChartMapping: multipleGraphConfigChartMapping,
  498. })
  499. }
  500. // 批量创建
  501. err = line_equation.CreateLineEquationChartAndEdb(multipleGraphConfig, batchAddChartList)
  502. // 更改es
  503. for _, v := range batchAddChartList {
  504. //添加es数据
  505. go data.EsAddOrEditChartInfo(v.ChartInfo.ChartInfoId)
  506. }
  507. return
  508. }
  509. // BatchSaveChartInfo 批量添加编辑图表
  510. func BatchSaveChartInfo(multipleGraphConfigId int, batchAddChartReq []request.AddChart, lineChartInfoConfig request.LineChartInfoReq, sysUser *system.Admin) (batchAddChartList []line_equation.BatchAddChart, err error, errMsg string, isSendEmail bool) {
  511. // 获取统一配置
  512. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(multipleGraphConfigId)
  513. if err != nil {
  514. errMsg = `获取失败`
  515. err = errors.New(`获取配置失败,ERR:` + err.Error())
  516. return
  517. }
  518. // 获取已经关联的图表
  519. chartMappingList, err := data_manage.GetMultipleGraphConfigChartMappingListById(multipleGraphConfig.MultipleGraphConfigId)
  520. if err != nil {
  521. errMsg = `获取失败`
  522. err = errors.New(`获取关联图表配置失败,ERR:` + err.Error())
  523. return
  524. }
  525. // 已经存在关联的图表map
  526. existSourceChartMap := make(map[int]*data_manage.MultipleGraphConfigChartMapping)
  527. // 已经存在的图表map
  528. existChartInfoMap := make(map[int]*data_manage.ChartInfo)
  529. for _, v := range chartMappingList {
  530. existSourceChartMap[v.Source] = v
  531. chartInfo, tmpErr := data_manage.GetChartInfoById(v.ChartInfoId)
  532. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  533. err = tmpErr
  534. return
  535. }
  536. if chartInfo != nil {
  537. existChartInfoMap[v.Source] = chartInfo
  538. }
  539. }
  540. source := utils.CHART_SOURCE_LINE_EQUATION
  541. // 获取需要
  542. for k, v := range batchAddChartReq {
  543. v.ChartName = strings.Trim(v.ChartName, " ")
  544. batchAddChartReq[k] = v
  545. if v.ChartName == "" {
  546. errMsg = "请填写图表名称!"
  547. err = errors.New(errMsg)
  548. isSendEmail = false
  549. return
  550. }
  551. if v.ChartClassifyId <= 0 {
  552. errMsg = "请填写图表名称!"
  553. err = errors.New(errMsg)
  554. isSendEmail = false
  555. return
  556. }
  557. chartClassify, tmpErr := data_manage.GetChartClassifyById(v.ChartClassifyId)
  558. if tmpErr != nil {
  559. if tmpErr.Error() == utils.ErrNoRow() {
  560. errMsg = "分类不存在"
  561. err = errors.New(errMsg)
  562. isSendEmail = false
  563. return
  564. }
  565. errMsg = "获取分类信息失败"
  566. err = errors.New("获取分类信息失败,Err:" + tmpErr.Error())
  567. return
  568. }
  569. if chartClassify == nil {
  570. errMsg = "分类不存在"
  571. err = errors.New(errMsg)
  572. isSendEmail = false
  573. return
  574. }
  575. // 判断图表是否存在
  576. var condition string
  577. var pars []interface{}
  578. condition += " AND chart_name=? AND source = ? "
  579. pars = append(pars, v.ChartName, source)
  580. // 已经创建了图表的类型
  581. if sourceChart, ok := existChartInfoMap[v.Source]; ok {
  582. condition += " AND chart_info_id != ? "
  583. pars = append(pars, sourceChart.ChartInfoId)
  584. }
  585. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  586. if tmpErr != nil {
  587. errMsg = "判断图表名称是否存在失败"
  588. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  589. return
  590. }
  591. if count > 0 {
  592. errMsg = "图表已存在,请重新填写"
  593. err = errors.New(errMsg)
  594. isSendEmail = false
  595. return
  596. }
  597. }
  598. edbList, _, _, err, errMsg := getConfigData(lineChartInfoConfig)
  599. var edbInfoIdArr []int
  600. for _, v := range edbList {
  601. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  602. }
  603. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  604. sort.Ints(edbInfoIdArr)
  605. var edbInfoIdArrStr []string
  606. for _, v := range edbInfoIdArr {
  607. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  608. }
  609. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  610. // 批量配置
  611. lineChartInfoConfigByte, err := json.Marshal(lineChartInfoConfig)
  612. if err != nil {
  613. return
  614. }
  615. multipleGraphConfig.Curve = string(lineChartInfoConfigByte)
  616. multipleGraphConfig.ModifyTime = time.Now()
  617. updateMultipleGraphConfigCols := []string{"Curve", "ModifyTime"}
  618. updateChartCols := []string{"ChartName", "DateType", "StartDate", "EndDate", "EdbInfoIds", "LeftMin", "LeftMax", "Disabled", "ExtraConfig"}
  619. // 多图
  620. batchAddChartList = make([]line_equation.BatchAddChart, 0)
  621. for _, v := range batchAddChartReq {
  622. tmpLineChartInfoConfig := lineChartInfoConfig
  623. tmpLineChartInfoConfig.Source = v.Source
  624. tmpLineChartInfoConfigByte, tmpErr := json.Marshal(tmpLineChartInfoConfig)
  625. if tmpErr != nil {
  626. err = tmpErr
  627. return
  628. }
  629. var multipleGraphConfigChartMapping *data_manage.MultipleGraphConfigChartMapping
  630. // 已经创建了图表的类型
  631. chartInfo, ok := existChartInfoMap[v.Source]
  632. if ok {
  633. chartInfo.ChartName = v.ChartName
  634. chartInfo.DateType = lineChartInfoConfig.DateType
  635. chartInfo.StartDate = lineChartInfoConfig.StartDate
  636. chartInfo.EndDate = lineChartInfoConfig.EndDate
  637. chartInfo.EdbInfoIds = edbInfoIdStr
  638. chartInfo.LeftMin = v.LeftMin
  639. chartInfo.LeftMax = v.LeftMax
  640. chartInfo.Disabled = disableVal
  641. chartInfo.ExtraConfig = string(tmpLineChartInfoConfigByte)
  642. //updateChartInfoList
  643. tmpMultipleGraphConfigChartMapping, ok2 := existSourceChartMap[v.Source]
  644. if !ok2 {
  645. err = errors.New("找不到对应的统一配置关系")
  646. return
  647. }
  648. multipleGraphConfigChartMapping = tmpMultipleGraphConfigChartMapping
  649. } else {
  650. chartInfo = &data_manage.ChartInfo{
  651. ChartInfoId: 0,
  652. ChartName: v.ChartName,
  653. //ChartNameEn: v.ChartName,
  654. ChartClassifyId: v.ChartClassifyId,
  655. SysUserId: sysUser.AdminId,
  656. SysUserRealName: sysUser.RealName,
  657. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + strconv.FormatInt(time.Now().UnixNano(), 10)),
  658. CreateTime: time.Now(),
  659. ModifyTime: time.Now(),
  660. DateType: lineChartInfoConfig.DateType,
  661. StartDate: lineChartInfoConfig.StartDate,
  662. EndDate: lineChartInfoConfig.EndDate,
  663. IsSetName: 0,
  664. EdbInfoIds: edbInfoIdStr,
  665. ChartType: 1,
  666. Calendar: "公历",
  667. SeasonStartDate: "",
  668. SeasonEndDate: "",
  669. ChartImage: "",
  670. Sort: 0,
  671. LeftMin: v.LeftMin,
  672. LeftMax: v.LeftMax,
  673. RightMin: "",
  674. RightMax: "",
  675. Disabled: disableVal,
  676. BarConfig: "",
  677. Source: source,
  678. ExtraConfig: string(tmpLineChartInfoConfigByte),
  679. }
  680. // 批量配置的关系图
  681. if tmpMultipleGraphConfigChartMapping, ok2 := existSourceChartMap[v.Source]; ok2 {
  682. multipleGraphConfigChartMapping = tmpMultipleGraphConfigChartMapping
  683. } else {
  684. multipleGraphConfigChartMapping = &data_manage.MultipleGraphConfigChartMapping{
  685. //Id: 0,
  686. //MultipleGraphConfigId: 0,
  687. //ChartInfoId: 0,
  688. Source: v.Source,
  689. ModifyTime: time.Now(),
  690. CreateTime: time.Now(),
  691. }
  692. }
  693. }
  694. // 指标信息
  695. mapList := make([]*data_manage.ChartEdbMapping, 0)
  696. for _, edbInfo := range edbList {
  697. mapItem := new(data_manage.ChartEdbMapping)
  698. mapItem.EdbInfoId = edbInfo.EdbInfoId
  699. mapItem.CreateTime = time.Now()
  700. mapItem.ModifyTime = time.Now()
  701. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  702. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + fmt.Sprint(v.Source) + "_" + timestamp + "_" + strconv.Itoa(edbInfo.EdbInfoId))
  703. mapItem.IsOrder = true
  704. mapItem.IsAxis = 1
  705. mapItem.EdbInfoType = 1
  706. mapItem.Source = source
  707. mapList = append(mapList, mapItem)
  708. }
  709. batchAddChartList = append(batchAddChartList, line_equation.BatchAddChart{
  710. ChartInfo: chartInfo,
  711. EdbMappingList: mapList,
  712. MultipleGraphConfigChartMapping: multipleGraphConfigChartMapping,
  713. })
  714. }
  715. // 批量创建
  716. err = line_equation.CreateAndEditLineEquationChartAndEdb(multipleGraphConfig, batchAddChartList, updateMultipleGraphConfigCols, updateChartCols)
  717. // 更改es
  718. for _, v := range batchAddChartList {
  719. //添加es数据
  720. go data.EsAddOrEditChartInfo(v.ChartInfo.ChartInfoId)
  721. }
  722. return
  723. }