chart_info.go 56 KB

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