chart_info.go 21 KB

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