chart_info.go 68 KB

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