chart_info.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. package line_feature
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/models/data_manage/line_feature"
  6. "eta/eta_api/models/data_manage/line_feature/response"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/data"
  9. "eta/eta_api/utils"
  10. "github.com/shopspring/decimal"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // GetStandardDeviationData 获取标准差图表的指标数据
  16. func GetStandardDeviationData(chartInfoId int, startDate, endDate string, mappingInfo *data_manage.ChartEdbInfoMapping, calculateValue int) (edbList []*data_manage.ChartEdbInfoMapping, dataResp response.LineFeatureDataResp, err error, errMsg string) {
  17. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  18. // 指标对应的所有数据
  19. _, edbList, err = data.GetEdbDataMapList(chartInfoId, 1, `公历`, startDate, endDate, []*data_manage.ChartEdbInfoMapping{mappingInfo}, "")
  20. if err != nil {
  21. return
  22. }
  23. if len(edbList) != 1 {
  24. errMsg = `指标异常`
  25. err = errors.New(errMsg)
  26. return
  27. }
  28. edb := edbList[0]
  29. dataList := edb.DataList.([]*data_manage.EdbDataList)
  30. newDataList := make([]data_manage.EdbDataList, 0)
  31. lenData := len(dataList)
  32. var minVal, maxVal float64
  33. if lenData >= calculateValue {
  34. tmpDataList := make([]float64, 0)
  35. for _, tmpData := range dataList {
  36. tmpDataList = append(tmpDataList, tmpData.Value)
  37. }
  38. for i := calculateValue; i <= lenData; i++ {
  39. tmpV := utils.CalculateStandardDeviation(tmpDataList[i-calculateValue : i])
  40. tmpV, _ = decimal.NewFromFloat(tmpV).Round(4).Float64()
  41. newDataList = append(newDataList, data_manage.EdbDataList{
  42. EdbDataId: i,
  43. EdbInfoId: edb.EdbInfoId,
  44. DataTime: dataList[i-1].DataTime,
  45. DataTimestamp: dataList[i-1].DataTimestamp,
  46. Value: tmpV,
  47. })
  48. if tmpV > maxVal {
  49. maxVal = tmpV
  50. }
  51. if tmpV < minVal {
  52. minVal = tmpV
  53. }
  54. }
  55. }
  56. dataResp = response.LineFeatureDataResp{
  57. MaxData: maxVal,
  58. MinData: minVal,
  59. LatestDate: edb.LatestDate,
  60. EdbInfoCategoryType: edb.EdbInfoCategoryType,
  61. ChartColor: `#00F`,
  62. ChartStyle: `spline`,
  63. PredictChartColor: `#00F`,
  64. ChartType: 0,
  65. ChartWidth: 3,
  66. EdbName: "标准差",
  67. EdbNameEn: "standard deviation",
  68. Unit: edb.Unit,
  69. UnitEn: edb.UnitEn,
  70. IsAxis: 1,
  71. DataList: newDataList,
  72. }
  73. return
  74. }
  75. // GetPercentileData 获取百分位图表的指标数据
  76. func GetPercentileData(chartInfoId int, startDate, endDate string, mappingInfo *data_manage.ChartEdbInfoMapping, calculateValue int, calculateUnit string, percentType int) (edbList []*data_manage.ChartEdbInfoMapping, dataResp response.LineFeatureDataResp, err error, errMsg string) {
  77. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  78. moveUnitDays, ok := utils.FrequencyDaysMap[calculateUnit]
  79. if !ok {
  80. errMsg = `错误的周期`
  81. err = errors.New(errMsg)
  82. return
  83. }
  84. calculateDay := calculateValue * moveUnitDays
  85. // 指标对应的所有数据
  86. _, edbList, err = data.GetEdbDataMapList(chartInfoId, 1, `公历`, startDate, endDate, []*data_manage.ChartEdbInfoMapping{mappingInfo}, "")
  87. if err != nil {
  88. return
  89. }
  90. if len(edbList) != 1 {
  91. errMsg = `指标异常`
  92. err = errors.New(errMsg)
  93. return
  94. }
  95. edb := edbList[0]
  96. dataList := edb.DataList.([]*data_manage.EdbDataList)
  97. newDataList := make([]data_manage.EdbDataList, 0)
  98. var edbMinVal, edbMaxVal float64
  99. dataMap := make(map[time.Time]float64, 0)
  100. for _, tmpData := range dataList {
  101. currDateTime, _ := time.ParseInLocation(utils.FormatDate, tmpData.DataTime, time.Local)
  102. dataMap[currDateTime] = tmpData.Value
  103. }
  104. //百分位:对所选指标滚动地取对应时间长度的数据值,取最大值Max,最小值Min,计算Max-Min,百分位=(现值-Min)/(Max-Min),Max=Min时不予计算。
  105. if percentType == utils.PercentCalculateTypeRange {
  106. for i, tmpData := range dataList {
  107. currDateTime, _ := time.ParseInLocation(utils.FormatDate, tmpData.DataTime, time.Local)
  108. maxVal := tmpData.Value
  109. minVal := tmpData.Value
  110. for k := 0; k < calculateDay; k++ {
  111. preVal, ok2 := dataMap[currDateTime.AddDate(0, 0, -k)]
  112. if ok2 {
  113. if preVal > maxVal {
  114. maxVal = preVal
  115. }
  116. if preVal < minVal {
  117. minVal = preVal
  118. }
  119. }
  120. }
  121. if maxVal == minVal {
  122. continue
  123. }
  124. //百分位=(现值-Min)/(Max-Min)
  125. tmpV := (tmpData.Value - minVal) / (maxVal - minVal) * 100
  126. tmpV, _ = decimal.NewFromFloat(tmpV).Round(4).Float64()
  127. newDataList = append(newDataList, data_manage.EdbDataList{
  128. EdbDataId: i,
  129. EdbInfoId: edb.EdbInfoId,
  130. DataTime: dataList[i].DataTime,
  131. DataTimestamp: dataList[i].DataTimestamp,
  132. Value: tmpV,
  133. })
  134. if tmpV < edbMinVal {
  135. edbMinVal = tmpV
  136. }
  137. if tmpV > edbMaxVal {
  138. edbMaxVal = tmpV
  139. }
  140. }
  141. }
  142. // 百分位数据个数算法
  143. // 数据区间第一个和最后一个数据点的时间和数据分别为(T1,S1)(T2,S2); N=T1到T2指标数据个数, n=小于等于S2的数据个数
  144. // 个数百分位=(n-1)/(N-1)
  145. var firstDate time.Time
  146. if len(dataList) > 0 {
  147. d, _ := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  148. firstDate = d
  149. }
  150. if percentType == utils.PercentCalculateTypeNum {
  151. for i, d := range dataList {
  152. // T2为当前日期
  153. s2 := decimal.NewFromFloat(d.Value)
  154. t2, _ := time.ParseInLocation(utils.FormatDate, d.DataTime, time.Local)
  155. // 往前找(时间长度)个有数据的
  156. var bigN, tinyN int
  157. for k := 0; k < calculateDay; k++ {
  158. tp := t2.AddDate(0, 0, -k)
  159. if tp.Before(firstDate) {
  160. break
  161. }
  162. preVal, preOk := dataMap[tp]
  163. if !preOk {
  164. continue
  165. }
  166. bigN += 1
  167. if decimal.NewFromFloat(preVal).LessThanOrEqual(s2) {
  168. tinyN += 1
  169. }
  170. }
  171. // N<=1时说明计算无效
  172. if bigN <= 1 {
  173. continue
  174. }
  175. numerator := decimal.NewFromInt(int64(tinyN - 1))
  176. denominator := decimal.NewFromInt(int64(bigN - 1))
  177. // 因为是百分位所以这里是要*100, 跟之前的算法保持同步
  178. percentVal, _ := numerator.Div(denominator).Mul(decimal.NewFromFloat(100)).Round(4).Float64()
  179. // 写进数组并判断指标最大最小值
  180. newDataList = append(newDataList, data_manage.EdbDataList{
  181. EdbDataId: i,
  182. EdbInfoId: edb.EdbInfoId,
  183. DataTime: dataList[i].DataTime,
  184. DataTimestamp: dataList[i].DataTimestamp,
  185. Value: percentVal,
  186. })
  187. if percentVal < edbMinVal {
  188. edbMinVal = percentVal
  189. }
  190. if percentVal > edbMaxVal {
  191. edbMaxVal = percentVal
  192. }
  193. }
  194. }
  195. dataResp = response.LineFeatureDataResp{
  196. MaxData: edbMaxVal,
  197. MinData: edbMinVal,
  198. LatestDate: edb.LatestDate,
  199. EdbInfoCategoryType: edb.EdbInfoCategoryType,
  200. ChartColor: `#00F`,
  201. ChartStyle: `spline`,
  202. PredictChartColor: `#00F`,
  203. ChartType: 0,
  204. ChartWidth: 3,
  205. EdbName: "百分位",
  206. EdbNameEn: "percentile",
  207. Unit: "%",
  208. UnitEn: "%",
  209. IsAxis: 1,
  210. DataList: newDataList,
  211. }
  212. return
  213. }
  214. // GetFrequencyDistributionData 获取频率分布的图表数据
  215. func GetFrequencyDistributionData(chartInfoId int, mappingInfo *data_manage.ChartEdbInfoMapping, dateType, stepVal int, startDate, endDate string) (edbList []*data_manage.ChartEdbInfoMapping, dataResp response.FrequencyDistributionResp, err error, errMsg string) {
  216. XDataList := make([]float64, 0)
  217. // 频度
  218. Y1DataList := make([]response.FrequencyDistributionYData, 0)
  219. // 累计频率
  220. Y2DataList := make([]response.FrequencyDistributionYData, 0)
  221. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  222. // 指标对应的所有数据
  223. _, edbList, err = data.GetEdbDataMapList(chartInfoId, 1, `公历`, startDate, endDate, []*data_manage.ChartEdbInfoMapping{mappingInfo}, "")
  224. if err != nil {
  225. return
  226. }
  227. if len(edbList) != 1 {
  228. err = errors.New("指标异常")
  229. return
  230. }
  231. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  232. var endDateTime time.Time
  233. if endDate != `` {
  234. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  235. }
  236. edb := edbList[0]
  237. dataList := edb.DataList.([]*data_manage.EdbDataList)
  238. if len(dataList) <= 0 {
  239. return
  240. }
  241. // 非自定义
  242. if dateType != 8 {
  243. endDate = dataList[len(dataList)-1].DataTime
  244. endDateTime, err = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  245. if err != nil {
  246. return
  247. }
  248. //日期类型:1:最近3月;2:最近6月;3:最近1年;4:最近2年;5:最近3年;6:最近5年;7:最近10年,8:自定义时间
  249. startDateTime = utils.GetDateByDateType2(dateType, endDateTime)
  250. startDate = startDateTime.Format(utils.FormatDate)
  251. newDataList := make([]*data_manage.EdbDataList, 0)
  252. for _, v := range dataList {
  253. tmpDataTime, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  254. if tmpErr != nil {
  255. err = tmpErr
  256. return
  257. }
  258. if tmpDataTime.Equal(startDateTime) || tmpDataTime.After(startDateTime) {
  259. newDataList = append(newDataList, v)
  260. }
  261. }
  262. dataList = newDataList
  263. }
  264. maxVal := dataList[0].Value
  265. minVal := dataList[0].Value
  266. dataValMap := make(map[float64]int)
  267. total := 0 // 数据总量
  268. for _, tmpData := range dataList {
  269. currDateTime, _ := time.ParseInLocation(utils.FormatDate, tmpData.DataTime, time.Local)
  270. if (currDateTime.Equal(startDateTime) || currDateTime.After(startDateTime)) && (endDateTime.IsZero() || currDateTime.Before(endDateTime)) {
  271. if maxVal < tmpData.Value {
  272. maxVal = tmpData.Value
  273. }
  274. if minVal > tmpData.Value {
  275. minVal = tmpData.Value
  276. }
  277. num, ok := dataValMap[tmpData.Value]
  278. if ok {
  279. dataValMap[tmpData.Value] = num + 1
  280. } else {
  281. dataValMap[tmpData.Value] = 1
  282. }
  283. total++
  284. }
  285. }
  286. if total <= 0 {
  287. errMsg = `没有数据`
  288. err = errors.New(errMsg)
  289. return
  290. }
  291. // 最大最小值 向上/下取整
  292. minVal = utils.GetFloorNewNum(minVal, 2)
  293. maxVal = utils.GetCeilNewNum(maxVal, 2)
  294. //间距
  295. spacing, _ := (decimal.NewFromFloat(maxVal).Sub(decimal.NewFromFloat(minVal))).Div(decimal.NewFromInt(int64(stepVal))).Float64()
  296. distributionDataNumMap := make(map[float64]int)
  297. for i := 1; i <= stepVal; i++ {
  298. tmpMinVal, _ := decimal.NewFromFloat(minVal).Add((decimal.NewFromFloat(spacing)).Mul(decimal.NewFromInt(int64(i - 1)))).Float64()
  299. tmpMaxVal, _ := decimal.NewFromFloat(minVal).Add((decimal.NewFromFloat(spacing)).Mul(decimal.NewFromInt(int64(i)))).Float64()
  300. XDataList = append(XDataList, tmpMinVal)
  301. distributionDataNumMap[tmpMinVal] = 0
  302. for tmpVal, num := range dataValMap {
  303. if tmpMinVal <= tmpVal {
  304. // 最后一期数据是要小于等于
  305. if i == stepVal {
  306. if tmpVal <= tmpMaxVal {
  307. distributionDataNumMap[tmpMinVal] += num
  308. }
  309. } else {
  310. if tmpVal < tmpMaxVal {
  311. distributionDataNumMap[tmpMinVal] += num
  312. }
  313. }
  314. }
  315. }
  316. }
  317. var minFrequency, maxFrequency float64
  318. tmpNum := 0
  319. for k, tmpMinVal := range XDataList {
  320. // 数量
  321. frequencyYNum := distributionDataNumMap[tmpMinVal]
  322. // 频率
  323. tmpFrequency, _ := decimal.NewFromInt(int64(frequencyYNum)).Div(decimal.NewFromInt(int64(total))).Mul(decimal.NewFromInt(100)).Round(4).Float64()
  324. Y1DataList = append(Y1DataList, response.FrequencyDistributionYData{
  325. X: tmpMinVal,
  326. Y: tmpFrequency,
  327. })
  328. if k == 0 {
  329. minFrequency = tmpFrequency
  330. maxFrequency = tmpFrequency
  331. } else {
  332. if tmpFrequency < minFrequency {
  333. minFrequency = tmpFrequency
  334. }
  335. if tmpFrequency > maxFrequency {
  336. maxFrequency = tmpFrequency
  337. }
  338. }
  339. // 累计数
  340. tmpNum += frequencyYNum
  341. // 累计频率
  342. tmpTotalFrequency, _ := decimal.NewFromInt(int64(tmpNum)).Div(decimal.NewFromInt(int64(total))).Mul(decimal.NewFromInt(100)).Round(4).Float64()
  343. Y2DataList = append(Y2DataList, response.FrequencyDistributionYData{
  344. X: tmpMinVal,
  345. Y: tmpTotalFrequency,
  346. })
  347. }
  348. newDataList := []response.FrequencyDistributionData{
  349. {
  350. Name: "频率",
  351. NameEn: "Frequency",
  352. Unit: "%",
  353. UnitEn: "%",
  354. Value: Y1DataList,
  355. Color: "#00F",
  356. IsAxis: 1,
  357. }, {
  358. Name: "累计频率",
  359. NameEn: "Total Frequency",
  360. Unit: "%",
  361. UnitEn: "%",
  362. Value: Y2DataList,
  363. Color: "#F00",
  364. IsAxis: 0,
  365. },
  366. }
  367. edbList[0].DataList = nil
  368. dataResp = response.FrequencyDistributionResp{
  369. LeftMinValue: minFrequency,
  370. LeftMaxValue: maxFrequency,
  371. RightMinValue: 0,
  372. RightMaxValue: 100,
  373. DataList: newDataList,
  374. }
  375. return
  376. }
  377. // AddChartInfo 添加图表
  378. func AddChartInfo(req data_manage.AddChartInfoReq, edbInfoMapping *data_manage.ChartEdbInfoMapping, source int, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  379. isSendEmail = true
  380. req.ChartName = strings.Trim(req.ChartName, " ")
  381. if req.ChartName == "" {
  382. errMsg = "请填写图表名称!"
  383. err = errors.New(errMsg)
  384. isSendEmail = false
  385. return
  386. }
  387. if req.ChartClassifyId <= 0 {
  388. errMsg = "分类参数错误!"
  389. err = errors.New(errMsg)
  390. isSendEmail = false
  391. return
  392. }
  393. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  394. if err != nil {
  395. if err.Error() == utils.ErrNoRow() {
  396. errMsg = "分类不存在"
  397. err = errors.New(errMsg)
  398. isSendEmail = false
  399. return
  400. }
  401. errMsg = "获取分类信息失败"
  402. err = errors.New("获取分类信息失败,Err:" + err.Error())
  403. return
  404. }
  405. if chartClassify == nil {
  406. errMsg = "分类不存在"
  407. err = errors.New(errMsg)
  408. isSendEmail = false
  409. return
  410. }
  411. edbInfoIdArr := []int{edbInfoMapping.EdbInfoId}
  412. var edbInfoIdArrStr []string
  413. for _, v := range edbInfoIdArr {
  414. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  415. }
  416. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  417. var chartInfoId int
  418. // 判断图表是否存在
  419. {
  420. var condition string
  421. var pars []interface{}
  422. switch lang {
  423. case utils.EnLangVersion:
  424. condition += " AND chart_name_en = ? AND source = ? "
  425. default:
  426. condition += " AND chart_name = ? AND source = ? "
  427. }
  428. pars = append(pars, req.ChartName, source)
  429. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  430. if tmpErr != nil {
  431. errMsg = "判断图表名称是否存在失败"
  432. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  433. return
  434. }
  435. if count > 0 {
  436. errMsg = "图表已存在,请重新填写"
  437. err = errors.New(errMsg)
  438. isSendEmail = false
  439. return
  440. }
  441. }
  442. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  443. chartInfo = new(data_manage.ChartInfo)
  444. chartInfo.ChartName = req.ChartName
  445. chartInfo.ChartNameEn = req.ChartName
  446. chartInfo.EdbInfoIds = edbInfoIdStr
  447. chartInfo.ChartClassifyId = req.ChartClassifyId
  448. chartInfo.SysUserId = sysUser.AdminId
  449. chartInfo.SysUserRealName = sysUser.RealName
  450. chartInfo.CreateTime = time.Now()
  451. chartInfo.ModifyTime = time.Now()
  452. chartInfo.IsSetName = 0
  453. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  454. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  455. chartInfo.ChartType = 9 // 相关性图
  456. chartInfo.Calendar = "公历"
  457. chartInfo.DateType = req.DateType
  458. chartInfo.StartDate = req.StartDate
  459. chartInfo.EndDate = req.EndDate
  460. chartInfo.SeasonStartDate = req.StartDate
  461. chartInfo.SeasonEndDate = req.EndDate
  462. chartInfo.ChartImage = req.ChartImage
  463. chartInfo.LeftMin = req.LeftMin
  464. chartInfo.LeftMax = req.LeftMax
  465. chartInfo.RightMin = req.RightMin
  466. chartInfo.RightMax = req.RightMax
  467. chartInfo.Disabled = disableVal
  468. chartInfo.Source = source
  469. chartInfo.ExtraConfig = req.ExtraConfig
  470. // 指标信息
  471. mapList := make([]*data_manage.ChartEdbMapping, 0)
  472. {
  473. mapItem := new(data_manage.ChartEdbMapping)
  474. mapItem.EdbInfoId = edbInfoMapping.EdbInfoId
  475. mapItem.CreateTime = time.Now()
  476. mapItem.ModifyTime = time.Now()
  477. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + strconv.Itoa(edbInfoMapping.EdbInfoId))
  478. mapItem.IsOrder = true
  479. mapItem.IsAxis = 1
  480. mapItem.EdbInfoType = 1
  481. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  482. mapList = append(mapList, mapItem)
  483. }
  484. // 新增图表和指标mapping
  485. chartInfoId, e := line_feature.CreateLineFeatureChartAndEdb(chartInfo, mapList)
  486. if e != nil {
  487. errMsg = "操作失败"
  488. err = errors.New("新增相关性图表失败, Err: " + e.Error())
  489. return
  490. }
  491. // 添加指标引用记录
  492. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartInfo)
  493. //添加es数据
  494. go data.EsAddOrEditChartInfo(chartInfoId)
  495. return
  496. }
  497. // EditChartInfo 编辑图表
  498. func EditChartInfo(req data_manage.EditChartInfoReq, edbInfoMapping *data_manage.ChartEdbInfoMapping, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  499. isSendEmail = true
  500. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  501. if err != nil {
  502. if err.Error() == utils.ErrNoRow() {
  503. errMsg = "图表已被删除,请刷新页面"
  504. err = errors.New(errMsg)
  505. isSendEmail = false
  506. return
  507. }
  508. errMsg = "获取图表信息失败"
  509. err = errors.New("获取图表信息失败,Err:" + err.Error())
  510. return
  511. }
  512. if !utils.InArrayByInt([]int{utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY}, chartItem.Source) {
  513. errMsg = "该图不是统计分析图表!"
  514. err = errors.New(errMsg)
  515. isSendEmail = false
  516. return
  517. }
  518. req.ChartName = strings.Trim(req.ChartName, " ")
  519. if req.ChartClassifyId <= 0 {
  520. errMsg = "分类参数错误!"
  521. err = errors.New(errMsg)
  522. isSendEmail = false
  523. return
  524. }
  525. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  526. if err != nil {
  527. if err.Error() == utils.ErrNoRow() {
  528. errMsg = "分类不存在"
  529. err = errors.New(errMsg)
  530. isSendEmail = false
  531. return
  532. }
  533. errMsg = "获取分类信息失败"
  534. err = errors.New("获取分类信息失败,Err:" + err.Error())
  535. return
  536. }
  537. if chartClassify == nil {
  538. errMsg = "分类不存在"
  539. err = errors.New(errMsg)
  540. isSendEmail = false
  541. return
  542. }
  543. // 图表操作权限
  544. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
  545. if !ok {
  546. errMsg = "没有该图表的操作权限"
  547. err = errors.New(errMsg)
  548. isSendEmail = false
  549. return
  550. }
  551. edbInfoIdArr := []int{edbInfoMapping.EdbInfoId}
  552. var edbInfoIdArrStr []string
  553. for _, v := range edbInfoIdArr {
  554. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  555. }
  556. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  557. //判断图表是否存在
  558. {
  559. var condition string
  560. var pars []interface{}
  561. condition += " AND chart_info_id <> ? AND source = ? "
  562. pars = append(pars, req.ChartInfoId, chartItem.Source)
  563. switch lang {
  564. case utils.EnLangVersion:
  565. condition += " AND chart_name_en = ?"
  566. default:
  567. condition += " AND chart_name=?"
  568. }
  569. pars = append(pars, req.ChartName)
  570. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  571. if tmpErr != nil {
  572. errMsg = "判断图表名称是否存在失败"
  573. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  574. return
  575. }
  576. if count > 0 {
  577. errMsg = "图表已存在,请重新填写"
  578. err = errors.New(errMsg)
  579. isSendEmail = false
  580. return
  581. }
  582. }
  583. // 图表启用与否
  584. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  585. switch lang {
  586. case utils.EnLangVersion:
  587. if req.ChartNameEn == `` {
  588. req.ChartNameEn = req.ChartName
  589. }
  590. default:
  591. if req.ChartNameEn == `` {
  592. req.ChartNameEn = chartItem.ChartNameEn
  593. }
  594. }
  595. // 修改图表与指标mapping
  596. err = line_feature.EditLineFeatureChartAndMapping(&req, edbInfoIdStr, "公历", req.DateType, disableVal, req.ExtraConfig)
  597. if err != nil {
  598. errMsg = "保存失败"
  599. err = errors.New("保存失败,Err:" + err.Error())
  600. return
  601. }
  602. resp := new(data_manage.AddChartInfoResp)
  603. resp.ChartInfoId = chartItem.ChartInfoId
  604. resp.UniqueCode = chartItem.UniqueCode
  605. resp.ChartType = req.ChartType
  606. // 添加指标引用记录
  607. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartItem)
  608. //添加es数据
  609. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  610. //修改my eta es数据
  611. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  612. return
  613. }
  614. // CopyChartInfo 复制图表
  615. func CopyChartInfo(configId, configSource, classifyId int, chartName string, edbInfoMapping *data_manage.ChartEdbInfoMapping, oldChartInfo *data_manage.ChartInfo, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  616. isSendEmail = true
  617. multipleGraphConfig, err := data_manage.GetMultipleGraphConfigById(configId)
  618. if err != nil {
  619. return
  620. }
  621. multipleGraphConfig.MultipleGraphConfigId = 0
  622. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  623. if err != nil {
  624. return
  625. }
  626. // 添加图
  627. addChartReq := data_manage.AddChartInfoReq{
  628. ChartClassifyId: classifyId,
  629. ChartName: chartName,
  630. ChartType: utils.CHART_TYPE_CURVE,
  631. Calendar: "公历",
  632. ExtraConfig: oldChartInfo.ExtraConfig,
  633. ChartImage: oldChartInfo.ChartImage,
  634. ChartThemeId: oldChartInfo.ChartThemeId,
  635. SourcesFrom: oldChartInfo.SourcesFrom,
  636. Instructions: oldChartInfo.Instructions,
  637. MarkersLines: oldChartInfo.MarkersLines,
  638. MarkersAreas: oldChartInfo.MarkersAreas,
  639. }
  640. chartSource := oldChartInfo.Source // 默认是相关性图
  641. chartInfo, err, errMsg, isSendEmail = AddChartInfo(addChartReq, edbInfoMapping, chartSource, sysUser, lang)
  642. if err != nil {
  643. return
  644. }
  645. // 添加关系
  646. multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  647. //Id: 0,
  648. MultipleGraphConfigId: multipleGraphConfig.MultipleGraphConfigId,
  649. ChartInfoId: chartInfo.ChartInfoId,
  650. Source: configSource,
  651. ModifyTime: time.Now(),
  652. CreateTime: time.Now(),
  653. }
  654. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  655. if err != nil {
  656. return
  657. }
  658. //添加es数据
  659. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  660. return
  661. }