chart_info.go 22 KB

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