edb_info.go 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/cache"
  6. "eta/eta_api/models"
  7. "eta/eta_api/models/data_manage"
  8. "eta/eta_api/models/data_manage/excel"
  9. "eta/eta_api/models/system"
  10. "eta/eta_api/services/alarm_msg"
  11. "eta/eta_api/services/data/data_manage_permission"
  12. "eta/eta_api/services/elastic"
  13. "eta/eta_api/utils"
  14. "fmt"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. // EdbInfoRefreshAllFromBaseV2 全部刷新指标(切换到edb_lib服务)
  21. func EdbInfoRefreshAllFromBaseV2(edbInfoId int, refreshAll, isRefreshTop bool) (err error, isAsync bool) {
  22. var errmsg string
  23. defer func() {
  24. if err != nil {
  25. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  26. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  27. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  28. }
  29. }()
  30. err, isAsync = EdbInfoRefreshAllFromBaseV3([]int{edbInfoId}, refreshAll, false, isRefreshTop)
  31. return
  32. }
  33. // EdbInfoRefreshAllFromBaseV3Bak 全部刷新指标(切换到edb_lib服务)
  34. // @author Roc
  35. // @datetime 2022-09-16 11:04:44
  36. // @description 将原有的单个指标刷新,调整为批量多个指标刷新
  37. // DeprecatedTime 2023-10-23 09:38:19废弃
  38. // Deprecated
  39. func EdbInfoRefreshAllFromBaseV3Bak(edbInfoIdList []int, refreshAll, isSync bool) (err error, isAsync bool) {
  40. var errmsg string
  41. defer func() {
  42. if err != nil {
  43. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  44. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  45. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  46. }
  47. }()
  48. // 获取关联的基础指标
  49. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr, err, errmsg := getRefreshEdbInfoListByIds(edbInfoIdList)
  50. if err != nil {
  51. return
  52. }
  53. // 需要刷新的指标数量
  54. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  55. if totalEdbInfo <= 20 || isSync {
  56. err = edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  57. } else {
  58. isAsync = true
  59. go edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  60. }
  61. return
  62. }
  63. // EdbInfoRefreshAllFromBaseV3
  64. //
  65. // @Description: 全部刷新指标(切换到edb_lib服务)
  66. // @author: Roc
  67. // @datetime 2023-10-23 09:57:55
  68. // @param edbInfoIdList []int
  69. // @param refreshAll bool
  70. // @param isSync bool 是否同步执行
  71. // @param isRefreshTop bool 是否刷新上面的指标
  72. // @return err error
  73. // @return isAsync bool 是否异步刷新
  74. func EdbInfoRefreshAllFromBaseV3(edbInfoIdList []int, refreshAll, isSync, isRefreshTop bool) (err error, isAsync bool) {
  75. var errmsg string
  76. defer func() {
  77. if err != nil {
  78. fmt.Println("EdbInfoRefreshAllFromBaseV3 Err:" + err.Error() + ";errmsg:" + errmsg)
  79. go alarm_msg.SendAlarmMsg("EdbInfoRefreshAllFromBaseV3,Err"+err.Error()+";errMsg:"+errmsg, 3)
  80. }
  81. }()
  82. var newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo
  83. var newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo
  84. var calculateArr, predictCalculateArr []int
  85. // 获取关联指标
  86. if isRefreshTop {
  87. // 获取所有关联的指标,上下所有的指标
  88. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr, err, errmsg = getRefreshEdbInfoListByIds(edbInfoIdList)
  89. if err != nil {
  90. return
  91. }
  92. } else {
  93. // 获取溯源的指标
  94. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr = getEdbInfoIdList(edbInfoIdList)
  95. }
  96. // 需要刷新的指标数量
  97. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr) + len(newBasePredictEdbInfoArr)
  98. if totalEdbInfo == 0 {
  99. return
  100. }
  101. if totalEdbInfo <= 20 || isSync {
  102. err = edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  103. } else {
  104. isAsync = true
  105. go edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  106. }
  107. return
  108. }
  109. // getEdbInfoIdList
  110. // @Description: 获取待更新的指标列表
  111. // @author: Roc
  112. // @datetime 2023-12-01 16:19:42
  113. // @param edbInfoIdList []int
  114. // @return newBaseEdbInfoArr []*data_manage.EdbInfo
  115. // @return newBasePredictEdbInfoArr []*data_manage.EdbInfo
  116. // @return newCalculateMap map[int]*data_manage.EdbInfo
  117. // @return newPredictCalculateMap map[int]*data_manage.EdbInfo
  118. // @return calculateArr []int
  119. // @return predictCalculateArr []int
  120. func getEdbInfoIdList(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
  121. traceEdbInfoList, err := TraceEdbInfoByEdbInfoIdList(edbInfoIdList)
  122. if err != nil {
  123. return
  124. }
  125. // existEdbInfoIdMap 已经处理了的指标id map
  126. existEdbInfoIdMap := make(map[int]int)
  127. // 基础指标
  128. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  129. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  130. newBaseMap := make(map[int]*data_manage.EdbInfo)
  131. newPredictBaseMap := make(map[int]*data_manage.EdbInfo)
  132. // 计算指标
  133. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  134. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  135. calculateArr = make([]int, 0)
  136. predictCalculateArr = make([]int, 0)
  137. // 获取关联指标
  138. for _, traceEdbInfo := range traceEdbInfoList {
  139. tmpBaseEdbInfoArr, tmpBasePredictEdbInfoArr, tmpCalculateMap, tmpPredictCalculateMap, _, _ := getRefreshEdbInfoListByTraceEdbInfo(traceEdbInfo, existEdbInfoIdMap)
  140. // 普通基础指标
  141. for _, edbInfo := range tmpBaseEdbInfoArr {
  142. if _, ok := newBaseMap[edbInfo.EdbInfoId]; !ok {
  143. newBaseMap[edbInfo.EdbInfoId] = edbInfo
  144. if edbInfo.NoUpdate == 1 {
  145. continue
  146. }
  147. newBaseEdbInfoArr = append(newBaseEdbInfoArr, edbInfo)
  148. }
  149. }
  150. // 预测基础指标
  151. for _, edbInfo := range tmpBasePredictEdbInfoArr {
  152. if _, ok := newPredictBaseMap[edbInfo.EdbInfoId]; !ok {
  153. newPredictBaseMap[edbInfo.EdbInfoId] = edbInfo
  154. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, edbInfo)
  155. }
  156. }
  157. // 普通计算指标
  158. for _, edbInfo := range tmpCalculateMap {
  159. if _, ok := newCalculateMap[edbInfo.EdbInfoId]; !ok {
  160. if edbInfo.NoUpdate == 1 {
  161. continue
  162. }
  163. newCalculateMap[edbInfo.EdbInfoId] = edbInfo
  164. calculateArr = append(calculateArr, edbInfo.EdbInfoId)
  165. }
  166. }
  167. // 预测计算指标
  168. for _, edbInfo := range tmpPredictCalculateMap {
  169. if _, ok := newPredictCalculateMap[edbInfo.EdbInfoId]; !ok {
  170. newPredictCalculateMap[edbInfo.EdbInfoId] = edbInfo
  171. predictCalculateArr = append(predictCalculateArr, edbInfo.EdbInfoId)
  172. }
  173. }
  174. }
  175. // 普通计算指标的id
  176. sort.Ints(calculateArr)
  177. // 预测计算指标的id
  178. sort.Ints(predictCalculateArr)
  179. return
  180. }
  181. // getRefreshEdbInfoListByTraceEdbInfo根据溯源获取关联指标
  182. func getRefreshEdbInfoListByTraceEdbInfo(traceEdbInfo data_manage.TraceEdbInfoResp, existEdbInfoIdMap map[int]int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
  183. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  184. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  185. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  186. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  187. calculateArr = make([]int, 0)
  188. predictCalculateArr = make([]int, 0)
  189. _, ok := existEdbInfoIdMap[traceEdbInfo.EdbInfoId]
  190. if ok {
  191. return
  192. }
  193. existEdbInfoIdMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfoId
  194. switch traceEdbInfo.EdbInfoType {
  195. //0-普通指标; 1-预测指标
  196. case 0: // 0-普通指标
  197. if traceEdbInfo.EdbType == 1 { //1-基础指标
  198. newBaseEdbInfoArr = append(newBaseEdbInfoArr, traceEdbInfo.EdbInfo)
  199. } else if traceEdbInfo.EdbType == 2 { //2-计算指标
  200. newCalculateMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfo
  201. calculateArr = append(calculateArr, traceEdbInfo.EdbInfoId)
  202. }
  203. case 1: // 1-预测指标
  204. if traceEdbInfo.EdbType == 1 { //1-基础指标
  205. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, traceEdbInfo.EdbInfo)
  206. } else if traceEdbInfo.EdbType == 2 { //2-计算指标
  207. newPredictCalculateMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfo
  208. predictCalculateArr = append(predictCalculateArr, traceEdbInfo.EdbInfoId)
  209. }
  210. }
  211. if traceEdbInfo.Child != nil && len(traceEdbInfo.Child) > 0 {
  212. for _, v := range traceEdbInfo.Child {
  213. tmpBaseEdbInfoArr, tmpPredictEdbInfoArr, tmpCalculateMap, tmpPredictCalculateMap, tmpCalculateArr, tmpPredictCalculateArr := getRefreshEdbInfoListByTraceEdbInfo(v, existEdbInfoIdMap)
  214. newBaseEdbInfoArr = append(newBaseEdbInfoArr, tmpBaseEdbInfoArr...)
  215. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, tmpPredictEdbInfoArr...)
  216. for k, tmpEdbInfo := range tmpCalculateMap {
  217. newCalculateMap[k] = tmpEdbInfo
  218. }
  219. for k, tmpEdbInfo := range tmpPredictCalculateMap {
  220. newPredictCalculateMap[k] = tmpEdbInfo
  221. }
  222. calculateArr = append(calculateArr, tmpCalculateArr...)
  223. predictCalculateArr = append(predictCalculateArr, tmpPredictCalculateArr...)
  224. }
  225. }
  226. return
  227. }
  228. func edbInfoRefreshAll(refreshAll bool, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) (err error) {
  229. var errmsg string
  230. defer func() {
  231. if err != nil {
  232. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  233. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  234. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  235. }
  236. }()
  237. var startDate string
  238. for _, bv := range newBaseEdbInfoArr {
  239. //source := bv.Source
  240. //edbInfoId := bv.EdbInfoId
  241. //edbCode := bv.EdbCode
  242. if bv.StartDate == "0000-00-00" {
  243. continue
  244. }
  245. //开始时间
  246. startDate = ``
  247. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  248. sTime, err := time.Parse(utils.FormatDate, bv.StartDate)
  249. if err != nil {
  250. return err
  251. }
  252. startDate = sTime.Format(utils.FormatDate)
  253. } else {
  254. sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
  255. if err != nil {
  256. return err
  257. }
  258. frequency := bv.Frequency
  259. var limitDay int
  260. switch frequency {
  261. case "日度":
  262. limitDay = utils.DATA_START_REFRESH_LIMIT
  263. case "周度":
  264. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  265. case "月度":
  266. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  267. case "季度":
  268. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  269. case "年度":
  270. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  271. default:
  272. limitDay = utils.DATA_START_REFRESH_LIMIT
  273. }
  274. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  275. }
  276. result, err := RefreshEdbData(bv.EdbInfoId, bv.Source, bv.SubSource, bv.EdbCode, startDate)
  277. if err != nil {
  278. fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err", time.Now())
  279. errmsg = "RefreshBaseEdbData Err:" + err.Error()
  280. return err
  281. }
  282. if result.Ret != 200 {
  283. fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  284. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  285. return fmt.Errorf("刷新失败, err:", errmsg)
  286. }
  287. //maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
  288. //if err != nil {
  289. // if err.Error() == utils.ErrNoRow() { //找不到数据,那么就进入到下一条数据做处理
  290. // continue
  291. // }
  292. // return err
  293. //}
  294. //if maxAndMinItem != nil {
  295. // err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  296. // if err != nil {
  297. // return err
  298. // }
  299. //}
  300. fmt.Println("end newBaseEdbInfoArr:", bv, time.Now())
  301. }
  302. //刷新相关普通计算指标
  303. for _, v := range calculateArr {
  304. edbInfo := newCalculateMap[v]
  305. if edbInfo == nil {
  306. return err
  307. }
  308. //计算指标就不用开始时间了吧,因为存在说开始时间变更的情况
  309. startDate = ``
  310. if !refreshAll { //如果不是全部更新,那么以指标的开始时间作为刷新
  311. startDate = edbInfo.StartDate
  312. source := edbInfo.Source
  313. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  314. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  315. } else {
  316. if source == utils.DATA_SOURCE_CALCULATE {
  317. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  318. if err != nil {
  319. return err
  320. }
  321. frequency := edbInfo.Frequency
  322. var limitDay int
  323. switch frequency {
  324. case "日度":
  325. limitDay = utils.DATA_START_REFRESH_LIMIT
  326. case "周度":
  327. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  328. case "月度":
  329. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  330. case "季度":
  331. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  332. case "年度":
  333. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  334. default:
  335. limitDay = utils.DATA_START_REFRESH_LIMIT
  336. }
  337. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  338. }
  339. }
  340. }
  341. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  342. if err != nil {
  343. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  344. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  345. return err
  346. }
  347. if result.Ret != 200 {
  348. fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  349. errmsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  350. return fmt.Errorf("刷新失败")
  351. }
  352. }
  353. //刷新相关预测计算指标
  354. for _, v := range predictCalculateArr {
  355. edbInfo := newPredictCalculateMap[v]
  356. if edbInfo == nil {
  357. return err
  358. }
  359. startDate = edbInfo.StartDate
  360. source := edbInfo.Source
  361. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  362. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  363. } else {
  364. if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  365. startDate = ``
  366. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  367. startDate = edbInfo.StartDate
  368. } else {
  369. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  370. if err != nil {
  371. return err
  372. }
  373. frequency := edbInfo.Frequency
  374. var limitDay int
  375. switch frequency {
  376. case "日度":
  377. limitDay = utils.DATA_START_REFRESH_LIMIT
  378. case "周度":
  379. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  380. case "月度":
  381. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  382. case "季度":
  383. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  384. case "年度":
  385. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  386. default:
  387. limitDay = utils.DATA_START_REFRESH_LIMIT
  388. }
  389. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  390. }
  391. }
  392. }
  393. result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  394. if err != nil {
  395. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  396. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  397. return err
  398. }
  399. if result.Ret != 200 {
  400. fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  401. errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  402. return fmt.Errorf("刷新失败")
  403. }
  404. }
  405. // 4、更新动态环差值
  406. {
  407. predictEdbInfoIdList := make([]int, 0)
  408. for _, v := range newBasePredictEdbInfoArr {
  409. predictEdbInfoIdList = append(predictEdbInfoIdList, v.EdbInfoId)
  410. }
  411. fmt.Println("predictEdbConfCalculateMappingDetailList:", predictEdbInfoIdList)
  412. predictEdbConfCalculateMappingDetailList, err := data_manage.GetPredictEdbConfCalculateMappingDetailListByEdbInfoId(predictEdbInfoIdList)
  413. if err != nil {
  414. errmsg = fmt.Sprint("更新动态环差值时,获取数据失败:" + err.Error())
  415. return fmt.Errorf("刷新失败, err:", errmsg)
  416. }
  417. for _, bv := range predictEdbConfCalculateMappingDetailList {
  418. result, err := RefreshEdbData(bv.EdbInfoId, utils.DATA_SOURCE_PREDICT, utils.DATA_SUB_SOURCE_EDB, bv.EdbCode, "")
  419. if err != nil {
  420. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err", time.Now())
  421. errmsg = "RefreshBasePredictEdbData Err:" + err.Error()
  422. return err
  423. }
  424. if result.Ret != 200 {
  425. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  426. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  427. return fmt.Errorf("刷新失败, err:%s", errmsg)
  428. }
  429. fmt.Println("end predictEdbConfCalculateMappingDetailList:", bv, time.Now())
  430. }
  431. }
  432. //5、再次刷新相关预测计算指标
  433. for _, v := range predictCalculateArr {
  434. edbInfo := newPredictCalculateMap[v]
  435. if edbInfo == nil {
  436. return err
  437. }
  438. startDate = edbInfo.StartDate
  439. source := edbInfo.Source
  440. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  441. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  442. } else {
  443. if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  444. startDate = ``
  445. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  446. startDate = edbInfo.StartDate
  447. } else {
  448. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  449. if err != nil {
  450. return err
  451. }
  452. frequency := edbInfo.Frequency
  453. var limitDay int
  454. switch frequency {
  455. case "日度":
  456. limitDay = utils.DATA_START_REFRESH_LIMIT
  457. case "周度":
  458. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  459. case "月度":
  460. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  461. case "季度":
  462. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  463. case "年度":
  464. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  465. default:
  466. limitDay = utils.DATA_START_REFRESH_LIMIT
  467. }
  468. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  469. }
  470. }
  471. }
  472. result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  473. if err != nil {
  474. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  475. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  476. return err
  477. }
  478. if result.Ret != 200 {
  479. fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  480. errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  481. return fmt.Errorf("刷新失败")
  482. }
  483. }
  484. return err
  485. }
  486. // EdbInfoRefresh 刷新单个指标,不会刷新关联指标
  487. func EdbInfoRefresh(edbInfo *data_manage.EdbInfoView, refreshAll bool) (err error) {
  488. var errmsg string
  489. defer func() {
  490. if err != nil {
  491. fmt.Println("EdbInfoRefresh Err:" + err.Error() + ";errmsg:" + errmsg)
  492. go alarm_msg.SendAlarmMsg("EdbInfoRefresh,Err"+err.Error()+";errMsg:"+errmsg, 3)
  493. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  494. }
  495. }()
  496. switch edbInfo.EdbInfoType {
  497. //指标类型,0:普通指标,1:预测指标
  498. case 0:
  499. if edbInfo.StartDate == "0000-00-00" {
  500. return
  501. }
  502. //开始时间
  503. startDate := ``
  504. if edbInfo.EdbType == 1 {
  505. // 基础指标
  506. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  507. sTime, err := time.Parse(utils.FormatDate, edbInfo.StartDate)
  508. if err != nil {
  509. return err
  510. }
  511. startDate = sTime.Format(utils.FormatDate)
  512. } else {
  513. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  514. if err != nil {
  515. return err
  516. }
  517. frequency := edbInfo.Frequency
  518. var limitDay int
  519. switch frequency {
  520. case "日度":
  521. limitDay = utils.DATA_START_REFRESH_LIMIT
  522. case "周度":
  523. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  524. case "月度":
  525. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  526. case "季度":
  527. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  528. case "年度":
  529. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  530. default:
  531. limitDay = utils.DATA_START_REFRESH_LIMIT
  532. }
  533. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  534. }
  535. result, err := RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, startDate)
  536. if err != nil {
  537. fmt.Println(edbInfo.EdbInfoId, "RefreshBaseEdbData err", time.Now())
  538. errmsg = "RefreshBaseEdbData Err:" + err.Error()
  539. return err
  540. }
  541. if result.Ret != 200 {
  542. fmt.Println(edbInfo.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  543. errmsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  544. return fmt.Errorf("刷新失败, err:", errmsg)
  545. }
  546. } else { //计算指标
  547. //计算指标就不用开始时间了吧,因为存在说开始时间变更的情况
  548. if !refreshAll { //如果不是全部更新,那么以指标的开始时间作为刷新
  549. startDate = edbInfo.StartDate
  550. source := edbInfo.Source
  551. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  552. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  553. } else {
  554. if source == utils.DATA_SOURCE_CALCULATE {
  555. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  556. if err != nil {
  557. return err
  558. }
  559. frequency := edbInfo.Frequency
  560. var limitDay int
  561. switch frequency {
  562. case "日度":
  563. limitDay = utils.DATA_START_REFRESH_LIMIT
  564. case "周度":
  565. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  566. case "月度":
  567. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  568. case "季度":
  569. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  570. case "年度":
  571. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  572. default:
  573. limitDay = utils.DATA_START_REFRESH_LIMIT
  574. }
  575. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  576. }
  577. }
  578. }
  579. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  580. if err != nil {
  581. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
  582. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  583. return err
  584. }
  585. if result.Ret != 200 {
  586. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  587. errmsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  588. return fmt.Errorf("刷新失败")
  589. }
  590. }
  591. case 1:
  592. startDate := edbInfo.StartDate
  593. var result *models.BaseResponse
  594. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  595. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  596. } else {
  597. startDate = ``
  598. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  599. startDate = edbInfo.StartDate
  600. } else {
  601. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  602. if err != nil {
  603. return err
  604. }
  605. frequency := edbInfo.Frequency
  606. var limitDay int
  607. switch frequency {
  608. case "日度":
  609. limitDay = utils.DATA_START_REFRESH_LIMIT
  610. case "周度":
  611. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  612. case "月度":
  613. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  614. case "季度":
  615. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  616. case "年度":
  617. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  618. default:
  619. limitDay = utils.DATA_START_REFRESH_LIMIT
  620. }
  621. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  622. }
  623. }
  624. if edbInfo.EdbType == 1 { //基础指标
  625. result, err = RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  626. } else {
  627. // 预测计算指标
  628. // 4、更新动态环差值
  629. {
  630. predictEdbConfCalculateMappingDetailList, err := data_manage.GetPredictEdbConfCalculateMappingDetailListByEdbInfoId([]int{edbInfo.EdbInfoId})
  631. if err != nil {
  632. errmsg = fmt.Sprint("更新动态环差值时,获取数据失败:" + err.Error())
  633. return fmt.Errorf("刷新失败, err:", errmsg)
  634. }
  635. for _, bv := range predictEdbConfCalculateMappingDetailList {
  636. result, err := RefreshEdbData(bv.EdbInfoId, utils.DATA_SOURCE_PREDICT, utils.DATA_SUB_SOURCE_EDB, bv.EdbCode, "")
  637. if err != nil {
  638. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err", time.Now())
  639. errmsg = "RefreshBasePredictEdbData Err:" + err.Error()
  640. return err
  641. }
  642. if result.Ret != 200 {
  643. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  644. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  645. return fmt.Errorf("刷新失败, err:%s", errmsg)
  646. }
  647. fmt.Println("end predictEdbConfCalculateMappingDetailList:", bv, time.Now())
  648. }
  649. }
  650. }
  651. if err != nil {
  652. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
  653. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  654. return err
  655. }
  656. if result.Ret != 200 {
  657. fmt.Println(edbInfo.EdbInfoId, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  658. errmsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  659. return fmt.Errorf("刷新失败")
  660. }
  661. }
  662. return err
  663. }
  664. // AddEdbInfo 新增手工指标数据
  665. func AddEdbInfo(secName, unit, frequency, noticeTime, mobile string, classifyId, userId int, userName string) (err error) {
  666. tradeCode, err := models.GetMaxTradeCode()
  667. if err != nil {
  668. return
  669. }
  670. if tradeCode == "" {
  671. err = errors.New("最大编码为空")
  672. return
  673. }
  674. maxTradeCode, err := utils.GetMaxTradeCode(tradeCode)
  675. if err != nil {
  676. err = errors.New("获取编码信息失败,Err:" + err.Error())
  677. return
  678. }
  679. if maxTradeCode == "" {
  680. err = errors.New("编码为空,请联系技术")
  681. return
  682. }
  683. //判断指标名称是否为空
  684. if secName == "" {
  685. err = errors.New("指标名称不能为空")
  686. return
  687. }
  688. item, err := models.GetEdbinfoBySecName(secName)
  689. if err != nil && err.Error() != utils.ErrNoRow() {
  690. err = errors.New("获取信息失败,Err:" + err.Error())
  691. return
  692. }
  693. if item != nil {
  694. err = errors.New("指标名称已存在")
  695. return
  696. }
  697. // 先删除该指标编码的历史数据(避免上海删除指标时,没有删除该指标数据)
  698. _ = models.DeleteAllEdbData(maxTradeCode)
  699. // 先删除历史的操作日志
  700. _ = models.DelEdbinfoOpRecordByTradeCode(maxTradeCode)
  701. // 再次新增指标
  702. err = models.AddEdbinfo(maxTradeCode, secName, unit, "手动", frequency, noticeTime, classifyId, userId, userName)
  703. if err != nil {
  704. err = errors.New("新增失败,Err:" + err.Error())
  705. return
  706. }
  707. err = models.AddEdbinfoUser(maxTradeCode, mobile)
  708. return
  709. }
  710. //全部刷新指标
  711. //func EdbInfoRefreshAllFromBase(edbInfoId int) (err error) {
  712. // var errmsg string
  713. // defer func() {
  714. // if err != nil {
  715. // fmt.Println("EdbInfoRefreshAllFromBase Err:" + err.Error() + ";errmsg:" + errmsg)
  716. // go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  717. // }
  718. // }()
  719. //
  720. // calculateList, err := data_manage.GetEdbInfoAllCalculate(edbInfoId)
  721. // if err != nil && err.Error() != utils.ErrNoRow() {
  722. // return errors.New("GetEdbInfoAllCalculate Err:" + err.Error())
  723. // }
  724. // baseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  725. // calculateInfoArr := make([]*data_manage.EdbInfo, 0)
  726. // baseEdbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  727. // if err != nil {
  728. // return errors.New("GetEdbInfoAllCalGetEdbInfoByIdr:" + err.Error())
  729. // }
  730. // baseEdbInfoArr = append(baseEdbInfoArr, baseEdbInfo)
  731. //
  732. // for _, v := range calculateList {
  733. // getBaseEdbInfoArr, getCalculateInfoArr, err := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  734. // if err != nil {
  735. // fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  736. // errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  737. // return err
  738. // }
  739. // baseEdbInfoArr = append(baseEdbInfoArr, getBaseEdbInfoArr...)
  740. // calculateInfoArr = append(calculateInfoArr, getCalculateInfoArr...)
  741. // calculateInfoArr = append(calculateInfoArr, v)
  742. // }
  743. // //取重
  744. // newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  745. // baseMap := make(map[int]int)
  746. // for _, v := range baseEdbInfoArr {
  747. // if _, ok := baseMap[v.EdbInfoId]; !ok {
  748. // newBaseEdbInfoArr = append(newBaseEdbInfoArr, v)
  749. // }
  750. // baseMap[v.EdbInfoId] = v.EdbInfoId
  751. // }
  752. //
  753. // var calculateArr []int
  754. // newCalculateMap := make(map[int]*data_manage.EdbInfo)
  755. // for _, v := range calculateInfoArr {
  756. // if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  757. // calculateArr = append(calculateArr, v.EdbInfoId)
  758. // }
  759. // newCalculateMap[v.EdbInfoId] = v
  760. // }
  761. // sort.Ints(calculateArr)
  762. // var startDate string
  763. // for _, bv := range newBaseEdbInfoArr {
  764. // source := bv.Source
  765. // edbInfoId := bv.EdbInfoId
  766. // edbCode := bv.EdbCode
  767. //
  768. // sTime, err := time.Parse(utils.FormatDate, bv.StartDate)
  769. // if err != nil {
  770. // return err
  771. // }
  772. // startDate = sTime.Format(utils.FormatDate)
  773. //
  774. // if source == utils.DATA_SOURCE_THS {
  775. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  776. // err = data_manage.RefreshAllEdbDataByThs(edbInfoId, source, edbCode, startDate, endDate)
  777. // if err != nil {
  778. // errmsg = "RefreshAllEdbDataByThs Err:" + err.Error()
  779. // }
  780. // } else if source == utils.DATA_SOURCE_WIND {
  781. // endDate := time.Now().Format(utils.FormatDate)
  782. // err = data_manage.RefreshAllEdbDataByWind(edbInfoId, source, edbCode, startDate, endDate)
  783. // if err != nil {
  784. // errmsg = "RefreshAllEdbDataByWind Err:" + err.Error()
  785. // }
  786. // } else if source == utils.DATA_SOURCE_PB {
  787. // startDate := sTime.Format(utils.FormatDateUnSpace)
  788. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDateUnSpace)
  789. // err = data_manage.RefreshAllEdbDataByPb(edbInfoId, source, edbCode, startDate, endDate)
  790. // if err != nil {
  791. // errmsg = "RefreshAllEdbDataByPb Err:" + err.Error()
  792. // }
  793. // } else if source == utils.DATA_SOURCE_MANUAL {
  794. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  795. // err = data_manage.RefreshAllEdbDataByManual(edbInfoId, source, edbCode, startDate, endDate)
  796. // if err != nil {
  797. // errmsg = "RefreshAllEdbDataByManual Err:" + err.Error()
  798. // }
  799. // } else if source == utils.DATA_SOURCE_LZ {
  800. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  801. // err = data_manage.RefreshAllEdbDataByLz(edbInfoId, source, edbCode, startDate, endDate)
  802. // if err != nil {
  803. // errmsg = "RefreshAllEdbDataByLz Err:" + err.Error()
  804. // }
  805. // } else if source == utils.DATA_SOURCE_YS {
  806. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  807. // err = data_manage.RefreshAllEdbDataByYs(edbInfoId, source, edbCode, startDate, endDate)
  808. // if err != nil {
  809. // errmsg = "RefreshAllEdbDataByYs Err:" + err.Error()
  810. // }
  811. // } else if source == utils.DATA_SOURCE_GL {
  812. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  813. // err = data_manage.RefreshAllEdbDataByGl(edbInfoId, source, edbCode, startDate, endDate)
  814. // if err != nil {
  815. // errmsg = "RefreshAllEdbDataByGl Err:" + err.Error()
  816. // }
  817. // } else if source == utils.DATA_SOURCE_ZZ {
  818. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  819. // err = data_manage.RefreshAllEdbDataByZz(edbInfoId, source, edbCode, startDate, endDate)
  820. // if err != nil {
  821. // errmsg = "RefreshAllEdbDataByZz Err:" + err.Error()
  822. // }
  823. // } else if source == utils.DATA_SOURCE_DL {
  824. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  825. // err = data_manage.RefreshAllEdbDataByDl(edbInfoId, source, edbCode, startDate, endDate)
  826. // if err != nil {
  827. // errmsg = "RefreshAllEdbDataByDl Err:" + err.Error()
  828. // }
  829. // } else if source == utils.DATA_SOURCE_SH {
  830. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  831. // err = data_manage.RefreshAllEdbDataBySh(edbInfoId, source, edbCode, startDate, endDate)
  832. // if err != nil {
  833. // errmsg = "RefreshAllEdbDataBySh Err:" + err.Error()
  834. // }
  835. // } else if source == utils.DATA_SOURCE_CFFEX {
  836. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  837. // err = data_manage.RefreshAllEdbDataByCffex(edbInfoId, source, edbCode, startDate, endDate)
  838. // if err != nil {
  839. // errmsg = "RefreshAllEdbDataByCffex Err:" + err.Error()
  840. // }
  841. // } else if source == utils.DATA_SOURCE_SHFE {
  842. // endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  843. // err = data_manage.RefreshAllEdbDataByShfe(edbInfoId, source, edbCode, startDate, endDate)
  844. // if err != nil {
  845. // errmsg = "RefreshAllEdbDataByShfe Err:" + err.Error()
  846. // }
  847. // }
  848. //
  849. // maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
  850. // if err != nil {
  851. // return err
  852. // }
  853. // if maxAndMinItem != nil {
  854. // err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  855. // if err != nil {
  856. // return err
  857. // }
  858. // }
  859. // fmt.Println("end newBaseEdbInfoArr:", bv, time.Now())
  860. // }
  861. //
  862. // endDate := "" //计算指标不限制日期
  863. // //刷新相关计算指标
  864. // for _, v := range calculateArr {
  865. // edbInfo := newCalculateMap[v]
  866. // if edbInfo == nil {
  867. // return err
  868. // }
  869. // edbInfoId := edbInfo.EdbInfoId
  870. // source := edbInfo.Source
  871. // edbCode := edbInfo.EdbCode
  872. //
  873. // if source == utils.DATA_SOURCE_CALCULATE {
  874. // startDate = edbInfo.StartDate
  875. // sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  876. // if err != nil {
  877. // return err
  878. // }
  879. // startDate = sTime.Format(utils.FormatDate)
  880. //
  881. // var edbInfoIdBytes []string
  882. // calculateMap, err := data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
  883. // if err != nil {
  884. // errmsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
  885. // return err
  886. // }
  887. // var formulaStr string
  888. // edbInfoList := make([]*data_manage.EdbInfo, 0)
  889. //
  890. // for _, v := range calculateMap {
  891. // formulaStr += v.FromTag + ","
  892. // edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  893. // edbInfo, _ := data_manage.GetEdbInfoById(v.FromEdbInfoId)
  894. // edbInfoList = append(edbInfoList, edbInfo)
  895. // }
  896. // err = data_manage.RefreshAllCalculate(edbInfoList, int(edbInfoId), source, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, endDate, edbInfoIdBytes)
  897. // if err != nil {
  898. // errmsg = "RefreshCalculate Err:" + err.Error()
  899. // return err
  900. // }
  901. // } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY { //刷新累计值转月值
  902. // calculateLjzzy, err := data_manage.GetEdbInfoCalculateLjzzyDetail(edbInfoId)
  903. // if err != nil {
  904. // errmsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
  905. // return err
  906. // }
  907. // fromEdbInfo, err := data_manage.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
  908. // if err != nil {
  909. // errmsg = "GetEdbInfoById Err:" + err.Error()
  910. // return err
  911. // }
  912. // startDate = edbInfo.StartDate
  913. // endDate = time.Now().Format(utils.FormatDate)
  914. // err = data_manage.RefreshAllCalculateLjzzy(edbInfoId, source, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
  915. // if err != nil {
  916. // errmsg = "RefreshAllCalculateLjzzy Err:" + err.Error()
  917. // return err
  918. // }
  919. // } else if source == utils.DATA_SOURCE_CALCULATE_TBZ { //刷新同比值
  920. // calculateTbz, err := data_manage.GetEdbInfoCalculateTbzDetail(edbInfoId)
  921. // if err != nil {
  922. // errmsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  923. // return err
  924. // }
  925. // fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  926. // if err != nil {
  927. // errmsg = "GetEdbInfoById Err:" + err.Error()
  928. // return err
  929. // }
  930. // startDate = edbInfo.StartDate
  931. // endDate = time.Now().Format(utils.FormatDate)
  932. // err = data_manage.RefreshAllCalculateTbz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
  933. // if err != nil {
  934. // errmsg = "RefreshAllCalculateTbz Err:" + err.Error()
  935. // return err
  936. // }
  937. // } else if source == utils.DATA_SOURCE_CALCULATE_TCZ { //同差值
  938. // calculateTcz, err := data_manage.GetEdbInfoCalculateTczDetail(edbInfoId)
  939. // if err != nil {
  940. // errmsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  941. // return err
  942. // }
  943. // fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTcz.FromEdbInfoId)
  944. // if err != nil {
  945. // errmsg = "GetEdbInfoById Err:" + err.Error()
  946. // return err
  947. // }
  948. // startDate = edbInfo.StartDate
  949. // endDate = time.Now().Format(utils.FormatDate)
  950. // err = data_manage.RefreshAllCalculateTcz(edbInfoId, source, fromEdbInfo, calculateTcz.EdbCode, startDate, endDate)
  951. // if err != nil {
  952. // errmsg = "RefreshCalculateTcz Err:" + err.Error()
  953. // return err
  954. // }
  955. // } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS { //N数值移动平均计算
  956. // calculateNszydpjjs, err := data_manage.GetEdbInfoCalculateNszydpjjsDetail(edbInfoId)
  957. // if err != nil {
  958. // errmsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  959. // return err
  960. // }
  961. // fromEdbInfo, err := data_manage.GetEdbInfoById(calculateNszydpjjs.FromEdbInfoId)
  962. // if err != nil {
  963. // errmsg = "GetEdbInfoById Err:" + err.Error()
  964. // return err
  965. // }
  966. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  967. // startDate = edbInfo.StartDate
  968. // err = data_manage.RefreshAllCalculateNszydpjjs(edbInfoId, edbInfo.Source, formulaInt, fromEdbInfo, calculateNszydpjjs.EdbCode, startDate)
  969. // if err != nil {
  970. // errmsg = "RefreshCalculateNszydpjjs Err:" + err.Error()
  971. // return err
  972. // }
  973. // } else if source == utils.DATA_SOURCE_CALCULATE_HBZ { //刷新环比值
  974. // calculateTbz, err := data_manage.GetEdbInfoCalculateHbzDetail(edbInfoId)
  975. // if err != nil {
  976. // errmsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
  977. // return err
  978. // }
  979. // fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  980. // if err != nil {
  981. // errmsg = "GetEdbInfoById Err:" + err.Error()
  982. // return err
  983. // }
  984. // startDate = edbInfo.StartDate
  985. // endDate = time.Now().Format(utils.FormatDate)
  986. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  987. // err = data_manage.RefreshAllCalculateHbz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  988. // if err != nil {
  989. // errmsg = "RefreshAllCalculateHbz Err:" + err.Error()
  990. // return err
  991. // }
  992. // } else if source == utils.DATA_SOURCE_CALCULATE_HCZ { //刷新环差值
  993. // calculateTbz, err := data_manage.GetEdbInfoCalculateHczDetail(edbInfoId)
  994. // if err != nil {
  995. // errmsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
  996. // return err
  997. // }
  998. // fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  999. // if err != nil {
  1000. // errmsg = "GetEdbInfoById Err:" + err.Error()
  1001. // return err
  1002. // }
  1003. // startDate = edbInfo.StartDate
  1004. // endDate = time.Now().Format(utils.FormatDate)
  1005. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1006. // err = data_manage.RefreshAllCalculateHcz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1007. // if err != nil {
  1008. // errmsg = "RefreshAllCalculateHcz Err:" + err.Error()
  1009. // return err
  1010. // }
  1011. // } else if source == utils.DATA_SOURCE_CALCULATE_BP { //刷新变频
  1012. // calculateTbz, err := data_manage.GetEdbInfoCalculateBpDetail(edbInfoId)
  1013. // if err != nil {
  1014. // errmsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1015. // return err
  1016. // }
  1017. // fromEdbInfo, err := data_manage.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1018. // if err != nil {
  1019. // errmsg = "GetEdbInfoById Err:" + err.Error()
  1020. // return err
  1021. // }
  1022. // startDate = edbInfo.StartDate
  1023. // endDate = time.Now().Format(utils.FormatDate)
  1024. // err = data_manage.RefreshAllCalculateBp(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
  1025. // if err != nil {
  1026. // errmsg = "RefreshAllCalculateBp Err:" + err.Error()
  1027. // return err
  1028. // }
  1029. //<<<<<<< HEAD
  1030. // } else if source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT {
  1031. // calculate, err := data_manage.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1032. // if err != nil {
  1033. // errmsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1034. // return err
  1035. // }
  1036. // fromEdbInfo, err := data_manage.GetEdbInfoById(calculate.FromEdbInfoId)
  1037. // if err != nil {
  1038. // errmsg = "GetEdbInfoById Err:" + err.Error()
  1039. // return err
  1040. // }
  1041. // startDate = edbInfo.StartDate
  1042. // endDate = time.Now().Format(utils.FormatDate)
  1043. // formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1044. // err = data_manage.RefreshAllCalculateTimeShift(edbInfoId, source, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1045. // if err != nil {
  1046. // errmsg = "RefreshAllCalculateTimeShift Err:" + err.Error()
  1047. //=======
  1048. // } else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ { //刷新直接拼接
  1049. // err = data_manage.RefreshAllCalculateZjpj(edbInfo)
  1050. // if err != nil {
  1051. // errmsg = "RefreshAllCalculateBp Err:" + err.Error()
  1052. //>>>>>>> 884fd669cc9e91923781680e1890a519c012d38e
  1053. // return err
  1054. // }
  1055. // }
  1056. //
  1057. // maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
  1058. // if err != nil {
  1059. // return err
  1060. // }
  1061. //
  1062. // if maxAndMinItem != nil {
  1063. // err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  1064. // if err != nil {
  1065. // return err
  1066. // }
  1067. // }
  1068. // fmt.Println("end calculateArr:", v, time.Now())
  1069. // }
  1070. // return err
  1071. //}
  1072. //
  1073. //// AddEdbInfo 新增手工指标数据
  1074. //func AddEdbInfo(secName, unit, frequency, noticeTime, mobile string, classifyId, userId int) (err error) {
  1075. // tradeCode, err := models.GetMaxTradeCode()
  1076. // if err != nil {
  1077. // return
  1078. // }
  1079. //
  1080. // if tradeCode == "" {
  1081. // err = errors.New("最大编码为空")
  1082. // return
  1083. // }
  1084. // maxTradeCode, err := utils.GetMaxTradeCode(tradeCode)
  1085. // if err != nil {
  1086. // err = errors.New("获取编码信息失败,Err:" + err.Error())
  1087. // return
  1088. // }
  1089. // if maxTradeCode == "" {
  1090. // err = errors.New("编码为空,请联系技术")
  1091. // return
  1092. // }
  1093. // //判断指标名称是否为空
  1094. // if secName == "" {
  1095. // err = errors.New("指标名称不能为空")
  1096. // return
  1097. // }
  1098. // item, err := models.GetEdbinfoBySecName(secName)
  1099. // if err != nil && err.Error() != utils.ErrNoRow() {
  1100. // err = errors.New("获取信息失败,Err:" + err.Error())
  1101. // return
  1102. // }
  1103. // if item != nil {
  1104. // err = errors.New("指标名称已存在")
  1105. // return
  1106. // }
  1107. // err = models.AddEdbinfo(maxTradeCode, secName, unit, "手动", frequency, noticeTime, classifyId, userId)
  1108. // if err != nil {
  1109. // err = errors.New("新增失败,Err:" + err.Error())
  1110. // return
  1111. // }
  1112. // err = models.AddEdbinfoUser(maxTradeCode, mobile)
  1113. // return
  1114. //}
  1115. //
  1116. //
  1117. //
  1118. //
  1119. // AddOrEditAllEdbInfoToEs 修复ES中的指标
  1120. func AddOrEditAllEdbInfoToEs() {
  1121. //添加es
  1122. total, itemInfoList, _ := data_manage.GetEdbInfoFilterList("", []interface{}{}, 0, 100000)
  1123. for k, itemInfo := range itemInfoList {
  1124. elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  1125. fmt.Println("剩余", int(total)-k-1, "条数据")
  1126. }
  1127. }
  1128. // AddOrEditEdbInfoToEs 添加/修改ES中的指标
  1129. func AddOrEditEdbInfoToEs(edbInfoId int) {
  1130. //添加es
  1131. itemInfo, _ := data_manage.GetEdbInfoByCondition("AND edb_info_id=?", []interface{}{edbInfoId})
  1132. go elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  1133. }
  1134. // DeleteEdbInfoToEs 删除ES中的指标
  1135. func DeleteEdbInfoToEs(edbInfoId int) {
  1136. //添加es
  1137. go elastic.EsDeleteEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(edbInfoId))
  1138. }
  1139. // RefreshManualData 根据手工指标code刷新手工指标数据
  1140. func RefreshManualData(edbCode string) {
  1141. var err error
  1142. defer func() {
  1143. if err != nil {
  1144. go alarm_msg.SendAlarmMsg("导入手工数据后,根据手工指标code刷新手工指标数据失败提醒,Err"+err.Error(), 3)
  1145. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"导入手工数据后,根据手工指标code刷新手工指标数据失败提醒", "errmsg:"+err.Error(), utils.EmailSendToUsers)
  1146. }
  1147. }()
  1148. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MANUAL, edbCode)
  1149. if err != nil {
  1150. if err.Error() == utils.ErrNoRow() {
  1151. err = nil
  1152. }
  1153. return
  1154. }
  1155. // 刷新指标库
  1156. err, _ = EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, true, true)
  1157. return
  1158. }
  1159. // 获取频度的英文版
  1160. func GetFrequencyEn(frequency string) (frequencyEn string) {
  1161. switch frequency {
  1162. case "日度":
  1163. frequencyEn = "day"
  1164. return
  1165. case "周度":
  1166. frequencyEn = "week"
  1167. return
  1168. case "旬度":
  1169. frequencyEn = "ten days"
  1170. return
  1171. case "月度":
  1172. frequencyEn = "month"
  1173. return
  1174. case "季度":
  1175. frequencyEn = "quarter"
  1176. return
  1177. case "年度":
  1178. frequencyEn = "year"
  1179. return
  1180. }
  1181. return
  1182. }
  1183. func GetLeadUnitEn(unit string) (unitEn string) {
  1184. switch unit {
  1185. case "天":
  1186. unitEn = "day"
  1187. return
  1188. case "周":
  1189. unitEn = "week"
  1190. return
  1191. case "月":
  1192. unitEn = "month"
  1193. return
  1194. case "季":
  1195. unitEn = "quarter"
  1196. return
  1197. case "年":
  1198. unitEn = "year"
  1199. return
  1200. }
  1201. return
  1202. }
  1203. // getRefreshEdbInfoList 获取待更新的指标(普通基础指标、普通运算指标,预测运算指标)
  1204. //func getRefreshEdbInfoList(edbInfoId int) (newBaseEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int, err error, errMsg string) {
  1205. // calculateList, err := data_manage.GetEdbInfoAllCalculate(edbInfoId)
  1206. // if err != nil && err.Error() != utils.ErrNoRow() {
  1207. // err = errors.New("GetEdbInfoAllCalculate Err:" + err.Error())
  1208. // return
  1209. // }
  1210. // // 获取指标信息
  1211. // baseEdbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1212. // if err != nil {
  1213. // err = errors.New("GetEdbInfoAllCalGetEdbInfoByIdr:" + err.Error())
  1214. // return
  1215. // }
  1216. //
  1217. // baseEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 基础普通指标
  1218. // calculateInfoArr := make([]*data_manage.EdbInfo, 0) //基础运算指标
  1219. // basePredictEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 预测普通指标
  1220. // predictCalculateInfoArr := make([]*data_manage.EdbInfo, 0) //预测运算指标
  1221. //
  1222. // if baseEdbInfo.EdbInfoType == 0 { //普通指标
  1223. // switch baseEdbInfo.EdbType {
  1224. // case 1: //基础指标
  1225. // baseEdbInfoArr = append(baseEdbInfoArr, baseEdbInfo)
  1226. // case 2: //计算指标
  1227. // calculateList = append(calculateList, baseEdbInfo)
  1228. // }
  1229. // } else { //预测指标
  1230. // switch baseEdbInfo.EdbType {
  1231. // case 1: //基础指标
  1232. // basePredictEdbInfoArr = append(basePredictEdbInfoArr, baseEdbInfo)
  1233. // case 2: //计算指标
  1234. // predictCalculateInfoArr = append(predictCalculateInfoArr, baseEdbInfo)
  1235. // }
  1236. // }
  1237. //
  1238. // // 计算指标
  1239. // for _, v := range calculateList {
  1240. // getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  1241. // if tmpErr != nil {
  1242. // err = tmpErr
  1243. // fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  1244. // errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1245. // return
  1246. // }
  1247. // for _, tmpEdbInfo := range getBaseEdbInfoArr {
  1248. // switch tmpEdbInfo.EdbInfoType {
  1249. // case 0: //普通基础指标
  1250. // baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  1251. // case 1: //预测基础指标
  1252. // basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1253. // }
  1254. // }
  1255. //
  1256. // for _, tmpEdbInfo := range getCalculateInfoArr {
  1257. // switch tmpEdbInfo.EdbInfoType {
  1258. // case 0: //普通运算指标
  1259. // calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  1260. // case 1: //预测运算指标
  1261. // predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1262. // }
  1263. // }
  1264. // switch v.EdbInfoType {
  1265. // case 0: //普通运算指标
  1266. // calculateInfoArr = append(calculateInfoArr, v)
  1267. // case 1: //预测运算指标
  1268. // predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  1269. // }
  1270. // }
  1271. //
  1272. // // 预测计算指标
  1273. // for _, v := range predictCalculateInfoArr {
  1274. // getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  1275. // if tmpErr != nil {
  1276. // err = tmpErr
  1277. // fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  1278. // errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1279. // return
  1280. // }
  1281. // for _, tmpEdbInfo := range getBaseEdbInfoArr {
  1282. // switch tmpEdbInfo.EdbInfoType {
  1283. // case 0: //普通基础指标
  1284. // baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  1285. // case 1: //预测基础指标
  1286. // basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1287. // }
  1288. // }
  1289. //
  1290. // for _, tmpEdbInfo := range getCalculateInfoArr {
  1291. // switch tmpEdbInfo.EdbInfoType {
  1292. // case 0: //普通运算指标
  1293. // calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  1294. // case 1: //预测运算指标
  1295. // predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1296. // }
  1297. // }
  1298. // switch v.EdbInfoType {
  1299. // case 0: //普通运算指标
  1300. // calculateInfoArr = append(calculateInfoArr, v)
  1301. // case 1: //预测运算指标
  1302. // predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  1303. // }
  1304. // }
  1305. //
  1306. // // 第一次的计算指标map
  1307. // newCalculateMap = make(map[int]*data_manage.EdbInfo)
  1308. // for _, v := range calculateInfoArr {
  1309. // if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  1310. // calculateArr = append(calculateArr, v.EdbInfoId)
  1311. // }
  1312. // newCalculateMap[v.EdbInfoId] = v
  1313. // }
  1314. //
  1315. // // 基础预测指标
  1316. // if len(basePredictEdbInfoArr) > 0 {
  1317. // basePredictEdbInfoIdList := make([]int, 0)
  1318. // for _, v := range basePredictEdbInfoArr {
  1319. // basePredictEdbInfoIdList = append(basePredictEdbInfoIdList, v.EdbInfoId)
  1320. // }
  1321. // baseEdbInfoList, tmpErr := data_manage.GetPredictEdbInfoAllCalculate(basePredictEdbInfoIdList)
  1322. // if tmpErr != nil {
  1323. // err = tmpErr
  1324. // return
  1325. // }
  1326. //
  1327. // tmpCalculateList := make([]*data_manage.EdbInfo, 0)
  1328. // for _, v := range baseEdbInfoList {
  1329. // switch v.EdbType {
  1330. // case 1:
  1331. // baseEdbInfoArr = append(baseEdbInfoArr, v)
  1332. // case 2:
  1333. // // 如果不在第一次里面,那么在第二次 刷新预测指标的时候 需要加进去
  1334. // if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  1335. // tmpCalculateList = append(tmpCalculateList, v)
  1336. // }
  1337. // }
  1338. // }
  1339. //
  1340. // // 这里是查询预测指标用到的 普通计算指标
  1341. // for _, v := range tmpCalculateList {
  1342. // getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  1343. // if tmpErr != nil {
  1344. // err = tmpErr
  1345. // fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  1346. // errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1347. // return
  1348. // }
  1349. //
  1350. // // 基础指标
  1351. // for _, tmpEdbInfo := range getBaseEdbInfoArr {
  1352. // switch tmpEdbInfo.EdbInfoType {
  1353. // case 0: //普通基础指标
  1354. // baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  1355. // case 1: //预测基础指标
  1356. // basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1357. // }
  1358. // }
  1359. //
  1360. // // 计算指标
  1361. // for _, tmpEdbInfo := range getCalculateInfoArr {
  1362. // switch tmpEdbInfo.EdbInfoType {
  1363. // case 0: //普通运算指标
  1364. // calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  1365. // case 1: //预测运算指标
  1366. // predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1367. // }
  1368. // }
  1369. // switch v.EdbInfoType {
  1370. // case 0: //普通运算指标
  1371. // calculateInfoArr = append(calculateInfoArr, v)
  1372. // case 1: //预测运算指标
  1373. // predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  1374. // }
  1375. // }
  1376. //
  1377. // // 第二次计算指标的map
  1378. // for _, v := range calculateInfoArr {
  1379. // if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  1380. // calculateArr = append(calculateArr, v.EdbInfoId)
  1381. // }
  1382. // newCalculateMap[v.EdbInfoId] = v
  1383. // }
  1384. // }
  1385. //
  1386. // // 普通基础指标去重
  1387. // newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  1388. // baseMap := make(map[int]int)
  1389. // for _, v := range baseEdbInfoArr {
  1390. // if _, ok := baseMap[v.EdbInfoId]; !ok {
  1391. // newBaseEdbInfoArr = append(newBaseEdbInfoArr, v)
  1392. // }
  1393. // baseMap[v.EdbInfoId] = v.EdbInfoId
  1394. // }
  1395. //
  1396. // // 普通计算指标的id
  1397. // sort.Ints(calculateArr)
  1398. //
  1399. // // 预测计算指标去重
  1400. // newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  1401. // for _, v := range predictCalculateInfoArr {
  1402. // if _, ok := newPredictCalculateMap[v.EdbInfoId]; !ok {
  1403. // predictCalculateArr = append(predictCalculateArr, v.EdbInfoId)
  1404. // }
  1405. // newPredictCalculateMap[v.EdbInfoId] = v
  1406. // }
  1407. // // 预测计算指标的id
  1408. // sort.Ints(predictCalculateArr)
  1409. //
  1410. // return
  1411. //}
  1412. // getRefreshEdbInfoList 获取待更新的指标(普通基础指标、普通运算指标,预测运算指标)
  1413. func getRefreshEdbInfoListByIds(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int, err error, errMsg string) {
  1414. calculateList, err := data_manage.GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList)
  1415. if err != nil && err.Error() != utils.ErrNoRow() {
  1416. err = errors.New("GetEdbInfoAllCalculate Err:" + err.Error())
  1417. return
  1418. }
  1419. // 获取指标信息
  1420. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1421. if err != nil {
  1422. err = errors.New("GetEdbInfoAllCalGetEdbInfoByIdr:" + err.Error())
  1423. return
  1424. }
  1425. baseEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 基础普通指标
  1426. calculateInfoArr := make([]*data_manage.EdbInfo, 0) //基础运算指标
  1427. basePredictEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 预测普通指标
  1428. predictCalculateInfoArr := make([]*data_manage.EdbInfo, 0) //预测运算指标
  1429. for _, baseEdbInfo := range edbInfoList {
  1430. // 指标若停更, 则忽略掉
  1431. if baseEdbInfo.NoUpdate == 1 {
  1432. continue
  1433. }
  1434. if baseEdbInfo.EdbInfoType == 0 { //普通指标
  1435. switch baseEdbInfo.EdbType {
  1436. case 1: //基础指标
  1437. baseEdbInfoArr = append(baseEdbInfoArr, baseEdbInfo)
  1438. case 2: //计算指标
  1439. calculateList = append(calculateList, baseEdbInfo)
  1440. }
  1441. } else { //预测指标
  1442. switch baseEdbInfo.EdbType {
  1443. case 1: //基础指标
  1444. basePredictEdbInfoArr = append(basePredictEdbInfoArr, baseEdbInfo)
  1445. case 2: //计算指标
  1446. predictCalculateInfoArr = append(predictCalculateInfoArr, baseEdbInfo)
  1447. }
  1448. }
  1449. }
  1450. // 计算指标
  1451. for _, v := range calculateList {
  1452. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  1453. if tmpErr != nil {
  1454. err = tmpErr
  1455. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  1456. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1457. return
  1458. }
  1459. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  1460. switch tmpEdbInfo.EdbInfoType {
  1461. case 0: //普通基础指标
  1462. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  1463. case 1: //预测基础指标
  1464. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1465. }
  1466. }
  1467. for _, tmpEdbInfo := range getCalculateInfoArr {
  1468. switch tmpEdbInfo.EdbInfoType {
  1469. case 0: //普通运算指标
  1470. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  1471. case 1: //预测运算指标
  1472. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1473. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  1474. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1475. } else {
  1476. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1477. }
  1478. }
  1479. }
  1480. switch v.EdbInfoType {
  1481. case 0: //普通运算指标
  1482. calculateInfoArr = append(calculateInfoArr, v)
  1483. case 1: //预测指标
  1484. if v.EdbType == 1 { //预测普通指标
  1485. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  1486. } else {
  1487. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  1488. }
  1489. }
  1490. }
  1491. // 预测计算指标
  1492. for _, v := range predictCalculateInfoArr {
  1493. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  1494. if tmpErr != nil {
  1495. err = tmpErr
  1496. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  1497. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1498. return
  1499. }
  1500. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  1501. switch tmpEdbInfo.EdbInfoType {
  1502. case 0: //普通基础指标
  1503. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  1504. case 1: //预测基础指标
  1505. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  1506. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1507. } else {
  1508. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1509. }
  1510. //basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1511. }
  1512. }
  1513. for _, tmpEdbInfo := range getCalculateInfoArr {
  1514. switch tmpEdbInfo.EdbInfoType {
  1515. case 0: //普通运算指标
  1516. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  1517. case 1: //预测运算指标
  1518. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  1519. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1520. } else {
  1521. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1522. }
  1523. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1524. }
  1525. }
  1526. switch v.EdbInfoType {
  1527. case 0: //普通运算指标
  1528. calculateInfoArr = append(calculateInfoArr, v)
  1529. case 1: //预测指标
  1530. if v.EdbType == 1 { //预测普通指标
  1531. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  1532. } else {
  1533. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  1534. }
  1535. }
  1536. }
  1537. // 第一次的计算指标map
  1538. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  1539. for _, v := range calculateInfoArr {
  1540. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  1541. calculateArr = append(calculateArr, v.EdbInfoId)
  1542. }
  1543. newCalculateMap[v.EdbInfoId] = v
  1544. }
  1545. // 基础预测指标
  1546. if len(basePredictEdbInfoArr) > 0 {
  1547. basePredictEdbInfoIdList := make([]int, 0)
  1548. for _, v := range basePredictEdbInfoArr {
  1549. basePredictEdbInfoIdList = append(basePredictEdbInfoIdList, v.EdbInfoId)
  1550. }
  1551. baseEdbInfoList, tmpErr := data_manage.GetPredictEdbInfoAllCalculate(basePredictEdbInfoIdList)
  1552. if tmpErr != nil {
  1553. err = tmpErr
  1554. return
  1555. }
  1556. tmpCalculateList := make([]*data_manage.EdbInfo, 0)
  1557. for _, v := range baseEdbInfoList {
  1558. switch v.EdbType {
  1559. case 1:
  1560. baseEdbInfoArr = append(baseEdbInfoArr, v)
  1561. case 2:
  1562. // 如果不在第一次里面,那么在第二次 刷新预测指标的时候 需要加进去
  1563. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  1564. tmpCalculateList = append(tmpCalculateList, v)
  1565. }
  1566. }
  1567. }
  1568. // 这里是查询预测指标用到的 普通计算指标
  1569. for _, v := range tmpCalculateList {
  1570. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  1571. if tmpErr != nil {
  1572. err = tmpErr
  1573. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  1574. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1575. return
  1576. }
  1577. // 基础指标
  1578. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  1579. switch tmpEdbInfo.EdbInfoType {
  1580. case 0: //普通基础指标
  1581. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  1582. case 1: //预测基础指标
  1583. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1584. }
  1585. }
  1586. // 计算指标
  1587. for _, tmpEdbInfo := range getCalculateInfoArr {
  1588. switch tmpEdbInfo.EdbInfoType {
  1589. case 0: //普通运算指标
  1590. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  1591. case 1: //预测运算指标
  1592. if v.EdbType == 1 { //预测普通指标
  1593. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  1594. } else {
  1595. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  1596. }
  1597. }
  1598. }
  1599. switch v.EdbInfoType {
  1600. case 0: //普通运算指标
  1601. calculateInfoArr = append(calculateInfoArr, v)
  1602. case 1: //预测指标
  1603. if v.EdbType == 1 { //预测普通指标
  1604. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  1605. } else { //预测运算指标
  1606. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  1607. }
  1608. }
  1609. }
  1610. // 第二次计算指标的map
  1611. for _, v := range calculateInfoArr {
  1612. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  1613. calculateArr = append(calculateArr, v.EdbInfoId)
  1614. }
  1615. newCalculateMap[v.EdbInfoId] = v
  1616. }
  1617. }
  1618. // 普通基础指标去重
  1619. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  1620. baseMap := make(map[int]int)
  1621. for _, v := range baseEdbInfoArr {
  1622. if _, ok := baseMap[v.EdbInfoId]; !ok {
  1623. newBaseEdbInfoArr = append(newBaseEdbInfoArr, v)
  1624. }
  1625. baseMap[v.EdbInfoId] = v.EdbInfoId
  1626. }
  1627. // 普通计算指标的id
  1628. sort.Ints(calculateArr)
  1629. // 普通预测指标去重
  1630. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  1631. basePredictMap := make(map[int]int)
  1632. for _, v := range basePredictEdbInfoArr {
  1633. if _, ok := basePredictMap[v.EdbInfoId]; !ok {
  1634. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, v)
  1635. }
  1636. basePredictMap[v.EdbInfoId] = v.EdbInfoId
  1637. }
  1638. // 预测计算指标去重
  1639. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  1640. for _, v := range predictCalculateInfoArr {
  1641. if _, ok := newPredictCalculateMap[v.EdbInfoId]; !ok {
  1642. predictCalculateArr = append(predictCalculateArr, v.EdbInfoId)
  1643. }
  1644. newPredictCalculateMap[v.EdbInfoId] = v
  1645. }
  1646. // 预测计算指标的id
  1647. sort.Ints(predictCalculateArr)
  1648. return
  1649. }
  1650. type AddEdbBaseInfoReq struct {
  1651. EdbCode string `description:"指标编码"`
  1652. EdbName string `description:"指标名称"`
  1653. Unit string `description:"单位"`
  1654. ClassifyId int `description:"所属分类"`
  1655. SysUserId int `description:"用户id"`
  1656. SysUserRealName string `description:"用户真实名称"`
  1657. }
  1658. // EdbInfoAdd 添加指标到指标库
  1659. func EdbInfoAdd(source, subSource, classifyId int, edbCode, edbName, frequency, unit, startDate, endDate string, sysUserId int, sysUserRealName, lang string) (edbInfo *data_manage.EdbInfo, err error, errMsg string, isSendEmail bool) {
  1660. // 有些指标的添加是走的指标公共服务
  1661. if source == utils.DATA_SOURCE_BUSINESS {
  1662. req2 := AddEdbBaseInfoReq{
  1663. EdbCode: edbCode,
  1664. EdbName: edbName,
  1665. Unit: unit,
  1666. ClassifyId: classifyId,
  1667. SysUserId: sysUserId,
  1668. SysUserRealName: sysUserRealName,
  1669. }
  1670. reqJson, tmpErr := json.Marshal(req2)
  1671. if tmpErr != nil {
  1672. err = tmpErr
  1673. return
  1674. }
  1675. respItem, tmpErr := AddBaseEdbInfo(string(reqJson), source, subSource, lang)
  1676. if tmpErr != nil {
  1677. errMsg = "添加失败"
  1678. err = tmpErr
  1679. return
  1680. }
  1681. isSendEmail = respItem.IsSendEmail
  1682. if respItem.Ret != 200 {
  1683. errMsg = respItem.Msg
  1684. err = errors.New(respItem.ErrMsg)
  1685. } else {
  1686. edbInfo, err = data_manage.GetEdbInfoById(respItem.Data.EdbInfoId)
  1687. }
  1688. return
  1689. }
  1690. isSendEmail = true
  1691. //判断指标名称是否存在
  1692. {
  1693. var count int
  1694. var condition string
  1695. var pars []interface{}
  1696. //condition += " AND source=? "
  1697. //pars = append(pars, source)
  1698. switch lang {
  1699. case utils.EnLangVersion:
  1700. condition += " AND edb_name_en = ? "
  1701. default:
  1702. condition += " AND edb_name=? "
  1703. }
  1704. pars = append(pars, edbName)
  1705. count, err = data_manage.GetEdbInfoCountByCondition(condition, pars)
  1706. if err != nil {
  1707. errMsg = "判断指标名称是否存在失败"
  1708. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  1709. return
  1710. }
  1711. if count > 0 {
  1712. errMsg = "指标名称已存在,请重新填写"
  1713. err = errors.New(errMsg)
  1714. isSendEmail = false
  1715. return
  1716. }
  1717. }
  1718. edbInfo = new(data_manage.EdbInfo)
  1719. edbInfo.Source = source
  1720. sourceNameMap := map[int]string{
  1721. utils.DATA_SOURCE_THS: "同花顺",
  1722. utils.DATA_SOURCE_WIND: "wind",
  1723. utils.DATA_SOURCE_PB: "彭博",
  1724. utils.DATA_SOURCE_PB_FINANCE: "彭博财务",
  1725. utils.DATA_SOURCE_MANUAL: "手工数据",
  1726. utils.DATA_SOURCE_LZ: "隆众",
  1727. utils.DATA_SOURCE_YS: "SMM",
  1728. utils.DATA_SOURCE_GL: "钢联",
  1729. utils.DATA_SOURCE_ZZ: "郑商所",
  1730. utils.DATA_SOURCE_DL: "大商所",
  1731. utils.DATA_SOURCE_SH: "上期所",
  1732. utils.DATA_SOURCE_CFFEX: "中金所",
  1733. utils.DATA_SOURCE_SHFE: "上期能源",
  1734. utils.DATA_SOURCE_GIE: "欧洲天然气",
  1735. utils.DATA_SOURCE_LT: "路透",
  1736. utils.DATA_SOURCE_COAL: "中国煤炭市场网",
  1737. utils.DATA_SOURCE_GOOGLE_TRAVEL: "our world in data",
  1738. utils.DATA_SOURCE_MYSTEEL_CHEMICAL: "钢联",
  1739. utils.DATA_SOURCE_EIA_STEO: "EIA STEO报告",
  1740. utils.DATA_SOURCE_COM_TRADE: "UN",
  1741. utils.DATA_SOURCE_SCI: "SCI",
  1742. utils.DATA_SOURCE_BAIINFO: "BAIINFO",
  1743. utils.DATA_SOURCE_STOCK_PLANT: "存量装置",
  1744. utils.DATA_SOURCE_NATIONAL_STATISTICS: "国家统计局",
  1745. utils.DATA_SOURCE_FUBAO: "富宝数据",
  1746. utils.DATA_SOURCE_GFEX: "广期所",
  1747. utils.DATA_SOURCE_SCI_HQ: "卓创红期",
  1748. utils.DATA_SOURCE_LY: "粮油商务网",
  1749. utils.DATA_SOURCE_TRADE_ANALYSIS: "持仓分析",
  1750. utils.DATA_SOURCE_CALCULATE_STL: "STL趋势分解",
  1751. }
  1752. sourceName, ok := sourceNameMap[source]
  1753. if !ok {
  1754. edbSource := data_manage.EdbSourceIdMap[source]
  1755. if edbSource != nil {
  1756. sourceName = edbSource.SourceName
  1757. }
  1758. if sourceName == "" {
  1759. errMsg = "指标来源异常"
  1760. err = errors.New(errMsg)
  1761. return
  1762. }
  1763. }
  1764. edbInfo.SourceName = sourceName
  1765. edbType := 1 //基础指标
  1766. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1767. edbType = 2 //计算指标
  1768. }
  1769. // 从缓存中获取
  1770. terminalCode, serverUrl, sourceIndexName, e := GetEdbTerminalCodeBySource(edbInfo.Source, edbInfo.EdbCode, edbInfo.StockCode)
  1771. if e != nil {
  1772. errMsg = "获取可以使用的终端地址失败"
  1773. err = errors.New("获取可以使用的终端地址失败,Err:" + e.Error())
  1774. return
  1775. }
  1776. //获取该层级下最大的排序数
  1777. maxSort, err := GetEdbClassifyMaxSort(classifyId, 0)
  1778. if err != nil {
  1779. errMsg = "获取失败"
  1780. err = errors.New("查询排序信息失败,Err:" + err.Error())
  1781. return
  1782. }
  1783. edbInfo.EdbCode = edbCode
  1784. edbInfo.EdbName = edbName
  1785. edbInfo.EdbNameEn = edbName
  1786. edbInfo.EdbNameSource = edbName
  1787. edbInfo.Frequency = frequency
  1788. edbInfo.Unit = unit
  1789. edbInfo.UnitEn = unit
  1790. edbInfo.StartDate = startDate
  1791. edbInfo.EndDate = endDate
  1792. edbInfo.ClassifyId = classifyId
  1793. edbInfo.SysUserId = sysUserId
  1794. edbInfo.SysUserRealName = sysUserRealName
  1795. edbInfo.CreateTime = time.Now()
  1796. edbInfo.ModifyTime = time.Now()
  1797. edbInfo.ServerUrl = serverUrl
  1798. edbInfo.Sort = maxSort + 1
  1799. edbInfo.DataDateType = `交易日`
  1800. edbInfo.TerminalCode = terminalCode
  1801. edbInfo.SourceIndexName = sourceIndexName
  1802. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1803. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  1804. itemVal, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  1805. if itemVal != nil && err == nil {
  1806. edbInfo.MaxValue = itemVal.MaxValue
  1807. edbInfo.MinValue = itemVal.MinValue
  1808. }
  1809. edbInfo.EdbType = edbType
  1810. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  1811. if err != nil {
  1812. errMsg = "保存失败"
  1813. err = errors.New("保存失败,Err:" + err.Error())
  1814. return
  1815. }
  1816. edbInfo.EdbInfoId = int(edbInfoId)
  1817. //保存数据
  1818. data_manage.ModifyEdbInfoDataStatus(edbInfoId, source, subSource, edbCode)
  1819. maxAndMinItem, _ := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  1820. if maxAndMinItem != nil {
  1821. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  1822. }
  1823. //添加es
  1824. AddOrEditEdbInfoToEs(int(edbInfoId))
  1825. go handleByAddEdbInfo(edbInfo)
  1826. return
  1827. }
  1828. // TraceEdbInfoByEdbInfoId 指标追溯
  1829. func TraceEdbInfoByEdbInfoId(edbInfoId, sysUserId int) (traceEdbInfo data_manage.TraceEdbInfoResp, err error) {
  1830. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1831. if err != nil {
  1832. return
  1833. }
  1834. edbInfoRuleMap := make(map[int]string, 0)
  1835. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  1836. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  1837. traceEdbInfo = data_manage.TraceEdbInfoResp{
  1838. //EdbInfoId: edbInfo.EdbInfoId,
  1839. EdbInfoId: edbInfoId,
  1840. EdbInfoType: edbInfo.EdbInfoType,
  1841. EdbName: edbInfo.EdbName,
  1842. EdbNameEn: edbInfo.EdbNameEn,
  1843. EdbType: edbInfo.EdbType,
  1844. //Source: edbInfo.Source,
  1845. UniqueCode: edbInfo.UniqueCode,
  1846. ClassifyId: edbInfo.ClassifyId,
  1847. IsStop: edbInfo.NoUpdate,
  1848. EdbInfo: edbInfo,
  1849. }
  1850. findIdMap := make(map[int]int)
  1851. findIdMap[edbInfoId] = edbInfoId
  1852. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  1853. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1854. edbInfoIdList := make([]int, 0)
  1855. for _, v := range findIdMap {
  1856. edbInfoIdList = append(edbInfoIdList, v)
  1857. }
  1858. classifyIdList := make([]int, 0)
  1859. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1860. if err != nil {
  1861. return
  1862. }
  1863. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1864. for _, tmpEdbInfo := range edbInfoList {
  1865. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  1866. classifyIdList = append(classifyIdList, tmpEdbInfo.ClassifyId)
  1867. }
  1868. // 指标权限Map
  1869. edbInfoIdPermissionMap := make(map[int]bool)
  1870. // 指标权限
  1871. {
  1872. // 当前的分类
  1873. classifyMap := make(map[int]*data_manage.EdbClassify)
  1874. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1875. if tmpErr != nil {
  1876. err = tmpErr
  1877. return
  1878. }
  1879. for _, v := range classifyList {
  1880. classifyMap[v.ClassifyId] = v
  1881. }
  1882. // 获取所有有权限的指标和分类
  1883. permissionEdbIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUserId, 0, 0)
  1884. if tmpErr != nil {
  1885. err = tmpErr
  1886. return
  1887. }
  1888. for _, v := range edbInfoMap {
  1889. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  1890. edbInfoIdPermissionMap[v.EdbInfoId] = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1891. }
  1892. }
  1893. }
  1894. traceEdbInfo, err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap, edbInfoIdPermissionMap)
  1895. // 权限校验
  1896. return
  1897. }
  1898. // TraceEdbInfoByEdbInfoIdList 指标追溯
  1899. func TraceEdbInfoByEdbInfoIdList(edbInfoIdList []int) (traceEdbInfoList []data_manage.TraceEdbInfoResp, err error) {
  1900. traceEdbInfoList = make([]data_manage.TraceEdbInfoResp, 0)
  1901. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1902. if err != nil {
  1903. return
  1904. }
  1905. edbInfoRuleMap := make(map[int]string, 0)
  1906. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  1907. findIdMap := make(map[int]int)
  1908. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  1909. for _, edbInfo := range edbInfoList {
  1910. findIdMap[edbInfo.EdbInfoId] = edbInfo.EdbInfoId
  1911. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  1912. traceEdbInfo := data_manage.TraceEdbInfoResp{
  1913. //EdbInfoId: edbInfo.EdbInfoId,
  1914. EdbInfoId: edbInfo.EdbInfoId,
  1915. EdbInfoType: edbInfo.EdbInfoType,
  1916. EdbName: edbInfo.EdbName,
  1917. EdbNameEn: edbInfo.EdbNameEn,
  1918. EdbType: edbInfo.EdbType,
  1919. //Source: edbInfo.Source,
  1920. UniqueCode: edbInfo.UniqueCode,
  1921. ClassifyId: edbInfo.ClassifyId,
  1922. IsStop: edbInfo.NoUpdate,
  1923. EdbInfo: edbInfo,
  1924. }
  1925. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfo.EdbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1926. traceEdbInfoList = append(traceEdbInfoList, traceEdbInfo)
  1927. }
  1928. //findEdbInfoIdList := make([]int, 0)
  1929. //for _, v := range findIdMap {
  1930. // findEdbInfoIdList = append(findEdbInfoIdList, v)
  1931. //}
  1932. //findEdbInfoList, err := data_manage.GetEdbInfoByIdList(findEdbInfoIdList)
  1933. //if err != nil {
  1934. // return
  1935. //}
  1936. //edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1937. //for _, tmpEdbInfo := range findEdbInfoList {
  1938. // edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  1939. //}
  1940. //for k, traceEdbInfo := range traceEdbInfoList {
  1941. // traceEdbInfoList[k], err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
  1942. //}
  1943. return
  1944. }
  1945. // traceEdbInfoByEdbInfoId 指标追溯
  1946. func traceEdbInfoByEdbInfoId(edbInfoId int, traceEdbInfo data_manage.TraceEdbInfoResp, edbInfoRuleMap map[int]string, findIdMap map[int]int, existMap map[int]data_manage.TraceEdbInfoResp, edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo) (child []data_manage.TraceEdbInfoResp, err error) {
  1947. traceEdbInfo, ok := existMap[edbInfoId]
  1948. if ok {
  1949. return
  1950. }
  1951. child = make([]data_manage.TraceEdbInfoResp, 0)
  1952. edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  1953. if e != nil {
  1954. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
  1955. return
  1956. }
  1957. // 指标信息map
  1958. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1959. if len(edbInfoMappingList) > 0 {
  1960. fromEdbInfoIdList := make([]int, 0)
  1961. for _, v := range edbInfoMappingList {
  1962. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  1963. }
  1964. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(fromEdbInfoIdList)
  1965. if tmpErr != nil {
  1966. err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", tmpErr.Error())
  1967. return
  1968. }
  1969. for _, v := range edbInfoList {
  1970. edbInfoMap[v.EdbInfoId] = v
  1971. }
  1972. }
  1973. edbMappingMap[edbInfoId] = edbInfoMappingList
  1974. for _, v := range edbInfoMappingList {
  1975. tmpEdbInfoId := v.FromEdbInfoId
  1976. tmpTraceEdbInfo := data_manage.TraceEdbInfoResp{
  1977. EdbInfoId: tmpEdbInfoId,
  1978. EdbInfoType: v.FromEdbInfoType,
  1979. EdbType: v.FromEdbType,
  1980. UniqueCode: v.FromUniqueCode,
  1981. ClassifyId: v.FromClassifyId,
  1982. IsStop: v.NoUpdate,
  1983. EdbInfo: edbInfoMap[v.FromEdbInfoId],
  1984. }
  1985. // 计算指标/预测指标继续溯源
  1986. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  1987. // 查过了就不查了
  1988. if _, ok2 := findIdMap[tmpEdbInfoId]; !ok2 {
  1989. tmpTraceEdbInfo.Child, e = traceEdbInfoByEdbInfoId(tmpEdbInfoId, tmpTraceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1990. if e != nil {
  1991. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
  1992. return
  1993. }
  1994. }
  1995. }
  1996. child = append(child, tmpTraceEdbInfo)
  1997. findIdMap[tmpEdbInfoId] = tmpEdbInfoId
  1998. }
  1999. existMap[edbInfoId] = traceEdbInfo
  2000. return
  2001. }
  2002. func handleTraceEdbInfo(traceEdbInfoResp data_manage.TraceEdbInfoResp, parentEdbInfoId int, edbInfoMap map[int]*data_manage.EdbInfo, edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo, edbInfoIdPermissionMap map[int]bool) (newTraceEdbInfoResp data_manage.TraceEdbInfoResp, err error) {
  2003. edbInfo, ok := edbInfoMap[traceEdbInfoResp.EdbInfoId]
  2004. if !ok {
  2005. err = errors.New("指标异常")
  2006. return
  2007. }
  2008. var parentEdbInfo *data_manage.EdbInfo
  2009. if parentEdbInfoId > 0 {
  2010. parentEdbInfo, ok = edbInfoMap[parentEdbInfoId]
  2011. if !ok {
  2012. err = errors.New("指标异常")
  2013. return
  2014. }
  2015. }
  2016. //traceEdbInfoResp.EdbName = edbInfo.EdbName
  2017. traceEdbInfoResp.EdbName, traceEdbInfoResp.EdbNameEn, traceEdbInfoResp.RuleTitle, traceEdbInfoResp.RuleTitleEn = getEdbRuleTitle(edbInfo, parentEdbInfo, traceEdbInfoResp.Child, edbInfoMap, edbMappingMap)
  2018. if traceEdbInfoResp.Child != nil && len(traceEdbInfoResp.Child) > 0 {
  2019. for k, v := range traceEdbInfoResp.Child {
  2020. traceEdbInfoResp.Child[k], err = handleTraceEdbInfo(v, traceEdbInfoResp.EdbInfoId, edbInfoMap, edbMappingMap, edbInfoIdPermissionMap)
  2021. if err != nil {
  2022. return
  2023. }
  2024. }
  2025. }
  2026. // 指标权限
  2027. traceEdbInfoResp.HaveOperaAuth, _ = edbInfoIdPermissionMap[traceEdbInfoResp.EdbInfoId]
  2028. newTraceEdbInfoResp = traceEdbInfoResp
  2029. return
  2030. }
  2031. // getEdbRule 获取规则名称
  2032. func getEdbRuleTitle(edbInfo, parentEdbInfo *data_manage.EdbInfo, childList []data_manage.TraceEdbInfoResp, edbInfoMap map[int]*data_manage.EdbInfo, edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo) (edbName, edbNameEn, ruleTitle, ruleTitleEn string) {
  2033. edbName = edbInfo.EdbName
  2034. edbNameEn = edbInfo.EdbNameEn
  2035. ruleTitle = `来源于` + edbInfo.SourceName
  2036. ruleTitleEn = `Source From: ` + edbInfo.SourceName
  2037. if parentEdbInfo != nil {
  2038. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  2039. if !ok {
  2040. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  2041. }
  2042. // 指标名称
  2043. switch parentEdbInfo.Source {
  2044. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  2045. for _, v := range edbMappingList {
  2046. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  2047. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  2048. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, v.FromTag)
  2049. }
  2050. }
  2051. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ, utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 直接拼接 ,累计值同比拼接
  2052. for _, v := range edbMappingList {
  2053. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  2054. tmpName := ``
  2055. tmpNameEn := ``
  2056. if v.FromTag == `A` {
  2057. tmpName = `拼接日期前`
  2058. tmpNameEn = `Before Concatenation Date:`
  2059. } else if v.FromTag == `B` {
  2060. tmpName = `拼接日期后`
  2061. tmpNameEn = `After Concatenation Date:`
  2062. }
  2063. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  2064. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, tmpNameEn)
  2065. }
  2066. }
  2067. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  2068. for _, v := range edbMappingList {
  2069. //(需对上游指标+自变量,领先10天/因变量)
  2070. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  2071. tmpName := ``
  2072. tmpNameEn := ``
  2073. if v.FromTag == `A` {
  2074. tmpName = fmt.Sprintf(`自变量,领先%d天`, v.MoveValue)
  2075. tmpNameEn = fmt.Sprintf(`Independent Variable, Lead %d Days:`, v.MoveValue)
  2076. } else if v.FromTag == `B` {
  2077. tmpName = `因变量`
  2078. tmpNameEn = `Dependent Variable`
  2079. }
  2080. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  2081. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, tmpNameEn)
  2082. }
  2083. }
  2084. case utils.DATA_SOURCE_CALCULATE_CORRELATION: // 滚动相关性
  2085. for _, v := range edbMappingList {
  2086. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  2087. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  2088. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, v.FromTag)
  2089. }
  2090. }
  2091. }
  2092. }
  2093. if edbInfo.EdbType == 1 {
  2094. // 基础指标的话,直接返回来源名称
  2095. //ruleTitle = `来源于`+edbInfo.SourceName
  2096. return
  2097. }
  2098. // 规则
  2099. switch edbInfo.Source {
  2100. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  2101. ruleTitle = "=" + edbInfo.CalculateFormula
  2102. case utils.DATA_SOURCE_CALCULATE_LJZZY, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  2103. ruleTitle = `累计转月值计算`
  2104. ruleTitleEn = `Cumulative to Monthly Calculation`
  2105. case utils.DATA_SOURCE_CALCULATE_TBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  2106. ruleTitle = `同比值计算`
  2107. ruleTitleEn = `Year-on-Year Value Calculation`
  2108. case utils.DATA_SOURCE_CALCULATE_TCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  2109. ruleTitle = `同差值计算`
  2110. ruleTitleEn = `Yearly Difference Calculation`
  2111. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  2112. ruleTitle = fmt.Sprintf("N数值移动均值计算(N=%s)", edbInfo.CalculateFormula)
  2113. ruleTitleEn = fmt.Sprintf("N-Value Moving Average Calculation(N=%s)", edbInfo.CalculateFormula)
  2114. case utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  2115. ruleTitle = fmt.Sprintf("N数值环比值计算(N=%s)", edbInfo.CalculateFormula)
  2116. ruleTitleEn = fmt.Sprintf("N-Value Sequential Comparison Calculation(N=%s)", edbInfo.CalculateFormula)
  2117. case utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  2118. ruleTitle = fmt.Sprintf("N数值环差值计算(N=%s)", edbInfo.CalculateFormula)
  2119. ruleTitleEn = fmt.Sprintf("N-Value Sequential Difference Calculation(N=%s)", edbInfo.CalculateFormula)
  2120. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  2121. moveType := `领先`
  2122. moveTypeEn := `Lead`
  2123. if edbInfo.MoveType == 2 {
  2124. moveType = "滞后"
  2125. moveTypeEn = `Lag`
  2126. }
  2127. ruleTitle = fmt.Sprintf("时间移位计算(%s%s%s)", moveType, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  2128. ruleTitleEn = fmt.Sprintf("Time Shift Calculation(%s%s%s)", moveTypeEn, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  2129. case utils.DATA_SOURCE_CALCULATE_BP, utils.DATA_SOURCE_PREDICT_CALCULATE_BP: // 变频
  2130. childFrequency := ``
  2131. if len(childList) > 0 {
  2132. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  2133. childFrequency = childEdbInfo.Frequency
  2134. }
  2135. }
  2136. ruleTitle = fmt.Sprintf("升频计算(%s转%s)", childFrequency, edbInfo.Frequency)
  2137. ruleTitleEn = fmt.Sprintf("Upsampling Calculation(%s转%s)", childFrequency, edbInfo.Frequency)
  2138. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: // 直接拼接
  2139. ruleTitle = fmt.Sprintf("直接拼接计算(%s)", edbInfo.CalculateFormula)
  2140. ruleTitleEn = fmt.Sprintf("Direct Concatenation Calculation(%s)", edbInfo.CalculateFormula)
  2141. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 累计值同比拼
  2142. ruleTitle = fmt.Sprintf("累计值同比值拼接计算(%s)", edbInfo.CalculateFormula)
  2143. ruleTitleEn = fmt.Sprintf("Cumulative Year-on-Year Concatenation Calculation(%s)", edbInfo.CalculateFormula)
  2144. case utils.DATA_SOURCE_PYTHON:
  2145. ruleTitle = `代码运算`
  2146. ruleTitleEn = `Code Computation`
  2147. case utils.DATA_SOURCE_CALCULATE_CJJX, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  2148. ruleTitle = fmt.Sprintf("超季节性计算(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  2149. ruleTitleEn = fmt.Sprintf("Super Seasonality Calculation(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  2150. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  2151. var startDate, endDate string
  2152. dateList := strings.Split(edbInfo.CalculateFormula, ",")
  2153. if len(dateList) == 2 {
  2154. startDate = dateList[0]
  2155. endDate = dateList[1]
  2156. }
  2157. ruleTitle = fmt.Sprintf("拟合残差计算(%s至%s)", startDate, endDate)
  2158. ruleTitleEn = fmt.Sprintf("Fit Residual Calculation(%s至%s)", startDate, endDate)
  2159. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  2160. ruleTitle = `数据调整`
  2161. ruleTitleEn = `Data Adjustment`
  2162. case utils.DATA_SOURCE_CALCULATE_NH, utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  2163. ruleTitle = `年化计算`
  2164. ruleTitleEn = `Annualization Calculation`
  2165. case utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: // 扩散指数->53
  2166. type KszsConfig struct {
  2167. DateType int `description:"扩散指标日期;1:全部指标日期并集;2:部分指标日期并集"`
  2168. CheckList []string `description:"选中的数据,A,B,C"`
  2169. }
  2170. var config KszsConfig
  2171. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &config)
  2172. if err != nil {
  2173. return
  2174. }
  2175. var startDate, endDate time.Time
  2176. childEdbInfoIdList := make([]int, 0)
  2177. if config.DateType == 1 {
  2178. for _, v := range childList {
  2179. childEdbInfoIdList = append(childEdbInfoIdList, v.EdbInfoId)
  2180. }
  2181. } else {
  2182. if parentEdbInfo != nil {
  2183. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  2184. if !ok {
  2185. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  2186. }
  2187. tagMap := make(map[string]int)
  2188. for _, v := range edbMappingList {
  2189. tagMap[v.FromTag] = v.FromEdbInfoId
  2190. }
  2191. for _, v := range config.CheckList {
  2192. if tmpEdbInfoId, ok := tagMap[v]; ok {
  2193. childEdbInfoIdList = append(childEdbInfoIdList, tmpEdbInfoId)
  2194. }
  2195. }
  2196. }
  2197. }
  2198. for _, v := range childEdbInfoIdList {
  2199. if childEdbInfo, ok := edbInfoMap[v]; ok {
  2200. tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.StartDate, time.Local)
  2201. if tmpErr != nil {
  2202. return
  2203. }
  2204. if startDate.IsZero() || startDate.After(tmpStartDate) {
  2205. startDate = tmpStartDate
  2206. }
  2207. tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.EndDate, time.Local)
  2208. if tmpErr != nil {
  2209. return
  2210. }
  2211. if endDate.IsZero() || endDate.Before(tmpEndDate) {
  2212. endDate = tmpEndDate
  2213. }
  2214. }
  2215. }
  2216. ruleTitle = fmt.Sprintf("扩散指数计算(%s至%s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  2217. ruleTitleEn = fmt.Sprintf("Diffusion Index Calculation(%s to %s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  2218. case utils.DATA_SOURCE_STOCK_PLANT:
  2219. ruleTitle = `来源于装置分析`
  2220. ruleTitleEn = `Derived from Plant Analysis`
  2221. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  2222. type EdbCalculateFormula struct {
  2223. BaseCalculateValue int `description:"基础计算窗口"`
  2224. BaseCalculateUnit string `description:"基础计算频度"`
  2225. LeadValue int `description:"领先期数"`
  2226. LeadUnit string `description:"频度"`
  2227. CalculateValue int `description:"计算窗口"`
  2228. CalculateUnit string `description:"计算频度"`
  2229. }
  2230. var correlationConf EdbCalculateFormula
  2231. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &correlationConf)
  2232. if err != nil {
  2233. return
  2234. }
  2235. ruleTitle = fmt.Sprintf("滚动相关性(计算窗口%d%s,B领先A%d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  2236. ruleTitleEn = fmt.Sprintf("Rolling Correlation (Calculation Window %d%s, B Leads A %d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  2237. case utils.DATA_SOURCE_CALCULATE_JP, utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  2238. childFrequency := ``
  2239. if len(childList) > 0 {
  2240. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  2241. childFrequency = childEdbInfo.Frequency
  2242. }
  2243. }
  2244. ruleTitle = fmt.Sprintf("降频计算(%s转%s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  2245. ruleTitleEn = fmt.Sprintf("Downsampling Calculation(%s to %s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  2246. case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION:
  2247. ruleTitle = fmt.Sprintf("标准差(滚动%s期)", edbInfo.CalculateFormula)
  2248. ruleTitleEn = fmt.Sprintf("Standard Deviation (Rolling %s Periods)", edbInfo.CalculateFormula)
  2249. case utils.DATA_SOURCE_CALCULATE_PERCENTILE, utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE:
  2250. type TempCalculate struct {
  2251. CalculateValue int `description:"计算窗口"`
  2252. CalculateUnit string `description:"计算频度"`
  2253. }
  2254. cf := TempCalculate{}
  2255. if e := json.Unmarshal([]byte(edbInfo.CalculateFormula), &cf); e != nil {
  2256. return
  2257. }
  2258. ruleTitle = fmt.Sprintf("百分位(时间长度%d%s)", cf.CalculateValue, cf.CalculateUnit)
  2259. ruleTitle = fmt.Sprintf("Percentile (Time Length %d%s)", cf.CalculateValue, cf.CalculateUnit)
  2260. case utils.DATA_SOURCE_CALCULATE_ZSXY, utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY:
  2261. ruleTitle = `指数修匀计算`
  2262. ruleTitle = `Index Smoothing Calculation`
  2263. }
  2264. return
  2265. }
  2266. // GetEdbChartAdminList
  2267. // @param source 来源 :1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格
  2268. func GetEdbChartAdminList(source int) (list []int, err error) {
  2269. switch source {
  2270. case 1: //手工数据指标
  2271. list, err = models.GetEdbInfoAdminList()
  2272. if err != nil {
  2273. return
  2274. }
  2275. case 2: //钢联化工数据库
  2276. list, err = data_manage.GetMysteelChemicalIndexAdminList()
  2277. if err != nil {
  2278. return
  2279. }
  2280. case 3: //ETA指标库
  2281. list, err = data_manage.GetEdbInfoAdminList(0)
  2282. if err != nil {
  2283. return
  2284. }
  2285. case 4: //ETA预测指标
  2286. list, err = data_manage.GetEdbInfoAdminList(1)
  2287. if err != nil {
  2288. return
  2289. }
  2290. case 5: //图库
  2291. list, err = data_manage.GetChartInfoAdminList()
  2292. if err != nil {
  2293. return
  2294. }
  2295. default:
  2296. return
  2297. }
  2298. return
  2299. }
  2300. // EdbInfoReplace 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  2301. func EdbInfoReplace(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, sysAdminId int, sysAdminRealName string) (replaceChartTotal, replaceCalculateTotal int, err error) {
  2302. defer func() {
  2303. if err != nil {
  2304. fmt.Println("ERR:", err.Error())
  2305. }
  2306. }()
  2307. // 替换关联表中的source_edb_info_id
  2308. relationEdbInfoIdList, replaceChartTotal, replaceCalculateTotal, err := data_manage.ReplaceChartEdb(oldEdbInfo, newEdbInfo)
  2309. if err != nil {
  2310. return
  2311. }
  2312. //加入到缓存队列中处理
  2313. go cache.AddReplaceEdbInfo(oldEdbInfo, newEdbInfo)
  2314. // 更新所有的关联指标
  2315. err, _ = EdbInfoRefreshAllFromBaseV3(relationEdbInfoIdList, true, true, true)
  2316. return
  2317. }
  2318. func GetTerminalFromBaseIndex(source int, edbCode string) (terminalCode string, sourceIndexName string, err error) {
  2319. tbName := GetBaseIndexTableName(source)
  2320. if tbName != "" {
  2321. var item data_manage.BaseIndexTerminalCode
  2322. item, err = data_manage.GetBaseIndexTerminalCode(edbCode, tbName)
  2323. if err != nil {
  2324. err = fmt.Errorf("查询数据源指标信息失败, Err: %s", err)
  2325. return
  2326. }
  2327. terminalCode = item.TerminalCode
  2328. sourceIndexName = item.IndexName
  2329. }
  2330. return
  2331. }
  2332. func GetBaseIndexTableName(source int) (tableName string) {
  2333. switch source {
  2334. case utils.DATA_SOURCE_YS:
  2335. tableName = "base_from_smm_index"
  2336. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  2337. tableName = "base_from_mysteel_chemical_index"
  2338. case utils.DATA_SOURCE_SCI:
  2339. tableName = "base_from_sci_index"
  2340. case utils.DATA_SOURCE_BAIINFO:
  2341. tableName = "base_from_baiinfo_index"
  2342. default:
  2343. }
  2344. return
  2345. }
  2346. // EdbInfoWsdAdd 添加指标到指标库
  2347. func EdbInfoWsdAdd(item *data_manage.EdbInfo) (edbInfo *data_manage.EdbInfo, err error, errMsg string, isSendEmail bool) {
  2348. isSendEmail = true
  2349. //判断指标名称是否存在
  2350. var condition string
  2351. var pars []interface{}
  2352. condition += " AND edb_name=? "
  2353. pars = append(pars, item.EdbName)
  2354. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  2355. if err != nil {
  2356. errMsg = "判断指标名称是否存在失败"
  2357. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  2358. return
  2359. }
  2360. if count > 0 {
  2361. errMsg = "指标名称已存在,请重新填写"
  2362. err = errors.New(errMsg)
  2363. isSendEmail = false
  2364. return
  2365. }
  2366. source := item.Source
  2367. edbInfo = new(data_manage.EdbInfo)
  2368. edbInfo.Source = source
  2369. sourceNameMap := map[int]string{
  2370. utils.DATA_SOURCE_THS: "同花顺",
  2371. utils.DATA_SOURCE_WIND: "wind",
  2372. utils.DATA_SOURCE_PB: "彭博",
  2373. utils.DATA_SOURCE_PB_FINANCE: "彭博财务",
  2374. utils.DATA_SOURCE_MANUAL: "手工数据",
  2375. utils.DATA_SOURCE_LZ: "隆众",
  2376. utils.DATA_SOURCE_YS: "SMM",
  2377. utils.DATA_SOURCE_GL: "钢联",
  2378. utils.DATA_SOURCE_ZZ: "郑商所",
  2379. utils.DATA_SOURCE_DL: "大商所",
  2380. utils.DATA_SOURCE_SH: "上期所",
  2381. utils.DATA_SOURCE_CFFEX: "中金所",
  2382. utils.DATA_SOURCE_SHFE: "上期能源",
  2383. utils.DATA_SOURCE_GIE: "欧洲天然气",
  2384. utils.DATA_SOURCE_LT: "路透",
  2385. utils.DATA_SOURCE_COAL: "中国煤炭市场网",
  2386. utils.DATA_SOURCE_GOOGLE_TRAVEL: "our world in data",
  2387. utils.DATA_SOURCE_MYSTEEL_CHEMICAL: "钢联",
  2388. utils.DATA_SOURCE_EIA_STEO: "EIA STEO报告",
  2389. utils.DATA_SOURCE_COM_TRADE: "UN",
  2390. utils.DATA_SOURCE_SCI: "SCI",
  2391. utils.DATA_SOURCE_BAIINFO: "BAIINFO",
  2392. utils.DATA_SOURCE_STOCK_PLANT: "存量装置",
  2393. utils.DATA_SOURCE_NATIONAL_STATISTICS: "国家统计局",
  2394. utils.DATA_SOURCE_FUBAO: "富宝数据",
  2395. }
  2396. sourceName, ok := sourceNameMap[source]
  2397. if !ok {
  2398. edbSource := data_manage.EdbSourceIdMap[source]
  2399. if edbSource != nil {
  2400. sourceName = edbSource.SourceName
  2401. }
  2402. if sourceName == "" {
  2403. errMsg = "指标来源异常"
  2404. err = errors.New(errMsg)
  2405. return
  2406. }
  2407. }
  2408. edbInfo.SourceName = sourceName
  2409. edbType := 1 //基础指标
  2410. if source == utils.DATA_SOURCE_STOCK_PLANT {
  2411. edbType = 2 //计算指标
  2412. }
  2413. //获取该层级下最大的排序数
  2414. maxSort, err := GetEdbClassifyMaxSort(item.ClassifyId, 0)
  2415. if err != nil {
  2416. errMsg = "获取失败"
  2417. err = errors.New("查询排序信息失败,Err:" + err.Error())
  2418. return
  2419. }
  2420. edbInfo.EdbCode = item.EdbCode
  2421. edbInfo.EdbName = item.EdbName
  2422. edbInfo.EdbNameSource = item.EdbName
  2423. edbInfo.EdbNameEn = item.EdbNameEn
  2424. edbInfo.Frequency = item.Frequency
  2425. edbInfo.Unit = item.Unit
  2426. edbInfo.UnitEn = item.UnitEn
  2427. edbInfo.ClassifyId = item.ClassifyId
  2428. edbInfo.SysUserId = item.SysUserId
  2429. edbInfo.SysUserRealName = item.SysUserRealName
  2430. edbInfo.CreateTime = time.Now()
  2431. edbInfo.ModifyTime = time.Now()
  2432. edbInfo.ServerUrl = item.ServerUrl
  2433. edbInfo.Sort = maxSort + 1
  2434. edbInfo.DataDateType = `交易日`
  2435. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2436. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  2437. edbInfo.EdbType = edbType
  2438. edbInfo.SubSource = item.SubSource
  2439. edbInfo.SubSourceName = "日期序列"
  2440. edbInfo.IndicatorCode = item.IndicatorCode
  2441. edbInfo.StockCode = item.StockCode
  2442. edbInfo.TerminalCode = item.TerminalCode
  2443. edbInfo.Extra = item.Extra
  2444. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  2445. if err != nil {
  2446. errMsg = "保存失败"
  2447. err = errors.New("保存失败,Err:" + err.Error())
  2448. return
  2449. }
  2450. edbInfo.EdbInfoId = int(edbInfoId)
  2451. //保存数据
  2452. err = data_manage.ModifyEdbInfoWindWsdDataStatus(source, item.SubSource, edbInfoId, item.EdbCode)
  2453. if err != nil {
  2454. errMsg = "保存失败"
  2455. err = errors.New("修改数据对应指标ID失败,Err:" + err.Error())
  2456. return
  2457. }
  2458. maxAndMinItem, _ := data_manage.GetEdbInfoWsdMaxAndMinInfo(source, item.SubSource, item.EdbCode)
  2459. if maxAndMinItem != nil {
  2460. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  2461. }
  2462. //添加es
  2463. AddOrEditEdbInfoToEs(int(edbInfoId))
  2464. return
  2465. }
  2466. // BatchRefreshEdbByEdbIds 批量刷新指标
  2467. func BatchRefreshEdbByEdbIds(edbIdList []int, redisKey string, refreshKeys []string) (syncing bool, err error) {
  2468. if len(edbIdList) <= 0 {
  2469. return
  2470. }
  2471. // 设置刷新缓存
  2472. if redisKey != `` {
  2473. // 设置最多10分钟缓存
  2474. utils.Rc.SetNX(redisKey, 1, time.Minute*10)
  2475. }
  2476. // 获取需要刷新的指标列表
  2477. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbIdList)
  2478. // 需要刷新的指标数量
  2479. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  2480. // 关联指标过多的时候, 异步刷新
  2481. if totalEdbInfo > 20 {
  2482. syncing = true
  2483. //go func() {
  2484. // defer func() {
  2485. // if err != nil {
  2486. // alarm_msg.SendAlarmMsg("BatchRefreshEdbByEdbIds, ErrMsg: "+err.Error(), 3)
  2487. // }
  2488. // }()
  2489. //
  2490. // err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  2491. //
  2492. // if redisKey != `` {
  2493. // _ = utils.Rc.Delete(redisKey)
  2494. // }
  2495. // if len(refreshKeys) > 0 {
  2496. // for _, v := range refreshKeys {
  2497. // _ = utils.Rc.Delete(v)
  2498. // }
  2499. // }
  2500. //}()
  2501. // 加入队列刷新
  2502. var refreshItem EdbQueueRefreshReq
  2503. refreshItem.RefreshKey = redisKey
  2504. refreshItem.ItemRefreshKeys = refreshKeys
  2505. refreshItem.BaseEdbInfoArr = newBaseEdbInfoArr
  2506. refreshItem.BasePredictEdbInfoArr = newBasePredictEdbInfoArr
  2507. refreshItem.CalculateMap = newCalculateMap
  2508. refreshItem.PredictCalculateMap = newPredictCalculateMap
  2509. refreshItem.CalculateArr = calculateArr
  2510. refreshItem.PredictCalculateArr = predictCalculateArr
  2511. go PushEdb2Refresh(refreshItem)
  2512. return
  2513. }
  2514. // 同步刷新
  2515. err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  2516. if redisKey != `` {
  2517. _ = utils.Rc.Delete(redisKey)
  2518. }
  2519. if len(refreshKeys) > 0 {
  2520. for _, v := range refreshKeys {
  2521. _ = utils.Rc.Delete(v)
  2522. }
  2523. }
  2524. return
  2525. }
  2526. // EdbInfoSmmApiAdd 添加指标到指标库
  2527. func EdbInfoSmmApiAdd(item *data_manage.EdbInfo) (edbInfo *data_manage.EdbInfo, err error, errMsg string, isSendEmail bool) {
  2528. isSendEmail = true
  2529. //判断指标名称是否存在
  2530. var condition string
  2531. var pars []interface{}
  2532. condition += " AND edb_name=? "
  2533. pars = append(pars, item.EdbName)
  2534. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  2535. if err != nil {
  2536. errMsg = "判断指标名称是否存在失败"
  2537. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  2538. return
  2539. }
  2540. if count > 0 {
  2541. errMsg = "指标名称已存在,请重新填写"
  2542. err = errors.New(errMsg)
  2543. isSendEmail = false
  2544. return
  2545. }
  2546. source := item.Source
  2547. edbInfo = new(data_manage.EdbInfo)
  2548. edbInfo.Source = source
  2549. sourceNameMap := map[int]string{
  2550. utils.DATA_SOURCE_THS: "同花顺",
  2551. utils.DATA_SOURCE_WIND: "wind",
  2552. utils.DATA_SOURCE_PB: "彭博",
  2553. utils.DATA_SOURCE_PB_FINANCE: "彭博财务",
  2554. utils.DATA_SOURCE_MANUAL: "手工数据",
  2555. utils.DATA_SOURCE_LZ: "隆众",
  2556. utils.DATA_SOURCE_YS: "SMM",
  2557. utils.DATA_SOURCE_GL: "钢联",
  2558. utils.DATA_SOURCE_ZZ: "郑商所",
  2559. utils.DATA_SOURCE_DL: "大商所",
  2560. utils.DATA_SOURCE_SH: "上期所",
  2561. utils.DATA_SOURCE_CFFEX: "中金所",
  2562. utils.DATA_SOURCE_SHFE: "上期能源",
  2563. utils.DATA_SOURCE_GIE: "欧洲天然气",
  2564. utils.DATA_SOURCE_LT: "路透",
  2565. utils.DATA_SOURCE_COAL: "中国煤炭市场网",
  2566. utils.DATA_SOURCE_GOOGLE_TRAVEL: "our world in data",
  2567. utils.DATA_SOURCE_MYSTEEL_CHEMICAL: "钢联",
  2568. utils.DATA_SOURCE_EIA_STEO: "EIA STEO报告",
  2569. utils.DATA_SOURCE_COM_TRADE: "UN",
  2570. utils.DATA_SOURCE_SCI: "SCI",
  2571. utils.DATA_SOURCE_BAIINFO: "BAIINFO",
  2572. utils.DATA_SOURCE_STOCK_PLANT: "存量装置",
  2573. utils.DATA_SOURCE_NATIONAL_STATISTICS: "国家统计局",
  2574. utils.DATA_SOURCE_FUBAO: "富宝数据",
  2575. }
  2576. sourceName, ok := sourceNameMap[source]
  2577. if !ok {
  2578. edbSource := data_manage.EdbSourceIdMap[source]
  2579. if edbSource != nil {
  2580. sourceName = edbSource.SourceName
  2581. }
  2582. if sourceName == "" {
  2583. errMsg = "指标来源异常"
  2584. err = errors.New(errMsg)
  2585. return
  2586. }
  2587. }
  2588. edbInfo.SourceName = sourceName
  2589. edbType := 1 //基础指标
  2590. if source == utils.DATA_SOURCE_STOCK_PLANT {
  2591. edbType = 2 //计算指标
  2592. }
  2593. //从缓存中获取
  2594. serverUrl := ``
  2595. if edbInfo.Source == utils.DATA_SOURCE_WIND {
  2596. windCacheKey := utils.CACHE_WIND_URL + ":" + item.EdbCode
  2597. serverUrl, _ = utils.Rc.RedisString(windCacheKey)
  2598. if serverUrl == `` {
  2599. if len(utils.Hz_Data_WIND_Url_List) >= 1 {
  2600. serverUrl = utils.Hz_Data_WIND_Url_List[len(utils.Hz_Data_WIND_Url_List)-1] //默认是最后一个服务器地址
  2601. }
  2602. }
  2603. }
  2604. //获取该层级下最大的排序数
  2605. maxSort, err := GetEdbClassifyMaxSort(item.ClassifyId, 0)
  2606. if err != nil {
  2607. errMsg = "获取失败"
  2608. err = errors.New("查询排序信息失败,Err:" + err.Error())
  2609. return
  2610. }
  2611. edbInfo.EdbCode = item.EdbCode
  2612. edbInfo.EdbName = item.EdbName
  2613. edbInfo.EdbNameSource = item.EdbName
  2614. edbInfo.Frequency = item.Frequency
  2615. edbInfo.Unit = item.Unit
  2616. edbInfo.ClassifyId = item.ClassifyId
  2617. edbInfo.SysUserId = item.SysUserId
  2618. edbInfo.SysUserRealName = item.SysUserRealName
  2619. edbInfo.CreateTime = time.Now()
  2620. edbInfo.ModifyTime = time.Now()
  2621. edbInfo.ServerUrl = serverUrl
  2622. edbInfo.Sort = maxSort + 1
  2623. edbInfo.DataDateType = `交易日`
  2624. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2625. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  2626. edbInfo.EdbType = edbType
  2627. edbInfo.SubSource = item.SubSource
  2628. edbInfo.SubSourceName = ""
  2629. edbInfo.IndicatorCode = item.IndicatorCode
  2630. edbInfo.StockCode = item.StockCode
  2631. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  2632. if err != nil {
  2633. errMsg = "保存失败"
  2634. err = errors.New("保存失败,Err:" + err.Error())
  2635. return
  2636. }
  2637. edbInfo.EdbInfoId = int(edbInfoId)
  2638. //保存数据
  2639. err = data_manage.ModifyEdbInfoWindWsdDataStatus(source, item.SubSource, edbInfoId, item.EdbCode)
  2640. if err != nil {
  2641. errMsg = "保存失败"
  2642. err = errors.New("修改数据对应指标ID失败,Err:" + err.Error())
  2643. return
  2644. }
  2645. maxAndMinItem, _ := data_manage.GetEdbInfoWsdMaxAndMinInfo(source, item.SubSource, item.EdbCode)
  2646. if maxAndMinItem != nil {
  2647. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  2648. }
  2649. //添加es
  2650. AddOrEditEdbInfoToEs(int(edbInfoId))
  2651. return
  2652. }
  2653. // GetEdbTerminal 获取终端信息
  2654. func GetEdbTerminal(source int, oldTerminalCode string) (edbTerminal *data_manage.EdbTerminal, err error) {
  2655. if source == utils.DATA_SOURCE_WIND && oldTerminalCode == "" {
  2656. tmpConfig := new(data_manage.EdbConfig)
  2657. tmpConfig, err = data_manage.GetConfigDetailByCode("wind_terminal_code")
  2658. if err != nil && err.Error() != utils.ErrNoRow() {
  2659. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  2660. return
  2661. }
  2662. err = nil
  2663. if tmpConfig != nil {
  2664. oldTerminalCode = tmpConfig.ConfigValue
  2665. //return
  2666. }
  2667. }
  2668. if oldTerminalCode != "" {
  2669. edbTerminal, err = data_manage.GetEdbTerminalByCode(oldTerminalCode)
  2670. if err != nil {
  2671. return
  2672. }
  2673. return
  2674. }
  2675. // 配置中没有的话,那么就从分组获取,
  2676. list, err := data_manage.GetEdbCountGroupByTerminal(source)
  2677. if err != nil {
  2678. return
  2679. }
  2680. //windUrlNum := 0
  2681. //获取对应的配置url
  2682. terminalNumMap := make(map[string]int)
  2683. for _, v := range list {
  2684. terminalNumMap[v.TerminalCode] = v.Total
  2685. }
  2686. terminalList, err := data_manage.GetEdbTerminalListBySource(source)
  2687. if err != nil {
  2688. return
  2689. }
  2690. if len(terminalList) == 0 {
  2691. err = errors.New("终端地址未配置")
  2692. return
  2693. }
  2694. num := 0
  2695. for _, v := range terminalList {
  2696. tmpNum := terminalNumMap[v.TerminalCode]
  2697. if edbTerminal == nil {
  2698. edbTerminal = v
  2699. num = tmpNum
  2700. } else if tmpNum < num {
  2701. edbTerminal = v
  2702. num = tmpNum
  2703. }
  2704. }
  2705. if edbTerminal == nil {
  2706. err = errors.New("获取配置的终端地址失败")
  2707. return
  2708. }
  2709. /*if edbTerminal != nil {
  2710. windUrl = edbTerminal.ServerUrl
  2711. terminalCode = edbTerminal.TerminalCode
  2712. }*/
  2713. return
  2714. }
  2715. func GetEdbTerminalCodeBySource(source int, edbCode, stockCode string) (terminalCode, serverUrl, sourceIndexName string, err error) {
  2716. var e error
  2717. if stockCode != "" {
  2718. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + stockCode
  2719. terminalCode, e = utils.Rc.RedisString(terminalCodeCacheKey)
  2720. if e != nil {
  2721. utils.FileLog.Info(fmt.Sprintf("从缓存中获取数据源终端信息失败, Err: %s", e))
  2722. }
  2723. terminalInfo, e := GetEdbTerminal(source, terminalCode)
  2724. if e != nil {
  2725. err = errors.New("获取可以使用的终端失败,Err:" + e.Error())
  2726. return
  2727. }
  2728. serverUrl = terminalInfo.ServerUrl
  2729. terminalCode = terminalInfo.TerminalCode
  2730. return
  2731. }
  2732. // 获取终端信息
  2733. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
  2734. terminalCode, tE := utils.Rc.RedisString(terminalCodeCacheKey)
  2735. if tE != nil {
  2736. utils.FileLog.Info(fmt.Sprintf("从缓存中获取数据源终端信息失败, Err: %s", tE))
  2737. }
  2738. var terminalCodeOrigin string
  2739. terminalCodeOrigin, sourceIndexName, tE = GetTerminalFromBaseIndex(source, edbCode)
  2740. if tE != nil {
  2741. utils.FileLog.Info(fmt.Sprintf("获取数据源终端信息失败, Err: %s", tE))
  2742. }
  2743. if terminalCodeOrigin != "" {
  2744. terminalCode = terminalCodeOrigin
  2745. }
  2746. if source == utils.DATA_SOURCE_WIND || source == utils.DATA_SOURCE_THS {
  2747. terminalInfo, e := GetEdbTerminal(source, terminalCode)
  2748. if e != nil {
  2749. err = errors.New("获取可以使用的终端失败,Err:" + e.Error())
  2750. return
  2751. }
  2752. serverUrl = terminalInfo.ServerUrl
  2753. terminalCode = terminalInfo.TerminalCode
  2754. }
  2755. return
  2756. }
  2757. // handleByAddEdbInfo
  2758. // @Description: 添加指标后的处理操作
  2759. // @author: Roc
  2760. // @datetime 2024-07-22 13:06:36
  2761. // @param edbInfo *data_manage.EdbInfo
  2762. func handleByAddEdbInfo(edbInfo *data_manage.EdbInfo) {
  2763. // 更新钢联化工状态为启用
  2764. if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  2765. // 启动钢联的刷新
  2766. _ = data_manage.UpdateMysteelChemicalRefreshStatus(edbInfo.EdbCode, 0)
  2767. }
  2768. // 如果是手工数据,那么需要标记手工数据为已加入指标库
  2769. if edbInfo.Source == utils.DATA_SOURCE_MANUAL {
  2770. _ = models.UpdateManualIsJoinEdbStatus(edbInfo.EdbCode, 1)
  2771. }
  2772. }
  2773. // handleByDelEdbInfo
  2774. // @Description: 删除指标后的处理操作
  2775. // @author: Roc
  2776. // @datetime 2024-07-22 13:06:36
  2777. // @param edbInfo *data_manage.EdbInfo
  2778. func handleByDelEdbInfo(edbInfo *data_manage.EdbInfo) {
  2779. // 如果删除的指标是自定义分析的来源,那么还需要删除指标与excel的关系
  2780. if edbInfo.Source == utils.DATA_SOURCE_CALCULATE_ZDYFX {
  2781. err := excel.DeleteCustomAnalysisExcelEdbMappingByEdbInfoId(edbInfo.EdbInfoId)
  2782. if err != nil {
  2783. utils.FileLog.Error(fmt.Sprintf("删除指标时,需要删除与自定义分析的关系失败,指标ID:%d,Err:%s", edbInfo.EdbInfoId, err.Error()), 3)
  2784. }
  2785. }
  2786. // 如果是手工数据,那么需要标记手工数据为未加入指标库
  2787. if edbInfo.Source == utils.DATA_SOURCE_MANUAL {
  2788. _ = models.UpdateManualIsJoinEdbStatus(edbInfo.EdbCode, 0)
  2789. }
  2790. }
  2791. func EditBaseEdbInfo(req data_manage.EditEdbInfoReq, sysUser *system.Admin, lang, requestBody, uri string) (isSendEmail bool, err error, errMsg string) {
  2792. isSendEmail = true
  2793. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  2794. if err != nil {
  2795. errMsg = "修改失败"
  2796. err = fmt.Errorf("获取指标信息失败,Err:" + err.Error())
  2797. return
  2798. }
  2799. // 记录旧的指标基本信息
  2800. oldEdbName := edbInfo.EdbName
  2801. oldFrequency := edbInfo.Frequency
  2802. oldUnit := edbInfo.Unit
  2803. var haveOperaAuth bool
  2804. // 权限校验
  2805. {
  2806. haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, sysUser.AdminId)
  2807. if err != nil {
  2808. errMsg = err.Error()
  2809. err = fmt.Errorf("校验指标权限失败,err:" + err.Error())
  2810. return
  2811. }
  2812. }
  2813. // 编辑权限校验
  2814. button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
  2815. if !button.OpButton {
  2816. errMsg = "无权限操作"
  2817. isSendEmail = false
  2818. return
  2819. }
  2820. //判断指标名称是否存在
  2821. var condition string
  2822. var pars []interface{}
  2823. condition += " AND edb_info_id<>? "
  2824. pars = append(pars, req.EdbInfoId)
  2825. //condition += " AND source=? "
  2826. //pars = append(pars, edbInfo.Source)
  2827. updateCols := make([]string, 0)
  2828. switch lang {
  2829. case utils.EnLangVersion:
  2830. condition += " AND edb_name_en = ? "
  2831. edbInfo.EdbNameEn = req.EdbName
  2832. edbInfo.Frequency = req.Frequency
  2833. edbInfo.UnitEn = req.Unit
  2834. edbInfo.ClassifyId = req.ClassifyId
  2835. updateCols = append(updateCols, "EdbNameEn", "Frequency", "UnitEn", "ClassifyId")
  2836. default:
  2837. condition += " AND edb_name=? "
  2838. edbInfo.EdbName = req.EdbName
  2839. edbInfo.EdbNameSource = req.EdbName
  2840. edbInfo.Frequency = req.Frequency
  2841. edbInfo.Unit = req.Unit
  2842. edbInfo.ClassifyId = req.ClassifyId
  2843. updateCols = append(updateCols, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId")
  2844. }
  2845. pars = append(pars, req.EdbName)
  2846. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  2847. if err != nil {
  2848. errMsg = "判断指标名称是否存在失败"
  2849. err = fmt.Errorf("判断指标名称是否存在失败,Err:" + err.Error())
  2850. return
  2851. }
  2852. if count > 0 {
  2853. errMsg = "指标名称已存在,请重新填写"
  2854. err = fmt.Errorf("指标名称已存在,请重新填写")
  2855. isSendEmail = false
  2856. return
  2857. }
  2858. if len(updateCols) > 0 {
  2859. err = edbInfo.Update(updateCols)
  2860. if err != nil {
  2861. errMsg = "保存失败"
  2862. err = fmt.Errorf("修改失败,Err:" + err.Error())
  2863. return
  2864. }
  2865. }
  2866. // 新增保存记录日志
  2867. oldEdbInfo := new(data_manage.EdbInfo)
  2868. oldEdbInfo.EdbInfoId = edbInfo.EdbInfoId
  2869. oldEdbInfo.EdbName = oldEdbName
  2870. oldEdbInfo.Frequency = oldFrequency
  2871. oldEdbInfo.Unit = oldUnit
  2872. newEdbInfoRecord := new(data_manage.EdbInfoEditRecord)
  2873. newEdbInfoRecord.EdbName = req.EdbName
  2874. newEdbInfoRecord.Frequency = req.Frequency
  2875. newEdbInfoRecord.Unit = req.Unit
  2876. newEdbInfoRecord.OperateUserId = sysUser.AdminId
  2877. newEdbInfoRecord.OperateUserRealName = sysUser.RealName
  2878. go AddEditEdbInfoRcord(oldEdbInfo, newEdbInfoRecord)
  2879. //新增操作日志
  2880. {
  2881. edbLog := new(data_manage.EdbInfoLog)
  2882. edbLog.EdbInfoId = edbInfo.EdbInfoId
  2883. edbLog.SourceName = edbInfo.SourceName
  2884. edbLog.Source = edbInfo.Source
  2885. edbLog.EdbCode = edbInfo.EdbCode
  2886. edbLog.EdbName = edbInfo.EdbName
  2887. edbLog.ClassifyId = edbInfo.ClassifyId
  2888. edbLog.SysUserId = sysUser.AdminId
  2889. edbLog.SysUserRealName = sysUser.RealName
  2890. edbLog.CreateTime = time.Now()
  2891. edbLog.Content = requestBody
  2892. edbLog.Status = "编辑指标"
  2893. edbLog.Method = uri
  2894. go data_manage.AddEdbInfoLog(edbLog)
  2895. }
  2896. //添加es
  2897. AddOrEditEdbInfoToEs(req.EdbInfoId)
  2898. switch lang {
  2899. case utils.EnLangVersion:
  2900. // 修改关联的预测指标基础信息
  2901. go ModifyPredictEdbEnBaseInfoBySourceEdb(edbInfo, req.Unit)
  2902. default:
  2903. // 修改关联的预测指标基础信息
  2904. go ModifyPredictEdbBaseInfoBySourceEdb(edbInfo, req.Frequency, req.Unit)
  2905. }
  2906. return
  2907. }
  2908. // GetIsSupplierStop
  2909. // @Description: 获取是否供应商停更
  2910. // @author: Roc
  2911. // @datetime 2024-08-27 10:28:19
  2912. // @param source int
  2913. // @param edbCode string
  2914. // @return isSupplierStop int
  2915. func GetIsSupplierStop(source int, edbCode string) (isSupplierStop int) {
  2916. switch source {
  2917. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  2918. item, err := data_manage.GetBaseFromMysteelChemicalIndexByCode(edbCode)
  2919. if err != nil {
  2920. return
  2921. }
  2922. isSupplierStop = item.IsSupplierStop
  2923. }
  2924. return
  2925. }