chart_info.go 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. package range_analysis
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/system"
  7. "eta_gn/eta_api/services/alarm_msg"
  8. "eta_gn/eta_api/services/data"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "math"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // GetAutoCalculateDateDataList 获取当前时间相关的区间作为计算依据
  19. func GetAutoCalculateDateDataList(currentDate string, dataList []*data_manage.EdbDataList, req *data_manage.ChartRangeAnalysisExtraConf) (newDataList []*data_manage.EdbDataList, err error) {
  20. currentDateTime, _ := time.ParseInLocation(utils.FormatDate, currentDate, time.Local)
  21. switch req.DateRangeType {
  22. case 0:
  23. // 智能划分得到一个开始日期,和结束日期
  24. var startDateTime time.Time
  25. if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
  26. startDate := req.AutoDateConf.StartDate
  27. if startDate == "" {
  28. startDate = "2020-01-01"
  29. }
  30. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  31. } else {
  32. startConf := req.AutoDateConf.StartDateConf
  33. startDate := ""
  34. if startConf.BaseDateType == 0 { //
  35. startDate = currentDate
  36. } else if startConf.BaseDateType == 1 {
  37. startDate = time.Now().Format(utils.FormatDate)
  38. }
  39. if startConf.MoveForward > 0 {
  40. startDate = GetEdbDateByMoveForward(startDate, startConf.MoveForward, dataList)
  41. }
  42. if len(startConf.DateChange) > 0 {
  43. startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
  44. if err != nil {
  45. err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
  46. return
  47. }
  48. }
  49. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  50. }
  51. var calStartTime, calEndTime time.Time
  52. if currentDateTime.Before(startDateTime) {
  53. calStartTime = currentDateTime
  54. calEndTime = startDateTime
  55. } else {
  56. calStartTime = startDateTime
  57. calEndTime = currentDateTime
  58. }
  59. // 根据日期,获取数据
  60. for _, vv := range dataList {
  61. dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
  62. if (dataTimeT.After(calStartTime) && dataTimeT.Before(calEndTime)) ||
  63. dataTimeT.Equal(calStartTime) ||
  64. dataTimeT.Equal(calEndTime) {
  65. newDataList = append(newDataList, vv)
  66. }
  67. }
  68. }
  69. return
  70. }
  71. // HandleDataByCalculateType 根据计算公式处理数据
  72. func HandleDataByCalculateType(originList []*data_manage.ChartRangeAnalysisDateDataItem, originDataList []*data_manage.EdbDataList, req *data_manage.ChartRangeAnalysisExtraConf) (newList []*data_manage.EdbDataList, err error) {
  73. if len(originList) == 0 {
  74. return
  75. }
  76. calculateType := req.CalculateType
  77. switch calculateType {
  78. case 0: //均值
  79. var sum float64
  80. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  81. for _, item := range originList {
  82. for _, v := range item.DataList {
  83. sum = 0
  84. //计算的数据返回需要重新确定
  85. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  86. if e != nil {
  87. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  88. return
  89. }
  90. for _, vv := range calDataList {
  91. sum += vv.Value
  92. }
  93. val := sum / float64(len(calDataList))
  94. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  95. newList = append(newList, &data_manage.EdbDataList{
  96. DataTime: v.DataTime,
  97. Value: val,
  98. DataTimestamp: v.DataTimestamp,
  99. })
  100. }
  101. }
  102. } else {
  103. for _, item := range originList {
  104. sum = 0
  105. for k, v := range item.DataList {
  106. sum += v.Value
  107. val := sum / float64(k+1)
  108. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  109. newList = append(newList, &data_manage.EdbDataList{
  110. DataTime: v.DataTime,
  111. Value: val,
  112. DataTimestamp: v.DataTimestamp,
  113. })
  114. }
  115. }
  116. }
  117. case 1: //累计值
  118. var sum float64
  119. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  120. for _, item := range originList {
  121. sum = 0
  122. for _, v := range item.DataList {
  123. sum = 0
  124. //计算的数据返回需要重新确定
  125. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  126. if e != nil {
  127. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  128. return
  129. }
  130. for _, vv := range calDataList {
  131. sum += vv.Value
  132. }
  133. val := sum
  134. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  135. newList = append(newList, &data_manage.EdbDataList{
  136. DataTime: v.DataTime,
  137. Value: val,
  138. DataTimestamp: v.DataTimestamp,
  139. })
  140. }
  141. }
  142. } else {
  143. for _, item := range originList {
  144. sum = 0
  145. for _, v := range item.DataList {
  146. sum += v.Value
  147. val := sum
  148. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  149. newList = append(newList, &data_manage.EdbDataList{
  150. DataTime: v.DataTime,
  151. Value: val,
  152. DataTimestamp: v.DataTimestamp,
  153. })
  154. }
  155. }
  156. }
  157. case 2: //涨幅
  158. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  159. for _, item := range originList {
  160. for _, v := range item.DataList {
  161. var baseVal float64
  162. //计算的数据返回需要重新确定
  163. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  164. if e != nil {
  165. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  166. return
  167. }
  168. if len(calDataList) == 0 {
  169. continue
  170. }
  171. baseVal = calDataList[0].Value
  172. baseDate := calDataList[0].DataTime
  173. if baseVal == 0 {
  174. continue
  175. }
  176. if v.DataTime == baseDate {
  177. continue
  178. }
  179. val := (v.Value - baseVal) / baseVal
  180. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  181. newList = append(newList, &data_manage.EdbDataList{
  182. DataTime: v.DataTime,
  183. Value: val,
  184. DataTimestamp: v.DataTimestamp,
  185. })
  186. }
  187. }
  188. } else {
  189. for _, item := range originList {
  190. if len(item.DataList) == 0 {
  191. break
  192. }
  193. baseVal := item.DataList[0].Value
  194. baseDate := item.DataList[0].DataTime
  195. if baseVal == 0 {
  196. break
  197. }
  198. for _, v := range item.DataList {
  199. if v.DataTime == baseDate {
  200. continue
  201. }
  202. val := (v.Value - baseVal) / baseVal
  203. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  204. newList = append(newList, &data_manage.EdbDataList{
  205. DataTime: v.DataTime,
  206. Value: val,
  207. DataTimestamp: v.DataTimestamp,
  208. })
  209. }
  210. }
  211. }
  212. case 3: //复合增长率
  213. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  214. for _, item := range originList {
  215. for _, v := range item.DataList {
  216. var baseVal float64
  217. var baseDate string
  218. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  219. if e != nil {
  220. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  221. return
  222. }
  223. if len(calDataList) == 0 {
  224. continue
  225. }
  226. baseVal = calDataList[0].Value
  227. baseDate = calDataList[0].DataTime
  228. if v.DataTime == baseDate {
  229. continue
  230. }
  231. if baseVal == 0 {
  232. continue
  233. }
  234. baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
  235. if e != nil {
  236. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  237. return
  238. }
  239. tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  240. if e != nil {
  241. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  242. return
  243. }
  244. // 计算两个日期相差的天数
  245. diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
  246. val := v.Value / baseVal
  247. val = math.Pow(val, 1/diff) - 1
  248. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  249. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  250. }
  251. }
  252. } else {
  253. for _, item := range originList {
  254. if len(item.DataList) == 0 {
  255. break
  256. }
  257. baseVal := item.DataList[0].Value
  258. baseDate := item.DataList[0].DataTime
  259. if baseVal == 0 {
  260. break
  261. }
  262. for _, v := range item.DataList {
  263. if v.DataTime == baseDate {
  264. continue
  265. }
  266. baseDateT, e := time.ParseInLocation(utils.FormatDate, baseDate, time.Local)
  267. if e != nil {
  268. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  269. return
  270. }
  271. tmpT, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  272. if e != nil {
  273. err = fmt.Errorf("time.ParseInLocation err: %v", e)
  274. return
  275. }
  276. // 计算两个日期相差的天数
  277. diff := tmpT.Sub(baseDateT).Hours() / 24 / 365
  278. val := v.Value / baseVal
  279. val = math.Pow(val, 1/diff) - 1
  280. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  281. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  282. }
  283. }
  284. }
  285. case 4: //最大值
  286. var maxVal float64
  287. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  288. for _, item := range originList {
  289. for _, v := range item.DataList {
  290. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  291. if e != nil {
  292. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  293. return
  294. }
  295. for kk, vv := range calDataList {
  296. if kk == 0 {
  297. maxVal = vv.Value
  298. }
  299. if vv.Value > maxVal {
  300. maxVal = vv.Value
  301. }
  302. }
  303. val := maxVal
  304. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  305. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  306. }
  307. }
  308. } else {
  309. for _, item := range originList {
  310. for k, v := range item.DataList {
  311. if k == 0 {
  312. maxVal = v.Value
  313. }
  314. if v.Value > maxVal {
  315. maxVal = v.Value
  316. }
  317. val := maxVal
  318. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  319. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  320. }
  321. }
  322. }
  323. case 5: //最小值
  324. var minVal float64
  325. if req.DateRangeType == 0 && req.AutoDateConf.IsAutoStartDate > 0 {
  326. for _, item := range originList {
  327. for _, v := range item.DataList {
  328. calDataList, e := GetAutoCalculateDateDataList(v.DataTime, originDataList, req)
  329. if e != nil {
  330. err = fmt.Errorf("获取区间数据失败:%s", e.Error())
  331. return
  332. }
  333. for kk, vv := range calDataList {
  334. if kk == 0 {
  335. minVal = vv.Value
  336. }
  337. if vv.Value < minVal {
  338. minVal = vv.Value
  339. }
  340. }
  341. val := minVal
  342. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  343. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  344. }
  345. }
  346. } else {
  347. for _, item := range originList {
  348. for k, v := range item.DataList {
  349. if k == 0 {
  350. minVal = v.Value
  351. }
  352. if v.Value < minVal {
  353. minVal = v.Value
  354. }
  355. val := minVal
  356. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  357. newList = append(newList, &data_manage.EdbDataList{DataTime: v.DataTime, Value: val, DataTimestamp: v.DataTimestamp})
  358. }
  359. }
  360. }
  361. }
  362. return
  363. }
  364. // GetChartEdbInfoFormat 区间计算图表-获取指标信息
  365. func GetChartEdbInfoFormat(chartInfoId int, edbInfoMappingList []*data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
  366. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  367. for _, edbInfoMapping := range edbInfoMappingList {
  368. if edbInfoMapping == nil {
  369. err = fmt.Errorf("指标信息有误")
  370. return
  371. }
  372. edbInfoMapping.FrequencyEn = data.GetFrequencyEn(edbInfoMapping.Frequency)
  373. if edbInfoMapping.Unit == `无` {
  374. edbInfoMapping.Unit = ``
  375. }
  376. if chartInfoId <= 0 {
  377. edbInfoMapping.IsAxis = 1
  378. edbInfoMapping.LeadValue = 0
  379. edbInfoMapping.LeadUnit = ""
  380. edbInfoMapping.ChartEdbMappingId = 0
  381. edbInfoMapping.ChartInfoId = 0
  382. edbInfoMapping.IsOrder = false
  383. edbInfoMapping.EdbInfoType = 1
  384. edbInfoMapping.ChartStyle = ""
  385. edbInfoMapping.ChartColor = ""
  386. edbInfoMapping.ChartWidth = 0
  387. } else {
  388. edbInfoMapping.LeadUnitEn = data.GetLeadUnitEn(edbInfoMapping.LeadUnit)
  389. }
  390. edbList = append(edbList, edbInfoMapping)
  391. }
  392. return
  393. }
  394. // GetChartDataByEdbInfoList 区间计算图表-根据指标信息获取x轴和y轴
  395. func GetChartDataByEdbInfoList(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, dataResp data_manage.ChartRangeAnalysisDataResp, err error) {
  396. if chartInfoId > 0 && req.EdbInfoMode == 1 {
  397. edbList, xEdbIdValue, dataResp, err = GetChartDataByEdbInfoListBySeries(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, req)
  398. return
  399. }
  400. for _, edbInfoMapping := range edbInfoMappingList {
  401. edbInfoMapping, err = getChartDataByEdbInfo(edbInfoMapping, req)
  402. if err != nil {
  403. return
  404. }
  405. edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
  406. edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
  407. if req.CalculateType == 2 || req.CalculateType == 3 {
  408. edbInfoMapping.ConvertUnit = "无"
  409. edbInfoMapping.ConvertEnUnit = "无"
  410. }
  411. if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
  412. edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
  413. edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
  414. }
  415. dataList := edbInfoMapping.DataList.([]*data_manage.EdbDataList)
  416. // 处理上下限
  417. var maxData, minData float64
  418. if len(dataList) > 0 {
  419. maxData = dataList[0].Value
  420. minData = dataList[0].Value
  421. for _, v := range dataList {
  422. if v.Value > maxData {
  423. maxData = v.Value
  424. }
  425. if v.Value < minData {
  426. minData = v.Value
  427. }
  428. }
  429. }
  430. edbInfoMapping.MaxData = maxData
  431. edbInfoMapping.MinData = minData
  432. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  433. }
  434. //根据时间类型来筛选最终的数据
  435. yearMax := 0
  436. if dateType == utils.DateTypeNYears {
  437. for _, v := range edbInfoMappingList {
  438. dataList := v.DataList.([]*data_manage.EdbDataList)
  439. if len(dataList) > 0 {
  440. latestDate := dataList[len(dataList)-1].DataTime
  441. if latestDate != "" {
  442. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  443. if tErr != nil {
  444. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  445. return
  446. }
  447. if lastDateT.Year() > yearMax {
  448. yearMax = lastDateT.Year()
  449. }
  450. }
  451. }
  452. }
  453. }
  454. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  455. if startDate != "" {
  456. for k, v := range edbInfoMappingList {
  457. var maxData, minData float64
  458. dataList := v.DataList.([]*data_manage.EdbDataList)
  459. newDataList := make([]*data_manage.EdbDataList, 0)
  460. if len(dataList) == 0 {
  461. newDataList = dataList
  462. } else {
  463. firstFlag := true
  464. for _, d := range dataList {
  465. if endDate != "" && d.DataTime > endDate {
  466. continue
  467. }
  468. if d.DataTime < startDate {
  469. continue
  470. }
  471. newDataList = append(newDataList, d)
  472. if firstFlag {
  473. maxData = d.Value
  474. minData = d.Value
  475. firstFlag = false
  476. } else {
  477. if d.Value > maxData {
  478. maxData = d.Value
  479. }
  480. if d.Value < minData {
  481. minData = d.Value
  482. }
  483. }
  484. }
  485. }
  486. edbInfoMappingList[k].DataList = newDataList
  487. edbInfoMappingList[k].MinData = minData
  488. edbInfoMappingList[k].MaxData = maxData
  489. }
  490. }
  491. dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req}
  492. if req.MultipleGraphConfigId > 0 {
  493. //判断MultipleGraphConfigId和图表关系是否正确
  494. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  495. if e != nil && !utils.IsErrNoRow(e) {
  496. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  497. return
  498. }
  499. if len(multipleGraphConfigEdbMappingList) > 0 {
  500. // 查询指标是否已存在,如果已经被删除了,则返回0值
  501. edbIds := make([]int, 0)
  502. for _, v := range multipleGraphConfigEdbMappingList {
  503. edbIds = append(edbIds, v.EdbInfoId)
  504. }
  505. var condition string
  506. var pars []interface{}
  507. condition = " AND edb_info_id in (" + utils.GetOrmInReplace(len(edbIds)) + ") "
  508. pars = append(pars, edbIds)
  509. count, e := data_manage.GetEdbInfoCountByCondition(condition, pars)
  510. if e != nil {
  511. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  512. return
  513. }
  514. if count > 0 {
  515. // 查询是否已经生成指标
  516. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  517. }
  518. }
  519. }
  520. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  521. if err != nil {
  522. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  523. return
  524. }
  525. return
  526. }
  527. func GetChartDataByEdbInfoListBySeries(chartInfoId int, dateType, startYear int, startDate, endDate string, edbInfoMappingList []*data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, dataResp data_manage.ChartRangeAnalysisDataResp, err error) {
  528. //查询seriesId
  529. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  530. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfoId)
  531. if err != nil {
  532. if utils.IsErrNoRow(err) {
  533. err = fmt.Errorf("图表关联关系不存在")
  534. return
  535. } else {
  536. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  537. return
  538. }
  539. }
  540. //根据seriesId查询数据
  541. //并把数据放到dataList中
  542. for _, edbInfoMapping := range edbInfoMappingList {
  543. dataOb := new(data_manage.FactorEdbSeriesCalculateDataQjjs)
  544. dataList, e := dataOb.GetEdbDataList(seriesMappingItem.FactorEdbSeriesId, edbInfoMapping.EdbInfoId, startDate, endDate)
  545. if e != nil {
  546. err = e
  547. return
  548. }
  549. edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
  550. edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
  551. if req.CalculateType == 2 || req.CalculateType == 3 {
  552. edbInfoMapping.ConvertUnit = "无"
  553. edbInfoMapping.ConvertEnUnit = "无"
  554. }
  555. if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
  556. edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
  557. edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
  558. }
  559. edbInfoMapping.DataList = dataList
  560. // 处理上下限
  561. var maxData, minData float64
  562. if len(dataList) > 0 {
  563. maxData = dataList[0].Value
  564. minData = dataList[0].Value
  565. for _, v := range dataList {
  566. if v.Value > maxData {
  567. maxData = v.Value
  568. }
  569. if v.Value < minData {
  570. minData = v.Value
  571. }
  572. }
  573. }
  574. edbInfoMapping.MaxData = maxData
  575. edbInfoMapping.MinData = minData
  576. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  577. }
  578. yearMax := 0
  579. if dateType == utils.DateTypeNYears {
  580. for _, v := range edbInfoMappingList {
  581. dataList := v.DataList.([]*data_manage.EdbDataList)
  582. latestDate := dataList[len(dataList)-1].DataTime
  583. if latestDate != "" {
  584. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  585. if tErr != nil {
  586. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  587. return
  588. }
  589. if lastDateT.Year() > yearMax {
  590. yearMax = lastDateT.Year()
  591. }
  592. }
  593. }
  594. }
  595. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  596. if startDate != "" {
  597. for k, v := range edbInfoMappingList {
  598. var maxData, minData float64
  599. dataList := v.DataList.([]*data_manage.EdbDataList)
  600. newDataList := make([]*data_manage.EdbDataList, 0)
  601. if len(dataList) == 0 {
  602. newDataList = dataList
  603. } else {
  604. firstFlag := true
  605. for _, d := range dataList {
  606. if endDate != "" && d.DataTime > endDate {
  607. continue
  608. }
  609. if d.DataTime < startDate {
  610. continue
  611. }
  612. newDataList = append(newDataList, d)
  613. if firstFlag {
  614. maxData = d.Value
  615. minData = d.Value
  616. firstFlag = false
  617. } else {
  618. if d.Value > maxData {
  619. maxData = d.Value
  620. }
  621. if d.Value < minData {
  622. minData = d.Value
  623. }
  624. }
  625. }
  626. }
  627. edbInfoMappingList[k].DataList = newDataList
  628. edbInfoMappingList[k].MinData = minData
  629. edbInfoMappingList[k].MaxData = maxData
  630. }
  631. }
  632. dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req, SeriesId: seriesMappingItem.FactorEdbSeriesId}
  633. // 查询配置关联关系
  634. if req.MultipleGraphConfigId > 0 {
  635. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  636. if e != nil && !utils.IsErrNoRow(e) {
  637. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  638. return
  639. }
  640. if len(multipleGraphConfigEdbMappingList) > 0 {
  641. // 查询指标是否已存在,如果已经被删除了,则返回0值
  642. edbIds := make([]int, 0)
  643. for _, v := range multipleGraphConfigEdbMappingList {
  644. edbIds = append(edbIds, v.EdbInfoId)
  645. }
  646. var condition string
  647. var pars []interface{}
  648. condition = " and edb_info_id in (" + utils.GetOrmInReplace(len(edbIds)) + ") "
  649. pars = append(pars, edbIds)
  650. count, e := data_manage.GetEdbInfoCountByCondition(condition, pars)
  651. if e != nil {
  652. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  653. return
  654. }
  655. if count > 0 {
  656. // 查询是否已经生成指标
  657. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  658. }
  659. }
  660. }
  661. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  662. if err != nil {
  663. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  664. return
  665. }
  666. return
  667. }
  668. // getChartDataByEdbInfo 区间计算图表-根据指标信息获取x轴和y轴
  669. func getChartDataByEdbInfo(edbInfoMapping *data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (newEdbInfoMapping *data_manage.ChartEdbInfoMapping, err error) {
  670. newEdbInfoMapping = edbInfoMapping
  671. // 指标的开始日期和结束日期
  672. edbStartDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.StartDate, time.Local)
  673. //edbStartDate := edbStartDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  674. edbEndDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.EndDate, time.Local)
  675. edbEndDate := edbEndDateTime.Format(utils.FormatDate)
  676. // 获取时间基准指标在时间区间内的值
  677. dataList := make([]*data_manage.EdbDataList, 0)
  678. switch edbInfoMapping.EdbInfoCategoryType {
  679. case 0:
  680. dataList, err = data_manage.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, "", "")
  681. case 1:
  682. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfoMapping.EdbInfoId, "", "", false)
  683. default:
  684. err = errors.New("指标base类型异常")
  685. return
  686. }
  687. /*var dataList data_manage.SortEdbDataList
  688. dataList = dataListTmp
  689. ascDataList := dataListTmp
  690. sort.Sort(dataList)*/
  691. dateList := make([]*data_manage.ChartRangeAnalysisDateDataItem, 0)
  692. switch req.DateRangeType {
  693. case 0:
  694. // 智能划分得到一个开始日期,和结束日期
  695. var startDateTime, endDateTime time.Time
  696. startDateTime = edbStartDateTime
  697. if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
  698. startDate := req.AutoDateConf.StartDate
  699. if startDate == "" {
  700. startDate = "2020-01-01"
  701. }
  702. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  703. } /*else {
  704. startConf := req.AutoDateConf.StartDateConf
  705. startDate := ""
  706. if startConf.BaseDateType == 0 { //
  707. startDate = edbEndDate
  708. } else if startConf.BaseDateType == 1 {
  709. startDate = time.Now().Format(utils.FormatDate)
  710. }
  711. if startConf.MoveForward > 0 {
  712. startDate = GetEdbDateByMoveForward(startConf.MoveForward, dataList)
  713. }
  714. if len(startConf.DateChange) > 0 {
  715. startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
  716. if err != nil {
  717. err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
  718. return
  719. }
  720. }
  721. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  722. }*/
  723. if req.AutoDateConf.IsAutoEndDate == 0 { //固定设置
  724. endDate := req.AutoDateConf.EndDate
  725. if endDate == "" {
  726. err = fmt.Errorf("智能划分截止日期处理失败:请输入截止日期")
  727. return
  728. }
  729. // todo 如果截止日期比指标日期还要大,则用指标的最新日期
  730. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  731. } else {
  732. endConf := req.AutoDateConf.EndDateConf
  733. endDate := edbEndDate
  734. if endConf.MoveForward > 0 {
  735. endDate = GetEdbDateByMoveForward(endDate, endConf.MoveForward, dataList)
  736. }
  737. if len(endConf.DateChange) > 0 {
  738. endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
  739. if err != nil {
  740. err = fmt.Errorf("智能划分结束日期处理失败:%s", err.Error())
  741. return
  742. }
  743. }
  744. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  745. }
  746. dateList = append(dateList, &data_manage.ChartRangeAnalysisDateDataItem{
  747. StartDate: startDateTime,
  748. EndDate: endDateTime})
  749. case 1:
  750. // 手工划分得到多个开始日期和结束日期(已排序)
  751. for _, v := range req.ManualDateConf {
  752. startDateT, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
  753. endDateT, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  754. tmp := &data_manage.ChartRangeAnalysisDateDataItem{
  755. StartDate: startDateT,
  756. EndDate: endDateT,
  757. }
  758. dateList = append(dateList, tmp)
  759. }
  760. case 2:
  761. // 跨年划分得到多个开始日期和结束日期
  762. startYear := edbStartDateTime.Year()
  763. endYear := edbEndDateTime.Year()
  764. startDay := req.YearDateConf.StartDay
  765. endDay := req.YearDateConf.EndDay
  766. for year := startYear; year <= endYear; year++ {
  767. startDate := fmt.Sprintf("%d-%s", year, startDay)
  768. endDate := fmt.Sprintf("%d-%s", year+1, endDay)
  769. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  770. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  771. if startDateTime.Before(edbStartDateTime) {
  772. break
  773. }
  774. tmp := &data_manage.ChartRangeAnalysisDateDataItem{
  775. StartDate: startDateTime,
  776. EndDate: endDateTime,
  777. }
  778. dateList = append(dateList, tmp)
  779. }
  780. }
  781. // 根据日期,获取数据
  782. for _, v := range dateList {
  783. for _, vv := range dataList {
  784. dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
  785. if dataTimeT.After(v.StartDate) && dataTimeT.Before(v.EndDate) ||
  786. dataTimeT.Equal(v.StartDate) ||
  787. dataTimeT.Equal(v.EndDate) {
  788. v.DataList = append(v.DataList, vv)
  789. }
  790. }
  791. }
  792. // 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
  793. newDataList, err := HandleDataByCalculateType(dateList, dataList, req)
  794. if err != nil {
  795. return
  796. }
  797. if req.UnNormalDataDealType > 0 {
  798. switch req.UnNormalDataDealType { //0:不处理,1:剔除,2替换
  799. case 1:
  800. dealDataList := make([]*data_manage.EdbDataList, 0)
  801. for _, v := range newDataList {
  802. if !utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  803. dealDataList = append(dealDataList, v)
  804. }
  805. }
  806. case 2:
  807. for i, v := range newDataList {
  808. if utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  809. newDataList[i].Value = req.UnNormalDataConf.ReplaceValue
  810. }
  811. }
  812. }
  813. }
  814. if req.DataConvertType > 0 {
  815. // 数据转换类型 0不转, 1乘 2除 3对数
  816. switch req.DataConvertType {
  817. case 1:
  818. for i, v := range newDataList {
  819. val := v.Value * req.DataConvertConf.Value
  820. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  821. newDataList[i].Value = val
  822. }
  823. //item.MaxData = item.MaxData * v.ConvertValue
  824. //item.MinData = item.MinData * v.ConvertValue
  825. case 2:
  826. for i, v := range newDataList {
  827. val := v.Value / req.DataConvertConf.Value
  828. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  829. newDataList[i].Value = val
  830. }
  831. //item.MaxData = item.MaxData / v.ConvertValue
  832. //item.MinData = item.MinData / v.ConvertValue
  833. case 3:
  834. for i, v := range newDataList {
  835. if v.Value <= 0 {
  836. err = errors.New("数据中含有负数或0,无法对数运算")
  837. return
  838. }
  839. val := math.Log(v.Value) / math.Log(req.DataConvertConf.Value)
  840. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  841. newDataList[i].Value = val
  842. }
  843. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  844. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  845. }
  846. }
  847. newEdbInfoMapping.DataList = newDataList
  848. //时间处理
  849. return
  850. }
  851. // RollingCorrelationChartDataResp 滚动区间计算图表数据
  852. type RollingCorrelationChartDataResp struct {
  853. MaxData float64
  854. MinData float64
  855. LatestDate string `description:"真实数据的最后日期"`
  856. EdbInfoCategoryType int
  857. ChartColor string
  858. ChartStyle string
  859. PredictChartColor string
  860. ChartType int
  861. ChartWidth int
  862. EdbName string
  863. EdbNameEn string
  864. Unit string
  865. UnitEn string
  866. IsAxis int
  867. DataList []data_manage.EdbDataList
  868. }
  869. // ChartInfoRefresh 图表刷新
  870. func ChartInfoRefresh(chartInfoId int, uniqueCode string) (isAsync bool, err error) {
  871. var errMsg string
  872. defer func() {
  873. if err != nil {
  874. tips := fmt.Sprintf("CorrelationChartInfoRefresh: %s", errMsg)
  875. utils.FileLog.Info(tips)
  876. go alarm_msg.SendAlarmMsg(tips, 3)
  877. }
  878. }()
  879. var chartInfo *data_manage.ChartInfo
  880. if chartInfoId > 0 {
  881. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  882. if err != nil {
  883. if utils.IsErrNoRow(err) {
  884. errMsg = "图表已被删除,请刷新页面"
  885. err = errors.New(errMsg)
  886. return
  887. }
  888. errMsg = "获取图表信息失败"
  889. err = errors.New("获取图表信息失败,Err:" + err.Error())
  890. return
  891. }
  892. } else {
  893. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  894. if err != nil {
  895. if utils.IsErrNoRow(err) {
  896. errMsg = "图表已被删除,请刷新页面"
  897. err = errors.New(errMsg)
  898. return
  899. }
  900. errMsg = "获取图表信息失败"
  901. err = errors.New("获取图表信息失败,Err:" + err.Error())
  902. return
  903. }
  904. }
  905. // 1.刷新图表关联的指标
  906. mappings, e := data_manage.GetChartEdbMappingList(chartInfoId)
  907. if e != nil {
  908. utils.FileLog.Info(fmt.Sprintf("获取图表关联指标失败, err: %v", e))
  909. return
  910. }
  911. if len(mappings) == 0 {
  912. utils.FileLog.Info("图表无关联指标")
  913. return
  914. }
  915. var edbIds []int
  916. for _, v := range mappings {
  917. edbIds = append(edbIds, v.EdbInfoId)
  918. }
  919. if e, _ = data.EdbInfoRefreshAllFromBaseV3(edbIds, false, true, false); e != nil {
  920. utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
  921. return
  922. }
  923. // todo 重新计算
  924. // 区间计算图表配置校验
  925. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  926. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  927. if err != nil {
  928. errMsg = "配置信息错误"
  929. err = errors.New(errMsg + ", Err: " + err.Error())
  930. return
  931. }
  932. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  933. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfo.ChartInfoId)
  934. if err != nil {
  935. if utils.IsErrNoRow(err) {
  936. err = nil
  937. } else {
  938. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  939. return
  940. }
  941. } else {
  942. _, e = FactorEdbStepCalculateRange(seriesMappingItem.FactorEdbSeriesId, edbIds, extraConfig, true)
  943. if e != nil {
  944. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  945. return
  946. }
  947. }
  948. // 4.清除图表缓存
  949. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  950. _ = utils.Rc.Delete(key)
  951. return
  952. }
  953. // AddChartInfo 添加图表
  954. func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  955. isSendEmail = true
  956. req.ChartName = strings.Trim(req.ChartName, " ")
  957. if req.ChartName == "" {
  958. errMsg = "请填写图表名称!"
  959. err = errors.New(errMsg)
  960. isSendEmail = false
  961. return
  962. }
  963. if req.ChartClassifyId <= 0 {
  964. errMsg = "分类参数错误!"
  965. err = errors.New(errMsg)
  966. isSendEmail = false
  967. return
  968. }
  969. // 区间计算图表配置校验
  970. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  971. if req.ExtraConfig == `` {
  972. errMsg = "配置信息错误"
  973. err = errors.New(errMsg)
  974. return
  975. }
  976. err = json.Unmarshal([]byte(req.ExtraConfig), &extraConfig)
  977. if err != nil {
  978. errMsg = "配置信息错误"
  979. err = errors.New(errMsg + ", Err: " + err.Error())
  980. return
  981. }
  982. if len(req.ChartEdbInfoList) <= 0 {
  983. errMsg = "请选择指标"
  984. err = errors.New(errMsg)
  985. isSendEmail = false
  986. return
  987. }
  988. if len(req.ChartEdbInfoList) > 100 {
  989. errMsg = "添加指标总数量不得超过100"
  990. err = errors.New(errMsg)
  991. isSendEmail = false
  992. return
  993. }
  994. err, errMsg, isSendEmail = CheckChartRangeExtraConfig(extraConfig)
  995. if err != nil {
  996. return
  997. }
  998. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  999. if err != nil {
  1000. if utils.IsErrNoRow(err) {
  1001. errMsg = "分类不存在"
  1002. err = errors.New(errMsg)
  1003. isSendEmail = false
  1004. return
  1005. }
  1006. errMsg = "获取分类信息失败"
  1007. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1008. return
  1009. }
  1010. if chartClassify == nil {
  1011. errMsg = "分类不存在"
  1012. err = errors.New(errMsg)
  1013. isSendEmail = false
  1014. return
  1015. }
  1016. var edbInfoIdArr []int
  1017. for _, v := range req.ChartEdbInfoList {
  1018. edbInfoId := v.EdbInfoId
  1019. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1020. if tmpErr != nil {
  1021. if utils.IsErrNoRow(tmpErr) {
  1022. errMsg = "指标不存在!"
  1023. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1024. return
  1025. } else {
  1026. errMsg = "获取指标信息失败!"
  1027. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1028. return
  1029. }
  1030. }
  1031. if edbInfo == nil {
  1032. errMsg = "指标已被删除,请重新选择!"
  1033. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1034. return
  1035. } else {
  1036. if edbInfo.EdbInfoId <= 0 {
  1037. errMsg = "指标已被删除,请重新选择!"
  1038. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1039. return
  1040. }
  1041. }
  1042. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1043. edbInfo.EdbNameSource = edbInfo.EdbName
  1044. }
  1045. sort.Ints(edbInfoIdArr)
  1046. var edbInfoIdArrStr []string
  1047. for _, v := range edbInfoIdArr {
  1048. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1049. }
  1050. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1051. var chartInfoId int
  1052. // 判断图表是否存在
  1053. {
  1054. var condition string
  1055. var pars []interface{}
  1056. switch lang {
  1057. case utils.EnLangVersion:
  1058. condition += " AND chart_name_en = ? AND source = ? "
  1059. default:
  1060. condition += " AND chart_name=? AND source = ? "
  1061. }
  1062. pars = append(pars, req.ChartName, source)
  1063. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  1064. if tmpErr != nil {
  1065. errMsg = "判断图表名称是否存在失败"
  1066. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  1067. return
  1068. }
  1069. if count > 0 {
  1070. errMsg = "图表已存在,请重新填写"
  1071. err = errors.New(errMsg)
  1072. isSendEmail = false
  1073. return
  1074. }
  1075. }
  1076. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  1077. chartInfo = new(data_manage.ChartInfo)
  1078. chartInfo.ChartName = req.ChartName
  1079. chartInfo.ChartNameEn = req.ChartName
  1080. chartInfo.EdbInfoIds = edbInfoIdStr
  1081. chartInfo.ChartClassifyId = req.ChartClassifyId
  1082. chartInfo.SysUserId = sysUser.AdminId
  1083. chartInfo.SysUserRealName = sysUser.RealName
  1084. chartInfo.CreateTime = time.Now()
  1085. chartInfo.ModifyTime = time.Now()
  1086. chartInfo.IsSetName = 0
  1087. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1088. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  1089. chartInfo.ChartType = 1 // 区间计算图
  1090. chartInfo.Calendar = "公历"
  1091. chartInfo.DateType = req.DateType
  1092. chartInfo.StartDate = req.StartDate
  1093. chartInfo.EndDate = req.EndDate
  1094. chartInfo.StartYear = req.StartYear
  1095. chartInfo.SeasonStartDate = req.StartDate
  1096. chartInfo.SeasonEndDate = req.EndDate
  1097. chartInfo.LeftMin = req.LeftMin
  1098. chartInfo.LeftMax = req.LeftMax
  1099. chartInfo.RightMin = req.RightMin
  1100. chartInfo.RightMax = req.RightMax
  1101. chartInfo.Disabled = disableVal
  1102. chartInfo.Source = source
  1103. chartInfo.ChartThemeId = req.ChartThemeId
  1104. chartInfo.SourcesFrom = req.SourcesFrom
  1105. chartInfo.Instructions = req.Instructions
  1106. chartInfo.MarkersLines = req.MarkersLines
  1107. chartInfo.MarkersAreas = req.MarkersAreas
  1108. if req.ExtraConfig != "" {
  1109. chartInfo.ExtraConfig = req.ExtraConfig
  1110. }
  1111. // 指标信息
  1112. mapList := make([]*data_manage.ChartEdbMapping, 0)
  1113. for _, v := range req.ChartEdbInfoList {
  1114. // todo 指标名称修改
  1115. mapItem := new(data_manage.ChartEdbMapping)
  1116. mapItem.EdbInfoId = v.EdbInfoId
  1117. mapItem.EdbAliasName = v.EdbAliasName
  1118. mapItem.IsAxis = v.IsAxis
  1119. mapItem.CreateTime = time.Now()
  1120. mapItem.ModifyTime = time.Now()
  1121. edbTimestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1122. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + edbTimestamp + "_" + strconv.Itoa(v.EdbInfoId))
  1123. mapItem.IsOrder = false
  1124. mapItem.EdbInfoType = 1
  1125. mapItem.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
  1126. mapList = append(mapList, mapItem)
  1127. }
  1128. // 新增图表和指标mapping
  1129. chartInfoId, e := data_manage.CreateRangeChartAndEdb(chartInfo, mapList)
  1130. if e != nil {
  1131. errMsg = "操作失败"
  1132. err = errors.New("新增区间计算图表失败, Err: " + e.Error())
  1133. return
  1134. }
  1135. // 把系列和图表绑定
  1136. if extraConfig.EdbInfoMode == 1 {
  1137. // 新增指标系列
  1138. err = AddSeries(edbInfoIdArr, chartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, req.ExtraConfig)
  1139. if err != nil {
  1140. errMsg = "操作失败"
  1141. err = errors.New("新增区间计算图表失败, Err: " + err.Error())
  1142. return
  1143. }
  1144. //todo 如果保存失败是否要删除
  1145. }
  1146. /*//添加配置信息
  1147. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  1148. //MultipleGraphConfigId: 0,
  1149. SysUserId: sysUser.AdminId,
  1150. SysUserRealName: sysUser.RealName,
  1151. ModifyTime: time.Now(),
  1152. CreateTime: time.Now(),
  1153. }
  1154. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  1155. if err != nil {
  1156. errMsg = "操作失败"
  1157. err = errors.New("新增区间计算图表配置失败, Err: " + err.Error())
  1158. return
  1159. }*/
  1160. if extraConfig.MultipleGraphConfigId > 0 {
  1161. multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  1162. //Id: 0,
  1163. MultipleGraphConfigId: extraConfig.MultipleGraphConfigId,
  1164. ChartInfoId: chartInfo.ChartInfoId,
  1165. Source: utils.CHART_SOURCE_RANGE_ANALYSIS,
  1166. ModifyTime: time.Now(),
  1167. CreateTime: time.Now(),
  1168. }
  1169. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1170. if err != nil {
  1171. errMsg = "操作失败"
  1172. err = errors.New("新增区间计算图表和配置关联关系失败, Err: " + err.Error())
  1173. return
  1174. }
  1175. }
  1176. // 添加指标引用记录
  1177. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartInfo)
  1178. //添加es数据
  1179. go data.EsAddOrEditChartInfo(chartInfoId)
  1180. return
  1181. }
  1182. // EditChartInfo 编辑图表
  1183. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1184. isSendEmail = true
  1185. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  1186. if err != nil {
  1187. if utils.IsErrNoRow(err) {
  1188. errMsg = "图表已被删除,请刷新页面"
  1189. err = errors.New(errMsg)
  1190. isSendEmail = false
  1191. return
  1192. }
  1193. errMsg = "获取图表信息失败"
  1194. err = errors.New("获取图表信息失败,Err:" + err.Error())
  1195. return
  1196. }
  1197. if chartItem.Source != utils.CHART_SOURCE_RANGE_ANALYSIS {
  1198. errMsg = "该图不是区间计算图表!"
  1199. err = errors.New(errMsg)
  1200. isSendEmail = false
  1201. return
  1202. }
  1203. req.ChartName = strings.Trim(req.ChartName, " ")
  1204. if req.ChartClassifyId <= 0 {
  1205. errMsg = "分类参数错误!"
  1206. err = errors.New(errMsg)
  1207. isSendEmail = false
  1208. return
  1209. }
  1210. // 区间计算图表配置校验
  1211. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  1212. if req.ExtraConfig == `` {
  1213. errMsg = "配置信息错误"
  1214. err = errors.New(errMsg)
  1215. return
  1216. }
  1217. err = json.Unmarshal([]byte(req.ExtraConfig), &extraConfig)
  1218. if err != nil {
  1219. errMsg = "配置信息错误"
  1220. err = errors.New(errMsg)
  1221. return
  1222. }
  1223. if len(req.ChartEdbInfoList) <= 0 {
  1224. errMsg = "请选择指标"
  1225. err = errors.New(errMsg)
  1226. isSendEmail = false
  1227. return
  1228. }
  1229. extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
  1230. if len(req.ChartEdbInfoList) > 100 {
  1231. errMsg = "添加指标总数量不得超过100"
  1232. err = errors.New(errMsg)
  1233. isSendEmail = false
  1234. return
  1235. }
  1236. err, errMsg, isSendEmail = CheckChartRangeExtraConfig(extraConfig)
  1237. if err != nil {
  1238. return
  1239. }
  1240. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1241. if err != nil {
  1242. if utils.IsErrNoRow(err) {
  1243. errMsg = "分类不存在"
  1244. err = errors.New(errMsg)
  1245. isSendEmail = false
  1246. return
  1247. }
  1248. errMsg = "获取分类信息失败"
  1249. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1250. return
  1251. }
  1252. if chartClassify == nil {
  1253. errMsg = "分类不存在"
  1254. err = errors.New(errMsg)
  1255. isSendEmail = false
  1256. return
  1257. }
  1258. // 图表操作权限
  1259. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
  1260. if !ok {
  1261. errMsg = "没有该图表的操作权限"
  1262. err = errors.New(errMsg)
  1263. isSendEmail = false
  1264. return
  1265. }
  1266. var edbInfoIdArr []int
  1267. for _, v := range req.ChartEdbInfoList {
  1268. edbInfoId := v.EdbInfoId
  1269. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1270. if tmpErr != nil {
  1271. if utils.IsErrNoRow(tmpErr) {
  1272. errMsg = "图表不存在!"
  1273. err = errors.New("图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1274. return
  1275. } else {
  1276. errMsg = "获取图表信息失败!"
  1277. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1278. return
  1279. }
  1280. }
  1281. if edbInfo == nil {
  1282. errMsg = "指标不存在!"
  1283. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1284. return
  1285. }
  1286. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1287. }
  1288. sort.Ints(edbInfoIdArr)
  1289. var edbInfoIdArrStr []string
  1290. for _, v := range edbInfoIdArr {
  1291. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1292. }
  1293. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1294. //判断图表是否存在
  1295. {
  1296. var condition string
  1297. var pars []interface{}
  1298. condition += " AND chart_info_id <> ? "
  1299. pars = append(pars, req.ChartInfoId)
  1300. switch lang {
  1301. case utils.EnLangVersion:
  1302. condition += " AND chart_name_en = ? AND source = ? "
  1303. default:
  1304. condition += " AND chart_name=? AND source = ? "
  1305. }
  1306. pars = append(pars, req.ChartName, chartItem.Source)
  1307. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  1308. if tmpErr != nil {
  1309. errMsg = "判断图表名称是否存在失败"
  1310. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  1311. return
  1312. }
  1313. if count > 0 {
  1314. errMsg = "图表已存在,请重新填写"
  1315. err = errors.New(errMsg)
  1316. isSendEmail = false
  1317. return
  1318. }
  1319. }
  1320. // 修改图表与指标mapping
  1321. req.ChartType = 1
  1322. dateType := req.DateType
  1323. err = data_manage.EditRangeChartInfoAndMapping(&req, edbInfoIdStr, lang, "公历", dateType, 0, ``)
  1324. if err != nil {
  1325. errMsg = "保存失败"
  1326. err = errors.New("保存失败,Err:" + err.Error())
  1327. return
  1328. }
  1329. addSeries := false
  1330. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  1331. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartItem.ChartInfoId)
  1332. if err != nil {
  1333. if utils.IsErrNoRow(err) {
  1334. err = nil
  1335. addSeries = true
  1336. } else {
  1337. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  1338. return
  1339. }
  1340. }
  1341. if extraConfig.EdbInfoMode == 1 {
  1342. if addSeries {
  1343. // 新增指标系列
  1344. err = AddSeries(edbInfoIdArr, chartItem.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, req.ExtraConfig)
  1345. if err != nil {
  1346. errMsg = "操作失败"
  1347. err = errors.New("新增区间计算图表失败, Err: " + err.Error())
  1348. return
  1349. }
  1350. } else {
  1351. err = EditSeries(seriesMappingItem, edbInfoIdArr, extraConfig, req.ExtraConfig, true)
  1352. if err != nil {
  1353. errMsg = "保存失败"
  1354. err = errors.New("保存失败,Err:" + err.Error())
  1355. return
  1356. }
  1357. }
  1358. // todo 编辑失败处理
  1359. } else if extraConfig.EdbInfoMode == 0 {
  1360. if !addSeries {
  1361. // 删除相关的系列
  1362. factorSeriesOb := new(data_manage.FactorEdbSeries)
  1363. err = factorSeriesOb.RemoveSeriesAndMappingByFactorEdbSeriesId(seriesMappingItem)
  1364. if err != nil {
  1365. errMsg = "保存失败"
  1366. err = errors.New("保存失败,Err:" + err.Error())
  1367. return
  1368. }
  1369. }
  1370. }
  1371. resp := new(data_manage.AddChartInfoResp)
  1372. resp.ChartInfoId = chartItem.ChartInfoId
  1373. resp.UniqueCode = chartItem.UniqueCode
  1374. resp.ChartType = req.ChartType
  1375. // 添加指标引用记录
  1376. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartItem)
  1377. //添加es数据
  1378. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1379. //修改my eta es数据
  1380. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1381. return
  1382. }
  1383. // CopyChartInfo 复制图表
  1384. func CopyChartInfo(classifyId int, chartName string, oldChartInfo *data_manage.ChartInfo, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1385. isSendEmail = true
  1386. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1387. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  1388. err = json.Unmarshal([]byte(oldChartInfo.ExtraConfig), &extraConfig)
  1389. if err != nil {
  1390. errMsg = "配置信息错误"
  1391. err = errors.New(errMsg + ", Err: " + err.Error())
  1392. return
  1393. }
  1394. //新增配置,并绑定图表
  1395. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  1396. //MultipleGraphConfigId: 0,
  1397. SysUserId: sysUser.AdminId,
  1398. SysUserRealName: sysUser.RealName,
  1399. ModifyTime: time.Now(),
  1400. CreateTime: time.Now(),
  1401. }
  1402. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  1403. if err != nil {
  1404. errMsg = "新增区间计算图表配置失败"
  1405. err = errors.New(errMsg + ", Err: " + err.Error())
  1406. return
  1407. }
  1408. extraConfig.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  1409. newExtraConfigBytes, err := json.Marshal(extraConfig)
  1410. if err != nil {
  1411. errMsg = "配置信息错误"
  1412. err = errors.New(errMsg + ", Err: " + err.Error())
  1413. return
  1414. }
  1415. newExtraConfig := string(newExtraConfigBytes)
  1416. chartInfo = &data_manage.ChartInfo{
  1417. ChartInfoId: 0,
  1418. ChartName: chartName,
  1419. ChartClassifyId: classifyId,
  1420. SysUserId: sysUser.AdminId,
  1421. SysUserRealName: sysUser.RealName,
  1422. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  1423. CreateTime: time.Now(),
  1424. ModifyTime: time.Now(),
  1425. DateType: oldChartInfo.DateType,
  1426. StartDate: oldChartInfo.StartDate,
  1427. EndDate: oldChartInfo.EndDate,
  1428. IsSetName: oldChartInfo.IsSetName,
  1429. EdbInfoIds: oldChartInfo.EdbInfoIds,
  1430. ChartType: oldChartInfo.ChartType,
  1431. Calendar: oldChartInfo.Calendar,
  1432. SeasonStartDate: oldChartInfo.SeasonStartDate,
  1433. SeasonEndDate: oldChartInfo.SeasonEndDate,
  1434. ChartImage: oldChartInfo.ChartImage,
  1435. BarConfig: oldChartInfo.BarConfig,
  1436. //Sort: sort,
  1437. LeftMin: oldChartInfo.LeftMin,
  1438. LeftMax: oldChartInfo.LeftMax,
  1439. RightMin: oldChartInfo.RightMin,
  1440. RightMax: oldChartInfo.RightMax,
  1441. Right2Min: oldChartInfo.Right2Min,
  1442. Right2Max: oldChartInfo.Right2Max,
  1443. Disabled: oldChartInfo.Disabled,
  1444. Source: oldChartInfo.Source,
  1445. ExtraConfig: newExtraConfig,
  1446. SeasonExtraConfig: oldChartInfo.SeasonExtraConfig,
  1447. StartYear: oldChartInfo.StartYear,
  1448. Unit: oldChartInfo.Unit,
  1449. UnitEn: oldChartInfo.UnitEn,
  1450. ChartThemeId: oldChartInfo.ChartThemeId,
  1451. SourcesFrom: oldChartInfo.SourcesFrom,
  1452. Instructions: oldChartInfo.Instructions,
  1453. MarkersLines: oldChartInfo.MarkersLines,
  1454. MarkersAreas: oldChartInfo.MarkersAreas,
  1455. }
  1456. newId, err := data_manage.AddChartInfo(chartInfo)
  1457. if err != nil {
  1458. err = fmt.Errorf("保存失败,Err:%s", err.Error())
  1459. return
  1460. }
  1461. chartInfo.ChartInfoId = int(newId)
  1462. edbInfoMappingList, err := data_manage.GetChartMappingList(oldChartInfo.ChartInfoId)
  1463. if err != nil {
  1464. err = fmt.Errorf("获取图表,指标信息失败,Err:" + err.Error())
  1465. return
  1466. }
  1467. // 添加图表与指标的关联关系
  1468. edbInfoIdArr := make([]int, 0)
  1469. {
  1470. mapList := make([]*data_manage.ChartEdbMapping, 0)
  1471. for _, v := range edbInfoMappingList {
  1472. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  1473. timestamp = strconv.FormatInt(time.Now().UnixNano(), 10)
  1474. mapItem := &data_manage.ChartEdbMapping{
  1475. //ChartEdbMappingId: 0,
  1476. ChartInfoId: chartInfo.ChartInfoId,
  1477. EdbInfoId: v.EdbInfoId,
  1478. CreateTime: time.Now(),
  1479. ModifyTime: time.Now(),
  1480. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  1481. MaxData: v.MaxData,
  1482. MinData: v.MinData,
  1483. IsOrder: v.IsOrder,
  1484. IsAxis: v.IsAxis,
  1485. EdbInfoType: v.EdbInfoType,
  1486. LeadValue: v.LeadValue,
  1487. LeadUnit: v.LeadUnit,
  1488. ChartStyle: v.ChartStyle,
  1489. ChartColor: v.ChartColor,
  1490. ChartWidth: v.ChartWidth,
  1491. Source: v.Source,
  1492. EdbAliasName: v.EdbAliasName,
  1493. IsConvert: v.IsConvert,
  1494. ConvertType: v.ConvertType,
  1495. ConvertValue: v.ConvertValue,
  1496. ConvertUnit: v.ConvertEnUnit,
  1497. ConvertEnUnit: v.ConvertEnUnit,
  1498. }
  1499. mapList = append(mapList, mapItem)
  1500. }
  1501. err = data_manage.AddChartEdbMapping(mapList)
  1502. if err != nil {
  1503. err = fmt.Errorf("保存失败,Err:%s", err.Error())
  1504. return
  1505. }
  1506. }
  1507. // 添加系列和图表映射
  1508. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  1509. _, err = chartSeriesOb.GetItemByChartInfoId(oldChartInfo.ChartInfoId)
  1510. if err != nil {
  1511. if utils.IsErrNoRow(err) {
  1512. err = nil
  1513. } else {
  1514. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  1515. return
  1516. }
  1517. } else {
  1518. // 新增指标系列
  1519. // 区间计算图表配置校验
  1520. err = AddSeries(edbInfoIdArr, chartInfo.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, chartInfo.ExtraConfig)
  1521. if err != nil {
  1522. errMsg = "操作失败"
  1523. err = errors.New("新增区间计算图表失败, Err: " + err.Error())
  1524. return
  1525. }
  1526. }
  1527. multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  1528. //Id: 0,
  1529. MultipleGraphConfigId: extraConfig.MultipleGraphConfigId,
  1530. ChartInfoId: chartInfo.ChartInfoId,
  1531. Source: utils.CHART_SOURCE_RANGE_ANALYSIS,
  1532. ModifyTime: time.Now(),
  1533. CreateTime: time.Now(),
  1534. }
  1535. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1536. if err != nil {
  1537. errMsg = "操作失败"
  1538. err = errors.New("新增区间计算图表和配置关联关系失败, Err: " + err.Error())
  1539. return
  1540. }
  1541. //添加es数据
  1542. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1543. return
  1544. }
  1545. func GetEdbDateByMoveForward(startDate string, moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
  1546. // 根据日期进行排序
  1547. index := 0
  1548. length := len(edbDataList)
  1549. for i := length - 1; i >= 0; i-- {
  1550. item := edbDataList[i]
  1551. if item.DataTime == startDate {
  1552. index += 1
  1553. continue
  1554. }
  1555. if index >= moveForward {
  1556. date = item.DataTime
  1557. break
  1558. }
  1559. if index > 0 {
  1560. index += 1
  1561. date = item.DataTime
  1562. }
  1563. }
  1564. return
  1565. }
  1566. // HandleEdbDateChange 处理日期变换
  1567. func HandleEdbDateChange(date string, dateChange []*data_manage.EdbDataDateChangeConf) (newDate string, err error) {
  1568. newDate = date
  1569. if newDate != "" {
  1570. if len(dateChange) > 0 {
  1571. var dateTime time.Time
  1572. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  1573. if err != nil {
  1574. err = fmt.Errorf("日期解析失败: %s", err.Error())
  1575. return
  1576. }
  1577. for _, v := range dateChange {
  1578. if v.ChangeType == 1 {
  1579. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  1580. newDate = dateTime.Format(utils.FormatDate)
  1581. } else if v.ChangeType == 2 {
  1582. newDate, err, _ = utils.HandleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  1583. if err != nil {
  1584. return
  1585. }
  1586. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  1587. if err != nil {
  1588. err = fmt.Errorf("日期解析失败: %s", err.Error())
  1589. return
  1590. }
  1591. }
  1592. }
  1593. }
  1594. }
  1595. return
  1596. }
  1597. // 添加指标系列和数据
  1598. func AddSeries(edbInfoIds []int, chartInfoId, chartInfoSource int, extraConf data_manage.ChartRangeAnalysisExtraConf, calculatesJson string) (err error) {
  1599. edbArr, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  1600. if e != nil {
  1601. err = fmt.Errorf("获取指标列表失败, Err: " + e.Error())
  1602. return
  1603. }
  1604. if len(edbArr) == 0 {
  1605. err = fmt.Errorf("获取指标列表失败, 指标不存在")
  1606. return
  1607. }
  1608. edbInfoType := edbArr[0].EdbInfoType
  1609. // 新增指标系列
  1610. seriesItem := new(data_manage.FactorEdbSeries)
  1611. seriesItem.SeriesName = extraConf.SeriesName
  1612. seriesItem.EdbInfoType = edbInfoType
  1613. seriesItem.CreateTime = time.Now().Local()
  1614. seriesItem.ModifyTime = time.Now().Local()
  1615. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
  1616. seriesItem.CalculateStep = calculatesJson
  1617. mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  1618. for _, v := range edbArr {
  1619. mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
  1620. EdbInfoId: v.EdbInfoId,
  1621. EdbCode: v.EdbCode,
  1622. CreateTime: time.Now().Local(),
  1623. ModifyTime: time.Now().Local(),
  1624. })
  1625. }
  1626. seriesId, e := seriesItem.CreateSeriesAndMapping(seriesItem, mappings)
  1627. if e != nil {
  1628. err = fmt.Errorf("新增因子指标系列失败, Err: " + e.Error())
  1629. return
  1630. }
  1631. // 图表关联-此处添加的chart_info_id=0
  1632. seriesChartMapping := new(data_manage.FactorEdbSeriesChartMapping)
  1633. seriesChartMapping.CalculateType = data_manage.FactorEdbSeriesChartCalculateTypeRange
  1634. //新增图表和指标的映射关系
  1635. seriesChartMapping.CalculateData = ""
  1636. seriesChartMapping.FactorEdbSeriesId = seriesId
  1637. seriesChartMapping.ChartInfoId = chartInfoId
  1638. seriesChartMapping.Source = chartInfoSource
  1639. seriesChartMapping.CreateTime = time.Now().Local()
  1640. seriesChartMapping.ModifyTime = time.Now().Local()
  1641. if e = seriesChartMapping.Create(); e != nil {
  1642. err = fmt.Errorf("新增图表关联失败, Err: " + e.Error())
  1643. return
  1644. }
  1645. // todo 计算指标数据并存储
  1646. _, e = FactorEdbStepCalculateRange(seriesId, edbInfoIds, extraConf, false)
  1647. if e != nil {
  1648. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  1649. return
  1650. }
  1651. // 更新系列计算状态
  1652. cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
  1653. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
  1654. seriesItem.ModifyTime = time.Now().Local()
  1655. if e = seriesItem.Update(cols); e != nil {
  1656. err = fmt.Errorf("更新因子指标系列计算状态失败, Err: " + e.Error())
  1657. return
  1658. }
  1659. return
  1660. }
  1661. func EditSeries(seriesMapping *data_manage.FactorEdbSeriesChartMapping, edbInfoIds []int, extraConf data_manage.ChartRangeAnalysisExtraConf, calculatesJson string, recalculate bool) (err error) {
  1662. seriesOb := new(data_manage.FactorEdbSeries)
  1663. seriesItem, e := seriesOb.GetItemById(seriesMapping.FactorEdbSeriesId)
  1664. if e != nil {
  1665. if utils.IsErrNoRow(e) {
  1666. err = fmt.Errorf("因子指标系列不存在, Err: " + e.Error())
  1667. return
  1668. }
  1669. err = fmt.Errorf("获取因子指标系列失败, Err: " + e.Error())
  1670. return
  1671. }
  1672. edbArr, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  1673. if e != nil {
  1674. err = fmt.Errorf("获取指标列表失败, Err: " + e.Error())
  1675. return
  1676. }
  1677. if len(edbArr) == 0 {
  1678. err = fmt.Errorf("指标列表为空")
  1679. return
  1680. }
  1681. var calculateResp data_manage.FactorEdbSeriesStepCalculateResp
  1682. calculateResp.SeriesId = seriesItem.FactorEdbSeriesId
  1683. // 如果不需要进行重新计算(比如只改了系列名称)那么只更新指标系列
  1684. seriesItem.SeriesName = extraConf.SeriesName
  1685. seriesItem.EdbInfoType = edbArr[0].EdbInfoType
  1686. seriesItem.ModifyTime = time.Now().Local()
  1687. updateCols := []string{seriesOb.Cols().SeriesName, seriesOb.Cols().EdbInfoType, seriesOb.Cols().ModifyTime}
  1688. if !recalculate {
  1689. if e = seriesItem.Update(updateCols); e != nil {
  1690. err = fmt.Errorf("更新因子指标系列失败, Err: " + e.Error())
  1691. return
  1692. }
  1693. return
  1694. }
  1695. // 更新系列信息和指标关联
  1696. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
  1697. seriesItem.CalculateStep = calculatesJson
  1698. updateCols = append(updateCols, seriesOb.Cols().CalculateState, seriesOb.Cols().CalculateStep)
  1699. mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  1700. for _, v := range edbArr {
  1701. mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
  1702. EdbInfoId: v.EdbInfoId,
  1703. EdbCode: v.EdbCode,
  1704. CreateTime: time.Now().Local(),
  1705. ModifyTime: time.Now().Local(),
  1706. })
  1707. }
  1708. if e = seriesItem.EditSeriesAndMapping(seriesItem, mappings, updateCols); e != nil {
  1709. err = fmt.Errorf("更新因子指标系列信息失败, Err: %s", e.Error())
  1710. return
  1711. }
  1712. // todo 重新计算
  1713. _, e = FactorEdbStepCalculateRange(seriesItem.FactorEdbSeriesId, edbInfoIds, extraConf, false)
  1714. if e != nil {
  1715. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  1716. return
  1717. }
  1718. // 更新系列计算状态
  1719. cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
  1720. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
  1721. seriesItem.ModifyTime = time.Now().Local()
  1722. if e = seriesItem.Update(cols); e != nil {
  1723. err = fmt.Errorf("更新因子指标系列计算状态失败, Err: %s", e.Error())
  1724. return
  1725. }
  1726. return
  1727. }
  1728. // FactorEdbStepCalculateRange 因子指标-区间计算
  1729. func FactorEdbStepCalculateRange(seriesId int, edbArr []int, extraConf data_manage.ChartRangeAnalysisExtraConf, recalculate bool) (calculateResp data_manage.FactorEdbSeriesStepCalculateResp, err error) {
  1730. // todo 如果指标已保存,则用指标数据还是图表指标数据?
  1731. // 获取图表x轴y轴
  1732. defer func() {
  1733. if err != nil {
  1734. tips := fmt.Sprintf("StepCalculate计算失败, ErrMsg: %v", err)
  1735. fmt.Println(tips)
  1736. utils.FileLog.Info(tips)
  1737. go alarm_msg.SendAlarmMsg(tips, 3)
  1738. }
  1739. }()
  1740. edbInfoMappingList, e := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbArr)
  1741. if e != nil {
  1742. err = fmt.Errorf("获取区间计算图表, A指标mapping信息失败, Err:%v", e)
  1743. return
  1744. }
  1745. _, _, _, err = GetChartDataByEdbInfoList(0, 0, 0, "", "", edbInfoMappingList, &extraConf)
  1746. if err != nil {
  1747. err = fmt.Errorf("获取图表数据失败, ErrMsg: %v", err)
  1748. return
  1749. }
  1750. // 重新计算-先清除原数据
  1751. calculateDataOb := new(data_manage.FactorEdbSeriesCalculateDataQjjs)
  1752. cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
  1753. pars := make([]interface{}, 0)
  1754. pars = append(pars, seriesId)
  1755. if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
  1756. err = fmt.Errorf("清除原数据失败, err: %v", e)
  1757. return
  1758. }
  1759. // 计算成功的保存结果
  1760. dataArr := make([]*data_manage.FactorEdbSeriesCalculateDataQjjs, 0)
  1761. for _, v := range edbInfoMappingList {
  1762. dataList := v.DataList.([]*data_manage.EdbDataList)
  1763. for _, dataItem := range dataList {
  1764. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  1765. dataArr = append(dataArr, &data_manage.FactorEdbSeriesCalculateDataQjjs{
  1766. FactorEdbSeriesId: seriesId,
  1767. EdbInfoId: v.EdbInfoId,
  1768. EdbCode: v.EdbCode,
  1769. DataTime: dataTime,
  1770. Value: dataItem.Value,
  1771. CreateTime: time.Now().Local(),
  1772. ModifyTime: time.Now().Local(),
  1773. DataTimestamp: dataItem.DataTimestamp,
  1774. })
  1775. }
  1776. }
  1777. if len(dataArr) == 0 {
  1778. err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
  1779. return
  1780. }
  1781. if e = calculateDataOb.CreateMulti(dataArr); e != nil {
  1782. err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
  1783. return
  1784. }
  1785. return
  1786. }
  1787. func CheckChartRangeExtraConfig(extraConfig data_manage.ChartRangeAnalysisExtraConf) (err error, errMsg string, isSendEmail bool) {
  1788. extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
  1789. if extraConfig.SeriesName == "" && extraConfig.EdbInfoMode == 1 {
  1790. errMsg = "请输入指标系列名称"
  1791. err = errors.New(errMsg)
  1792. isSendEmail = false
  1793. return
  1794. }
  1795. if extraConfig.CalculateType > 5 || extraConfig.CalculateType < 0 {
  1796. errMsg = "计算方式参数错误"
  1797. err = errors.New(errMsg)
  1798. isSendEmail = false
  1799. return
  1800. }
  1801. switch extraConfig.DateRangeType {
  1802. case 0:
  1803. case 1:
  1804. if len(extraConfig.ManualDateConf) == 0 {
  1805. errMsg = "请选择时间区间"
  1806. err = errors.New(errMsg)
  1807. return
  1808. }
  1809. // 先按开始时间排序
  1810. sort.Sort(data_manage.ChartRangeAnalysisManualDateConfList(extraConfig.ManualDateConf))
  1811. // 校验日期
  1812. // 1.如果截止时间小于指标的截止日期,需要重置为指标的截止日期
  1813. // 2.时间区间不能重叠
  1814. for i := 1; i < len(extraConfig.ManualDateConf); i++ {
  1815. start1, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i-1].EndDate)
  1816. if e != nil {
  1817. err = e
  1818. errMsg = "截止日期格式有误"
  1819. return
  1820. }
  1821. start2, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].EndDate)
  1822. if e != nil {
  1823. err = e
  1824. errMsg = "截止日期格式有误"
  1825. return
  1826. }
  1827. start3, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].StartDate)
  1828. if e != nil {
  1829. err = e
  1830. errMsg = "截止日期格式有误"
  1831. return
  1832. }
  1833. // 如果当前区间的开始时间小于等于前一个区间的结束时间,则存在重叠
  1834. if !start2.After(start1) || start3.Before(start1) {
  1835. errMsg = "日期区间存在重叠"
  1836. return
  1837. }
  1838. }
  1839. //如果截止时间大于指标的截止日期,需要重置为指标的截止日期
  1840. case 2:
  1841. if extraConfig.YearDateConf.StartDay == "" || extraConfig.YearDateConf.EndDay == "" {
  1842. errMsg = "请选择时间区间"
  1843. return
  1844. }
  1845. if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.StartDay); e != nil {
  1846. errMsg = "开始日期格式有误"
  1847. return
  1848. }
  1849. if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.EndDay); e != nil {
  1850. errMsg = "结束日期格式有误"
  1851. return
  1852. }
  1853. }
  1854. return
  1855. }