edb_info.go 103 KB

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