edb_info.go 63 KB

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