chart_info.go 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/shopspring/decimal"
  7. "hongze/hz_crm_api/models/company"
  8. "hongze/hz_crm_api/models/data_manage"
  9. "hongze/hz_crm_api/models/system"
  10. "hongze/hz_crm_api/services/alarm_msg"
  11. "hongze/hz_crm_api/utils"
  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 _, v := range edbList {
  524. // 指标别名
  525. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  526. for _, reqEdb := range barChartConf.EdbInfoIdList {
  527. if v.EdbInfoId == reqEdb.EdbInfoId {
  528. v.EdbAliasName = reqEdb.Name
  529. }
  530. }
  531. }
  532. }
  533. case 10: // 截面散点图
  534. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  535. xEdbIdValue, dataResp, err = GetSectionScatterChartData(mappingList, edbDataListMap, sectionScatterConf)
  536. // 这个数据没有必要返回给前端
  537. for _, v := range edbList {
  538. v.DataList = nil
  539. }
  540. }
  541. return
  542. }
  543. // GetEdbDataMapList 获取指标最后的基础数据
  544. 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) {
  545. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList)
  546. return
  547. }
  548. // getEdbDataMapList 获取指标最后的基础数据
  549. 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) {
  550. // 指标对应的所有数据
  551. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  552. for _, v := range mappingList {
  553. //fmt.Println("v:", v.EdbInfoId)
  554. item := new(data_manage.ChartEdbInfoMapping)
  555. item.EdbInfoId = v.EdbInfoId
  556. item.SourceName = v.SourceName
  557. item.Source = v.Source
  558. item.EdbCode = v.EdbCode
  559. item.EdbName = v.EdbName
  560. item.EdbNameEn = v.EdbNameEn
  561. item.Frequency = v.Frequency
  562. item.EdbType = v.EdbType
  563. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  564. if v.Unit != `无` {
  565. item.Unit = v.Unit
  566. }
  567. item.UnitEn = v.UnitEn
  568. item.StartDate = v.StartDate
  569. item.EndDate = v.EndDate
  570. item.ModifyTime = v.ModifyTime
  571. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  572. item.PredictChartColor = v.PredictChartColor
  573. item.ClassifyId = v.ClassifyId
  574. if chartInfoId <= 0 {
  575. item.IsAxis = 1
  576. item.LeadValue = 0
  577. item.LeadUnit = ""
  578. item.ChartEdbMappingId = 0
  579. item.ChartInfoId = 0
  580. item.IsOrder = false
  581. item.EdbInfoType = 1
  582. item.ChartStyle = ""
  583. item.ChartColor = ""
  584. item.ChartWidth = 0
  585. item.MaxData = v.MaxValue
  586. item.MinData = v.MinValue
  587. } else {
  588. item.IsAxis = v.IsAxis
  589. item.EdbInfoType = v.EdbInfoType
  590. item.LeadValue = v.LeadValue
  591. item.LeadUnit = v.LeadUnit
  592. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  593. item.ChartEdbMappingId = v.ChartEdbMappingId
  594. item.ChartInfoId = v.ChartInfoId
  595. item.ChartStyle = v.ChartStyle
  596. item.ChartColor = v.ChartColor
  597. item.ChartWidth = v.ChartWidth
  598. item.IsOrder = v.IsOrder
  599. item.MaxData = v.MaxData
  600. item.MinData = v.MinData
  601. }
  602. item.LatestValue = v.LatestValue
  603. item.LatestDate = v.LatestDate
  604. item.UniqueCode = v.UniqueCode
  605. item.MoveLatestDate = v.LatestDate
  606. var startDateReal string
  607. var diffSeconds int64
  608. if chartType == 2 { //季节性图
  609. startDateReal = startDate
  610. } else {
  611. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  612. var startTimeRealTemp time.Time
  613. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  614. switch v.LeadUnit {
  615. case "天":
  616. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  617. case "月":
  618. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  619. case "季":
  620. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  621. case "周":
  622. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  623. case "年":
  624. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  625. }
  626. if startTimeRealTemp.Before(startDateParse) {
  627. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  628. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  629. } else {
  630. startDateReal = startDate
  631. diffSeconds = 0
  632. }
  633. // 预测指标的开始日期也要偏移
  634. {
  635. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  636. if tmpErr != nil {
  637. err = tmpErr
  638. return
  639. }
  640. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  641. if tmpErr != nil {
  642. err = tmpErr
  643. return
  644. }
  645. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  646. }
  647. } else {
  648. startDateReal = startDate
  649. }
  650. }
  651. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  652. calendarPreYear := 0
  653. if calendar == "农历" {
  654. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  655. if err != nil {
  656. fmt.Println("time.Parse:" + err.Error())
  657. }
  658. calendarPreYear = newStartDateReal.Year() - 1
  659. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  660. startDateReal = newStartDateReal.Format(utils.FormatDate)
  661. }
  662. dataList := make([]*data_manage.EdbDataList, 0)
  663. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  664. //var newEdbInfo *data_manage.EdbInfo
  665. switch v.EdbInfoCategoryType {
  666. case 0:
  667. dataList, err = data_manage.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate)
  668. case 1:
  669. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, false)
  670. default:
  671. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  672. }
  673. if err != nil {
  674. return
  675. }
  676. edbDataListMap[v.EdbInfoId] = dataList
  677. if diffSeconds != 0 && v.EdbInfoType == 0 {
  678. dataListLen := len(dataList)
  679. for i := 0; i < dataListLen; i++ {
  680. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  681. }
  682. }
  683. if chartType == 2 {
  684. latestDateStr := v.LatestDate //实际数据的截止日期
  685. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  686. if tmpErr != nil {
  687. //item.DataList = dataList
  688. item.IsNullData = true
  689. edbList = append(edbList, item)
  690. continue
  691. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  692. return
  693. }
  694. latestDateYear := latestDate.Year() //实际数据截止年份
  695. if calendar == "农历" {
  696. if len(dataList) <= 0 {
  697. result := new(data_manage.EdbDataResult)
  698. item.DataList = result
  699. } else {
  700. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  701. if tmpErr != nil {
  702. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  703. return
  704. }
  705. // 处理季节图的截止日期
  706. for k, edbDataItems := range result.List {
  707. var cuttingDataTimestamp int64
  708. // 切割的日期时间字符串
  709. cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
  710. //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳
  711. if edbDataItems.Year >= latestDateYear {
  712. for _, tmpData := range edbDataItems.Items {
  713. if tmpData.DataTime == cuttingDataTimeStr {
  714. cuttingDataTimestamp = tmpData.DataTimestamp
  715. break
  716. }
  717. }
  718. }
  719. edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
  720. result.List[k] = edbDataItems
  721. }
  722. if result.List[0].Year != calendarPreYear {
  723. itemList := make([]*data_manage.EdbDataList, 0)
  724. items := new(data_manage.EdbDataItems)
  725. //items.Year = calendarPreYear
  726. items.Items = itemList
  727. newResult := new(data_manage.EdbDataResult)
  728. newResult.List = append(newResult.List, items)
  729. newResult.List = append(newResult.List, result.List...)
  730. item.DataList = newResult
  731. } else {
  732. item.DataList = result
  733. }
  734. }
  735. } else {
  736. currentYear := time.Now().Year()
  737. quarterDataList := make([]*data_manage.QuarterData, 0)
  738. quarterMap := make(map[int][]*data_manage.EdbDataList)
  739. var quarterArr []int
  740. for _, v := range dataList {
  741. itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
  742. if tmpErr != nil {
  743. err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
  744. return
  745. }
  746. year := itemDate.Year()
  747. newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
  748. timestamp := newItemDate.UnixNano() / 1e6
  749. v.DataTimestamp = timestamp
  750. if findVal, ok := quarterMap[year]; !ok {
  751. quarterArr = append(quarterArr, year)
  752. findVal = append(findVal, v)
  753. quarterMap[year] = findVal
  754. } else {
  755. findVal = append(findVal, v)
  756. quarterMap[year] = findVal
  757. }
  758. }
  759. for _, v := range quarterArr {
  760. itemList := quarterMap[v]
  761. quarterItem := new(data_manage.QuarterData)
  762. quarterItem.Year = v
  763. quarterItem.DataList = itemList
  764. //如果等于最后的实际日期,那么将切割时间戳记录
  765. if v == latestDateYear {
  766. var cuttingDataTimestamp int64
  767. for _, tmpData := range itemList {
  768. if tmpData.DataTime == latestDateStr {
  769. cuttingDataTimestamp = tmpData.DataTimestamp
  770. break
  771. }
  772. }
  773. quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
  774. } else if v > latestDateYear {
  775. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  776. if len(itemList) > 0 {
  777. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  778. }
  779. }
  780. quarterDataList = append(quarterDataList, quarterItem)
  781. }
  782. item.DataList = quarterDataList
  783. }
  784. } else if chartType == 7 { //柱方图
  785. //item.DataList = dataList
  786. } else {
  787. item.DataList = dataList
  788. }
  789. edbList = append(edbList, item)
  790. }
  791. return
  792. }
  793. // BarChartData 柱方图的数据处理
  794. 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) {
  795. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  796. edbDataMap := make(map[int]map[string]float64)
  797. for edbInfoId, edbDataList := range edbDataListMap {
  798. edbDateData := make(map[string]float64)
  799. for _, edbData := range edbDataList {
  800. edbDateData[edbData.DataTime] = edbData.Value
  801. }
  802. edbDataMap[edbInfoId] = edbDateData
  803. }
  804. // edbIdList 指标展示顺序;x轴的指标顺序
  805. edbIdList = make([]int, 0)
  806. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  807. dateData := make(map[int]float64)
  808. if barChartInfoSort.Sort == 0 {
  809. for _, v := range mappingList {
  810. edbIdList = append(edbIdList, v.EdbInfoId)
  811. }
  812. } else {
  813. lenBarChartInfoDateList := len(barChartInfoDateList)
  814. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  815. err = errors.New("排序日期异常")
  816. return
  817. }
  818. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  819. // 日期配置
  820. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  821. for edbInfoId, dataList := range edbDataListMap {
  822. if len(dataList) <= 0 {
  823. // 没有数据的指标id
  824. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  825. continue
  826. }
  827. findDate := barChartInfoDate.Date
  828. switch barChartInfoDate.Type {
  829. case 1: //最新值
  830. findDate = dataList[len(dataList)-1].DataTime
  831. case 2: //近期几天
  832. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  833. if tmpErr != nil {
  834. err = tmpErr
  835. return
  836. }
  837. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  838. lenData := len(dataList) - 1
  839. for i := lenData; i >= 0; i-- {
  840. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  841. if tmpErr != nil {
  842. err = tmpErr
  843. return
  844. }
  845. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  846. findDate = dataList[i].DataTime
  847. break
  848. }
  849. }
  850. case 3: // 固定日期
  851. //最早的日期
  852. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  853. if tmpErr != nil {
  854. err = tmpErr
  855. return
  856. }
  857. //寻找固定日期的数据
  858. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  859. if tmpErr != nil {
  860. err = tmpErr
  861. return
  862. }
  863. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  864. tmpDate := tmpDateTime.Format(utils.FormatDate)
  865. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  866. findDate = tmpDate
  867. break
  868. }
  869. }
  870. default:
  871. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  872. return
  873. }
  874. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  875. dateData[edbInfoId] = tmpValue
  876. } else {
  877. // 没有数据的指标id
  878. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  879. }
  880. }
  881. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  882. // 排序
  883. dateDataSort := utils.NewMapSorter(dateData)
  884. sort.Sort(dateDataSort)
  885. if barChartInfoSort.Sort == 1 {
  886. // 先将没有数据的指标id放在最前面
  887. if len(notDataEdbIdList) > 0 {
  888. edbIdList = append(edbIdList, notDataEdbIdList...)
  889. }
  890. for _, v := range dateDataSort {
  891. edbIdList = append(edbIdList, v.Key)
  892. }
  893. } else {
  894. for i := len(dateDataSort) - 1; i >= 0; i-- {
  895. edbIdList = append(edbIdList, dateDataSort[i].Key)
  896. }
  897. // 再将没有数据的指标id放在最后面
  898. if len(notDataEdbIdList) > 0 {
  899. edbIdList = append(edbIdList, notDataEdbIdList...)
  900. }
  901. }
  902. }
  903. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  904. for _, barChartInfoDate := range barChartInfoDateList {
  905. var maxDate time.Time
  906. findDataList := make([]float64, 0) // 当前日期的数据值
  907. for _, edbInfoId := range edbIdList {
  908. findDate := barChartInfoDate.Date //需要的日期值
  909. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  910. if len(dataList) <= 0 {
  911. // 没有数据的指标id
  912. findDataList = append(findDataList, 0)
  913. continue
  914. }
  915. switch barChartInfoDate.Type {
  916. case 1: //最新值
  917. dataList := edbDataListMap[edbInfoId]
  918. findDate = dataList[len(dataList)-1].DataTime
  919. case 2: //近期几天
  920. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  921. if tmpErr != nil {
  922. err = tmpErr
  923. return
  924. }
  925. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  926. lenData := len(dataList) - 1
  927. for i := lenData; i >= 0; i-- {
  928. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  929. if tmpErr != nil {
  930. err = tmpErr
  931. return
  932. }
  933. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  934. findDate = dataList[i].DataTime
  935. break
  936. }
  937. }
  938. case 3: // 固定日期
  939. //最早的日期
  940. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  941. if tmpErr != nil {
  942. err = tmpErr
  943. return
  944. }
  945. //寻找固定日期的数据
  946. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  947. if tmpErr != nil {
  948. err = tmpErr
  949. return
  950. }
  951. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  952. tmpDate := tmpDateTime.Format(utils.FormatDate)
  953. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  954. findDate = tmpDate
  955. break
  956. }
  957. }
  958. default:
  959. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  960. return
  961. }
  962. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  963. if maxDate.IsZero() {
  964. maxDate = findDateTime
  965. } else {
  966. if findDateTime.After(maxDate) {
  967. maxDate = findDateTime
  968. }
  969. }
  970. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  971. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  972. findDataList = append(findDataList, tmpValue)
  973. } else {
  974. findDataList = append(findDataList, 0)
  975. }
  976. }
  977. yDate := "0000-00-00"
  978. if !maxDate.IsZero() {
  979. yDate = maxDate.Format(utils.FormatDate)
  980. }
  981. yDataList = append(yDataList, data_manage.YData{
  982. Date: yDate,
  983. Value: findDataList,
  984. Color: barChartInfoDate.Color,
  985. Name: barChartInfoDate.Name,
  986. })
  987. }
  988. return
  989. }
  990. // ChartInfoRefreshV2 图表刷新
  991. // @author Roc
  992. // @datetime 2022-09-16 10:15:38
  993. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  994. func ChartInfoRefreshV2(chartInfoId int) (err error) {
  995. var errmsg string
  996. defer func() {
  997. if err != nil {
  998. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  999. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1000. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1001. }
  1002. }()
  1003. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1004. if err != nil {
  1005. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1006. return
  1007. }
  1008. edbIdList := make([]int, 0)
  1009. for _, v := range edbMappingList {
  1010. edbIdList = append(edbIdList, v.EdbInfoId)
  1011. }
  1012. // 批量刷新
  1013. err = EdbInfoRefreshAllFromBaseV3(edbIdList, false)
  1014. if err != nil {
  1015. return
  1016. }
  1017. return
  1018. }
  1019. // BatchChartInfoRefreshV2 图表批量刷新
  1020. // @author Roc
  1021. // @datetime 2022-09-16 10:15:38
  1022. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1023. func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo) (err error) {
  1024. if len(chartInfoList) <= 0 {
  1025. return
  1026. }
  1027. chartInfoIdSlice := make([]int, 0)
  1028. for _, chartInfo := range chartInfoList {
  1029. chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
  1030. }
  1031. var errmsg string
  1032. defer func() {
  1033. if err != nil {
  1034. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1035. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1036. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1037. }
  1038. }()
  1039. edbIdList := make([]int, 0)
  1040. edbInfoIdMap := make(map[int]int)
  1041. edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
  1042. for _, v := range edbMappingList {
  1043. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  1044. edbIdList = append(edbIdList, v.EdbInfoId)
  1045. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  1046. }
  1047. }
  1048. // 批量刷新
  1049. err = EdbInfoRefreshAllFromBaseV3(edbIdList, false)
  1050. if err != nil {
  1051. return
  1052. }
  1053. return
  1054. }
  1055. func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
  1056. // 相关性图表不判断指标
  1057. if utils.InArrayByInt([]int{utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION, utils.CHART_SOURCE_LINE_EQUATION}, source) && chartNameEn != "" {
  1058. return true
  1059. }
  1060. if chartNameEn != "" && len(edbList) <= 0 {
  1061. return true
  1062. }
  1063. if chartNameEn == "" {
  1064. return false
  1065. }
  1066. // 截面散点图的话,肯定是有英文配置的
  1067. if chartType == utils.CHART_TYPE_SECTION_SCATTER {
  1068. return true
  1069. }
  1070. for _, v := range edbList {
  1071. if v.EdbNameEn == "" {
  1072. return false
  1073. }
  1074. if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
  1075. return false
  1076. }
  1077. }
  1078. return true
  1079. }
  1080. func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
  1081. if edbNameEn == "" {
  1082. return false
  1083. }
  1084. if unit != "无" && unit != "" && unitEn == "" {
  1085. return false
  1086. }
  1087. return true
  1088. }
  1089. // CheckIsDisableChart 根据指标Id判断是否是禁用图表
  1090. // return int 0:启用,1:禁用
  1091. func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
  1092. crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
  1093. if err != nil {
  1094. return
  1095. }
  1096. //没有配置,立马返回
  1097. if crmConfig.ConfigValue == `` {
  1098. return
  1099. }
  1100. confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
  1101. for _, edbInfoId := range edbInfoIdArr {
  1102. for _, confEdbInfoId := range confEdbInfoIdList {
  1103. if strconv.Itoa(edbInfoId) == confEdbInfoId {
  1104. isDisable = 1
  1105. return
  1106. }
  1107. }
  1108. }
  1109. return
  1110. }
  1111. // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
  1112. func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
  1113. switch chartType {
  1114. case 10: //截面散点
  1115. var extraConfig data_manage.SectionScatterReq
  1116. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1117. if err != nil {
  1118. return
  1119. }
  1120. // 判断是否有配置日期序列
  1121. if len(extraConfig.SeriesList) <= 0 {
  1122. errMsg = `请配置序列`
  1123. err = errors.New(errMsg)
  1124. return
  1125. }
  1126. // 判断是否有填写指标
  1127. if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
  1128. errMsg = `请选择指标`
  1129. err = errors.New(errMsg)
  1130. return
  1131. }
  1132. // 遍历指标列表获取指标id
  1133. edbIdMap := make(map[int]int)
  1134. for _, v := range extraConfig.SeriesList[0].EdbInfoList {
  1135. // X 轴的指标id
  1136. if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
  1137. edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
  1138. edbIdList = append(edbIdList, v.XEdbInfoId)
  1139. }
  1140. // Y 轴的指标id
  1141. if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
  1142. edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
  1143. edbIdList = append(edbIdList, v.YEdbInfoId)
  1144. }
  1145. }
  1146. }
  1147. return
  1148. }
  1149. // GetSectionScatterChartData 截面散点图的数据处理
  1150. func GetSectionScatterChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
  1151. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1152. edbDataMap := make(map[int]map[string]float64)
  1153. for edbInfoId, edbDataList := range edbDataListMap {
  1154. edbDateData := make(map[string]float64)
  1155. for _, edbData := range edbDataList {
  1156. edbDateData[edbData.DataTime] = edbData.Value
  1157. }
  1158. edbDataMap[edbInfoId] = edbDateData
  1159. }
  1160. // edbIdList 指标展示顺序;x轴的指标顺序
  1161. edbIdList = make([]int, 0)
  1162. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  1163. for _, v := range mappingList {
  1164. edbIdList = append(edbIdList, v.EdbInfoId)
  1165. edbMappingMap[v.EdbInfoId] = v
  1166. }
  1167. //SectionScatterSeriesInfoResp
  1168. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  1169. for _, seriesItem := range extraConfig.SeriesList {
  1170. var maxDate time.Time
  1171. // 系列中的指标数据
  1172. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  1173. var minXVal, maxXVal, minYVal, maxYVal float64
  1174. for _, edbConf := range seriesItem.EdbInfoList {
  1175. tmpItem := data_manage.SectionScatterEdbItemResp{
  1176. IsShow: edbConf.IsShow,
  1177. Name: edbConf.Name,
  1178. NameEn: edbConf.NameEn,
  1179. } //单个坐标点的数据
  1180. //X轴的数据
  1181. {
  1182. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  1183. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1184. if !ok {
  1185. continue
  1186. }
  1187. findDate := edbConf.XDate //需要的日期值
  1188. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1189. if len(dataList) <= 0 {
  1190. // 没有数据的指标id
  1191. //findDataList = append(findDataList, 0)
  1192. continue
  1193. }
  1194. tmpItem.XEdbInfoId = edbInfoId
  1195. tmpItem.XName = edbMappingInfo.EdbName
  1196. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  1197. switch edbConf.XDateType {
  1198. case 1: //最新值
  1199. findDate = dataList[len(dataList)-1].DataTime
  1200. case 2: //近期几天
  1201. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1202. if tmpErr != nil {
  1203. err = tmpErr
  1204. return
  1205. }
  1206. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  1207. lenData := len(dataList) - 1
  1208. for i := lenData; i >= 0; i-- {
  1209. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1210. if tmpErr != nil {
  1211. err = tmpErr
  1212. return
  1213. }
  1214. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1215. findDate = dataList[i].DataTime
  1216. break
  1217. }
  1218. }
  1219. case 3: // 固定日期
  1220. //最早的日期
  1221. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1222. if tmpErr != nil {
  1223. err = tmpErr
  1224. return
  1225. }
  1226. //寻找固定日期的数据
  1227. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  1228. if tmpErr != nil {
  1229. err = tmpErr
  1230. return
  1231. }
  1232. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1233. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1234. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1235. findDate = tmpDate
  1236. break
  1237. }
  1238. }
  1239. default:
  1240. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  1241. return
  1242. }
  1243. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1244. if maxDate.IsZero() {
  1245. maxDate = findDateTime
  1246. } else {
  1247. if findDateTime.After(maxDate) {
  1248. maxDate = findDateTime
  1249. }
  1250. }
  1251. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1252. tmpItem.XDate = findDate
  1253. tmpItem.XValue = tmpValue
  1254. } else {
  1255. continue
  1256. }
  1257. }
  1258. //Y轴的数据
  1259. {
  1260. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1261. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1262. if !ok {
  1263. continue
  1264. }
  1265. findDate := edbConf.YDate //需要的日期值
  1266. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1267. if len(dataList) <= 0 {
  1268. // 没有数据的指标id
  1269. //findDataList = append(findDataList, 0)
  1270. continue
  1271. }
  1272. tmpItem.YEdbInfoId = edbInfoId
  1273. tmpItem.YName = edbMappingInfo.EdbName
  1274. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1275. switch edbConf.YDateType {
  1276. case 1: //最新值
  1277. findDate = dataList[len(dataList)-1].DataTime
  1278. case 2: //近期几天
  1279. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1280. if tmpErr != nil {
  1281. err = tmpErr
  1282. return
  1283. }
  1284. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1285. lenData := len(dataList) - 1
  1286. for i := lenData; i >= 0; i-- {
  1287. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1288. if tmpErr != nil {
  1289. err = tmpErr
  1290. return
  1291. }
  1292. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1293. findDate = dataList[i].DataTime
  1294. break
  1295. }
  1296. }
  1297. case 3: // 固定日期
  1298. //最早的日期
  1299. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1300. if tmpErr != nil {
  1301. err = tmpErr
  1302. return
  1303. }
  1304. //寻找固定日期的数据
  1305. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1306. if tmpErr != nil {
  1307. err = tmpErr
  1308. return
  1309. }
  1310. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1311. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1312. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1313. findDate = tmpDate
  1314. break
  1315. }
  1316. }
  1317. default:
  1318. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1319. return
  1320. }
  1321. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1322. if maxDate.IsZero() {
  1323. maxDate = findDateTime
  1324. } else {
  1325. if findDateTime.After(maxDate) {
  1326. maxDate = findDateTime
  1327. }
  1328. }
  1329. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1330. tmpItem.YDate = findDate
  1331. tmpItem.YValue = tmpValue
  1332. } else {
  1333. continue
  1334. }
  1335. }
  1336. // 获取当前系列的X轴的最大最小值
  1337. {
  1338. if tmpItem.XValue < minXVal {
  1339. minXVal = tmpItem.XValue
  1340. }
  1341. if tmpItem.XValue > maxXVal {
  1342. maxXVal = tmpItem.XValue
  1343. }
  1344. if tmpItem.YValue < minYVal {
  1345. minYVal = tmpItem.YValue
  1346. }
  1347. if tmpItem.YValue > maxYVal {
  1348. maxYVal = tmpItem.YValue
  1349. }
  1350. }
  1351. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1352. }
  1353. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  1354. var trendLine, rSquare string
  1355. // 生成线性方程式
  1356. var a, b float64
  1357. {
  1358. coordinateData := make([]utils.Coordinate, 0)
  1359. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1360. tmpCoordinate1 := utils.Coordinate{
  1361. X: tmpSeriesEdbInfo.XValue,
  1362. Y: tmpSeriesEdbInfo.YValue,
  1363. }
  1364. coordinateData = append(coordinateData, tmpCoordinate1)
  1365. }
  1366. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  1367. if len(coordinateData) >= 2 {
  1368. a, b = utils.GetLinearResult(coordinateData)
  1369. if !math.IsNaN(a) && !math.IsNaN(b) {
  1370. if b > 0 {
  1371. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1372. } else {
  1373. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1374. }
  1375. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1376. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1377. }
  1378. // 计算R平方
  1379. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  1380. }
  1381. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  1382. X: minXVal,
  1383. Y: minYVal,
  1384. }, data_manage.CoordinatePoint{
  1385. X: maxXVal,
  1386. Y: maxYVal,
  1387. })
  1388. }
  1389. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  1390. Name: seriesItem.Name,
  1391. NameEn: seriesItem.NameEn,
  1392. Color: seriesItem.Color,
  1393. EdbInfoList: tmpSeriesEdbInfoList,
  1394. ShowTrendLine: seriesItem.ShowTrendLine,
  1395. ShowFitEquation: seriesItem.ShowFitEquation,
  1396. ShowRSquare: seriesItem.ShowRSquare,
  1397. TrendLine: trendLine,
  1398. RSquare: rSquare,
  1399. TrendLimitData: trendLimitData,
  1400. })
  1401. }
  1402. chartDataResp = data_manage.SectionScatterInfoResp{
  1403. XName: extraConfig.XName,
  1404. XNameEn: extraConfig.XNameEn,
  1405. XUnitName: extraConfig.XUnitName,
  1406. XUnitNameEn: extraConfig.XUnitNameEn,
  1407. YName: extraConfig.YName,
  1408. YNameEn: extraConfig.YNameEn,
  1409. YUnitName: extraConfig.YUnitName,
  1410. YUnitNameEn: extraConfig.YUnitNameEn,
  1411. XMinValue: extraConfig.XMinValue,
  1412. XMaxValue: extraConfig.XMaxValue,
  1413. YMinValue: extraConfig.YMinValue,
  1414. YMaxValue: extraConfig.YMaxValue,
  1415. DataList: dataListResp,
  1416. }
  1417. return
  1418. }
  1419. // AddChartInfo 添加图表
  1420. func AddChartInfo(req data_manage.AddChartInfoReq, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1421. isSendEmail = true // 默认错误的时候要发送邮件
  1422. req.ChartName = strings.Trim(req.ChartName, " ")
  1423. if req.ChartName == "" {
  1424. errMsg = "请填写图表名称!"
  1425. err = errors.New(errMsg)
  1426. isSendEmail = false
  1427. return
  1428. }
  1429. if req.ChartClassifyId <= 0 {
  1430. errMsg = "分类参数错误!"
  1431. err = errors.New(errMsg)
  1432. isSendEmail = false
  1433. return
  1434. }
  1435. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1436. if err != nil {
  1437. if err.Error() == utils.ErrNoRow() {
  1438. errMsg = "分类不存在"
  1439. err = errors.New(errMsg)
  1440. return
  1441. }
  1442. errMsg = "获取分类信息失败"
  1443. return
  1444. }
  1445. if chartClassify == nil {
  1446. errMsg = "分类不存在"
  1447. err = errors.New(errMsg)
  1448. return
  1449. }
  1450. chartType := req.ChartType
  1451. extraConfig := req.ExtraConfig
  1452. // 关联指标
  1453. var edbInfoIdArr []int
  1454. chartEdbInfoList := req.ChartEdbInfoList
  1455. if len(chartEdbInfoList) > 0 {
  1456. for _, v := range chartEdbInfoList {
  1457. edbInfoId := v.EdbInfoId
  1458. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1459. if tmpErr != nil {
  1460. if tmpErr.Error() == utils.ErrNoRow() {
  1461. errMsg = "指标不存在!"
  1462. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1463. return
  1464. } else {
  1465. errMsg = "获取指标信息失败!"
  1466. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1467. return
  1468. }
  1469. }
  1470. if edbInfo == nil {
  1471. errMsg = "指标已被删除,请重新选择!"
  1472. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1473. return
  1474. } else {
  1475. if edbInfo.EdbInfoId <= 0 {
  1476. errMsg = "指标已被删除,请重新选择!"
  1477. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1478. return
  1479. }
  1480. }
  1481. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1482. edbInfo.EdbNameSource = edbInfo.EdbName
  1483. //edbList = append(edbList, edbInfo)
  1484. //新增季节性图表数据计算
  1485. if chartType == 2 {
  1486. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  1487. if tmpErr != nil {
  1488. errMsg = "保存失败"
  1489. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  1490. return
  1491. }
  1492. if existCount <= 0 {
  1493. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  1494. }
  1495. }
  1496. }
  1497. } else {
  1498. if extraConfig == `` {
  1499. errMsg = "请选择指标!"
  1500. err = errors.New(errMsg)
  1501. return
  1502. }
  1503. tmpEdbInfoIdArr, tmpErr, tmpErrMsg := CheckChartExtraConfig(chartType, extraConfig)
  1504. if err != nil {
  1505. errMsg = tmpErrMsg
  1506. err = errors.New("添加失败:" + tmpErr.Error())
  1507. return
  1508. }
  1509. edbInfoIdArr = tmpEdbInfoIdArr
  1510. lenEdbInfoIdArr := len(edbInfoIdArr)
  1511. if lenEdbInfoIdArr > 0 {
  1512. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  1513. if tmpErr != nil {
  1514. errMsg = "指标异常!"
  1515. err = errors.New("指标异常!Err:" + tmpErr.Error())
  1516. return
  1517. }
  1518. if len(tmpEdbList) != lenEdbInfoIdArr {
  1519. errMsg = "指标异常!"
  1520. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  1521. return
  1522. }
  1523. for _, v := range tmpEdbList {
  1524. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  1525. EdbInfoId: v.EdbInfoId,
  1526. MaxData: 0,
  1527. MinData: 0,
  1528. IsOrder: false,
  1529. IsAxis: 1,
  1530. EdbInfoType: 1,
  1531. LeadValue: 0,
  1532. LeadUnit: "",
  1533. ChartStyle: "",
  1534. ChartColor: "",
  1535. PredictChartColor: "",
  1536. ChartWidth: 0,
  1537. Source: utils.CHART_SOURCE_DEFAULT,
  1538. })
  1539. }
  1540. }
  1541. }
  1542. if len(edbInfoIdArr) <= 0 {
  1543. errMsg = "请选择指标!"
  1544. err = errors.New(errMsg)
  1545. return
  1546. }
  1547. // 图表额外配置
  1548. extraConfig, err, errMsg = HandleExtraConfig(chartType, extraConfig)
  1549. if err != nil {
  1550. if errMsg == `` {
  1551. errMsg = "指标异常!"
  1552. }
  1553. err = errors.New("指标异常!Err:" + err.Error())
  1554. return
  1555. }
  1556. sort.Ints(edbInfoIdArr)
  1557. var edbInfoIdArrStr []string
  1558. for _, v := range edbInfoIdArr {
  1559. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1560. }
  1561. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1562. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  1563. //if err != nil && err.Error() != utils.ErrNoRow() {
  1564. // br.Msg = "保存失败"
  1565. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  1566. // return
  1567. //}
  1568. //if count > 0 {
  1569. // br.Msg = "所选指标已存在相同指标"
  1570. // return
  1571. //}
  1572. //判断图表是否存在
  1573. var condition string
  1574. var pars []interface{}
  1575. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1576. //condition += " AND chart_classify_id=? "
  1577. //pars = append(pars, req.ChartClassifyId)
  1578. condition += " AND chart_name=? AND source = ? "
  1579. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  1580. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1581. if err != nil {
  1582. errMsg = "判断图表名称是否存在失败"
  1583. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  1584. return
  1585. }
  1586. if count > 0 {
  1587. errMsg = "图表已存在,请重新填写"
  1588. err = errors.New("判断图表名称是否存在失败")
  1589. isSendEmail = false
  1590. return
  1591. }
  1592. barChartConf := `` //柱方图的配置信息
  1593. if chartType == 7 {
  1594. barChartConfReq := req.BarChartInfo
  1595. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  1596. errMsg = "请选择指标"
  1597. err = errors.New(errMsg)
  1598. isSendEmail = false
  1599. return
  1600. }
  1601. if len(barChartConfReq.DateList) <= 0 {
  1602. errMsg = "请选择日期"
  1603. err = errors.New(errMsg)
  1604. isSendEmail = false
  1605. return
  1606. }
  1607. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  1608. if tmpErr != nil {
  1609. errMsg = "柱方图信息异常"
  1610. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  1611. return
  1612. }
  1613. barChartConf = string(barChartConfByte)
  1614. }
  1615. disableVal := CheckIsDisableChart(edbInfoIdArr)
  1616. chartInfo = new(data_manage.ChartInfo)
  1617. chartInfo.ChartName = req.ChartName
  1618. chartInfo.EdbInfoIds = edbInfoIdStr
  1619. chartInfo.ChartClassifyId = req.ChartClassifyId
  1620. chartInfo.SysUserId = sysUserId
  1621. chartInfo.SysUserRealName = sysUserRealName
  1622. chartInfo.CreateTime = time.Now()
  1623. chartInfo.ModifyTime = time.Now()
  1624. chartInfo.IsSetName = 0
  1625. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1626. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  1627. if req.DateType > 0 {
  1628. chartInfo.DateType = req.DateType
  1629. } else {
  1630. chartInfo.DateType = 3
  1631. }
  1632. if chartType == 0 {
  1633. chartType = 1
  1634. }
  1635. chartInfo.ChartType = chartType
  1636. calendar := req.Calendar
  1637. if calendar == "" {
  1638. calendar = "公历"
  1639. }
  1640. chartInfo.Calendar = calendar
  1641. chartInfo.StartDate = req.StartDate
  1642. chartInfo.EndDate = req.EndDate
  1643. chartInfo.SeasonStartDate = req.SeasonStartDate
  1644. chartInfo.SeasonEndDate = req.SeasonEndDate
  1645. chartInfo.LeftMin = req.LeftMin
  1646. chartInfo.LeftMax = req.LeftMax
  1647. chartInfo.RightMin = req.RightMin
  1648. chartInfo.RightMax = req.RightMax
  1649. chartInfo.Disabled = disableVal
  1650. chartInfo.BarConfig = barChartConf
  1651. chartInfo.ExtraConfig = extraConfig
  1652. chartInfo.Source = utils.CHART_SOURCE_DEFAULT
  1653. newId, err := data_manage.AddChartInfo(chartInfo)
  1654. if err != nil {
  1655. errMsg = `保存失败`
  1656. err = errors.New("保存失败,Err:" + err.Error())
  1657. return
  1658. }
  1659. chartInfo.ChartInfoId = int(newId)
  1660. mapList := make([]*data_manage.ChartEdbMapping, 0)
  1661. for _, v := range chartEdbInfoList {
  1662. mapItem := new(data_manage.ChartEdbMapping)
  1663. mapItem.ChartInfoId = int(newId)
  1664. mapItem.EdbInfoId = v.EdbInfoId
  1665. mapItem.CreateTime = time.Now()
  1666. mapItem.ModifyTime = time.Now()
  1667. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1668. mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", v.EdbInfoId, "_", timestamp))
  1669. mapItem.MaxData = v.MaxData
  1670. mapItem.MinData = v.MinData
  1671. mapItem.IsOrder = v.IsOrder
  1672. mapItem.IsAxis = v.IsAxis
  1673. mapItem.EdbInfoType = v.EdbInfoType
  1674. mapItem.LeadValue = v.LeadValue
  1675. mapItem.LeadUnit = v.LeadUnit
  1676. mapItem.ChartStyle = v.ChartStyle
  1677. mapItem.ChartColor = v.ChartColor
  1678. mapItem.PredictChartColor = v.PredictChartColor
  1679. mapItem.ChartWidth = v.ChartWidth
  1680. mapItem.Source = utils.CHART_SOURCE_DEFAULT
  1681. mapList = append(mapList, mapItem)
  1682. }
  1683. err = data_manage.AddChartEdbMapping(mapList)
  1684. if err != nil {
  1685. errMsg = `保存失败`
  1686. err = errors.New("保存失败,Err:" + err.Error())
  1687. return
  1688. }
  1689. //添加es数据
  1690. go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1691. return
  1692. }
  1693. // EditChartInfo 编辑图表
  1694. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1695. isSendEmail = true // 默认错误的时候要发送邮件
  1696. req.ChartName = strings.Trim(req.ChartName, " ")
  1697. if req.ChartName == "" {
  1698. errMsg = "请填写图表名称!"
  1699. err = errors.New(errMsg)
  1700. isSendEmail = false
  1701. return
  1702. }
  1703. if req.ChartClassifyId <= 0 {
  1704. errMsg = "分类参数错误!"
  1705. err = errors.New(errMsg)
  1706. isSendEmail = false
  1707. return
  1708. }
  1709. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1710. if err != nil {
  1711. if err.Error() == utils.ErrNoRow() {
  1712. errMsg = "分类不存在"
  1713. err = errors.New(errMsg)
  1714. return
  1715. }
  1716. errMsg = "获取分类信息失败"
  1717. return
  1718. }
  1719. if chartClassify == nil {
  1720. errMsg = "分类不存在"
  1721. err = errors.New(errMsg)
  1722. return
  1723. }
  1724. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  1725. if err != nil {
  1726. if err.Error() == utils.ErrNoRow() {
  1727. errMsg = "图表已被删除,请刷新页面"
  1728. err = errors.New(errMsg)
  1729. isSendEmail = false
  1730. return
  1731. }
  1732. errMsg = "获取图表信息失败"
  1733. err = errors.New("获取图表信息失败,Err:" + err.Error())
  1734. return
  1735. }
  1736. // 图表关联指标id
  1737. chartEdbInfoList := make([]*data_manage.ChartSaveItem, 0)
  1738. // 关联指标
  1739. var edbInfoIdArr []int
  1740. // 非散点截面图的直接通过前端获取,散点截面图需要额外从配置中获取
  1741. if chartItem.ChartType != 10 {
  1742. //edbList := make([]*data_manage.EdbInfo, 0)
  1743. for _, v := range req.ChartEdbInfoList {
  1744. edbInfoId := v.EdbInfoId
  1745. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1746. if tmpErr != nil {
  1747. if tmpErr.Error() == utils.ErrNoRow() {
  1748. errMsg = "指标不存在!"
  1749. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1750. return
  1751. } else {
  1752. errMsg = "获取指标信息失败!"
  1753. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1754. return
  1755. }
  1756. }
  1757. if edbInfo == nil {
  1758. errMsg = "指标已被删除,请重新选择!"
  1759. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1760. return
  1761. } else {
  1762. if edbInfo.EdbInfoId <= 0 {
  1763. errMsg = "指标已被删除,请重新选择!"
  1764. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  1765. return
  1766. }
  1767. }
  1768. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1769. edbInfo.EdbNameSource = edbInfo.EdbName
  1770. //edbList = append(edbList, edbInfo)
  1771. //新增季节性图表数据计算
  1772. if req.ChartType == 2 {
  1773. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  1774. if tmpErr != nil {
  1775. errMsg = "保存失败"
  1776. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  1777. return
  1778. }
  1779. if existCount <= 0 {
  1780. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  1781. }
  1782. }
  1783. }
  1784. chartEdbInfoList = req.ChartEdbInfoList
  1785. } else {
  1786. if req.ExtraConfig == `` {
  1787. errMsg = "请选择指标!"
  1788. err = errors.New(errMsg)
  1789. isSendEmail = false
  1790. return
  1791. }
  1792. // 校验配置的指标列表
  1793. tmpEdbInfoIdArr, tmpErr, tmpErrMsg := CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
  1794. if tmpErr != nil {
  1795. errMsg = tmpErrMsg
  1796. err = errors.New("添加失败:" + tmpErr.Error())
  1797. return
  1798. }
  1799. edbInfoIdArr = tmpEdbInfoIdArr
  1800. lenEdbInfoIdArr := len(edbInfoIdArr)
  1801. if lenEdbInfoIdArr > 0 {
  1802. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  1803. if tmpErr != nil {
  1804. errMsg = "指标异常!"
  1805. err = errors.New("指标异常!Err:" + tmpErr.Error())
  1806. return
  1807. }
  1808. if len(tmpEdbList) != lenEdbInfoIdArr {
  1809. errMsg = "指标异常!"
  1810. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  1811. return
  1812. }
  1813. for _, v := range tmpEdbList {
  1814. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  1815. EdbInfoId: v.EdbInfoId,
  1816. MaxData: 0,
  1817. MinData: 0,
  1818. IsOrder: false,
  1819. IsAxis: 1,
  1820. EdbInfoType: 1,
  1821. LeadValue: 0,
  1822. LeadUnit: "",
  1823. ChartStyle: "",
  1824. ChartColor: "",
  1825. PredictChartColor: "",
  1826. ChartWidth: 0,
  1827. Source: utils.CHART_SOURCE_DEFAULT,
  1828. })
  1829. }
  1830. }
  1831. }
  1832. if len(edbInfoIdArr) <= 0 {
  1833. errMsg = "请选择指标!"
  1834. err = errors.New(errMsg)
  1835. isSendEmail = false
  1836. return
  1837. }
  1838. // 图表额外配置
  1839. extraConfig, err, errMsg := HandleExtraConfig(req.ChartType, req.ExtraConfig)
  1840. if err != nil {
  1841. if errMsg == `` {
  1842. errMsg = "指标异常!"
  1843. }
  1844. err = errors.New("指标异常!Err:" + err.Error())
  1845. return
  1846. }
  1847. req.ExtraConfig = extraConfig
  1848. //图表操作权限
  1849. ok := CheckOpChartPermission(sysUser, chartItem.SysUserId)
  1850. if !ok {
  1851. errMsg = "没有该图表的操作权限"
  1852. err = errors.New(errMsg)
  1853. isSendEmail = false
  1854. return
  1855. }
  1856. var calendar string
  1857. var dateType int
  1858. dateType = req.DateType
  1859. if req.ChartType != chartItem.ChartType {
  1860. if chartItem.ChartType != 2 && chartItem.Calendar == "" { //非季节性图
  1861. calendar = "公历"
  1862. }
  1863. if chartItem.ChartType == 2 && chartItem.DateType <= 0 {
  1864. dateType = 3
  1865. }
  1866. }
  1867. sort.Ints(edbInfoIdArr)
  1868. var edbInfoIdArrStr []string
  1869. for _, v := range edbInfoIdArr {
  1870. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1871. }
  1872. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1873. //mappingCondition := ` AND chart_info_id<> ` + strconv.Itoa(req.ChartInfoId)
  1874. //count, err := data_manage.ChartInfoExist(mappingCondition, edbInfoIdStr)
  1875. //if err != nil && err.Error() != utils.ErrNoRow() {
  1876. // br.Msg = "保存失败"
  1877. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  1878. // return
  1879. //}
  1880. //if count > 0 {
  1881. // br.Msg = "所选指标已存在相同指标"
  1882. // return
  1883. //}
  1884. //判断图表是否存在
  1885. var condition string
  1886. var pars []interface{}
  1887. condition += " AND chart_info_id<>? "
  1888. pars = append(pars, req.ChartInfoId)
  1889. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1890. //condition += " AND chart_classify_id=? "
  1891. //pars = append(pars, req.ChartClassifyId)
  1892. condition += " AND chart_name=? AND source = ? "
  1893. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  1894. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1895. if err != nil {
  1896. errMsg = "判断图表名称是否存在失败"
  1897. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  1898. return
  1899. }
  1900. if count > 0 {
  1901. errMsg = "图表已存在,请重新填写"
  1902. err = errors.New(errMsg)
  1903. isSendEmail = false
  1904. return
  1905. }
  1906. barChartConf := `` //柱方图的配置信息
  1907. if req.ChartType == 7 {
  1908. barChartConfReq := req.BarChartInfo
  1909. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  1910. errMsg = "请选择指标"
  1911. err = errors.New(errMsg)
  1912. isSendEmail = false
  1913. return
  1914. }
  1915. if len(barChartConfReq.DateList) <= 0 {
  1916. errMsg = "请选择日期"
  1917. err = errors.New(errMsg)
  1918. isSendEmail = false
  1919. return
  1920. }
  1921. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  1922. if tmpErr != nil {
  1923. errMsg = "柱方图信息异常"
  1924. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  1925. return
  1926. }
  1927. barChartConf = string(barChartConfByte)
  1928. }
  1929. // 图表启用与否
  1930. disableVal := CheckIsDisableChart(edbInfoIdArr)
  1931. err = data_manage.EditChartInfoAndMapping(&req, edbInfoIdStr, calendar, dateType, disableVal, barChartConf, chartEdbInfoList)
  1932. if err != nil {
  1933. errMsg = "保存失败"
  1934. err = errors.New("保存失败,Err:" + err.Error())
  1935. return
  1936. }
  1937. resp := new(data_manage.AddChartInfoResp)
  1938. resp.ChartInfoId = chartItem.ChartInfoId
  1939. resp.UniqueCode = chartItem.UniqueCode
  1940. resp.ChartType = req.ChartType
  1941. //添加es数据
  1942. go EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1943. //修改my eta es数据
  1944. go EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1945. return
  1946. }