chart_info.go 22 KB

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