edb_info.go 102 KB

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