chart_info.go 23 KB

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