edb_info.go 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  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. // AddOrEditAllEdbInfoToEs 修复ES中的所有指标
  658. func AddOrEditAllEdbInfoToEs() {
  659. //添加es
  660. total, itemInfoList, _ := data_manage.GetEdbInfoFilterList("", []interface{}{}, 0, 100000)
  661. for k, itemInfo := range itemInfoList {
  662. elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  663. fmt.Println("剩余", int(total)-k-1, "条指标数据")
  664. }
  665. }
  666. // DeleteEdbInfoToEs 删除ES中的指标
  667. func DeleteEdbInfoToEs(edbInfoId int) {
  668. //添加es
  669. go elastic.EsDeleteEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(edbInfoId))
  670. }
  671. // RefreshManualData 根据手工指标code刷新手工指标数据
  672. func RefreshManualData(edbCode string) {
  673. var err error
  674. defer func() {
  675. if err != nil {
  676. go alarm_msg.SendAlarmMsg("导入手工数据后,根据手工指标code刷新手工指标数据失败提醒,Err"+err.Error(), 3)
  677. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"导入手工数据后,根据手工指标code刷新手工指标数据失败提醒", "errmsg:"+err.Error(), utils.EmailSendToUsers)
  678. }
  679. }()
  680. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MANUAL, edbCode)
  681. if err != nil {
  682. if utils.IsErrNoRow(err) {
  683. err = nil
  684. }
  685. return
  686. }
  687. // 刷新指标库
  688. err, _ = EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, true, true)
  689. return
  690. }
  691. // 获取频度的英文版
  692. func GetFrequencyEn(frequency string) (frequencyEn string) {
  693. switch frequency {
  694. case "日度":
  695. frequencyEn = "day"
  696. return
  697. case "周度":
  698. frequencyEn = "week"
  699. return
  700. case "旬度":
  701. frequencyEn = "ten days"
  702. return
  703. case "月度":
  704. frequencyEn = "month"
  705. return
  706. case "季度":
  707. frequencyEn = "quarter"
  708. return
  709. case "年度":
  710. frequencyEn = "year"
  711. return
  712. }
  713. return
  714. }
  715. func GetLeadUnitEn(unit string) (unitEn string) {
  716. switch unit {
  717. case "天":
  718. unitEn = "day"
  719. return
  720. case "周":
  721. unitEn = "week"
  722. return
  723. case "月":
  724. unitEn = "month"
  725. return
  726. case "季":
  727. unitEn = "quarter"
  728. return
  729. case "年":
  730. unitEn = "year"
  731. return
  732. }
  733. return
  734. }
  735. // getRefreshEdbInfoList 获取待更新的指标(普通基础指标、普通运算指标,预测运算指标)
  736. func getRefreshEdbInfoListByIds(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int, err error, errMsg string) {
  737. calculateList, err := data_manage.GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList)
  738. if err != nil && !utils.IsErrNoRow(err) {
  739. err = errors.New("GetEdbInfoAllCalculate Err:" + err.Error())
  740. return
  741. }
  742. // 获取指标信息
  743. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  744. if err != nil {
  745. err = errors.New("GetEdbInfoAllCalGetEdbInfoByIdr:" + err.Error())
  746. return
  747. }
  748. baseEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 基础普通指标
  749. calculateInfoArr := make([]*data_manage.EdbInfo, 0) //基础运算指标
  750. basePredictEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 预测普通指标
  751. predictCalculateInfoArr := make([]*data_manage.EdbInfo, 0) //预测运算指标
  752. for _, baseEdbInfo := range edbInfoList {
  753. // 指标若停更, 则忽略掉
  754. if baseEdbInfo.NoUpdate == 1 {
  755. continue
  756. }
  757. if baseEdbInfo.EdbInfoType == 0 { //普通指标
  758. switch baseEdbInfo.EdbType {
  759. case 1: //基础指标
  760. baseEdbInfoArr = append(baseEdbInfoArr, baseEdbInfo)
  761. case 2: //计算指标
  762. calculateList = append(calculateList, baseEdbInfo)
  763. }
  764. } else { //预测指标
  765. switch baseEdbInfo.EdbType {
  766. case 1: //基础指标
  767. basePredictEdbInfoArr = append(basePredictEdbInfoArr, baseEdbInfo)
  768. case 2: //计算指标
  769. predictCalculateInfoArr = append(predictCalculateInfoArr, baseEdbInfo)
  770. }
  771. }
  772. }
  773. // 计算指标
  774. for _, v := range calculateList {
  775. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  776. if tmpErr != nil {
  777. err = tmpErr
  778. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  779. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  780. return
  781. }
  782. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  783. switch tmpEdbInfo.EdbInfoType {
  784. case 0: //普通基础指标
  785. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  786. case 1: //预测基础指标
  787. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  788. }
  789. }
  790. for _, tmpEdbInfo := range getCalculateInfoArr {
  791. switch tmpEdbInfo.EdbInfoType {
  792. case 0: //普通运算指标
  793. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  794. case 1: //预测运算指标
  795. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  796. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  797. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  798. } else {
  799. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  800. }
  801. }
  802. }
  803. switch v.EdbInfoType {
  804. case 0: //普通运算指标
  805. calculateInfoArr = append(calculateInfoArr, v)
  806. case 1: //预测指标
  807. if v.EdbType == 1 { //预测普通指标
  808. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  809. } else {
  810. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  811. }
  812. }
  813. }
  814. // 预测计算指标
  815. for _, v := range predictCalculateInfoArr {
  816. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  817. if tmpErr != nil {
  818. err = tmpErr
  819. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  820. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  821. return
  822. }
  823. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  824. switch tmpEdbInfo.EdbInfoType {
  825. case 0: //普通基础指标
  826. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  827. case 1: //预测基础指标
  828. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  829. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  830. } else {
  831. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  832. }
  833. //basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  834. }
  835. }
  836. for _, tmpEdbInfo := range getCalculateInfoArr {
  837. switch tmpEdbInfo.EdbInfoType {
  838. case 0: //普通运算指标
  839. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  840. case 1: //预测运算指标
  841. if tmpEdbInfo.EdbType == 1 { //预测普通指标
  842. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  843. } else {
  844. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  845. }
  846. //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  847. }
  848. }
  849. switch v.EdbInfoType {
  850. case 0: //普通运算指标
  851. calculateInfoArr = append(calculateInfoArr, v)
  852. case 1: //预测指标
  853. if v.EdbType == 1 { //预测普通指标
  854. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  855. } else {
  856. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  857. }
  858. }
  859. }
  860. // 第一次的计算指标map
  861. newCalculateMap = make(map[int]*data_manage.EdbInfo)
  862. for _, v := range calculateInfoArr {
  863. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  864. calculateArr = append(calculateArr, v.EdbInfoId)
  865. }
  866. newCalculateMap[v.EdbInfoId] = v
  867. }
  868. // 基础预测指标
  869. if len(basePredictEdbInfoArr) > 0 {
  870. basePredictEdbInfoIdList := make([]int, 0)
  871. for _, v := range basePredictEdbInfoArr {
  872. basePredictEdbInfoIdList = append(basePredictEdbInfoIdList, v.EdbInfoId)
  873. }
  874. baseEdbInfoList, tmpErr := data_manage.GetPredictEdbInfoAllCalculate(basePredictEdbInfoIdList)
  875. if tmpErr != nil {
  876. err = tmpErr
  877. return
  878. }
  879. tmpCalculateList := make([]*data_manage.EdbInfo, 0)
  880. for _, v := range baseEdbInfoList {
  881. switch v.EdbType {
  882. case 1:
  883. baseEdbInfoArr = append(baseEdbInfoArr, v)
  884. case 2:
  885. // 如果不在第一次里面,那么在第二次 刷新预测指标的时候 需要加进去
  886. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  887. tmpCalculateList = append(tmpCalculateList, v)
  888. }
  889. }
  890. }
  891. // 这里是查询预测指标用到的 普通计算指标
  892. for _, v := range tmpCalculateList {
  893. getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
  894. if tmpErr != nil {
  895. err = tmpErr
  896. fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
  897. errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
  898. return
  899. }
  900. // 基础指标
  901. for _, tmpEdbInfo := range getBaseEdbInfoArr {
  902. switch tmpEdbInfo.EdbInfoType {
  903. case 0: //普通基础指标
  904. baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
  905. case 1: //预测基础指标
  906. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  907. }
  908. }
  909. // 计算指标
  910. for _, tmpEdbInfo := range getCalculateInfoArr {
  911. switch tmpEdbInfo.EdbInfoType {
  912. case 0: //普通运算指标
  913. calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
  914. case 1: //预测运算指标
  915. if v.EdbType == 1 { //预测普通指标
  916. basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
  917. } else {
  918. predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
  919. }
  920. }
  921. }
  922. switch v.EdbInfoType {
  923. case 0: //普通运算指标
  924. calculateInfoArr = append(calculateInfoArr, v)
  925. case 1: //预测指标
  926. if v.EdbType == 1 { //预测普通指标
  927. basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
  928. } else { //预测运算指标
  929. predictCalculateInfoArr = append(predictCalculateInfoArr, v)
  930. }
  931. }
  932. }
  933. // 第二次计算指标的map
  934. for _, v := range calculateInfoArr {
  935. if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
  936. calculateArr = append(calculateArr, v.EdbInfoId)
  937. }
  938. newCalculateMap[v.EdbInfoId] = v
  939. }
  940. }
  941. // 普通基础指标去重
  942. newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  943. baseMap := make(map[int]int)
  944. for _, v := range baseEdbInfoArr {
  945. if _, ok := baseMap[v.EdbInfoId]; !ok {
  946. newBaseEdbInfoArr = append(newBaseEdbInfoArr, v)
  947. }
  948. baseMap[v.EdbInfoId] = v.EdbInfoId
  949. }
  950. // 普通计算指标的id
  951. sort.Ints(calculateArr)
  952. // 普通预测指标去重
  953. newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
  954. basePredictMap := make(map[int]int)
  955. for _, v := range basePredictEdbInfoArr {
  956. if _, ok := basePredictMap[v.EdbInfoId]; !ok {
  957. newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, v)
  958. }
  959. basePredictMap[v.EdbInfoId] = v.EdbInfoId
  960. }
  961. // 预测计算指标去重
  962. newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
  963. for _, v := range predictCalculateInfoArr {
  964. if _, ok := newPredictCalculateMap[v.EdbInfoId]; !ok {
  965. predictCalculateArr = append(predictCalculateArr, v.EdbInfoId)
  966. }
  967. newPredictCalculateMap[v.EdbInfoId] = v
  968. }
  969. // 预测计算指标的id
  970. sort.Ints(predictCalculateArr)
  971. return
  972. }
  973. type AddEdbBaseInfoReq struct {
  974. EdbCode string `description:"指标编码"`
  975. EdbName string `description:"指标名称"`
  976. Unit string `description:"单位"`
  977. ClassifyId int `description:"所属分类"`
  978. SysUserId int `description:"用户id"`
  979. SysUserRealName string `description:"用户真实名称"`
  980. }
  981. // EdbInfoAdd 添加指标到指标库
  982. 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) {
  983. // 有些指标的添加是走的指标公共服务
  984. if source == utils.DATA_SOURCE_BUSINESS {
  985. req2 := AddEdbBaseInfoReq{
  986. EdbCode: edbCode,
  987. EdbName: edbName,
  988. Unit: unit,
  989. ClassifyId: classifyId,
  990. SysUserId: sysUserId,
  991. SysUserRealName: sysUserRealName,
  992. }
  993. reqJson, tmpErr := json.Marshal(req2)
  994. if tmpErr != nil {
  995. err = tmpErr
  996. return
  997. }
  998. respItem, tmpErr := AddBaseEdbInfo(string(reqJson), source, subSource, lang)
  999. if tmpErr != nil {
  1000. errMsg = "添加失败"
  1001. err = tmpErr
  1002. return
  1003. }
  1004. isSendEmail = respItem.IsSendEmail
  1005. if respItem.Ret != 200 {
  1006. errMsg = respItem.Msg
  1007. err = errors.New(respItem.ErrMsg)
  1008. } else {
  1009. edbInfo, err = data_manage.GetEdbInfoById(respItem.Data.EdbInfoId)
  1010. }
  1011. return
  1012. }
  1013. isSendEmail = true
  1014. //判断指标名称是否存在
  1015. {
  1016. var count int
  1017. var condition string
  1018. var pars []interface{}
  1019. //condition += " AND source=? "
  1020. //pars = append(pars, source)
  1021. switch lang {
  1022. case utils.EnLangVersion:
  1023. condition += " AND edb_name_en = ? "
  1024. default:
  1025. condition += " AND edb_name=? "
  1026. }
  1027. pars = append(pars, edbName)
  1028. count, err = data_manage.GetEdbInfoCountByCondition(condition, pars)
  1029. if err != nil {
  1030. errMsg = "判断指标名称是否存在失败"
  1031. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  1032. return
  1033. }
  1034. if count > 0 {
  1035. errMsg = "指标名称已存在,请重新填写"
  1036. err = errors.New(errMsg)
  1037. isSendEmail = false
  1038. return
  1039. }
  1040. }
  1041. edbInfo = new(data_manage.EdbInfo)
  1042. edbInfo.Source = source
  1043. var sourceName string
  1044. edbSource := data_manage.GetEdbSourceBySourceId(source)
  1045. if edbSource != nil {
  1046. sourceName = edbSource.SourceName
  1047. }
  1048. if sourceName == "" {
  1049. errMsg = "指标来源异常"
  1050. err = errors.New(errMsg)
  1051. return
  1052. }
  1053. edbInfo.SourceName = sourceName
  1054. edbType := 1 //基础指标
  1055. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1056. edbType = 2 //计算指标
  1057. }
  1058. // 从缓存中获取
  1059. terminalCode, serverUrl, sourceIndexName, e := GetEdbTerminalCodeBySource(edbInfo.Source, edbInfo.EdbCode, edbInfo.StockCode)
  1060. if e != nil {
  1061. errMsg = "获取可以使用的终端地址失败"
  1062. err = errors.New("获取可以使用的终端地址失败,Err:" + e.Error())
  1063. return
  1064. }
  1065. //获取该层级下最大的排序数
  1066. maxSort, err := GetEdbClassifyMaxSort(classifyId, 0)
  1067. if err != nil {
  1068. errMsg = "获取失败"
  1069. err = errors.New("查询排序信息失败,Err:" + err.Error())
  1070. return
  1071. }
  1072. edbInfo.EdbCode = edbCode
  1073. edbInfo.EdbName = edbName
  1074. edbInfo.EdbNameEn = edbName
  1075. edbInfo.EdbNameSource = edbName
  1076. edbInfo.Frequency = frequency
  1077. edbInfo.Unit = unit
  1078. edbInfo.UnitEn = unit
  1079. if startDate != `` {
  1080. startDateTime, tmpErr := utils.FormatDateStrToTime(startDate)
  1081. if tmpErr != nil {
  1082. err = tmpErr
  1083. return
  1084. }
  1085. edbInfo.StartDate = startDateTime
  1086. }
  1087. if endDate != `` {
  1088. endDateTime, tmpErr := utils.FormatDateStrToTime(endDate)
  1089. if tmpErr != nil {
  1090. err = tmpErr
  1091. return
  1092. }
  1093. edbInfo.EndDate = endDateTime
  1094. }
  1095. edbInfo.ClassifyId = classifyId
  1096. edbInfo.SysUserId = sysUserId
  1097. edbInfo.SysUserRealName = sysUserRealName
  1098. edbInfo.CreateTime = time.Now()
  1099. edbInfo.ModifyTime = time.Now()
  1100. edbInfo.ServerUrl = serverUrl
  1101. edbInfo.Sort = maxSort + 1
  1102. edbInfo.DataDateType = `交易日`
  1103. edbInfo.TerminalCode = terminalCode
  1104. edbInfo.SourceIndexName = sourceIndexName
  1105. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1106. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  1107. itemVal, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  1108. if itemVal != nil && err == nil {
  1109. edbInfo.MaxValue = itemVal.MaxValue
  1110. edbInfo.MinValue = itemVal.MinValue
  1111. }
  1112. edbInfo.EdbType = edbType
  1113. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  1114. if err != nil {
  1115. errMsg = "保存失败"
  1116. err = errors.New("保存失败,Err:" + err.Error())
  1117. return
  1118. }
  1119. edbInfo.EdbInfoId = int(edbInfoId)
  1120. //保存数据
  1121. data_manage.ModifyEdbInfoDataStatus(edbInfoId, source, subSource, edbCode)
  1122. maxAndMinItem, _ := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
  1123. if maxAndMinItem != nil {
  1124. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  1125. }
  1126. //添加es
  1127. AddOrEditEdbInfoToEs(int(edbInfoId))
  1128. go handleByAddEdbInfo(edbInfo)
  1129. return
  1130. }
  1131. // TraceEdbInfoByEdbInfoId 指标追溯
  1132. func TraceEdbInfoByEdbInfoId(edbInfoId, sysUserId int) (traceEdbInfo data_manage.TraceEdbInfoResp, err error) {
  1133. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1134. if err != nil {
  1135. return
  1136. }
  1137. edbInfoRuleMap := make(map[int]string, 0)
  1138. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  1139. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  1140. traceEdbInfo = data_manage.TraceEdbInfoResp{
  1141. //EdbInfoId: edbInfo.EdbInfoId,
  1142. EdbInfoId: edbInfoId,
  1143. EdbInfoType: edbInfo.EdbInfoType,
  1144. EdbName: edbInfo.EdbName,
  1145. EdbNameEn: edbInfo.EdbNameEn,
  1146. EdbType: edbInfo.EdbType,
  1147. //Source: edbInfo.Source,
  1148. UniqueCode: edbInfo.UniqueCode,
  1149. ClassifyId: edbInfo.ClassifyId,
  1150. IsStop: edbInfo.NoUpdate,
  1151. EdbInfo: edbInfo,
  1152. }
  1153. findIdMap := make(map[int]int)
  1154. findIdMap[edbInfoId] = edbInfoId
  1155. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  1156. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1157. edbInfoIdList := make([]int, 0)
  1158. for _, v := range findIdMap {
  1159. edbInfoIdList = append(edbInfoIdList, v)
  1160. }
  1161. classifyIdList := make([]int, 0)
  1162. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1163. if err != nil {
  1164. return
  1165. }
  1166. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1167. for _, tmpEdbInfo := range edbInfoList {
  1168. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  1169. classifyIdList = append(classifyIdList, tmpEdbInfo.ClassifyId)
  1170. }
  1171. // 指标权限Map
  1172. edbInfoIdPermissionMap := make(map[int]bool)
  1173. // 指标权限
  1174. {
  1175. // 当前的分类
  1176. classifyMap := make(map[int]*data_manage.EdbClassify)
  1177. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1178. if tmpErr != nil {
  1179. err = tmpErr
  1180. return
  1181. }
  1182. for _, v := range classifyList {
  1183. classifyMap[v.ClassifyId] = v
  1184. }
  1185. // 获取所有有权限的指标和分类
  1186. permissionEdbIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUserId, 0, 0)
  1187. if tmpErr != nil {
  1188. err = tmpErr
  1189. return
  1190. }
  1191. for _, v := range edbInfoMap {
  1192. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  1193. edbInfoIdPermissionMap[v.EdbInfoId] = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1194. }
  1195. }
  1196. }
  1197. traceEdbInfo, err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap, edbInfoIdPermissionMap)
  1198. // 权限校验
  1199. return
  1200. }
  1201. // TraceEdbInfoByEdbInfoIdList 指标追溯
  1202. func TraceEdbInfoByEdbInfoIdList(edbInfoIdList []int) (traceEdbInfoList []data_manage.TraceEdbInfoResp, err error) {
  1203. traceEdbInfoList = make([]data_manage.TraceEdbInfoResp, 0)
  1204. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  1205. if err != nil {
  1206. return
  1207. }
  1208. edbInfoRuleMap := make(map[int]string, 0)
  1209. edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
  1210. findIdMap := make(map[int]int)
  1211. existMap := make(map[int]data_manage.TraceEdbInfoResp)
  1212. for _, edbInfo := range edbInfoList {
  1213. findIdMap[edbInfo.EdbInfoId] = edbInfo.EdbInfoId
  1214. //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
  1215. traceEdbInfo := data_manage.TraceEdbInfoResp{
  1216. //EdbInfoId: edbInfo.EdbInfoId,
  1217. EdbInfoId: edbInfo.EdbInfoId,
  1218. EdbInfoType: edbInfo.EdbInfoType,
  1219. EdbName: edbInfo.EdbName,
  1220. EdbNameEn: edbInfo.EdbNameEn,
  1221. EdbType: edbInfo.EdbType,
  1222. //Source: edbInfo.Source,
  1223. UniqueCode: edbInfo.UniqueCode,
  1224. ClassifyId: edbInfo.ClassifyId,
  1225. IsStop: edbInfo.NoUpdate,
  1226. EdbInfo: edbInfo,
  1227. }
  1228. traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfo.EdbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1229. traceEdbInfoList = append(traceEdbInfoList, traceEdbInfo)
  1230. }
  1231. //findEdbInfoIdList := make([]int, 0)
  1232. //for _, v := range findIdMap {
  1233. // findEdbInfoIdList = append(findEdbInfoIdList, v)
  1234. //}
  1235. //findEdbInfoList, err := data_manage.GetEdbInfoByIdList(findEdbInfoIdList)
  1236. //if err != nil {
  1237. // return
  1238. //}
  1239. //edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1240. //for _, tmpEdbInfo := range findEdbInfoList {
  1241. // edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
  1242. //}
  1243. //for k, traceEdbInfo := range traceEdbInfoList {
  1244. // traceEdbInfoList[k], err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
  1245. //}
  1246. return
  1247. }
  1248. // traceEdbInfoByEdbInfoId 指标追溯
  1249. 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) {
  1250. traceEdbInfo, ok := existMap[edbInfoId]
  1251. if ok {
  1252. return
  1253. }
  1254. child = make([]data_manage.TraceEdbInfoResp, 0)
  1255. edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
  1256. if e != nil {
  1257. err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
  1258. return
  1259. }
  1260. // 指标信息map
  1261. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1262. if len(edbInfoMappingList) > 0 {
  1263. fromEdbInfoIdList := make([]int, 0)
  1264. for _, v := range edbInfoMappingList {
  1265. fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
  1266. }
  1267. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(fromEdbInfoIdList)
  1268. if tmpErr != nil {
  1269. err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", tmpErr.Error())
  1270. return
  1271. }
  1272. for _, v := range edbInfoList {
  1273. edbInfoMap[v.EdbInfoId] = v
  1274. }
  1275. }
  1276. edbMappingMap[edbInfoId] = edbInfoMappingList
  1277. for _, v := range edbInfoMappingList {
  1278. tmpEdbInfoId := v.FromEdbInfoId
  1279. tmpTraceEdbInfo := data_manage.TraceEdbInfoResp{
  1280. EdbInfoId: tmpEdbInfoId,
  1281. EdbInfoType: v.FromEdbInfoType,
  1282. EdbType: v.FromEdbType,
  1283. UniqueCode: v.FromUniqueCode,
  1284. ClassifyId: v.FromClassifyId,
  1285. IsStop: v.NoUpdate,
  1286. EdbInfo: edbInfoMap[v.FromEdbInfoId],
  1287. }
  1288. // 计算指标/预测指标继续溯源
  1289. if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
  1290. // 查过了就不查了
  1291. if _, ok2 := findIdMap[tmpEdbInfoId]; !ok2 {
  1292. tmpTraceEdbInfo.Child, e = traceEdbInfoByEdbInfoId(tmpEdbInfoId, tmpTraceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
  1293. if e != nil {
  1294. err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
  1295. return
  1296. }
  1297. }
  1298. }
  1299. child = append(child, tmpTraceEdbInfo)
  1300. findIdMap[tmpEdbInfoId] = tmpEdbInfoId
  1301. }
  1302. existMap[edbInfoId] = traceEdbInfo
  1303. return
  1304. }
  1305. 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) {
  1306. edbInfo, ok := edbInfoMap[traceEdbInfoResp.EdbInfoId]
  1307. if !ok {
  1308. err = errors.New("指标异常")
  1309. return
  1310. }
  1311. var parentEdbInfo *data_manage.EdbInfo
  1312. if parentEdbInfoId > 0 {
  1313. parentEdbInfo, ok = edbInfoMap[parentEdbInfoId]
  1314. if !ok {
  1315. err = errors.New("指标异常")
  1316. return
  1317. }
  1318. }
  1319. //traceEdbInfoResp.EdbName = edbInfo.EdbName
  1320. traceEdbInfoResp.EdbName, traceEdbInfoResp.EdbNameEn, traceEdbInfoResp.RuleTitle, traceEdbInfoResp.RuleTitleEn = getEdbRuleTitle(edbInfo, parentEdbInfo, traceEdbInfoResp.Child, edbInfoMap, edbMappingMap)
  1321. if traceEdbInfoResp.Child != nil && len(traceEdbInfoResp.Child) > 0 {
  1322. for k, v := range traceEdbInfoResp.Child {
  1323. traceEdbInfoResp.Child[k], err = handleTraceEdbInfo(v, traceEdbInfoResp.EdbInfoId, edbInfoMap, edbMappingMap, edbInfoIdPermissionMap)
  1324. if err != nil {
  1325. return
  1326. }
  1327. }
  1328. }
  1329. // 指标权限
  1330. traceEdbInfoResp.HaveOperaAuth, _ = edbInfoIdPermissionMap[traceEdbInfoResp.EdbInfoId]
  1331. newTraceEdbInfoResp = traceEdbInfoResp
  1332. return
  1333. }
  1334. // getEdbRule 获取规则名称
  1335. 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) {
  1336. edbName = edbInfo.EdbName
  1337. edbNameEn = edbInfo.EdbNameEn
  1338. ruleTitle = `来源于` + edbInfo.SourceName
  1339. ruleTitleEn = `Source From: ` + edbInfo.SourceName
  1340. if parentEdbInfo != nil {
  1341. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  1342. if !ok {
  1343. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  1344. }
  1345. // 指标名称
  1346. switch parentEdbInfo.Source {
  1347. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  1348. for _, v := range edbMappingList {
  1349. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1350. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  1351. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, v.FromTag)
  1352. }
  1353. }
  1354. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ, utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 直接拼接 ,累计值同比拼接
  1355. for _, v := range edbMappingList {
  1356. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1357. tmpName := ``
  1358. tmpNameEn := ``
  1359. if v.FromTag == `A` {
  1360. tmpName = `拼接日期前`
  1361. tmpNameEn = `Before Concatenation Date:`
  1362. } else if v.FromTag == `B` {
  1363. tmpName = `拼接日期后`
  1364. tmpNameEn = `After Concatenation Date:`
  1365. }
  1366. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  1367. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, tmpNameEn)
  1368. }
  1369. }
  1370. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  1371. for _, v := range edbMappingList {
  1372. //(需对上游指标+自变量,领先10天/因变量)
  1373. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1374. tmpName := ``
  1375. tmpNameEn := ``
  1376. if v.FromTag == `A` {
  1377. tmpName = fmt.Sprintf(`自变量,领先%d天`, v.MoveValue)
  1378. tmpNameEn = fmt.Sprintf(`Independent Variable, Lead %d Days:`, v.MoveValue)
  1379. } else if v.FromTag == `B` {
  1380. tmpName = `因变量`
  1381. tmpNameEn = `Dependent Variable`
  1382. }
  1383. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, tmpName)
  1384. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, tmpNameEn)
  1385. }
  1386. }
  1387. case utils.DATA_SOURCE_CALCULATE_CORRELATION: // 滚动相关性
  1388. for _, v := range edbMappingList {
  1389. if v.FromEdbInfoId == edbInfo.EdbInfoId {
  1390. edbName = fmt.Sprintf("%s(%s)", edbInfo.EdbName, v.FromTag)
  1391. edbNameEn = fmt.Sprintf("%s(%s)", edbInfo.EdbNameEn, v.FromTag)
  1392. }
  1393. }
  1394. }
  1395. }
  1396. if edbInfo.EdbType == 1 {
  1397. // 基础指标的话,直接返回来源名称
  1398. //ruleTitle = `来源于`+edbInfo.SourceName
  1399. return
  1400. }
  1401. // 规则
  1402. switch edbInfo.Source {
  1403. case utils.DATA_SOURCE_CALCULATE, utils.DATA_SOURCE_PREDICT_CALCULATE:
  1404. ruleTitle = "=" + edbInfo.CalculateFormula
  1405. case utils.DATA_SOURCE_CALCULATE_LJZZY, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY:
  1406. ruleTitle = `累计转月值计算`
  1407. ruleTitleEn = `Cumulative to Monthly Calculation`
  1408. case utils.DATA_SOURCE_CALCULATE_TBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ:
  1409. ruleTitle = `同比值计算`
  1410. ruleTitleEn = `Year-on-Year Value Calculation`
  1411. case utils.DATA_SOURCE_CALCULATE_TCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ:
  1412. ruleTitle = `同差值计算`
  1413. ruleTitleEn = `Yearly Difference Calculation`
  1414. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS:
  1415. ruleTitle = fmt.Sprintf("N数值移动均值计算(N=%s)", edbInfo.CalculateFormula)
  1416. ruleTitleEn = fmt.Sprintf("N-Value Moving Average Calculation(N=%s)", edbInfo.CalculateFormula)
  1417. case utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ:
  1418. ruleTitle = fmt.Sprintf("N数值环比值计算(N=%s)", edbInfo.CalculateFormula)
  1419. ruleTitleEn = fmt.Sprintf("N-Value Sequential Comparison Calculation(N=%s)", edbInfo.CalculateFormula)
  1420. case utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ:
  1421. ruleTitle = fmt.Sprintf("N数值环差值计算(N=%s)", edbInfo.CalculateFormula)
  1422. ruleTitleEn = fmt.Sprintf("N-Value Sequential Difference Calculation(N=%s)", edbInfo.CalculateFormula)
  1423. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
  1424. moveType := `领先`
  1425. moveTypeEn := `Lead`
  1426. if edbInfo.MoveType == 2 {
  1427. moveType = "滞后"
  1428. moveTypeEn = `Lag`
  1429. }
  1430. ruleTitle = fmt.Sprintf("时间移位计算(%s%s%s)", moveType, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  1431. ruleTitleEn = fmt.Sprintf("Time Shift Calculation(%s%s%s)", moveTypeEn, edbInfo.CalculateFormula, edbInfo.MoveFrequency)
  1432. case utils.DATA_SOURCE_CALCULATE_BP, utils.DATA_SOURCE_PREDICT_CALCULATE_BP: // 变频
  1433. childFrequency := ``
  1434. if len(childList) > 0 {
  1435. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  1436. childFrequency = childEdbInfo.Frequency
  1437. }
  1438. }
  1439. ruleTitle = fmt.Sprintf("升频计算(%s转%s)", childFrequency, edbInfo.Frequency)
  1440. ruleTitleEn = fmt.Sprintf("Upsampling Calculation(%s转%s)", childFrequency, edbInfo.Frequency)
  1441. case utils.DATA_SOURCE_CALCULATE_ZJPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: // 直接拼接
  1442. ruleTitle = fmt.Sprintf("直接拼接计算(%s)", edbInfo.CalculateFormula)
  1443. ruleTitleEn = fmt.Sprintf("Direct Concatenation Calculation(%s)", edbInfo.CalculateFormula)
  1444. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ, utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: // 累计值同比拼
  1445. ruleTitle = fmt.Sprintf("累计值同比值拼接计算(%s)", edbInfo.CalculateFormula)
  1446. ruleTitleEn = fmt.Sprintf("Cumulative Year-on-Year Concatenation Calculation(%s)", edbInfo.CalculateFormula)
  1447. case utils.DATA_SOURCE_PYTHON:
  1448. ruleTitle = `代码运算`
  1449. ruleTitleEn = `Code Computation`
  1450. case utils.DATA_SOURCE_CALCULATE_CJJX, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
  1451. ruleTitle = fmt.Sprintf("超季节性计算(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  1452. ruleTitleEn = fmt.Sprintf("Super Seasonality Calculation(N=%s,%s)", edbInfo.CalculateFormula, edbInfo.Calendar)
  1453. case utils.DATA_SOURCE_CALCULATE_NHCC, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //计算指标(拟合残差)
  1454. var startDate, endDate string
  1455. dateList := strings.Split(edbInfo.CalculateFormula, ",")
  1456. if len(dateList) == 2 {
  1457. startDate = dateList[0]
  1458. endDate = dateList[1]
  1459. }
  1460. ruleTitle = fmt.Sprintf("拟合残差计算(%s至%s)", startDate, endDate)
  1461. ruleTitleEn = fmt.Sprintf("Fit Residual Calculation(%s至%s)", startDate, endDate)
  1462. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  1463. ruleTitle = `数据调整`
  1464. ruleTitleEn = `Data Adjustment`
  1465. case utils.DATA_SOURCE_CALCULATE_NH, utils.DATA_SOURCE_PREDICT_CALCULATE_NH:
  1466. ruleTitle = `年化计算`
  1467. ruleTitleEn = `Annualization Calculation`
  1468. case utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: // 扩散指数->53
  1469. type KszsConfig struct {
  1470. DateType int `description:"扩散指标日期;1:全部指标日期并集;2:部分指标日期并集"`
  1471. CheckList []string `description:"选中的数据,A,B,C"`
  1472. }
  1473. var config KszsConfig
  1474. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &config)
  1475. if err != nil {
  1476. return
  1477. }
  1478. var startDate, endDate time.Time
  1479. childEdbInfoIdList := make([]int, 0)
  1480. if config.DateType == 1 {
  1481. for _, v := range childList {
  1482. childEdbInfoIdList = append(childEdbInfoIdList, v.EdbInfoId)
  1483. }
  1484. } else {
  1485. if parentEdbInfo != nil {
  1486. edbMappingList, ok := edbMappingMap[parentEdbInfo.EdbInfoId]
  1487. if !ok {
  1488. edbMappingList = []*data_manage.EdbInfoCalculateMappingInfo{}
  1489. }
  1490. tagMap := make(map[string]int)
  1491. for _, v := range edbMappingList {
  1492. tagMap[v.FromTag] = v.FromEdbInfoId
  1493. }
  1494. for _, v := range config.CheckList {
  1495. if tmpEdbInfoId, ok := tagMap[v]; ok {
  1496. childEdbInfoIdList = append(childEdbInfoIdList, tmpEdbInfoId)
  1497. }
  1498. }
  1499. }
  1500. }
  1501. for _, v := range childEdbInfoIdList {
  1502. if childEdbInfo, ok := edbInfoMap[v]; ok {
  1503. tmpStartDate := childEdbInfo.StartDate
  1504. if startDate.IsZero() || startDate.After(tmpStartDate) {
  1505. startDate = tmpStartDate
  1506. }
  1507. tmpEndDate := childEdbInfo.EndDate
  1508. if endDate.IsZero() || endDate.Before(tmpEndDate) {
  1509. endDate = tmpEndDate
  1510. }
  1511. }
  1512. }
  1513. ruleTitle = fmt.Sprintf("扩散指数计算(%s至%s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  1514. ruleTitleEn = fmt.Sprintf("Diffusion Index Calculation(%s to %s)", startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  1515. case utils.DATA_SOURCE_STOCK_PLANT:
  1516. ruleTitle = `来源于装置分析`
  1517. ruleTitleEn = `Derived from Plant Analysis`
  1518. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  1519. type EdbCalculateFormula struct {
  1520. BaseCalculateValue int `description:"基础计算窗口"`
  1521. BaseCalculateUnit string `description:"基础计算频度"`
  1522. LeadValue int `description:"领先期数"`
  1523. LeadUnit string `description:"频度"`
  1524. CalculateValue int `description:"计算窗口"`
  1525. CalculateUnit string `description:"计算频度"`
  1526. }
  1527. var correlationConf EdbCalculateFormula
  1528. err := json.Unmarshal([]byte(edbInfo.CalculateFormula), &correlationConf)
  1529. if err != nil {
  1530. return
  1531. }
  1532. ruleTitle = fmt.Sprintf("滚动相关性(计算窗口%d%s,B领先A%d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  1533. ruleTitleEn = fmt.Sprintf("Rolling Correlation (Calculation Window %d%s, B Leads A %d%s)", correlationConf.CalculateValue, correlationConf.CalculateUnit, correlationConf.LeadValue, correlationConf.LeadUnit)
  1534. case utils.DATA_SOURCE_CALCULATE_JP, utils.DATA_SOURCE_PREDICT_CALCULATE_JP:
  1535. childFrequency := ``
  1536. if len(childList) > 0 {
  1537. if childEdbInfo, ok := edbInfoMap[childList[0].EdbInfoId]; ok {
  1538. childFrequency = childEdbInfo.Frequency
  1539. }
  1540. }
  1541. ruleTitle = fmt.Sprintf("降频计算(%s转%s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  1542. ruleTitleEn = fmt.Sprintf("Downsampling Calculation(%s to %s,%s)", childFrequency, edbInfo.Frequency, edbInfo.CalculateFormula)
  1543. case utils.DATA_SOURCE_CALCULATE_STANDARD_DEVIATION:
  1544. ruleTitle = fmt.Sprintf("标准差(滚动%s期)", edbInfo.CalculateFormula)
  1545. ruleTitleEn = fmt.Sprintf("Standard Deviation (Rolling %s Periods)", edbInfo.CalculateFormula)
  1546. case utils.DATA_SOURCE_CALCULATE_PERCENTILE, utils.DATA_SOURCE_PREDICT_CALCULATE_PERCENTILE:
  1547. type TempCalculate struct {
  1548. CalculateValue int `description:"计算窗口"`
  1549. CalculateUnit string `description:"计算频度"`
  1550. }
  1551. cf := TempCalculate{}
  1552. if e := json.Unmarshal([]byte(edbInfo.CalculateFormula), &cf); e != nil {
  1553. return
  1554. }
  1555. ruleTitle = fmt.Sprintf("百分位(时间长度%d%s)", cf.CalculateValue, cf.CalculateUnit)
  1556. ruleTitle = fmt.Sprintf("Percentile (Time Length %d%s)", cf.CalculateValue, cf.CalculateUnit)
  1557. case utils.DATA_SOURCE_CALCULATE_ZSXY, utils.DATA_SOURCE_PREDICT_CALCULATE_ZSXY:
  1558. ruleTitle = `指数修匀计算`
  1559. ruleTitle = `Index Smoothing Calculation`
  1560. }
  1561. return
  1562. }
  1563. // GetEdbChartAdminList
  1564. // @param source 来源 :1:手工数据指标 2:钢联化工数据库 3:ETA指标库 4:ETA预测指标 5:图库 6:ETA表格
  1565. func GetEdbChartAdminList(source int) (list []int, err error) {
  1566. switch source {
  1567. case 1: //手工数据指标
  1568. list, err = models.GetEdbInfoAdminList()
  1569. if err != nil {
  1570. return
  1571. }
  1572. case 3: //ETA指标库
  1573. list, err = data_manage.GetEdbInfoAdminList(0)
  1574. if err != nil {
  1575. return
  1576. }
  1577. case 4: //ETA预测指标
  1578. list, err = data_manage.GetEdbInfoAdminList(1)
  1579. if err != nil {
  1580. return
  1581. }
  1582. case 5: //图库
  1583. list, err = data_manage.GetChartInfoAdminList()
  1584. if err != nil {
  1585. return
  1586. }
  1587. default:
  1588. return
  1589. }
  1590. return
  1591. }
  1592. // EdbInfoReplace 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  1593. func EdbInfoReplace(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, sysAdminId int, sysAdminRealName string) (replaceChartTotal, replaceCalculateTotal int, err error) {
  1594. defer func() {
  1595. if err != nil {
  1596. fmt.Println("ERR:", err.Error())
  1597. }
  1598. }()
  1599. // 替换关联表中的source_edb_info_id
  1600. relationEdbInfoIdList, replaceChartTotal, replaceCalculateTotal, err := data_manage.ReplaceChartEdb(oldEdbInfo, newEdbInfo)
  1601. if err != nil {
  1602. return
  1603. }
  1604. //加入到缓存队列中处理
  1605. go cache.AddReplaceEdbInfo(oldEdbInfo, newEdbInfo)
  1606. // 更新所有的关联指标
  1607. err, _ = EdbInfoRefreshAllFromBaseV3(relationEdbInfoIdList, true, true, true)
  1608. return
  1609. }
  1610. func GetTerminalFromBaseIndex(source int, edbCode string) (terminalCode string, sourceIndexName string, err error) {
  1611. tbName := GetBaseIndexTableName(source)
  1612. if tbName != "" {
  1613. var item data_manage.BaseIndexTerminalCode
  1614. item, err = data_manage.GetBaseIndexTerminalCode(edbCode, tbName)
  1615. if err != nil {
  1616. err = fmt.Errorf("查询数据源指标信息失败, Err: %s", err)
  1617. return
  1618. }
  1619. terminalCode = item.TerminalCode
  1620. sourceIndexName = item.IndexName
  1621. }
  1622. return
  1623. }
  1624. func GetBaseIndexTableName(source int) (tableName string) {
  1625. switch source {
  1626. case utils.DATA_SOURCE_YS:
  1627. tableName = "base_from_smm_index"
  1628. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  1629. tableName = "base_from_mysteel_chemical_index"
  1630. case utils.DATA_SOURCE_SCI:
  1631. tableName = "base_from_sci_index"
  1632. case utils.DATA_SOURCE_BAIINFO:
  1633. tableName = "base_from_baiinfo_index"
  1634. default:
  1635. }
  1636. return
  1637. }
  1638. // EdbInfoWsdAdd 添加指标到指标库
  1639. func EdbInfoWsdAdd(item *data_manage.EdbInfo) (edbInfo *data_manage.EdbInfo, err error, errMsg string, isSendEmail bool) {
  1640. isSendEmail = true
  1641. //判断指标名称是否存在
  1642. var condition string
  1643. var pars []interface{}
  1644. condition += " AND edb_name=? "
  1645. pars = append(pars, item.EdbName)
  1646. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1647. if err != nil {
  1648. errMsg = "判断指标名称是否存在失败"
  1649. err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
  1650. return
  1651. }
  1652. if count > 0 {
  1653. errMsg = "指标名称已存在,请重新填写"
  1654. err = errors.New(errMsg)
  1655. isSendEmail = false
  1656. return
  1657. }
  1658. source := item.Source
  1659. edbInfo = new(data_manage.EdbInfo)
  1660. edbInfo.Source = source
  1661. var sourceName string
  1662. edbSource := data_manage.GetEdbSourceBySourceId(source)
  1663. if edbSource != nil {
  1664. sourceName = edbSource.SourceName
  1665. }
  1666. if sourceName == "" {
  1667. errMsg = "指标来源异常"
  1668. err = errors.New(errMsg)
  1669. return
  1670. }
  1671. edbInfo.SourceName = sourceName
  1672. edbType := 1 //基础指标
  1673. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1674. edbType = 2 //计算指标
  1675. }
  1676. //获取该层级下最大的排序数
  1677. maxSort, err := GetEdbClassifyMaxSort(item.ClassifyId, 0)
  1678. if err != nil {
  1679. errMsg = "获取失败"
  1680. err = errors.New("查询排序信息失败,Err:" + err.Error())
  1681. return
  1682. }
  1683. edbInfo.EdbCode = item.EdbCode
  1684. edbInfo.EdbName = item.EdbName
  1685. edbInfo.EdbNameSource = item.EdbName
  1686. edbInfo.EdbNameEn = item.EdbNameEn
  1687. edbInfo.Frequency = item.Frequency
  1688. edbInfo.Unit = item.Unit
  1689. edbInfo.UnitEn = item.UnitEn
  1690. edbInfo.ClassifyId = item.ClassifyId
  1691. edbInfo.SysUserId = item.SysUserId
  1692. edbInfo.SysUserRealName = item.SysUserRealName
  1693. edbInfo.CreateTime = time.Now()
  1694. edbInfo.ModifyTime = time.Now()
  1695. edbInfo.ServerUrl = item.ServerUrl
  1696. edbInfo.Sort = maxSort + 1
  1697. edbInfo.DataDateType = `交易日`
  1698. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1699. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  1700. edbInfo.EdbType = edbType
  1701. edbInfo.SubSource = item.SubSource
  1702. edbInfo.SubSourceName = "日期序列"
  1703. edbInfo.IndicatorCode = item.IndicatorCode
  1704. edbInfo.StockCode = item.StockCode
  1705. edbInfo.TerminalCode = item.TerminalCode
  1706. edbInfo.Extra = item.Extra
  1707. edbInfoId, err := data_manage.AddEdbInfo(edbInfo)
  1708. if err != nil {
  1709. errMsg = "保存失败"
  1710. err = errors.New("保存失败,Err:" + err.Error())
  1711. return
  1712. }
  1713. edbInfo.EdbInfoId = int(edbInfoId)
  1714. //保存数据
  1715. err = data_manage.ModifyEdbInfoWindWsdDataStatus(source, item.SubSource, edbInfoId, item.EdbCode)
  1716. if err != nil {
  1717. errMsg = "保存失败"
  1718. err = errors.New("修改数据对应指标ID失败,Err:" + err.Error())
  1719. return
  1720. }
  1721. maxAndMinItem, _ := data_manage.GetEdbInfoWsdMaxAndMinInfo(source, item.SubSource, item.EdbCode)
  1722. if maxAndMinItem != nil {
  1723. err = data_manage.ModifyEdbInfoMaxAndMinInfo(int(edbInfoId), maxAndMinItem)
  1724. }
  1725. //添加es
  1726. AddOrEditEdbInfoToEs(int(edbInfoId))
  1727. return
  1728. }
  1729. // BatchRefreshEdbByEdbIds 批量刷新指标
  1730. func BatchRefreshEdbByEdbIds(edbIdList []int, redisKey string, refreshKeys []string) (syncing bool, err error) {
  1731. if len(edbIdList) <= 0 {
  1732. return
  1733. }
  1734. // 设置刷新缓存
  1735. if redisKey != `` {
  1736. // 设置最多10分钟缓存
  1737. utils.Rc.SetNX(redisKey, 1, time.Minute*10)
  1738. }
  1739. // 获取需要刷新的指标列表
  1740. newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbIdList)
  1741. // 需要刷新的指标数量
  1742. totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
  1743. // 关联指标过多的时候, 异步刷新
  1744. if totalEdbInfo > 20 {
  1745. syncing = true
  1746. // 加入队列刷新
  1747. var refreshItem EdbQueueRefreshReq
  1748. refreshItem.RefreshKey = redisKey
  1749. refreshItem.ItemRefreshKeys = refreshKeys
  1750. refreshItem.BaseEdbInfoArr = newBaseEdbInfoArr
  1751. refreshItem.BasePredictEdbInfoArr = newBasePredictEdbInfoArr
  1752. refreshItem.CalculateMap = newCalculateMap
  1753. refreshItem.PredictCalculateMap = newPredictCalculateMap
  1754. refreshItem.CalculateArr = calculateArr
  1755. refreshItem.PredictCalculateArr = predictCalculateArr
  1756. go PushEdb2Refresh(refreshItem)
  1757. return
  1758. }
  1759. // 同步刷新
  1760. err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
  1761. if redisKey != `` {
  1762. _ = utils.Rc.Delete(redisKey)
  1763. }
  1764. if len(refreshKeys) > 0 {
  1765. for _, v := range refreshKeys {
  1766. _ = utils.Rc.Delete(v)
  1767. }
  1768. }
  1769. return
  1770. }
  1771. // GetEdbTerminal 获取终端信息
  1772. func GetEdbTerminal(source int, oldTerminalCode string) (edbTerminal *data_manage.EdbTerminal, err error) {
  1773. if source == utils.DATA_SOURCE_WIND && oldTerminalCode == "" {
  1774. tmpConfig := new(data_manage.EdbConfig)
  1775. tmpConfig, err = data_manage.GetConfigDetailByCode("wind_terminal_code")
  1776. if err != nil && !utils.IsErrNoRow(err) {
  1777. err = errors.New("获取配置的wind地址失败,err:" + err.Error())
  1778. return
  1779. }
  1780. err = nil
  1781. if tmpConfig != nil {
  1782. oldTerminalCode = tmpConfig.ConfigValue
  1783. //return
  1784. }
  1785. }
  1786. if oldTerminalCode != "" {
  1787. edbTerminal, err = data_manage.GetEdbTerminalByCode(oldTerminalCode)
  1788. if err != nil {
  1789. return
  1790. }
  1791. return
  1792. }
  1793. // 配置中没有的话,那么就从分组获取,
  1794. list, err := data_manage.GetEdbCountGroupByTerminal(source)
  1795. if err != nil {
  1796. return
  1797. }
  1798. //windUrlNum := 0
  1799. //获取对应的配置url
  1800. terminalNumMap := make(map[string]int)
  1801. for _, v := range list {
  1802. terminalNumMap[v.TerminalCode] = v.Total
  1803. }
  1804. terminalList, err := data_manage.GetEdbTerminalListBySource(source)
  1805. if err != nil {
  1806. return
  1807. }
  1808. if len(terminalList) == 0 {
  1809. err = errors.New("终端地址未配置")
  1810. return
  1811. }
  1812. num := 0
  1813. for _, v := range terminalList {
  1814. tmpNum := terminalNumMap[v.TerminalCode]
  1815. if edbTerminal == nil {
  1816. edbTerminal = v
  1817. num = tmpNum
  1818. } else if tmpNum < num {
  1819. edbTerminal = v
  1820. num = tmpNum
  1821. }
  1822. }
  1823. if edbTerminal == nil {
  1824. err = errors.New("获取配置的终端地址失败")
  1825. return
  1826. }
  1827. return
  1828. }
  1829. func GetEdbTerminalCodeBySource(source int, edbCode, stockCode string) (terminalCode, serverUrl, sourceIndexName string, err error) {
  1830. var e error
  1831. if stockCode != "" {
  1832. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + stockCode
  1833. terminalCode, e = utils.Rc.RedisString(terminalCodeCacheKey)
  1834. if e != nil {
  1835. utils.FileLog.Info(fmt.Sprintf("从缓存中获取数据源终端信息失败, Err: %s", e))
  1836. }
  1837. terminalInfo, e := GetEdbTerminal(source, terminalCode)
  1838. if e != nil {
  1839. err = errors.New("获取可以使用的终端失败,Err:" + e.Error())
  1840. return
  1841. }
  1842. serverUrl = terminalInfo.ServerUrl
  1843. terminalCode = terminalInfo.TerminalCode
  1844. return
  1845. }
  1846. // 获取终端信息
  1847. terminalCodeCacheKey := utils.CACHE_EDB_TERMINAL_CODE_URL + edbCode
  1848. terminalCode, tE := utils.Rc.RedisString(terminalCodeCacheKey)
  1849. if tE != nil {
  1850. utils.FileLog.Info(fmt.Sprintf("从缓存中获取数据源终端信息失败, Err: %s", tE))
  1851. }
  1852. var terminalCodeOrigin string
  1853. terminalCodeOrigin, sourceIndexName, tE = GetTerminalFromBaseIndex(source, edbCode)
  1854. if tE != nil {
  1855. utils.FileLog.Info(fmt.Sprintf("获取数据源终端信息失败, Err: %s", tE))
  1856. }
  1857. if terminalCodeOrigin != "" {
  1858. terminalCode = terminalCodeOrigin
  1859. }
  1860. if source == utils.DATA_SOURCE_WIND || source == utils.DATA_SOURCE_THS {
  1861. terminalInfo, e := GetEdbTerminal(source, terminalCode)
  1862. if e != nil {
  1863. err = errors.New("获取可以使用的终端失败,Err:" + e.Error())
  1864. return
  1865. }
  1866. serverUrl = terminalInfo.ServerUrl
  1867. terminalCode = terminalInfo.TerminalCode
  1868. }
  1869. return
  1870. }
  1871. // handleByAddEdbInfo
  1872. // @Description: 添加指标后的处理操作
  1873. // @author: Roc
  1874. // @datetime 2024-07-22 13:06:36
  1875. // @param edbInfo *data_manage.EdbInfo
  1876. func handleByAddEdbInfo(edbInfo *data_manage.EdbInfo) {
  1877. // 如果是手工数据,那么需要标记手工数据为已加入指标库
  1878. if edbInfo.Source == utils.DATA_SOURCE_MANUAL {
  1879. _ = models.UpdateManualIsJoinEdbStatus(edbInfo.EdbCode, 1)
  1880. }
  1881. }
  1882. // handleByDelEdbInfo
  1883. // @Description: 删除指标后的处理操作
  1884. // @author: Roc
  1885. // @datetime 2024-07-22 13:06:36
  1886. // @param edbInfo *data_manage.EdbInfo
  1887. func handleByDelEdbInfo(edbInfo *data_manage.EdbInfo) {
  1888. // 如果删除的指标是自定义分析的来源,那么还需要删除指标与excel的关系
  1889. if edbInfo.Source == utils.DATA_SOURCE_CALCULATE_ZDYFX {
  1890. err := excel.DeleteCustomAnalysisExcelEdbMappingByEdbInfoId(edbInfo.EdbInfoId)
  1891. if err != nil {
  1892. utils.FileLog.Error(fmt.Sprintf("删除指标时,需要删除与自定义分析的关系失败,指标ID:%d,Err:%s", edbInfo.EdbInfoId, err.Error()), 3)
  1893. }
  1894. }
  1895. // 如果是手工数据,那么需要标记手工数据为未加入指标库
  1896. if edbInfo.Source == utils.DATA_SOURCE_MANUAL {
  1897. _ = models.UpdateManualIsJoinEdbStatus(edbInfo.EdbCode, 0)
  1898. }
  1899. }
  1900. // GetIsSupplierStop
  1901. // @Description: 获取是否供应商停更
  1902. // @author: Roc
  1903. // @datetime 2024-08-27 10:28:19
  1904. // @param source int
  1905. // @param edbCode string
  1906. // @return isSupplierStop int
  1907. func GetIsSupplierStop(source int, edbCode string) (isSupplierStop int) {
  1908. return
  1909. }
  1910. //IsSupplierStop int `description:"是否供应商停更:1:停更,0:未停更"`