edb_info.go 109 KB

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