edb_info.go 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/cache"
  6. "eta_gn/eta_api/models"
  7. "eta_gn/eta_api/models/data_manage"
  8. "eta_gn/eta_api/models/data_manage/excel"
  9. "eta_gn/eta_api/services/alarm_msg"
  10. "eta_gn/eta_api/services/data/data_manage_permission"
  11. "eta_gn/eta_api/services/elastic"
  12. "eta_gn/eta_api/utils"
  13. "fmt"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // EdbInfoRefreshAllFromBaseV2 全部刷新指标(切换到edb_lib服务)
  20. func EdbInfoRefreshAllFromBaseV2(edbInfoId int, refreshAll, isRefreshTop bool) (err error, isAsync bool) {
  21. var errmsg string
  22. defer func() {
  23. if err != nil {
  24. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  25. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  26. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  27. }
  28. }()
  29. err, isAsync = EdbInfoRefreshAllFromBaseV3([]int{edbInfoId}, refreshAll, false, isRefreshTop)
  30. return
  31. }
  32. // EdbInfoRefreshAllFromBaseV3
  33. //
  34. // @Description: 全部刷新指标(切换到edb_lib服务)
  35. // @author: Roc
  36. // @datetime 2023-10-23 09:57:55
  37. // @param edbInfoIdList []int
  38. // @param refreshAll bool
  39. // @param isSync bool 是否同步执行
  40. // @param isRefreshTop bool 是否刷新上面的指标
  41. // @return err error
  42. // @return isAsync bool 是否异步刷新
  43. func EdbInfoRefreshAllFromBaseV3(edbInfoIdList []int, refreshAll, isSync, isRefreshTop bool) (err error, isAsync bool) {
  44. var errmsg string
  45. defer func() {
  46. if err != nil {
  47. fmt.Println("EdbInfoRefreshAllFromBaseV3 Err:" + err.Error() + ";errmsg:" + errmsg)
  48. go alarm_msg.SendAlarmMsg("EdbInfoRefreshAllFromBaseV3,Err"+err.Error()+";errMsg:"+errmsg, 3)
  49. }
  50. }()
  51. var newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo
  52. var newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo
  53. var calculateArr, predictCalculateArr []int
  54. // 获取关联指标
  55. if isRefreshTop {
  56. // 获取所有关联的指标,上下所有的指标
  57. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr, err, errmsg = getRefreshEdbInfoListByIds(edbInfoIdList)
  58. if err != nil {
  59. return
  60. }
  61. } else {
  62. // 获取溯源的指标
  63. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr = getEdbInfoIdList(edbInfoIdList)
  64. }
  65. // 需要刷新的指标数量
  66. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr) + len(newBasePredictEdbInfoArr)
  67. if totalEdbInfo == 0 {
  68. return
  69. }
  70. if totalEdbInfo <= 20 || isSync {
  71. err = edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  72. } else {
  73. isAsync = true
  74. go edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  75. }
  76. return
  77. }
  78. // getEdbInfoIdList
  79. // @Description: 获取待更新的指标列表
  80. // @author: Roc
  81. // @datetime 2023-12-01 16:19:42
  82. // @param edbInfoIdList []int
  83. // @return newBaseEdbInfoArr []*data_manage.EdbInfo
  84. // @return newBasePredictEdbInfoArr []*data_manage.EdbInfo
  85. // @return newCalculateMap map[int]*data_manage.EdbInfo
  86. // @return newPredictCalculateMap map[int]*data_manage.EdbInfo
  87. // @return calculateArr []int
  88. // @return predictCalculateArr []int
  89. func getEdbInfoIdList(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
  90. traceEdbInfoList, err := TraceEdbInfoByEdbInfoIdList(edbInfoIdList)
  91. if err != nil {
  92. return
  93. }
  94. // existEdbInfoIdMap 已经处理了的指标id map
  95. existEdbInfoIdMap := make(map[int]int)
  96. // 基础指标
  97. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  98. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  99. newBaseMap := make(map[int]*data_manage.EdbInfo)
  100. newPredictBaseMap := make(map[int]*data_manage.EdbInfo)
  101. // 计算指标
  102. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  103. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  104. calculateArr = make([]int, 0)
  105. predictCalculateArr = make([]int, 0)
  106. // 获取关联指标
  107. for _, traceEdbInfo := range traceEdbInfoList {
  108. tmpBaseEdbInfoArr, tmpBasePredictEdbInfoArr, tmpCalculateMap, tmpPredictCalculateMap, _, _ := getRefreshEdbInfoListByTraceEdbInfo(traceEdbInfo, existEdbInfoIdMap)
  109. // 普通基础指标
  110. for _, edbInfo := range tmpBaseEdbInfoArr {
  111. if _, ok := newBaseMap[edbInfo.EdbInfoId]; !ok {
  112. newBaseMap[edbInfo.EdbInfoId] = edbInfo
  113. if edbInfo.NoUpdate == 1 {
  114. continue
  115. }
  116. newBaseEdbInfoArr = append(newBaseEdbInfoArr, edbInfo)
  117. }
  118. }
  119. // 预测基础指标
  120. for _, edbInfo := range tmpBasePredictEdbInfoArr {
  121. if _, ok := newPredictBaseMap[edbInfo.EdbInfoId]; !ok {
  122. newPredictBaseMap[edbInfo.EdbInfoId] = edbInfo
  123. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, edbInfo)
  124. }
  125. }
  126. // 普通计算指标
  127. for _, edbInfo := range tmpCalculateMap {
  128. if _, ok := newCalculateMap[edbInfo.EdbInfoId]; !ok {
  129. if edbInfo.NoUpdate == 1 {
  130. continue
  131. }
  132. newCalculateMap[edbInfo.EdbInfoId] = edbInfo
  133. calculateArr = append(calculateArr, edbInfo.EdbInfoId)
  134. }
  135. }
  136. // 预测计算指标
  137. for _, edbInfo := range tmpPredictCalculateMap {
  138. if _, ok := newPredictCalculateMap[edbInfo.EdbInfoId]; !ok {
  139. newPredictCalculateMap[edbInfo.EdbInfoId] = edbInfo
  140. predictCalculateArr = append(predictCalculateArr, edbInfo.EdbInfoId)
  141. }
  142. }
  143. }
  144. // 普通计算指标的id
  145. sort.Ints(calculateArr)
  146. // 预测计算指标的id
  147. sort.Ints(predictCalculateArr)
  148. return
  149. }
  150. // getRefreshEdbInfoListByTraceEdbInfo根据溯源获取关联指标
  151. 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) {
  152. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  153. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  154. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  155. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  156. calculateArr = make([]int, 0)
  157. predictCalculateArr = make([]int, 0)
  158. _, ok := existEdbInfoIdMap[traceEdbInfo.EdbInfoId]
  159. if ok {
  160. return
  161. }
  162. existEdbInfoIdMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfoId
  163. switch traceEdbInfo.EdbInfoType {
  164. //0-普通指标; 1-预测指标
  165. case 0: // 0-普通指标
  166. if traceEdbInfo.EdbType == 1 { //1-基础指标
  167. newBaseEdbInfoArr = append(newBaseEdbInfoArr, traceEdbInfo.EdbInfo)
  168. } else if traceEdbInfo.EdbType == 2 { //2-计算指标
  169. newCalculateMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfo
  170. calculateArr = append(calculateArr, traceEdbInfo.EdbInfoId)
  171. }
  172. case 1: // 1-预测指标
  173. if traceEdbInfo.EdbType == 1 { //1-基础指标
  174. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, traceEdbInfo.EdbInfo)
  175. } else if traceEdbInfo.EdbType == 2 { //2-计算指标
  176. newPredictCalculateMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfo
  177. predictCalculateArr = append(predictCalculateArr, traceEdbInfo.EdbInfoId)
  178. }
  179. }
  180. if traceEdbInfo.Child != nil && len(traceEdbInfo.Child) > 0 {
  181. for _, v := range traceEdbInfo.Child {
  182. tmpBaseEdbInfoArr, tmpPredictEdbInfoArr, tmpCalculateMap, tmpPredictCalculateMap, tmpCalculateArr, tmpPredictCalculateArr := getRefreshEdbInfoListByTraceEdbInfo(v, existEdbInfoIdMap)
  183. newBaseEdbInfoArr = append(newBaseEdbInfoArr, tmpBaseEdbInfoArr...)
  184. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, tmpPredictEdbInfoArr...)
  185. for k, tmpEdbInfo := range tmpCalculateMap {
  186. newCalculateMap[k] = tmpEdbInfo
  187. }
  188. for k, tmpEdbInfo := range tmpPredictCalculateMap {
  189. newPredictCalculateMap[k] = tmpEdbInfo
  190. }
  191. calculateArr = append(calculateArr, tmpCalculateArr...)
  192. predictCalculateArr = append(predictCalculateArr, tmpPredictCalculateArr...)
  193. }
  194. }
  195. return
  196. }
  197. func edbInfoRefreshAll(refreshAll bool, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) (err error) {
  198. var errmsg string
  199. defer func() {
  200. if err != nil {
  201. fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
  202. go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
  203. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  204. }
  205. }()
  206. var startDate string
  207. for _, bv := range newBaseEdbInfoArr {
  208. //source := bv.Source
  209. //edbInfoId := bv.EdbInfoId
  210. //edbCode := bv.EdbCode
  211. //fmt.Println(bv.StartDate)
  212. //if utils.TimeToFormatDate(bv.StartDate) == "0000-00-00" {
  213. // continue
  214. //}
  215. //开始时间
  216. startDate = ``
  217. if refreshAll { //刷新所有数据,那么就是从配置的开始时间开始刷新
  218. startDate = utils.BaseEdbRefreshStartDate
  219. } else {
  220. sTime := bv.EndDate
  221. // 如果日期是空,那么就采用系统默认的日期
  222. if sTime.IsZero() {
  223. sTime, _ = time.ParseInLocation(utils.FormatDate, utils.BaseEdbRefreshStartDate, time.Local)
  224. }
  225. frequency := bv.Frequency
  226. var limitDay int
  227. switch frequency {
  228. case "日度":
  229. limitDay = utils.DATA_START_REFRESH_LIMIT
  230. case "周度":
  231. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  232. case "月度":
  233. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  234. case "季度":
  235. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  236. case "年度":
  237. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  238. default:
  239. limitDay = utils.DATA_START_REFRESH_LIMIT
  240. }
  241. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  242. }
  243. result, err := RefreshEdbData(bv.EdbInfoId, bv.Source, bv.SubSource, bv.EdbCode, startDate)
  244. if err != nil {
  245. fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err", time.Now())
  246. errmsg = "RefreshBaseEdbData Err:" + err.Error()
  247. return err
  248. }
  249. if result.Ret != 200 {
  250. fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  251. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  252. return fmt.Errorf("刷新失败, err:", errmsg)
  253. }
  254. //fmt.Println("end newBaseEdbInfoArr:", bv, time.Now())
  255. }
  256. //刷新相关普通计算指标
  257. for _, v := range calculateArr {
  258. edbInfo := newCalculateMap[v]
  259. if edbInfo == nil {
  260. return err
  261. }
  262. //计算指标就不用开始时间了吧,因为存在说开始时间变更的情况
  263. startDate = ``
  264. if !refreshAll { //如果不是全部更新,那么以指标的开始时间作为刷新
  265. startDate = utils.TimeToFormatDate(edbInfo.StartDate)
  266. source := edbInfo.Source
  267. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  268. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  269. } else {
  270. if source == utils.DATA_SOURCE_CALCULATE {
  271. sTime := edbInfo.EndDate
  272. frequency := edbInfo.Frequency
  273. var limitDay int
  274. switch frequency {
  275. case "日度":
  276. limitDay = utils.DATA_START_REFRESH_LIMIT
  277. case "周度":
  278. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  279. case "月度":
  280. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  281. case "季度":
  282. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  283. case "年度":
  284. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  285. default:
  286. limitDay = utils.DATA_START_REFRESH_LIMIT
  287. }
  288. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  289. }
  290. }
  291. }
  292. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  293. if err != nil {
  294. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  295. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  296. return err
  297. }
  298. if result.Ret != 200 {
  299. fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  300. errmsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  301. return fmt.Errorf("刷新失败")
  302. }
  303. }
  304. //刷新相关预测计算指标
  305. for _, v := range predictCalculateArr {
  306. edbInfo := newPredictCalculateMap[v]
  307. if edbInfo == nil {
  308. return err
  309. }
  310. startDate = utils.TimeToFormatDate(edbInfo.StartDate)
  311. source := edbInfo.Source
  312. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  313. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  314. } else {
  315. if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  316. startDate = ``
  317. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  318. startDate = utils.TimeToFormatDate(edbInfo.StartDate)
  319. } else {
  320. sTime := edbInfo.EndDate
  321. frequency := edbInfo.Frequency
  322. var limitDay int
  323. switch frequency {
  324. case "日度":
  325. limitDay = utils.DATA_START_REFRESH_LIMIT
  326. case "周度":
  327. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  328. case "月度":
  329. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  330. case "季度":
  331. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  332. case "年度":
  333. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  334. default:
  335. limitDay = utils.DATA_START_REFRESH_LIMIT
  336. }
  337. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  338. }
  339. }
  340. }
  341. result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  342. if err != nil {
  343. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  344. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  345. return err
  346. }
  347. if result.Ret != 200 {
  348. fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  349. errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  350. return fmt.Errorf("刷新失败")
  351. }
  352. }
  353. // 4、更新动态环差值
  354. {
  355. predictEdbInfoIdList := make([]int, 0)
  356. for _, v := range newBasePredictEdbInfoArr {
  357. predictEdbInfoIdList = append(predictEdbInfoIdList, v.EdbInfoId)
  358. }
  359. predictEdbConfCalculateMappingDetailList, err := data_manage.GetPredictEdbConfCalculateMappingDetailListByEdbInfoId(predictEdbInfoIdList)
  360. if err != nil {
  361. errmsg = fmt.Sprint("更新动态环差值时,获取数据失败:" + err.Error())
  362. return fmt.Errorf("刷新失败, err:", errmsg)
  363. }
  364. for _, bv := range predictEdbConfCalculateMappingDetailList {
  365. result, err := RefreshEdbData(bv.EdbInfoId, utils.DATA_SOURCE_PREDICT, utils.DATA_SUB_SOURCE_EDB, bv.EdbCode, "")
  366. if err != nil {
  367. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err", time.Now())
  368. errmsg = "RefreshBasePredictEdbData Err:" + err.Error()
  369. return err
  370. }
  371. if result.Ret != 200 {
  372. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  373. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  374. return fmt.Errorf("刷新失败, err:%s", errmsg)
  375. }
  376. //fmt.Println("end predictEdbConfCalculateMappingDetailList:", bv, time.Now())
  377. }
  378. }
  379. //5、再次刷新相关预测计算指标
  380. for _, v := range predictCalculateArr {
  381. edbInfo := newPredictCalculateMap[v]
  382. if edbInfo == nil {
  383. return err
  384. }
  385. startDate = utils.TimeToFormatDate(edbInfo.StartDate)
  386. source := edbInfo.Source
  387. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  388. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  389. } else {
  390. if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
  391. startDate = ``
  392. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  393. startDate = utils.TimeToFormatDate(edbInfo.StartDate)
  394. } else {
  395. sTime := edbInfo.EndDate
  396. frequency := edbInfo.Frequency
  397. var limitDay int
  398. switch frequency {
  399. case "日度":
  400. limitDay = utils.DATA_START_REFRESH_LIMIT
  401. case "周度":
  402. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  403. case "月度":
  404. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  405. case "季度":
  406. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  407. case "年度":
  408. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  409. default:
  410. limitDay = utils.DATA_START_REFRESH_LIMIT
  411. }
  412. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  413. }
  414. }
  415. }
  416. result, err := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  417. if err != nil {
  418. fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
  419. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  420. return err
  421. }
  422. if result.Ret != 200 {
  423. fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  424. errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  425. return fmt.Errorf("刷新失败")
  426. }
  427. }
  428. return err
  429. }
  430. // EdbInfoRefresh 刷新单个指标,不会刷新关联指标
  431. func EdbInfoRefresh(edbInfo *data_manage.EdbInfoView, refreshAll bool) (err error) {
  432. var errmsg string
  433. defer func() {
  434. if err != nil {
  435. fmt.Println("EdbInfoRefresh Err:" + err.Error() + ";errmsg:" + errmsg)
  436. go alarm_msg.SendAlarmMsg("EdbInfoRefresh,Err"+err.Error()+";errMsg:"+errmsg, 3)
  437. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
  438. }
  439. }()
  440. switch edbInfo.EdbInfoType {
  441. //指标类型,0:普通指标,1:预测指标
  442. case 0:
  443. if edbInfo.StartDate == "0000-00-00" {
  444. return
  445. }
  446. //开始时间
  447. startDate := utils.BaseEdbRefreshStartDate // 默认成刷新开始日期
  448. if edbInfo.EdbType == 1 {
  449. // 基础指标
  450. if !refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  451. sDate := edbInfo.EndDate
  452. if sDate == `` || sDate == `0000-00-00` {
  453. sDate = utils.BaseEdbRefreshStartDate
  454. }
  455. sTime, err := time.Parse(utils.FormatDate, sDate)
  456. if err != nil {
  457. return err
  458. }
  459. frequency := edbInfo.Frequency
  460. var limitDay int
  461. switch frequency {
  462. case "日度":
  463. limitDay = utils.DATA_START_REFRESH_LIMIT
  464. case "周度":
  465. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  466. case "月度":
  467. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  468. case "季度":
  469. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  470. case "年度":
  471. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  472. default:
  473. limitDay = utils.DATA_START_REFRESH_LIMIT
  474. }
  475. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  476. }
  477. result, err := RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, startDate)
  478. if err != nil {
  479. fmt.Println(edbInfo.EdbInfoId, "RefreshBaseEdbData err", time.Now())
  480. errmsg = "RefreshBaseEdbData Err:" + err.Error()
  481. return err
  482. }
  483. if result.Ret != 200 {
  484. fmt.Println(edbInfo.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  485. errmsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  486. return fmt.Errorf("刷新失败, err:", errmsg)
  487. }
  488. } else { //计算指标
  489. //计算指标就不用开始时间了吧,因为存在说开始时间变更的情况
  490. if !refreshAll { //如果不是全部更新,那么以指标的开始时间作为刷新
  491. source := edbInfo.Source
  492. if source == utils.DATA_SOURCE_CALCULATE {
  493. sDate := edbInfo.EndDate
  494. if sDate == `` || sDate == `0000-00-00` {
  495. sDate = utils.BaseEdbRefreshStartDate
  496. }
  497. sTime, err := time.Parse(utils.FormatDate, sDate)
  498. if err != nil {
  499. return err
  500. }
  501. frequency := edbInfo.Frequency
  502. var limitDay int
  503. switch frequency {
  504. case "日度":
  505. limitDay = utils.DATA_START_REFRESH_LIMIT
  506. case "周度":
  507. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  508. case "月度":
  509. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  510. case "季度":
  511. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  512. case "年度":
  513. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  514. default:
  515. limitDay = utils.DATA_START_REFRESH_LIMIT
  516. }
  517. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  518. }
  519. }
  520. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  521. if err != nil {
  522. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
  523. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  524. return err
  525. }
  526. if result.Ret != 200 {
  527. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  528. errmsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  529. return fmt.Errorf("刷新失败")
  530. }
  531. }
  532. case 1:
  533. startDate := edbInfo.StartDate
  534. var result *models.BaseResponse
  535. if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
  536. startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
  537. } else {
  538. startDate = ``
  539. if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
  540. startDate = edbInfo.StartDate
  541. } else {
  542. sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  543. if err != nil {
  544. return err
  545. }
  546. frequency := edbInfo.Frequency
  547. var limitDay int
  548. switch frequency {
  549. case "日度":
  550. limitDay = utils.DATA_START_REFRESH_LIMIT
  551. case "周度":
  552. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  553. case "月度":
  554. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  555. case "季度":
  556. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  557. case "年度":
  558. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  559. default:
  560. limitDay = utils.DATA_START_REFRESH_LIMIT
  561. }
  562. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  563. }
  564. }
  565. if edbInfo.EdbType == 1 { //基础指标
  566. result, err = RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  567. } else {
  568. // 预测计算指标
  569. // 4、更新动态环差值
  570. {
  571. predictEdbConfCalculateMappingDetailList, err := data_manage.GetPredictEdbConfCalculateMappingDetailListByEdbInfoId([]int{edbInfo.EdbInfoId})
  572. if err != nil {
  573. errmsg = fmt.Sprint("更新动态环差值时,获取数据失败:" + err.Error())
  574. return fmt.Errorf("刷新失败, err:", errmsg)
  575. }
  576. for _, bv := range predictEdbConfCalculateMappingDetailList {
  577. result, err := RefreshEdbData(bv.EdbInfoId, utils.DATA_SOURCE_PREDICT, utils.DATA_SUB_SOURCE_EDB, bv.EdbCode, "")
  578. if err != nil {
  579. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err", time.Now())
  580. errmsg = "RefreshBasePredictEdbData Err:" + err.Error()
  581. return err
  582. }
  583. if result.Ret != 200 {
  584. fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  585. errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  586. return fmt.Errorf("刷新失败, err:%s", errmsg)
  587. }
  588. //fmt.Println("end predictEdbConfCalculateMappingDetailList:", bv, time.Now())
  589. }
  590. }
  591. }
  592. if err != nil {
  593. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
  594. errmsg = "RefreshEdbCalculateData Err:" + err.Error()
  595. return err
  596. }
  597. if result.Ret != 200 {
  598. fmt.Println(edbInfo.EdbInfoId, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  599. errmsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  600. return fmt.Errorf("刷新失败")
  601. }
  602. }
  603. return err
  604. }
  605. // AddEdbInfo 新增手工指标数据
  606. func AddEdbInfo(secName, unit, frequency, noticeTime, mobile string, classifyId, userId int, userName string) (err error) {
  607. tradeCode, err := models.GetMaxTradeCode()
  608. if err != nil {
  609. return
  610. }
  611. if tradeCode == "" {
  612. err = errors.New("最大编码为空")
  613. return
  614. }
  615. maxTradeCode, err := utils.GetMaxTradeCode(tradeCode)
  616. if err != nil {
  617. err = errors.New("获取编码信息失败,Err:" + err.Error())
  618. return
  619. }
  620. if maxTradeCode == "" {
  621. err = errors.New("编码为空,请联系技术")
  622. return
  623. }
  624. //判断指标名称是否为空
  625. if secName == "" {
  626. err = errors.New("指标名称不能为空")
  627. return
  628. }
  629. item, err := models.GetEdbinfoBySecName(secName)
  630. if err != nil && !utils.IsErrNoRow(err) {
  631. err = errors.New("获取信息失败,Err:" + err.Error())
  632. return
  633. }
  634. if item != nil {
  635. err = errors.New("指标名称已存在")
  636. return
  637. }
  638. // 先删除该指标编码的历史数据(避免上海删除指标时,没有删除该指标数据)
  639. _ = models.DeleteAllEdbData(maxTradeCode)
  640. // 先删除历史的操作日志
  641. _ = models.DelEdbinfoOpRecordByTradeCode(maxTradeCode)
  642. // 再次新增指标
  643. err = models.AddEdbinfo(maxTradeCode, secName, unit, "手动", frequency, noticeTime, classifyId, userId, userName)
  644. if err != nil {
  645. err = errors.New("新增失败,Err:" + err.Error())
  646. return
  647. }
  648. err = models.AddEdbinfoUser(maxTradeCode, mobile)
  649. return
  650. }
  651. // AddOrEditEdbInfoToEs 添加/修改ES中的指标
  652. func AddOrEditEdbInfoToEs(edbInfoId int) {
  653. //添加es
  654. itemInfo, _ := data_manage.GetEdbInfoByCondition("AND edb_info_id=?", []interface{}{edbInfoId})
  655. go elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  656. }
  657. // DeleteEdbInfoToEs 删除ES中的指标
  658. func DeleteEdbInfoToEs(edbInfoId int) {
  659. //添加es
  660. go elastic.EsDeleteEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(edbInfoId))
  661. }
  662. // RefreshManualData 根据手工指标code刷新手工指标数据
  663. func RefreshManualData(edbCode string) {
  664. var err error
  665. defer func() {
  666. if err != nil {
  667. go alarm_msg.SendAlarmMsg("导入手工数据后,根据手工指标code刷新手工指标数据失败提醒,Err"+err.Error(), 3)
  668. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"导入手工数据后,根据手工指标code刷新手工指标数据失败提醒", "errmsg:"+err.Error(), utils.EmailSendToUsers)
  669. }
  670. }()
  671. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MANUAL, edbCode)
  672. if err != nil {
  673. if utils.IsErrNoRow(err) {
  674. err = nil
  675. }
  676. return
  677. }
  678. // 刷新指标库
  679. err, _ = EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, true, true)
  680. return
  681. }
  682. // 获取频度的英文版
  683. func GetFrequencyEn(frequency string) (frequencyEn string) {
  684. switch frequency {
  685. case "日度":
  686. frequencyEn = "day"
  687. return
  688. case "周度":
  689. frequencyEn = "week"
  690. return
  691. case "旬度":
  692. frequencyEn = "ten days"
  693. return
  694. case "月度":
  695. frequencyEn = "month"
  696. return
  697. case "季度":
  698. frequencyEn = "quarter"
  699. return
  700. case "年度":
  701. frequencyEn = "year"
  702. return
  703. }
  704. return
  705. }
  706. func GetLeadUnitEn(unit string) (unitEn string) {
  707. switch unit {
  708. case "天":
  709. unitEn = "day"
  710. return
  711. case "周":
  712. unitEn = "week"
  713. return
  714. case "月":
  715. unitEn = "month"
  716. return
  717. case "季":
  718. unitEn = "quarter"
  719. return
  720. case "年":
  721. unitEn = "year"
  722. return
  723. }
  724. return
  725. }
  726. // getRefreshEdbInfoList 获取待更新的指标(普通基础指标、普通运算指标,预测运算指标)
  727. func getRefreshEdbInfoListByIds(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int, err error, errMsg string) {
  728. calculateList, err := data_manage.GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList)
  729. if err != nil && !utils.IsErrNoRow(err) {
  730. err = errors.New("GetEdbInfoAllCalculate Err:" + err.Error())
  731. return
  732. }
  733. // 获取指标信息
  734. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  735. if err != nil {
  736. err = errors.New("GetEdbInfoAllCalGetEdbInfoByIdr:" + err.Error())
  737. return
  738. }
  739. baseEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 基础普通指标
  740. calculateInfoArr := make([]*data_manage.EdbInfo, 0) //基础运算指标
  741. basePredictEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 预测普通指标
  742. predictCalculateInfoArr := make([]*data_manage.EdbInfo, 0) //预测运算指标
  743. for _, baseEdbInfo := range edbInfoList {
  744. // 指标若停更, 则忽略掉
  745. if baseEdbInfo.NoUpdate == 1 {
  746. continue
  747. }
  748. if baseEdbInfo.EdbInfoType == 0 { //普通指标
  749. switch baseEdbInfo.EdbType {
  750. case 1: //基础指标
  751. baseEdbInfoArr = append(baseEdbInfoArr, baseEdbInfo)
  752. case 2: //计算指标
  753. calculateList = append(calculateList, baseEdbInfo)
  754. }
  755. } else { //预测指标
  756. switch baseEdbInfo.EdbType {
  757. case 1: //基础指标
  758. basePredictEdbInfoArr = append(basePredictEdbInfoArr, baseEdbInfo)
  759. case 2: //计算指标
  760. predictCalculateInfoArr = append(predictCalculateInfoArr, baseEdbInfo)
  761. }
  762. }
  763. }
  764. // 计算指标
  765. for _, v := range calculateList {
  766. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  767. if tmpErr != nil {
  768. err = tmpErr
  769. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  770. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  771. return
  772. }
  773. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  774. switch tmpEdbInfo.EdbInfoType {
  775. case 0: //普通基础指标
  776. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  777. case 1: //预测基础指标
  778. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  779. }
  780. }
  781. for _, tmpEdbInfo := range getCalculateInfoArr {
  782. switch tmpEdbInfo.EdbInfoType {
  783. case 0: //普通运算指标
  784. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  785. case 1: //预测运算指标
  786. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  787. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  788. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  789. } else {
  790. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  791. }
  792. }
  793. }
  794. switch v.EdbInfoType {
  795. case 0: //普通运算指标
  796. calculateInfoArr = append(calculateInfoArr, v)
  797. case 1: //预测指标
  798. if v.EdbType == 1 { //预测普通指标
  799. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  800. } else {
  801. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  802. }
  803. }
  804. }
  805. // 预测计算指标
  806. for _, v := range predictCalculateInfoArr {
  807. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  808. if tmpErr != nil {
  809. err = tmpErr
  810. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  811. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  812. return
  813. }
  814. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  815. switch tmpEdbInfo.EdbInfoType {
  816. case 0: //普通基础指标
  817. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  818. case 1: //预测基础指标
  819. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  820. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  821. } else {
  822. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  823. }
  824. //basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  825. }
  826. }
  827. for _, tmpEdbInfo := range getCalculateInfoArr {
  828. switch tmpEdbInfo.EdbInfoType {
  829. case 0: //普通运算指标
  830. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  831. case 1: //预测运算指标
  832. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  833. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  834. } else {
  835. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  836. }
  837. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  838. }
  839. }
  840. switch v.EdbInfoType {
  841. case 0: //普通运算指标
  842. calculateInfoArr = append(calculateInfoArr, v)
  843. case 1: //预测指标
  844. if v.EdbType == 1 { //预测普通指标
  845. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  846. } else {
  847. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  848. }
  849. }
  850. }
  851. // 第一次的计算指标map
  852. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  853. for _, v := range calculateInfoArr {
  854. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  855. calculateArr = append(calculateArr, v.EdbInfoId)
  856. }
  857. newCalculateMap[v.EdbInfoId] = v
  858. }
  859. // 基础预测指标
  860. if len(basePredictEdbInfoArr) > 0 {
  861. basePredictEdbInfoIdList := make([]int, 0)
  862. for _, v := range basePredictEdbInfoArr {
  863. basePredictEdbInfoIdList = append(basePredictEdbInfoIdList, v.EdbInfoId)
  864. }
  865. baseEdbInfoList, tmpErr := data_manage.GetPredictEdbInfoAllCalculate(basePredictEdbInfoIdList)
  866. if tmpErr != nil {
  867. err = tmpErr
  868. return
  869. }
  870. tmpCalculateList := make([]*data_manage.EdbInfo, 0)
  871. for _, v := range baseEdbInfoList {
  872. switch v.EdbType {
  873. case 1:
  874. baseEdbInfoArr = append(baseEdbInfoArr, v)
  875. case 2:
  876. // 如果不在第一次里面,那么在第二次 刷新预测指标的时候 需要加进去
  877. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  878. tmpCalculateList = append(tmpCalculateList, v)
  879. }
  880. }
  881. }
  882. // 这里是查询预测指标用到的 普通计算指标
  883. for _, v := range tmpCalculateList {
  884. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  885. if tmpErr != nil {
  886. err = tmpErr
  887. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  888. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  889. return
  890. }
  891. // 基础指标
  892. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  893. switch tmpEdbInfo.EdbInfoType {
  894. case 0: //普通基础指标
  895. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  896. case 1: //预测基础指标
  897. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  898. }
  899. }
  900. // 计算指标
  901. for _, tmpEdbInfo := range getCalculateInfoArr {
  902. switch tmpEdbInfo.EdbInfoType {
  903. case 0: //普通运算指标
  904. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  905. case 1: //预测运算指标
  906. if v.EdbType == 1 { //预测普通指标
  907. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  908. } else {
  909. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  910. }
  911. }
  912. }
  913. switch v.EdbInfoType {
  914. case 0: //普通运算指标
  915. calculateInfoArr = append(calculateInfoArr, v)
  916. case 1: //预测指标
  917. if v.EdbType == 1 { //预测普通指标
  918. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  919. } else { //预测运算指标
  920. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  921. }
  922. }
  923. }
  924. // 第二次计算指标的map
  925. for _, v := range calculateInfoArr {
  926. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  927. calculateArr = append(calculateArr, v.EdbInfoId)
  928. }
  929. newCalculateMap[v.EdbInfoId] = v
  930. }
  931. }
  932. // 普通基础指标去重
  933. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  934. baseMap := make(map[int]int)
  935. for _, v := range baseEdbInfoArr {
  936. if _, ok := baseMap[v.EdbInfoId]; !ok {
  937. newBaseEdbInfoArr = append(newBaseEdbInfoArr, v)
  938. }
  939. baseMap[v.EdbInfoId] = v.EdbInfoId
  940. }
  941. // 普通计算指标的id
  942. sort.Ints(calculateArr)
  943. // 普通预测指标去重
  944. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  945. basePredictMap := make(map[int]int)
  946. for _, v := range basePredictEdbInfoArr {
  947. if _, ok := basePredictMap[v.EdbInfoId]; !ok {
  948. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, v)
  949. }
  950. basePredictMap[v.EdbInfoId] = v.EdbInfoId
  951. }
  952. // 预测计算指标去重
  953. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  954. for _, v := range predictCalculateInfoArr {
  955. if _, ok := newPredictCalculateMap[v.EdbInfoId]; !ok {
  956. predictCalculateArr = append(predictCalculateArr, v.EdbInfoId)
  957. }
  958. newPredictCalculateMap[v.EdbInfoId] = v
  959. }
  960. // 预测计算指标的id
  961. sort.Ints(predictCalculateArr)
  962. return
  963. }
  964. type AddEdbBaseInfoReq struct {
  965. EdbCode string `description:"指标编码"`
  966. EdbName string `description:"指标名称"`
  967. Unit string `description:"单位"`
  968. ClassifyId int `description:"所属分类"`
  969. SysUserId int `description:"用户id"`
  970. SysUserRealName string `description:"用户真实名称"`
  971. }
  972. // EdbInfoAdd 添加指标到指标库
  973. func EdbInfoAdd(source, subSource, classifyId int, edbCode, edbName, frequency, unit, startDate, endDate string, sysUserId int, sysUserRealName, lang string) (edbInfo *data_manage.EdbInfo, err error, errMsg string, isSendEmail bool) {
  974. // 有些指标的添加是走的指标公共服务
  975. if source == utils.DATA_SOURCE_BUSINESS {
  976. req2 := AddEdbBaseInfoReq{
  977. EdbCode: edbCode,
  978. EdbName: edbName,
  979. Unit: unit,
  980. ClassifyId: classifyId,
  981. SysUserId: sysUserId,
  982. SysUserRealName: sysUserRealName,
  983. }
  984. reqJson, tmpErr := json.Marshal(req2)
  985. if tmpErr != nil {
  986. err = tmpErr
  987. return
  988. }
  989. respItem, tmpErr := AddBaseEdbInfo(string(reqJson), source, subSource, lang)
  990. if tmpErr != nil {
  991. errMsg = "添加失败"
  992. err = tmpErr
  993. return
  994. }
  995. isSendEmail = respItem.IsSendEmail
  996. if respItem.Ret != 200 {
  997. errMsg = respItem.Msg
  998. err = errors.New(respItem.ErrMsg)
  999. } else {
  1000. edbInfo, err = data_manage.GetEdbInfoById(respItem.Data.EdbInfoId)
  1001. }
  1002. return
  1003. }
  1004. isSendEmail = true
  1005. //判断指标名称是否存在
  1006. {
  1007. var count int
  1008. var condition string
  1009. var pars []interface{}
  1010. //condition += " AND source=? "
  1011. //pars = append(pars, source)
  1012. switch lang {
  1013. case utils.EnLangVersion:
  1014. condition += " AND edb_name_en = ? "
  1015. default:
  1016. condition += " AND edb_name=? "
  1017. }
  1018. pars = append(pars, edbName)
  1019. count, err = data_manage.GetEdbInfoCountByCondition(condition, pars)
  1020. if err != nil {
  1021. errMsg = "判断指标名称是否存在失败"
  1022. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  1023. return
  1024. }
  1025. if count > 0 {
  1026. errMsg = "指标名称已存在,请重新填写"
  1027. err = errors.New(errMsg)
  1028. isSendEmail = false
  1029. return
  1030. }
  1031. }
  1032. edbInfo = new(data_manage.EdbInfo)
  1033. edbInfo.Source = source
  1034. var sourceName string
  1035. edbSource := data_manage.GetEdbSourceBySourceId(source)
  1036. if edbSource != nil {
  1037. sourceName = edbSource.SourceName
  1038. }
  1039. if sourceName == "" {
  1040. errMsg = "指标来源异常"
  1041. err = errors.New(errMsg)
  1042. return
  1043. }
  1044. edbInfo.SourceName = sourceName
  1045. edbType := 1 //基础指标
  1046. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1047. edbType = 2 //计算指标
  1048. }
  1049. // 从缓存中获取
  1050. terminalCode, serverUrl, sourceIndexName, e := GetEdbTerminalCodeBySource(edbInfo.Source, edbInfo.EdbCode, edbInfo.StockCode)
  1051. if e != nil {
  1052. errMsg = "获取可以使用的终端地址失败"
  1053. err = errors.New("获取可以使用的终端地址失败,Err:" + e.Error())
  1054. return
  1055. }
  1056. //获取该层级下最大的排序数
  1057. maxSort, err := GetEdbClassifyMaxSort(classifyId, 0)
  1058. if err != nil {
  1059. errMsg = "获取失败"
  1060. err = errors.New("查询排序信息失败,Err:" + err.Error())
  1061. return
  1062. }
  1063. edbInfo.EdbCode = edbCode
  1064. edbInfo.EdbName = edbName
  1065. edbInfo.EdbNameEn = edbName
  1066. edbInfo.EdbNameSource = edbName
  1067. edbInfo.Frequency = frequency
  1068. edbInfo.Unit = unit
  1069. edbInfo.UnitEn = unit
  1070. if startDate != `` {
  1071. startDateTime, tmpErr := utils.FormatDateStrToTime(startDate)
  1072. if tmpErr != nil {
  1073. err = tmpErr
  1074. return
  1075. }
  1076. edbInfo.StartDate = startDateTime
  1077. }
  1078. if endDate != `` {
  1079. endDateTime, tmpErr := utils.FormatDateStrToTime(endDate)
  1080. if tmpErr != nil {
  1081. err = tmpErr
  1082. return
  1083. }
  1084. edbInfo.EndDate = endDateTime
  1085. }
  1086. edbInfo.ClassifyId = classifyId
  1087. edbInfo.SysUserId = sysUserId
  1088. edbInfo.SysUserRealName = sysUserRealName
  1089. edbInfo.CreateTime = time.Now()
  1090. edbInfo.ModifyTime = time.Now()
  1091. edbInfo.ServerUrl = serverUrl
  1092. edbInfo.Sort = maxSort + 1
  1093. edbInfo.DataDateType = `交易日`
  1094. edbInfo.TerminalCode = terminalCode
  1095. edbInfo.SourceIndexName = sourceIndexName
  1096. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1097. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  1098. itemVal, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  1099. if itemVal != nil && err == nil {
  1100. edbInfo.MaxValue = itemVal.MaxValue
  1101. edbInfo.MinValue = itemVal.MinValue
  1102. }
  1103. edbInfo.EdbType = edbType
  1104. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  1105. if err != nil {
  1106. errMsg = "保存失败"
  1107. err = errors.New("保存失败,Err:" + err.Error())
  1108. return
  1109. }
  1110. edbInfo.EdbInfoId = int(edbInfoId)
  1111. //保存数据
  1112. data_manage.ModifyEdbInfoDataStatus(edbInfoId, source, subSource, edbCode)
  1113. maxAndMinItem, _ := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  1114. if maxAndMinItem != nil {
  1115. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  1116. }
  1117. //添加es
  1118. AddOrEditEdbInfoToEs(int(edbInfoId))
  1119. go handleByAddEdbInfo(edbInfo)
  1120. return
  1121. }
  1122. // TraceEdbInfoByEdbInfoId 指标追溯
  1123. func TraceEdbInfoByEdbInfoId(edbInfoId, sysUserId int) (traceEdbInfo data_manage.TraceEdbInfoResp, err error) {
  1124. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1125. if err != nil {
  1126. return
  1127. }
  1128. edbInfoRuleMap := make(map[int]string, 0)
  1129. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  1130. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  1131. traceEdbInfo = data_manage.TraceEdbInfoResp{
  1132. //EdbInfoId: edbInfo.EdbInfoId,
  1133. EdbInfoId: edbInfoId,
  1134. EdbInfoType: edbInfo.EdbInfoType,
  1135. EdbName: edbInfo.EdbName,
  1136. EdbNameEn: edbInfo.EdbNameEn,
  1137. EdbType: edbInfo.EdbType,
  1138. //Source: edbInfo.Source,
  1139. UniqueCode: edbInfo.UniqueCode,
  1140. ClassifyId: edbInfo.ClassifyId,
  1141. IsStop: edbInfo.NoUpdate,
  1142. EdbInfo: edbInfo,
  1143. }
  1144. findIdMap := make(map[int]int)
  1145. findIdMap[edbInfoId] = edbInfoId
  1146. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  1147. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1148. edbInfoIdList := make([]int, 0)
  1149. for _, v := range findIdMap {
  1150. edbInfoIdList = append(edbInfoIdList, v)
  1151. }
  1152. classifyIdList := make([]int, 0)
  1153. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1154. if err != nil {
  1155. return
  1156. }
  1157. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1158. for _, tmpEdbInfo := range edbInfoList {
  1159. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  1160. classifyIdList = append(classifyIdList, tmpEdbInfo.ClassifyId)
  1161. }
  1162. // 指标权限Map
  1163. edbInfoIdPermissionMap := make(map[int]bool)
  1164. // 指标权限
  1165. {
  1166. // 当前的分类
  1167. classifyMap := make(map[int]*data_manage.EdbClassify)
  1168. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1169. if tmpErr != nil {
  1170. err = tmpErr
  1171. return
  1172. }
  1173. for _, v := range classifyList {
  1174. classifyMap[v.ClassifyId] = v
  1175. }
  1176. // 获取所有有权限的指标和分类
  1177. permissionEdbIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUserId, 0, 0)
  1178. if tmpErr != nil {
  1179. err = tmpErr
  1180. return
  1181. }
  1182. for _, v := range edbInfoMap {
  1183. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  1184. edbInfoIdPermissionMap[v.EdbInfoId] = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1185. }
  1186. }
  1187. }
  1188. traceEdbInfo, err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap, edbInfoIdPermissionMap)
  1189. // 权限校验
  1190. return
  1191. }
  1192. // TraceEdbInfoByEdbInfoIdList 指标追溯
  1193. func TraceEdbInfoByEdbInfoIdList(edbInfoIdList []int) (traceEdbInfoList []data_manage.TraceEdbInfoResp, err error) {
  1194. traceEdbInfoList = make([]data_manage.TraceEdbInfoResp, 0)
  1195. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1196. if err != nil {
  1197. return
  1198. }
  1199. edbInfoRuleMap := make(map[int]string, 0)
  1200. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  1201. findIdMap := make(map[int]int)
  1202. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  1203. for _, edbInfo := range edbInfoList {
  1204. findIdMap[edbInfo.EdbInfoId] = edbInfo.EdbInfoId
  1205. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  1206. traceEdbInfo := data_manage.TraceEdbInfoResp{
  1207. //EdbInfoId: edbInfo.EdbInfoId,
  1208. EdbInfoId: edbInfo.EdbInfoId,
  1209. EdbInfoType: edbInfo.EdbInfoType,
  1210. EdbName: edbInfo.EdbName,
  1211. EdbNameEn: edbInfo.EdbNameEn,
  1212. EdbType: edbInfo.EdbType,
  1213. //Source: edbInfo.Source,
  1214. UniqueCode: edbInfo.UniqueCode,
  1215. ClassifyId: edbInfo.ClassifyId,
  1216. IsStop: edbInfo.NoUpdate,
  1217. EdbInfo: edbInfo,
  1218. }
  1219. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfo.EdbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1220. traceEdbInfoList = append(traceEdbInfoList, traceEdbInfo)
  1221. }
  1222. //findEdbInfoIdList := make([]int, 0)
  1223. //for _, v := range findIdMap {
  1224. // findEdbInfoIdList = append(findEdbInfoIdList, v)
  1225. //}
  1226. //findEdbInfoList, err := data_manage.GetEdbInfoByIdList(findEdbInfoIdList)
  1227. //if err != nil {
  1228. // return
  1229. //}
  1230. //edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1231. //for _, tmpEdbInfo := range findEdbInfoList {
  1232. // edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  1233. //}
  1234. //for k, traceEdbInfo := range traceEdbInfoList {
  1235. // traceEdbInfoList[k], err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
  1236. //}
  1237. return
  1238. }
  1239. // traceEdbInfoByEdbInfoId 指标追溯
  1240. 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) {
  1241. traceEdbInfo, ok := existMap[edbInfoId]
  1242. if ok {
  1243. return
  1244. }
  1245. child = make([]data_manage.TraceEdbInfoResp, 0)
  1246. edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  1247. if e != nil {
  1248. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
  1249. return
  1250. }
  1251. // 指标信息map
  1252. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1253. if len(edbInfoMappingList) > 0 {
  1254. fromEdbInfoIdList := make([]int, 0)
  1255. for _, v := range edbInfoMappingList {
  1256. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  1257. }
  1258. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(fromEdbInfoIdList)
  1259. if tmpErr != nil {
  1260. err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", tmpErr.Error())
  1261. return
  1262. }
  1263. for _, v := range edbInfoList {
  1264. edbInfoMap[v.EdbInfoId] = v
  1265. }
  1266. }
  1267. edbMappingMap[edbInfoId] = edbInfoMappingList
  1268. for _, v := range edbInfoMappingList {
  1269. tmpEdbInfoId := v.FromEdbInfoId
  1270. tmpTraceEdbInfo := data_manage.TraceEdbInfoResp{
  1271. EdbInfoId: tmpEdbInfoId,
  1272. EdbInfoType: v.FromEdbInfoType,
  1273. EdbType: v.FromEdbType,
  1274. UniqueCode: v.FromUniqueCode,
  1275. ClassifyId: v.FromClassifyId,
  1276. IsStop: v.NoUpdate,
  1277. EdbInfo: edbInfoMap[v.FromEdbInfoId],
  1278. }
  1279. // 计算指标/预测指标继续溯源
  1280. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  1281. // 查过了就不查了
  1282. if _, ok2 := findIdMap[tmpEdbInfoId]; !ok2 {
  1283. tmpTraceEdbInfo.Child, e = traceEdbInfoByEdbInfoId(tmpEdbInfoId, tmpTraceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1284. if e != nil {
  1285. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
  1286. return
  1287. }
  1288. }
  1289. }
  1290. child = append(child, tmpTraceEdbInfo)
  1291. findIdMap[tmpEdbInfoId] = tmpEdbInfoId
  1292. }
  1293. existMap[edbInfoId] = traceEdbInfo
  1294. return
  1295. }
  1296. func handleTraceEdbInfo(traceEdbInfoResp data_manage.TraceEdbInfoResp, parentEdbInfoId int, edbInfoMap map[int]*data_manage.EdbInfo, edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo, edbInfoIdPermissionMap map[int]bool) (newTraceEdbInfoResp data_manage.TraceEdbInfoResp, err error) {
  1297. edbInfo, ok := edbInfoMap[traceEdbInfoResp.EdbInfoId]
  1298. if !ok {
  1299. err = errors.New("指标异常")
  1300. return
  1301. }
  1302. var parentEdbInfo *data_manage.EdbInfo
  1303. if parentEdbInfoId > 0 {
  1304. parentEdbInfo, ok = edbInfoMap[parentEdbInfoId]
  1305. if !ok {
  1306. err = errors.New("指标异常")
  1307. return
  1308. }
  1309. }
  1310. //traceEdbInfoResp.EdbName = edbInfo.EdbName
  1311. traceEdbInfoResp.EdbName, traceEdbInfoResp.EdbNameEn, traceEdbInfoResp.RuleTitle, traceEdbInfoResp.RuleTitleEn = getEdbRuleTitle(edbInfo, parentEdbInfo, traceEdbInfoResp.Child, edbInfoMap, edbMappingMap)
  1312. if traceEdbInfoResp.Child != nil && len(traceEdbInfoResp.Child) > 0 {
  1313. for k, v := range traceEdbInfoResp.Child {
  1314. traceEdbInfoResp.Child[k], err = handleTraceEdbInfo(v, traceEdbInfoResp.EdbInfoId, edbInfoMap, edbMappingMap, edbInfoIdPermissionMap)
  1315. if err != nil {
  1316. return
  1317. }
  1318. }
  1319. }
  1320. // 指标权限
  1321. traceEdbInfoResp.HaveOperaAuth, _ = edbInfoIdPermissionMap[traceEdbInfoResp.EdbInfoId]
  1322. newTraceEdbInfoResp = traceEdbInfoResp
  1323. return
  1324. }
  1325. // getEdbRule 获取规则名称
  1326. func getEdbRuleTitle(edbInfo, parentEdbInfo *data_manage.EdbInfo, childList []data_manage.TraceEdbInfoResp, edbInfoMap map[int]*data_manage.EdbInfo, edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo) (edbName, edbNameEn, ruleTitle, ruleTitleEn string) {
  1327. edbName = edbInfo.EdbName
  1328. edbNameEn = edbInfo.EdbNameEn
  1329. ruleTitle = `来源于` + edbInfo.SourceName
  1330. ruleTitleEn = `Source From: ` + edbInfo.SourceName
  1331. if parentEdbInfo != nil {
  1332. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  1333. if !ok {
  1334. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  1335. }
  1336. // 指标名称
  1337. switch parentEdbInfo.Source {
  1338. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  1339. for _, v := range edbMappingList {
  1340. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1341. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  1342. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, v.FromTag)
  1343. }
  1344. }
  1345. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ, utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 直接拼接 ,累计值同比拼接
  1346. for _, v := range edbMappingList {
  1347. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1348. tmpName := ``
  1349. tmpNameEn := ``
  1350. if v.FromTag == `A` {
  1351. tmpName = `拼接日期前`
  1352. tmpNameEn = `Before Concatenation Date:`
  1353. } else if v.FromTag == `B` {
  1354. tmpName = `拼接日期后`
  1355. tmpNameEn = `After Concatenation Date:`
  1356. }
  1357. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  1358. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, tmpNameEn)
  1359. }
  1360. }
  1361. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  1362. for _, v := range edbMappingList {
  1363. //(需对上游指标+自变量,领先10天/因变量)
  1364. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1365. tmpName := ``
  1366. tmpNameEn := ``
  1367. if v.FromTag == `A` {
  1368. tmpName = fmt.Sprintf(`自变量,领先%d天`, v.MoveValue)
  1369. tmpNameEn = fmt.Sprintf(`Independent Variable, Lead %d Days:`, v.MoveValue)
  1370. } else if v.FromTag == `B` {
  1371. tmpName = `因变量`
  1372. tmpNameEn = `Dependent Variable`
  1373. }
  1374. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  1375. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, tmpNameEn)
  1376. }
  1377. }
  1378. case utils.DATA_SOURCE_CALCULATE_CORRELATION: // 滚动相关性
  1379. for _, v := range edbMappingList {
  1380. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1381. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  1382. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, v.FromTag)
  1383. }
  1384. }
  1385. }
  1386. }
  1387. if edbInfo.EdbType == 1 {
  1388. // 基础指标的话,直接返回来源名称
  1389. //ruleTitle = `来源于`+edbInfo.SourceName
  1390. return
  1391. }
  1392. // 规则
  1393. switch edbInfo.Source {
  1394. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  1395. ruleTitle = "=" + edbInfo.CalculateFormula
  1396. case utils.DATA_SOURCE_CALCULATE_LJZZY, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  1397. ruleTitle = `累计转月值计算`
  1398. ruleTitleEn = `Cumulative to Monthly Calculation`
  1399. case utils.DATA_SOURCE_CALCULATE_TBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  1400. ruleTitle = `同比值计算`
  1401. ruleTitleEn = `Year-on-Year Value Calculation`
  1402. case utils.DATA_SOURCE_CALCULATE_TCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  1403. ruleTitle = `同差值计算`
  1404. ruleTitleEn = `Yearly Difference Calculation`
  1405. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  1406. ruleTitle = fmt.Sprintf("N数值移动均值计算(N=%s)", edbInfo.CalculateFormula)
  1407. ruleTitleEn = fmt.Sprintf("N-Value Moving Average Calculation(N=%s)", edbInfo.CalculateFormula)
  1408. case utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  1409. ruleTitle = fmt.Sprintf("N数值环比值计算(N=%s)", edbInfo.CalculateFormula)
  1410. ruleTitleEn = fmt.Sprintf("N-Value Sequential Comparison Calculation(N=%s)", edbInfo.CalculateFormula)
  1411. case utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  1412. ruleTitle = fmt.Sprintf("N数值环差值计算(N=%s)", edbInfo.CalculateFormula)
  1413. ruleTitleEn = fmt.Sprintf("N-Value Sequential Difference Calculation(N=%s)", edbInfo.CalculateFormula)
  1414. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  1415. moveType := `领先`
  1416. moveTypeEn := `Lead`
  1417. if edbInfo.MoveType == 2 {
  1418. moveType = "滞后"
  1419. moveTypeEn = `Lag`
  1420. }
  1421. ruleTitle = fmt.Sprintf("时间移位计算(%s%s%s)", moveType, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  1422. ruleTitleEn = fmt.Sprintf("Time Shift Calculation(%s%s%s)", moveTypeEn, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  1423. case utils.DATA_SOURCE_CALCULATE_BP, utils.DATA_SOURCE_PREDICT_CALCULATE_BP: // 变频
  1424. childFrequency := ``
  1425. if len(childList) > 0 {
  1426. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  1427. childFrequency = childEdbInfo.Frequency
  1428. }
  1429. }
  1430. ruleTitle = fmt.Sprintf("升频计算(%s转%s)", childFrequency, edbInfo.Frequency)
  1431. ruleTitleEn = fmt.Sprintf("Upsampling Calculation(%s转%s)", childFrequency, edbInfo.Frequency)
  1432. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: // 直接拼接
  1433. ruleTitle = fmt.Sprintf("直接拼接计算(%s)", edbInfo.CalculateFormula)
  1434. ruleTitleEn = fmt.Sprintf("Direct Concatenation Calculation(%s)", edbInfo.CalculateFormula)
  1435. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 累计值同比拼
  1436. ruleTitle = fmt.Sprintf("累计值同比值拼接计算(%s)", edbInfo.CalculateFormula)
  1437. ruleTitleEn = fmt.Sprintf("Cumulative Year-on-Year Concatenation Calculation(%s)", edbInfo.CalculateFormula)
  1438. case utils.DATA_SOURCE_PYTHON:
  1439. ruleTitle = `代码运算`
  1440. ruleTitleEn = `Code Computation`
  1441. case utils.DATA_SOURCE_CALCULATE_CJJX, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  1442. ruleTitle = fmt.Sprintf("超季节性计算(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  1443. ruleTitleEn = fmt.Sprintf("Super Seasonality Calculation(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  1444. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  1445. var startDate, endDate string
  1446. dateList := strings.Split(edbInfo.CalculateFormula, ",")
  1447. if len(dateList) == 2 {
  1448. startDate = dateList[0]
  1449. endDate = dateList[1]
  1450. }
  1451. ruleTitle = fmt.Sprintf("拟合残差计算(%s至%s)", startDate, endDate)
  1452. ruleTitleEn = fmt.Sprintf("Fit Residual Calculation(%s至%s)", startDate, endDate)
  1453. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  1454. ruleTitle = `数据调整`
  1455. ruleTitleEn = `Data Adjustment`
  1456. case utils.DATA_SOURCE_CALCULATE_NH, utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  1457. ruleTitle = `年化计算`
  1458. ruleTitleEn = `Annualization Calculation`
  1459. case utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: // 扩散指数->53
  1460. type KszsConfig struct {
  1461. DateType int `description:"扩散指标日期;1:全部指标日期并集;2:部分指标日期并集"`
  1462. CheckList []string `description:"选中的数据,A,B,C"`
  1463. }
  1464. var config KszsConfig
  1465. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &config)
  1466. if err != nil {
  1467. return
  1468. }
  1469. var startDate, endDate time.Time
  1470. childEdbInfoIdList := make([]int, 0)
  1471. if config.DateType == 1 {
  1472. for _, v := range childList {
  1473. childEdbInfoIdList = append(childEdbInfoIdList, v.EdbInfoId)
  1474. }
  1475. } else {
  1476. if parentEdbInfo != nil {
  1477. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  1478. if !ok {
  1479. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  1480. }
  1481. tagMap := make(map[string]int)
  1482. for _, v := range edbMappingList {
  1483. tagMap[v.FromTag] = v.FromEdbInfoId
  1484. }
  1485. for _, v := range config.CheckList {
  1486. if tmpEdbInfoId, ok := tagMap[v]; ok {
  1487. childEdbInfoIdList = append(childEdbInfoIdList, tmpEdbInfoId)
  1488. }
  1489. }
  1490. }
  1491. }
  1492. for _, v := range childEdbInfoIdList {
  1493. if childEdbInfo, ok := edbInfoMap[v]; ok {
  1494. tmpStartDate := childEdbInfo.StartDate
  1495. if startDate.IsZero() || startDate.After(tmpStartDate) {
  1496. startDate = tmpStartDate
  1497. }
  1498. tmpEndDate := childEdbInfo.EndDate
  1499. if endDate.IsZero() || endDate.Before(tmpEndDate) {
  1500. endDate = tmpEndDate
  1501. }
  1502. }
  1503. }
  1504. ruleTitle = fmt.Sprintf("扩散指数计算(%s至%s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  1505. ruleTitleEn = fmt.Sprintf("Diffusion Index Calculation(%s to %s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  1506. case utils.DATA_SOURCE_STOCK_PLANT:
  1507. ruleTitle = `来源于装置分析`
  1508. ruleTitleEn = `Derived from Plant Analysis`
  1509. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  1510. type EdbCalculateFormula struct {
  1511. BaseCalculateValue int `description:"基础计算窗口"`
  1512. BaseCalculateUnit string `description:"基础计算频度"`
  1513. LeadValue int `description:"领先期数"`
  1514. LeadUnit string `description:"频度"`
  1515. CalculateValue int `description:"计算窗口"`
  1516. CalculateUnit string `description:"计算频度"`
  1517. }
  1518. var correlationConf EdbCalculateFormula
  1519. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &correlationConf)
  1520. if err != nil {
  1521. return
  1522. }
  1523. ruleTitle = fmt.Sprintf("滚动相关性(计算窗口%d%s,B领先A%d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  1524. ruleTitleEn = fmt.Sprintf("Rolling Correlation (Calculation Window %d%s, B Leads A %d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  1525. case utils.DATA_SOURCE_CALCULATE_JP, utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  1526. childFrequency := ``
  1527. if len(childList) > 0 {
  1528. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  1529. childFrequency = childEdbInfo.Frequency
  1530. }
  1531. }
  1532. ruleTitle = fmt.Sprintf("降频计算(%s转%s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  1533. ruleTitleEn = fmt.Sprintf("Downsampling Calculation(%s to %s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  1534. case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION:
  1535. ruleTitle = fmt.Sprintf("标准差(滚动%s期)", edbInfo.CalculateFormula)
  1536. ruleTitleEn = fmt.Sprintf("Standard Deviation (Rolling %s Periods)", edbInfo.CalculateFormula)
  1537. case utils.DATA_SOURCE_CALCULATE_PERCENTILE, utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE:
  1538. type TempCalculate struct {
  1539. CalculateValue int `description:"计算窗口"`
  1540. CalculateUnit string `description:"计算频度"`
  1541. }
  1542. cf := TempCalculate{}
  1543. if e := json.Unmarshal([]byte(edbInfo.CalculateFormula), &cf); e != nil {
  1544. return
  1545. }
  1546. ruleTitle = fmt.Sprintf("百分位(时间长度%d%s)", cf.CalculateValue, cf.CalculateUnit)
  1547. ruleTitle = fmt.Sprintf("Percentile (Time Length %d%s)", cf.CalculateValue, cf.CalculateUnit)
  1548. case utils.DATA_SOURCE_CALCULATE_ZSXY, utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY:
  1549. ruleTitle = `指数修匀计算`
  1550. ruleTitle = `Index Smoothing Calculation`
  1551. }
  1552. return
  1553. }
  1554. // GetEdbChartAdminList
  1555. // @param source 来源 :1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格
  1556. func GetEdbChartAdminList(source int) (list []int, err error) {
  1557. switch source {
  1558. case 1: //手工数据指标
  1559. list, err = models.GetEdbInfoAdminList()
  1560. if err != nil {
  1561. return
  1562. }
  1563. case 3: //ETA指标库
  1564. list, err = data_manage.GetEdbInfoAdminList(0)
  1565. if err != nil {
  1566. return
  1567. }
  1568. case 4: //ETA预测指标
  1569. list, err = data_manage.GetEdbInfoAdminList(1)
  1570. if err != nil {
  1571. return
  1572. }
  1573. case 5: //图库
  1574. list, err = data_manage.GetChartInfoAdminList()
  1575. if err != nil {
  1576. return
  1577. }
  1578. default:
  1579. return
  1580. }
  1581. return
  1582. }
  1583. // EdbInfoReplace 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  1584. func EdbInfoReplace(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, sysAdminId int, sysAdminRealName string) (replaceChartTotal, replaceCalculateTotal int, err error) {
  1585. defer func() {
  1586. if err != nil {
  1587. fmt.Println("ERR:", err.Error())
  1588. }
  1589. }()
  1590. // 替换关联表中的source_edb_info_id
  1591. relationEdbInfoIdList, replaceChartTotal, replaceCalculateTotal, err := data_manage.ReplaceChartEdb(oldEdbInfo, newEdbInfo)
  1592. if err != nil {
  1593. return
  1594. }
  1595. //加入到缓存队列中处理
  1596. go cache.AddReplaceEdbInfo(oldEdbInfo, newEdbInfo)
  1597. // 更新所有的关联指标
  1598. err, _ = EdbInfoRefreshAllFromBaseV3(relationEdbInfoIdList, true, true, true)
  1599. return
  1600. }
  1601. func GetTerminalFromBaseIndex(source int, edbCode string) (terminalCode string, sourceIndexName string, err error) {
  1602. tbName := GetBaseIndexTableName(source)
  1603. if tbName != "" {
  1604. var item data_manage.BaseIndexTerminalCode
  1605. item, err = data_manage.GetBaseIndexTerminalCode(edbCode, tbName)
  1606. if err != nil {
  1607. err = fmt.Errorf("查询数据源指标信息失败, Err: %s", err)
  1608. return
  1609. }
  1610. terminalCode = item.TerminalCode
  1611. sourceIndexName = item.IndexName
  1612. }
  1613. return
  1614. }
  1615. func GetBaseIndexTableName(source int) (tableName string) {
  1616. switch source {
  1617. case utils.DATA_SOURCE_YS:
  1618. tableName = "base_from_smm_index"
  1619. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  1620. tableName = "base_from_mysteel_chemical_index"
  1621. case utils.DATA_SOURCE_SCI:
  1622. tableName = "base_from_sci_index"
  1623. case utils.DATA_SOURCE_BAIINFO:
  1624. tableName = "base_from_baiinfo_index"
  1625. default:
  1626. }
  1627. return
  1628. }
  1629. // EdbInfoWsdAdd 添加指标到指标库
  1630. func EdbInfoWsdAdd(item *data_manage.EdbInfo) (edbInfo *data_manage.EdbInfo, err error, errMsg string, isSendEmail bool) {
  1631. isSendEmail = true
  1632. //判断指标名称是否存在
  1633. var condition string
  1634. var pars []interface{}
  1635. condition += " AND edb_name=? "
  1636. pars = append(pars, item.EdbName)
  1637. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1638. if err != nil {
  1639. errMsg = "判断指标名称是否存在失败"
  1640. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  1641. return
  1642. }
  1643. if count > 0 {
  1644. errMsg = "指标名称已存在,请重新填写"
  1645. err = errors.New(errMsg)
  1646. isSendEmail = false
  1647. return
  1648. }
  1649. source := item.Source
  1650. edbInfo = new(data_manage.EdbInfo)
  1651. edbInfo.Source = source
  1652. var sourceName string
  1653. edbSource := data_manage.GetEdbSourceBySourceId(source)
  1654. if edbSource != nil {
  1655. sourceName = edbSource.SourceName
  1656. }
  1657. if sourceName == "" {
  1658. errMsg = "指标来源异常"
  1659. err = errors.New(errMsg)
  1660. return
  1661. }
  1662. edbInfo.SourceName = sourceName
  1663. edbType := 1 //基础指标
  1664. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1665. edbType = 2 //计算指标
  1666. }
  1667. //获取该层级下最大的排序数
  1668. maxSort, err := GetEdbClassifyMaxSort(item.ClassifyId, 0)
  1669. if err != nil {
  1670. errMsg = "获取失败"
  1671. err = errors.New("查询排序信息失败,Err:" + err.Error())
  1672. return
  1673. }
  1674. edbInfo.EdbCode = item.EdbCode
  1675. edbInfo.EdbName = item.EdbName
  1676. edbInfo.EdbNameSource = item.EdbName
  1677. edbInfo.EdbNameEn = item.EdbNameEn
  1678. edbInfo.Frequency = item.Frequency
  1679. edbInfo.Unit = item.Unit
  1680. edbInfo.UnitEn = item.UnitEn
  1681. edbInfo.ClassifyId = item.ClassifyId
  1682. edbInfo.SysUserId = item.SysUserId
  1683. edbInfo.SysUserRealName = item.SysUserRealName
  1684. edbInfo.CreateTime = time.Now()
  1685. edbInfo.ModifyTime = time.Now()
  1686. edbInfo.ServerUrl = item.ServerUrl
  1687. edbInfo.Sort = maxSort + 1
  1688. edbInfo.DataDateType = `交易日`
  1689. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1690. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  1691. edbInfo.EdbType = edbType
  1692. edbInfo.SubSource = item.SubSource
  1693. edbInfo.SubSourceName = "日期序列"
  1694. edbInfo.IndicatorCode = item.IndicatorCode
  1695. edbInfo.StockCode = item.StockCode
  1696. edbInfo.TerminalCode = item.TerminalCode
  1697. edbInfo.Extra = item.Extra
  1698. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  1699. if err != nil {
  1700. errMsg = "保存失败"
  1701. err = errors.New("保存失败,Err:" + err.Error())
  1702. return
  1703. }
  1704. edbInfo.EdbInfoId = int(edbInfoId)
  1705. //保存数据
  1706. err = data_manage.ModifyEdbInfoWindWsdDataStatus(source, item.SubSource, edbInfoId, item.EdbCode)
  1707. if err != nil {
  1708. errMsg = "保存失败"
  1709. err = errors.New("修改数据对应指标ID失败,Err:" + err.Error())
  1710. return
  1711. }
  1712. maxAndMinItem, _ := data_manage.GetEdbInfoWsdMaxAndMinInfo(source, item.SubSource, item.EdbCode)
  1713. if maxAndMinItem != nil {
  1714. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  1715. }
  1716. //添加es
  1717. AddOrEditEdbInfoToEs(int(edbInfoId))
  1718. return
  1719. }
  1720. // BatchRefreshEdbByEdbIds 批量刷新指标
  1721. func BatchRefreshEdbByEdbIds(edbIdList []int, redisKey string, refreshKeys []string) (syncing bool, err error) {
  1722. if len(edbIdList) <= 0 {
  1723. return
  1724. }
  1725. // 设置刷新缓存
  1726. if redisKey != `` {
  1727. // 设置最多10分钟缓存
  1728. utils.Rc.SetNX(redisKey, 1, time.Minute*10)
  1729. }
  1730. // 获取需要刷新的指标列表
  1731. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbIdList)
  1732. // 需要刷新的指标数量
  1733. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  1734. // 关联指标过多的时候, 异步刷新
  1735. if totalEdbInfo > 20 {
  1736. syncing = true
  1737. // 加入队列刷新
  1738. var refreshItem EdbQueueRefreshReq
  1739. refreshItem.RefreshKey = redisKey
  1740. refreshItem.ItemRefreshKeys = refreshKeys
  1741. refreshItem.BaseEdbInfoArr = newBaseEdbInfoArr
  1742. refreshItem.BasePredictEdbInfoArr = newBasePredictEdbInfoArr
  1743. refreshItem.CalculateMap = newCalculateMap
  1744. refreshItem.PredictCalculateMap = newPredictCalculateMap
  1745. refreshItem.CalculateArr = calculateArr
  1746. refreshItem.PredictCalculateArr = predictCalculateArr
  1747. go PushEdb2Refresh(refreshItem)
  1748. return
  1749. }
  1750. // 同步刷新
  1751. err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1752. if redisKey != `` {
  1753. _ = utils.Rc.Delete(redisKey)
  1754. }
  1755. if len(refreshKeys) > 0 {
  1756. for _, v := range refreshKeys {
  1757. _ = utils.Rc.Delete(v)
  1758. }
  1759. }
  1760. return
  1761. }
  1762. // GetEdbTerminal 获取终端信息
  1763. func GetEdbTerminal(source int, oldTerminalCode string) (edbTerminal *data_manage.EdbTerminal, err error) {
  1764. if source == utils.DATA_SOURCE_WIND && oldTerminalCode == "" {
  1765. tmpConfig := new(data_manage.EdbConfig)
  1766. tmpConfig, err = data_manage.GetConfigDetailByCode("wind_terminal_code")
  1767. if err != nil && !utils.IsErrNoRow(err) {
  1768. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  1769. return
  1770. }
  1771. err = nil
  1772. if tmpConfig != nil {
  1773. oldTerminalCode = tmpConfig.ConfigValue
  1774. //return
  1775. }
  1776. }
  1777. if oldTerminalCode != "" {
  1778. edbTerminal, err = data_manage.GetEdbTerminalByCode(oldTerminalCode)
  1779. if err != nil {
  1780. return
  1781. }
  1782. return
  1783. }
  1784. // 配置中没有的话,那么就从分组获取,
  1785. list, err := data_manage.GetEdbCountGroupByTerminal(source)
  1786. if err != nil {
  1787. return
  1788. }
  1789. //windUrlNum := 0
  1790. //获取对应的配置url
  1791. terminalNumMap := make(map[string]int)
  1792. for _, v := range list {
  1793. terminalNumMap[v.TerminalCode] = v.Total
  1794. }
  1795. terminalList, err := data_manage.GetEdbTerminalListBySource(source)
  1796. if err != nil {
  1797. return
  1798. }
  1799. if len(terminalList) == 0 {
  1800. err = errors.New("终端地址未配置")
  1801. return
  1802. }
  1803. num := 0
  1804. for _, v := range terminalList {
  1805. tmpNum := terminalNumMap[v.TerminalCode]
  1806. if edbTerminal == nil {
  1807. edbTerminal = v
  1808. num = tmpNum
  1809. } else if tmpNum < num {
  1810. edbTerminal = v
  1811. num = tmpNum
  1812. }
  1813. }
  1814. if edbTerminal == nil {
  1815. err = errors.New("获取配置的终端地址失败")
  1816. return
  1817. }
  1818. return
  1819. }
  1820. func GetEdbTerminalCodeBySource(source int, edbCode, stockCode string) (terminalCode, serverUrl, sourceIndexName string, err error) {
  1821. var e error
  1822. if stockCode != "" {
  1823. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + stockCode
  1824. terminalCode, e = utils.Rc.RedisString(terminalCodeCacheKey)
  1825. if e != nil {
  1826. utils.FileLog.Info(fmt.Sprintf("从缓存中获取数据源终端信息失败, Err: %s", e))
  1827. }
  1828. terminalInfo, e := GetEdbTerminal(source, terminalCode)
  1829. if e != nil {
  1830. err = errors.New("获取可以使用的终端失败,Err:" + e.Error())
  1831. return
  1832. }
  1833. serverUrl = terminalInfo.ServerUrl
  1834. terminalCode = terminalInfo.TerminalCode
  1835. return
  1836. }
  1837. // 获取终端信息
  1838. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
  1839. terminalCode, tE := utils.Rc.RedisString(terminalCodeCacheKey)
  1840. if tE != nil {
  1841. utils.FileLog.Info(fmt.Sprintf("从缓存中获取数据源终端信息失败, Err: %s", tE))
  1842. }
  1843. var terminalCodeOrigin string
  1844. terminalCodeOrigin, sourceIndexName, tE = GetTerminalFromBaseIndex(source, edbCode)
  1845. if tE != nil {
  1846. utils.FileLog.Info(fmt.Sprintf("获取数据源终端信息失败, Err: %s", tE))
  1847. }
  1848. if terminalCodeOrigin != "" {
  1849. terminalCode = terminalCodeOrigin
  1850. }
  1851. if source == utils.DATA_SOURCE_WIND || source == utils.DATA_SOURCE_THS {
  1852. terminalInfo, e := GetEdbTerminal(source, terminalCode)
  1853. if e != nil {
  1854. err = errors.New("获取可以使用的终端失败,Err:" + e.Error())
  1855. return
  1856. }
  1857. serverUrl = terminalInfo.ServerUrl
  1858. terminalCode = terminalInfo.TerminalCode
  1859. }
  1860. return
  1861. }
  1862. // handleByAddEdbInfo
  1863. // @Description: 添加指标后的处理操作
  1864. // @author: Roc
  1865. // @datetime 2024-07-22 13:06:36
  1866. // @param edbInfo *data_manage.EdbInfo
  1867. func handleByAddEdbInfo(edbInfo *data_manage.EdbInfo) {
  1868. // 如果是手工数据,那么需要标记手工数据为已加入指标库
  1869. if edbInfo.Source == utils.DATA_SOURCE_MANUAL {
  1870. _ = models.UpdateManualIsJoinEdbStatus(edbInfo.EdbCode, 1)
  1871. }
  1872. }
  1873. // handleByDelEdbInfo
  1874. // @Description: 删除指标后的处理操作
  1875. // @author: Roc
  1876. // @datetime 2024-07-22 13:06:36
  1877. // @param edbInfo *data_manage.EdbInfo
  1878. func handleByDelEdbInfo(edbInfo *data_manage.EdbInfo) {
  1879. // 如果删除的指标是自定义分析的来源,那么还需要删除指标与excel的关系
  1880. if edbInfo.Source == utils.DATA_SOURCE_CALCULATE_ZDYFX {
  1881. err := excel.DeleteCustomAnalysisExcelEdbMappingByEdbInfoId(edbInfo.EdbInfoId)
  1882. if err != nil {
  1883. utils.FileLog.Error(fmt.Sprintf("删除指标时,需要删除与自定义分析的关系失败,指标ID:%d,Err:%s", edbInfo.EdbInfoId, err.Error()), 3)
  1884. }
  1885. }
  1886. // 如果是手工数据,那么需要标记手工数据为未加入指标库
  1887. if edbInfo.Source == utils.DATA_SOURCE_MANUAL {
  1888. _ = models.UpdateManualIsJoinEdbStatus(edbInfo.EdbCode, 0)
  1889. }
  1890. }
  1891. // GetIsSupplierStop
  1892. // @Description: 获取是否供应商停更
  1893. // @author: Roc
  1894. // @datetime 2024-08-27 10:28:19
  1895. // @param source int
  1896. // @param edbCode string
  1897. // @return isSupplierStop int
  1898. func GetIsSupplierStop(source int, edbCode string) (isSupplierStop int) {
  1899. return
  1900. }
  1901. //IsSupplierStop int `description:"是否供应商停更:1:停更,0:未停更"`