edb_info.go 70 KB

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