edb_info.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_ETA_mobile_api/models/data_manage"
  7. "hongze/hongze_ETA_mobile_api/services/alarm_msg"
  8. "hongze/hongze_ETA_mobile_api/services/elastic"
  9. "hongze/hongze_ETA_mobile_api/utils"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // 获取频度的英文版
  16. func GetFrequencyEn(frequency string) (frequencyEn string) {
  17. switch frequency {
  18. case "日度":
  19. frequencyEn = "day"
  20. return
  21. case "周度":
  22. frequencyEn = "week"
  23. return
  24. case "旬度":
  25. frequencyEn = "ten days"
  26. return
  27. case "月度":
  28. frequencyEn = "month"
  29. return
  30. case "季度":
  31. frequencyEn = "quarter"
  32. return
  33. case "年度":
  34. frequencyEn = "year"
  35. return
  36. }
  37. return
  38. }
  39. func GetLeadUnitEn(unit string) (unitEn string) {
  40. switch unit {
  41. case "天":
  42. unitEn = "day"
  43. return
  44. case "周":
  45. unitEn = "week"
  46. return
  47. case "月":
  48. unitEn = "month"
  49. return
  50. case "季":
  51. unitEn = "quarter"
  52. return
  53. case "年":
  54. unitEn = "year"
  55. return
  56. }
  57. return
  58. }
  59. // EdbInfoRefreshAllFromBaseV3 全部刷新指标(切换到edb_lib服务)
  60. // @author Roc
  61. // @datetime 2022-09-16 11:04:44
  62. // @description 将原有的单个指标刷新,调整为批量多个指标刷新
  63. func EdbInfoRefreshAllFromBaseV3(edbInfoIdList []int, refreshAll bool) (err error) {
  64. var errmsg string
  65. defer func() {
  66. if err != nil {
  67. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  68. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  69. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  70. }
  71. }()
  72. // 获取关联的基础指标
  73. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr, err, errmsg := getRefreshEdbInfoListByIds(edbInfoIdList)
  74. if err != nil {
  75. return
  76. }
  77. var startDate string
  78. for _, bv := range newBaseEdbInfoArr {
  79. //source := bv.Source
  80. //edbInfoId := bv.EdbInfoId
  81. //edbCode := bv.EdbCode
  82. if bv.StartDate == "0000-00-00" {
  83. continue
  84. }
  85. //开始时间
  86. startDate = ``
  87. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  88. sTime, err := time.Parse(utils.FormatDate, bv.StartDate)
  89. if err != nil {
  90. return err
  91. }
  92. startDate = sTime.Format(utils.FormatDate)
  93. } else {
  94. sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
  95. if err != nil {
  96. return err
  97. }
  98. frequency := bv.Frequency
  99. var limitDay int
  100. switch frequency {
  101. case "日度":
  102. limitDay = utils.DATA_START_REFRESH_LIMIT
  103. case "周度":
  104. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  105. case "月度":
  106. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  107. case "季度":
  108. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  109. case "年度":
  110. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  111. default:
  112. limitDay = utils.DATA_START_REFRESH_LIMIT
  113. }
  114. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  115. }
  116. result, err := RefreshEdbData(bv.EdbInfoId, bv.Source, bv.EdbCode, startDate)
  117. if err != nil {
  118. fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err", time.Now())
  119. errmsg = "RefreshBaseEdbData Err:" + err.Error()
  120. return err
  121. }
  122. if result.Ret != 200 {
  123. fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  124. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  125. return fmt.Errorf("刷新失败, err:", errmsg)
  126. }
  127. //maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
  128. //if err != nil {
  129. // if err.Error() == utils.ErrNoRow() { //找不到数据,那么就进入到下一条数据做处理
  130. // continue
  131. // }
  132. // return err
  133. //}
  134. //if maxAndMinItem != nil {
  135. // err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  136. // if err != nil {
  137. // return err
  138. // }
  139. //}
  140. fmt.Println("end newBaseEdbInfoArr:", bv, time.Now())
  141. }
  142. //刷新相关普通计算指标
  143. for _, v := range calculateArr {
  144. edbInfo := newCalculateMap[v]
  145. if edbInfo == nil {
  146. return err
  147. }
  148. //计算指标就不用开始时间了吧,因为存在说开始时间变更的情况
  149. startDate = ``
  150. if !refreshAll { //如果不是全部更新,那么以指标的开始时间作为刷新
  151. startDate = edbInfo.StartDate
  152. source := edbInfo.Source
  153. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  154. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  155. } else {
  156. if source == utils.DATA_SOURCE_CALCULATE {
  157. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  158. if err != nil {
  159. return err
  160. }
  161. frequency := edbInfo.Frequency
  162. var limitDay int
  163. switch frequency {
  164. case "日度":
  165. limitDay = utils.DATA_START_REFRESH_LIMIT
  166. case "周度":
  167. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  168. case "月度":
  169. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  170. case "季度":
  171. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  172. case "年度":
  173. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  174. default:
  175. limitDay = utils.DATA_START_REFRESH_LIMIT
  176. }
  177. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  178. }
  179. }
  180. }
  181. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  182. if err != nil {
  183. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  184. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  185. return err
  186. }
  187. if result.Ret != 200 {
  188. fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  189. errmsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  190. return fmt.Errorf("刷新失败")
  191. }
  192. }
  193. //刷新相关预测计算指标
  194. for _, v := range predictCalculateArr {
  195. edbInfo := newPredictCalculateMap[v]
  196. if edbInfo == nil {
  197. return err
  198. }
  199. startDate = edbInfo.StartDate
  200. source := edbInfo.Source
  201. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  202. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  203. } else {
  204. if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  205. startDate = ``
  206. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  207. startDate = edbInfo.StartDate
  208. } else {
  209. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  210. if err != nil {
  211. return err
  212. }
  213. frequency := edbInfo.Frequency
  214. var limitDay int
  215. switch frequency {
  216. case "日度":
  217. limitDay = utils.DATA_START_REFRESH_LIMIT
  218. case "周度":
  219. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  220. case "月度":
  221. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  222. case "季度":
  223. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  224. case "年度":
  225. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  226. default:
  227. limitDay = utils.DATA_START_REFRESH_LIMIT
  228. }
  229. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  230. }
  231. }
  232. }
  233. result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  234. if err != nil {
  235. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  236. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  237. return err
  238. }
  239. if result.Ret != 200 {
  240. fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  241. errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  242. return fmt.Errorf("刷新失败")
  243. }
  244. }
  245. // 4、更新动态环差值
  246. {
  247. predictEdbInfoIdList := make([]int, 0)
  248. for _, v := range newBasePredictEdbInfoArr {
  249. predictEdbInfoIdList = append(predictEdbInfoIdList, v.EdbInfoId)
  250. }
  251. fmt.Println("predictEdbConfCalculateMappingDetailList:", predictEdbInfoIdList)
  252. predictEdbConfCalculateMappingDetailList, err := data_manage.GetPredictEdbConfCalculateMappingDetailListByEdbInfoId(predictEdbInfoIdList)
  253. if err != nil {
  254. errmsg = fmt.Sprint("更新动态环差值时,获取数据失败:" + err.Error())
  255. return fmt.Errorf("刷新失败, err:", errmsg)
  256. }
  257. for _, bv := range predictEdbConfCalculateMappingDetailList {
  258. result, err := RefreshEdbData(bv.EdbInfoId, utils.DATA_SOURCE_PREDICT, bv.EdbCode, "")
  259. if err != nil {
  260. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err", time.Now())
  261. errmsg = "RefreshBasePredictEdbData Err:" + err.Error()
  262. return err
  263. }
  264. if result.Ret != 200 {
  265. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  266. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  267. return fmt.Errorf("刷新失败, err:%s", errmsg)
  268. }
  269. fmt.Println("end predictEdbConfCalculateMappingDetailList:", bv, time.Now())
  270. }
  271. }
  272. //5、再次刷新相关预测计算指标
  273. for _, v := range predictCalculateArr {
  274. edbInfo := newPredictCalculateMap[v]
  275. if edbInfo == nil {
  276. return err
  277. }
  278. startDate = edbInfo.StartDate
  279. source := edbInfo.Source
  280. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  281. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  282. } else {
  283. if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  284. startDate = ``
  285. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  286. startDate = edbInfo.StartDate
  287. } else {
  288. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  289. if err != nil {
  290. return err
  291. }
  292. frequency := edbInfo.Frequency
  293. var limitDay int
  294. switch frequency {
  295. case "日度":
  296. limitDay = utils.DATA_START_REFRESH_LIMIT
  297. case "周度":
  298. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  299. case "月度":
  300. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  301. case "季度":
  302. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  303. case "年度":
  304. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  305. default:
  306. limitDay = utils.DATA_START_REFRESH_LIMIT
  307. }
  308. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  309. }
  310. }
  311. }
  312. result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  313. if err != nil {
  314. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  315. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  316. return err
  317. }
  318. if result.Ret != 200 {
  319. fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  320. errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  321. return fmt.Errorf("刷新失败")
  322. }
  323. }
  324. return err
  325. }
  326. // getRefreshEdbInfoList 获取待更新的指标(普通基础指标、普通运算指标,预测运算指标)
  327. func getRefreshEdbInfoListByIds(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int, err error, errMsg string) {
  328. calculateList, err := data_manage.GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList)
  329. if err != nil && err.Error() != utils.ErrNoRow() {
  330. err = errors.New("GetEdbInfoAllCalculate Err:" + err.Error())
  331. return
  332. }
  333. // 获取指标信息
  334. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  335. if err != nil {
  336. err = errors.New("GetEdbInfoAllCalGetEdbInfoByIdr:" + err.Error())
  337. return
  338. }
  339. baseEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 基础普通指标
  340. calculateInfoArr := make([]*data_manage.EdbInfo, 0) //基础运算指标
  341. basePredictEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 预测普通指标
  342. predictCalculateInfoArr := make([]*data_manage.EdbInfo, 0) //预测运算指标
  343. for _, baseEdbInfo := range edbInfoList {
  344. if baseEdbInfo.EdbInfoType == 0 { //普通指标
  345. switch baseEdbInfo.EdbType {
  346. case 1: //基础指标
  347. baseEdbInfoArr = append(baseEdbInfoArr, baseEdbInfo)
  348. case 2: //计算指标
  349. calculateList = append(calculateList, baseEdbInfo)
  350. }
  351. } else { //预测指标
  352. switch baseEdbInfo.EdbType {
  353. case 1: //基础指标
  354. basePredictEdbInfoArr = append(basePredictEdbInfoArr, baseEdbInfo)
  355. case 2: //计算指标
  356. predictCalculateInfoArr = append(predictCalculateInfoArr, baseEdbInfo)
  357. }
  358. }
  359. }
  360. // 计算指标
  361. for _, v := range calculateList {
  362. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  363. if tmpErr != nil {
  364. err = tmpErr
  365. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  366. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  367. return
  368. }
  369. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  370. switch tmpEdbInfo.EdbInfoType {
  371. case 0: //普通基础指标
  372. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  373. case 1: //预测基础指标
  374. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  375. }
  376. }
  377. for _, tmpEdbInfo := range getCalculateInfoArr {
  378. switch tmpEdbInfo.EdbInfoType {
  379. case 0: //普通运算指标
  380. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  381. case 1: //预测运算指标
  382. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  383. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  384. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  385. } else {
  386. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  387. }
  388. }
  389. }
  390. switch v.EdbInfoType {
  391. case 0: //普通运算指标
  392. calculateInfoArr = append(calculateInfoArr, v)
  393. case 1: //预测指标
  394. if v.EdbType == 1 { //预测普通指标
  395. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  396. } else {
  397. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  398. }
  399. }
  400. }
  401. // 预测计算指标
  402. for _, v := range predictCalculateInfoArr {
  403. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  404. if tmpErr != nil {
  405. err = tmpErr
  406. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  407. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  408. return
  409. }
  410. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  411. switch tmpEdbInfo.EdbInfoType {
  412. case 0: //普通基础指标
  413. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  414. case 1: //预测基础指标
  415. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  416. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  417. } else {
  418. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  419. }
  420. //basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  421. }
  422. }
  423. for _, tmpEdbInfo := range getCalculateInfoArr {
  424. switch tmpEdbInfo.EdbInfoType {
  425. case 0: //普通运算指标
  426. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  427. case 1: //预测运算指标
  428. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  429. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  430. } else {
  431. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  432. }
  433. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  434. }
  435. }
  436. switch v.EdbInfoType {
  437. case 0: //普通运算指标
  438. calculateInfoArr = append(calculateInfoArr, v)
  439. case 1: //预测指标
  440. if v.EdbType == 1 { //预测普通指标
  441. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  442. } else {
  443. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  444. }
  445. }
  446. }
  447. // 第一次的计算指标map
  448. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  449. for _, v := range calculateInfoArr {
  450. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  451. calculateArr = append(calculateArr, v.EdbInfoId)
  452. }
  453. newCalculateMap[v.EdbInfoId] = v
  454. }
  455. // 基础预测指标
  456. if len(basePredictEdbInfoArr) > 0 {
  457. basePredictEdbInfoIdList := make([]int, 0)
  458. for _, v := range basePredictEdbInfoArr {
  459. basePredictEdbInfoIdList = append(basePredictEdbInfoIdList, v.EdbInfoId)
  460. }
  461. baseEdbInfoList, tmpErr := data_manage.GetPredictEdbInfoAllCalculate(basePredictEdbInfoIdList)
  462. if tmpErr != nil {
  463. err = tmpErr
  464. return
  465. }
  466. tmpCalculateList := make([]*data_manage.EdbInfo, 0)
  467. for _, v := range baseEdbInfoList {
  468. switch v.EdbType {
  469. case 1:
  470. baseEdbInfoArr = append(baseEdbInfoArr, v)
  471. case 2:
  472. // 如果不在第一次里面,那么在第二次 刷新预测指标的时候 需要加进去
  473. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  474. tmpCalculateList = append(tmpCalculateList, v)
  475. }
  476. }
  477. }
  478. // 这里是查询预测指标用到的 普通计算指标
  479. for _, v := range tmpCalculateList {
  480. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  481. if tmpErr != nil {
  482. err = tmpErr
  483. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  484. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  485. return
  486. }
  487. // 基础指标
  488. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  489. switch tmpEdbInfo.EdbInfoType {
  490. case 0: //普通基础指标
  491. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  492. case 1: //预测基础指标
  493. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  494. }
  495. }
  496. // 计算指标
  497. for _, tmpEdbInfo := range getCalculateInfoArr {
  498. switch tmpEdbInfo.EdbInfoType {
  499. case 0: //普通运算指标
  500. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  501. case 1: //预测运算指标
  502. if v.EdbType == 1 { //预测普通指标
  503. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  504. } else {
  505. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  506. }
  507. }
  508. }
  509. switch v.EdbInfoType {
  510. case 0: //普通运算指标
  511. calculateInfoArr = append(calculateInfoArr, v)
  512. case 1: //预测指标
  513. if v.EdbType == 1 { //预测普通指标
  514. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  515. } else { //预测运算指标
  516. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  517. }
  518. }
  519. }
  520. // 第二次计算指标的map
  521. for _, v := range calculateInfoArr {
  522. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  523. calculateArr = append(calculateArr, v.EdbInfoId)
  524. }
  525. newCalculateMap[v.EdbInfoId] = v
  526. }
  527. }
  528. // 普通基础指标去重
  529. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  530. baseMap := make(map[int]int)
  531. for _, v := range baseEdbInfoArr {
  532. if _, ok := baseMap[v.EdbInfoId]; !ok {
  533. newBaseEdbInfoArr = append(newBaseEdbInfoArr, v)
  534. }
  535. baseMap[v.EdbInfoId] = v.EdbInfoId
  536. }
  537. // 普通计算指标的id
  538. sort.Ints(calculateArr)
  539. // 普通预测指标去重
  540. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  541. basePredictMap := make(map[int]int)
  542. for _, v := range basePredictEdbInfoArr {
  543. if _, ok := basePredictMap[v.EdbInfoId]; !ok {
  544. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, v)
  545. }
  546. basePredictMap[v.EdbInfoId] = v.EdbInfoId
  547. }
  548. // 预测计算指标去重
  549. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  550. for _, v := range predictCalculateInfoArr {
  551. if _, ok := newPredictCalculateMap[v.EdbInfoId]; !ok {
  552. predictCalculateArr = append(predictCalculateArr, v.EdbInfoId)
  553. }
  554. newPredictCalculateMap[v.EdbInfoId] = v
  555. }
  556. // 预测计算指标的id
  557. sort.Ints(predictCalculateArr)
  558. return
  559. }
  560. // AddOrEditEdbInfoToEs 添加/修改ES中的指标
  561. func AddOrEditEdbInfoToEs(edbInfoId int) {
  562. //添加es
  563. itemInfo, _ := data_manage.GetEdbInfoByCondition("AND edb_info_id=?", []interface{}{edbInfoId})
  564. go elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  565. }
  566. // TraceEdbInfoByEdbInfoId 指标追溯
  567. func TraceEdbInfoByEdbInfoId(edbInfoId int) (traceEdbInfo data_manage.TraceEdbInfoResp, err error) {
  568. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  569. if err != nil {
  570. return
  571. }
  572. edbInfoRuleMap := make(map[int]string, 0)
  573. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  574. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  575. traceEdbInfo = data_manage.TraceEdbInfoResp{
  576. //EdbInfoId: edbInfo.EdbInfoId,
  577. EdbInfoId: edbInfoId,
  578. EdbName: edbInfo.EdbName,
  579. }
  580. findIdMap := make(map[int]int)
  581. findIdMap[edbInfoId] = edbInfoId
  582. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  583. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  584. edbInfoIdList := make([]int, 0)
  585. for _, v := range findIdMap {
  586. edbInfoIdList = append(edbInfoIdList, v)
  587. }
  588. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  589. if err != nil {
  590. return
  591. }
  592. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  593. for _, tmpEdbInfo := range edbInfoList {
  594. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  595. }
  596. traceEdbInfo, err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
  597. return
  598. }
  599. // traceEdbInfoByEdbInfoId 指标追溯
  600. 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) {
  601. traceEdbInfo, ok := existMap[edbInfoId]
  602. if ok {
  603. return
  604. }
  605. child = make([]data_manage.TraceEdbInfoResp, 0)
  606. edbInfoMappingList, err := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  607. edbMappingMap[edbInfoId] = edbInfoMappingList
  608. for _, v := range edbInfoMappingList {
  609. tmpEdbInfoId := v.FromEdbInfoId
  610. tmpTraceEdbInfo := data_manage.TraceEdbInfoResp{
  611. EdbInfoId: tmpEdbInfoId,
  612. //EdbName: v.EdbName,
  613. //RuleTitle: edbInfoRuleMap[v.EdbInfoId],
  614. //Child: make([]data_manage.TraceEdbInfoResp, 0),
  615. }
  616. if v.EdbType == 2 { // 计算指标
  617. // 查过了就不查了
  618. if _, ok2 := findIdMap[tmpEdbInfoId]; !ok2 {
  619. tmpTraceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(tmpEdbInfoId, tmpTraceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  620. }
  621. //else {
  622. // tmpTraceEdbInfo = existMap[v.EdbInfoId]
  623. //}
  624. }
  625. child = append(child, tmpTraceEdbInfo)
  626. findIdMap[tmpEdbInfoId] = tmpEdbInfoId
  627. //existMap[v.EdbInfoId] = tmpTraceEdbInfo
  628. }
  629. existMap[edbInfoId] = traceEdbInfo
  630. return
  631. }
  632. 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) {
  633. edbInfo, ok := edbInfoMap[traceEdbInfoResp.EdbInfoId]
  634. if !ok {
  635. err = errors.New("指标异常")
  636. return
  637. }
  638. var parentEdbInfo *data_manage.EdbInfo
  639. if parentEdbInfoId > 0 {
  640. parentEdbInfo, ok = edbInfoMap[parentEdbInfoId]
  641. if !ok {
  642. err = errors.New("指标异常")
  643. return
  644. }
  645. }
  646. //traceEdbInfoResp.EdbName = edbInfo.EdbName
  647. traceEdbInfoResp.EdbName, traceEdbInfoResp.RuleTitle = getEdbRuleTitle(edbInfo, parentEdbInfo, traceEdbInfoResp.Child, edbInfoMap, edbMappingMap)
  648. if traceEdbInfoResp.Child != nil && len(traceEdbInfoResp.Child) > 0 {
  649. for k, v := range traceEdbInfoResp.Child {
  650. traceEdbInfoResp.Child[k], err = handleTraceEdbInfo(v, traceEdbInfoResp.EdbInfoId, edbInfoMap, edbMappingMap)
  651. if err != nil {
  652. return
  653. }
  654. }
  655. }
  656. newTraceEdbInfoResp = traceEdbInfoResp
  657. return
  658. }
  659. // getEdbRule 获取规则名称
  660. 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) {
  661. edbName = edbInfo.EdbName
  662. ruleTitle = `来源于` + edbInfo.SourceName
  663. if edbInfo.EdbType == 1 {
  664. // 基础指标的话,直接返回来源名称
  665. //ruleTitle = `来源于`+edbInfo.SourceName
  666. return
  667. }
  668. if parentEdbInfo != nil {
  669. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  670. if !ok {
  671. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  672. }
  673. // 指标名称
  674. switch parentEdbInfo.Source {
  675. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  676. for _, v := range edbMappingList {
  677. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  678. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  679. }
  680. }
  681. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ, utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 直接拼接 ,累计值同比拼接
  682. for _, v := range edbMappingList {
  683. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  684. tmpName := ``
  685. if v.FromTag == `A` {
  686. tmpName = `拼接日期前`
  687. } else if v.FromTag == `B` {
  688. tmpName = `拼接日期后`
  689. }
  690. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  691. }
  692. }
  693. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  694. for _, v := range edbMappingList {
  695. //(需对上游指标+自变量,领先10天/因变量)
  696. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  697. tmpName := ``
  698. if v.FromTag == `A` {
  699. tmpName = fmt.Sprintf(`自变量,领先%d天`, v.MoveValue)
  700. } else if v.FromTag == `B` {
  701. tmpName = `因变量`
  702. }
  703. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  704. }
  705. }
  706. case utils.DATA_SOURCE_CALCULATE_CORRELATION: // 滚动相关性
  707. for _, v := range edbMappingList {
  708. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  709. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  710. }
  711. }
  712. }
  713. }
  714. // 规则
  715. switch edbInfo.Source {
  716. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  717. ruleTitle = "=" + edbInfo.CalculateFormula
  718. case utils.DATA_SOURCE_CALCULATE_LJZZY, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  719. ruleTitle = `累计转月值计算`
  720. case utils.DATA_SOURCE_CALCULATE_TBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  721. ruleTitle = `同比值计算`
  722. case utils.DATA_SOURCE_CALCULATE_TCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  723. ruleTitle = `同差值计算`
  724. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  725. ruleTitle = fmt.Sprintf("N数值移动均值计算(N=%s)", edbInfo.CalculateFormula)
  726. case utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  727. ruleTitle = fmt.Sprintf("N数值环比值计算(N=%s)", edbInfo.CalculateFormula)
  728. case utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  729. ruleTitle = fmt.Sprintf("N数值环差值计算(N=%s)", edbInfo.CalculateFormula)
  730. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  731. moveType := `领先`
  732. if edbInfo.MoveType == 2 {
  733. moveType = "滞后"
  734. }
  735. ruleTitle = fmt.Sprintf("时间移位计算(%s%s%s)", moveType, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  736. case utils.DATA_SOURCE_CALCULATE_BP, utils.DATA_SOURCE_PREDICT_CALCULATE_BP: // 变频
  737. childFrequency := ``
  738. if len(childList) > 0 {
  739. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  740. childFrequency = childEdbInfo.Frequency
  741. }
  742. }
  743. ruleTitle = fmt.Sprintf("升频计算(%s转%s)", childFrequency, edbInfo.Frequency)
  744. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: // 直接拼接
  745. ruleTitle = fmt.Sprintf("直接拼接计算(%s)", edbInfo.CalculateFormula)
  746. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 累计值同比拼
  747. ruleTitle = fmt.Sprintf("累计值同比值拼接计算(%s)", edbInfo.CalculateFormula)
  748. case utils.DATA_SOURCE_PYTHON:
  749. ruleTitle = `代码运算`
  750. case utils.DATA_SOURCE_CALCULATE_CJJX, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  751. ruleTitle = fmt.Sprintf("超季节性计算(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  752. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  753. var startDate, endDate string
  754. dateList := strings.Split(edbInfo.CalculateFormula, ",")
  755. if len(dateList) == 2 {
  756. startDate = dateList[0]
  757. endDate = dateList[1]
  758. }
  759. ruleTitle = fmt.Sprintf("拟合残差计算(%s至%s)", startDate, endDate)
  760. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  761. ruleTitle = `数据调整`
  762. case utils.DATA_SOURCE_CALCULATE_NH, utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  763. ruleTitle = `年化计算`
  764. case utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: // 扩散指数->53
  765. type KszsConfig struct {
  766. DateType int `description:"扩散指标日期;1:全部指标日期并集;2:部分指标日期并集"`
  767. CheckList []string `description:"选中的数据,A,B,C"`
  768. }
  769. var config KszsConfig
  770. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &config)
  771. if err != nil {
  772. return
  773. }
  774. var startDate, endDate time.Time
  775. childEdbInfoIdList := make([]int, 0)
  776. if config.DateType == 1 {
  777. for _, v := range childList {
  778. childEdbInfoIdList = append(childEdbInfoIdList, v.EdbInfoId)
  779. }
  780. } else {
  781. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  782. if !ok {
  783. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  784. }
  785. tagMap := make(map[string]int)
  786. for _, v := range edbMappingList {
  787. tagMap[v.FromTag] = v.FromEdbInfoId
  788. }
  789. for _, v := range config.CheckList {
  790. if tmpEdbInfoId, ok := tagMap[v]; ok {
  791. childEdbInfoIdList = append(childEdbInfoIdList, tmpEdbInfoId)
  792. }
  793. }
  794. }
  795. for _, v := range childEdbInfoIdList {
  796. if childEdbInfo, ok := edbInfoMap[v]; ok {
  797. tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.StartDate, time.Local)
  798. if tmpErr != nil {
  799. return
  800. }
  801. if startDate.IsZero() || startDate.After(tmpStartDate) {
  802. startDate = tmpStartDate
  803. }
  804. tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, childEdbInfo.EndDate, time.Local)
  805. if tmpErr != nil {
  806. return
  807. }
  808. if endDate.IsZero() || endDate.Before(tmpEndDate) {
  809. endDate = tmpEndDate
  810. }
  811. }
  812. }
  813. ruleTitle = fmt.Sprintf("扩散指数计算(%s至%s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  814. case utils.DATA_SOURCE_STOCK_PLANT:
  815. ruleTitle = `来源于装置分析`
  816. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  817. type EdbCalculateFormula struct {
  818. BaseCalculateValue int `description:"基础计算窗口"`
  819. BaseCalculateUnit string `description:"基础计算频度"`
  820. LeadValue int `description:"领先期数"`
  821. LeadUnit string `description:"频度"`
  822. CalculateValue int `description:"计算窗口"`
  823. CalculateUnit string `description:"计算频度"`
  824. }
  825. var correlationConf EdbCalculateFormula
  826. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &correlationConf)
  827. if err != nil {
  828. return
  829. }
  830. ruleTitle = fmt.Sprintf("滚动相关性(计算窗口%d%s,B领先A%d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  831. case utils.DATA_SOURCE_CALCULATE_JP, utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  832. childFrequency := ``
  833. if len(childList) > 0 {
  834. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  835. childFrequency = childEdbInfo.Frequency
  836. }
  837. }
  838. ruleTitle = fmt.Sprintf("降频计算(%s转%s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  839. }
  840. return
  841. }