edb_info.go 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models/data_manage"
  6. "eta/eta_mobile/services/alarm_msg"
  7. "eta/eta_mobile/services/elastic"
  8. "eta/eta_mobile/utils"
  9. "fmt"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // EdbInfoRefreshAllFromBaseV2 全部刷新指标(切换到edb_lib服务)
  16. func EdbInfoRefreshAllFromBaseV2(edbInfoId int, refreshAll bool) (err error, isAsync bool) {
  17. var errmsg string
  18. defer func() {
  19. if err != nil {
  20. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  21. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  22. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  23. }
  24. }()
  25. err, isAsync = EdbInfoRefreshAllFromBaseV3([]int{edbInfoId}, refreshAll, false)
  26. return
  27. //// 获取关联的基础指标
  28. //newBaseEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr, err, errmsg := getRefreshEdbInfoList(edbInfoId)
  29. //if err != nil {
  30. // return
  31. //}
  32. //
  33. //var startDate string
  34. //for _, bv := range newBaseEdbInfoArr {
  35. // //source := bv.Source
  36. // //edbInfoId := bv.EdbInfoId
  37. // //edbCode := bv.EdbCode
  38. // if bv.StartDate == "0000-00-00" {
  39. // continue
  40. // }
  41. // //开始时间
  42. // startDate = ``
  43. // if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  44. // sTime, err := time.Parse(utils.FormatDate, bv.StartDate)
  45. // if err != nil {
  46. // return err
  47. // }
  48. // startDate = sTime.Format(utils.FormatDate)
  49. // } else {
  50. // sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
  51. // if err != nil {
  52. // return err
  53. // }
  54. // frequency := bv.Frequency
  55. // var limitDay int
  56. // switch frequency {
  57. // case "日度":
  58. // limitDay = utils.DATA_START_REFRESH_LIMIT
  59. // case "周度":
  60. // limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  61. // case "月度":
  62. // limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  63. // case "季度":
  64. // limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  65. // case "年度":
  66. // limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  67. // default:
  68. // limitDay = utils.DATA_START_REFRESH_LIMIT
  69. // }
  70. // startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  71. // }
  72. // result, err := RefreshEdbData(bv.EdbInfoId, bv.Source, bv.EdbCode, startDate)
  73. // if err != nil {
  74. // fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err", time.Now())
  75. // errmsg = "RefreshBaseEdbData Err:" + err.Error()
  76. // return err
  77. // }
  78. // if result.Ret != 200 {
  79. // fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  80. // errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  81. // return fmt.Errorf("刷新失败, err:", errmsg)
  82. // }
  83. //
  84. // //maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
  85. // //if err != nil {
  86. // // if err.Error() == utils.ErrNoRow() { //找不到数据,那么就进入到下一条数据做处理
  87. // // continue
  88. // // }
  89. // // return err
  90. // //}
  91. // //if maxAndMinItem != nil {
  92. // // err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  93. // // if err != nil {
  94. // // return err
  95. // // }
  96. // //}
  97. // fmt.Println("end newBaseEdbInfoArr:", bv, time.Now())
  98. //}
  99. //
  100. ////刷新相关普通计算指标
  101. //for _, v := range calculateArr {
  102. // edbInfo := newCalculateMap[v]
  103. // if edbInfo == nil {
  104. // return err
  105. // }
  106. // startDate = edbInfo.StartDate
  107. // source := edbInfo.Source
  108. // if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  109. // startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  110. // } else {
  111. // if source == utils.DATA_SOURCE_CALCULATE {
  112. // startDate = ``
  113. // if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  114. // startDate = edbInfo.StartDate
  115. // } else {
  116. // sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  117. // if err != nil {
  118. // return err
  119. // }
  120. // frequency := edbInfo.Frequency
  121. // var limitDay int
  122. // switch frequency {
  123. // case "日度":
  124. // limitDay = utils.DATA_START_REFRESH_LIMIT
  125. // case "周度":
  126. // limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  127. // case "月度":
  128. // limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  129. // case "季度":
  130. // limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  131. // case "年度":
  132. // limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  133. // default:
  134. // limitDay = utils.DATA_START_REFRESH_LIMIT
  135. // }
  136. // startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  137. // }
  138. // }
  139. // }
  140. //
  141. // result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  142. // if err != nil {
  143. // fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  144. // errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  145. // return err
  146. // }
  147. // if result.Ret != 200 {
  148. // fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  149. // errmsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  150. // return fmt.Errorf("刷新失败")
  151. // }
  152. //}
  153. //
  154. ////刷新相关预测计算指标
  155. //for _, v := range predictCalculateArr {
  156. // edbInfo := newPredictCalculateMap[v]
  157. // if edbInfo == nil {
  158. // return err
  159. // }
  160. // startDate = edbInfo.StartDate
  161. // source := edbInfo.Source
  162. // if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  163. // startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  164. // } else {
  165. // if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  166. // startDate = ``
  167. // if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  168. // startDate = edbInfo.StartDate
  169. // } else {
  170. // sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  171. // if err != nil {
  172. // return err
  173. // }
  174. // frequency := edbInfo.Frequency
  175. // var limitDay int
  176. // switch frequency {
  177. // case "日度":
  178. // limitDay = utils.DATA_START_REFRESH_LIMIT
  179. // case "周度":
  180. // limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  181. // case "月度":
  182. // limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  183. // case "季度":
  184. // limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  185. // case "年度":
  186. // limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  187. // default:
  188. // limitDay = utils.DATA_START_REFRESH_LIMIT
  189. // }
  190. // startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  191. // }
  192. // }
  193. // }
  194. //
  195. // result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  196. // if err != nil {
  197. // fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  198. // errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  199. // return err
  200. // }
  201. // if result.Ret != 200 {
  202. // fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  203. // errmsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  204. // return fmt.Errorf("刷新失败")
  205. // }
  206. //}
  207. //return err
  208. }
  209. // 获取频度的英文版
  210. func GetFrequencyEn(frequency string) (frequencyEn string) {
  211. switch frequency {
  212. case "日度":
  213. frequencyEn = "day"
  214. return
  215. case "周度":
  216. frequencyEn = "week"
  217. return
  218. case "旬度":
  219. frequencyEn = "ten days"
  220. return
  221. case "月度":
  222. frequencyEn = "month"
  223. return
  224. case "季度":
  225. frequencyEn = "quarter"
  226. return
  227. case "年度":
  228. frequencyEn = "year"
  229. return
  230. }
  231. return
  232. }
  233. func GetLeadUnitEn(unit string) (unitEn string) {
  234. switch unit {
  235. case "天":
  236. unitEn = "day"
  237. return
  238. case "周":
  239. unitEn = "week"
  240. return
  241. case "月":
  242. unitEn = "month"
  243. return
  244. case "季":
  245. unitEn = "quarter"
  246. return
  247. case "年":
  248. unitEn = "year"
  249. return
  250. }
  251. return
  252. }
  253. // EdbInfoRefreshAllFromBaseV3Bak 全部刷新指标(切换到edb_lib服务)
  254. // @author Roc
  255. // @datetime 2022-09-16 11:04:44
  256. // @description 将原有的单个指标刷新,调整为批量多个指标刷新
  257. // DeprecatedTime 2023-10-23 09:38:19废弃
  258. // Deprecated
  259. func EdbInfoRefreshAllFromBaseV3Bak(edbInfoIdList []int, refreshAll, isSync bool) (err error, isAsync bool) {
  260. var errmsg string
  261. defer func() {
  262. if err != nil {
  263. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  264. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  265. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  266. }
  267. }()
  268. // 获取关联的基础指标
  269. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr, err, errmsg := getRefreshEdbInfoListByIds(edbInfoIdList)
  270. if err != nil {
  271. return
  272. }
  273. // 需要刷新的指标数量
  274. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  275. if totalEdbInfo <= 20 || isSync {
  276. err = edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  277. } else {
  278. isAsync = true
  279. go edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  280. }
  281. return
  282. }
  283. // EdbInfoRefreshAllFromBaseV3
  284. //
  285. // @Description: 全部刷新指标(切换到edb_lib服务)
  286. // @author: Roc
  287. // @datetime2023-10-23 09:57:55
  288. // @param edbInfoIdList []int
  289. // @param refreshAll bool
  290. // @param isSync bool 是否同步执行
  291. // @return err error
  292. // @return isAsync bool 是否异步刷新
  293. func EdbInfoRefreshAllFromBaseV3(edbInfoIdList []int, refreshAll, isSync bool) (err error, isAsync bool) {
  294. var errmsg string
  295. defer func() {
  296. if err != nil {
  297. fmt.Println("EdbInfoRefreshAllFromBaseV3 Err:" + err.Error() + ";errmsg:" + errmsg)
  298. go alarm_msg.SendAlarmMsg("EdbInfoRefreshAllFromBaseV3,Err"+err.Error()+";errMsg:"+errmsg, 3)
  299. }
  300. }()
  301. // 获取需要刷新的指标列表
  302. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbInfoIdList)
  303. // 需要刷新的指标数量
  304. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  305. if totalEdbInfo <= 20 || isSync {
  306. err = edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  307. } else {
  308. isAsync = true
  309. go edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  310. }
  311. return
  312. }
  313. // getEdbInfoIdList
  314. // @Description: 获取待更新的指标列表
  315. // @author: Roc
  316. // @datetime 2023-12-01 16:19:42
  317. // @param edbInfoIdList []int
  318. // @return newBaseEdbInfoArr []*data_manage.EdbInfo
  319. // @return newBasePredictEdbInfoArr []*data_manage.EdbInfo
  320. // @return newCalculateMap map[int]*data_manage.EdbInfo
  321. // @return newPredictCalculateMap map[int]*data_manage.EdbInfo
  322. // @return calculateArr []int
  323. // @return predictCalculateArr []int
  324. func getEdbInfoIdList(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
  325. traceEdbInfoList, err := TraceEdbInfoByEdbInfoIdList(edbInfoIdList)
  326. if err != nil {
  327. return
  328. }
  329. // existEdbInfoIdMap 已经处理了的指标id map
  330. existEdbInfoIdMap := make(map[int]int)
  331. // 基础指标
  332. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  333. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  334. newBaseMap := make(map[int]*data_manage.EdbInfo)
  335. newPredictBaseMap := make(map[int]*data_manage.EdbInfo)
  336. // 计算指标
  337. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  338. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  339. calculateArr = make([]int, 0)
  340. predictCalculateArr = make([]int, 0)
  341. // 获取关联指标
  342. for _, traceEdbInfo := range traceEdbInfoList {
  343. tmpBaseEdbInfoArr, tmpBasePredictEdbInfoArr, tmpCalculateMap, tmpPredictCalculateMap, _, _ := getRefreshEdbInfoListByTraceEdbInfo(traceEdbInfo, existEdbInfoIdMap)
  344. // 普通基础指标
  345. for _, edbInfo := range tmpBaseEdbInfoArr {
  346. if _, ok := newBaseMap[edbInfo.EdbInfoId]; !ok {
  347. newBaseMap[edbInfo.EdbInfoId] = edbInfo
  348. newBaseEdbInfoArr = append(newBaseEdbInfoArr, edbInfo)
  349. }
  350. }
  351. // 预测基础指标
  352. for _, edbInfo := range tmpBasePredictEdbInfoArr {
  353. if _, ok := newPredictBaseMap[edbInfo.EdbInfoId]; !ok {
  354. newPredictBaseMap[edbInfo.EdbInfoId] = edbInfo
  355. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, edbInfo)
  356. }
  357. }
  358. // 普通计算指标
  359. for _, edbInfo := range tmpCalculateMap {
  360. if _, ok := newCalculateMap[edbInfo.EdbInfoId]; !ok {
  361. newCalculateMap[edbInfo.EdbInfoId] = edbInfo
  362. calculateArr = append(calculateArr, edbInfo.EdbInfoId)
  363. }
  364. }
  365. // 预测计算指标
  366. for _, edbInfo := range tmpPredictCalculateMap {
  367. if _, ok := newPredictCalculateMap[edbInfo.EdbInfoId]; !ok {
  368. newPredictCalculateMap[edbInfo.EdbInfoId] = edbInfo
  369. predictCalculateArr = append(predictCalculateArr, edbInfo.EdbInfoId)
  370. }
  371. }
  372. }
  373. // 普通计算指标的id
  374. sort.Ints(calculateArr)
  375. // 预测计算指标的id
  376. sort.Ints(predictCalculateArr)
  377. return
  378. }
  379. // getRefreshEdbInfoListByTraceEdbInfo
  380. //
  381. // @Description: 根据溯源获取关联指标
  382. // @author: Roc
  383. // @datetime2023-10-23 10:55:01
  384. // @param traceEdbInfo data_manage.TraceEdbInfoResp
  385. // @param existEdbInfoIdMap map[int]int
  386. // @return newBaseEdbInfoArr []*data_manage.EdbInfo
  387. // @return newBasePredictEdbInfoArr []*data_manage.EdbInfo
  388. // @return newCalculateMap map[int]*data_manage.EdbInfo
  389. // @return newPredictCalculateMap map[int]*data_manage.EdbInfo
  390. // @return calculateArr []int
  391. // @return predictCalculateArr []int
  392. func getRefreshEdbInfoListByTraceEdbInfo(traceEdbInfo data_manage.TraceEdbInfoResp, existEdbInfoIdMap map[int]int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
  393. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  394. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  395. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  396. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  397. calculateArr = make([]int, 0)
  398. predictCalculateArr = make([]int, 0)
  399. _, ok := existEdbInfoIdMap[traceEdbInfo.EdbInfoId]
  400. if ok {
  401. return
  402. }
  403. existEdbInfoIdMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfoId
  404. switch traceEdbInfo.EdbInfoType {
  405. //0-普通指标; 1-预测指标
  406. case 0: // 0-普通指标
  407. if traceEdbInfo.EdbType == 1 { //1-基础指标
  408. newBaseEdbInfoArr = append(newBaseEdbInfoArr, traceEdbInfo.EdbInfo)
  409. } else if traceEdbInfo.EdbType == 2 { //2-计算指标
  410. newCalculateMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfo
  411. calculateArr = append(calculateArr, traceEdbInfo.EdbInfoId)
  412. }
  413. case 1: // 1-预测指标
  414. if traceEdbInfo.EdbType == 1 { //1-基础指标
  415. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, traceEdbInfo.EdbInfo)
  416. } else if traceEdbInfo.EdbType == 2 { //2-计算指标
  417. newPredictCalculateMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfo
  418. predictCalculateArr = append(predictCalculateArr, traceEdbInfo.EdbInfoId)
  419. }
  420. }
  421. if traceEdbInfo.Child != nil && len(traceEdbInfo.Child) > 0 {
  422. for _, v := range traceEdbInfo.Child {
  423. tmpBaseEdbInfoArr, tmpPredictEdbInfoArr, tmpCalculateMap, tmpPredictCalculateMap, tmpCalculateArr, tmpPredictCalculateArr := getRefreshEdbInfoListByTraceEdbInfo(v, existEdbInfoIdMap)
  424. newBaseEdbInfoArr = append(newBaseEdbInfoArr, tmpBaseEdbInfoArr...)
  425. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, tmpPredictEdbInfoArr...)
  426. for k, tmpEdbInfo := range tmpCalculateMap {
  427. newCalculateMap[k] = tmpEdbInfo
  428. }
  429. for k, tmpEdbInfo := range tmpPredictCalculateMap {
  430. newPredictCalculateMap[k] = tmpEdbInfo
  431. }
  432. calculateArr = append(calculateArr, tmpCalculateArr...)
  433. predictCalculateArr = append(predictCalculateArr, tmpPredictCalculateArr...)
  434. }
  435. }
  436. return
  437. }
  438. func edbInfoRefreshAll(refreshAll bool, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) (err error) {
  439. var errmsg string
  440. defer func() {
  441. if err != nil {
  442. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  443. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  444. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  445. }
  446. }()
  447. var startDate string
  448. for _, bv := range newBaseEdbInfoArr {
  449. //source := bv.Source
  450. //edbInfoId := bv.EdbInfoId
  451. //edbCode := bv.EdbCode
  452. if bv.StartDate == "0000-00-00" {
  453. continue
  454. }
  455. //开始时间
  456. startDate = ``
  457. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  458. sTime, err := time.Parse(utils.FormatDate, bv.StartDate)
  459. if err != nil {
  460. return err
  461. }
  462. startDate = sTime.Format(utils.FormatDate)
  463. } else {
  464. sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
  465. if err != nil {
  466. return err
  467. }
  468. frequency := bv.Frequency
  469. var limitDay int
  470. switch frequency {
  471. case "日度":
  472. limitDay = utils.DATA_START_REFRESH_LIMIT
  473. case "周度":
  474. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  475. case "月度":
  476. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  477. case "季度":
  478. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  479. case "年度":
  480. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  481. default:
  482. limitDay = utils.DATA_START_REFRESH_LIMIT
  483. }
  484. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  485. }
  486. result, err := RefreshEdbData(bv.EdbInfoId, bv.Source, bv.EdbCode, startDate)
  487. if err != nil {
  488. fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err", time.Now())
  489. errmsg = "RefreshBaseEdbData Err:" + err.Error()
  490. return err
  491. }
  492. if result.Ret != 200 {
  493. fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  494. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  495. return fmt.Errorf("刷新失败, err:", errmsg)
  496. }
  497. //maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
  498. //if err != nil {
  499. // if err.Error() == utils.ErrNoRow() { //找不到数据,那么就进入到下一条数据做处理
  500. // continue
  501. // }
  502. // return err
  503. //}
  504. //if maxAndMinItem != nil {
  505. // err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  506. // if err != nil {
  507. // return err
  508. // }
  509. //}
  510. fmt.Println("end newBaseEdbInfoArr:", bv, time.Now())
  511. }
  512. //刷新相关普通计算指标
  513. for _, v := range calculateArr {
  514. edbInfo := newCalculateMap[v]
  515. if edbInfo == nil {
  516. return err
  517. }
  518. //计算指标就不用开始时间了吧,因为存在说开始时间变更的情况
  519. startDate = ``
  520. if !refreshAll { //如果不是全部更新,那么以指标的开始时间作为刷新
  521. startDate = edbInfo.StartDate
  522. source := edbInfo.Source
  523. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  524. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  525. } else {
  526. if source == utils.DATA_SOURCE_CALCULATE {
  527. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  528. if err != nil {
  529. return err
  530. }
  531. frequency := edbInfo.Frequency
  532. var limitDay int
  533. switch frequency {
  534. case "日度":
  535. limitDay = utils.DATA_START_REFRESH_LIMIT
  536. case "周度":
  537. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  538. case "月度":
  539. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  540. case "季度":
  541. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  542. case "年度":
  543. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  544. default:
  545. limitDay = utils.DATA_START_REFRESH_LIMIT
  546. }
  547. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  548. }
  549. }
  550. }
  551. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  552. if err != nil {
  553. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  554. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  555. return err
  556. }
  557. if result.Ret != 200 {
  558. fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  559. errmsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  560. return fmt.Errorf("刷新失败")
  561. }
  562. }
  563. //刷新相关预测计算指标
  564. for _, v := range predictCalculateArr {
  565. edbInfo := newPredictCalculateMap[v]
  566. if edbInfo == nil {
  567. return err
  568. }
  569. startDate = edbInfo.StartDate
  570. source := edbInfo.Source
  571. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  572. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  573. } else {
  574. if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  575. startDate = ``
  576. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  577. startDate = edbInfo.StartDate
  578. } else {
  579. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  580. if err != nil {
  581. return err
  582. }
  583. frequency := edbInfo.Frequency
  584. var limitDay int
  585. switch frequency {
  586. case "日度":
  587. limitDay = utils.DATA_START_REFRESH_LIMIT
  588. case "周度":
  589. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  590. case "月度":
  591. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  592. case "季度":
  593. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  594. case "年度":
  595. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  596. default:
  597. limitDay = utils.DATA_START_REFRESH_LIMIT
  598. }
  599. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  600. }
  601. }
  602. }
  603. result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  604. if err != nil {
  605. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  606. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  607. return err
  608. }
  609. if result.Ret != 200 {
  610. fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  611. errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  612. return fmt.Errorf("刷新失败")
  613. }
  614. }
  615. // 4、更新动态环差值
  616. {
  617. predictEdbInfoIdList := make([]int, 0)
  618. for _, v := range newBasePredictEdbInfoArr {
  619. predictEdbInfoIdList = append(predictEdbInfoIdList, v.EdbInfoId)
  620. }
  621. fmt.Println("predictEdbConfCalculateMappingDetailList:", predictEdbInfoIdList)
  622. predictEdbConfCalculateMappingDetailList, err := data_manage.GetPredictEdbConfCalculateMappingDetailListByEdbInfoId(predictEdbInfoIdList)
  623. if err != nil {
  624. errmsg = fmt.Sprint("更新动态环差值时,获取数据失败:" + err.Error())
  625. return fmt.Errorf("刷新失败, err:", errmsg)
  626. }
  627. for _, bv := range predictEdbConfCalculateMappingDetailList {
  628. result, err := RefreshEdbData(bv.EdbInfoId, utils.DATA_SOURCE_PREDICT, bv.EdbCode, "")
  629. if err != nil {
  630. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err", time.Now())
  631. errmsg = "RefreshBasePredictEdbData Err:" + err.Error()
  632. return err
  633. }
  634. if result.Ret != 200 {
  635. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  636. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  637. return fmt.Errorf("刷新失败, err:%s", errmsg)
  638. }
  639. fmt.Println("end predictEdbConfCalculateMappingDetailList:", bv, time.Now())
  640. }
  641. }
  642. //5、再次刷新相关预测计算指标
  643. for _, v := range predictCalculateArr {
  644. edbInfo := newPredictCalculateMap[v]
  645. if edbInfo == nil {
  646. return err
  647. }
  648. startDate = edbInfo.StartDate
  649. source := edbInfo.Source
  650. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  651. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  652. } else {
  653. if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  654. startDate = ``
  655. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  656. startDate = edbInfo.StartDate
  657. } else {
  658. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  659. if err != nil {
  660. return err
  661. }
  662. frequency := edbInfo.Frequency
  663. var limitDay int
  664. switch frequency {
  665. case "日度":
  666. limitDay = utils.DATA_START_REFRESH_LIMIT
  667. case "周度":
  668. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  669. case "月度":
  670. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  671. case "季度":
  672. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  673. case "年度":
  674. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  675. default:
  676. limitDay = utils.DATA_START_REFRESH_LIMIT
  677. }
  678. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  679. }
  680. }
  681. }
  682. result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  683. if err != nil {
  684. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  685. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  686. return err
  687. }
  688. if result.Ret != 200 {
  689. fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  690. errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  691. return fmt.Errorf("刷新失败")
  692. }
  693. }
  694. return err
  695. }
  696. // getRefreshEdbInfoList 获取待更新的指标(普通基础指标、普通运算指标,预测运算指标)
  697. func getRefreshEdbInfoListByIds(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int, err error, errMsg string) {
  698. calculateList, err := data_manage.GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList)
  699. if err != nil && err.Error() != utils.ErrNoRow() {
  700. err = errors.New("GetEdbInfoAllCalculate Err:" + err.Error())
  701. return
  702. }
  703. // 获取指标信息
  704. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  705. if err != nil {
  706. err = errors.New("GetEdbInfoAllCalGetEdbInfoByIdr:" + err.Error())
  707. return
  708. }
  709. baseEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 基础普通指标
  710. calculateInfoArr := make([]*data_manage.EdbInfo, 0) //基础运算指标
  711. basePredictEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 预测普通指标
  712. predictCalculateInfoArr := make([]*data_manage.EdbInfo, 0) //预测运算指标
  713. for _, baseEdbInfo := range edbInfoList {
  714. if baseEdbInfo.EdbInfoType == 0 { //普通指标
  715. switch baseEdbInfo.EdbType {
  716. case 1: //基础指标
  717. baseEdbInfoArr = append(baseEdbInfoArr, baseEdbInfo)
  718. case 2: //计算指标
  719. calculateList = append(calculateList, baseEdbInfo)
  720. }
  721. } else { //预测指标
  722. switch baseEdbInfo.EdbType {
  723. case 1: //基础指标
  724. basePredictEdbInfoArr = append(basePredictEdbInfoArr, baseEdbInfo)
  725. case 2: //计算指标
  726. predictCalculateInfoArr = append(predictCalculateInfoArr, baseEdbInfo)
  727. }
  728. }
  729. }
  730. // 计算指标
  731. for _, v := range calculateList {
  732. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  733. if tmpErr != nil {
  734. err = tmpErr
  735. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  736. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  737. return
  738. }
  739. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  740. switch tmpEdbInfo.EdbInfoType {
  741. case 0: //普通基础指标
  742. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  743. case 1: //预测基础指标
  744. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  745. }
  746. }
  747. for _, tmpEdbInfo := range getCalculateInfoArr {
  748. switch tmpEdbInfo.EdbInfoType {
  749. case 0: //普通运算指标
  750. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  751. case 1: //预测运算指标
  752. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  753. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  754. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  755. } else {
  756. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  757. }
  758. }
  759. }
  760. switch v.EdbInfoType {
  761. case 0: //普通运算指标
  762. calculateInfoArr = append(calculateInfoArr, v)
  763. case 1: //预测指标
  764. if v.EdbType == 1 { //预测普通指标
  765. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  766. } else {
  767. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  768. }
  769. }
  770. }
  771. // 预测计算指标
  772. for _, v := range predictCalculateInfoArr {
  773. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  774. if tmpErr != nil {
  775. err = tmpErr
  776. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  777. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  778. return
  779. }
  780. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  781. switch tmpEdbInfo.EdbInfoType {
  782. case 0: //普通基础指标
  783. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  784. case 1: //预测基础指标
  785. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  786. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  787. } else {
  788. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  789. }
  790. //basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  791. }
  792. }
  793. for _, tmpEdbInfo := range getCalculateInfoArr {
  794. switch tmpEdbInfo.EdbInfoType {
  795. case 0: //普通运算指标
  796. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  797. case 1: //预测运算指标
  798. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  799. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  800. } else {
  801. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  802. }
  803. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  804. }
  805. }
  806. switch v.EdbInfoType {
  807. case 0: //普通运算指标
  808. calculateInfoArr = append(calculateInfoArr, v)
  809. case 1: //预测指标
  810. if v.EdbType == 1 { //预测普通指标
  811. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  812. } else {
  813. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  814. }
  815. }
  816. }
  817. // 第一次的计算指标map
  818. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  819. for _, v := range calculateInfoArr {
  820. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  821. calculateArr = append(calculateArr, v.EdbInfoId)
  822. }
  823. newCalculateMap[v.EdbInfoId] = v
  824. }
  825. // 基础预测指标
  826. if len(basePredictEdbInfoArr) > 0 {
  827. basePredictEdbInfoIdList := make([]int, 0)
  828. for _, v := range basePredictEdbInfoArr {
  829. basePredictEdbInfoIdList = append(basePredictEdbInfoIdList, v.EdbInfoId)
  830. }
  831. baseEdbInfoList, tmpErr := data_manage.GetPredictEdbInfoAllCalculate(basePredictEdbInfoIdList)
  832. if tmpErr != nil {
  833. err = tmpErr
  834. return
  835. }
  836. tmpCalculateList := make([]*data_manage.EdbInfo, 0)
  837. for _, v := range baseEdbInfoList {
  838. switch v.EdbType {
  839. case 1:
  840. baseEdbInfoArr = append(baseEdbInfoArr, v)
  841. case 2:
  842. // 如果不在第一次里面,那么在第二次 刷新预测指标的时候 需要加进去
  843. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  844. tmpCalculateList = append(tmpCalculateList, v)
  845. }
  846. }
  847. }
  848. // 这里是查询预测指标用到的 普通计算指标
  849. for _, v := range tmpCalculateList {
  850. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  851. if tmpErr != nil {
  852. err = tmpErr
  853. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  854. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  855. return
  856. }
  857. // 基础指标
  858. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  859. switch tmpEdbInfo.EdbInfoType {
  860. case 0: //普通基础指标
  861. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  862. case 1: //预测基础指标
  863. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  864. }
  865. }
  866. // 计算指标
  867. for _, tmpEdbInfo := range getCalculateInfoArr {
  868. switch tmpEdbInfo.EdbInfoType {
  869. case 0: //普通运算指标
  870. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  871. case 1: //预测运算指标
  872. if v.EdbType == 1 { //预测普通指标
  873. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  874. } else {
  875. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  876. }
  877. }
  878. }
  879. switch v.EdbInfoType {
  880. case 0: //普通运算指标
  881. calculateInfoArr = append(calculateInfoArr, v)
  882. case 1: //预测指标
  883. if v.EdbType == 1 { //预测普通指标
  884. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  885. } else { //预测运算指标
  886. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  887. }
  888. }
  889. }
  890. // 第二次计算指标的map
  891. for _, v := range calculateInfoArr {
  892. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  893. calculateArr = append(calculateArr, v.EdbInfoId)
  894. }
  895. newCalculateMap[v.EdbInfoId] = v
  896. }
  897. }
  898. // 普通基础指标去重
  899. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  900. baseMap := make(map[int]int)
  901. for _, v := range baseEdbInfoArr {
  902. if _, ok := baseMap[v.EdbInfoId]; !ok {
  903. newBaseEdbInfoArr = append(newBaseEdbInfoArr, v)
  904. }
  905. baseMap[v.EdbInfoId] = v.EdbInfoId
  906. }
  907. // 普通计算指标的id
  908. sort.Ints(calculateArr)
  909. // 普通预测指标去重
  910. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  911. basePredictMap := make(map[int]int)
  912. for _, v := range basePredictEdbInfoArr {
  913. if _, ok := basePredictMap[v.EdbInfoId]; !ok {
  914. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, v)
  915. }
  916. basePredictMap[v.EdbInfoId] = v.EdbInfoId
  917. }
  918. // 预测计算指标去重
  919. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  920. for _, v := range predictCalculateInfoArr {
  921. if _, ok := newPredictCalculateMap[v.EdbInfoId]; !ok {
  922. predictCalculateArr = append(predictCalculateArr, v.EdbInfoId)
  923. }
  924. newPredictCalculateMap[v.EdbInfoId] = v
  925. }
  926. // 预测计算指标的id
  927. sort.Ints(predictCalculateArr)
  928. return
  929. }
  930. // AddOrEditEdbInfoToEs 添加/修改ES中的指标
  931. func AddOrEditEdbInfoToEs(edbInfoId int) {
  932. //添加es
  933. itemInfo, _ := data_manage.GetEdbInfoByCondition("AND edb_info_id=?", []interface{}{edbInfoId})
  934. go elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  935. }
  936. // DeleteEdbInfoToEs 删除ES中的指标
  937. func DeleteEdbInfoToEs(edbInfoId int) {
  938. //添加es
  939. go elastic.EsDeleteEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(edbInfoId))
  940. }
  941. // EdbInfoAdd 添加指标到指标库
  942. func EdbInfoAdd(source, subSource, classifyId int, edbCode, edbName, frequency, unit, startDate, endDate string, sysUserId int, sysUserRealName string) (edbInfo *data_manage.EdbInfo, err error, errMsg string, isSendEmail bool) {
  943. isSendEmail = true
  944. //判断指标名称是否存在
  945. var condition string
  946. var pars []interface{}
  947. //condition += " AND source=? "
  948. //pars = append(pars, source)
  949. condition += " AND edb_name=? "
  950. pars = append(pars, edbName)
  951. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  952. if err != nil {
  953. errMsg = "判断指标名称是否存在失败"
  954. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  955. return
  956. }
  957. if count > 0 {
  958. errMsg = "指标名称已存在,请重新填写"
  959. err = errors.New(errMsg)
  960. isSendEmail = false
  961. return
  962. }
  963. edbInfo = new(data_manage.EdbInfo)
  964. edbInfo.Source = source
  965. sourceNameMap := map[int]string{
  966. utils.DATA_SOURCE_THS: "同花顺",
  967. utils.DATA_SOURCE_WIND: "wind",
  968. utils.DATA_SOURCE_PB: "彭博",
  969. utils.DATA_SOURCE_PB_FINANCE: "彭博财务",
  970. utils.DATA_SOURCE_MANUAL: "手工数据",
  971. utils.DATA_SOURCE_LZ: "隆众",
  972. utils.DATA_SOURCE_YS: "SMM",
  973. utils.DATA_SOURCE_GL: "钢联",
  974. utils.DATA_SOURCE_ZZ: "郑商所",
  975. utils.DATA_SOURCE_DL: "大商所",
  976. utils.DATA_SOURCE_SH: "上期所",
  977. utils.DATA_SOURCE_CFFEX: "中金所",
  978. utils.DATA_SOURCE_SHFE: "上期能源",
  979. utils.DATA_SOURCE_GIE: "欧洲天然气",
  980. utils.DATA_SOURCE_LT: "路透",
  981. utils.DATA_SOURCE_COAL: "中国煤炭网",
  982. utils.DATA_SOURCE_GOOGLE_TRAVEL: "our world in data",
  983. utils.DATA_SOURCE_MYSTEEL_CHEMICAL: "钢联化工",
  984. utils.DATA_SOURCE_EIA_STEO: "EIA STEO报告",
  985. utils.DATA_SOURCE_COM_TRADE: "UN",
  986. utils.DATA_SOURCE_SCI: "SCI",
  987. utils.DATA_SOURCE_BAIINFO: "BAIINFO",
  988. utils.DATA_SOURCE_STOCK_PLANT: "存量装置",
  989. utils.DATA_SOURCE_NATIONAL_STATISTICS: "国家统计局",
  990. utils.DATA_SOURCE_FUBAO: "富宝数据",
  991. }
  992. sourceName, ok := sourceNameMap[source]
  993. if !ok {
  994. edbSource := data_manage.EdbSourceIdMap[source]
  995. if edbSource != nil {
  996. sourceName = edbSource.SourceName
  997. }
  998. if sourceName == "" {
  999. errMsg = "指标来源异常"
  1000. err = errors.New(errMsg)
  1001. return
  1002. }
  1003. }
  1004. edbInfo.SourceName = sourceName
  1005. edbType := 1 //基础指标
  1006. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1007. edbType = 2 //计算指标
  1008. }
  1009. //从缓存中获取
  1010. serverUrl := ``
  1011. if edbInfo.Source == utils.DATA_SOURCE_WIND {
  1012. windCacheKey := utils.CACHE_WIND_URL + ":" + edbCode
  1013. serverUrl, _ = utils.Rc.RedisString(windCacheKey)
  1014. if serverUrl == `` {
  1015. if len(utils.Hz_Data_WIND_Url_List) >= 1 {
  1016. serverUrl = utils.Hz_Data_WIND_Url_List[len(utils.Hz_Data_WIND_Url_List)-1] //默认是最后一个服务器地址
  1017. }
  1018. }
  1019. }
  1020. //获取该层级下最大的排序数
  1021. maxSort, err := GetEdbClassifyMaxSort(classifyId, 0)
  1022. if err != nil {
  1023. errMsg = "获取失败"
  1024. err = errors.New("查询排序信息失败,Err:" + err.Error())
  1025. return
  1026. }
  1027. edbInfo.EdbCode = edbCode
  1028. edbInfo.EdbName = edbName
  1029. edbInfo.EdbNameSource = edbName
  1030. edbInfo.Frequency = frequency
  1031. edbInfo.Unit = unit
  1032. edbInfo.StartDate = startDate
  1033. edbInfo.EndDate = endDate
  1034. edbInfo.ClassifyId = classifyId
  1035. edbInfo.SysUserId = sysUserId
  1036. edbInfo.SysUserRealName = sysUserRealName
  1037. edbInfo.CreateTime = time.Now()
  1038. edbInfo.ModifyTime = time.Now()
  1039. edbInfo.ServerUrl = serverUrl
  1040. edbInfo.Sort = maxSort + 1
  1041. edbInfo.DataDateType = `交易日`
  1042. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1043. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  1044. itemVal, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  1045. if itemVal != nil && err == nil {
  1046. edbInfo.MaxValue = itemVal.MaxValue
  1047. edbInfo.MinValue = itemVal.MinValue
  1048. }
  1049. edbInfo.EdbType = edbType
  1050. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  1051. if err != nil {
  1052. errMsg = "保存失败"
  1053. err = errors.New("保存失败,Err:" + err.Error())
  1054. return
  1055. }
  1056. edbInfo.EdbInfoId = int(edbInfoId)
  1057. //保存数据
  1058. data_manage.ModifyEdbInfoDataStatus(edbInfoId, source, subSource, edbCode)
  1059. //处理同名指标
  1060. //{
  1061. // edbNameList, tmpErr := data_manage.GetEdbInfoByName(edbName)
  1062. // if tmpErr != nil {
  1063. // errMsg = "保存失败"
  1064. // err = errors.New("获取指标信息失败,Err:" + tmpErr.Error())
  1065. // return
  1066. // }
  1067. // if len(edbNameList) >= 2 {
  1068. // for _, v := range edbNameList {
  1069. // edbName := v.EdbName + "(" + v.SourceName + ")"
  1070. // err = data_manage.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  1071. // if err != nil {
  1072. // errMsg = "保存失败"
  1073. // err = errors.New("修改指标名称失败,Err:" + err.Error())
  1074. // return
  1075. // }
  1076. // }
  1077. // }
  1078. //}
  1079. //上传到ES
  1080. //indexName := utils.DATA_INDEX_NAME
  1081. //docId := strconv.Itoa(int(edbInfoId))
  1082. //
  1083. //go data.EsAddOrEditEdbInfo(indexName, docId, edbInfo)
  1084. maxAndMinItem, _ := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  1085. if maxAndMinItem != nil {
  1086. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  1087. }
  1088. //添加es
  1089. AddOrEditEdbInfoToEs(int(edbInfoId))
  1090. return
  1091. }
  1092. // TraceEdbInfoByEdbInfoIdList 指标追溯
  1093. func TraceEdbInfoByEdbInfoIdList(edbInfoIdList []int) (traceEdbInfoList []data_manage.TraceEdbInfoResp, err error) {
  1094. traceEdbInfoList = make([]data_manage.TraceEdbInfoResp, 0)
  1095. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1096. if err != nil {
  1097. return
  1098. }
  1099. edbInfoRuleMap := make(map[int]string, 0)
  1100. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  1101. findIdMap := make(map[int]int)
  1102. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  1103. for _, edbInfo := range edbInfoList {
  1104. findIdMap[edbInfo.EdbInfoId] = edbInfo.EdbInfoId
  1105. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  1106. traceEdbInfo := data_manage.TraceEdbInfoResp{
  1107. //EdbInfoId: edbInfo.EdbInfoId,
  1108. EdbInfoId: edbInfo.EdbInfoId,
  1109. EdbInfoType: edbInfo.EdbInfoType,
  1110. EdbName: edbInfo.EdbName,
  1111. EdbType: edbInfo.EdbType,
  1112. //Source: edbInfo.Source,
  1113. UniqueCode: edbInfo.UniqueCode,
  1114. ClassifyId: edbInfo.ClassifyId,
  1115. EdbInfo: edbInfo,
  1116. }
  1117. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfo.EdbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1118. traceEdbInfoList = append(traceEdbInfoList, traceEdbInfo)
  1119. }
  1120. //findEdbInfoIdList := make([]int, 0)
  1121. //for _, v := range findIdMap {
  1122. // findEdbInfoIdList = append(findEdbInfoIdList, v)
  1123. //}
  1124. //findEdbInfoList, err := data_manage.GetEdbInfoByIdList(findEdbInfoIdList)
  1125. //if err != nil {
  1126. // return
  1127. //}
  1128. //edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1129. //for _, tmpEdbInfo := range findEdbInfoList {
  1130. // edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  1131. //}
  1132. //for k, traceEdbInfo := range traceEdbInfoList {
  1133. // traceEdbInfoList[k], err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
  1134. //}
  1135. return
  1136. }
  1137. // TraceEdbInfoByEdbInfoId 指标追溯
  1138. func TraceEdbInfoByEdbInfoId(edbInfoId int) (traceEdbInfo data_manage.TraceEdbInfoResp, err error) {
  1139. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1140. if err != nil {
  1141. return
  1142. }
  1143. edbInfoRuleMap := make(map[int]string, 0)
  1144. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  1145. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  1146. traceEdbInfo = data_manage.TraceEdbInfoResp{
  1147. //EdbInfoId: edbInfo.EdbInfoId,
  1148. EdbInfoId: edbInfoId,
  1149. EdbName: edbInfo.EdbName,
  1150. EdbInfo: edbInfo,
  1151. }
  1152. findIdMap := make(map[int]int)
  1153. findIdMap[edbInfoId] = edbInfoId
  1154. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  1155. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1156. edbInfoIdList := make([]int, 0)
  1157. for _, v := range findIdMap {
  1158. edbInfoIdList = append(edbInfoIdList, v)
  1159. }
  1160. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1161. if err != nil {
  1162. return
  1163. }
  1164. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1165. for _, tmpEdbInfo := range edbInfoList {
  1166. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  1167. }
  1168. traceEdbInfo, err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
  1169. return
  1170. }
  1171. // traceEdbInfoByEdbInfoId 指标追溯
  1172. 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) {
  1173. traceEdbInfo, ok := existMap[edbInfoId]
  1174. if ok {
  1175. return
  1176. }
  1177. child = make([]data_manage.TraceEdbInfoResp, 0)
  1178. edbInfoMappingList, err := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  1179. if err != nil {
  1180. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", err.Error())
  1181. return
  1182. }
  1183. // 指标信息map
  1184. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1185. if len(edbInfoMappingList) > 0 {
  1186. fromEdbInfoIdList := make([]int, 0)
  1187. for _, v := range edbInfoMappingList {
  1188. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  1189. }
  1190. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(fromEdbInfoIdList)
  1191. if tmpErr != nil {
  1192. err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", tmpErr.Error())
  1193. return
  1194. }
  1195. for _, v := range edbInfoList {
  1196. edbInfoMap[v.EdbInfoId] = v
  1197. }
  1198. }
  1199. edbMappingMap[edbInfoId] = edbInfoMappingList
  1200. for _, v := range edbInfoMappingList {
  1201. tmpEdbInfoId := v.FromEdbInfoId
  1202. tmpTraceEdbInfo := data_manage.TraceEdbInfoResp{
  1203. EdbInfoId: tmpEdbInfoId,
  1204. EdbInfo: edbInfoMap[v.FromEdbInfoId],
  1205. //EdbName: v.EdbName,
  1206. //RuleTitle: edbInfoRuleMap[v.EdbInfoId],
  1207. //Child: make([]data_manage.TraceEdbInfoResp, 0),
  1208. }
  1209. if v.EdbType == 2 { // 计算指标
  1210. // 查过了就不查了
  1211. if _, ok2 := findIdMap[tmpEdbInfoId]; !ok2 {
  1212. tmpTraceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(tmpEdbInfoId, tmpTraceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1213. }
  1214. //else {
  1215. // tmpTraceEdbInfo = existMap[v.EdbInfoId]
  1216. //}
  1217. }
  1218. child = append(child, tmpTraceEdbInfo)
  1219. findIdMap[tmpEdbInfoId] = tmpEdbInfoId
  1220. //existMap[v.EdbInfoId] = tmpTraceEdbInfo
  1221. }
  1222. existMap[edbInfoId] = traceEdbInfo
  1223. return
  1224. }
  1225. 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) {
  1226. edbInfo, ok := edbInfoMap[traceEdbInfoResp.EdbInfoId]
  1227. if !ok {
  1228. err = errors.New("指标异常")
  1229. return
  1230. }
  1231. var parentEdbInfo *data_manage.EdbInfo
  1232. if parentEdbInfoId > 0 {
  1233. parentEdbInfo, ok = edbInfoMap[parentEdbInfoId]
  1234. if !ok {
  1235. err = errors.New("指标异常")
  1236. return
  1237. }
  1238. }
  1239. //traceEdbInfoResp.EdbName = edbInfo.EdbName
  1240. traceEdbInfoResp.EdbName, traceEdbInfoResp.RuleTitle = getEdbRuleTitle(edbInfo, parentEdbInfo, traceEdbInfoResp.Child, edbInfoMap, edbMappingMap)
  1241. if traceEdbInfoResp.Child != nil && len(traceEdbInfoResp.Child) > 0 {
  1242. for k, v := range traceEdbInfoResp.Child {
  1243. traceEdbInfoResp.Child[k], err = handleTraceEdbInfo(v, traceEdbInfoResp.EdbInfoId, edbInfoMap, edbMappingMap)
  1244. if err != nil {
  1245. return
  1246. }
  1247. }
  1248. }
  1249. newTraceEdbInfoResp = traceEdbInfoResp
  1250. return
  1251. }
  1252. // getEdbRule 获取规则名称
  1253. 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) {
  1254. edbName = edbInfo.EdbName
  1255. ruleTitle = `来源于` + edbInfo.SourceName
  1256. if edbInfo.EdbType == 1 {
  1257. // 基础指标的话,直接返回来源名称
  1258. //ruleTitle = `来源于`+edbInfo.SourceName
  1259. return
  1260. }
  1261. if parentEdbInfo != nil {
  1262. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  1263. if !ok {
  1264. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  1265. }
  1266. // 指标名称
  1267. switch parentEdbInfo.Source {
  1268. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  1269. for _, v := range edbMappingList {
  1270. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1271. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  1272. }
  1273. }
  1274. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ, utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 直接拼接 ,累计值同比拼接
  1275. for _, v := range edbMappingList {
  1276. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1277. tmpName := ``
  1278. if v.FromTag == `A` {
  1279. tmpName = `拼接日期前`
  1280. } else if v.FromTag == `B` {
  1281. tmpName = `拼接日期后`
  1282. }
  1283. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  1284. }
  1285. }
  1286. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  1287. for _, v := range edbMappingList {
  1288. //(需对上游指标+自变量,领先10天/因变量)
  1289. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1290. tmpName := ``
  1291. if v.FromTag == `A` {
  1292. tmpName = fmt.Sprintf(`自变量,领先%d天`, v.MoveValue)
  1293. } else if v.FromTag == `B` {
  1294. tmpName = `因变量`
  1295. }
  1296. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  1297. }
  1298. }
  1299. case utils.DATA_SOURCE_CALCULATE_CORRELATION: // 滚动相关性
  1300. for _, v := range edbMappingList {
  1301. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1302. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  1303. }
  1304. }
  1305. }
  1306. }
  1307. // 规则
  1308. switch edbInfo.Source {
  1309. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  1310. ruleTitle = "=" + edbInfo.CalculateFormula
  1311. case utils.DATA_SOURCE_CALCULATE_LJZZY, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  1312. ruleTitle = `累计转月值计算`
  1313. case utils.DATA_SOURCE_CALCULATE_TBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  1314. ruleTitle = `同比值计算`
  1315. case utils.DATA_SOURCE_CALCULATE_TCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  1316. ruleTitle = `同差值计算`
  1317. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  1318. ruleTitle = fmt.Sprintf("N数值移动均值计算(N=%s)", edbInfo.CalculateFormula)
  1319. case utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  1320. ruleTitle = fmt.Sprintf("N数值环比值计算(N=%s)", edbInfo.CalculateFormula)
  1321. case utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  1322. ruleTitle = fmt.Sprintf("N数值环差值计算(N=%s)", edbInfo.CalculateFormula)
  1323. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  1324. moveType := `领先`
  1325. if edbInfo.MoveType == 2 {
  1326. moveType = "滞后"
  1327. }
  1328. ruleTitle = fmt.Sprintf("时间移位计算(%s%s%s)", moveType, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  1329. case utils.DATA_SOURCE_CALCULATE_BP, utils.DATA_SOURCE_PREDICT_CALCULATE_BP: // 变频
  1330. childFrequency := ``
  1331. if len(childList) > 0 {
  1332. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  1333. childFrequency = childEdbInfo.Frequency
  1334. }
  1335. }
  1336. ruleTitle = fmt.Sprintf("升频计算(%s转%s)", childFrequency, edbInfo.Frequency)
  1337. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: // 直接拼接
  1338. ruleTitle = fmt.Sprintf("直接拼接计算(%s)", edbInfo.CalculateFormula)
  1339. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 累计值同比拼
  1340. ruleTitle = fmt.Sprintf("累计值同比值拼接计算(%s)", edbInfo.CalculateFormula)
  1341. case utils.DATA_SOURCE_PYTHON:
  1342. ruleTitle = `代码运算`
  1343. case utils.DATA_SOURCE_CALCULATE_CJJX, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  1344. ruleTitle = fmt.Sprintf("超季节性计算(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  1345. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  1346. var startDate, endDate string
  1347. dateList := strings.Split(edbInfo.CalculateFormula, ",")
  1348. if len(dateList) == 2 {
  1349. startDate = dateList[0]
  1350. endDate = dateList[1]
  1351. }
  1352. ruleTitle = fmt.Sprintf("拟合残差计算(%s至%s)", startDate, endDate)
  1353. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  1354. ruleTitle = `数据调整`
  1355. case utils.DATA_SOURCE_CALCULATE_NH, utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  1356. ruleTitle = `年化计算`
  1357. case utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: // 扩散指数->53
  1358. type KszsConfig struct {
  1359. DateType int `description:"扩散指标日期;1:全部指标日期并集;2:部分指标日期并集"`
  1360. CheckList []string `description:"选中的数据,A,B,C"`
  1361. }
  1362. var config KszsConfig
  1363. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &config)
  1364. if err != nil {
  1365. return
  1366. }
  1367. var startDate, endDate time.Time
  1368. childEdbInfoIdList := make([]int, 0)
  1369. if config.DateType == 1 {
  1370. for _, v := range childList {
  1371. childEdbInfoIdList = append(childEdbInfoIdList, v.EdbInfoId)
  1372. }
  1373. } else {
  1374. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  1375. if !ok {
  1376. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  1377. }
  1378. tagMap := make(map[string]int)
  1379. for _, v := range edbMappingList {
  1380. tagMap[v.FromTag] = v.FromEdbInfoId
  1381. }
  1382. for _, v := range config.CheckList {
  1383. if tmpEdbInfoId, ok := tagMap[v]; ok {
  1384. childEdbInfoIdList = append(childEdbInfoIdList, tmpEdbInfoId)
  1385. }
  1386. }
  1387. }
  1388. for _, v := range childEdbInfoIdList {
  1389. if childEdbInfo, ok := edbInfoMap[v]; ok {
  1390. tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.StartDate, time.Local)
  1391. if tmpErr != nil {
  1392. return
  1393. }
  1394. if startDate.IsZero() || startDate.After(tmpStartDate) {
  1395. startDate = tmpStartDate
  1396. }
  1397. tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.EndDate, time.Local)
  1398. if tmpErr != nil {
  1399. return
  1400. }
  1401. if endDate.IsZero() || endDate.Before(tmpEndDate) {
  1402. endDate = tmpEndDate
  1403. }
  1404. }
  1405. }
  1406. ruleTitle = fmt.Sprintf("扩散指数计算(%s至%s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  1407. case utils.DATA_SOURCE_STOCK_PLANT:
  1408. ruleTitle = `来源于装置分析`
  1409. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  1410. type EdbCalculateFormula struct {
  1411. BaseCalculateValue int `description:"基础计算窗口"`
  1412. BaseCalculateUnit string `description:"基础计算频度"`
  1413. LeadValue int `description:"领先期数"`
  1414. LeadUnit string `description:"频度"`
  1415. CalculateValue int `description:"计算窗口"`
  1416. CalculateUnit string `description:"计算频度"`
  1417. }
  1418. var correlationConf EdbCalculateFormula
  1419. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &correlationConf)
  1420. if err != nil {
  1421. return
  1422. }
  1423. ruleTitle = fmt.Sprintf("滚动相关性(计算窗口%d%s,B领先A%d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  1424. case utils.DATA_SOURCE_CALCULATE_JP, utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  1425. childFrequency := ``
  1426. if len(childList) > 0 {
  1427. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  1428. childFrequency = childEdbInfo.Frequency
  1429. }
  1430. }
  1431. ruleTitle = fmt.Sprintf("降频计算(%s转%s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  1432. }
  1433. return
  1434. }
  1435. // EdbInfoAdd 添加指标到指标库
  1436. func EdbInfoWsdAdd(item *data_manage.EdbInfo) (edbInfo *data_manage.EdbInfo, err error, errMsg string, isSendEmail bool) {
  1437. isSendEmail = true
  1438. //判断指标名称是否存在
  1439. var condition string
  1440. var pars []interface{}
  1441. condition += " AND edb_name=? "
  1442. pars = append(pars, item.EdbName)
  1443. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1444. if err != nil {
  1445. errMsg = "判断指标名称是否存在失败"
  1446. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  1447. return
  1448. }
  1449. if count > 0 {
  1450. errMsg = "指标名称已存在,请重新填写"
  1451. err = errors.New(errMsg)
  1452. isSendEmail = false
  1453. return
  1454. }
  1455. source := item.Source
  1456. edbInfo = new(data_manage.EdbInfo)
  1457. edbInfo.Source = source
  1458. sourceNameMap := map[int]string{
  1459. utils.DATA_SOURCE_THS: "同花顺",
  1460. utils.DATA_SOURCE_WIND: "wind",
  1461. utils.DATA_SOURCE_PB: "彭博",
  1462. utils.DATA_SOURCE_PB_FINANCE: "彭博财务",
  1463. utils.DATA_SOURCE_MANUAL: "手工数据",
  1464. utils.DATA_SOURCE_LZ: "隆众",
  1465. utils.DATA_SOURCE_YS: "SMM",
  1466. utils.DATA_SOURCE_GL: "钢联",
  1467. utils.DATA_SOURCE_ZZ: "郑商所",
  1468. utils.DATA_SOURCE_DL: "大商所",
  1469. utils.DATA_SOURCE_SH: "上期所",
  1470. utils.DATA_SOURCE_CFFEX: "中金所",
  1471. utils.DATA_SOURCE_SHFE: "上期能源",
  1472. utils.DATA_SOURCE_GIE: "欧洲天然气",
  1473. utils.DATA_SOURCE_LT: "路透",
  1474. utils.DATA_SOURCE_COAL: "中国煤炭网",
  1475. utils.DATA_SOURCE_GOOGLE_TRAVEL: "our world in data",
  1476. utils.DATA_SOURCE_MYSTEEL_CHEMICAL: "钢联化工",
  1477. utils.DATA_SOURCE_EIA_STEO: "EIA STEO报告",
  1478. utils.DATA_SOURCE_COM_TRADE: "UN",
  1479. utils.DATA_SOURCE_SCI: "SCI",
  1480. utils.DATA_SOURCE_BAIINFO: "BAIINFO",
  1481. utils.DATA_SOURCE_STOCK_PLANT: "存量装置",
  1482. utils.DATA_SOURCE_NATIONAL_STATISTICS: "国家统计局",
  1483. utils.DATA_SOURCE_FUBAO: "富宝数据",
  1484. }
  1485. sourceName, ok := sourceNameMap[source]
  1486. if !ok {
  1487. edbSource := data_manage.EdbSourceIdMap[source]
  1488. if edbSource != nil {
  1489. sourceName = edbSource.SourceName
  1490. }
  1491. if sourceName == "" {
  1492. errMsg = "指标来源异常"
  1493. err = errors.New(errMsg)
  1494. return
  1495. }
  1496. }
  1497. edbInfo.SourceName = sourceName
  1498. edbType := 1 //基础指标
  1499. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1500. edbType = 2 //计算指标
  1501. }
  1502. //从缓存中获取
  1503. serverUrl := ``
  1504. if edbInfo.Source == utils.DATA_SOURCE_WIND {
  1505. windCacheKey := utils.CACHE_WIND_URL + ":" + item.EdbCode
  1506. serverUrl, _ = utils.Rc.RedisString(windCacheKey)
  1507. if serverUrl == `` {
  1508. if len(utils.Hz_Data_WIND_Url_List) >= 1 {
  1509. serverUrl = utils.Hz_Data_WIND_Url_List[len(utils.Hz_Data_WIND_Url_List)-1] //默认是最后一个服务器地址
  1510. }
  1511. }
  1512. }
  1513. //获取该层级下最大的排序数
  1514. maxSort, err := GetEdbClassifyMaxSort(item.ClassifyId, 0)
  1515. if err != nil {
  1516. errMsg = "获取失败"
  1517. err = errors.New("查询排序信息失败,Err:" + err.Error())
  1518. return
  1519. }
  1520. edbInfo.EdbCode = item.EdbCode
  1521. edbInfo.EdbName = item.EdbName
  1522. edbInfo.EdbNameSource = item.EdbName
  1523. edbInfo.Frequency = item.Frequency
  1524. edbInfo.Unit = item.Unit
  1525. edbInfo.ClassifyId = item.ClassifyId
  1526. edbInfo.SysUserId = item.SysUserId
  1527. edbInfo.SysUserRealName = item.SysUserRealName
  1528. edbInfo.CreateTime = time.Now()
  1529. edbInfo.ModifyTime = time.Now()
  1530. edbInfo.ServerUrl = serverUrl
  1531. edbInfo.Sort = maxSort + 1
  1532. edbInfo.DataDateType = `交易日`
  1533. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1534. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  1535. edbInfo.EdbType = edbType
  1536. edbInfo.SubSource = item.SubSource
  1537. edbInfo.SubSourceName = "日期序列"
  1538. edbInfo.IndicatorCode = item.IndicatorCode
  1539. edbInfo.StockCode = item.StockCode
  1540. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  1541. if err != nil {
  1542. errMsg = "保存失败"
  1543. err = errors.New("保存失败,Err:" + err.Error())
  1544. return
  1545. }
  1546. edbInfo.EdbInfoId = int(edbInfoId)
  1547. //保存数据
  1548. err = data_manage.ModifyEdbInfoWindWsdDataStatus(source, item.SubSource, edbInfoId, item.EdbCode)
  1549. if err != nil {
  1550. errMsg = "保存失败"
  1551. err = errors.New("修改数据对应指标ID失败,Err:" + err.Error())
  1552. return
  1553. }
  1554. maxAndMinItem, _ := data_manage.GetEdbInfoWsdMaxAndMinInfo(source, item.SubSource, item.EdbCode)
  1555. if maxAndMinItem != nil {
  1556. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  1557. }
  1558. //添加es
  1559. AddOrEditEdbInfoToEs(int(edbInfoId))
  1560. return
  1561. }