chart_info.go 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/company"
  7. "eta/eta_api/models/data_manage"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/alarm_msg"
  10. "eta/eta_api/utils"
  11. "fmt"
  12. "github.com/shopspring/decimal"
  13. "math"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. func ChartInfoRefreshV1(chartInfoId int) (err error) {
  20. var errmsg string
  21. defer func() {
  22. if err != nil {
  23. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  24. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  25. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  26. }
  27. }()
  28. baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshData(chartInfoId)
  29. if err != nil {
  30. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  31. return
  32. }
  33. newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  34. baseMap := make(map[int]*data_manage.EdbInfo)
  35. for _, bv := range baseEdbInfoArr {
  36. // 如果不是普通指标,那么过滤
  37. if bv.EdbInfoType != 0 {
  38. continue
  39. }
  40. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  41. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  42. }
  43. baseMap[bv.EdbInfoId] = bv
  44. }
  45. fmt.Println("calculateInfoArr:", len(calculateInfoArr))
  46. newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
  47. calculateMap := make(map[int]*data_manage.EdbInfo)
  48. var calculateArr []int
  49. for _, bv := range calculateInfoArr {
  50. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  51. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  52. calculateArr = append(calculateArr, bv.EdbInfoId)
  53. }
  54. calculateMap[bv.EdbInfoId] = bv
  55. }
  56. sort.Ints(calculateArr)
  57. // 刷新相关基础指标
  58. for _, bv := range newBaseEdbInfoArr {
  59. err, errmsg = refreshBaseEdbInfo(bv)
  60. if err != nil {
  61. return
  62. }
  63. }
  64. // 刷新相关计算指标
  65. for _, v := range calculateArr {
  66. edbInfo := calculateMap[v]
  67. if edbInfo == nil {
  68. return err
  69. }
  70. err, errmsg = refreshCalculateEdbInfo(edbInfo)
  71. if err != nil {
  72. return
  73. }
  74. }
  75. return err
  76. }
  77. // BatchChartInfoRefresh 图表批量刷新
  78. func BatchChartInfoRefresh(chartInfoList []*data_manage.ChartInfo) (err error) {
  79. if len(chartInfoList) <= 0 {
  80. return
  81. }
  82. chartInfoIdSlice := make([]string, 0)
  83. for _, chartInfo := range chartInfoList {
  84. chartInfoIdSlice = append(chartInfoIdSlice, strconv.Itoa(chartInfo.ChartInfoId))
  85. }
  86. var errmsg string
  87. defer func() {
  88. if err != nil {
  89. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  90. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  91. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  92. }
  93. }()
  94. baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshDataByChartInfoIdSlice(chartInfoIdSlice)
  95. if err != nil {
  96. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  97. return
  98. }
  99. //基础指标去重
  100. newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  101. baseMap := make(map[int]*data_manage.EdbInfo)
  102. for _, bv := range baseEdbInfoArr {
  103. fmt.Println(bv.UniqueCode)
  104. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  105. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  106. }
  107. baseMap[bv.EdbInfoId] = bv
  108. }
  109. fmt.Println("calculateInfoArr:", len(calculateInfoArr))
  110. //计算指标去重
  111. newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
  112. calculateMap := make(map[int]*data_manage.EdbInfo)
  113. var calculateArr []int
  114. for _, bv := range calculateInfoArr {
  115. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  116. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  117. calculateArr = append(calculateArr, bv.EdbInfoId)
  118. }
  119. calculateMap[bv.EdbInfoId] = bv
  120. }
  121. sort.Ints(calculateArr)
  122. // 刷新相关基础指标
  123. for _, bv := range newBaseEdbInfoArr {
  124. err, errmsg = refreshBaseEdbInfo(bv)
  125. if err != nil {
  126. return
  127. }
  128. }
  129. // 刷新相关计算指标
  130. for _, v := range calculateArr {
  131. edbInfo := calculateMap[v]
  132. if edbInfo == nil {
  133. return err
  134. }
  135. err, errmsg = refreshCalculateEdbInfo(edbInfo)
  136. if err != nil {
  137. return
  138. }
  139. }
  140. return err
  141. }
  142. // refreshBaseEdbInfo 刷新基础指标
  143. func refreshBaseEdbInfo(bv *data_manage.EdbInfo) (err error, errMsg string) {
  144. source := bv.Source
  145. subSource := bv.SubSource
  146. edbInfoId := bv.EdbInfoId
  147. edbCode := bv.EdbCode
  148. //startDate := bv.StartDate
  149. frequency := bv.Frequency
  150. if bv.StartDate == "0000-00-00" {
  151. return
  152. }
  153. sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
  154. if err != nil {
  155. return
  156. }
  157. var limitDay int
  158. startDate := ""
  159. switch frequency {
  160. case "日度":
  161. limitDay = utils.DATA_START_REFRESH_LIMIT
  162. case "周度":
  163. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  164. case "月度":
  165. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  166. case "季度":
  167. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  168. case "年度":
  169. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  170. default:
  171. limitDay = utils.DATA_START_REFRESH_LIMIT
  172. }
  173. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  174. fmt.Println("source:", source)
  175. respItem, err := RefreshEdbData(edbInfoId, source, subSource, edbCode, startDate)
  176. if err != nil {
  177. errMsg = errors.New("RefreshEdbData Err:" + err.Error()).Error()
  178. return
  179. }
  180. if respItem.Ret != 200 {
  181. errMsg = errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  182. return
  183. }
  184. maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  185. if err != nil {
  186. return
  187. }
  188. if maxAndMinItem != nil {
  189. err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  190. if err != nil {
  191. return
  192. }
  193. }
  194. return
  195. }
  196. // refreshCalculateEdbInfo 刷新计算指标
  197. func refreshCalculateEdbInfo(edbInfo *data_manage.EdbInfo) (err error, errMsg string) {
  198. startDate := edbInfo.StartDate
  199. source := edbInfo.Source
  200. if source == utils.DATA_SOURCE_CALCULATE {
  201. startDate = edbInfo.StartDate
  202. sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
  203. if tmpErr != nil {
  204. err = tmpErr
  205. return
  206. }
  207. startDate = sTime.Format(utils.FormatDate)
  208. }
  209. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  210. if err != nil {
  211. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
  212. errMsg = "RefreshEdbCalculateData Err:" + err.Error()
  213. return
  214. }
  215. if result.Ret != 200 {
  216. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  217. errMsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  218. err = errors.New("刷新失败")
  219. return
  220. }
  221. return
  222. }
  223. type ChartInfoReq struct {
  224. ChartInfoId int `description:"图表id,新增时传0"`
  225. }
  226. // DeleteChartInfoDataRedis 清除图表缓存
  227. func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
  228. var req ChartInfoReq
  229. err = json.Unmarshal(bodyByte, &req)
  230. if err != nil {
  231. return
  232. }
  233. if req.ChartInfoId > 0 {
  234. err = utils.Rc.Delete(GetChartInfoDataKey(req.ChartInfoId))
  235. }
  236. return
  237. }
  238. // DeleteChartClassifyRedis 清除图表分类缓存
  239. func DeleteChartClassifyRedis(bodyByte []byte) (err error) {
  240. err = utils.Rc.Delete(utils.CACHE_CHART_CLASSIFY)
  241. return
  242. }
  243. // GetChartInfoDataKey 获取图表缓存的key
  244. func GetChartInfoDataKey(chartInfoId int) string {
  245. key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
  246. return key
  247. }
  248. // CheckOpChartPermission 判断图表操作权限
  249. func CheckOpChartPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  250. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  251. ok = true
  252. }
  253. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  254. if ok == false && createUserId == sysUser.AdminId {
  255. ok = true
  256. }
  257. // 如果图表权限id 是 1 ,那么允许编辑
  258. if ok == false && sysUser.ChartPermission == 1 {
  259. ok = true
  260. }
  261. return
  262. }
  263. // GetChartEdbData 获取图表的指标数据
  264. func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
  265. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  266. xEdbIdValue = make([]int, 0)
  267. yDataList = make([]data_manage.YData, 0)
  268. var extraConfig interface{}
  269. switch chartType {
  270. case 7: // 柱形图
  271. var barConfig data_manage.BarChartInfoReq
  272. if extraConfigStr == `` {
  273. errMsg = "柱方图未配置"
  274. err = errors.New(errMsg)
  275. return
  276. }
  277. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  278. if err != nil {
  279. errMsg = "柱方图配置异常"
  280. err = errors.New(errMsg)
  281. return
  282. }
  283. extraConfig = barConfig
  284. case 10: // 截面散点图
  285. var tmpExtraConfig data_manage.SectionScatterReq
  286. if extraConfigStr == `` {
  287. errMsg = "截面散点图未配置"
  288. err = errors.New(errMsg)
  289. return
  290. }
  291. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  292. if err != nil {
  293. errMsg = "截面散点配置异常"
  294. err = errors.New(errMsg)
  295. return
  296. }
  297. extraConfig = tmpExtraConfig
  298. case utils.CHART_TYPE_RADAR:
  299. var barConfig data_manage.RadarChartInfoReq
  300. if extraConfigStr == `` {
  301. errMsg = "雷达图未配置"
  302. err = errors.New(errMsg)
  303. return
  304. }
  305. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  306. if err != nil {
  307. errMsg = "雷达图配置异常"
  308. err = errors.New(errMsg)
  309. return
  310. }
  311. extraConfig = barConfig
  312. default:
  313. xEdbIdValue = make([]int, 0)
  314. yDataList = make([]data_manage.YData, 0)
  315. }
  316. // 指标对应的所有数据
  317. edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  318. if err != nil {
  319. return
  320. }
  321. // 特殊图形数据处理
  322. switch chartType {
  323. case 7: // 柱形图
  324. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  325. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  326. for k := range yDataList {
  327. yDataList[k].Unit = barChartConf.Unit
  328. yDataList[k].UnitEn = barChartConf.UnitEn
  329. }
  330. for _, v := range edbList {
  331. // 指标别名
  332. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  333. for _, reqEdb := range barChartConf.EdbInfoIdList {
  334. if v.EdbInfoId == reqEdb.EdbInfoId {
  335. v.EdbAliasName = reqEdb.Name
  336. }
  337. }
  338. }
  339. }
  340. case 10: // 截面散点图
  341. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  342. xEdbIdValue, dataResp, err = GetSectionScatterChartData(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
  343. var tmpExtraConfig data_manage.SectionScatterReq
  344. if extraConfigStr == `` {
  345. errMsg = "截面散点图未配置"
  346. err = errors.New(errMsg)
  347. return
  348. }
  349. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  350. if err != nil {
  351. errMsg = "截面散点配置异常"
  352. err = errors.New(errMsg)
  353. return
  354. }
  355. // 这个数据没有必要返回给前端
  356. for _, v := range edbList {
  357. v.DataList = nil
  358. }
  359. case utils.CHART_TYPE_RADAR: //雷达图
  360. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  361. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  362. }
  363. return
  364. }
  365. // GetEdbDataMapList 获取指标最后的基础数据
  366. func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtra string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  367. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtra)
  368. return
  369. }
  370. // getEdbDataMapList 获取指标最后的基础数据
  371. func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  372. // 指标对应的所有数据
  373. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  374. for _, v := range mappingList {
  375. //fmt.Println("v:", v.EdbInfoId)
  376. item := new(data_manage.ChartEdbInfoMapping)
  377. item.EdbInfoId = v.EdbInfoId
  378. item.SourceName = v.SourceName
  379. item.Source = v.Source
  380. item.EdbCode = v.EdbCode
  381. item.EdbName = v.EdbName
  382. item.EdbNameEn = v.EdbNameEn
  383. item.Frequency = v.Frequency
  384. item.EdbType = v.EdbType
  385. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  386. if v.Unit != `无` {
  387. item.Unit = v.Unit
  388. }
  389. item.UnitEn = v.UnitEn
  390. item.StartDate = v.StartDate
  391. item.EndDate = v.EndDate
  392. item.ModifyTime = v.ModifyTime
  393. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  394. item.PredictChartColor = v.PredictChartColor
  395. item.ClassifyId = v.ClassifyId
  396. if chartInfoId <= 0 {
  397. item.IsAxis = 1
  398. item.LeadValue = 0
  399. item.LeadUnit = ""
  400. item.ChartEdbMappingId = 0
  401. item.ChartInfoId = 0
  402. item.IsOrder = false
  403. item.EdbInfoType = 1
  404. item.ChartStyle = ""
  405. item.ChartColor = ""
  406. item.ChartWidth = 0
  407. item.MaxData = v.MaxValue
  408. item.MinData = v.MinValue
  409. } else {
  410. item.IsAxis = v.IsAxis
  411. item.EdbInfoType = v.EdbInfoType
  412. item.LeadValue = v.LeadValue
  413. item.LeadUnit = v.LeadUnit
  414. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  415. item.ChartEdbMappingId = v.ChartEdbMappingId
  416. item.ChartInfoId = v.ChartInfoId
  417. item.ChartStyle = v.ChartStyle
  418. item.ChartColor = v.ChartColor
  419. item.ChartWidth = v.ChartWidth
  420. item.IsOrder = v.IsOrder
  421. item.MaxData = v.MaxData
  422. item.MinData = v.MinData
  423. }
  424. item.LatestValue = v.LatestValue
  425. item.LatestDate = v.LatestDate
  426. item.UniqueCode = v.UniqueCode
  427. item.MoveLatestDate = v.LatestDate
  428. item.EdbAliasName = v.EdbAliasName
  429. item.IsConvert = v.IsConvert
  430. item.ConvertType = v.ConvertType
  431. item.ConvertValue = v.ConvertValue
  432. item.ConvertUnit = v.ConvertUnit
  433. item.ConvertEnUnit = v.ConvertEnUnit
  434. var startDateReal string
  435. var diffSeconds int64
  436. if chartType == 2 { //季节性图
  437. startDateReal = startDate
  438. } else {
  439. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  440. var startTimeRealTemp time.Time
  441. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  442. switch v.LeadUnit {
  443. case "天":
  444. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  445. case "月":
  446. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  447. case "季":
  448. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  449. case "周":
  450. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  451. case "年":
  452. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  453. }
  454. if startTimeRealTemp.Before(startDateParse) {
  455. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  456. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  457. } else {
  458. startDateReal = startDate
  459. diffSeconds = 0
  460. }
  461. // 预测指标的开始日期也要偏移
  462. {
  463. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  464. if tmpErr != nil {
  465. err = tmpErr
  466. return
  467. }
  468. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  469. if tmpErr != nil {
  470. err = tmpErr
  471. return
  472. }
  473. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  474. }
  475. } else {
  476. startDateReal = startDate
  477. }
  478. }
  479. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  480. calendarPreYear := 0
  481. if calendar == "农历" {
  482. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  483. if err != nil {
  484. fmt.Println("time.Parse:" + err.Error())
  485. }
  486. calendarPreYear = newStartDateReal.Year() - 1
  487. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  488. startDateReal = newStartDateReal.Format(utils.FormatDate)
  489. }
  490. dataList := make([]*data_manage.EdbDataList, 0)
  491. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  492. //fmt.Println("calendarPreYear:", calendarPreYear)
  493. //var newEdbInfo *data_manage.EdbInfo
  494. switch v.EdbInfoCategoryType {
  495. case 0:
  496. dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  497. case 1:
  498. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
  499. default:
  500. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  501. }
  502. if err != nil {
  503. return
  504. }
  505. if v.IsConvert == 1 {
  506. switch v.ConvertType {
  507. case 1:
  508. for i, data := range dataList {
  509. dataList[i].Value = data.Value * v.ConvertValue
  510. }
  511. //item.MaxData = item.MaxData * v.ConvertValue
  512. //item.MinData = item.MinData * v.ConvertValue
  513. case 2:
  514. for i, data := range dataList {
  515. dataList[i].Value = data.Value / v.ConvertValue
  516. }
  517. //item.MaxData = item.MaxData / v.ConvertValue
  518. //item.MinData = item.MinData / v.ConvertValue
  519. case 3:
  520. for i, data := range dataList {
  521. if data.Value <= 0 {
  522. err = errors.New("数据中含有负数或0,无法对数运算")
  523. return
  524. }
  525. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  526. }
  527. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  528. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  529. }
  530. }
  531. edbDataListMap[v.EdbInfoId] = dataList
  532. if diffSeconds != 0 && v.EdbInfoType == 0 {
  533. dataListLen := len(dataList)
  534. for i := 0; i < dataListLen; i++ {
  535. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  536. }
  537. }
  538. if chartType == 2 {
  539. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  540. if tmpErr != nil {
  541. //item.DataList = dataList
  542. item.IsNullData = true
  543. edbList = append(edbList, item)
  544. continue
  545. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  546. return
  547. }
  548. if calendar == "农历" {
  549. if len(dataList) <= 0 {
  550. result := new(data_manage.EdbDataResult)
  551. item.DataList = result
  552. } else {
  553. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  554. if tmpErr != nil {
  555. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  556. return
  557. }
  558. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  559. if tErr != nil {
  560. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  561. return
  562. }
  563. item.DataList = quarterDataList
  564. }
  565. } else {
  566. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  567. if tErr != nil {
  568. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  569. return
  570. }
  571. item.DataList = quarterDataList
  572. }
  573. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  574. //item.DataList = dataList
  575. } else {
  576. item.DataList = dataList
  577. }
  578. edbList = append(edbList, item)
  579. }
  580. return
  581. }
  582. // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
  583. func GetSeasonEdbInfoDataListByXDate(dataList []*data_manage.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort data_manage.QuarterDataList, err error) {
  584. xStartDate := "01-01"
  585. xEndDate := "12-31"
  586. jumpYear := 0
  587. legends := make([]data_manage.SeasonChartLegend, 0)
  588. var seasonExtra data_manage.SeasonExtraItem
  589. if seasonExtraConfig != "" {
  590. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  591. if err != nil {
  592. return
  593. }
  594. }
  595. if seasonExtra.XStartDate != "" {
  596. xStartDate = seasonExtra.XStartDate
  597. xEndDate = seasonExtra.XEndDate
  598. jumpYear = seasonExtra.JumpYear
  599. legends = seasonExtra.ChartLegend
  600. }
  601. length := len(dataList)
  602. if length == 0 {
  603. return
  604. }
  605. legendMap := make(map[string]string, 0)
  606. if len(legends) > 0 {
  607. for _, v := range legends {
  608. legendMap[v.Name] = v.Value
  609. }
  610. }
  611. latestDateStr := latestDate.Format(utils.FormatDate)
  612. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  613. //根据数据确定最早的年份,和最近年份
  614. //根据横轴的日期,汇总所有的年份
  615. startDate := dataList[0].DataTime
  616. startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
  617. if tmpErr != nil {
  618. err = tmpErr
  619. return
  620. }
  621. startYear := startDateT.Year()
  622. //获取数据的最新日期
  623. lastDate := dataList[length-1].DataTime
  624. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  625. if tmpErr != nil {
  626. err = tmpErr
  627. return
  628. }
  629. endYear := lastDateT.Year()
  630. nowYear := time.Now().Year()
  631. dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
  632. quarterDataList := make([]*data_manage.QuarterData, 0)
  633. quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
  634. //整理出日期
  635. idx := 1
  636. chartLegendMap := make(map[string]int, 0)
  637. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  638. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  639. currentEndYear := currentStartYear
  640. if jumpYear == 1 {
  641. currentEndYear = currentStartYear + 1
  642. }
  643. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  644. name := fmt.Sprintf("%s_%s", startStr, endStr)
  645. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  646. startT, tEr := time.Parse(utils.FormatDate, startStr)
  647. if tEr != nil {
  648. err = tEr
  649. return
  650. }
  651. endT, tEr := time.Parse(utils.FormatDate, endStr)
  652. if tEr != nil {
  653. err = tEr
  654. return
  655. }
  656. if lastDateT.Before(startT) {
  657. //如果最新的日期在起始日之前,则跳出循环
  658. break
  659. }
  660. if endT.Year() > nowYear {
  661. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  662. nowYear = endT.Year()
  663. }
  664. item := data_manage.QuarterXDateItem{
  665. StartDate: startT,
  666. EndDate: endT,
  667. ShowName: showName,
  668. }
  669. dataMap[name] = item
  670. chartLegendMap[name] = idx
  671. idx++
  672. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  673. if lastDateT.Before(endT) {
  674. //如果最新的日期在起始日之前,则跳出循环
  675. break
  676. }
  677. }
  678. lenYear := len(dataMap)
  679. for k, v := range dataMap {
  680. if i, ok := chartLegendMap[k]; ok {
  681. v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  682. }
  683. dataMap[k] = v
  684. }
  685. for _, v := range dataList {
  686. dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
  687. year := dataTimeT.Year()
  688. newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
  689. for k, dateItem := range dataMap {
  690. tmpVal := data_manage.EdbDataList{
  691. EdbDataId: v.EdbDataId,
  692. EdbInfoId: v.EdbInfoId,
  693. DataTime: v.DataTime,
  694. DataTimestamp: v.DataTimestamp,
  695. Value: v.Value,
  696. }
  697. if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
  698. if jumpYear == 1 {
  699. //计算前一年最大的日期, 只补齐数据到去年
  700. beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
  701. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  702. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  703. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  704. } else {
  705. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  706. }
  707. } else {
  708. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  709. }
  710. timestamp := newItemDate.UnixNano() / 1e6
  711. tmpVal.DataTimestamp = timestamp
  712. tmpV := &tmpVal
  713. if findVal, ok := quarterMap[k]; !ok {
  714. findVal = append(findVal, tmpV)
  715. quarterMap[k] = findVal
  716. } else {
  717. findVal = append(findVal, tmpV)
  718. quarterMap[k] = findVal
  719. }
  720. if v.DataTime == latestDateStr {
  721. dateItem.CuttingDataTimestamp = timestamp
  722. dataMap[k] = dateItem
  723. }
  724. //break
  725. }
  726. }
  727. }
  728. for k, v := range dataMap {
  729. itemList := quarterMap[k]
  730. quarterItem := new(data_manage.QuarterData)
  731. quarterItem.Years = v.ShowName
  732. quarterItem.ChartLegend = v.ChartLegend
  733. if le, ok := legendMap[v.ShowName]; ok {
  734. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  735. quarterItem.ChartLegend = le
  736. }
  737. }
  738. quarterItem.DataList = itemList
  739. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  740. //如果等于最后的实际日期,那么将切割时间戳记录
  741. if quarterItem.CuttingDataTimestamp == 0 {
  742. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  743. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  744. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  745. }
  746. }
  747. quarterDataList = append(quarterDataList, quarterItem)
  748. }
  749. if len(quarterDataList) > 0 {
  750. quarterDataListSort = quarterDataList
  751. sort.Sort(quarterDataListSort)
  752. }
  753. return
  754. }
  755. // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
  756. func GetSeasonEdbInfoDataListByXDateNong(result *data_manage.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort data_manage.QuarterDataList, err error) {
  757. xStartDate := "01-01"
  758. xEndDate := "12-31"
  759. jumpYear := 0
  760. legends := make([]data_manage.SeasonChartLegend, 0)
  761. var seasonExtra data_manage.SeasonExtraItem
  762. if seasonExtraConfig != "" {
  763. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  764. if err != nil {
  765. return
  766. }
  767. }
  768. if seasonExtra.XStartDate != "" {
  769. xStartDate = seasonExtra.XStartDate
  770. xEndDate = seasonExtra.XEndDate
  771. jumpYear = seasonExtra.JumpYear
  772. legends = seasonExtra.ChartLegend
  773. }
  774. length := len(result.List)
  775. if length == 0 {
  776. return
  777. }
  778. legendMap := make(map[string]string, 0)
  779. if len(legends) > 0 {
  780. for _, v := range legends {
  781. legendMap[v.Name] = v.Value
  782. }
  783. }
  784. latestDateYear := latestDate.Year()
  785. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  786. //根据数据确定最早的年份,和最近年份
  787. //根据横轴的日期,汇总所有的年份
  788. startYear := result.List[0].Year
  789. /*if jumpYear == 1 {
  790. if startYear != calendarPreYear {
  791. startYear = startYear - 1
  792. }
  793. }*/
  794. itemLength := len(result.List[length-1].Items)
  795. //获取数据的最新日期
  796. lastDate := result.List[length-1].Items[itemLength-1].DataTime
  797. maxY := result.List[length-1].Year
  798. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  799. if tmpErr != nil {
  800. err = tmpErr
  801. return
  802. }
  803. endYear := lastDateT.Year()
  804. nowYear := time.Now().Year()
  805. dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
  806. quarterDataList := make([]*data_manage.QuarterData, 0)
  807. resultData := make([]*data_manage.QuarterData, 0)
  808. quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
  809. //整理出日期
  810. var startTmpT, endTmpT time.Time
  811. idx := 1
  812. chartLegendMap := make(map[string]int, 0)
  813. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  814. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  815. currentEndYear := currentStartYear
  816. if jumpYear == 1 {
  817. currentEndYear = currentStartYear + 1
  818. }
  819. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  820. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  821. startT, tEr := time.Parse(utils.FormatDate, startStr)
  822. if tEr != nil {
  823. err = tEr
  824. return
  825. }
  826. endT, tEr := time.Parse(utils.FormatDate, endStr)
  827. if tEr != nil {
  828. err = tEr
  829. return
  830. }
  831. if lastDateT.Before(startT) {
  832. //如果最新的日期在起始日之前,则跳出循环
  833. break
  834. }
  835. if endT.Year() > nowYear {
  836. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  837. nowYear = endT.Year()
  838. }
  839. item := data_manage.QuarterXDateItem{
  840. StartDate: startT,
  841. EndDate: endT,
  842. ShowName: showName,
  843. }
  844. dataMap[showName] = item
  845. fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
  846. startTmpT = startT
  847. endTmpT = endT
  848. chartLegendMap[showName] = idx
  849. idx++
  850. if lastDateT.Before(endT) {
  851. //如果最新的日期在起始日之前,则跳出循环
  852. break
  853. }
  854. }
  855. lenYear := len(dataMap)
  856. for k, v := range dataMap {
  857. if i, ok := chartLegendMap[k]; ok {
  858. v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  859. }
  860. dataMap[k] = v
  861. }
  862. yearDataListMap := make(map[int]*data_manage.EdbDataItems, 0)
  863. for _, lv := range result.List {
  864. yearDataListMap[lv.Year] = lv
  865. }
  866. //判断哪些点应该落在同一条时间线上
  867. /*maxY := lastDateT.Year()
  868. changeFlag := false
  869. if lastDateT.Month() >= 11 {
  870. maxY = maxY + 1
  871. }
  872. if maxY < nowYear {
  873. changeFlag = true
  874. maxY = nowYear
  875. }*/
  876. /*endTmp := fmt.Sprintf("%d-%s", maxY, xEndDate)
  877. endTmpT, _ := time.Parse(utils.FormatDate, endTmp)
  878. minY := maxY
  879. if jumpYear == 1 {
  880. minY = maxY - 1
  881. }
  882. startTmp := fmt.Sprintf("%d-%s", minY, xStartDate)
  883. startTmpT, _ := time.Parse(utils.FormatDate, startTmp)*/
  884. fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
  885. fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
  886. fmt.Printf("maxY 为%d \n", maxY)
  887. for name, dateItem := range dataMap {
  888. tY := dateItem.EndDate.Year()
  889. if lastDateT.Month() >= 11 {
  890. if maxY > endTmpT.Year() {
  891. tY = tY + 1
  892. }
  893. }
  894. lv, ok1 := yearDataListMap[tY]
  895. fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
  896. if !ok1 {
  897. continue
  898. }
  899. for _, item := range lv.Items {
  900. tmpVal := data_manage.EdbDataList{
  901. EdbDataId: item.EdbDataId,
  902. EdbInfoId: item.EdbInfoId,
  903. DataTime: item.DataTime,
  904. DataTimestamp: item.DataTimestamp,
  905. Value: item.Value,
  906. }
  907. dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
  908. if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
  909. tmpV := &tmpVal
  910. if findVal, ok := quarterMap[name]; !ok {
  911. findVal = append(findVal, tmpV)
  912. quarterMap[name] = findVal
  913. } else {
  914. findVal = append(findVal, tmpV)
  915. quarterMap[name] = findVal
  916. }
  917. if lv.Year >= latestDateYear {
  918. // 切割的日期时间字符串
  919. cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
  920. if item.DataTime == cuttingDataTimeStr {
  921. dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
  922. dataMap[name] = dateItem
  923. }
  924. }
  925. }
  926. }
  927. }
  928. for k, v := range dataMap {
  929. itemList := quarterMap[k]
  930. quarterItem := new(data_manage.QuarterData)
  931. quarterItem.Years = v.ShowName
  932. quarterItem.ChartLegend = v.ChartLegend
  933. if le, ok := legendMap[v.ShowName]; ok {
  934. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  935. quarterItem.ChartLegend = le
  936. }
  937. }
  938. quarterItem.DataList = itemList
  939. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  940. //如果等于最后的实际日期,那么将切割时间戳记录
  941. if quarterItem.CuttingDataTimestamp == 0 {
  942. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  943. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  944. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  945. }
  946. }
  947. quarterDataList = append(quarterDataList, quarterItem)
  948. }
  949. if result.List[0].Year != calendarPreYear {
  950. itemList := make([]*data_manage.EdbDataList, 0)
  951. items := new(data_manage.QuarterData)
  952. //items.Year = calendarPreYear
  953. items.DataList = itemList
  954. newResult := make([]*data_manage.QuarterData, 0)
  955. newResult = append(newResult, items)
  956. newResult = append(newResult, quarterDataList...)
  957. resultData = newResult
  958. } else {
  959. resultData = quarterDataList
  960. }
  961. if len(quarterDataList) > 0 {
  962. quarterDataListSort = resultData
  963. sort.Sort(quarterDataListSort)
  964. }
  965. return
  966. }
  967. // BarChartData 柱方图的数据处理
  968. 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) {
  969. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  970. edbDataMap := make(map[int]map[string]float64)
  971. for edbInfoId, edbDataList := range edbDataListMap {
  972. edbDateData := make(map[string]float64)
  973. for _, edbData := range edbDataList {
  974. edbDateData[edbData.DataTime] = edbData.Value
  975. }
  976. edbDataMap[edbInfoId] = edbDateData
  977. }
  978. // edbIdList 指标展示顺序;x轴的指标顺序
  979. edbIdList = make([]int, 0)
  980. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  981. dateData := make(map[int]float64)
  982. if barChartInfoSort.Sort == 0 {
  983. for _, v := range mappingList {
  984. edbIdList = append(edbIdList, v.EdbInfoId)
  985. }
  986. } else {
  987. lenBarChartInfoDateList := len(barChartInfoDateList)
  988. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  989. err = errors.New("排序日期异常")
  990. return
  991. }
  992. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  993. // 日期配置
  994. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  995. for edbInfoId, dataList := range edbDataListMap {
  996. if len(dataList) <= 0 {
  997. // 没有数据的指标id
  998. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  999. continue
  1000. }
  1001. findDate := barChartInfoDate.Date
  1002. switch barChartInfoDate.Type {
  1003. case 1: //最新值
  1004. findDate = dataList[len(dataList)-1].DataTime
  1005. case 2: //近期几天
  1006. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1007. if tmpErr != nil {
  1008. err = tmpErr
  1009. return
  1010. }
  1011. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1012. lenData := len(dataList) - 1
  1013. for i := lenData; i >= 0; i-- {
  1014. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1015. if tmpErr != nil {
  1016. err = tmpErr
  1017. return
  1018. }
  1019. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1020. findDate = dataList[i].DataTime
  1021. break
  1022. }
  1023. }
  1024. case 3: // 固定日期
  1025. //最早的日期
  1026. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1027. if tmpErr != nil {
  1028. err = tmpErr
  1029. return
  1030. }
  1031. //寻找固定日期的数据
  1032. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1033. if tmpErr != nil {
  1034. err = tmpErr
  1035. return
  1036. }
  1037. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1038. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1039. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1040. findDate = tmpDate
  1041. break
  1042. }
  1043. }
  1044. default:
  1045. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1046. return
  1047. }
  1048. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1049. dateData[edbInfoId] = tmpValue
  1050. } else {
  1051. // 没有数据的指标id
  1052. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1053. }
  1054. }
  1055. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1056. // 排序
  1057. dateDataSort := utils.NewMapSorter(dateData)
  1058. sort.Sort(dateDataSort)
  1059. if barChartInfoSort.Sort == 1 {
  1060. // 先将没有数据的指标id放在最前面
  1061. if len(notDataEdbIdList) > 0 {
  1062. edbIdList = append(edbIdList, notDataEdbIdList...)
  1063. }
  1064. for _, v := range dateDataSort {
  1065. edbIdList = append(edbIdList, v.Key)
  1066. }
  1067. } else {
  1068. for i := len(dateDataSort) - 1; i >= 0; i-- {
  1069. edbIdList = append(edbIdList, dateDataSort[i].Key)
  1070. }
  1071. // 再将没有数据的指标id放在最后面
  1072. if len(notDataEdbIdList) > 0 {
  1073. edbIdList = append(edbIdList, notDataEdbIdList...)
  1074. }
  1075. }
  1076. }
  1077. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  1078. for _, barChartInfoDate := range barChartInfoDateList {
  1079. var maxDate time.Time
  1080. findDataList := make([]float64, 0) // 当前日期的数据值
  1081. for _, edbInfoId := range edbIdList {
  1082. findDate := barChartInfoDate.Date //需要的日期值
  1083. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1084. if len(dataList) <= 0 {
  1085. // 没有数据的指标id
  1086. findDataList = append(findDataList, 0)
  1087. continue
  1088. }
  1089. switch barChartInfoDate.Type {
  1090. case 1: //最新值
  1091. dataList := edbDataListMap[edbInfoId]
  1092. findDate = dataList[len(dataList)-1].DataTime
  1093. case 2: //近期几天
  1094. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1095. if tmpErr != nil {
  1096. err = tmpErr
  1097. return
  1098. }
  1099. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1100. lenData := len(dataList) - 1
  1101. for i := lenData; i >= 0; i-- {
  1102. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1103. if tmpErr != nil {
  1104. err = tmpErr
  1105. return
  1106. }
  1107. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1108. findDate = dataList[i].DataTime
  1109. break
  1110. }
  1111. }
  1112. case 3: // 固定日期
  1113. //最早的日期
  1114. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1115. if tmpErr != nil {
  1116. err = tmpErr
  1117. return
  1118. }
  1119. //寻找固定日期的数据
  1120. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1121. if tmpErr != nil {
  1122. err = tmpErr
  1123. return
  1124. }
  1125. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1126. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1127. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1128. findDate = tmpDate
  1129. break
  1130. }
  1131. }
  1132. default:
  1133. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1134. return
  1135. }
  1136. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1137. if maxDate.IsZero() {
  1138. maxDate = findDateTime
  1139. } else {
  1140. if findDateTime.After(maxDate) {
  1141. maxDate = findDateTime
  1142. }
  1143. }
  1144. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1145. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  1146. findDataList = append(findDataList, tmpValue)
  1147. } else {
  1148. findDataList = append(findDataList, 0)
  1149. }
  1150. }
  1151. yDate := "0000-00-00"
  1152. if !maxDate.IsZero() {
  1153. yDate = maxDate.Format(utils.FormatDate)
  1154. }
  1155. yDataList = append(yDataList, data_manage.YData{
  1156. Date: yDate,
  1157. Value: findDataList,
  1158. Color: barChartInfoDate.Color,
  1159. Name: barChartInfoDate.Name,
  1160. })
  1161. }
  1162. return
  1163. }
  1164. // ChartInfoRefreshV2 图表刷新
  1165. // @author Roc
  1166. // @datetime 2022-09-16 10:15:38
  1167. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1168. func ChartInfoRefreshV2(chartInfoId int) (err error, isAsync bool) {
  1169. var errmsg string
  1170. defer func() {
  1171. if err != nil {
  1172. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1173. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1174. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1175. }
  1176. }()
  1177. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1178. if err != nil {
  1179. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1180. return
  1181. }
  1182. edbIdList := make([]int, 0)
  1183. for _, v := range edbMappingList {
  1184. edbIdList = append(edbIdList, v.EdbInfoId)
  1185. }
  1186. // 批量刷新
  1187. err, isAsync = EdbInfoRefreshAllFromBaseV3(edbIdList, false, false, false)
  1188. if err != nil {
  1189. return
  1190. }
  1191. return
  1192. }
  1193. // BatchChartInfoRefreshV2 图表批量刷新
  1194. // @author Roc
  1195. // @datetime 2022-09-16 10:15:38
  1196. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1197. func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo, redisKey string) (err error, isAsync bool) {
  1198. if len(chartInfoList) <= 0 {
  1199. return
  1200. }
  1201. chartInfoIdSlice := make([]int, 0)
  1202. for _, chartInfo := range chartInfoList {
  1203. chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
  1204. }
  1205. var errMsg string
  1206. defer func() {
  1207. if err != nil {
  1208. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errMsg, 3)
  1209. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1210. fmt.Println("ChartInfoRefresh Err:" + errMsg)
  1211. }
  1212. }()
  1213. // 是否删除缓存
  1214. isDeleteCache := true
  1215. if redisKey != `` {
  1216. //设置最多10分钟缓存
  1217. utils.Rc.SetNX(redisKey, 1, time.Minute*10)
  1218. defer func() {
  1219. if isDeleteCache {
  1220. utils.Rc.Delete(redisKey)
  1221. }
  1222. }()
  1223. }
  1224. // 获取需要刷新的指标信息
  1225. edbIdList := make([]int, 0)
  1226. edbInfoIdMap := make(map[int]int)
  1227. edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
  1228. for _, v := range edbMappingList {
  1229. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  1230. edbIdList = append(edbIdList, v.EdbInfoId)
  1231. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  1232. }
  1233. }
  1234. // 获取需要刷新的指标列表
  1235. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbIdList)
  1236. // 需要刷新的指标数量
  1237. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  1238. if totalEdbInfo > 20 { // 关联指标过多的时候,异步刷新
  1239. isAsync = true
  1240. isDeleteCache = false // 不删除缓存
  1241. go refreshChartEdbInfo(chartInfoList, redisKey, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1242. } else {
  1243. err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1244. //清除图表缓存
  1245. for _, v := range chartInfoList {
  1246. key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
  1247. _ = utils.Rc.Delete(key)
  1248. }
  1249. }
  1250. return
  1251. }
  1252. // refreshChartEdbInfo
  1253. // @Description: 刷新图表中的质保列表
  1254. // @author: Roc
  1255. // @datetime 2023-12-01 16:05:39
  1256. // @param chartInfoList []*data_manage.ChartInfo
  1257. // @param redisKey string
  1258. // @param newBaseEdbInfoArr []*data_manage.EdbInfo
  1259. // @param newBasePredictEdbInfoArr []*data_manage.EdbInfo
  1260. // @param newCalculateMap map[int]*data_manage.EdbInfo
  1261. // @param newPredictCalculateMap map[int]*data_manage.EdbInfo
  1262. // @param calculateArr []int
  1263. // @param predictCalculateArr []int
  1264. func refreshChartEdbInfo(chartInfoList []*data_manage.ChartInfo, redisKey string, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
  1265. edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1266. //清除图表缓存
  1267. for _, v := range chartInfoList {
  1268. key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
  1269. _ = utils.Rc.Delete(key)
  1270. }
  1271. // 如果是有插入redisKey,那么刷新结束后就清除缓存
  1272. if redisKey != `` {
  1273. utils.Rc.Delete(redisKey)
  1274. }
  1275. }
  1276. // GetBatchChartRefreshKey
  1277. // @Description: 获取批量刷新key
  1278. // @author: Roc
  1279. // @datetime 2023-11-30 13:30:26
  1280. // @param source string
  1281. // @param reportId int
  1282. // @param reportChapterId int
  1283. // @return string
  1284. func GetBatchChartRefreshKey(source string, reportId, reportChapterId int) string {
  1285. if source == `` {
  1286. return ``
  1287. }
  1288. return fmt.Sprint("batch_chart_refresh:", source, ":", reportId, ":", reportChapterId)
  1289. }
  1290. // CheckBatchChartRefreshResult
  1291. // @Description: 判断报告中的图表是否刷新完成
  1292. // @author: Roc
  1293. // @datetime 2023-11-30 13:30:26
  1294. // @param source string
  1295. // @param reportId int
  1296. // @param reportChapterId int
  1297. // @return string
  1298. func CheckBatchChartRefreshResult(source string, reportId, reportChapterId int) (refreshResult bool) {
  1299. refreshResult = true
  1300. redisKey := GetBatchChartRefreshKey(source, reportId, reportChapterId)
  1301. if redisKey != `` {
  1302. // 如果找到了key,那么就是还在更新中
  1303. isOk := utils.Rc.IsExist(redisKey)
  1304. if isOk {
  1305. refreshResult = false
  1306. }
  1307. }
  1308. return
  1309. }
  1310. func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
  1311. // 相关性图表不判断指标
  1312. if utils.InArrayByInt([]int{utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION, utils.CHART_SOURCE_LINE_EQUATION}, source) && chartNameEn != "" {
  1313. return true
  1314. }
  1315. if chartNameEn != "" && len(edbList) <= 0 {
  1316. return true
  1317. }
  1318. if chartNameEn == "" {
  1319. return false
  1320. }
  1321. // 截面散点图的话,肯定是有英文配置的
  1322. if chartType == utils.CHART_TYPE_SECTION_SCATTER {
  1323. return true
  1324. }
  1325. for _, v := range edbList {
  1326. if v.EdbNameEn == "" {
  1327. return false
  1328. }
  1329. if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
  1330. return false
  1331. }
  1332. }
  1333. return true
  1334. }
  1335. func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
  1336. if edbNameEn == "" {
  1337. return false
  1338. }
  1339. if unit != "无" && unit != "" && unitEn == "" {
  1340. return false
  1341. }
  1342. return true
  1343. }
  1344. // CheckIsDisableChart 根据指标Id判断是否是禁用图表
  1345. // return int 0:启用,1:禁用
  1346. func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
  1347. crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
  1348. if err != nil {
  1349. return
  1350. }
  1351. //没有配置,立马返回
  1352. if crmConfig.ConfigValue == `` {
  1353. return
  1354. }
  1355. confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
  1356. for _, edbInfoId := range edbInfoIdArr {
  1357. for _, confEdbInfoId := range confEdbInfoIdList {
  1358. if strconv.Itoa(edbInfoId) == confEdbInfoId {
  1359. isDisable = 1
  1360. return
  1361. }
  1362. }
  1363. }
  1364. return
  1365. }
  1366. // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
  1367. func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
  1368. switch chartType {
  1369. case 10: //截面散点
  1370. var extraConfig data_manage.SectionScatterReq
  1371. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1372. if err != nil {
  1373. return
  1374. }
  1375. // 判断是否有配置日期序列
  1376. if len(extraConfig.SeriesList) <= 0 {
  1377. errMsg = `请配置序列`
  1378. err = errors.New(errMsg)
  1379. return
  1380. }
  1381. // 判断是否有填写指标
  1382. if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
  1383. errMsg = `请选择指标`
  1384. err = errors.New(errMsg)
  1385. return
  1386. }
  1387. // 遍历指标列表获取指标id
  1388. edbIdMap := make(map[int]int)
  1389. for _, v := range extraConfig.SeriesList[0].EdbInfoList {
  1390. // X 轴的指标id
  1391. if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
  1392. edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
  1393. edbIdList = append(edbIdList, v.XEdbInfoId)
  1394. }
  1395. // Y 轴的指标id
  1396. if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
  1397. edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
  1398. edbIdList = append(edbIdList, v.YEdbInfoId)
  1399. }
  1400. }
  1401. case utils.CHART_TYPE_RADAR:
  1402. var extraConfig data_manage.RadarChartInfoReq
  1403. if extraConfigStr == `` {
  1404. errMsg = "雷达图未配置"
  1405. err = errors.New(errMsg)
  1406. return
  1407. }
  1408. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1409. if err != nil {
  1410. errMsg = "雷达图配置异常"
  1411. err = errors.New(errMsg)
  1412. return
  1413. }
  1414. }
  1415. return
  1416. }
  1417. // GetSectionScatterChartData 截面散点图的数据处理
  1418. func GetSectionScatterChartData(chartInfoId int, mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
  1419. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1420. edbDataMap := make(map[int]map[string]float64)
  1421. for edbInfoId, edbDataList := range edbDataListMap {
  1422. edbDateData := make(map[string]float64)
  1423. for _, edbData := range edbDataList {
  1424. edbDateData[edbData.DataTime] = edbData.Value
  1425. }
  1426. edbDataMap[edbInfoId] = edbDateData
  1427. }
  1428. // edbIdList 指标展示顺序;x轴的指标顺序
  1429. edbIdList = make([]int, 0)
  1430. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  1431. for _, v := range mappingList {
  1432. edbIdList = append(edbIdList, v.EdbInfoId)
  1433. edbMappingMap[v.EdbInfoId] = v
  1434. }
  1435. //SectionScatterSeriesInfoResp
  1436. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  1437. for _, seriesItem := range extraConfig.SeriesList {
  1438. var maxDate time.Time
  1439. // 系列中的指标数据
  1440. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  1441. var minXVal, maxXVal, minYVal, maxYVal float64
  1442. for _, edbConf := range seriesItem.EdbInfoList {
  1443. tmpItem := data_manage.SectionScatterEdbItemResp{
  1444. IsShow: edbConf.IsShow,
  1445. Name: edbConf.Name,
  1446. NameEn: edbConf.NameEn,
  1447. } //单个坐标点的数据
  1448. //X轴的数据
  1449. {
  1450. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  1451. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1452. if !ok {
  1453. continue
  1454. }
  1455. findDate := edbConf.XDate //需要的日期值
  1456. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1457. if len(dataList) <= 0 {
  1458. // 没有数据的指标id
  1459. //findDataList = append(findDataList, 0)
  1460. continue
  1461. }
  1462. tmpItem.XEdbInfoId = edbInfoId
  1463. tmpItem.XName = edbMappingInfo.EdbName
  1464. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  1465. switch edbConf.XDateType {
  1466. case 1: //最新值
  1467. findDate = dataList[len(dataList)-1].DataTime
  1468. case 2: //近期几天
  1469. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1470. if tmpErr != nil {
  1471. err = tmpErr
  1472. return
  1473. }
  1474. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  1475. lenData := len(dataList) - 1
  1476. for i := lenData; i >= 0; i-- {
  1477. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1478. if tmpErr != nil {
  1479. err = tmpErr
  1480. return
  1481. }
  1482. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1483. findDate = dataList[i].DataTime
  1484. break
  1485. }
  1486. }
  1487. case 3: // 固定日期
  1488. //最早的日期
  1489. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1490. if tmpErr != nil {
  1491. err = tmpErr
  1492. return
  1493. }
  1494. //寻找固定日期的数据
  1495. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  1496. if tmpErr != nil {
  1497. err = tmpErr
  1498. return
  1499. }
  1500. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1501. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1502. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1503. findDate = tmpDate
  1504. break
  1505. }
  1506. }
  1507. default:
  1508. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  1509. return
  1510. }
  1511. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1512. if maxDate.IsZero() {
  1513. maxDate = findDateTime
  1514. } else {
  1515. if findDateTime.After(maxDate) {
  1516. maxDate = findDateTime
  1517. }
  1518. }
  1519. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1520. tmpItem.XDate = findDate
  1521. tmpItem.XValue = tmpValue
  1522. } else {
  1523. continue
  1524. }
  1525. }
  1526. //Y轴的数据
  1527. {
  1528. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1529. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1530. if !ok {
  1531. continue
  1532. }
  1533. findDate := edbConf.YDate //需要的日期值
  1534. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1535. if len(dataList) <= 0 {
  1536. // 没有数据的指标id
  1537. //findDataList = append(findDataList, 0)
  1538. continue
  1539. }
  1540. tmpItem.YEdbInfoId = edbInfoId
  1541. tmpItem.YName = edbMappingInfo.EdbName
  1542. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1543. switch edbConf.YDateType {
  1544. case 1: //最新值
  1545. findDate = dataList[len(dataList)-1].DataTime
  1546. case 2: //近期几天
  1547. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1548. if tmpErr != nil {
  1549. err = tmpErr
  1550. return
  1551. }
  1552. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1553. lenData := len(dataList) - 1
  1554. for i := lenData; i >= 0; i-- {
  1555. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1556. if tmpErr != nil {
  1557. err = tmpErr
  1558. return
  1559. }
  1560. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1561. findDate = dataList[i].DataTime
  1562. break
  1563. }
  1564. }
  1565. case 3: // 固定日期
  1566. //最早的日期
  1567. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1568. if tmpErr != nil {
  1569. err = tmpErr
  1570. return
  1571. }
  1572. //寻找固定日期的数据
  1573. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1574. if tmpErr != nil {
  1575. err = tmpErr
  1576. return
  1577. }
  1578. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1579. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1580. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1581. findDate = tmpDate
  1582. break
  1583. }
  1584. }
  1585. default:
  1586. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1587. return
  1588. }
  1589. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1590. if maxDate.IsZero() {
  1591. maxDate = findDateTime
  1592. } else {
  1593. if findDateTime.After(maxDate) {
  1594. maxDate = findDateTime
  1595. }
  1596. }
  1597. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1598. tmpItem.YDate = findDate
  1599. tmpItem.YValue = tmpValue
  1600. } else {
  1601. continue
  1602. }
  1603. }
  1604. // 获取当前系列的X轴的最大最小值
  1605. {
  1606. if tmpItem.XValue < minXVal {
  1607. minXVal = tmpItem.XValue
  1608. }
  1609. if tmpItem.XValue > maxXVal {
  1610. maxXVal = tmpItem.XValue
  1611. }
  1612. if tmpItem.YValue < minYVal {
  1613. minYVal = tmpItem.YValue
  1614. }
  1615. if tmpItem.YValue > maxYVal {
  1616. maxYVal = tmpItem.YValue
  1617. }
  1618. }
  1619. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1620. }
  1621. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  1622. var trendLine, rSquare string
  1623. // 生成线性方程式
  1624. var a, b float64
  1625. {
  1626. coordinateData := make([]utils.Coordinate, 0)
  1627. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1628. tmpCoordinate1 := utils.Coordinate{
  1629. X: tmpSeriesEdbInfo.XValue,
  1630. Y: tmpSeriesEdbInfo.YValue,
  1631. }
  1632. coordinateData = append(coordinateData, tmpCoordinate1)
  1633. }
  1634. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  1635. if len(coordinateData) >= 2 {
  1636. a, b = utils.GetLinearResult(coordinateData)
  1637. if !math.IsNaN(a) && !math.IsNaN(b) {
  1638. if b > 0 {
  1639. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1640. } else {
  1641. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1642. }
  1643. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1644. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1645. }
  1646. // 计算R平方
  1647. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  1648. }
  1649. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  1650. X: minXVal,
  1651. Y: minYVal,
  1652. }, data_manage.CoordinatePoint{
  1653. X: maxXVal,
  1654. Y: maxYVal,
  1655. })
  1656. }
  1657. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  1658. Name: seriesItem.Name,
  1659. NameEn: seriesItem.NameEn,
  1660. IsNameDefault: seriesItem.IsNameDefault,
  1661. Color: seriesItem.Color,
  1662. EdbInfoList: tmpSeriesEdbInfoList,
  1663. ShowTrendLine: seriesItem.ShowTrendLine,
  1664. ShowFitEquation: seriesItem.ShowFitEquation,
  1665. ShowRSquare: seriesItem.ShowRSquare,
  1666. TrendLine: trendLine,
  1667. RSquare: rSquare,
  1668. TrendLimitData: trendLimitData,
  1669. })
  1670. }
  1671. // 截面散点图点击详情时自动更新系列名
  1672. if len(extraConfig.SeriesList) > 0 {
  1673. // 默认名字的时候才自动更新
  1674. if extraConfig.SeriesList[0].IsNameDefault {
  1675. firstXEdbInfoId := extraConfig.SeriesList[0].EdbInfoList[0].XEdbInfoId
  1676. needUpdate := false
  1677. if v, ok := edbMappingMap[firstXEdbInfoId]; ok {
  1678. extraConfig.SeriesList[0].Name = v.LatestDate
  1679. extraConfig.SeriesList[0].NameEn = v.LatestDate
  1680. dataListResp[0].Name = v.LatestDate
  1681. dataListResp[0].NameEn = v.LatestDate
  1682. needUpdate = true
  1683. }
  1684. extraConfigByte, e := json.Marshal(extraConfig)
  1685. if e != nil {
  1686. errMsg := "截面散点系列更新异常"
  1687. err = errors.New(errMsg)
  1688. return
  1689. }
  1690. extraConfigStr := string(extraConfigByte)
  1691. if needUpdate {
  1692. err = data_manage.EditChartInfoExtraConfig(chartInfoId, extraConfigStr)
  1693. if err != nil {
  1694. errMsg := "截面散点系列更新异常"
  1695. err = errors.New(errMsg)
  1696. return
  1697. }
  1698. }
  1699. }
  1700. }
  1701. chartDataResp = data_manage.SectionScatterInfoResp{
  1702. XName: extraConfig.XName,
  1703. XNameEn: extraConfig.XNameEn,
  1704. XUnitName: extraConfig.XUnitName,
  1705. XUnitNameEn: extraConfig.XUnitNameEn,
  1706. YName: extraConfig.YName,
  1707. YNameEn: extraConfig.YNameEn,
  1708. YUnitName: extraConfig.YUnitName,
  1709. YUnitNameEn: extraConfig.YUnitNameEn,
  1710. XMinValue: extraConfig.XMinValue,
  1711. XMaxValue: extraConfig.XMaxValue,
  1712. YMinValue: extraConfig.YMinValue,
  1713. YMaxValue: extraConfig.YMaxValue,
  1714. DataList: dataListResp,
  1715. }
  1716. return
  1717. }
  1718. // AddChartInfo 添加图表
  1719. func AddChartInfo(req data_manage.AddChartInfoReq, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1720. isSendEmail = true // 默认错误的时候要发送邮件
  1721. req.ChartName = strings.Trim(req.ChartName, " ")
  1722. if req.ChartName == "" {
  1723. errMsg = "请填写图表名称!"
  1724. err = errors.New(errMsg)
  1725. isSendEmail = false
  1726. return
  1727. }
  1728. if req.ChartClassifyId <= 0 {
  1729. errMsg = "分类参数错误!"
  1730. err = errors.New(errMsg)
  1731. isSendEmail = false
  1732. return
  1733. }
  1734. //if req.ChartThemeId <= 0 {
  1735. // errMsg = "请选择主题"
  1736. // err = errors.New(errMsg)
  1737. // isSendEmail = false
  1738. // return
  1739. //}
  1740. //
  1741. //// 判断主题是否存在
  1742. //_, err = chart_theme.GetChartThemeId(req.ChartThemeId)
  1743. //if err != nil {
  1744. // if err.Error() == utils.ErrNoRow() {
  1745. // errMsg = "主题不存在"
  1746. // err = errors.New(errMsg)
  1747. // return
  1748. // }
  1749. // errMsg = "获取主题信息失败"
  1750. // return
  1751. //}
  1752. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1753. if err != nil {
  1754. if err.Error() == utils.ErrNoRow() {
  1755. errMsg = "分类不存在"
  1756. err = errors.New(errMsg)
  1757. return
  1758. }
  1759. errMsg = "获取分类信息失败"
  1760. return
  1761. }
  1762. if chartClassify == nil {
  1763. errMsg = "分类不存在"
  1764. err = errors.New(errMsg)
  1765. return
  1766. }
  1767. chartType := req.ChartType
  1768. extraConfig := req.ExtraConfig
  1769. var extraConfigEdbInfoIdArr []int
  1770. extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(chartType, extraConfig)
  1771. if err != nil {
  1772. err = errors.New("添加失败:" + err.Error())
  1773. return
  1774. }
  1775. // 季节性图表额外配置信息
  1776. var seasonExtraConfig string
  1777. // 关联指标
  1778. var edbInfoIdArr []int
  1779. chartEdbInfoList := req.ChartEdbInfoList
  1780. if len(chartEdbInfoList) > 0 {
  1781. for _, v := range chartEdbInfoList {
  1782. edbInfoId := v.EdbInfoId
  1783. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1784. if tmpErr != nil {
  1785. if tmpErr.Error() == utils.ErrNoRow() {
  1786. errMsg = "指标不存在!"
  1787. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1788. return
  1789. } else {
  1790. errMsg = "获取指标信息失败!"
  1791. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1792. return
  1793. }
  1794. }
  1795. if edbInfo == nil {
  1796. errMsg = "指标已被删除,请重新选择!"
  1797. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1798. return
  1799. } else {
  1800. if edbInfo.EdbInfoId <= 0 {
  1801. errMsg = "指标已被删除,请重新选择!"
  1802. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1803. return
  1804. }
  1805. }
  1806. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1807. edbInfo.EdbNameSource = edbInfo.EdbName
  1808. //edbList = append(edbList, edbInfo)
  1809. //新增季节性图表数据计算
  1810. if chartType == 2 {
  1811. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  1812. if tmpErr != nil {
  1813. errMsg = "保存失败"
  1814. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  1815. return
  1816. }
  1817. if existCount <= 0 {
  1818. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  1819. }
  1820. // 处理季节性图表横轴配置
  1821. {
  1822. if req.SeasonExtraConfig.XEndDate != "" {
  1823. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  1824. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  1825. err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
  1826. return
  1827. }
  1828. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  1829. if tErr != nil {
  1830. errMsg = "季节性图表配置信息异常"
  1831. err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
  1832. return
  1833. }
  1834. seasonExtraConfig = string(seasonExtra)
  1835. }
  1836. }
  1837. }
  1838. }
  1839. } else {
  1840. if extraConfig == `` {
  1841. errMsg = "请选择指标!"
  1842. err = errors.New(errMsg)
  1843. return
  1844. }
  1845. edbInfoIdArr = extraConfigEdbInfoIdArr
  1846. lenEdbInfoIdArr := len(edbInfoIdArr)
  1847. if lenEdbInfoIdArr > 0 {
  1848. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  1849. if tmpErr != nil {
  1850. errMsg = "指标异常!"
  1851. err = errors.New("指标异常!Err:" + tmpErr.Error())
  1852. return
  1853. }
  1854. if len(tmpEdbList) != lenEdbInfoIdArr {
  1855. errMsg = "指标异常!"
  1856. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  1857. return
  1858. }
  1859. for _, v := range tmpEdbList {
  1860. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  1861. EdbInfoId: v.EdbInfoId,
  1862. MaxData: 0,
  1863. MinData: 0,
  1864. IsOrder: false,
  1865. IsAxis: 1,
  1866. EdbInfoType: 1,
  1867. LeadValue: 0,
  1868. LeadUnit: "",
  1869. ChartStyle: "",
  1870. ChartColor: "",
  1871. PredictChartColor: "",
  1872. ChartWidth: 0,
  1873. Source: utils.CHART_SOURCE_DEFAULT,
  1874. })
  1875. }
  1876. }
  1877. }
  1878. if len(edbInfoIdArr) <= 0 {
  1879. errMsg = "请选择指标!"
  1880. err = errors.New(errMsg)
  1881. return
  1882. }
  1883. // 图表额外配置
  1884. extraConfig, err, errMsg = HandleExtraConfig(chartType, extraConfig)
  1885. if err != nil {
  1886. if errMsg == `` {
  1887. errMsg = "指标异常!"
  1888. }
  1889. err = errors.New("指标异常!Err:" + err.Error())
  1890. return
  1891. }
  1892. sort.Ints(edbInfoIdArr)
  1893. var edbInfoIdArrStr []string
  1894. for _, v := range edbInfoIdArr {
  1895. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1896. }
  1897. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1898. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  1899. //if err != nil && err.Error() != utils.ErrNoRow() {
  1900. // br.Msg = "保存失败"
  1901. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  1902. // return
  1903. //}
  1904. //if count > 0 {
  1905. // br.Msg = "所选指标已存在相同指标"
  1906. // return
  1907. //}
  1908. //判断图表是否存在
  1909. var condition string
  1910. var pars []interface{}
  1911. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1912. //condition += " AND chart_classify_id=? "
  1913. //pars = append(pars, req.ChartClassifyId)
  1914. condition += " AND chart_name=? AND source = ? "
  1915. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  1916. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1917. if err != nil {
  1918. errMsg = "判断图表名称是否存在失败"
  1919. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  1920. return
  1921. }
  1922. if count > 0 {
  1923. errMsg = "图表已存在,请重新填写"
  1924. err = errors.New("判断图表名称是否存在失败")
  1925. isSendEmail = false
  1926. return
  1927. }
  1928. barChartConf := `` //柱方图的配置信息
  1929. if chartType == 7 {
  1930. barChartConfReq := req.BarChartInfo
  1931. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  1932. errMsg = "请选择指标"
  1933. err = errors.New(errMsg)
  1934. isSendEmail = false
  1935. return
  1936. }
  1937. if len(barChartConfReq.DateList) <= 0 {
  1938. errMsg = "请选择日期"
  1939. err = errors.New(errMsg)
  1940. isSendEmail = false
  1941. return
  1942. }
  1943. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  1944. if tmpErr != nil {
  1945. errMsg = "柱方图信息异常"
  1946. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  1947. return
  1948. }
  1949. barChartConf = string(barChartConfByte)
  1950. }
  1951. disableVal := CheckIsDisableChart(edbInfoIdArr)
  1952. chartInfo = new(data_manage.ChartInfo)
  1953. chartInfo.ChartName = req.ChartName
  1954. chartInfo.EdbInfoIds = edbInfoIdStr
  1955. chartInfo.ChartClassifyId = req.ChartClassifyId
  1956. chartInfo.SysUserId = sysUserId
  1957. chartInfo.SysUserRealName = sysUserRealName
  1958. chartInfo.CreateTime = time.Now()
  1959. chartInfo.ModifyTime = time.Now()
  1960. chartInfo.IsSetName = 0
  1961. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1962. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  1963. // todo 判断是否需要重新计算用户的start_date
  1964. if req.DateType > 0 {
  1965. chartInfo.DateType = req.DateType
  1966. } else {
  1967. chartInfo.DateType = 3
  1968. }
  1969. if chartType == 0 {
  1970. chartType = 1
  1971. }
  1972. chartInfo.ChartType = chartType
  1973. calendar := req.Calendar
  1974. if calendar == "" {
  1975. calendar = "公历"
  1976. }
  1977. chartInfo.Calendar = calendar
  1978. chartInfo.StartDate = req.StartDate
  1979. chartInfo.EndDate = req.EndDate
  1980. chartInfo.SeasonStartDate = req.StartDate
  1981. chartInfo.SeasonEndDate = req.EndDate
  1982. chartInfo.LeftMin = req.LeftMin
  1983. chartInfo.LeftMax = req.LeftMax
  1984. chartInfo.RightMin = req.RightMin
  1985. chartInfo.RightMax = req.RightMax
  1986. chartInfo.Disabled = disableVal
  1987. chartInfo.BarConfig = barChartConf
  1988. chartInfo.ExtraConfig = extraConfig
  1989. chartInfo.SeasonExtraConfig = seasonExtraConfig
  1990. chartInfo.StartYear = req.StartYear
  1991. chartInfo.Source = utils.CHART_SOURCE_DEFAULT
  1992. chartInfo.ChartThemeId = req.ChartThemeId
  1993. chartInfo.SourcesFrom = req.SourcesFrom
  1994. chartInfo.Instructions = req.Instructions
  1995. chartInfo.MarkersLines = req.MarkersLines
  1996. chartInfo.MarkersAreas = req.MarkersAreas
  1997. chartInfo.Unit = req.Unit
  1998. chartInfo.UnitEn = req.UnitEn
  1999. newId, err := data_manage.AddChartInfo(chartInfo)
  2000. if err != nil {
  2001. errMsg = `保存失败`
  2002. err = errors.New("保存失败,Err:" + err.Error())
  2003. return
  2004. }
  2005. chartInfo.ChartInfoId = int(newId)
  2006. mapList := make([]*data_manage.ChartEdbMapping, 0)
  2007. for _, v := range chartEdbInfoList {
  2008. mapItem := new(data_manage.ChartEdbMapping)
  2009. mapItem.ChartInfoId = int(newId)
  2010. mapItem.EdbInfoId = v.EdbInfoId
  2011. mapItem.CreateTime = time.Now()
  2012. mapItem.ModifyTime = time.Now()
  2013. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2014. mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", v.EdbInfoId, "_", timestamp))
  2015. mapItem.MaxData = v.MaxData
  2016. mapItem.MinData = v.MinData
  2017. mapItem.IsOrder = v.IsOrder
  2018. mapItem.IsAxis = v.IsAxis
  2019. mapItem.EdbInfoType = v.EdbInfoType
  2020. mapItem.LeadValue = v.LeadValue
  2021. mapItem.LeadUnit = v.LeadUnit
  2022. mapItem.ChartStyle = v.ChartStyle
  2023. mapItem.ChartColor = v.ChartColor
  2024. mapItem.PredictChartColor = v.PredictChartColor
  2025. mapItem.ChartWidth = v.ChartWidth
  2026. mapItem.Source = utils.CHART_SOURCE_DEFAULT
  2027. mapItem.EdbAliasName = v.EdbAliasName
  2028. mapItem.IsConvert = v.IsConvert
  2029. mapItem.ConvertType = v.ConvertType
  2030. mapItem.ConvertValue = v.ConvertValue
  2031. mapItem.ConvertUnit = v.ConvertUnit
  2032. mapItem.ConvertEnUnit = v.ConvertEnUnit
  2033. mapList = append(mapList, mapItem)
  2034. }
  2035. err = data_manage.AddChartEdbMapping(mapList)
  2036. if err != nil {
  2037. errMsg = `保存失败`
  2038. err = errors.New("保存失败,Err:" + err.Error())
  2039. return
  2040. }
  2041. //添加es数据
  2042. go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  2043. return
  2044. }
  2045. // EditChartInfo 编辑图表
  2046. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  2047. isSendEmail = true // 默认错误的时候要发送邮件
  2048. req.ChartName = strings.Trim(req.ChartName, " ")
  2049. if req.ChartName == "" {
  2050. errMsg = "请填写图表名称!"
  2051. err = errors.New(errMsg)
  2052. isSendEmail = false
  2053. return
  2054. }
  2055. if req.ChartClassifyId <= 0 {
  2056. errMsg = "分类参数错误!"
  2057. err = errors.New(errMsg)
  2058. isSendEmail = false
  2059. return
  2060. }
  2061. //if req.ChartThemeId <= 0 {
  2062. // errMsg = "请选择主题!"
  2063. // err = errors.New(errMsg)
  2064. // isSendEmail = false
  2065. // return
  2066. //}
  2067. //
  2068. //// 判断主题是否存在
  2069. //_, err = chart_theme.GetChartThemeId(req.ChartThemeId)
  2070. //if err != nil {
  2071. // if err.Error() == utils.ErrNoRow() {
  2072. // errMsg = "主题不存在"
  2073. // err = errors.New(errMsg)
  2074. // return
  2075. // }
  2076. // errMsg = "获取主题信息失败"
  2077. // return
  2078. //}
  2079. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  2080. if err != nil {
  2081. if err.Error() == utils.ErrNoRow() {
  2082. errMsg = "分类不存在"
  2083. err = errors.New(errMsg)
  2084. return
  2085. }
  2086. errMsg = "获取分类信息失败"
  2087. return
  2088. }
  2089. if chartClassify == nil {
  2090. errMsg = "分类不存在"
  2091. err = errors.New(errMsg)
  2092. return
  2093. }
  2094. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  2095. if err != nil {
  2096. if err.Error() == utils.ErrNoRow() {
  2097. errMsg = "图表已被删除,请刷新页面"
  2098. err = errors.New(errMsg)
  2099. isSendEmail = false
  2100. return
  2101. }
  2102. errMsg = "获取图表信息失败"
  2103. err = errors.New("获取图表信息失败,Err:" + err.Error())
  2104. return
  2105. }
  2106. var extraConfigEdbInfoIdArr []int
  2107. extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
  2108. if err != nil {
  2109. err = errors.New("添加失败:" + err.Error())
  2110. return
  2111. }
  2112. // 季节性图表额外配置信息
  2113. var seasonExtraConfig string
  2114. // 图表关联指标id
  2115. chartEdbInfoList := make([]*data_manage.ChartSaveItem, 0)
  2116. // 关联指标
  2117. var edbInfoIdArr []int
  2118. // 非散点截面图的直接通过前端获取,散点截面图需要额外从配置中获取
  2119. if chartItem.ChartType != 10 {
  2120. //edbList := make([]*data_manage.EdbInfo, 0)
  2121. for _, v := range req.ChartEdbInfoList {
  2122. edbInfoId := v.EdbInfoId
  2123. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  2124. if tmpErr != nil {
  2125. if tmpErr.Error() == utils.ErrNoRow() {
  2126. errMsg = "指标不存在!"
  2127. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  2128. return
  2129. } else {
  2130. errMsg = "获取指标信息失败!"
  2131. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  2132. return
  2133. }
  2134. }
  2135. if edbInfo == nil {
  2136. errMsg = "指标已被删除,请重新选择!"
  2137. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  2138. return
  2139. } else {
  2140. if edbInfo.EdbInfoId <= 0 {
  2141. errMsg = "指标已被删除,请重新选择!"
  2142. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  2143. return
  2144. }
  2145. }
  2146. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  2147. edbInfo.EdbNameSource = edbInfo.EdbName
  2148. //edbList = append(edbList, edbInfo)
  2149. //新增季节性图表数据计算
  2150. if req.ChartType == 2 {
  2151. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  2152. if tmpErr != nil {
  2153. errMsg = "保存失败"
  2154. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  2155. return
  2156. }
  2157. if existCount <= 0 {
  2158. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  2159. }
  2160. // 处理季节性图表横轴配置
  2161. {
  2162. if req.SeasonExtraConfig.XEndDate != "" {
  2163. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  2164. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  2165. err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
  2166. return
  2167. }
  2168. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  2169. if tErr != nil {
  2170. errMsg = "季节性图表配置信息异常"
  2171. err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
  2172. return
  2173. }
  2174. seasonExtraConfig = string(seasonExtra)
  2175. }
  2176. }
  2177. }
  2178. }
  2179. chartEdbInfoList = req.ChartEdbInfoList
  2180. } else {
  2181. if req.ExtraConfig == `` {
  2182. errMsg = "请选择指标!"
  2183. err = errors.New(errMsg)
  2184. isSendEmail = false
  2185. return
  2186. }
  2187. edbInfoIdArr = extraConfigEdbInfoIdArr
  2188. lenEdbInfoIdArr := len(edbInfoIdArr)
  2189. if lenEdbInfoIdArr > 0 {
  2190. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  2191. if tmpErr != nil {
  2192. errMsg = "指标异常!"
  2193. err = errors.New("指标异常!Err:" + tmpErr.Error())
  2194. return
  2195. }
  2196. if len(tmpEdbList) != lenEdbInfoIdArr {
  2197. errMsg = "指标异常!"
  2198. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  2199. return
  2200. }
  2201. for _, v := range tmpEdbList {
  2202. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  2203. EdbInfoId: v.EdbInfoId,
  2204. MaxData: 0,
  2205. MinData: 0,
  2206. IsOrder: false,
  2207. IsAxis: 1,
  2208. EdbInfoType: 1,
  2209. LeadValue: 0,
  2210. LeadUnit: "",
  2211. ChartStyle: "",
  2212. ChartColor: "",
  2213. PredictChartColor: "",
  2214. ChartWidth: 0,
  2215. Source: utils.CHART_SOURCE_DEFAULT,
  2216. })
  2217. }
  2218. }
  2219. }
  2220. if len(edbInfoIdArr) <= 0 {
  2221. errMsg = "请选择指标!"
  2222. err = errors.New(errMsg)
  2223. isSendEmail = false
  2224. return
  2225. }
  2226. // 图表额外配置
  2227. extraConfig, err, errMsg := HandleExtraConfig(req.ChartType, req.ExtraConfig)
  2228. if err != nil {
  2229. if errMsg == `` {
  2230. errMsg = "指标异常!"
  2231. }
  2232. err = errors.New("指标异常!Err:" + err.Error())
  2233. return
  2234. }
  2235. req.ExtraConfig = extraConfig
  2236. //图表操作权限
  2237. ok := CheckOpChartPermission(sysUser, chartItem.SysUserId)
  2238. if !ok {
  2239. errMsg = "没有该图表的操作权限"
  2240. err = errors.New(errMsg)
  2241. isSendEmail = false
  2242. return
  2243. }
  2244. var calendar string
  2245. var dateType int
  2246. dateType = req.DateType
  2247. if req.ChartType != chartItem.ChartType {
  2248. if chartItem.ChartType != 2 && chartItem.Calendar == "" { //非季节性图
  2249. calendar = "公历"
  2250. }
  2251. if chartItem.ChartType == 2 && chartItem.DateType <= 0 {
  2252. dateType = 3
  2253. }
  2254. }
  2255. sort.Ints(edbInfoIdArr)
  2256. var edbInfoIdArrStr []string
  2257. for _, v := range edbInfoIdArr {
  2258. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  2259. }
  2260. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  2261. //mappingCondition := ` AND chart_info_id<> ` + strconv.Itoa(req.ChartInfoId)
  2262. //count, err := data_manage.ChartInfoExist(mappingCondition, edbInfoIdStr)
  2263. //if err != nil && err.Error() != utils.ErrNoRow() {
  2264. // br.Msg = "保存失败"
  2265. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2266. // return
  2267. //}
  2268. //if count > 0 {
  2269. // br.Msg = "所选指标已存在相同指标"
  2270. // return
  2271. //}
  2272. //判断图表是否存在
  2273. var condition string
  2274. var pars []interface{}
  2275. condition += " AND chart_info_id<>? "
  2276. pars = append(pars, req.ChartInfoId)
  2277. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  2278. //condition += " AND chart_classify_id=? "
  2279. //pars = append(pars, req.ChartClassifyId)
  2280. condition += " AND chart_name=? AND source = ? "
  2281. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  2282. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  2283. if err != nil {
  2284. errMsg = "判断图表名称是否存在失败"
  2285. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  2286. return
  2287. }
  2288. if count > 0 {
  2289. errMsg = "图表已存在,请重新填写"
  2290. err = errors.New(errMsg)
  2291. isSendEmail = false
  2292. return
  2293. }
  2294. barChartConf := `` //柱方图的配置信息
  2295. if req.ChartType == 7 {
  2296. barChartConfReq := req.BarChartInfo
  2297. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  2298. errMsg = "请选择指标"
  2299. err = errors.New(errMsg)
  2300. isSendEmail = false
  2301. return
  2302. }
  2303. if len(barChartConfReq.DateList) <= 0 {
  2304. errMsg = "请选择日期"
  2305. err = errors.New(errMsg)
  2306. isSendEmail = false
  2307. return
  2308. }
  2309. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  2310. if tmpErr != nil {
  2311. errMsg = "柱方图信息异常"
  2312. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  2313. return
  2314. }
  2315. barChartConf = string(barChartConfByte)
  2316. }
  2317. // 图表启用与否
  2318. disableVal := CheckIsDisableChart(edbInfoIdArr)
  2319. err = data_manage.EditChartInfoAndMapping(&req, edbInfoIdStr, calendar, dateType, disableVal, barChartConf, chartEdbInfoList, seasonExtraConfig)
  2320. if err != nil {
  2321. errMsg = "保存失败"
  2322. err = errors.New("保存失败,Err:" + err.Error())
  2323. return
  2324. }
  2325. resp := new(data_manage.AddChartInfoResp)
  2326. resp.ChartInfoId = chartItem.ChartInfoId
  2327. resp.UniqueCode = chartItem.UniqueCode
  2328. resp.ChartType = req.ChartType
  2329. //添加es数据
  2330. go EsAddOrEditChartInfo(chartItem.ChartInfoId)
  2331. //修改my eta es数据
  2332. go EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  2333. return
  2334. }
  2335. // GetEdbSourceByEdbInfoIdList 获取关联指标的来源
  2336. func GetEdbSourceByEdbInfoIdList(chartEdbInfoMappingList []*data_manage.ChartEdbInfoMapping) (sourceNameList, sourceNameEnList []string) {
  2337. sourceNameList = make([]string, 0)
  2338. sourceNameEnList = make([]string, 0)
  2339. sourceMap := make(map[int]string)
  2340. for _, v := range chartEdbInfoMappingList {
  2341. // 指标类型:1:基础指标,2:计算指标
  2342. if v.EdbType == 2 || v.EdbInfoCategoryType == 1 {
  2343. //sourceMap[0] = "弘则研究"
  2344. baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  2345. for _, baseEdbInfo := range baseEdbInfoArr {
  2346. if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
  2347. sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
  2348. }
  2349. }
  2350. } else {
  2351. sourceMap[v.Source] = v.SourceName
  2352. }
  2353. }
  2354. for source, sourceName := range sourceMap {
  2355. if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL, utils.DATA_SOURCE_MYSTEEL_CHEMICAL}, source) {
  2356. continue
  2357. }
  2358. sourceNameList = append(sourceNameList, sourceName)
  2359. sourceNameEn, ok := utils.DataSourceEnMap[source]
  2360. if !ok {
  2361. sourceNameEn = sourceName
  2362. }
  2363. sourceNameEnList = append(sourceNameEnList, sourceNameEn)
  2364. }
  2365. //sourceNameList = append(sourceNameList, utils.ChartDefaultNameCn)
  2366. //sourceNameEnList = append(sourceNameEnList, utils.ChartDefaultNameEn)
  2367. // 图表来源
  2368. conf, e := models.GetBusinessConf()
  2369. if e != nil {
  2370. return
  2371. }
  2372. if conf[models.BusinessConfCompanyName] != "" {
  2373. sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
  2374. sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
  2375. }
  2376. return
  2377. }
  2378. // RadarChartData 雷达图的数据处理
  2379. func RadarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.RadarChartInfoReq) (edbIdList []int, chartDataResp data_manage.RadarChartInfoResp, err error) {
  2380. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  2381. edbDataMap := make(map[int]map[string]float64)
  2382. for edbInfoId, edbDataList := range edbDataListMap {
  2383. edbDateData := make(map[string]float64)
  2384. for _, edbData := range edbDataList {
  2385. edbDateData[edbData.DataTime] = edbData.Value
  2386. }
  2387. edbDataMap[edbInfoId] = edbDateData
  2388. }
  2389. // edbIdList 指标展示顺序;x轴的指标顺序
  2390. edbIdList = make([]int, 0)
  2391. for _, v := range mappingList {
  2392. edbIdList = append(edbIdList, v.EdbInfoId)
  2393. }
  2394. chartDateList := extraConfig.DateList
  2395. yDataList := make([]data_manage.RadarYData, 0) //y轴的数据列表
  2396. for _, chartDate := range chartDateList {
  2397. var maxDate time.Time
  2398. findDataList := make([]float64, 0) // 当前日期的数据值
  2399. for _, edbInfoId := range edbIdList {
  2400. findDate := chartDate.Date //需要的日期值
  2401. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  2402. if len(dataList) <= 0 {
  2403. // 没有数据的指标id
  2404. findDataList = append(findDataList, 0)
  2405. continue
  2406. }
  2407. switch chartDate.Type {
  2408. case 1: //最新值
  2409. findDate = dataList[len(dataList)-1].DataTime
  2410. case 2: //近期几天
  2411. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  2412. if tmpErr != nil {
  2413. err = tmpErr
  2414. return
  2415. }
  2416. findDateTime = findDateTime.AddDate(0, 0, -chartDate.Value)
  2417. lenData := len(dataList) - 1
  2418. for i := lenData; i >= 0; i-- {
  2419. currDateTime, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  2420. if e != nil {
  2421. err = e
  2422. return
  2423. }
  2424. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  2425. findDate = dataList[i].DataTime
  2426. break
  2427. }
  2428. }
  2429. case 3: // 固定日期
  2430. //最早的日期
  2431. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  2432. if tmpErr != nil {
  2433. err = tmpErr
  2434. return
  2435. }
  2436. //寻找固定日期的数据
  2437. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, chartDate.Date, time.Local)
  2438. if tmpErr != nil {
  2439. err = tmpErr
  2440. return
  2441. }
  2442. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  2443. tmpDate := tmpDateTime.Format(utils.FormatDate)
  2444. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  2445. findDate = tmpDate
  2446. break
  2447. }
  2448. }
  2449. default:
  2450. err = errors.New(fmt.Sprint("日期类型异常,Type:", chartDate.Type))
  2451. return
  2452. }
  2453. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  2454. if maxDate.IsZero() {
  2455. maxDate = findDateTime
  2456. } else {
  2457. if findDateTime.After(maxDate) {
  2458. maxDate = findDateTime
  2459. }
  2460. }
  2461. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  2462. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  2463. findDataList = append(findDataList, tmpValue)
  2464. } else {
  2465. findDataList = append(findDataList, 0)
  2466. }
  2467. }
  2468. yDate := "0000-00-00"
  2469. if !maxDate.IsZero() {
  2470. yDate = maxDate.Format(utils.FormatDate)
  2471. }
  2472. yDataList = append(yDataList, data_manage.RadarYData{
  2473. Date: yDate,
  2474. Value: findDataList,
  2475. Color: chartDate.Color,
  2476. Name: chartDate.Name,
  2477. })
  2478. }
  2479. chartDataResp = data_manage.RadarChartInfoResp{
  2480. YDataList: yDataList,
  2481. XEdbIdValue: edbIdList,
  2482. }
  2483. return
  2484. }
  2485. // GetChartConvertEdbData 获取图表数据转换的指标数据
  2486. func GetChartConvertEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
  2487. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  2488. xEdbIdValue = make([]int, 0)
  2489. yDataList = make([]data_manage.YData, 0)
  2490. var extraConfig interface{}
  2491. switch chartType {
  2492. case 7: // 柱形图
  2493. var barConfig data_manage.BarChartInfoReq
  2494. if extraConfigStr == `` {
  2495. errMsg = "柱方图未配置"
  2496. err = errors.New(errMsg)
  2497. return
  2498. }
  2499. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  2500. if err != nil {
  2501. errMsg = "柱方图配置异常"
  2502. err = errors.New(errMsg)
  2503. return
  2504. }
  2505. extraConfig = barConfig
  2506. case 10: // 截面散点图
  2507. var tmpExtraConfig data_manage.SectionScatterReq
  2508. if extraConfigStr == `` {
  2509. errMsg = "截面散点图未配置"
  2510. err = errors.New(errMsg)
  2511. return
  2512. }
  2513. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  2514. if err != nil {
  2515. errMsg = "截面散点配置异常"
  2516. err = errors.New(errMsg)
  2517. return
  2518. }
  2519. extraConfig = tmpExtraConfig
  2520. case utils.CHART_TYPE_RADAR:
  2521. var barConfig data_manage.RadarChartInfoReq
  2522. if extraConfigStr == `` {
  2523. errMsg = "雷达图未配置"
  2524. err = errors.New(errMsg)
  2525. return
  2526. }
  2527. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  2528. if err != nil {
  2529. errMsg = "雷达图配置异常"
  2530. err = errors.New(errMsg)
  2531. return
  2532. }
  2533. extraConfig = barConfig
  2534. default:
  2535. xEdbIdValue = make([]int, 0)
  2536. yDataList = make([]data_manage.YData, 0)
  2537. }
  2538. // 指标对应的所有数据
  2539. edbDataListMap, edbList, err := getEdbConvertDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  2540. if err != nil {
  2541. return
  2542. }
  2543. // 特殊图形数据处理
  2544. switch chartType {
  2545. case 7: // 柱形图
  2546. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  2547. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  2548. for k := range yDataList {
  2549. yDataList[k].Unit = barChartConf.Unit
  2550. yDataList[k].UnitEn = barChartConf.UnitEn
  2551. }
  2552. for _, v := range edbList {
  2553. // 指标别名
  2554. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  2555. for _, reqEdb := range barChartConf.EdbInfoIdList {
  2556. if v.EdbInfoId == reqEdb.EdbInfoId {
  2557. v.EdbAliasName = reqEdb.Name
  2558. }
  2559. }
  2560. }
  2561. }
  2562. case 10: // 截面散点图
  2563. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  2564. xEdbIdValue, dataResp, err = GetSectionScatterChartData(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
  2565. var tmpExtraConfig data_manage.SectionScatterReq
  2566. if extraConfigStr == `` {
  2567. errMsg = "截面散点图未配置"
  2568. err = errors.New(errMsg)
  2569. return
  2570. }
  2571. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  2572. if err != nil {
  2573. errMsg = "截面散点配置异常"
  2574. err = errors.New(errMsg)
  2575. return
  2576. }
  2577. // 这个数据没有必要返回给前端
  2578. for _, v := range edbList {
  2579. v.DataList = nil
  2580. }
  2581. case utils.CHART_TYPE_RADAR: //雷达图
  2582. radarConf := extraConfig.(data_manage.RadarChartInfoReq)
  2583. xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
  2584. }
  2585. return
  2586. }
  2587. func getEdbConvertDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  2588. // 指标对应的所有数据
  2589. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  2590. for _, v := range mappingList {
  2591. //fmt.Println("v:", v.EdbInfoId)
  2592. item := new(data_manage.ChartEdbInfoMapping)
  2593. item.EdbInfoId = v.EdbInfoId
  2594. item.SourceName = v.SourceName
  2595. item.Source = v.Source
  2596. item.EdbCode = v.EdbCode
  2597. item.EdbName = v.EdbName
  2598. item.EdbNameEn = v.EdbNameEn
  2599. item.Frequency = v.Frequency
  2600. item.EdbType = v.EdbType
  2601. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  2602. if v.Unit != `无` {
  2603. item.Unit = v.Unit
  2604. }
  2605. item.UnitEn = v.UnitEn
  2606. item.StartDate = v.StartDate
  2607. item.EndDate = v.EndDate
  2608. item.ModifyTime = v.ModifyTime
  2609. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  2610. item.PredictChartColor = v.PredictChartColor
  2611. item.ClassifyId = v.ClassifyId
  2612. if chartInfoId <= 0 {
  2613. item.IsAxis = 1
  2614. item.LeadValue = 0
  2615. item.LeadUnit = ""
  2616. item.ChartEdbMappingId = 0
  2617. item.ChartInfoId = 0
  2618. item.IsOrder = false
  2619. item.EdbInfoType = 1
  2620. item.ChartStyle = ""
  2621. item.ChartColor = ""
  2622. item.ChartWidth = 0
  2623. item.MaxData = v.MaxValue
  2624. item.MinData = v.MinValue
  2625. } else {
  2626. item.IsAxis = v.IsAxis
  2627. item.EdbInfoType = v.EdbInfoType
  2628. item.LeadValue = v.LeadValue
  2629. item.LeadUnit = v.LeadUnit
  2630. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  2631. item.ChartEdbMappingId = v.ChartEdbMappingId
  2632. item.ChartInfoId = v.ChartInfoId
  2633. item.ChartStyle = v.ChartStyle
  2634. item.ChartColor = v.ChartColor
  2635. item.ChartWidth = v.ChartWidth
  2636. item.IsOrder = v.IsOrder
  2637. item.MaxData = v.MaxData
  2638. item.MinData = v.MinData
  2639. }
  2640. item.LatestValue = v.LatestValue
  2641. item.LatestDate = v.LatestDate
  2642. item.UniqueCode = v.UniqueCode
  2643. item.MoveLatestDate = v.LatestDate
  2644. item.EdbAliasName = v.EdbAliasName
  2645. item.IsConvert = v.IsConvert
  2646. item.ConvertType = v.ConvertType
  2647. item.ConvertValue = v.ConvertValue
  2648. item.ConvertUnit = v.ConvertUnit
  2649. item.ConvertEnUnit = v.ConvertEnUnit
  2650. var startDateReal string
  2651. var diffSeconds int64
  2652. if chartType == 2 { //季节性图
  2653. startDateReal = startDate
  2654. } else {
  2655. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  2656. var startTimeRealTemp time.Time
  2657. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  2658. switch v.LeadUnit {
  2659. case "天":
  2660. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  2661. case "月":
  2662. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  2663. case "季":
  2664. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  2665. case "周":
  2666. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  2667. case "年":
  2668. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  2669. }
  2670. if startTimeRealTemp.Before(startDateParse) {
  2671. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  2672. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  2673. } else {
  2674. startDateReal = startDate
  2675. diffSeconds = 0
  2676. }
  2677. // 预测指标的开始日期也要偏移
  2678. {
  2679. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  2680. if tmpErr != nil {
  2681. err = tmpErr
  2682. return
  2683. }
  2684. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  2685. if tmpErr != nil {
  2686. err = tmpErr
  2687. return
  2688. }
  2689. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  2690. }
  2691. } else {
  2692. startDateReal = startDate
  2693. }
  2694. }
  2695. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  2696. calendarPreYear := 0
  2697. if calendar == "农历" {
  2698. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  2699. if err != nil {
  2700. fmt.Println("time.Parse:" + err.Error())
  2701. }
  2702. calendarPreYear = newStartDateReal.Year() - 1
  2703. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  2704. startDateReal = newStartDateReal.Format(utils.FormatDate)
  2705. }
  2706. dataList := make([]*data_manage.EdbDataList, 0)
  2707. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  2708. //fmt.Println("calendarPreYear:", calendarPreYear)
  2709. //var newEdbInfo *data_manage.EdbInfo
  2710. switch v.EdbInfoCategoryType {
  2711. case 0:
  2712. dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
  2713. case 1:
  2714. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
  2715. default:
  2716. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  2717. }
  2718. if err != nil {
  2719. return
  2720. }
  2721. if v.IsConvert == 1 {
  2722. switch v.ConvertType {
  2723. case 1:
  2724. for i, data := range dataList {
  2725. dataList[i].Value = data.Value * v.ConvertValue
  2726. }
  2727. item.MaxData = item.MaxData * v.ConvertValue
  2728. item.MinData = item.MinData * v.ConvertValue
  2729. case 2:
  2730. for i, data := range dataList {
  2731. dataList[i].Value = data.Value / v.ConvertValue
  2732. }
  2733. item.MaxData = item.MaxData / v.ConvertValue
  2734. item.MinData = item.MinData / v.ConvertValue
  2735. case 3:
  2736. for i, data := range dataList {
  2737. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  2738. }
  2739. item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  2740. item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  2741. }
  2742. }
  2743. edbDataListMap[v.EdbInfoId] = dataList
  2744. if diffSeconds != 0 && v.EdbInfoType == 0 {
  2745. dataListLen := len(dataList)
  2746. for i := 0; i < dataListLen; i++ {
  2747. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  2748. }
  2749. }
  2750. if chartType == 2 {
  2751. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  2752. if tmpErr != nil {
  2753. //item.DataList = dataList
  2754. item.IsNullData = true
  2755. edbList = append(edbList, item)
  2756. continue
  2757. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  2758. return
  2759. }
  2760. if calendar == "农历" {
  2761. if len(dataList) <= 0 {
  2762. result := new(data_manage.EdbDataResult)
  2763. item.DataList = result
  2764. } else {
  2765. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  2766. if tmpErr != nil {
  2767. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  2768. return
  2769. }
  2770. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  2771. if tErr != nil {
  2772. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  2773. return
  2774. }
  2775. item.DataList = quarterDataList
  2776. }
  2777. } else {
  2778. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  2779. if tErr != nil {
  2780. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  2781. return
  2782. }
  2783. item.DataList = quarterDataList
  2784. }
  2785. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  2786. //item.DataList = dataList
  2787. } else {
  2788. item.DataList = dataList
  2789. }
  2790. edbList = append(edbList, item)
  2791. }
  2792. return
  2793. }