chart_info.go 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/company"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "math"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. func ChartInfoRefreshV1(chartInfoId int) (err error) {
  19. var errmsg string
  20. defer func() {
  21. if err != nil {
  22. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  23. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  24. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  25. }
  26. }()
  27. baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshData(chartInfoId)
  28. if err != nil {
  29. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  30. return
  31. }
  32. newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  33. baseMap := make(map[int]*data_manage.EdbInfo)
  34. for _, bv := range baseEdbInfoArr {
  35. // 如果不是普通指标,那么过滤
  36. if bv.EdbInfoType != 0 {
  37. continue
  38. }
  39. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  40. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  41. }
  42. baseMap[bv.EdbInfoId] = bv
  43. }
  44. fmt.Println("calculateInfoArr:", len(calculateInfoArr))
  45. newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
  46. calculateMap := make(map[int]*data_manage.EdbInfo)
  47. var calculateArr []int
  48. for _, bv := range calculateInfoArr {
  49. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  50. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  51. calculateArr = append(calculateArr, bv.EdbInfoId)
  52. }
  53. calculateMap[bv.EdbInfoId] = bv
  54. }
  55. sort.Ints(calculateArr)
  56. // 刷新相关基础指标
  57. for _, bv := range newBaseEdbInfoArr {
  58. err, errmsg = refreshBaseEdbInfo(bv)
  59. if err != nil {
  60. return
  61. }
  62. }
  63. // 刷新相关计算指标
  64. for _, v := range calculateArr {
  65. edbInfo := calculateMap[v]
  66. if edbInfo == nil {
  67. return err
  68. }
  69. err, errmsg = refreshCalculateEdbInfo(edbInfo)
  70. if err != nil {
  71. return
  72. }
  73. }
  74. return err
  75. }
  76. // BatchChartInfoRefresh 图表批量刷新
  77. func BatchChartInfoRefresh(chartInfoList []*data_manage.ChartInfo) (err error) {
  78. if len(chartInfoList) <= 0 {
  79. return
  80. }
  81. chartInfoIdSlice := make([]string, 0)
  82. for _, chartInfo := range chartInfoList {
  83. chartInfoIdSlice = append(chartInfoIdSlice, strconv.Itoa(chartInfo.ChartInfoId))
  84. }
  85. var errmsg string
  86. defer func() {
  87. if err != nil {
  88. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  89. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  90. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  91. }
  92. }()
  93. baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshDataByChartInfoIdSlice(chartInfoIdSlice)
  94. if err != nil {
  95. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  96. return
  97. }
  98. //基础指标去重
  99. newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  100. baseMap := make(map[int]*data_manage.EdbInfo)
  101. for _, bv := range baseEdbInfoArr {
  102. fmt.Println(bv.UniqueCode)
  103. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  104. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  105. }
  106. baseMap[bv.EdbInfoId] = bv
  107. }
  108. fmt.Println("calculateInfoArr:", len(calculateInfoArr))
  109. //计算指标去重
  110. newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
  111. calculateMap := make(map[int]*data_manage.EdbInfo)
  112. var calculateArr []int
  113. for _, bv := range calculateInfoArr {
  114. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  115. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  116. calculateArr = append(calculateArr, bv.EdbInfoId)
  117. }
  118. calculateMap[bv.EdbInfoId] = bv
  119. }
  120. sort.Ints(calculateArr)
  121. // 刷新相关基础指标
  122. for _, bv := range newBaseEdbInfoArr {
  123. err, errmsg = refreshBaseEdbInfo(bv)
  124. if err != nil {
  125. return
  126. }
  127. }
  128. // 刷新相关计算指标
  129. for _, v := range calculateArr {
  130. edbInfo := calculateMap[v]
  131. if edbInfo == nil {
  132. return err
  133. }
  134. err, errmsg = refreshCalculateEdbInfo(edbInfo)
  135. if err != nil {
  136. return
  137. }
  138. }
  139. return err
  140. }
  141. // refreshBaseEdbInfo 刷新基础指标
  142. func refreshBaseEdbInfo(bv *data_manage.EdbInfo) (err error, errMsg string) {
  143. source := bv.Source
  144. edbInfoId := bv.EdbInfoId
  145. edbCode := bv.EdbCode
  146. //startDate := bv.StartDate
  147. frequency := bv.Frequency
  148. if bv.StartDate == "0000-00-00" {
  149. return
  150. }
  151. sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
  152. if err != nil {
  153. return
  154. }
  155. var limitDay int
  156. startDate := ""
  157. switch frequency {
  158. case "日度":
  159. limitDay = utils.DATA_START_REFRESH_LIMIT
  160. case "周度":
  161. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  162. case "月度":
  163. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  164. case "季度":
  165. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  166. case "年度":
  167. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  168. default:
  169. limitDay = utils.DATA_START_REFRESH_LIMIT
  170. }
  171. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  172. fmt.Println("source:", source)
  173. respItem, err := RefreshEdbData(edbInfoId, source, edbCode, startDate)
  174. if err != nil {
  175. errMsg = errors.New("RefreshEdbData Err:" + err.Error()).Error()
  176. return
  177. }
  178. if respItem.Ret != 200 {
  179. errMsg = errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  180. return
  181. }
  182. //if source == utils.DATA_SOURCE_THS {
  183. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  184. // //err = RefreshEdbDataByThs(edbInfoId, edbCode, startDate, endDate)
  185. // //if err != nil {
  186. // // errmsg = "RefreshEdbDataByThs Err:" + err.Error()
  187. // //}
  188. // respItem, err :=RefreshThsEdbData(edbInfoId,edbCode,startDate)
  189. // if err != nil {
  190. // errmsg=errors.New("RefreshThsEdbData Err:"+err.Error()).Error()
  191. // return err
  192. // }
  193. // if respItem.Ret != 200 {
  194. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  195. // return err
  196. // }
  197. //} else if source == utils.DATA_SOURCE_WIND {
  198. // //endDate := time.Now().Format(utils.FormatDate)
  199. // //err = RefreshEdbDataByWind(edbInfoId, edbCode, startDate, endDate)
  200. // //if err != nil {
  201. // // errmsg = "RefreshEdbDataByWind Err:" + err.Error()
  202. // //}
  203. // respItem, err :=RefreshWindEdbData(edbInfoId,edbCode,startDate)
  204. // if err != nil {
  205. // errmsg=errors.New("RefreshWindEdbData Err:"+err.Error()).Error()
  206. // return err
  207. // }
  208. // if respItem.Ret != 200 {
  209. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  210. // return err
  211. // }
  212. //} else if source == utils.DATA_SOURCE_PB {
  213. // //startDate := sTime.Format(utils.FormatDateUnSpace)
  214. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDateUnSpace)
  215. // //err = RefreshEdbDataByPb(edbInfoId, edbCode, startDate, endDate)
  216. // //if err != nil {
  217. // // errmsg = "RefreshEdbDataByPb Err:" + err.Error()
  218. // //}
  219. // startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDateUnSpace)
  220. // respItem, err :=RefreshPbEdbData(edbInfoId,edbCode,startDate)
  221. // if err != nil {
  222. // errmsg=errors.New("RefreshPbEdbData Err:"+err.Error()).Error()
  223. // return err
  224. // }
  225. // if respItem.Ret != 200 {
  226. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  227. // return err
  228. // }
  229. //} else if source == utils.DATA_SOURCE_MANUAL {
  230. // //fmt.Println("RefreshAllEdbDataByManual:" + edbCode)
  231. // //err = data_manage.RefreshAllEdbDataByManual(edbInfoId, bv.Source, edbCode)
  232. // //if err != nil {
  233. // // errmsg = "RefreshEdbDataByManual Err:" + err.Error()
  234. // //}
  235. // respItem, err :=RefreshManualEdbData(edbInfoId,edbCode,startDate)
  236. // if err != nil {
  237. // errmsg=errors.New("RefreshManualEdbData Err:"+err.Error()).Error()
  238. // return err
  239. // }
  240. // if respItem.Ret != 200 {
  241. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  242. // return err
  243. // }
  244. //} else if source == utils.DATA_SOURCE_LZ {
  245. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  246. // //err = data_manage.RefreshEdbDataByLz(edbInfoId, edbCode, startDate, endDate)
  247. // //if err != nil {
  248. // // errmsg = "RefreshEdbDataByLz Err:" + err.Error()
  249. // //}
  250. // respItem, err :=RefreshLzEdbData(edbInfoId,edbCode,startDate)
  251. // if err != nil {
  252. // errmsg=errors.New("RefreshLzEdbData Err:"+err.Error()).Error()
  253. // return err
  254. // }
  255. // if respItem.Ret != 200 {
  256. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  257. // return err
  258. // }
  259. //} else if source == utils.DATA_SOURCE_YS {
  260. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  261. // //err = data_manage.RefreshAllEdbDataByYs(edbInfoId, source, edbCode, startDate, endDate)
  262. // //if err != nil {
  263. // // errmsg = "RefreshEdbDataByYs Err:" + err.Error()
  264. // //}
  265. // respItem, err :=RefreshSmmEdbData(edbInfoId,edbCode,startDate)
  266. // if err != nil {
  267. // errmsg=errors.New("RefreshSmmEdbData Err:"+err.Error()).Error()
  268. // return err
  269. // }
  270. // if respItem.Ret != 200 {
  271. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  272. // return err
  273. // }
  274. //} else if source == utils.DATA_SOURCE_GL {
  275. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  276. // //err = data_manage.RefreshEdbDataByGl(edbInfoId, edbCode, startDate, endDate)
  277. // //if err != nil {
  278. // // errmsg = "RefreshEdbDataByGl Err:" + err.Error()
  279. // //}
  280. // respItem, err :=RefreshMysteelEdbData(edbInfoId,edbCode,startDate)
  281. // if err != nil {
  282. // errmsg=errors.New("RefreshMysteelEdbData Err:"+err.Error()).Error()
  283. // return err
  284. // }
  285. // if respItem.Ret != 200 {
  286. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  287. // return err
  288. // }
  289. //} else if source == utils.DATA_SOURCE_LT {
  290. // //endDate := time.Now().Format(utils.FormatDate)
  291. // //err = data_manage.RefreshAllEdbDataByLt(edbInfoId, source, edbCode, startDate, endDate)
  292. // //if err != nil {
  293. // // errmsg = "RefreshAllEdbDataByLT Err:" + err.Error()
  294. // //}
  295. // respItem, err :=RefreshLtEdbData(edbInfoId,edbCode,startDate)
  296. // if err != nil {
  297. // errmsg=errors.New("RefreshLtEdbData Err:"+err.Error()).Error()
  298. // return err
  299. // }
  300. // if respItem.Ret != 200 {
  301. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  302. // return err
  303. // }
  304. //} else if source == utils.DATA_SOURCE_ZZ {
  305. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  306. // //err = data_manage.RefreshEdbDataByZz(edbInfoId, edbCode, startDate, endDate)
  307. // //if err != nil {
  308. // // errmsg = "RefreshAllEdbDataByZz Err:" + err.Error()
  309. // //}
  310. // respItem, err :=RefreshZzEdbData(edbInfoId,edbCode,startDate)
  311. // if err != nil {
  312. // errmsg=errors.New("RefreshZzEdbData Err:"+err.Error()).Error()
  313. // return err
  314. // }
  315. // if respItem.Ret != 200 {
  316. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  317. // return err
  318. // }
  319. //} else if source == utils.DATA_SOURCE_DL {
  320. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  321. // //err = data_manage.RefreshEdbDataByDl(edbInfoId, edbCode, startDate, endDate)
  322. // //if err != nil {
  323. // // errmsg = "RefreshAllEdbDataByDl Err:" + err.Error()
  324. // //}
  325. // respItem, err :=RefreshDlEdbData(edbInfoId,edbCode,startDate)
  326. // if err != nil {
  327. // errmsg=errors.New("RefreshDlEdbData Err:"+err.Error()).Error()
  328. // return err
  329. // }
  330. // if respItem.Ret != 200 {
  331. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  332. // return err
  333. // }
  334. //} else if source == utils.DATA_SOURCE_SH {
  335. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  336. // //err = data_manage.RefreshEdbDataBySh(edbInfoId, edbCode, startDate, endDate)
  337. // //if err != nil {
  338. // // errmsg = "RefreshAllEdbDataBySh Err:" + err.Error()
  339. // //}
  340. // respItem, err :=RefreshShEdbData(edbInfoId,edbCode,startDate)
  341. // if err != nil {
  342. // errmsg=errors.New("RefreshShEdbData Err:"+err.Error()).Error()
  343. // return err
  344. // }
  345. // if respItem.Ret != 200 {
  346. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  347. // return err
  348. // }
  349. //} else if source == utils.DATA_SOURCE_CFFEX {
  350. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  351. // //err = data_manage.RefreshEdbDataByCffex(edbInfoId, edbCode, startDate, endDate)
  352. // //if err != nil {
  353. // // errmsg = "RefreshAllEdbDataByCffex Err:" + err.Error()
  354. // //}
  355. // respItem, err :=RefreshCffexEdbData(edbInfoId,edbCode,startDate)
  356. // if err != nil {
  357. // errmsg=errors.New("RefreshCffexEdbData Err:"+err.Error()).Error()
  358. // return err
  359. // }
  360. // if respItem.Ret != 200 {
  361. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  362. // return err
  363. // }
  364. //} else if source == utils.DATA_SOURCE_SHFE {
  365. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  366. // //err = data_manage.RefreshEdbDataByShfe(edbInfoId, edbCode, startDate, endDate)
  367. // //if err != nil {
  368. // // errmsg = "RefreshAllEdbDataByShfe Err:" + err.Error()
  369. // //}
  370. // respItem, err :=RefreshShfeEdbData(edbInfoId,edbCode,startDate)
  371. // if err != nil {
  372. // errmsg=errors.New("RefreshShfeEdbData Err:"+err.Error()).Error()
  373. // return err
  374. // }
  375. // if respItem.Ret != 200 {
  376. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  377. // return err
  378. // }
  379. //} else if source == utils.DATA_SOURCE_GIE {
  380. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  381. // //err = data_manage.RefreshEdbDataByGie(edbInfoId, edbCode, startDate, endDate)
  382. // //if err != nil {
  383. // // errmsg = "RefreshEdbDataByGie Err:" + err.Error()
  384. // //}
  385. // respItem, err :=RefreshGieEdbData(edbInfoId,edbCode,startDate)
  386. // if err != nil {
  387. // errmsg=errors.New("RefreshGieEdbData Err:"+err.Error()).Error()
  388. // return err
  389. // }
  390. // if respItem.Ret != 200 {
  391. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  392. // return err
  393. // }
  394. //}
  395. maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
  396. if err != nil {
  397. return
  398. }
  399. if maxAndMinItem != nil {
  400. err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  401. if err != nil {
  402. return
  403. }
  404. }
  405. return
  406. }
  407. // refreshCalculateEdbInfo 刷新计算指标
  408. func refreshCalculateEdbInfo(edbInfo *data_manage.EdbInfo) (err error, errMsg string) {
  409. startDate := edbInfo.StartDate
  410. source := edbInfo.Source
  411. if source == utils.DATA_SOURCE_CALCULATE {
  412. startDate = edbInfo.StartDate
  413. sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
  414. if tmpErr != nil {
  415. err = tmpErr
  416. return
  417. }
  418. startDate = sTime.Format(utils.FormatDate)
  419. }
  420. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  421. if err != nil {
  422. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
  423. errMsg = "RefreshEdbCalculateData Err:" + err.Error()
  424. return
  425. }
  426. if result.Ret != 200 {
  427. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  428. errMsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  429. err = errors.New("刷新失败")
  430. return
  431. }
  432. return
  433. }
  434. type ChartInfoReq struct {
  435. ChartInfoId int `description:"图表id,新增时传0"`
  436. }
  437. // DeleteChartInfoDataRedis 清除图表缓存
  438. func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
  439. var req ChartInfoReq
  440. err = json.Unmarshal(bodyByte, &req)
  441. if err != nil {
  442. return
  443. }
  444. if req.ChartInfoId > 0 {
  445. err = utils.Rc.Delete(GetChartInfoDataKey(req.ChartInfoId))
  446. }
  447. return
  448. }
  449. // DeleteChartClassifyRedis 清除图表分类缓存
  450. func DeleteChartClassifyRedis(bodyByte []byte) (err error) {
  451. err = utils.Rc.Delete(utils.CACHE_CHART_CLASSIFY)
  452. return
  453. }
  454. // GetChartInfoDataKey 获取图表缓存的key
  455. func GetChartInfoDataKey(chartInfoId int) string {
  456. key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
  457. return key
  458. }
  459. // CheckOpChartPermission 判断图表操作权限
  460. func CheckOpChartPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  461. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  462. ok = true
  463. }
  464. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  465. if ok == false && createUserId == sysUser.AdminId {
  466. ok = true
  467. }
  468. // 如果图表权限id 是 1 ,那么允许编辑
  469. if ok == false && sysUser.ChartPermission == 1 {
  470. ok = true
  471. }
  472. return
  473. }
  474. // GetChartEdbData 获取图表的指标数据
  475. func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
  476. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  477. xEdbIdValue = make([]int, 0)
  478. yDataList = make([]data_manage.YData, 0)
  479. var extraConfig interface{}
  480. switch chartType {
  481. case 7: // 柱形图
  482. var barConfig data_manage.BarChartInfoReq
  483. if extraConfigStr == `` {
  484. errMsg = "柱方图未配置"
  485. err = errors.New(errMsg)
  486. return
  487. }
  488. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  489. if err != nil {
  490. errMsg = "柱方图配置异常"
  491. err = errors.New(errMsg)
  492. return
  493. }
  494. extraConfig = barConfig
  495. case 10: // 截面散点图
  496. var tmpExtraConfig data_manage.SectionScatterReq
  497. if extraConfigStr == `` {
  498. errMsg = "截面散点图未配置"
  499. err = errors.New(errMsg)
  500. return
  501. }
  502. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  503. if err != nil {
  504. errMsg = "截面散点配置异常"
  505. err = errors.New(errMsg)
  506. return
  507. }
  508. extraConfig = tmpExtraConfig
  509. default:
  510. xEdbIdValue = make([]int, 0)
  511. yDataList = make([]data_manage.YData, 0)
  512. }
  513. // 指标对应的所有数据
  514. edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList)
  515. if err != nil {
  516. return
  517. }
  518. // 特殊图形数据处理
  519. switch chartType {
  520. case 7: // 柱形图
  521. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  522. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  523. for k := range yDataList {
  524. yDataList[k].Unit = barChartConf.Unit
  525. yDataList[k].UnitEn = barChartConf.UnitEn
  526. }
  527. for _, v := range edbList {
  528. // 指标别名
  529. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  530. for _, reqEdb := range barChartConf.EdbInfoIdList {
  531. if v.EdbInfoId == reqEdb.EdbInfoId {
  532. v.EdbAliasName = reqEdb.Name
  533. }
  534. }
  535. }
  536. }
  537. case 10: // 截面散点图
  538. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  539. xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  540. // 这个数据没有必要返回给前端
  541. for _, v := range edbList {
  542. v.DataList = nil
  543. }
  544. }
  545. return
  546. }
  547. // GetEdbDataMapList 获取指标最后的基础数据
  548. func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  549. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList)
  550. return
  551. }
  552. // getEdbDataMapList 获取指标最后的基础数据
  553. func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  554. // 指标对应的所有数据
  555. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  556. for _, v := range mappingList {
  557. //fmt.Println("v:", v.EdbInfoId)
  558. item := new(data_manage.ChartEdbInfoMapping)
  559. item.EdbInfoId = v.EdbInfoId
  560. item.SourceName = v.SourceName
  561. item.Source = v.Source
  562. item.EdbCode = v.EdbCode
  563. item.EdbName = v.EdbName
  564. item.EdbNameEn = v.EdbNameEn
  565. item.Frequency = v.Frequency
  566. item.EdbType = v.EdbType
  567. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  568. if v.Unit != `无` {
  569. item.Unit = v.Unit
  570. }
  571. item.UnitEn = v.UnitEn
  572. item.StartDate = v.StartDate
  573. item.EndDate = v.EndDate
  574. item.ModifyTime = v.ModifyTime
  575. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  576. item.PredictChartColor = v.PredictChartColor
  577. item.ClassifyId = v.ClassifyId
  578. if chartInfoId <= 0 {
  579. item.IsAxis = 1
  580. item.LeadValue = 0
  581. item.LeadUnit = ""
  582. item.ChartEdbMappingId = 0
  583. item.ChartInfoId = 0
  584. item.IsOrder = false
  585. item.EdbInfoType = 1
  586. item.ChartStyle = ""
  587. item.ChartColor = ""
  588. item.ChartWidth = 0
  589. item.MaxData = v.MaxValue
  590. item.MinData = v.MinValue
  591. } else {
  592. item.IsAxis = v.IsAxis
  593. item.EdbInfoType = v.EdbInfoType
  594. item.LeadValue = v.LeadValue
  595. item.LeadUnit = v.LeadUnit
  596. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  597. item.ChartEdbMappingId = v.ChartEdbMappingId
  598. item.ChartInfoId = v.ChartInfoId
  599. item.ChartStyle = v.ChartStyle
  600. item.ChartColor = v.ChartColor
  601. item.ChartWidth = v.ChartWidth
  602. item.IsOrder = v.IsOrder
  603. item.MaxData = v.MaxData
  604. item.MinData = v.MinData
  605. }
  606. item.LatestValue = v.LatestValue
  607. item.LatestDate = v.LatestDate
  608. item.UniqueCode = v.UniqueCode
  609. item.MoveLatestDate = v.LatestDate
  610. var startDateReal string
  611. var diffSeconds int64
  612. if chartType == 2 { //季节性图
  613. startDateReal = startDate
  614. } else {
  615. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  616. var startTimeRealTemp time.Time
  617. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  618. switch v.LeadUnit {
  619. case "天":
  620. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  621. case "月":
  622. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  623. case "季":
  624. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  625. case "周":
  626. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  627. case "年":
  628. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  629. }
  630. if startTimeRealTemp.Before(startDateParse) {
  631. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  632. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  633. } else {
  634. startDateReal = startDate
  635. diffSeconds = 0
  636. }
  637. // 预测指标的开始日期也要偏移
  638. {
  639. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  640. if tmpErr != nil {
  641. err = tmpErr
  642. return
  643. }
  644. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  645. if tmpErr != nil {
  646. err = tmpErr
  647. return
  648. }
  649. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  650. }
  651. } else {
  652. startDateReal = startDate
  653. }
  654. }
  655. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  656. calendarPreYear := 0
  657. if calendar == "农历" {
  658. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  659. if err != nil {
  660. fmt.Println("time.Parse:" + err.Error())
  661. }
  662. calendarPreYear = newStartDateReal.Year() - 1
  663. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  664. startDateReal = newStartDateReal.Format(utils.FormatDate)
  665. }
  666. dataList := make([]*data_manage.EdbDataList, 0)
  667. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  668. //var newEdbInfo *data_manage.EdbInfo
  669. switch v.EdbInfoCategoryType {
  670. case 0:
  671. dataList, err = data_manage.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate)
  672. case 1:
  673. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
  674. default:
  675. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  676. }
  677. if err != nil {
  678. return
  679. }
  680. edbDataListMap[v.EdbInfoId] = dataList
  681. if diffSeconds != 0 && v.EdbInfoType == 0 {
  682. dataListLen := len(dataList)
  683. for i := 0; i < dataListLen; i++ {
  684. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  685. }
  686. }
  687. if chartType == 2 {
  688. latestDateStr := v.LatestDate //实际数据的截止日期
  689. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  690. if tmpErr != nil {
  691. //item.DataList = dataList
  692. item.IsNullData = true
  693. edbList = append(edbList, item)
  694. continue
  695. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  696. return
  697. }
  698. latestDateYear := latestDate.Year() //实际数据截止年份
  699. if calendar == "农历" {
  700. if len(dataList) <= 0 {
  701. result := new(data_manage.EdbDataResult)
  702. item.DataList = result
  703. } else {
  704. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  705. if tmpErr != nil {
  706. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  707. return
  708. }
  709. // 处理季节图的截止日期
  710. for k, edbDataItems := range result.List {
  711. var cuttingDataTimestamp int64
  712. // 切割的日期时间字符串
  713. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  714. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  715. if edbDataItems.Year >= latestDateYear {
  716. for _, tmpData := range edbDataItems.Items {
  717. if tmpData.DataTime == cuttingDataTimeStr {
  718. cuttingDataTimestamp = tmpData.DataTimestamp
  719. break
  720. }
  721. }
  722. }
  723. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  724. result.List[k] = edbDataItems
  725. }
  726. if result.List[0].Year != calendarPreYear {
  727. itemList := make([]*data_manage.EdbDataList, 0)
  728. items := new(data_manage.EdbDataItems)
  729. //items.Year = calendarPreYear
  730. items.Items = itemList
  731. newResult := new(data_manage.EdbDataResult)
  732. newResult.List = append(newResult.List, items)
  733. newResult.List = append(newResult.List, result.List...)
  734. item.DataList = newResult
  735. } else {
  736. item.DataList = result
  737. }
  738. }
  739. } else {
  740. currentYear := time.Now().Year()
  741. quarterDataList := make([]*data_manage.QuarterData, 0)
  742. quarterMap := make(map[int][]*data_manage.EdbDataList)
  743. var quarterArr []int
  744. for _, v := range dataList {
  745. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  746. if tmpErr != nil {
  747. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  748. return
  749. }
  750. year := itemDate.Year()
  751. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  752. timestamp := newItemDate.UnixNano() / 1e6
  753. v.DataTimestamp = timestamp
  754. if findVal, ok := quarterMap[year]; !ok {
  755. quarterArr = append(quarterArr, year)
  756. findVal = append(findVal, v)
  757. quarterMap[year] = findVal
  758. } else {
  759. findVal = append(findVal, v)
  760. quarterMap[year] = findVal
  761. }
  762. }
  763. for _, v := range quarterArr {
  764. itemList := quarterMap[v]
  765. quarterItem := new(data_manage.QuarterData)
  766. quarterItem.Year = v
  767. quarterItem.DataList = itemList
  768. //如果等于最后的实际日期,那么将切割时间戳记录
  769. if v == latestDateYear {
  770. var cuttingDataTimestamp int64
  771. for _, tmpData := range itemList {
  772. if tmpData.DataTime == latestDateStr {
  773. cuttingDataTimestamp = tmpData.DataTimestamp
  774. break
  775. }
  776. }
  777. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  778. } else if v > latestDateYear {
  779. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  780. if len(itemList) > 0 {
  781. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  782. }
  783. }
  784. quarterDataList = append(quarterDataList, quarterItem)
  785. }
  786. item.DataList = quarterDataList
  787. }
  788. } else if chartType == 7 { //柱方图
  789. //item.DataList = dataList
  790. } else {
  791. item.DataList = dataList
  792. }
  793. edbList = append(edbList, item)
  794. }
  795. return
  796. }
  797. // BarChartData 柱方图的数据处理
  798. func BarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, barChartInfoDateList []data_manage.BarChartInfoDateReq, barChartInfoSort data_manage.BarChartInfoSortReq) (edbIdList []int, yDataList []data_manage.YData, err error) {
  799. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  800. edbDataMap := make(map[int]map[string]float64)
  801. for edbInfoId, edbDataList := range edbDataListMap {
  802. edbDateData := make(map[string]float64)
  803. for _, edbData := range edbDataList {
  804. edbDateData[edbData.DataTime] = edbData.Value
  805. }
  806. edbDataMap[edbInfoId] = edbDateData
  807. }
  808. // edbIdList 指标展示顺序;x轴的指标顺序
  809. edbIdList = make([]int, 0)
  810. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  811. dateData := make(map[int]float64)
  812. if barChartInfoSort.Sort == 0 {
  813. for _, v := range mappingList {
  814. edbIdList = append(edbIdList, v.EdbInfoId)
  815. }
  816. } else {
  817. lenBarChartInfoDateList := len(barChartInfoDateList)
  818. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  819. err = errors.New("排序日期异常")
  820. return
  821. }
  822. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  823. // 日期配置
  824. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  825. for edbInfoId, dataList := range edbDataListMap {
  826. if len(dataList) <= 0 {
  827. // 没有数据的指标id
  828. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  829. continue
  830. }
  831. findDate := barChartInfoDate.Date
  832. switch barChartInfoDate.Type {
  833. case 1: //最新值
  834. findDate = dataList[len(dataList)-1].DataTime
  835. case 2: //近期几天
  836. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  837. if tmpErr != nil {
  838. err = tmpErr
  839. return
  840. }
  841. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  842. lenData := len(dataList) - 1
  843. for i := lenData; i >= 0; i-- {
  844. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  845. if tmpErr != nil {
  846. err = tmpErr
  847. return
  848. }
  849. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  850. findDate = dataList[i].DataTime
  851. break
  852. }
  853. }
  854. case 3: // 固定日期
  855. //最早的日期
  856. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  857. if tmpErr != nil {
  858. err = tmpErr
  859. return
  860. }
  861. //寻找固定日期的数据
  862. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  863. if tmpErr != nil {
  864. err = tmpErr
  865. return
  866. }
  867. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  868. tmpDate := tmpDateTime.Format(utils.FormatDate)
  869. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  870. findDate = tmpDate
  871. break
  872. }
  873. }
  874. default:
  875. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  876. return
  877. }
  878. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  879. dateData[edbInfoId] = tmpValue
  880. } else {
  881. // 没有数据的指标id
  882. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  883. }
  884. }
  885. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  886. // 排序
  887. dateDataSort := utils.NewMapSorter(dateData)
  888. sort.Sort(dateDataSort)
  889. if barChartInfoSort.Sort == 1 {
  890. // 先将没有数据的指标id放在最前面
  891. if len(notDataEdbIdList) > 0 {
  892. edbIdList = append(edbIdList, notDataEdbIdList...)
  893. }
  894. for _, v := range dateDataSort {
  895. edbIdList = append(edbIdList, v.Key)
  896. }
  897. } else {
  898. for i := len(dateDataSort) - 1; i >= 0; i-- {
  899. edbIdList = append(edbIdList, dateDataSort[i].Key)
  900. }
  901. // 再将没有数据的指标id放在最后面
  902. if len(notDataEdbIdList) > 0 {
  903. edbIdList = append(edbIdList, notDataEdbIdList...)
  904. }
  905. }
  906. }
  907. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  908. for _, barChartInfoDate := range barChartInfoDateList {
  909. var maxDate time.Time
  910. findDataList := make([]float64, 0) // 当前日期的数据值
  911. for _, edbInfoId := range edbIdList {
  912. findDate := barChartInfoDate.Date //需要的日期值
  913. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  914. if len(dataList) <= 0 {
  915. // 没有数据的指标id
  916. findDataList = append(findDataList, 0)
  917. continue
  918. }
  919. switch barChartInfoDate.Type {
  920. case 1: //最新值
  921. dataList := edbDataListMap[edbInfoId]
  922. findDate = dataList[len(dataList)-1].DataTime
  923. case 2: //近期几天
  924. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  925. if tmpErr != nil {
  926. err = tmpErr
  927. return
  928. }
  929. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  930. lenData := len(dataList) - 1
  931. for i := lenData; i >= 0; i-- {
  932. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  933. if tmpErr != nil {
  934. err = tmpErr
  935. return
  936. }
  937. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  938. findDate = dataList[i].DataTime
  939. break
  940. }
  941. }
  942. case 3: // 固定日期
  943. //最早的日期
  944. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  945. if tmpErr != nil {
  946. err = tmpErr
  947. return
  948. }
  949. //寻找固定日期的数据
  950. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  951. if tmpErr != nil {
  952. err = tmpErr
  953. return
  954. }
  955. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  956. tmpDate := tmpDateTime.Format(utils.FormatDate)
  957. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  958. findDate = tmpDate
  959. break
  960. }
  961. }
  962. default:
  963. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  964. return
  965. }
  966. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  967. if maxDate.IsZero() {
  968. maxDate = findDateTime
  969. } else {
  970. if findDateTime.After(maxDate) {
  971. maxDate = findDateTime
  972. }
  973. }
  974. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  975. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  976. findDataList = append(findDataList, tmpValue)
  977. } else {
  978. findDataList = append(findDataList, 0)
  979. }
  980. }
  981. yDate := "0000-00-00"
  982. if !maxDate.IsZero() {
  983. yDate = maxDate.Format(utils.FormatDate)
  984. }
  985. yDataList = append(yDataList, data_manage.YData{
  986. Date: yDate,
  987. Value: findDataList,
  988. Color: barChartInfoDate.Color,
  989. Name: barChartInfoDate.Name,
  990. })
  991. }
  992. return
  993. }
  994. // ChartInfoRefreshV2 图表刷新
  995. // @author Roc
  996. // @datetime 2022-09-16 10:15:38
  997. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  998. func ChartInfoRefreshV2(chartInfoId int) (err error) {
  999. var errmsg string
  1000. defer func() {
  1001. if err != nil {
  1002. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1003. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1004. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1005. }
  1006. }()
  1007. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1008. if err != nil {
  1009. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1010. return
  1011. }
  1012. edbIdList := make([]int, 0)
  1013. for _, v := range edbMappingList {
  1014. edbIdList = append(edbIdList, v.EdbInfoId)
  1015. }
  1016. // 批量刷新
  1017. err, _ = EdbInfoRefreshAllFromBaseV3(edbIdList, false, true)
  1018. if err != nil {
  1019. return
  1020. }
  1021. return
  1022. }
  1023. // BatchChartInfoRefreshV2 图表批量刷新
  1024. // @author Roc
  1025. // @datetime 2022-09-16 10:15:38
  1026. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1027. func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo) (err error) {
  1028. if len(chartInfoList) <= 0 {
  1029. return
  1030. }
  1031. chartInfoIdSlice := make([]int, 0)
  1032. for _, chartInfo := range chartInfoList {
  1033. chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
  1034. }
  1035. var errmsg string
  1036. defer func() {
  1037. if err != nil {
  1038. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1039. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1040. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1041. }
  1042. }()
  1043. edbIdList := make([]int, 0)
  1044. edbInfoIdMap := make(map[int]int)
  1045. edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
  1046. for _, v := range edbMappingList {
  1047. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  1048. edbIdList = append(edbIdList, v.EdbInfoId)
  1049. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  1050. }
  1051. }
  1052. // 批量刷新
  1053. err, _ = EdbInfoRefreshAllFromBaseV3(edbIdList, false, true)
  1054. if err != nil {
  1055. return
  1056. }
  1057. return
  1058. }
  1059. func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
  1060. // 相关性图表不判断指标
  1061. if utils.InArrayByInt([]int{utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION, utils.CHART_SOURCE_LINE_EQUATION}, source) && chartNameEn != "" {
  1062. return true
  1063. }
  1064. if chartNameEn != "" && len(edbList) <= 0 {
  1065. return true
  1066. }
  1067. if chartNameEn == "" {
  1068. return false
  1069. }
  1070. // 截面散点图的话,肯定是有英文配置的
  1071. if chartType == utils.CHART_TYPE_SECTION_SCATTER {
  1072. return true
  1073. }
  1074. for _, v := range edbList {
  1075. if v.EdbNameEn == "" {
  1076. return false
  1077. }
  1078. if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
  1079. return false
  1080. }
  1081. }
  1082. return true
  1083. }
  1084. func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
  1085. if edbNameEn == "" {
  1086. return false
  1087. }
  1088. if unit != "无" && unit != "" && unitEn == "" {
  1089. return false
  1090. }
  1091. return true
  1092. }
  1093. // CheckIsDisableChart 根据指标Id判断是否是禁用图表
  1094. // return int 0:启用,1:禁用
  1095. func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
  1096. crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
  1097. if err != nil {
  1098. return
  1099. }
  1100. //没有配置,立马返回
  1101. if crmConfig.ConfigValue == `` {
  1102. return
  1103. }
  1104. confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
  1105. for _, edbInfoId := range edbInfoIdArr {
  1106. for _, confEdbInfoId := range confEdbInfoIdList {
  1107. if strconv.Itoa(edbInfoId) == confEdbInfoId {
  1108. isDisable = 1
  1109. return
  1110. }
  1111. }
  1112. }
  1113. return
  1114. }
  1115. // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
  1116. func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
  1117. switch chartType {
  1118. case 10: //截面散点
  1119. var extraConfig data_manage.SectionScatterReq
  1120. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1121. if err != nil {
  1122. return
  1123. }
  1124. // 判断是否有配置日期序列
  1125. if len(extraConfig.SeriesList) <= 0 {
  1126. errMsg = `请配置序列`
  1127. err = errors.New(errMsg)
  1128. return
  1129. }
  1130. // 判断是否有填写指标
  1131. if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
  1132. errMsg = `请选择指标`
  1133. err = errors.New(errMsg)
  1134. return
  1135. }
  1136. // 遍历指标列表获取指标id
  1137. edbIdMap := make(map[int]int)
  1138. for _, v := range extraConfig.SeriesList[0].EdbInfoList {
  1139. // X 轴的指标id
  1140. if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
  1141. edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
  1142. edbIdList = append(edbIdList, v.XEdbInfoId)
  1143. }
  1144. // Y 轴的指标id
  1145. if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
  1146. edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
  1147. edbIdList = append(edbIdList, v.YEdbInfoId)
  1148. }
  1149. }
  1150. }
  1151. return
  1152. }
  1153. // GetSectionScatterChartData 截面散点图的数据处理
  1154. func GetSectionScatterChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
  1155. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1156. edbDataMap := make(map[int]map[string]float64)
  1157. for edbInfoId, edbDataList := range edbDataListMap {
  1158. edbDateData := make(map[string]float64)
  1159. for _, edbData := range edbDataList {
  1160. edbDateData[edbData.DataTime] = edbData.Value
  1161. }
  1162. edbDataMap[edbInfoId] = edbDateData
  1163. }
  1164. // edbIdList 指标展示顺序;x轴的指标顺序
  1165. edbIdList = make([]int, 0)
  1166. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  1167. for _, v := range mappingList {
  1168. edbIdList = append(edbIdList, v.EdbInfoId)
  1169. edbMappingMap[v.EdbInfoId] = v
  1170. }
  1171. //SectionScatterSeriesInfoResp
  1172. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  1173. for _, seriesItem := range extraConfig.SeriesList {
  1174. var maxDate time.Time
  1175. // 系列中的指标数据
  1176. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  1177. var minXVal, maxXVal, minYVal, maxYVal float64
  1178. for _, edbConf := range seriesItem.EdbInfoList {
  1179. tmpItem := data_manage.SectionScatterEdbItemResp{
  1180. IsShow: edbConf.IsShow,
  1181. Name: edbConf.Name,
  1182. NameEn: edbConf.NameEn,
  1183. } //单个坐标点的数据
  1184. //X轴的数据
  1185. {
  1186. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  1187. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1188. if !ok {
  1189. continue
  1190. }
  1191. findDate := edbConf.XDate //需要的日期值
  1192. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1193. if len(dataList) <= 0 {
  1194. // 没有数据的指标id
  1195. //findDataList = append(findDataList, 0)
  1196. continue
  1197. }
  1198. tmpItem.XEdbInfoId = edbInfoId
  1199. tmpItem.XName = edbMappingInfo.EdbName
  1200. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  1201. switch edbConf.XDateType {
  1202. case 1: //最新值
  1203. findDate = dataList[len(dataList)-1].DataTime
  1204. case 2: //近期几天
  1205. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1206. if tmpErr != nil {
  1207. err = tmpErr
  1208. return
  1209. }
  1210. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  1211. lenData := len(dataList) - 1
  1212. for i := lenData; i >= 0; i-- {
  1213. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1214. if tmpErr != nil {
  1215. err = tmpErr
  1216. return
  1217. }
  1218. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1219. findDate = dataList[i].DataTime
  1220. break
  1221. }
  1222. }
  1223. case 3: // 固定日期
  1224. //最早的日期
  1225. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1226. if tmpErr != nil {
  1227. err = tmpErr
  1228. return
  1229. }
  1230. //寻找固定日期的数据
  1231. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  1232. if tmpErr != nil {
  1233. err = tmpErr
  1234. return
  1235. }
  1236. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1237. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1238. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1239. findDate = tmpDate
  1240. break
  1241. }
  1242. }
  1243. default:
  1244. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  1245. return
  1246. }
  1247. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1248. if maxDate.IsZero() {
  1249. maxDate = findDateTime
  1250. } else {
  1251. if findDateTime.After(maxDate) {
  1252. maxDate = findDateTime
  1253. }
  1254. }
  1255. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1256. tmpItem.XDate = findDate
  1257. tmpItem.XValue = tmpValue
  1258. } else {
  1259. continue
  1260. }
  1261. }
  1262. //Y轴的数据
  1263. {
  1264. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1265. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1266. if !ok {
  1267. continue
  1268. }
  1269. findDate := edbConf.YDate //需要的日期值
  1270. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1271. if len(dataList) <= 0 {
  1272. // 没有数据的指标id
  1273. //findDataList = append(findDataList, 0)
  1274. continue
  1275. }
  1276. tmpItem.YEdbInfoId = edbInfoId
  1277. tmpItem.YName = edbMappingInfo.EdbName
  1278. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1279. switch edbConf.YDateType {
  1280. case 1: //最新值
  1281. findDate = dataList[len(dataList)-1].DataTime
  1282. case 2: //近期几天
  1283. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1284. if tmpErr != nil {
  1285. err = tmpErr
  1286. return
  1287. }
  1288. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1289. lenData := len(dataList) - 1
  1290. for i := lenData; i >= 0; i-- {
  1291. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1292. if tmpErr != nil {
  1293. err = tmpErr
  1294. return
  1295. }
  1296. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1297. findDate = dataList[i].DataTime
  1298. break
  1299. }
  1300. }
  1301. case 3: // 固定日期
  1302. //最早的日期
  1303. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1304. if tmpErr != nil {
  1305. err = tmpErr
  1306. return
  1307. }
  1308. //寻找固定日期的数据
  1309. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1310. if tmpErr != nil {
  1311. err = tmpErr
  1312. return
  1313. }
  1314. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1315. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1316. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1317. findDate = tmpDate
  1318. break
  1319. }
  1320. }
  1321. default:
  1322. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1323. return
  1324. }
  1325. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1326. if maxDate.IsZero() {
  1327. maxDate = findDateTime
  1328. } else {
  1329. if findDateTime.After(maxDate) {
  1330. maxDate = findDateTime
  1331. }
  1332. }
  1333. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1334. tmpItem.YDate = findDate
  1335. tmpItem.YValue = tmpValue
  1336. } else {
  1337. continue
  1338. }
  1339. }
  1340. // 获取当前系列的X轴的最大最小值
  1341. {
  1342. if tmpItem.XValue < minXVal {
  1343. minXVal = tmpItem.XValue
  1344. }
  1345. if tmpItem.XValue > maxXVal {
  1346. maxXVal = tmpItem.XValue
  1347. }
  1348. if tmpItem.YValue < minYVal {
  1349. minYVal = tmpItem.YValue
  1350. }
  1351. if tmpItem.YValue > maxYVal {
  1352. maxYVal = tmpItem.YValue
  1353. }
  1354. }
  1355. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1356. }
  1357. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  1358. var trendLine, rSquare string
  1359. // 生成线性方程式
  1360. var a, b float64
  1361. {
  1362. coordinateData := make([]utils.Coordinate, 0)
  1363. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1364. tmpCoordinate1 := utils.Coordinate{
  1365. X: tmpSeriesEdbInfo.XValue,
  1366. Y: tmpSeriesEdbInfo.YValue,
  1367. }
  1368. coordinateData = append(coordinateData, tmpCoordinate1)
  1369. }
  1370. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  1371. if len(coordinateData) >= 2 {
  1372. a, b = utils.GetLinearResult(coordinateData)
  1373. if !math.IsNaN(a) && !math.IsNaN(b) {
  1374. if b > 0 {
  1375. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1376. } else {
  1377. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1378. }
  1379. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1380. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1381. }
  1382. // 计算R平方
  1383. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  1384. }
  1385. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  1386. X: minXVal,
  1387. Y: minYVal,
  1388. }, data_manage.CoordinatePoint{
  1389. X: maxXVal,
  1390. Y: maxYVal,
  1391. })
  1392. }
  1393. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  1394. Name: seriesItem.Name,
  1395. NameEn: seriesItem.NameEn,
  1396. Color: seriesItem.Color,
  1397. EdbInfoList: tmpSeriesEdbInfoList,
  1398. ShowTrendLine: seriesItem.ShowTrendLine,
  1399. ShowFitEquation: seriesItem.ShowFitEquation,
  1400. ShowRSquare: seriesItem.ShowRSquare,
  1401. TrendLine: trendLine,
  1402. RSquare: rSquare,
  1403. TrendLimitData: trendLimitData,
  1404. })
  1405. }
  1406. chartDataResp = data_manage.SectionScatterInfoResp{
  1407. XName: extraConfig.XName,
  1408. XNameEn: extraConfig.XNameEn,
  1409. XUnitName: extraConfig.XUnitName,
  1410. XUnitNameEn: extraConfig.XUnitNameEn,
  1411. YName: extraConfig.YName,
  1412. YNameEn: extraConfig.YNameEn,
  1413. YUnitName: extraConfig.YUnitName,
  1414. YUnitNameEn: extraConfig.YUnitNameEn,
  1415. XMinValue: extraConfig.XMinValue,
  1416. XMaxValue: extraConfig.XMaxValue,
  1417. YMinValue: extraConfig.YMinValue,
  1418. YMaxValue: extraConfig.YMaxValue,
  1419. DataList: dataListResp,
  1420. }
  1421. return
  1422. }
  1423. // AddChartInfo 添加图表
  1424. func AddChartInfo(req data_manage.AddChartInfoReq, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1425. isSendEmail = true // 默认错误的时候要发送邮件
  1426. req.ChartName = strings.Trim(req.ChartName, " ")
  1427. if req.ChartName == "" {
  1428. errMsg = "请填写图表名称!"
  1429. err = errors.New(errMsg)
  1430. isSendEmail = false
  1431. return
  1432. }
  1433. if req.ChartClassifyId <= 0 {
  1434. errMsg = "分类参数错误!"
  1435. err = errors.New(errMsg)
  1436. isSendEmail = false
  1437. return
  1438. }
  1439. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1440. if err != nil {
  1441. if err.Error() == utils.ErrNoRow() {
  1442. errMsg = "分类不存在"
  1443. err = errors.New(errMsg)
  1444. return
  1445. }
  1446. errMsg = "获取分类信息失败"
  1447. return
  1448. }
  1449. if chartClassify == nil {
  1450. errMsg = "分类不存在"
  1451. err = errors.New(errMsg)
  1452. return
  1453. }
  1454. chartType := req.ChartType
  1455. extraConfig := req.ExtraConfig
  1456. // 关联指标
  1457. var edbInfoIdArr []int
  1458. chartEdbInfoList := req.ChartEdbInfoList
  1459. if len(chartEdbInfoList) > 0 {
  1460. for _, v := range chartEdbInfoList {
  1461. edbInfoId := v.EdbInfoId
  1462. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1463. if tmpErr != nil {
  1464. if tmpErr.Error() == utils.ErrNoRow() {
  1465. errMsg = "指标不存在!"
  1466. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1467. return
  1468. } else {
  1469. errMsg = "获取指标信息失败!"
  1470. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1471. return
  1472. }
  1473. }
  1474. if edbInfo == nil {
  1475. errMsg = "指标已被删除,请重新选择!"
  1476. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1477. return
  1478. } else {
  1479. if edbInfo.EdbInfoId <= 0 {
  1480. errMsg = "指标已被删除,请重新选择!"
  1481. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1482. return
  1483. }
  1484. }
  1485. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1486. edbInfo.EdbNameSource = edbInfo.EdbName
  1487. //edbList = append(edbList, edbInfo)
  1488. //新增季节性图表数据计算
  1489. if chartType == 2 {
  1490. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  1491. if tmpErr != nil {
  1492. errMsg = "保存失败"
  1493. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  1494. return
  1495. }
  1496. if existCount <= 0 {
  1497. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  1498. }
  1499. }
  1500. }
  1501. } else {
  1502. if extraConfig == `` {
  1503. errMsg = "请选择指标!"
  1504. err = errors.New(errMsg)
  1505. return
  1506. }
  1507. tmpEdbInfoIdArr, tmpErr, tmpErrMsg := CheckChartExtraConfig(chartType, extraConfig)
  1508. if err != nil {
  1509. errMsg = tmpErrMsg
  1510. err = errors.New("添加失败:" + tmpErr.Error())
  1511. return
  1512. }
  1513. edbInfoIdArr = tmpEdbInfoIdArr
  1514. lenEdbInfoIdArr := len(edbInfoIdArr)
  1515. if lenEdbInfoIdArr > 0 {
  1516. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  1517. if tmpErr != nil {
  1518. errMsg = "指标异常!"
  1519. err = errors.New("指标异常!Err:" + tmpErr.Error())
  1520. return
  1521. }
  1522. if len(tmpEdbList) != lenEdbInfoIdArr {
  1523. errMsg = "指标异常!"
  1524. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  1525. return
  1526. }
  1527. for _, v := range tmpEdbList {
  1528. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  1529. EdbInfoId: v.EdbInfoId,
  1530. MaxData: 0,
  1531. MinData: 0,
  1532. IsOrder: false,
  1533. IsAxis: 1,
  1534. EdbInfoType: 1,
  1535. LeadValue: 0,
  1536. LeadUnit: "",
  1537. ChartStyle: "",
  1538. ChartColor: "",
  1539. PredictChartColor: "",
  1540. ChartWidth: 0,
  1541. Source: utils.CHART_SOURCE_DEFAULT,
  1542. })
  1543. }
  1544. }
  1545. }
  1546. if len(edbInfoIdArr) <= 0 {
  1547. errMsg = "请选择指标!"
  1548. err = errors.New(errMsg)
  1549. return
  1550. }
  1551. // 图表额外配置
  1552. extraConfig, err, errMsg = HandleExtraConfig(chartType, extraConfig)
  1553. if err != nil {
  1554. if errMsg == `` {
  1555. errMsg = "指标异常!"
  1556. }
  1557. err = errors.New("指标异常!Err:" + err.Error())
  1558. return
  1559. }
  1560. sort.Ints(edbInfoIdArr)
  1561. var edbInfoIdArrStr []string
  1562. for _, v := range edbInfoIdArr {
  1563. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1564. }
  1565. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1566. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  1567. //if err != nil && err.Error() != utils.ErrNoRow() {
  1568. // br.Msg = "保存失败"
  1569. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  1570. // return
  1571. //}
  1572. //if count > 0 {
  1573. // br.Msg = "所选指标已存在相同指标"
  1574. // return
  1575. //}
  1576. //判断图表是否存在
  1577. var condition string
  1578. var pars []interface{}
  1579. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1580. //condition += " AND chart_classify_id=? "
  1581. //pars = append(pars, req.ChartClassifyId)
  1582. condition += " AND chart_name=? AND source = ? "
  1583. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  1584. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1585. if err != nil {
  1586. errMsg = "判断图表名称是否存在失败"
  1587. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  1588. return
  1589. }
  1590. if count > 0 {
  1591. errMsg = "图表已存在,请重新填写"
  1592. err = errors.New("判断图表名称是否存在失败")
  1593. isSendEmail = false
  1594. return
  1595. }
  1596. barChartConf := `` //柱方图的配置信息
  1597. if chartType == 7 {
  1598. barChartConfReq := req.BarChartInfo
  1599. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  1600. errMsg = "请选择指标"
  1601. err = errors.New(errMsg)
  1602. isSendEmail = false
  1603. return
  1604. }
  1605. if len(barChartConfReq.DateList) <= 0 {
  1606. errMsg = "请选择日期"
  1607. err = errors.New(errMsg)
  1608. isSendEmail = false
  1609. return
  1610. }
  1611. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  1612. if tmpErr != nil {
  1613. errMsg = "柱方图信息异常"
  1614. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  1615. return
  1616. }
  1617. barChartConf = string(barChartConfByte)
  1618. }
  1619. disableVal := CheckIsDisableChart(edbInfoIdArr)
  1620. chartInfo = new(data_manage.ChartInfo)
  1621. chartInfo.ChartName = req.ChartName
  1622. chartInfo.EdbInfoIds = edbInfoIdStr
  1623. chartInfo.ChartClassifyId = req.ChartClassifyId
  1624. chartInfo.SysUserId = sysUserId
  1625. chartInfo.SysUserRealName = sysUserRealName
  1626. chartInfo.CreateTime = time.Now()
  1627. chartInfo.ModifyTime = time.Now()
  1628. chartInfo.IsSetName = 0
  1629. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1630. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  1631. if req.DateType > 0 {
  1632. chartInfo.DateType = req.DateType
  1633. } else {
  1634. chartInfo.DateType = 3
  1635. }
  1636. if chartType == 0 {
  1637. chartType = 1
  1638. }
  1639. chartInfo.ChartType = chartType
  1640. calendar := req.Calendar
  1641. if calendar == "" {
  1642. calendar = "公历"
  1643. }
  1644. chartInfo.Calendar = calendar
  1645. chartInfo.StartDate = req.StartDate
  1646. chartInfo.EndDate = req.EndDate
  1647. chartInfo.SeasonStartDate = req.SeasonStartDate
  1648. chartInfo.SeasonEndDate = req.SeasonEndDate
  1649. chartInfo.LeftMin = req.LeftMin
  1650. chartInfo.LeftMax = req.LeftMax
  1651. chartInfo.RightMin = req.RightMin
  1652. chartInfo.RightMax = req.RightMax
  1653. chartInfo.Disabled = disableVal
  1654. chartInfo.BarConfig = barChartConf
  1655. chartInfo.ExtraConfig = extraConfig
  1656. chartInfo.Source = utils.CHART_SOURCE_DEFAULT
  1657. newId, err := data_manage.AddChartInfo(chartInfo)
  1658. if err != nil {
  1659. errMsg = `保存失败`
  1660. err = errors.New("保存失败,Err:" + err.Error())
  1661. return
  1662. }
  1663. chartInfo.ChartInfoId = int(newId)
  1664. mapList := make([]*data_manage.ChartEdbMapping, 0)
  1665. for _, v := range chartEdbInfoList {
  1666. mapItem := new(data_manage.ChartEdbMapping)
  1667. mapItem.ChartInfoId = int(newId)
  1668. mapItem.EdbInfoId = v.EdbInfoId
  1669. mapItem.CreateTime = time.Now()
  1670. mapItem.ModifyTime = time.Now()
  1671. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1672. mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", v.EdbInfoId, "_", timestamp))
  1673. mapItem.MaxData = v.MaxData
  1674. mapItem.MinData = v.MinData
  1675. mapItem.IsOrder = v.IsOrder
  1676. mapItem.IsAxis = v.IsAxis
  1677. mapItem.EdbInfoType = v.EdbInfoType
  1678. mapItem.LeadValue = v.LeadValue
  1679. mapItem.LeadUnit = v.LeadUnit
  1680. mapItem.ChartStyle = v.ChartStyle
  1681. mapItem.ChartColor = v.ChartColor
  1682. mapItem.PredictChartColor = v.PredictChartColor
  1683. mapItem.ChartWidth = v.ChartWidth
  1684. mapItem.Source = utils.CHART_SOURCE_DEFAULT
  1685. mapList = append(mapList, mapItem)
  1686. }
  1687. err = data_manage.AddChartEdbMapping(mapList)
  1688. if err != nil {
  1689. errMsg = `保存失败`
  1690. err = errors.New("保存失败,Err:" + err.Error())
  1691. return
  1692. }
  1693. //添加es数据
  1694. go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1695. return
  1696. }
  1697. // EditChartInfo 编辑图表
  1698. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1699. isSendEmail = true // 默认错误的时候要发送邮件
  1700. req.ChartName = strings.Trim(req.ChartName, " ")
  1701. if req.ChartName == "" {
  1702. errMsg = "请填写图表名称!"
  1703. err = errors.New(errMsg)
  1704. isSendEmail = false
  1705. return
  1706. }
  1707. if req.ChartClassifyId <= 0 {
  1708. errMsg = "分类参数错误!"
  1709. err = errors.New(errMsg)
  1710. isSendEmail = false
  1711. return
  1712. }
  1713. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1714. if err != nil {
  1715. if err.Error() == utils.ErrNoRow() {
  1716. errMsg = "分类不存在"
  1717. err = errors.New(errMsg)
  1718. return
  1719. }
  1720. errMsg = "获取分类信息失败"
  1721. return
  1722. }
  1723. if chartClassify == nil {
  1724. errMsg = "分类不存在"
  1725. err = errors.New(errMsg)
  1726. return
  1727. }
  1728. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  1729. if err != nil {
  1730. if err.Error() == utils.ErrNoRow() {
  1731. errMsg = "图表已被删除,请刷新页面"
  1732. err = errors.New(errMsg)
  1733. isSendEmail = false
  1734. return
  1735. }
  1736. errMsg = "获取图表信息失败"
  1737. err = errors.New("获取图表信息失败,Err:" + err.Error())
  1738. return
  1739. }
  1740. // 图表关联指标id
  1741. chartEdbInfoList := make([]*data_manage.ChartSaveItem, 0)
  1742. // 关联指标
  1743. var edbInfoIdArr []int
  1744. // 非散点截面图的直接通过前端获取,散点截面图需要额外从配置中获取
  1745. if chartItem.ChartType != 10 {
  1746. //edbList := make([]*data_manage.EdbInfo, 0)
  1747. for _, v := range req.ChartEdbInfoList {
  1748. edbInfoId := v.EdbInfoId
  1749. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1750. if tmpErr != nil {
  1751. if tmpErr.Error() == utils.ErrNoRow() {
  1752. errMsg = "指标不存在!"
  1753. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1754. return
  1755. } else {
  1756. errMsg = "获取指标信息失败!"
  1757. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1758. return
  1759. }
  1760. }
  1761. if edbInfo == nil {
  1762. errMsg = "指标已被删除,请重新选择!"
  1763. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1764. return
  1765. } else {
  1766. if edbInfo.EdbInfoId <= 0 {
  1767. errMsg = "指标已被删除,请重新选择!"
  1768. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1769. return
  1770. }
  1771. }
  1772. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1773. edbInfo.EdbNameSource = edbInfo.EdbName
  1774. //edbList = append(edbList, edbInfo)
  1775. //新增季节性图表数据计算
  1776. if req.ChartType == 2 {
  1777. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  1778. if tmpErr != nil {
  1779. errMsg = "保存失败"
  1780. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  1781. return
  1782. }
  1783. if existCount <= 0 {
  1784. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  1785. }
  1786. }
  1787. }
  1788. chartEdbInfoList = req.ChartEdbInfoList
  1789. } else {
  1790. if req.ExtraConfig == `` {
  1791. errMsg = "请选择指标!"
  1792. err = errors.New(errMsg)
  1793. isSendEmail = false
  1794. return
  1795. }
  1796. // 校验配置的指标列表
  1797. tmpEdbInfoIdArr, tmpErr, tmpErrMsg := CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
  1798. if tmpErr != nil {
  1799. errMsg = tmpErrMsg
  1800. err = errors.New("添加失败:" + tmpErr.Error())
  1801. return
  1802. }
  1803. edbInfoIdArr = tmpEdbInfoIdArr
  1804. lenEdbInfoIdArr := len(edbInfoIdArr)
  1805. if lenEdbInfoIdArr > 0 {
  1806. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  1807. if tmpErr != nil {
  1808. errMsg = "指标异常!"
  1809. err = errors.New("指标异常!Err:" + tmpErr.Error())
  1810. return
  1811. }
  1812. if len(tmpEdbList) != lenEdbInfoIdArr {
  1813. errMsg = "指标异常!"
  1814. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  1815. return
  1816. }
  1817. for _, v := range tmpEdbList {
  1818. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  1819. EdbInfoId: v.EdbInfoId,
  1820. MaxData: 0,
  1821. MinData: 0,
  1822. IsOrder: false,
  1823. IsAxis: 1,
  1824. EdbInfoType: 1,
  1825. LeadValue: 0,
  1826. LeadUnit: "",
  1827. ChartStyle: "",
  1828. ChartColor: "",
  1829. PredictChartColor: "",
  1830. ChartWidth: 0,
  1831. Source: utils.CHART_SOURCE_DEFAULT,
  1832. })
  1833. }
  1834. }
  1835. }
  1836. if len(edbInfoIdArr) <= 0 {
  1837. errMsg = "请选择指标!"
  1838. err = errors.New(errMsg)
  1839. isSendEmail = false
  1840. return
  1841. }
  1842. // 图表额外配置
  1843. extraConfig, err, errMsg := HandleExtraConfig(req.ChartType, req.ExtraConfig)
  1844. if err != nil {
  1845. if errMsg == `` {
  1846. errMsg = "指标异常!"
  1847. }
  1848. err = errors.New("指标异常!Err:" + err.Error())
  1849. return
  1850. }
  1851. req.ExtraConfig = extraConfig
  1852. //图表操作权限
  1853. ok := CheckOpChartPermission(sysUser, chartItem.SysUserId)
  1854. if !ok {
  1855. errMsg = "没有该图表的操作权限"
  1856. err = errors.New(errMsg)
  1857. isSendEmail = false
  1858. return
  1859. }
  1860. var calendar string
  1861. var dateType int
  1862. dateType = req.DateType
  1863. if req.ChartType != chartItem.ChartType {
  1864. if chartItem.ChartType != 2 && chartItem.Calendar == "" { //非季节性图
  1865. calendar = "公历"
  1866. }
  1867. if chartItem.ChartType == 2 && chartItem.DateType <= 0 {
  1868. dateType = 3
  1869. }
  1870. }
  1871. sort.Ints(edbInfoIdArr)
  1872. var edbInfoIdArrStr []string
  1873. for _, v := range edbInfoIdArr {
  1874. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1875. }
  1876. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1877. //mappingCondition := ` AND chart_info_id<> ` + strconv.Itoa(req.ChartInfoId)
  1878. //count, err := data_manage.ChartInfoExist(mappingCondition, edbInfoIdStr)
  1879. //if err != nil && err.Error() != utils.ErrNoRow() {
  1880. // br.Msg = "保存失败"
  1881. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  1882. // return
  1883. //}
  1884. //if count > 0 {
  1885. // br.Msg = "所选指标已存在相同指标"
  1886. // return
  1887. //}
  1888. //判断图表是否存在
  1889. var condition string
  1890. var pars []interface{}
  1891. condition += " AND chart_info_id<>? "
  1892. pars = append(pars, req.ChartInfoId)
  1893. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1894. //condition += " AND chart_classify_id=? "
  1895. //pars = append(pars, req.ChartClassifyId)
  1896. condition += " AND chart_name=? AND source = ? "
  1897. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  1898. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1899. if err != nil {
  1900. errMsg = "判断图表名称是否存在失败"
  1901. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  1902. return
  1903. }
  1904. if count > 0 {
  1905. errMsg = "图表已存在,请重新填写"
  1906. err = errors.New(errMsg)
  1907. isSendEmail = false
  1908. return
  1909. }
  1910. barChartConf := `` //柱方图的配置信息
  1911. if req.ChartType == 7 {
  1912. barChartConfReq := req.BarChartInfo
  1913. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  1914. errMsg = "请选择指标"
  1915. err = errors.New(errMsg)
  1916. isSendEmail = false
  1917. return
  1918. }
  1919. if len(barChartConfReq.DateList) <= 0 {
  1920. errMsg = "请选择日期"
  1921. err = errors.New(errMsg)
  1922. isSendEmail = false
  1923. return
  1924. }
  1925. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  1926. if tmpErr != nil {
  1927. errMsg = "柱方图信息异常"
  1928. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  1929. return
  1930. }
  1931. barChartConf = string(barChartConfByte)
  1932. }
  1933. // 图表启用与否
  1934. disableVal := CheckIsDisableChart(edbInfoIdArr)
  1935. err = data_manage.EditChartInfoAndMapping(&req, edbInfoIdStr, calendar, dateType, disableVal, barChartConf, chartEdbInfoList)
  1936. if err != nil {
  1937. errMsg = "保存失败"
  1938. err = errors.New("保存失败,Err:" + err.Error())
  1939. return
  1940. }
  1941. resp := new(data_manage.AddChartInfoResp)
  1942. resp.ChartInfoId = chartItem.ChartInfoId
  1943. resp.UniqueCode = chartItem.UniqueCode
  1944. resp.ChartType = req.ChartType
  1945. //添加es数据
  1946. go EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1947. //修改my eta es数据
  1948. go EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1949. return
  1950. }