edb_info.go 110 KB

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