chart_info.go 71 KB

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