chart_info.go 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  1. package range_analysis
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/system"
  7. "eta/eta_api/services/alarm_msg"
  8. "eta/eta_api/services/data"
  9. "eta/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. if edbInfoMapping.ConvertUnit == "无" {
  416. edbInfoMapping.ConvertUnit = ""
  417. }
  418. if edbInfoMapping.ConvertEnUnit == "无" {
  419. edbInfoMapping.ConvertEnUnit = ""
  420. }
  421. dataList := edbInfoMapping.DataList.([]*data_manage.EdbDataList)
  422. // 处理上下限
  423. var maxData, minData float64
  424. if len(dataList) > 0 {
  425. maxData = dataList[0].Value
  426. minData = dataList[0].Value
  427. for _, v := range dataList {
  428. if v.Value > maxData {
  429. maxData = v.Value
  430. }
  431. if v.Value < minData {
  432. minData = v.Value
  433. }
  434. }
  435. }
  436. edbInfoMapping.MaxData = maxData
  437. edbInfoMapping.MinData = minData
  438. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  439. }
  440. //根据时间类型来筛选最终的数据
  441. yearMax := 0
  442. if dateType == utils.DateTypeNYears {
  443. for _, v := range edbInfoMappingList {
  444. dataList := v.DataList.([]*data_manage.EdbDataList)
  445. if len(dataList) > 0 {
  446. latestDate := dataList[len(dataList)-1].DataTime
  447. if latestDate != "" {
  448. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  449. if tErr != nil {
  450. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  451. return
  452. }
  453. if lastDateT.Year() > yearMax {
  454. yearMax = lastDateT.Year()
  455. }
  456. }
  457. }
  458. }
  459. }
  460. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  461. if startDate != "" {
  462. for k, v := range edbInfoMappingList {
  463. var maxData, minData float64
  464. dataList := v.DataList.([]*data_manage.EdbDataList)
  465. newDataList := make([]*data_manage.EdbDataList, 0)
  466. if len(dataList) == 0 {
  467. newDataList = dataList
  468. } else {
  469. firstFlag := true
  470. for _, d := range dataList {
  471. if endDate != "" && d.DataTime > endDate {
  472. continue
  473. }
  474. if d.DataTime < startDate {
  475. continue
  476. }
  477. newDataList = append(newDataList, d)
  478. if firstFlag {
  479. maxData = d.Value
  480. minData = d.Value
  481. firstFlag = false
  482. } else {
  483. if d.Value > maxData {
  484. maxData = d.Value
  485. }
  486. if d.Value < minData {
  487. minData = d.Value
  488. }
  489. }
  490. }
  491. }
  492. edbInfoMappingList[k].DataList = newDataList
  493. edbInfoMappingList[k].MinData = minData
  494. edbInfoMappingList[k].MaxData = maxData
  495. }
  496. }
  497. dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req}
  498. if req.MultipleGraphConfigId > 0 {
  499. //判断MultipleGraphConfigId和图表关系是否正确
  500. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  501. if e != nil && e.Error() != utils.ErrNoRow() {
  502. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  503. return
  504. }
  505. if len(multipleGraphConfigEdbMappingList) > 0 {
  506. // 查询指标是否已存在,如果已经被删除了,则返回0值
  507. edbIds := make([]int, 0)
  508. for _, v := range multipleGraphConfigEdbMappingList {
  509. edbIds = append(edbIds, v.EdbInfoId)
  510. }
  511. var condition string
  512. var pars []interface{}
  513. condition = " AND edb_info_id in (" + utils.GetOrmInReplace(len(edbIds)) + ") "
  514. pars = append(pars, edbIds)
  515. count, e := data_manage.GetEdbInfoCountByCondition(condition, pars)
  516. if e != nil {
  517. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  518. return
  519. }
  520. if count > 0 {
  521. // 查询是否已经生成指标
  522. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  523. }
  524. }
  525. }
  526. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  527. if err != nil {
  528. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  529. return
  530. }
  531. return
  532. }
  533. 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) {
  534. //查询seriesId
  535. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  536. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfoId)
  537. if err != nil {
  538. if err.Error() == utils.ErrNoRow() {
  539. err = fmt.Errorf("图表关联关系不存在")
  540. return
  541. } else {
  542. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  543. return
  544. }
  545. }
  546. //根据seriesId查询数据
  547. //并把数据放到dataList中
  548. for _, edbInfoMapping := range edbInfoMappingList {
  549. dataOb := new(data_manage.FactorEdbSeriesCalculateDataQjjs)
  550. dataList, e := dataOb.GetEdbDataList(seriesMappingItem.FactorEdbSeriesId, edbInfoMapping.EdbInfoId, startDate, endDate)
  551. if e != nil {
  552. err = e
  553. return
  554. }
  555. edbInfoMapping.ConvertUnit = edbInfoMapping.Unit
  556. edbInfoMapping.ConvertEnUnit = edbInfoMapping.UnitEn
  557. if req.CalculateType == 2 || req.CalculateType == 3 {
  558. edbInfoMapping.ConvertUnit = "无"
  559. edbInfoMapping.ConvertEnUnit = "无"
  560. }
  561. if req.DataConvertType > 0 && req.DataConvertConf.Unit != "" {
  562. edbInfoMapping.ConvertUnit = req.DataConvertConf.Unit
  563. edbInfoMapping.ConvertEnUnit = req.DataConvertConf.Unit
  564. }
  565. if edbInfoMapping.ConvertUnit == "无" {
  566. edbInfoMapping.ConvertUnit = ""
  567. }
  568. if edbInfoMapping.ConvertEnUnit == "无" {
  569. edbInfoMapping.ConvertEnUnit = ""
  570. }
  571. edbInfoMapping.DataList = dataList
  572. // 处理上下限
  573. var maxData, minData float64
  574. if len(dataList) > 0 {
  575. maxData = dataList[0].Value
  576. minData = dataList[0].Value
  577. for _, v := range dataList {
  578. if v.Value > maxData {
  579. maxData = v.Value
  580. }
  581. if v.Value < minData {
  582. minData = v.Value
  583. }
  584. }
  585. }
  586. edbInfoMapping.MaxData = maxData
  587. edbInfoMapping.MinData = minData
  588. xEdbIdValue = append(xEdbIdValue, edbInfoMapping.EdbInfoId)
  589. }
  590. yearMax := 0
  591. if dateType == utils.DateTypeNYears {
  592. for _, v := range edbInfoMappingList {
  593. dataList := v.DataList.([]*data_manage.EdbDataList)
  594. latestDate := dataList[len(dataList)-1].DataTime
  595. if latestDate != "" {
  596. lastDateT, tErr := time.Parse(utils.FormatDate, latestDate)
  597. if tErr != nil {
  598. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  599. return
  600. }
  601. if lastDateT.Year() > yearMax {
  602. yearMax = lastDateT.Year()
  603. }
  604. }
  605. }
  606. }
  607. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  608. if startDate != "" {
  609. for k, v := range edbInfoMappingList {
  610. var maxData, minData float64
  611. dataList := v.DataList.([]*data_manage.EdbDataList)
  612. newDataList := make([]*data_manage.EdbDataList, 0)
  613. if len(dataList) == 0 {
  614. newDataList = dataList
  615. } else {
  616. firstFlag := true
  617. for _, d := range dataList {
  618. if endDate != "" && d.DataTime > endDate {
  619. continue
  620. }
  621. if d.DataTime < startDate {
  622. continue
  623. }
  624. newDataList = append(newDataList, d)
  625. if firstFlag {
  626. maxData = d.Value
  627. minData = d.Value
  628. firstFlag = false
  629. } else {
  630. if d.Value > maxData {
  631. maxData = d.Value
  632. }
  633. if d.Value < minData {
  634. minData = d.Value
  635. }
  636. }
  637. }
  638. }
  639. edbInfoMappingList[k].DataList = newDataList
  640. edbInfoMappingList[k].MinData = minData
  641. edbInfoMappingList[k].MaxData = maxData
  642. }
  643. }
  644. dataResp = data_manage.ChartRangeAnalysisDataResp{ChartRangeAnalysisExtraConf: req, SeriesId: seriesMappingItem.FactorEdbSeriesId}
  645. // 查询配置关联关系
  646. if req.MultipleGraphConfigId > 0 {
  647. multipleGraphConfigEdbMappingList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  648. if e != nil && e.Error() != utils.ErrNoRow() {
  649. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  650. return
  651. }
  652. if len(multipleGraphConfigEdbMappingList) > 0 {
  653. // 查询指标是否已存在,如果已经被删除了,则返回0值
  654. edbIds := make([]int, 0)
  655. for _, v := range multipleGraphConfigEdbMappingList {
  656. edbIds = append(edbIds, v.EdbInfoId)
  657. }
  658. var condition string
  659. var pars []interface{}
  660. condition = " and edb_info_id in (" + utils.GetOrmInReplace(len(edbIds)) + ") "
  661. pars = append(pars, edbIds)
  662. count, e := data_manage.GetEdbInfoCountByCondition(condition, pars)
  663. if e != nil {
  664. err = fmt.Errorf("获取区间计算图表, 指标信息失败, Err:" + e.Error())
  665. return
  666. }
  667. if count > 0 {
  668. // 查询是否已经生成指标
  669. dataResp.ConfigEdbNum = len(multipleGraphConfigEdbMappingList)
  670. }
  671. }
  672. }
  673. edbList, err = GetChartEdbInfoFormat(chartInfoId, edbInfoMappingList)
  674. if err != nil {
  675. err = fmt.Errorf("获取区间计算图表, 完善指标信息失败, Err:" + err.Error())
  676. return
  677. }
  678. return
  679. }
  680. // getChartDataByEdbInfo 区间计算图表-根据指标信息获取x轴和y轴
  681. func getChartDataByEdbInfo(edbInfoMapping *data_manage.ChartEdbInfoMapping, req *data_manage.ChartRangeAnalysisExtraConf) (newEdbInfoMapping *data_manage.ChartEdbInfoMapping, err error) {
  682. newEdbInfoMapping = edbInfoMapping
  683. // 指标的开始日期和结束日期
  684. edbStartDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.StartDate, time.Local)
  685. //edbStartDate := edbStartDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  686. edbEndDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfoMapping.EndDate, time.Local)
  687. edbEndDate := edbEndDateTime.Format(utils.FormatDate)
  688. // 获取时间基准指标在时间区间内的值
  689. dataList := make([]*data_manage.EdbDataList, 0)
  690. switch edbInfoMapping.EdbInfoCategoryType {
  691. case 0:
  692. dataList, err = data_manage.GetEdbDataList(edbInfoMapping.Source, edbInfoMapping.SubSource, edbInfoMapping.EdbInfoId, "", "")
  693. case 1:
  694. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfoMapping.EdbInfoId, "", "", false)
  695. default:
  696. err = errors.New("指标base类型异常")
  697. return
  698. }
  699. /*var dataList data_manage.SortEdbDataList
  700. dataList = dataListTmp
  701. ascDataList := dataListTmp
  702. sort.Sort(dataList)*/
  703. dateList := make([]*data_manage.ChartRangeAnalysisDateDataItem, 0)
  704. switch req.DateRangeType {
  705. case 0:
  706. // 智能划分得到一个开始日期,和结束日期
  707. var startDateTime, endDateTime time.Time
  708. startDateTime = edbStartDateTime
  709. if req.AutoDateConf.IsAutoStartDate == 0 { //固定设置
  710. startDate := req.AutoDateConf.StartDate
  711. if startDate == "" {
  712. startDate = "2020-01-01"
  713. }
  714. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  715. } /*else {
  716. startConf := req.AutoDateConf.StartDateConf
  717. startDate := ""
  718. if startConf.BaseDateType == 0 { //
  719. startDate = edbEndDate
  720. } else if startConf.BaseDateType == 1 {
  721. startDate = time.Now().Format(utils.FormatDate)
  722. }
  723. if startConf.MoveForward > 0 {
  724. startDate = GetEdbDateByMoveForward(startConf.MoveForward, dataList)
  725. }
  726. if len(startConf.DateChange) > 0 {
  727. startDate, err = HandleEdbDateChange(startDate, startConf.DateChange)
  728. if err != nil {
  729. err = fmt.Errorf("智能划分开始日期处理失败:%s", err.Error())
  730. return
  731. }
  732. }
  733. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  734. }*/
  735. if req.AutoDateConf.IsAutoEndDate == 0 { //固定设置
  736. endDate := req.AutoDateConf.EndDate
  737. if endDate == "" {
  738. err = fmt.Errorf("智能划分截止日期处理失败:请输入截止日期")
  739. return
  740. }
  741. // todo 如果截止日期比指标日期还要大,则用指标的最新日期
  742. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  743. } else {
  744. endConf := req.AutoDateConf.EndDateConf
  745. endDate := edbEndDate
  746. if endConf.MoveForward > 0 {
  747. endDate = GetEdbDateByMoveForward(endDate, endConf.MoveForward, dataList)
  748. }
  749. if len(endConf.DateChange) > 0 {
  750. endDate, err = HandleEdbDateChange(endDate, endConf.DateChange)
  751. if err != nil {
  752. err = fmt.Errorf("智能划分结束日期处理失败:%s", err.Error())
  753. return
  754. }
  755. }
  756. endDateTime, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  757. }
  758. dateList = append(dateList, &data_manage.ChartRangeAnalysisDateDataItem{
  759. StartDate: startDateTime,
  760. EndDate: endDateTime})
  761. case 1:
  762. // 手工划分得到多个开始日期和结束日期(已排序)
  763. for _, v := range req.ManualDateConf {
  764. startDateT, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
  765. endDateT, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  766. tmp := &data_manage.ChartRangeAnalysisDateDataItem{
  767. StartDate: startDateT,
  768. EndDate: endDateT,
  769. }
  770. dateList = append(dateList, tmp)
  771. }
  772. case 2:
  773. // 跨年划分得到多个开始日期和结束日期
  774. startYear := edbStartDateTime.Year()
  775. endYear := edbEndDateTime.Year()
  776. startDay := req.YearDateConf.StartDay
  777. endDay := req.YearDateConf.EndDay
  778. for year := startYear; year <= endYear; year++ {
  779. startDate := fmt.Sprintf("%d-%s", year, startDay)
  780. endDate := fmt.Sprintf("%d-%s", year+1, endDay)
  781. startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  782. endDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  783. if startDateTime.Before(edbStartDateTime) {
  784. break
  785. }
  786. tmp := &data_manage.ChartRangeAnalysisDateDataItem{
  787. StartDate: startDateTime,
  788. EndDate: endDateTime,
  789. }
  790. dateList = append(dateList, tmp)
  791. }
  792. }
  793. // 根据日期,获取数据
  794. for _, v := range dateList {
  795. for _, vv := range dataList {
  796. dataTimeT, _ := time.ParseInLocation(utils.FormatDate, vv.DataTime, time.Local)
  797. if dataTimeT.After(v.StartDate) && dataTimeT.Before(v.EndDate) ||
  798. dataTimeT.Equal(v.StartDate) ||
  799. dataTimeT.Equal(v.EndDate) {
  800. v.DataList = append(v.DataList, vv)
  801. }
  802. }
  803. }
  804. // 根据时间区间类型来获取数据的计算窗口,然后再拼接成整段数据
  805. newDataList, err := HandleDataByCalculateType(dateList, dataList, req)
  806. if err != nil {
  807. return
  808. }
  809. if req.UnNormalDataDealType > 0 {
  810. switch req.UnNormalDataDealType { //0:不处理,1:剔除,2替换
  811. case 1:
  812. dealDataList := make([]*data_manage.EdbDataList, 0)
  813. for _, v := range newDataList {
  814. if !utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  815. dealDataList = append(dealDataList, v)
  816. }
  817. }
  818. newDataList = dealDataList
  819. case 2:
  820. for i, v := range newDataList {
  821. if utils.CompareFloatByOpStrings(req.UnNormalDataConf.Formula, v.Value, req.UnNormalDataConf.Value) {
  822. newDataList[i].Value = req.UnNormalDataConf.ReplaceValue
  823. }
  824. }
  825. }
  826. }
  827. if req.DataConvertType > 0 {
  828. // 数据转换类型 0不转, 1乘 2除 3对数
  829. switch req.DataConvertType {
  830. case 1:
  831. for i, v := range newDataList {
  832. val := v.Value * req.DataConvertConf.Value
  833. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  834. newDataList[i].Value = val
  835. }
  836. //item.MaxData = item.MaxData * v.ConvertValue
  837. //item.MinData = item.MinData * v.ConvertValue
  838. case 2:
  839. for i, v := range newDataList {
  840. val := v.Value / req.DataConvertConf.Value
  841. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  842. newDataList[i].Value = val
  843. }
  844. //item.MaxData = item.MaxData / v.ConvertValue
  845. //item.MinData = item.MinData / v.ConvertValue
  846. case 3:
  847. for i, v := range newDataList {
  848. if v.Value <= 0 {
  849. err = errors.New("数据中含有负数或0,无法对数运算")
  850. return
  851. }
  852. val := math.Log(v.Value) / math.Log(req.DataConvertConf.Value)
  853. val, _ = decimal.NewFromFloat(val).Round(4).Float64()
  854. newDataList[i].Value = val
  855. }
  856. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  857. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  858. }
  859. }
  860. newEdbInfoMapping.DataList = newDataList
  861. //时间处理
  862. return
  863. }
  864. // RollingCorrelationChartDataResp 滚动区间计算图表数据
  865. type RollingCorrelationChartDataResp struct {
  866. MaxData float64
  867. MinData float64
  868. LatestDate string `description:"真实数据的最后日期"`
  869. EdbInfoCategoryType int
  870. ChartColor string
  871. ChartStyle string
  872. PredictChartColor string
  873. ChartType int
  874. ChartWidth int
  875. EdbName string
  876. EdbNameEn string
  877. Unit string
  878. UnitEn string
  879. IsAxis int
  880. DataList []data_manage.EdbDataList
  881. }
  882. // ChartInfoRefresh 图表刷新
  883. func ChartInfoRefresh(chartInfoId int, uniqueCode string) (isAsync bool, err error) {
  884. var errMsg string
  885. defer func() {
  886. if err != nil {
  887. tips := fmt.Sprintf("CorrelationChartInfoRefresh: %s", errMsg)
  888. utils.FileLog.Info(tips)
  889. go alarm_msg.SendAlarmMsg(tips, 3)
  890. }
  891. }()
  892. var chartInfo *data_manage.ChartInfo
  893. if chartInfoId > 0 {
  894. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  895. if err != nil {
  896. if err.Error() == utils.ErrNoRow() {
  897. errMsg = "图表已被删除,请刷新页面"
  898. err = errors.New(errMsg)
  899. return
  900. }
  901. errMsg = "获取图表信息失败"
  902. err = errors.New("获取图表信息失败,Err:" + err.Error())
  903. return
  904. }
  905. } else {
  906. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  907. if err != nil {
  908. if err.Error() == utils.ErrNoRow() {
  909. errMsg = "图表已被删除,请刷新页面"
  910. err = errors.New(errMsg)
  911. return
  912. }
  913. errMsg = "获取图表信息失败"
  914. err = errors.New("获取图表信息失败,Err:" + err.Error())
  915. return
  916. }
  917. }
  918. // 1.刷新图表关联的指标
  919. mappings, e := data_manage.GetChartEdbMappingList(chartInfoId)
  920. if e != nil {
  921. utils.FileLog.Info(fmt.Sprintf("获取图表关联指标失败, err: %v", e))
  922. return
  923. }
  924. if len(mappings) == 0 {
  925. utils.FileLog.Info("图表无关联指标")
  926. return
  927. }
  928. var edbIds []int
  929. for _, v := range mappings {
  930. edbIds = append(edbIds, v.EdbInfoId)
  931. }
  932. if e, _ = data.EdbInfoRefreshAllFromBaseV3(edbIds, false, true, false); e != nil {
  933. utils.FileLog.Info(fmt.Sprintf("批量刷新指标失败, err: %v", e))
  934. return
  935. }
  936. // todo 重新计算
  937. // 区间计算图表配置校验
  938. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  939. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  940. if err != nil {
  941. errMsg = "配置信息错误"
  942. err = errors.New(errMsg + ", Err: " + err.Error())
  943. return
  944. }
  945. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  946. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartInfo.ChartInfoId)
  947. if err != nil {
  948. if err.Error() == utils.ErrNoRow() {
  949. err = nil
  950. } else {
  951. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  952. return
  953. }
  954. } else {
  955. _, e = FactorEdbStepCalculateRange(seriesMappingItem.FactorEdbSeriesId, edbIds, extraConfig, true)
  956. if e != nil {
  957. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  958. return
  959. }
  960. }
  961. // 4.清除图表缓存
  962. key := utils.HZ_CHART_LIB_DETAIL + uniqueCode
  963. _ = utils.Rc.Delete(key)
  964. return
  965. }
  966. // AddChartInfo 添加图表
  967. func AddChartInfo(req data_manage.AddChartInfoReq, source int, sysUser *system.Admin, lang string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  968. isSendEmail = true
  969. req.ChartName = strings.Trim(req.ChartName, " ")
  970. if req.ChartName == "" {
  971. errMsg = "请填写图表名称!"
  972. err = errors.New(errMsg)
  973. isSendEmail = false
  974. return
  975. }
  976. if req.ChartClassifyId <= 0 {
  977. errMsg = "分类参数错误!"
  978. err = errors.New(errMsg)
  979. isSendEmail = false
  980. return
  981. }
  982. // 区间计算图表配置校验
  983. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  984. if req.ExtraConfig == `` {
  985. errMsg = "配置信息错误"
  986. err = errors.New(errMsg)
  987. return
  988. }
  989. err = json.Unmarshal([]byte(req.ExtraConfig), &extraConfig)
  990. if err != nil {
  991. errMsg = "配置信息错误"
  992. err = errors.New(errMsg + ", Err: " + err.Error())
  993. return
  994. }
  995. if len(req.ChartEdbInfoList) <= 0 {
  996. errMsg = "请选择指标"
  997. err = errors.New(errMsg)
  998. isSendEmail = false
  999. return
  1000. }
  1001. if len(req.ChartEdbInfoList) > 100 {
  1002. errMsg = "添加指标总数量不得超过100"
  1003. err = errors.New(errMsg)
  1004. isSendEmail = false
  1005. return
  1006. }
  1007. err, errMsg, isSendEmail = CheckChartRangeExtraConfig(extraConfig)
  1008. if err != nil {
  1009. return
  1010. }
  1011. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1012. if err != nil {
  1013. if err.Error() == utils.ErrNoRow() {
  1014. errMsg = "分类不存在"
  1015. err = errors.New(errMsg)
  1016. isSendEmail = false
  1017. return
  1018. }
  1019. errMsg = "获取分类信息失败"
  1020. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1021. return
  1022. }
  1023. if chartClassify == nil {
  1024. errMsg = "分类不存在"
  1025. err = errors.New(errMsg)
  1026. isSendEmail = false
  1027. return
  1028. }
  1029. var edbInfoIdArr []int
  1030. for _, v := range req.ChartEdbInfoList {
  1031. edbInfoId := v.EdbInfoId
  1032. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1033. if tmpErr != nil {
  1034. if tmpErr.Error() == utils.ErrNoRow() {
  1035. errMsg = "指标不存在!"
  1036. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1037. return
  1038. } else {
  1039. errMsg = "获取指标信息失败!"
  1040. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1041. return
  1042. }
  1043. }
  1044. if edbInfo == nil {
  1045. errMsg = "指标已被删除,请重新选择!"
  1046. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1047. return
  1048. } else {
  1049. if edbInfo.EdbInfoId <= 0 {
  1050. errMsg = "指标已被删除,请重新选择!"
  1051. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1052. return
  1053. }
  1054. }
  1055. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1056. edbInfo.EdbNameSource = edbInfo.EdbName
  1057. }
  1058. sort.Ints(edbInfoIdArr)
  1059. var edbInfoIdArrStr []string
  1060. for _, v := range edbInfoIdArr {
  1061. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1062. }
  1063. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1064. var chartInfoId int
  1065. // 判断图表是否存在
  1066. {
  1067. var condition string
  1068. var pars []interface{}
  1069. switch lang {
  1070. case utils.EnLangVersion:
  1071. condition += " AND chart_name_en = ? AND source = ? "
  1072. default:
  1073. condition += " AND chart_name=? AND source = ? "
  1074. }
  1075. pars = append(pars, req.ChartName, source)
  1076. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  1077. if tmpErr != nil {
  1078. errMsg = "判断图表名称是否存在失败"
  1079. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  1080. return
  1081. }
  1082. if count > 0 {
  1083. errMsg = "图表已存在,请重新填写"
  1084. err = errors.New(errMsg)
  1085. isSendEmail = false
  1086. return
  1087. }
  1088. }
  1089. disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  1090. chartInfo = new(data_manage.ChartInfo)
  1091. chartInfo.ChartName = req.ChartName
  1092. chartInfo.ChartNameEn = req.ChartName
  1093. chartInfo.EdbInfoIds = edbInfoIdStr
  1094. chartInfo.ChartClassifyId = req.ChartClassifyId
  1095. chartInfo.SysUserId = sysUser.AdminId
  1096. chartInfo.SysUserRealName = sysUser.RealName
  1097. chartInfo.CreateTime = time.Now()
  1098. chartInfo.ModifyTime = time.Now()
  1099. chartInfo.IsSetName = 0
  1100. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1101. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  1102. chartInfo.ChartType = 1 // 区间计算图
  1103. chartInfo.Calendar = "公历"
  1104. chartInfo.DateType = req.DateType
  1105. chartInfo.StartDate = req.StartDate
  1106. chartInfo.EndDate = req.EndDate
  1107. chartInfo.StartYear = req.StartYear
  1108. chartInfo.SeasonStartDate = req.StartDate
  1109. chartInfo.SeasonEndDate = req.EndDate
  1110. chartInfo.LeftMin = req.LeftMin
  1111. chartInfo.LeftMax = req.LeftMax
  1112. chartInfo.RightMin = req.RightMin
  1113. chartInfo.RightMax = req.RightMax
  1114. chartInfo.Disabled = disableVal
  1115. chartInfo.Source = source
  1116. chartInfo.ChartThemeId = req.ChartThemeId
  1117. chartInfo.SourcesFrom = req.SourcesFrom
  1118. chartInfo.Instructions = req.Instructions
  1119. chartInfo.MarkersLines = req.MarkersLines
  1120. chartInfo.MarkersAreas = req.MarkersAreas
  1121. if req.ExtraConfig != "" {
  1122. chartInfo.ExtraConfig = req.ExtraConfig
  1123. }
  1124. // 指标信息
  1125. mapList := make([]*data_manage.ChartEdbMapping, 0)
  1126. for _, v := range req.ChartEdbInfoList {
  1127. // todo 指标名称修改
  1128. mapItem := new(data_manage.ChartEdbMapping)
  1129. mapItem.EdbInfoId = v.EdbInfoId
  1130. mapItem.EdbAliasName = v.EdbAliasName
  1131. mapItem.IsAxis = v.IsAxis
  1132. mapItem.CreateTime = time.Now()
  1133. mapItem.ModifyTime = time.Now()
  1134. edbTimestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1135. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + edbTimestamp + "_" + strconv.Itoa(v.EdbInfoId))
  1136. mapItem.IsOrder = false
  1137. mapItem.EdbInfoType = 1
  1138. mapItem.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
  1139. mapList = append(mapList, mapItem)
  1140. }
  1141. // 新增图表和指标mapping
  1142. chartInfoId, e := data_manage.CreateRangeChartAndEdb(chartInfo, mapList)
  1143. if e != nil {
  1144. errMsg = "操作失败"
  1145. err = errors.New("新增区间计算图表失败, Err: " + e.Error())
  1146. return
  1147. }
  1148. // 把系列和图表绑定
  1149. if extraConfig.EdbInfoMode == 1 {
  1150. // 新增指标系列
  1151. err = AddSeries(edbInfoIdArr, chartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, req.ExtraConfig)
  1152. if err != nil {
  1153. errMsg = "操作失败"
  1154. err = errors.New("新增区间计算图表失败, Err: " + err.Error())
  1155. return
  1156. }
  1157. //todo 如果保存失败是否要删除
  1158. }
  1159. /*//添加配置信息
  1160. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  1161. //MultipleGraphConfigId: 0,
  1162. SysUserId: sysUser.AdminId,
  1163. SysUserRealName: sysUser.RealName,
  1164. ModifyTime: time.Now(),
  1165. CreateTime: time.Now(),
  1166. }
  1167. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  1168. if err != nil {
  1169. errMsg = "操作失败"
  1170. err = errors.New("新增区间计算图表配置失败, Err: " + err.Error())
  1171. return
  1172. }*/
  1173. if extraConfig.MultipleGraphConfigId > 0 {
  1174. multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  1175. //Id: 0,
  1176. MultipleGraphConfigId: extraConfig.MultipleGraphConfigId,
  1177. ChartInfoId: chartInfo.ChartInfoId,
  1178. Source: utils.CHART_SOURCE_RANGE_ANALYSIS,
  1179. ModifyTime: time.Now(),
  1180. CreateTime: time.Now(),
  1181. }
  1182. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1183. if err != nil {
  1184. errMsg = "操作失败"
  1185. err = errors.New("新增区间计算图表和配置关联关系失败, Err: " + err.Error())
  1186. return
  1187. }
  1188. }
  1189. // 添加指标引用记录
  1190. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartInfo)
  1191. //添加es数据
  1192. go data.EsAddOrEditChartInfo(chartInfoId)
  1193. return
  1194. }
  1195. // EditChartInfo 编辑图表
  1196. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin, lang string) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1197. isSendEmail = true
  1198. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  1199. if err != nil {
  1200. if err.Error() == utils.ErrNoRow() {
  1201. errMsg = "图表已被删除,请刷新页面"
  1202. err = errors.New(errMsg)
  1203. isSendEmail = false
  1204. return
  1205. }
  1206. errMsg = "获取图表信息失败"
  1207. err = errors.New("获取图表信息失败,Err:" + err.Error())
  1208. return
  1209. }
  1210. if chartItem.Source != utils.CHART_SOURCE_RANGE_ANALYSIS {
  1211. errMsg = "该图不是区间计算图表!"
  1212. err = errors.New(errMsg)
  1213. isSendEmail = false
  1214. return
  1215. }
  1216. req.ChartName = strings.Trim(req.ChartName, " ")
  1217. if req.ChartClassifyId <= 0 {
  1218. errMsg = "分类参数错误!"
  1219. err = errors.New(errMsg)
  1220. isSendEmail = false
  1221. return
  1222. }
  1223. // 区间计算图表配置校验
  1224. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  1225. if req.ExtraConfig == `` {
  1226. errMsg = "配置信息错误"
  1227. err = errors.New(errMsg)
  1228. return
  1229. }
  1230. err = json.Unmarshal([]byte(req.ExtraConfig), &extraConfig)
  1231. if err != nil {
  1232. errMsg = "配置信息错误"
  1233. err = errors.New(errMsg)
  1234. return
  1235. }
  1236. if len(req.ChartEdbInfoList) <= 0 {
  1237. errMsg = "请选择指标"
  1238. err = errors.New(errMsg)
  1239. isSendEmail = false
  1240. return
  1241. }
  1242. extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
  1243. if len(req.ChartEdbInfoList) > 100 {
  1244. errMsg = "添加指标总数量不得超过100"
  1245. err = errors.New(errMsg)
  1246. isSendEmail = false
  1247. return
  1248. }
  1249. err, errMsg, isSendEmail = CheckChartRangeExtraConfig(extraConfig)
  1250. if err != nil {
  1251. return
  1252. }
  1253. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1254. if err != nil {
  1255. if err.Error() == utils.ErrNoRow() {
  1256. errMsg = "分类不存在"
  1257. err = errors.New(errMsg)
  1258. isSendEmail = false
  1259. return
  1260. }
  1261. errMsg = "获取分类信息失败"
  1262. err = errors.New("获取分类信息失败,Err:" + err.Error())
  1263. return
  1264. }
  1265. if chartClassify == nil {
  1266. errMsg = "分类不存在"
  1267. err = errors.New(errMsg)
  1268. isSendEmail = false
  1269. return
  1270. }
  1271. // 图表操作权限
  1272. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
  1273. if !ok {
  1274. errMsg = "没有该图表的操作权限"
  1275. err = errors.New(errMsg)
  1276. isSendEmail = false
  1277. return
  1278. }
  1279. var edbInfoIdArr []int
  1280. for _, v := range req.ChartEdbInfoList {
  1281. edbInfoId := v.EdbInfoId
  1282. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1283. if tmpErr != nil {
  1284. if tmpErr.Error() == utils.ErrNoRow() {
  1285. errMsg = "图表不存在!"
  1286. err = errors.New("图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1287. return
  1288. } else {
  1289. errMsg = "获取图表信息失败!"
  1290. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1291. return
  1292. }
  1293. }
  1294. if edbInfo == nil {
  1295. errMsg = "指标不存在!"
  1296. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1297. return
  1298. }
  1299. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1300. }
  1301. sort.Ints(edbInfoIdArr)
  1302. var edbInfoIdArrStr []string
  1303. for _, v := range edbInfoIdArr {
  1304. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1305. }
  1306. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1307. //判断图表是否存在
  1308. {
  1309. var condition string
  1310. var pars []interface{}
  1311. condition += " AND chart_info_id <> ? "
  1312. pars = append(pars, req.ChartInfoId)
  1313. switch lang {
  1314. case utils.EnLangVersion:
  1315. condition += " AND chart_name_en = ? AND source = ? "
  1316. default:
  1317. condition += " AND chart_name=? AND source = ? "
  1318. }
  1319. pars = append(pars, req.ChartName, chartItem.Source)
  1320. count, tmpErr := data_manage.GetChartInfoCountByCondition(condition, pars)
  1321. if tmpErr != nil {
  1322. errMsg = "判断图表名称是否存在失败"
  1323. err = errors.New("判断图表名称是否存在失败,Err:" + tmpErr.Error())
  1324. return
  1325. }
  1326. if count > 0 {
  1327. errMsg = "图表已存在,请重新填写"
  1328. err = errors.New(errMsg)
  1329. isSendEmail = false
  1330. return
  1331. }
  1332. }
  1333. // 修改图表与指标mapping
  1334. req.ChartType = 1
  1335. dateType := req.DateType
  1336. err = data_manage.EditRangeChartInfoAndMapping(&req, edbInfoIdStr, lang, "公历", dateType, 0, ``)
  1337. if err != nil {
  1338. errMsg = "保存失败"
  1339. err = errors.New("保存失败,Err:" + err.Error())
  1340. return
  1341. }
  1342. addSeries := false
  1343. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  1344. seriesMappingItem, err := chartSeriesOb.GetItemByChartInfoId(chartItem.ChartInfoId)
  1345. if err != nil {
  1346. if err.Error() == utils.ErrNoRow() {
  1347. err = nil
  1348. addSeries = true
  1349. } else {
  1350. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  1351. return
  1352. }
  1353. }
  1354. if extraConfig.EdbInfoMode == 1 {
  1355. if addSeries {
  1356. // 新增指标系列
  1357. err = AddSeries(edbInfoIdArr, chartItem.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, req.ExtraConfig)
  1358. if err != nil {
  1359. errMsg = "操作失败"
  1360. err = errors.New("新增区间计算图表失败, Err: " + err.Error())
  1361. return
  1362. }
  1363. } else {
  1364. err = EditSeries(seriesMappingItem, edbInfoIdArr, extraConfig, req.ExtraConfig, true)
  1365. if err != nil {
  1366. errMsg = "保存失败"
  1367. err = errors.New("保存失败,Err:" + err.Error())
  1368. return
  1369. }
  1370. }
  1371. // todo 编辑失败处理
  1372. } else if extraConfig.EdbInfoMode == 0 {
  1373. if !addSeries {
  1374. // 删除相关的系列
  1375. factorSeriesOb := new(data_manage.FactorEdbSeries)
  1376. err = factorSeriesOb.RemoveSeriesAndMappingByFactorEdbSeriesId(seriesMappingItem)
  1377. if err != nil {
  1378. errMsg = "保存失败"
  1379. err = errors.New("保存失败,Err:" + err.Error())
  1380. return
  1381. }
  1382. }
  1383. }
  1384. resp := new(data_manage.AddChartInfoResp)
  1385. resp.ChartInfoId = chartItem.ChartInfoId
  1386. resp.UniqueCode = chartItem.UniqueCode
  1387. resp.ChartType = req.ChartType
  1388. // 添加指标引用记录
  1389. _ = data.SaveChartEdbInfoRelation(edbInfoIdArr, chartItem)
  1390. //添加es数据
  1391. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1392. //修改my eta es数据
  1393. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1394. return
  1395. }
  1396. // CopyChartInfo 复制图表
  1397. 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) {
  1398. isSendEmail = true
  1399. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1400. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  1401. err = json.Unmarshal([]byte(oldChartInfo.ExtraConfig), &extraConfig)
  1402. if err != nil {
  1403. errMsg = "配置信息错误"
  1404. err = errors.New(errMsg + ", Err: " + err.Error())
  1405. return
  1406. }
  1407. //新增配置,并绑定图表
  1408. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  1409. //MultipleGraphConfigId: 0,
  1410. SysUserId: sysUser.AdminId,
  1411. SysUserRealName: sysUser.RealName,
  1412. ModifyTime: time.Now(),
  1413. CreateTime: time.Now(),
  1414. }
  1415. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  1416. if err != nil {
  1417. errMsg = "新增区间计算图表配置失败"
  1418. err = errors.New(errMsg + ", Err: " + err.Error())
  1419. return
  1420. }
  1421. extraConfig.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  1422. newExtraConfigBytes, err := json.Marshal(extraConfig)
  1423. if err != nil {
  1424. errMsg = "配置信息错误"
  1425. err = errors.New(errMsg + ", Err: " + err.Error())
  1426. return
  1427. }
  1428. newExtraConfig := string(newExtraConfigBytes)
  1429. chartInfo = &data_manage.ChartInfo{
  1430. ChartInfoId: 0,
  1431. ChartName: chartName,
  1432. ChartClassifyId: classifyId,
  1433. SysUserId: sysUser.AdminId,
  1434. SysUserRealName: sysUser.RealName,
  1435. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  1436. CreateTime: time.Now(),
  1437. ModifyTime: time.Now(),
  1438. DateType: oldChartInfo.DateType,
  1439. StartDate: oldChartInfo.StartDate,
  1440. EndDate: oldChartInfo.EndDate,
  1441. IsSetName: oldChartInfo.IsSetName,
  1442. EdbInfoIds: oldChartInfo.EdbInfoIds,
  1443. ChartType: oldChartInfo.ChartType,
  1444. Calendar: oldChartInfo.Calendar,
  1445. SeasonStartDate: oldChartInfo.SeasonStartDate,
  1446. SeasonEndDate: oldChartInfo.SeasonEndDate,
  1447. ChartImage: oldChartInfo.ChartImage,
  1448. BarConfig: oldChartInfo.BarConfig,
  1449. //Sort: sort,
  1450. LeftMin: oldChartInfo.LeftMin,
  1451. LeftMax: oldChartInfo.LeftMax,
  1452. RightMin: oldChartInfo.RightMin,
  1453. RightMax: oldChartInfo.RightMax,
  1454. Right2Min: oldChartInfo.Right2Min,
  1455. Right2Max: oldChartInfo.Right2Max,
  1456. Disabled: oldChartInfo.Disabled,
  1457. Source: oldChartInfo.Source,
  1458. ExtraConfig: newExtraConfig,
  1459. SeasonExtraConfig: oldChartInfo.SeasonExtraConfig,
  1460. StartYear: oldChartInfo.StartYear,
  1461. Unit: oldChartInfo.Unit,
  1462. UnitEn: oldChartInfo.UnitEn,
  1463. ChartThemeId: oldChartInfo.ChartThemeId,
  1464. SourcesFrom: oldChartInfo.SourcesFrom,
  1465. Instructions: oldChartInfo.Instructions,
  1466. MarkersLines: oldChartInfo.MarkersLines,
  1467. MarkersAreas: oldChartInfo.MarkersAreas,
  1468. }
  1469. newId, err := data_manage.AddChartInfo(chartInfo)
  1470. if err != nil {
  1471. err = fmt.Errorf("保存失败,Err:%s", err.Error())
  1472. return
  1473. }
  1474. chartInfo.ChartInfoId = int(newId)
  1475. edbInfoMappingList, err := data_manage.GetChartMappingList(oldChartInfo.ChartInfoId)
  1476. if err != nil {
  1477. err = fmt.Errorf("获取图表,指标信息失败,Err:" + err.Error())
  1478. return
  1479. }
  1480. // 添加图表与指标的关联关系
  1481. edbInfoIdArr := make([]int, 0)
  1482. {
  1483. mapList := make([]*data_manage.ChartEdbMapping, 0)
  1484. for _, v := range edbInfoMappingList {
  1485. edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
  1486. timestamp = strconv.FormatInt(time.Now().UnixNano(), 10)
  1487. mapItem := &data_manage.ChartEdbMapping{
  1488. //ChartEdbMappingId: 0,
  1489. ChartInfoId: chartInfo.ChartInfoId,
  1490. EdbInfoId: v.EdbInfoId,
  1491. CreateTime: time.Now(),
  1492. ModifyTime: time.Now(),
  1493. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  1494. MaxData: v.MaxData,
  1495. MinData: v.MinData,
  1496. IsOrder: v.IsOrder,
  1497. IsAxis: v.IsAxis,
  1498. EdbInfoType: v.EdbInfoType,
  1499. LeadValue: v.LeadValue,
  1500. LeadUnit: v.LeadUnit,
  1501. ChartStyle: v.ChartStyle,
  1502. ChartColor: v.ChartColor,
  1503. ChartWidth: v.ChartWidth,
  1504. Source: v.Source,
  1505. EdbAliasName: v.EdbAliasName,
  1506. IsConvert: v.IsConvert,
  1507. ConvertType: v.ConvertType,
  1508. ConvertValue: v.ConvertValue,
  1509. ConvertUnit: v.ConvertEnUnit,
  1510. ConvertEnUnit: v.ConvertEnUnit,
  1511. }
  1512. mapList = append(mapList, mapItem)
  1513. }
  1514. err = data_manage.AddChartEdbMapping(mapList)
  1515. if err != nil {
  1516. err = fmt.Errorf("保存失败,Err:%s", err.Error())
  1517. return
  1518. }
  1519. }
  1520. // 添加系列和图表映射
  1521. chartSeriesOb := new(data_manage.FactorEdbSeriesChartMapping)
  1522. _, err = chartSeriesOb.GetItemByChartInfoId(oldChartInfo.ChartInfoId)
  1523. if err != nil {
  1524. if err.Error() == utils.ErrNoRow() {
  1525. err = nil
  1526. } else {
  1527. err = fmt.Errorf("获取图表关联失败, Err: " + err.Error())
  1528. return
  1529. }
  1530. } else {
  1531. // 新增指标系列
  1532. // 区间计算图表配置校验
  1533. err = AddSeries(edbInfoIdArr, chartInfo.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS, extraConfig, chartInfo.ExtraConfig)
  1534. if err != nil {
  1535. errMsg = "操作失败"
  1536. err = errors.New("新增区间计算图表失败, Err: " + err.Error())
  1537. return
  1538. }
  1539. }
  1540. multipleGraphConfigChartMapping := &data_manage.MultipleGraphConfigChartMapping{
  1541. //Id: 0,
  1542. MultipleGraphConfigId: extraConfig.MultipleGraphConfigId,
  1543. ChartInfoId: chartInfo.ChartInfoId,
  1544. Source: utils.CHART_SOURCE_RANGE_ANALYSIS,
  1545. ModifyTime: time.Now(),
  1546. CreateTime: time.Now(),
  1547. }
  1548. err = data_manage.AddMultipleGraphConfigChartMapping(multipleGraphConfigChartMapping)
  1549. if err != nil {
  1550. errMsg = "操作失败"
  1551. err = errors.New("新增区间计算图表和配置关联关系失败, Err: " + err.Error())
  1552. return
  1553. }
  1554. //添加es数据
  1555. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1556. return
  1557. }
  1558. /*
  1559. // CalculateCorrelation 计算区间计算-获取x轴和y轴
  1560. func CalculateCorrelation(leadValue int, leadUnit, frequencyA, frequencyB string, dataListA, dataListB []*data_manage.EdbDataList) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
  1561. xData := make([]int, 0)
  1562. yData := make([]float64, 0)
  1563. if leadValue == 0 {
  1564. xData = append(xData, 0)
  1565. }
  1566. if leadValue > 0 {
  1567. leadMin := 0 - leadValue
  1568. xLen := 2*leadValue + 1
  1569. for i := 0; i < xLen; i++ {
  1570. n := leadMin + i
  1571. xData = append(xData, n)
  1572. }
  1573. }
  1574. // 计算窗口,不包含第一天
  1575. //startDateTime, _ := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  1576. //startDate = startDateTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  1577. //// 2023-03-02 时间序列始终以指标B为基准, 始终是A进行平移
  1578. //baseEdbInfo := edbInfoMappingB
  1579. //changeEdbInfo := edbInfoMappingA
  1580. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  1581. //baseEdbInfo := edbInfoMappingA
  1582. //changeEdbInfo := edbInfoMappingB
  1583. // 获取时间基准指标在时间区间内的值
  1584. //aDataList := make([]*data_manage.EdbDataList, 0)
  1585. //switch baseEdbInfo.EdbInfoCategoryType {
  1586. //case 0:
  1587. // aDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, startDate, endDate)
  1588. //case 1:
  1589. // _, aDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, startDate, endDate, false)
  1590. //default:
  1591. // err = errors.New("指标base类型异常")
  1592. // return
  1593. //}
  1594. //
  1595. //// 获取变频指标所有日期的值, 插值法完善数据
  1596. //bDataList := make([]*data_manage.EdbDataList, 0)
  1597. //switch changeEdbInfo.EdbInfoCategoryType {
  1598. //case 0:
  1599. // bDataList, err = data_manage.GetEdbDataList(changeEdbInfo.Source, changeEdbInfo.SubSource, changeEdbInfo.EdbInfoId, "", "")
  1600. //case 1:
  1601. // _, bDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(changeEdbInfo.EdbInfoId, "", "", false)
  1602. //default:
  1603. // err = errors.New("指标change类型异常")
  1604. // return
  1605. //}
  1606. //changeDataMap := make(map[string]float64)
  1607. //newChangeDataList, e := HandleDataByLinearRegression(bDataList, changeDataMap)
  1608. //if e != nil {
  1609. // err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  1610. // return
  1611. //}
  1612. // 2023-03-17 时间序列始终以指标A为基准, 始终是B进行平移
  1613. baseDataList := make([]*data_manage.EdbDataList, 0)
  1614. baseDataMap := make(map[string]float64)
  1615. changeDataList := make([]*data_manage.EdbDataList, 0)
  1616. changeDataMap := make(map[string]float64)
  1617. // 先把低频指标升频为高频
  1618. {
  1619. frequencyIntMap := map[string]int{
  1620. "日度": 1,
  1621. "周度": 2,
  1622. "旬度": 3,
  1623. "月度": 4,
  1624. "季度": 5,
  1625. "年度": 6,
  1626. }
  1627. // 如果A指标是高频,那么就需要对B指标进行升频
  1628. if frequencyIntMap[frequencyA] < frequencyIntMap[frequencyB] {
  1629. tmpNewChangeDataList, e := HandleDataByLinearRegression(dataListB, changeDataMap)
  1630. if e != nil {
  1631. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  1632. return
  1633. }
  1634. changeDataList = tmpNewChangeDataList
  1635. baseDataList = dataListA
  1636. for _, v := range baseDataList {
  1637. baseDataMap[v.DataTime] = v.Value
  1638. }
  1639. } else if frequencyIntMap[frequencyA] > frequencyIntMap[frequencyB] {
  1640. // 如果B指标是高频,那么就需要对A指标进行升频
  1641. tmpNewChangeDataList, e := HandleDataByLinearRegression(dataListA, baseDataMap)
  1642. if e != nil {
  1643. err = fmt.Errorf("获取变频指标插值法Map失败, Err: %s", e.Error())
  1644. return
  1645. }
  1646. baseDataList = tmpNewChangeDataList
  1647. changeDataList = dataListB
  1648. for _, v := range changeDataList {
  1649. changeDataMap[v.DataTime] = v.Value
  1650. }
  1651. } else {
  1652. baseDataList = dataListA
  1653. for _, v := range baseDataList {
  1654. baseDataMap[v.DataTime] = v.Value
  1655. }
  1656. changeDataList = dataListB
  1657. for _, v := range changeDataList {
  1658. changeDataMap[v.DataTime] = v.Value
  1659. }
  1660. }
  1661. }
  1662. // 计算不领先也不滞后时的相关系数
  1663. baseCalculateData := make([]float64, 0)
  1664. baseDataTimeArr := make([]string, 0)
  1665. for i := range baseDataList {
  1666. baseDataTimeArr = append(baseDataTimeArr, baseDataList[i].DataTime)
  1667. baseCalculateData = append(baseCalculateData, baseDataList[i].Value)
  1668. }
  1669. //zeroBaseData := make([]float64, 0)
  1670. //zeroCalculateData := make([]float64, 0)
  1671. //for i := range baseDataTimeArr {
  1672. // tmpBaseVal, ok1 := baseDataMap[baseDataTimeArr[i]]
  1673. // tmpCalculateVal, ok2 := changeDataMap[baseDataTimeArr[i]]
  1674. // if ok1 && ok2 {
  1675. // zeroBaseData = append(zeroBaseData, tmpBaseVal)
  1676. // zeroCalculateData = append(zeroCalculateData, tmpCalculateVal)
  1677. // }
  1678. //}
  1679. //if len(zeroBaseData) != len(zeroCalculateData) {
  1680. // err = fmt.Errorf("相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(zeroCalculateData))
  1681. // return
  1682. //}
  1683. //zeroRatio := utils.CalculateCorrelationByIntArr(zeroBaseData, zeroCalculateData)
  1684. //if leadValue == 0 {
  1685. // yData = append(yData, zeroRatio)
  1686. //}
  1687. // 计算领先/滞后N期
  1688. if leadValue > 0 {
  1689. // 平移变频指标领先/滞后的日期(单位天)
  1690. moveUnitDays := utils.FrequencyDaysMap[leadUnit]
  1691. for i := range xData {
  1692. //if xData[i] == 0 {
  1693. // yData = append(yData, zeroRatio)
  1694. // continue
  1695. //}
  1696. xCalculateData := make([]float64, 0)
  1697. yCalculateData := make([]float64, 0)
  1698. // 平移指定天数
  1699. mDays := int(moveUnitDays) * xData[i]
  1700. _, dMap := MoveDataDaysToNewDataList(changeDataList, mDays)
  1701. // 取出对应的基准日期的值
  1702. for i2 := range baseDataTimeArr {
  1703. tmpDate := baseDataTimeArr[i2]
  1704. if yVal, ok := dMap[tmpDate]; ok {
  1705. xCalculateData = append(xCalculateData, baseCalculateData[i2])
  1706. yCalculateData = append(yCalculateData, yVal)
  1707. }
  1708. }
  1709. if len(yCalculateData) <= 0 {
  1710. //err = fmt.Errorf("领先滞后相关系数两组序列元素数不一致, %d-%d", len(baseCalculateData), len(yCalculateData))
  1711. //return
  1712. // 领先滞后后,没有可以计算的数据了
  1713. continue
  1714. }
  1715. // 公式计算出领先/滞后频度对应点的区间计算系数
  1716. ratio := utils.CalculateCorrelationByIntArr(xCalculateData, yCalculateData)
  1717. yData = append(yData, ratio)
  1718. }
  1719. }
  1720. xEdbIdValue = xData
  1721. yDataList = make([]data_manage.YData, 0)
  1722. yDate := "0000-00-00"
  1723. yDataList = append(yDataList, data_manage.YData{
  1724. Date: yDate,
  1725. Value: yData,
  1726. })
  1727. return
  1728. }
  1729. // GetFactorChartDataByChartId 获取多因子区间计算图表数据
  1730. func GetFactorChartDataByChartId(chartInfoId int, extraConfig string) (xEdbIdValue []int, yDataList []data_manage.YData, err error) {
  1731. if chartInfoId <= 0 {
  1732. return
  1733. }
  1734. // 指标对应的图例
  1735. extra := new(data_manage.CorrelationChartInfoExtraConfig)
  1736. if extraConfig != "" {
  1737. if e := json.Unmarshal([]byte(extraConfig), extra); e != nil {
  1738. err = fmt.Errorf("解析图表额外配置失败, err: %v", e)
  1739. return
  1740. }
  1741. }
  1742. legends := make(map[string]*data_manage.CorrelationChartLegend)
  1743. if extra != nil {
  1744. for _, v := range extra.LegendConfig {
  1745. s := fmt.Sprintf("%d-%d", v.SeriesId, v.EdbInfoId)
  1746. legends[s] = v
  1747. }
  1748. }
  1749. // 获取图表引用到的系列指标
  1750. chartMappingOb := new(data_manage.FactorEdbSeriesChartMapping)
  1751. cond := fmt.Sprintf(" AND %s = ? AND %s = 1", chartMappingOb.Cols().ChartInfoId, chartMappingOb.Cols().EdbUsed)
  1752. pars := make([]interface{}, 0)
  1753. pars = append(pars, chartInfoId)
  1754. chartMappings, e := chartMappingOb.GetItemsByCondition(cond, pars, []string{}, "")
  1755. if e != nil {
  1756. err = fmt.Errorf("获取图表引用系列指标失败")
  1757. return
  1758. }
  1759. // 取出计算结果
  1760. yDataList = make([]data_manage.YData, 0)
  1761. yDate := "0000-00-00"
  1762. for k, m := range chartMappings {
  1763. var values []data_manage.FactorEdbSeriesCorrelationMatrixValues
  1764. if m.CalculateData != "" {
  1765. e = json.Unmarshal([]byte(m.CalculateData), &values)
  1766. if e != nil {
  1767. err = fmt.Errorf("系列指标计算数据有误, err: %v", e)
  1768. return
  1769. }
  1770. }
  1771. var y []float64
  1772. for _, v := range values {
  1773. if k == 0 {
  1774. xEdbIdValue = append(xEdbIdValue, v.XData)
  1775. }
  1776. y = append(y, v.YData)
  1777. }
  1778. var yData data_manage.YData
  1779. yData.Date = yDate
  1780. yData.Value = y
  1781. yData.SeriesEdb.SeriesId = m.FactorEdbSeriesId
  1782. yData.SeriesEdb.EdbInfoId = m.EdbInfoId
  1783. // 图例
  1784. s := fmt.Sprintf("%d-%d", m.FactorEdbSeriesId, m.EdbInfoId)
  1785. legend := legends[s]
  1786. if legend != nil {
  1787. yData.Name = legend.LegendName
  1788. yData.Color = legend.Color
  1789. }
  1790. yDataList = append(yDataList, yData)
  1791. }
  1792. return
  1793. }
  1794. // FormatChartEdbInfoMappings 补充指标信息
  1795. func FormatChartEdbInfoMappings(chartInfoId int, mappings []*data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
  1796. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  1797. if len(mappings) == 0 {
  1798. return
  1799. }
  1800. for _, v := range mappings {
  1801. if chartInfoId <= 0 {
  1802. v.IsAxis = 1
  1803. v.LeadValue = 0
  1804. v.LeadUnit = ""
  1805. v.ChartEdbMappingId = 0
  1806. v.ChartInfoId = 0
  1807. v.IsOrder = false
  1808. v.EdbInfoType = 1
  1809. v.ChartStyle = ""
  1810. v.ChartColor = ""
  1811. v.ChartWidth = 0
  1812. } else {
  1813. v.LeadUnitEn = data.GetLeadUnitEn(v.LeadUnit)
  1814. v.LeadUnitEn = data.GetLeadUnitEn(v.LeadUnit)
  1815. }
  1816. v.FrequencyEn = data.GetFrequencyEn(v.Frequency)
  1817. if v.Unit == `无` {
  1818. v.Unit = ``
  1819. }
  1820. edbList = append(edbList, v)
  1821. }
  1822. return
  1823. }*/
  1824. func GetEdbDateByMoveForward(startDate string, moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
  1825. // 根据日期进行排序
  1826. index := 0
  1827. length := len(edbDataList)
  1828. for i := length - 1; i >= 0; i-- {
  1829. item := edbDataList[i]
  1830. if item.DataTime == startDate {
  1831. index += 1
  1832. continue
  1833. }
  1834. if index >= moveForward {
  1835. date = item.DataTime
  1836. break
  1837. }
  1838. if index > 0 {
  1839. index += 1
  1840. date = item.DataTime
  1841. }
  1842. }
  1843. return
  1844. }
  1845. // HandleEdbDateChange 处理日期变换
  1846. func HandleEdbDateChange(date string, dateChange []*data_manage.EdbDataDateChangeConf) (newDate string, err error) {
  1847. newDate = date
  1848. if newDate != "" {
  1849. if len(dateChange) > 0 {
  1850. var dateTime time.Time
  1851. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  1852. if err != nil {
  1853. err = fmt.Errorf("日期解析失败: %s", err.Error())
  1854. return
  1855. }
  1856. for _, v := range dateChange {
  1857. if v.ChangeType == 1 {
  1858. dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
  1859. newDate = dateTime.Format(utils.FormatDate)
  1860. } else if v.ChangeType == 2 {
  1861. newDate, err, _ = utils.HandleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
  1862. if err != nil {
  1863. return
  1864. }
  1865. dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
  1866. if err != nil {
  1867. err = fmt.Errorf("日期解析失败: %s", err.Error())
  1868. return
  1869. }
  1870. }
  1871. }
  1872. }
  1873. }
  1874. return
  1875. }
  1876. // 添加指标系列和数据
  1877. func AddSeries(edbInfoIds []int, chartInfoId, chartInfoSource int, extraConf data_manage.ChartRangeAnalysisExtraConf, calculatesJson string) (err error) {
  1878. edbArr, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  1879. if e != nil {
  1880. err = fmt.Errorf("获取指标列表失败, Err: " + e.Error())
  1881. return
  1882. }
  1883. if len(edbArr) == 0 {
  1884. err = fmt.Errorf("获取指标列表失败, 指标不存在")
  1885. return
  1886. }
  1887. edbInfoType := edbArr[0].EdbInfoType
  1888. // 新增指标系列
  1889. seriesItem := new(data_manage.FactorEdbSeries)
  1890. seriesItem.SeriesName = extraConf.SeriesName
  1891. seriesItem.EdbInfoType = edbInfoType
  1892. seriesItem.CreateTime = time.Now().Local()
  1893. seriesItem.ModifyTime = time.Now().Local()
  1894. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
  1895. seriesItem.CalculateStep = calculatesJson
  1896. mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  1897. for _, v := range edbArr {
  1898. mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
  1899. EdbInfoId: v.EdbInfoId,
  1900. EdbCode: v.EdbCode,
  1901. CreateTime: time.Now().Local(),
  1902. ModifyTime: time.Now().Local(),
  1903. })
  1904. }
  1905. seriesId, e := seriesItem.CreateSeriesAndMapping(seriesItem, mappings)
  1906. if e != nil {
  1907. err = fmt.Errorf("新增因子指标系列失败, Err: " + e.Error())
  1908. return
  1909. }
  1910. // 图表关联-此处添加的chart_info_id=0
  1911. seriesChartMapping := new(data_manage.FactorEdbSeriesChartMapping)
  1912. seriesChartMapping.CalculateType = data_manage.FactorEdbSeriesChartCalculateTypeRange
  1913. //新增图表和指标的映射关系
  1914. seriesChartMapping.CalculateData = ""
  1915. seriesChartMapping.FactorEdbSeriesId = seriesId
  1916. seriesChartMapping.ChartInfoId = chartInfoId
  1917. seriesChartMapping.Source = chartInfoSource
  1918. seriesChartMapping.CreateTime = time.Now().Local()
  1919. seriesChartMapping.ModifyTime = time.Now().Local()
  1920. if e = seriesChartMapping.Create(); e != nil {
  1921. err = fmt.Errorf("新增图表关联失败, Err: " + e.Error())
  1922. return
  1923. }
  1924. // todo 计算指标数据并存储
  1925. _, e = FactorEdbStepCalculateRange(seriesId, edbInfoIds, extraConf, false)
  1926. if e != nil {
  1927. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  1928. return
  1929. }
  1930. // 更新系列计算状态
  1931. cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
  1932. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
  1933. seriesItem.ModifyTime = time.Now().Local()
  1934. if e = seriesItem.Update(cols); e != nil {
  1935. err = fmt.Errorf("更新因子指标系列计算状态失败, Err: " + e.Error())
  1936. return
  1937. }
  1938. return
  1939. }
  1940. func EditSeries(seriesMapping *data_manage.FactorEdbSeriesChartMapping, edbInfoIds []int, extraConf data_manage.ChartRangeAnalysisExtraConf, calculatesJson string, recalculate bool) (err error) {
  1941. seriesOb := new(data_manage.FactorEdbSeries)
  1942. seriesItem, e := seriesOb.GetItemById(seriesMapping.FactorEdbSeriesId)
  1943. if e != nil {
  1944. if e.Error() == utils.ErrNoRow() {
  1945. err = fmt.Errorf("因子指标系列不存在, Err: " + e.Error())
  1946. return
  1947. }
  1948. err = fmt.Errorf("获取因子指标系列失败, Err: " + e.Error())
  1949. return
  1950. }
  1951. edbArr, e := data_manage.GetEdbInfoByIdList(edbInfoIds)
  1952. if e != nil {
  1953. err = fmt.Errorf("获取指标列表失败, Err: " + e.Error())
  1954. return
  1955. }
  1956. if len(edbArr) == 0 {
  1957. err = fmt.Errorf("指标列表为空")
  1958. return
  1959. }
  1960. var calculateResp data_manage.FactorEdbSeriesStepCalculateResp
  1961. calculateResp.SeriesId = seriesItem.FactorEdbSeriesId
  1962. // 如果不需要进行重新计算(比如只改了系列名称)那么只更新指标系列
  1963. seriesItem.SeriesName = extraConf.SeriesName
  1964. seriesItem.EdbInfoType = edbArr[0].EdbInfoType
  1965. seriesItem.ModifyTime = time.Now().Local()
  1966. updateCols := []string{seriesOb.Cols().SeriesName, seriesOb.Cols().EdbInfoType, seriesOb.Cols().ModifyTime}
  1967. if !recalculate {
  1968. if e = seriesItem.Update(updateCols); e != nil {
  1969. err = fmt.Errorf("更新因子指标系列失败, Err: " + e.Error())
  1970. return
  1971. }
  1972. return
  1973. }
  1974. // 更新系列信息和指标关联
  1975. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculating
  1976. seriesItem.CalculateStep = calculatesJson
  1977. updateCols = append(updateCols, seriesOb.Cols().CalculateState, seriesOb.Cols().CalculateStep)
  1978. mappings := make([]*data_manage.FactorEdbSeriesMapping, 0)
  1979. for _, v := range edbArr {
  1980. mappings = append(mappings, &data_manage.FactorEdbSeriesMapping{
  1981. EdbInfoId: v.EdbInfoId,
  1982. EdbCode: v.EdbCode,
  1983. CreateTime: time.Now().Local(),
  1984. ModifyTime: time.Now().Local(),
  1985. })
  1986. }
  1987. if e = seriesItem.EditSeriesAndMapping(seriesItem, mappings, updateCols); e != nil {
  1988. err = fmt.Errorf("更新因子指标系列信息失败, Err: %s", e.Error())
  1989. return
  1990. }
  1991. // todo 重新计算
  1992. _, e = FactorEdbStepCalculateRange(seriesItem.FactorEdbSeriesId, edbInfoIds, extraConf, false)
  1993. if e != nil {
  1994. err = fmt.Errorf("计算因子指标失败, Err: " + e.Error())
  1995. return
  1996. }
  1997. // 更新系列计算状态
  1998. cols := []string{seriesItem.Cols().CalculateState, seriesItem.Cols().ModifyTime}
  1999. seriesItem.CalculateState = data_manage.FactorEdbSeriesCalculated
  2000. seriesItem.ModifyTime = time.Now().Local()
  2001. if e = seriesItem.Update(cols); e != nil {
  2002. err = fmt.Errorf("更新因子指标系列计算状态失败, Err: %s", e.Error())
  2003. return
  2004. }
  2005. return
  2006. }
  2007. // FactorEdbStepCalculateRange 因子指标-区间计算
  2008. func FactorEdbStepCalculateRange(seriesId int, edbArr []int, extraConf data_manage.ChartRangeAnalysisExtraConf, recalculate bool) (calculateResp data_manage.FactorEdbSeriesStepCalculateResp, err error) {
  2009. // todo 如果指标已保存,则用指标数据还是图表指标数据?
  2010. // 获取图表x轴y轴
  2011. defer func() {
  2012. if err != nil {
  2013. tips := fmt.Sprintf("StepCalculate计算失败, ErrMsg: %v", err)
  2014. fmt.Println(tips)
  2015. utils.FileLog.Info(tips)
  2016. go alarm_msg.SendAlarmMsg(tips, 3)
  2017. }
  2018. /*if len(calculateResp.Fail) > 0 {
  2019. tips := "StepCalculate计算失败, ErrMsg: "
  2020. for _, f := range calculateResp.Fail {
  2021. tips += fmt.Sprintf("code: %s, err: %s\n", f.EdbCode, f.ErrMsg)
  2022. }
  2023. fmt.Println(tips)
  2024. utils.FileLog.Info(tips)
  2025. go alarm_msg.SendAlarmMsg(tips, 2)
  2026. }*/
  2027. }()
  2028. edbInfoMappingList, e := data_manage.GetChartEdbMappingListByEdbInfoIdList(edbArr)
  2029. if e != nil {
  2030. err = fmt.Errorf("获取区间计算图表, A指标mapping信息失败, Err:%v", e)
  2031. return
  2032. }
  2033. _, _, _, err = GetChartDataByEdbInfoList(0, 0, 0, "", "", edbInfoMappingList, &extraConf)
  2034. if err != nil {
  2035. err = fmt.Errorf("获取图表数据失败, ErrMsg: %v", err)
  2036. return
  2037. }
  2038. // 重新计算-先清除原数据
  2039. calculateDataOb := new(data_manage.FactorEdbSeriesCalculateDataQjjs)
  2040. cond := fmt.Sprintf("%s = ?", calculateDataOb.Cols().FactorEdbSeriesId)
  2041. pars := make([]interface{}, 0)
  2042. pars = append(pars, seriesId)
  2043. if e := calculateDataOb.RemoveByCondition(cond, pars); e != nil {
  2044. err = fmt.Errorf("清除原数据失败, err: %v", e)
  2045. return
  2046. }
  2047. // 计算成功的保存结果
  2048. dataArr := make([]*data_manage.FactorEdbSeriesCalculateDataQjjs, 0)
  2049. for _, v := range edbInfoMappingList {
  2050. dataList := v.DataList.([]*data_manage.EdbDataList)
  2051. for _, dataItem := range dataList {
  2052. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataItem.DataTime, time.Local)
  2053. dataArr = append(dataArr, &data_manage.FactorEdbSeriesCalculateDataQjjs{
  2054. FactorEdbSeriesId: seriesId,
  2055. EdbInfoId: v.EdbInfoId,
  2056. EdbCode: v.EdbCode,
  2057. DataTime: dataTime,
  2058. Value: dataItem.Value,
  2059. CreateTime: time.Now().Local(),
  2060. ModifyTime: time.Now().Local(),
  2061. DataTimestamp: dataItem.DataTimestamp,
  2062. })
  2063. }
  2064. }
  2065. if len(dataArr) == 0 {
  2066. err = fmt.Errorf("计算结果无数据, seriesId: %d", seriesId)
  2067. return
  2068. }
  2069. if e = calculateDataOb.CreateMulti(dataArr); e != nil {
  2070. err = fmt.Errorf("保存计算结果失败, seriesId: %d, err: %v, ", seriesId, e)
  2071. return
  2072. }
  2073. return
  2074. }
  2075. func CheckChartRangeExtraConfig(extraConfig data_manage.ChartRangeAnalysisExtraConf) (err error, errMsg string, isSendEmail bool) {
  2076. extraConfig.SeriesName = strings.TrimSpace(extraConfig.SeriesName)
  2077. if extraConfig.SeriesName == "" && extraConfig.EdbInfoMode == 1 {
  2078. errMsg = "请输入指标系列名称"
  2079. err = errors.New(errMsg)
  2080. isSendEmail = false
  2081. return
  2082. }
  2083. if extraConfig.CalculateType > 5 || extraConfig.CalculateType < 0 {
  2084. errMsg = "计算方式参数错误"
  2085. err = errors.New(errMsg)
  2086. isSendEmail = false
  2087. return
  2088. }
  2089. switch extraConfig.DateRangeType {
  2090. case 0:
  2091. case 1:
  2092. if len(extraConfig.ManualDateConf) == 0 {
  2093. errMsg = "请选择时间区间"
  2094. err = errors.New(errMsg)
  2095. return
  2096. }
  2097. // 先按开始时间排序
  2098. sort.Sort(data_manage.ChartRangeAnalysisManualDateConfList(extraConfig.ManualDateConf))
  2099. // 校验日期
  2100. // 1.如果截止时间小于指标的截止日期,需要重置为指标的截止日期
  2101. // 2.时间区间不能重叠
  2102. for i := 1; i < len(extraConfig.ManualDateConf); i++ {
  2103. start1, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i-1].EndDate)
  2104. if e != nil {
  2105. err = e
  2106. errMsg = "截止日期格式有误"
  2107. return
  2108. }
  2109. start2, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].EndDate)
  2110. if e != nil {
  2111. err = e
  2112. errMsg = "截止日期格式有误"
  2113. return
  2114. }
  2115. start3, e := time.Parse(utils.FormatDate, extraConfig.ManualDateConf[i].StartDate)
  2116. if e != nil {
  2117. err = e
  2118. errMsg = "截止日期格式有误"
  2119. return
  2120. }
  2121. // 如果当前区间的开始时间小于等于前一个区间的结束时间,则存在重叠
  2122. if !start2.After(start1) || start3.Before(start1) {
  2123. errMsg = "日期区间存在重叠"
  2124. return
  2125. }
  2126. }
  2127. //如果截止时间大于指标的截止日期,需要重置为指标的截止日期
  2128. case 2:
  2129. if extraConfig.YearDateConf.StartDay == "" || extraConfig.YearDateConf.EndDay == "" {
  2130. errMsg = "请选择时间区间"
  2131. return
  2132. }
  2133. if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.StartDay); e != nil {
  2134. errMsg = "开始日期格式有误"
  2135. return
  2136. }
  2137. if _, e := time.Parse(utils.FormatMonthDay, extraConfig.YearDateConf.EndDay); e != nil {
  2138. errMsg = "结束日期格式有误"
  2139. return
  2140. }
  2141. }
  2142. return
  2143. }