12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490 |
- package data
- import (
- "errors"
- "eta/eta_chart_lib/models"
- "eta/eta_chart_lib/models/data_manage"
- "eta/eta_chart_lib/services/alarm_msg"
- "eta/eta_chart_lib/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "math"
- "sort"
- "time"
- )
- // EdbInfoRefreshAllFromBaseBak
- // @author Roc
- // @datetime 2022-09-16 11:04:44
- // @description 全部刷新指标(切换到edb_lib服务),将原有的单个指标刷新,调整为批量多个指标刷新
- // DeprecatedTime 2023-10-23 09:38:19废弃
- // Deprecated
- func EdbInfoRefreshAllFromBaseBak(edbInfoIdList []int, refreshAll bool) (err error, errmsg string) {
- defer func() {
- if err != nil {
- fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
- go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
- }
- }()
- // 获取关联的基础指标
- newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr, err, errmsg := getRefreshEdbInfoListByIds(edbInfoIdList)
- if err != nil {
- return
- }
- var startDate string
- for _, bv := range newBaseEdbInfoArr {
- //source := bv.Source
- //edbInfoId := bv.EdbInfoId
- //edbCode := bv.EdbCode
- if bv.StartDate == "0000-00-00" {
- continue
- }
- //开始时间
- startDate = ``
- if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
- sTime, tmpErr := time.Parse(utils.FormatDate, bv.StartDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = err.Error()
- return
- }
- startDate = sTime.Format(utils.FormatDate)
- } else {
- sTime, tmpErr := time.Parse(utils.FormatDate, bv.EndDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = err.Error()
- return
- }
- frequency := bv.Frequency
- var limitDay int
- switch frequency {
- case "日度":
- limitDay = utils.DATA_REFRESH
- case "周度":
- limitDay = utils.DATA_REFRESH * 7
- case "月度":
- limitDay = utils.DATA_REFRESH * 30
- case "季度":
- limitDay = utils.DATA_REFRESH * 90
- case "年度":
- limitDay = utils.DATA_REFRESH * 365
- default:
- limitDay = utils.DATA_REFRESH
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- }
- result, tmpErr := RefreshEdbData(bv.EdbInfoId, bv.Source, bv.SubSource, bv.EdbCode, startDate)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err", time.Now())
- errmsg = "RefreshBaseEdbData Err:" + tmpErr.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = fmt.Errorf("刷新失败, err:", errmsg)
- return
- }
- //maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
- //if err != nil {
- // if err.Error() == utils.ErrNoRow() { //找不到数据,那么就进入到下一条数据做处理
- // continue
- // }
- // return err
- //}
- //if maxAndMinItem != nil {
- // err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
- // if err != nil {
- // return err
- // }
- //}
- fmt.Println("end newBaseEdbInfoArr:", bv, time.Now())
- }
- //刷新相关普通计算指标
- for _, v := range calculateArr {
- edbInfo := newCalculateMap[v]
- if edbInfo == nil {
- return
- }
- startDate = edbInfo.StartDate
- source := edbInfo.Source
- if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
- startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
- } else {
- if source == utils.DATA_SOURCE_CALCULATE {
- startDate = ``
- if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
- startDate = edbInfo.StartDate
- } else {
- sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = tmpErr.Error()
- return
- }
- frequency := edbInfo.Frequency
- var limitDay int
- switch frequency {
- case "日度":
- limitDay = utils.DATA_REFRESH
- case "周度":
- limitDay = utils.DATA_REFRESH * 7
- case "月度":
- limitDay = utils.DATA_REFRESH * 30
- case "季度":
- limitDay = utils.DATA_REFRESH * 90
- case "年度":
- limitDay = utils.DATA_REFRESH * 365
- default:
- limitDay = utils.DATA_REFRESH
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- }
- }
- }
- result, tmpErr := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
- errmsg = "RefreshEdbCalculateData Err:" + tmpErr.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = fmt.Errorf("刷新失败")
- return
- }
- }
- //刷新相关预测计算指标
- for _, v := range predictCalculateArr {
- edbInfo := newPredictCalculateMap[v]
- if edbInfo == nil {
- return
- }
- startDate = edbInfo.StartDate
- source := edbInfo.Source
- if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
- startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
- } else {
- if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
- startDate = ``
- if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
- startDate = edbInfo.StartDate
- } else {
- sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = tmpErr.Error()
- return
- }
- frequency := edbInfo.Frequency
- var limitDay int
- switch frequency {
- case "日度":
- limitDay = utils.DATA_REFRESH
- case "周度":
- limitDay = utils.DATA_REFRESH * 7
- case "月度":
- limitDay = utils.DATA_REFRESH * 30
- case "季度":
- limitDay = utils.DATA_REFRESH * 90
- case "年度":
- limitDay = utils.DATA_REFRESH * 365
- default:
- limitDay = utils.DATA_REFRESH
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- }
- }
- }
- result, tmpErr := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(v, "RefreshPredictEdbCalculateData err", time.Now())
- errmsg = "RefreshPredictEdbCalculateData Err:" + tmpErr.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = fmt.Errorf("刷新失败")
- return
- }
- }
- // 4、更新动态环差值
- {
- predictEdbInfoIdList := make([]int, 0)
- for _, v := range newBasePredictEdbInfoArr {
- predictEdbInfoIdList = append(predictEdbInfoIdList, v.EdbInfoId)
- }
- fmt.Println("predictEdbConfCalculateMappingDetailList:", predictEdbInfoIdList)
- predictEdbConfCalculateMappingDetailList, tmpErr := data_manage.GetPredictEdbConfCalculateMappingDetailListByEdbInfoId(predictEdbInfoIdList)
- if tmpErr != nil {
- err = tmpErr
- errmsg = fmt.Sprint("更新动态环差值时,获取数据失败:" + err.Error())
- return
- }
- for _, bv := range predictEdbConfCalculateMappingDetailList {
- result, tmpErr := RefreshEdbData(bv.EdbInfoId, utils.DATA_SOURCE_PREDICT, utils.DATA_SUB_SOURCE_EDB, bv.EdbCode, "")
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err", time.Now())
- errmsg = "RefreshBasePredictEdbData Err:" + err.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = errors.New("刷新失败:" + errmsg)
- return
- }
- fmt.Println("end predictEdbConfCalculateMappingDetailList:", bv, time.Now())
- }
- }
- //5、刷新相关预测计算指标
- for _, v := range predictCalculateArr {
- edbInfo := newPredictCalculateMap[v]
- if edbInfo == nil {
- return
- }
- startDate = edbInfo.StartDate
- source := edbInfo.Source
- if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
- startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
- } else {
- if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
- startDate = ``
- if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
- startDate = edbInfo.StartDate
- } else {
- sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = tmpErr.Error()
- return
- }
- frequency := edbInfo.Frequency
- var limitDay int
- switch frequency {
- case "日度":
- limitDay = utils.DATA_REFRESH
- case "周度":
- limitDay = utils.DATA_REFRESH * 7
- case "月度":
- limitDay = utils.DATA_REFRESH * 30
- case "季度":
- limitDay = utils.DATA_REFRESH * 90
- case "年度":
- limitDay = utils.DATA_REFRESH * 365
- default:
- limitDay = utils.DATA_REFRESH
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- }
- }
- }
- result, tmpErr := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(v, "RefreshPredictEdbCalculateData err", time.Now())
- errmsg = "RefreshPredictEdbCalculateData Err:" + tmpErr.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = fmt.Errorf("刷新失败")
- return
- }
- }
- return
- }
- // getRefreshEdbInfoList 获取待更新的指标(普通基础指标、普通运算指标,预测运算指标)
- func getRefreshEdbInfoListByIds(edbInfoIdList []int) (newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int, err error, errMsg string) {
- calculateList, err := data_manage.GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList)
- if err != nil && err.Error() != utils.ErrNoRow() {
- err = errors.New("GetEdbInfoAllCalculate Err:" + err.Error())
- return
- }
- // 获取指标信息
- edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
- if err != nil {
- err = errors.New("GetEdbInfoAllCalGetEdbInfoByIdr:" + err.Error())
- return
- }
- baseEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 基础普通指标
- calculateInfoArr := make([]*data_manage.EdbInfo, 0) //基础运算指标
- basePredictEdbInfoArr := make([]*data_manage.EdbInfo, 0) // 预测普通指标
- predictCalculateInfoArr := make([]*data_manage.EdbInfo, 0) //预测运算指标
- for _, baseEdbInfo := range edbInfoList {
- if baseEdbInfo.EdbInfoType == 0 { //普通指标
- switch baseEdbInfo.EdbType {
- case 1: //基础指标
- baseEdbInfoArr = append(baseEdbInfoArr, baseEdbInfo)
- case 2: //计算指标
- calculateList = append(calculateList, baseEdbInfo)
- }
- } else { //预测指标
- switch baseEdbInfo.EdbType {
- case 1: //基础指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, baseEdbInfo)
- case 2: //计算指标
- predictCalculateInfoArr = append(predictCalculateInfoArr, baseEdbInfo)
- }
- }
- }
- // 计算指标
- for _, v := range calculateList {
- getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
- errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- for _, tmpEdbInfo := range getBaseEdbInfoArr {
- switch tmpEdbInfo.EdbInfoType {
- case 0: //普通基础指标
- baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
- case 1: //预测基础指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
- }
- }
- for _, tmpEdbInfo := range getCalculateInfoArr {
- switch tmpEdbInfo.EdbInfoType {
- case 0: //普通运算指标
- calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
- case 1: //预测运算指标
- //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
- if tmpEdbInfo.EdbType == 1 { //预测普通指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
- } else {
- predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
- }
- }
- }
- switch v.EdbInfoType {
- case 0: //普通运算指标
- calculateInfoArr = append(calculateInfoArr, v)
- case 1: //预测运算指标
- if v.EdbType == 1 { //预测普通指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
- } else {
- predictCalculateInfoArr = append(predictCalculateInfoArr, v)
- }
- }
- }
- // 预测计算指标
- for _, v := range predictCalculateInfoArr {
- getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
- errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- for _, tmpEdbInfo := range getBaseEdbInfoArr {
- switch tmpEdbInfo.EdbInfoType {
- case 0: //普通基础指标
- baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
- case 1: //预测基础指标
- if tmpEdbInfo.EdbType == 1 { //预测普通指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
- } else {
- predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
- }
- //basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
- }
- }
- for _, tmpEdbInfo := range getCalculateInfoArr {
- switch tmpEdbInfo.EdbInfoType {
- case 0: //普通运算指标
- calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
- case 1: //预测运算指标
- if tmpEdbInfo.EdbType == 1 { //预测普通指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
- } else {
- predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
- }
- //predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
- }
- }
- switch v.EdbInfoType {
- case 0: //普通运算指标
- calculateInfoArr = append(calculateInfoArr, v)
- case 1: //预测运算指标
- if v.EdbType == 1 { //预测普通指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
- } else {
- predictCalculateInfoArr = append(predictCalculateInfoArr, v)
- }
- }
- }
- // 第一次的计算指标map
- newCalculateMap = make(map[int]*data_manage.EdbInfo)
- for _, v := range calculateInfoArr {
- if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
- calculateArr = append(calculateArr, v.EdbInfoId)
- }
- newCalculateMap[v.EdbInfoId] = v
- }
- // 基础预测指标
- if len(basePredictEdbInfoArr) > 0 {
- basePredictEdbInfoIdList := make([]int, 0)
- for _, v := range basePredictEdbInfoArr {
- basePredictEdbInfoIdList = append(basePredictEdbInfoIdList, v.EdbInfoId)
- }
- baseEdbInfoList, tmpErr := data_manage.GetPredictEdbInfoAllCalculate(basePredictEdbInfoIdList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpCalculateList := make([]*data_manage.EdbInfo, 0)
- for _, v := range baseEdbInfoList {
- switch v.EdbType {
- case 1:
- baseEdbInfoArr = append(baseEdbInfoArr, v)
- case 2:
- // 如果不在第一次里面,那么在第二次 刷新预测指标的时候 需要加进去
- if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
- tmpCalculateList = append(tmpCalculateList, v)
- }
- }
- }
- // 这里是查询预测指标用到的 普通计算指标
- for _, v := range tmpCalculateList {
- getBaseEdbInfoArr, getCalculateInfoArr, tmpErr := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println("GetRefreshEdbInfoFromBase err:" + err.Error())
- errMsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- // 基础指标
- for _, tmpEdbInfo := range getBaseEdbInfoArr {
- switch tmpEdbInfo.EdbInfoType {
- case 0: //普通基础指标
- baseEdbInfoArr = append(baseEdbInfoArr, tmpEdbInfo)
- case 1: //预测基础指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
- }
- }
- // 计算指标
- for _, tmpEdbInfo := range getCalculateInfoArr {
- switch tmpEdbInfo.EdbInfoType {
- case 0: //普通运算指标
- calculateInfoArr = append(calculateInfoArr, tmpEdbInfo)
- case 1: //预测运算指标
- if tmpEdbInfo.EdbType == 1 { //预测普通指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, tmpEdbInfo)
- } else {
- predictCalculateInfoArr = append(predictCalculateInfoArr, tmpEdbInfo)
- }
- }
- }
- switch v.EdbInfoType {
- case 0: //普通运算指标
- calculateInfoArr = append(calculateInfoArr, v)
- case 1: //预测运算指标
- if v.EdbType == 1 { //预测普通指标
- basePredictEdbInfoArr = append(basePredictEdbInfoArr, v)
- } else {
- predictCalculateInfoArr = append(predictCalculateInfoArr, v)
- }
- }
- }
- // 第二次计算指标的map
- for _, v := range calculateInfoArr {
- if _, ok := newCalculateMap[v.EdbInfoId]; !ok {
- calculateArr = append(calculateArr, v.EdbInfoId)
- }
- newCalculateMap[v.EdbInfoId] = v
- }
- }
- // 普通基础指标去重
- newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
- baseMap := make(map[int]int)
- for _, v := range baseEdbInfoArr {
- if _, ok := baseMap[v.EdbInfoId]; !ok {
- newBaseEdbInfoArr = append(newBaseEdbInfoArr, v)
- }
- baseMap[v.EdbInfoId] = v.EdbInfoId
- }
- // 普通计算指标的id
- sort.Ints(calculateArr)
- // 普通预测指标去重
- newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
- basePredictMap := make(map[int]int)
- for _, v := range basePredictEdbInfoArr {
- if _, ok := basePredictMap[v.EdbInfoId]; !ok {
- newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, v)
- }
- basePredictMap[v.EdbInfoId] = v.EdbInfoId
- }
- // 预测计算指标去重
- newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
- for _, v := range predictCalculateInfoArr {
- if _, ok := newPredictCalculateMap[v.EdbInfoId]; !ok {
- predictCalculateArr = append(predictCalculateArr, v.EdbInfoId)
- }
- newPredictCalculateMap[v.EdbInfoId] = v
- }
- // 预测计算指标的id
- sort.Ints(predictCalculateArr)
- return
- }
- // GetEdbSourceByEdbInfoIdList 获取关联指标的来源
- func GetEdbSourceByEdbInfoIdList(chartEdbInfoMappingList []*models.ChartEdbInfoMapping) (sourceNameList, sourceNameEnList []string) {
- sourceNameList = make([]string, 0)
- sourceNameEnList = make([]string, 0)
- sourceMap := make(map[int]string)
- for _, v := range chartEdbInfoMappingList {
- // 指标类型:1:基础指标,2:计算指标
- if v.EdbType == 2 || v.EdbInfoCategoryType == 1 {
- //sourceMap[0] = "弘则研究"
- baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
- for _, baseEdbInfo := range baseEdbInfoArr {
- if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
- sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
- }
- }
- } else {
- sourceMap[v.Source] = v.SourceName
- }
- }
- for source, sourceName := range sourceMap {
- if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL}, source) {
- continue
- }
- sourceNameList = append(sourceNameList, sourceName)
- sourceNameEn, ok := utils.DataSourceEnMap[source]
- if !ok {
- sourceNameEn = sourceName
- }
- sourceNameEnList = append(sourceNameEnList, sourceNameEn)
- }
- //sourceNameList = append(sourceNameList, utils.ChartDefaultNameCn)
- //sourceNameEnList = append(sourceNameEnList, utils.ChartDefaultNameEn)
- // 图表来源
- conf, e := models.GetBusinessConf()
- if e != nil {
- return
- }
- if conf[models.BusinessConfCompanyName] != "" {
- sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
- sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
- }
- return
- }
- // edbInfoRefreshAll
- //
- // @Description: 指标刷新服务
- // @author: Roc
- // @datetime2023-10-23 11:04:30
- // @param refreshAll bool
- // @param newBaseEdbInfoArr []*data_manage.EdbInfo
- // @param newBasePredictEdbInfoArr []*data_manage.EdbInfo
- // @param newCalculateMap map[int]*data_manage.EdbInfo
- // @param newPredictCalculateMap map[int]*data_manage.EdbInfo
- // @param calculateArr []int
- // @param predictCalculateArr []int
- // @return err error
- // @return errmsg string
- func edbInfoRefreshAll(refreshAll bool, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) (err error, errmsg string) {
- defer func() {
- if err != nil {
- fmt.Println("EdbInfoRefreshAllFromBaseV2 Err:" + err.Error() + ";errmsg:" + errmsg)
- go alarm_msg.SendAlarmMsg("EdbInfoRefreshFromBaseV2,Err"+err.Error()+";errMsg:"+errmsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "EdbInfoRefreshFromBase:"+errmsg, utils.EmailSendToUsers)
- }
- }()
- var startDate string
- for _, bv := range newBaseEdbInfoArr {
- //source := bv.Source
- //edbInfoId := bv.EdbInfoId
- //edbCode := bv.EdbCode
- if bv.StartDate == "0000-00-00" {
- continue
- }
- //开始时间
- startDate = ``
- if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
- sTime, tmpErr := time.Parse(utils.FormatDate, bv.StartDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = err.Error()
- return
- }
- startDate = sTime.Format(utils.FormatDate)
- } else {
- sTime, tmpErr := time.Parse(utils.FormatDate, bv.EndDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = err.Error()
- return
- }
- frequency := bv.Frequency
- var limitDay int
- switch frequency {
- case "日度":
- limitDay = utils.DATA_REFRESH
- case "周度":
- limitDay = utils.DATA_REFRESH * 7
- case "月度":
- limitDay = utils.DATA_REFRESH * 30
- case "季度":
- limitDay = utils.DATA_REFRESH * 90
- case "年度":
- limitDay = utils.DATA_REFRESH * 365
- default:
- limitDay = utils.DATA_REFRESH
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- }
- result, tmpErr := RefreshEdbData(bv.EdbInfoId, bv.Source, bv.SubSource, bv.EdbCode, startDate)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err", time.Now())
- errmsg = "RefreshBaseEdbData Err:" + tmpErr.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBaseEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = fmt.Errorf("刷新失败, err:", errmsg)
- return
- }
- //maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
- //if err != nil {
- // if err.Error() == utils.ErrNoRow() { //找不到数据,那么就进入到下一条数据做处理
- // continue
- // }
- // return err
- //}
- //if maxAndMinItem != nil {
- // err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
- // if err != nil {
- // return err
- // }
- //}
- fmt.Println("end newBaseEdbInfoArr:", bv, time.Now())
- }
- //刷新相关普通计算指标
- for _, v := range calculateArr {
- edbInfo := newCalculateMap[v]
- if edbInfo == nil {
- return
- }
- startDate = edbInfo.StartDate
- source := edbInfo.Source
- if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
- startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
- } else {
- if source == utils.DATA_SOURCE_CALCULATE {
- startDate = ``
- if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
- startDate = edbInfo.StartDate
- } else {
- sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = tmpErr.Error()
- return
- }
- frequency := edbInfo.Frequency
- var limitDay int
- switch frequency {
- case "日度":
- limitDay = utils.DATA_REFRESH
- case "周度":
- limitDay = utils.DATA_REFRESH * 7
- case "月度":
- limitDay = utils.DATA_REFRESH * 30
- case "季度":
- limitDay = utils.DATA_REFRESH * 90
- case "年度":
- limitDay = utils.DATA_REFRESH * 365
- default:
- limitDay = utils.DATA_REFRESH
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- }
- }
- }
- result, tmpErr := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(v, "RefreshEdbCalculateData err", time.Now())
- errmsg = "RefreshEdbCalculateData Err:" + tmpErr.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(v, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = fmt.Errorf("刷新失败")
- return
- }
- }
- //刷新相关预测计算指标
- for _, v := range predictCalculateArr {
- edbInfo := newPredictCalculateMap[v]
- if edbInfo == nil {
- return
- }
- startDate = edbInfo.StartDate
- source := edbInfo.Source
- if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
- startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
- } else {
- if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
- startDate = ``
- if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
- startDate = edbInfo.StartDate
- } else {
- sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = tmpErr.Error()
- return
- }
- frequency := edbInfo.Frequency
- var limitDay int
- switch frequency {
- case "日度":
- limitDay = utils.DATA_REFRESH
- case "周度":
- limitDay = utils.DATA_REFRESH * 7
- case "月度":
- limitDay = utils.DATA_REFRESH * 30
- case "季度":
- limitDay = utils.DATA_REFRESH * 90
- case "年度":
- limitDay = utils.DATA_REFRESH * 365
- default:
- limitDay = utils.DATA_REFRESH
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- }
- }
- }
- result, tmpErr := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(v, "RefreshPredictEdbCalculateData err", time.Now())
- errmsg = "RefreshPredictEdbCalculateData Err:" + tmpErr.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = fmt.Errorf("刷新失败")
- return
- }
- }
- // 4、更新动态环差值
- {
- predictEdbInfoIdList := make([]int, 0)
- for _, v := range newBasePredictEdbInfoArr {
- predictEdbInfoIdList = append(predictEdbInfoIdList, v.EdbInfoId)
- }
- fmt.Println("predictEdbConfCalculateMappingDetailList:", predictEdbInfoIdList)
- predictEdbConfCalculateMappingDetailList, tmpErr := data_manage.GetPredictEdbConfCalculateMappingDetailListByEdbInfoId(predictEdbInfoIdList)
- if tmpErr != nil {
- err = tmpErr
- errmsg = fmt.Sprint("更新动态环差值时,获取数据失败:" + err.Error())
- return
- }
- for _, bv := range predictEdbConfCalculateMappingDetailList {
- result, tmpErr := RefreshEdbData(bv.EdbInfoId, utils.DATA_SOURCE_PREDICT, utils.DATA_SUB_SOURCE_EDB, bv.EdbCode, "")
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err", time.Now())
- errmsg = "RefreshBasePredictEdbData Err:" + err.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(bv.EdbInfoId, "RefreshBasePredictEdbData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = errors.New("刷新失败:" + errmsg)
- return
- }
- fmt.Println("end predictEdbConfCalculateMappingDetailList:", bv, time.Now())
- }
- }
- //5、刷新相关预测计算指标
- for _, v := range predictCalculateArr {
- edbInfo := newPredictCalculateMap[v]
- if edbInfo == nil {
- return
- }
- startDate = edbInfo.StartDate
- source := edbInfo.Source
- if startDate == "" || startDate == "0000-00-00" { //如果没有开始日期,说明还没有计算出来数据,那么就往前面推40年吧(也意味着重新计算了)
- startDate = time.Now().AddDate(-40, 0, 0).Format(utils.FormatDate)
- } else {
- if source == utils.DATA_SOURCE_PREDICT_CALCULATE {
- startDate = ``
- if refreshAll { //刷新所有数据,用开始时间作为起始日期去刷新
- startDate = edbInfo.StartDate
- } else {
- sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
- if tmpErr != nil {
- err = tmpErr
- errmsg = tmpErr.Error()
- return
- }
- frequency := edbInfo.Frequency
- var limitDay int
- switch frequency {
- case "日度":
- limitDay = utils.DATA_REFRESH
- case "周度":
- limitDay = utils.DATA_REFRESH * 7
- case "月度":
- limitDay = utils.DATA_REFRESH * 30
- case "季度":
- limitDay = utils.DATA_REFRESH * 90
- case "年度":
- limitDay = utils.DATA_REFRESH * 365
- default:
- limitDay = utils.DATA_REFRESH
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- }
- }
- }
- result, tmpErr := RefreshPredictEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println(v, "RefreshPredictEdbCalculateData err", time.Now())
- errmsg = "RefreshPredictEdbCalculateData Err:" + tmpErr.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errmsg = fmt.Sprint(v, "RefreshPredictEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = fmt.Errorf("刷新失败")
- return
- }
- }
- return
- }
- // EdbInfoRefreshAllFromBaseV3
- //
- // @Description: 全部刷新指标(切换到edb_lib服务)
- // @author: Roc
- // @datetime2023-10-23 09:57:55
- // @param edbInfoIdList []int
- // @param refreshAll bool
- // @param isSync bool
- // @return err error
- // @return isAsync bool
- func EdbInfoRefreshAllFromBase(edbInfoIdList []int, refreshAll bool) (err error, errMsg string) {
- defer func() {
- if err != nil {
- fmt.Println("EdbInfoRefreshAllFromBaseV3 Err:" + err.Error() + ";errMsg:" + errMsg)
- go alarm_msg.SendAlarmMsg("EdbInfoRefreshAllFromBaseV3,Err"+err.Error()+";errMsg:"+errMsg, 3)
- }
- }()
- traceEdbInfoList, err := TraceEdbInfoByEdbInfoIdList(edbInfoIdList)
- if err != nil {
- return
- }
- // existEdbInfoIdMap 已经处理了的指标id map
- existEdbInfoIdMap := make(map[int]int)
- // 基础指标
- newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
- newBasePredictEdbInfoArr := make([]*data_manage.EdbInfo, 0)
- newBaseMap := make(map[int]*data_manage.EdbInfo)
- newPredictBaseMap := make(map[int]*data_manage.EdbInfo)
- // 计算指标
- newCalculateMap := make(map[int]*data_manage.EdbInfo)
- newPredictCalculateMap := make(map[int]*data_manage.EdbInfo)
- calculateArr := make([]int, 0)
- predictCalculateArr := make([]int, 0)
- // 获取关联指标
- for _, traceEdbInfo := range traceEdbInfoList {
- tmpBaseEdbInfoArr, tmpBasePredictEdbInfoArr, tmpCalculateMap, tmpPredictCalculateMap, _, _ := getRefreshEdbInfoListByTraceEdbInfo(traceEdbInfo, existEdbInfoIdMap)
- // 普通基础指标
- for _, edbInfo := range tmpBaseEdbInfoArr {
- if _, ok := newBaseMap[edbInfo.EdbInfoId]; !ok {
- if edbInfo.NoUpdate == 1 {
- continue
- }
- newBaseMap[edbInfo.EdbInfoId] = edbInfo
- newBaseEdbInfoArr = append(newBaseEdbInfoArr, edbInfo)
- }
- }
- // 预测基础指标
- for _, edbInfo := range tmpBasePredictEdbInfoArr {
- if _, ok := newPredictBaseMap[edbInfo.EdbInfoId]; !ok {
- newPredictBaseMap[edbInfo.EdbInfoId] = edbInfo
- newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, edbInfo)
- }
- }
- // 普通计算指标
- for _, edbInfo := range tmpCalculateMap {
- if _, ok := newCalculateMap[edbInfo.EdbInfoId]; !ok {
- if edbInfo.NoUpdate == 1 {
- continue
- }
- newCalculateMap[edbInfo.EdbInfoId] = edbInfo
- calculateArr = append(calculateArr, edbInfo.EdbInfoId)
- }
- }
- // 预测计算指标
- for _, edbInfo := range tmpPredictCalculateMap {
- if _, ok := newPredictCalculateMap[edbInfo.EdbInfoId]; !ok {
- newPredictCalculateMap[edbInfo.EdbInfoId] = edbInfo
- predictCalculateArr = append(predictCalculateArr, edbInfo.EdbInfoId)
- }
- }
- }
- // 普通计算指标的id
- sort.Ints(calculateArr)
- // 预测计算指标的id
- sort.Ints(predictCalculateArr)
- // 需要刷新的指标数量
- totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr) + len(newBasePredictEdbInfoArr)
- if totalEdbInfo == 0 {
- return
- }
- //if totalEdbInfo <= 20{
- // err,errMsg = edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- //} else {
- // go edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- //}
- err, errMsg = edbInfoRefreshAll(refreshAll, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- return
- }
- // getRefreshEdbInfoListByTraceEdbInfo
- //
- // @Description: 根据溯源获取关联指标(去掉重复指标id)
- // @author: Roc
- // @datetime2023-10-23 11:22:50
- // @param traceEdbInfo data_manage.TraceEdbInfoResp
- // @param existEdbInfoIdMap map[int]int
- // @return newBaseEdbInfoArr []*data_manage.EdbInfo
- // @return newBasePredictEdbInfoArr []*data_manage.EdbInfo
- // @return newCalculateMap map[int]*data_manage.EdbInfo
- // @return newPredictCalculateMap map[int]*data_manage.EdbInfo
- // @return calculateArr []int
- // @return predictCalculateArr []int
- 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) {
- newBaseEdbInfoArr = make([]*data_manage.EdbInfo, 0)
- newBasePredictEdbInfoArr = make([]*data_manage.EdbInfo, 0)
- newCalculateMap = make(map[int]*data_manage.EdbInfo)
- newPredictCalculateMap = make(map[int]*data_manage.EdbInfo)
- calculateArr = make([]int, 0)
- predictCalculateArr = make([]int, 0)
- _, ok := existEdbInfoIdMap[traceEdbInfo.EdbInfoId]
- if ok {
- return
- }
- existEdbInfoIdMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfoId
- switch traceEdbInfo.EdbInfoType {
- //0-普通指标; 1-预测指标
- case 0: // 0-普通指标
- if traceEdbInfo.EdbType == 1 { //1-基础指标
- newBaseEdbInfoArr = append(newBaseEdbInfoArr, traceEdbInfo.EdbInfo)
- } else if traceEdbInfo.EdbType == 2 { //2-计算指标
- newCalculateMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfo
- calculateArr = append(calculateArr, traceEdbInfo.EdbInfoId)
- }
- case 1: // 1-预测指标
- if traceEdbInfo.EdbType == 1 { //1-基础指标
- newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, traceEdbInfo.EdbInfo)
- } else if traceEdbInfo.EdbType == 2 { //2-计算指标
- newPredictCalculateMap[traceEdbInfo.EdbInfoId] = traceEdbInfo.EdbInfo
- predictCalculateArr = append(predictCalculateArr, traceEdbInfo.EdbInfoId)
- }
- }
- if traceEdbInfo.Child != nil && len(traceEdbInfo.Child) > 0 {
- for _, v := range traceEdbInfo.Child {
- tmpBaseEdbInfoArr, tmpPredictEdbInfoArr, tmpCalculateMap, tmpPredictCalculateMap, tmpCalculateArr, tmpPredictCalculateArr := getRefreshEdbInfoListByTraceEdbInfo(v, existEdbInfoIdMap)
- newBaseEdbInfoArr = append(newBaseEdbInfoArr, tmpBaseEdbInfoArr...)
- newBasePredictEdbInfoArr = append(newBasePredictEdbInfoArr, tmpPredictEdbInfoArr...)
- for k, tmpEdbInfo := range tmpCalculateMap {
- newCalculateMap[k] = tmpEdbInfo
- }
- for k, tmpEdbInfo := range tmpPredictCalculateMap {
- newPredictCalculateMap[k] = tmpEdbInfo
- }
- calculateArr = append(calculateArr, tmpCalculateArr...)
- predictCalculateArr = append(predictCalculateArr, tmpPredictCalculateArr...)
- }
- }
- return
- }
- // TraceEdbInfoByEdbInfoIdList
- //
- // @Description: 根据指标id列表返回指标追溯的列表
- // @author: Roc
- // @datetime2023-10-23 11:22:21
- // @param edbInfoIdList []int
- // @return traceEdbInfoList []data_manage.TraceEdbInfoResp
- // @return err error
- func TraceEdbInfoByEdbInfoIdList(edbInfoIdList []int) (traceEdbInfoList []data_manage.TraceEdbInfoResp, err error) {
- traceEdbInfoList = make([]data_manage.TraceEdbInfoResp, 0)
- edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
- if err != nil {
- return
- }
- edbInfoRuleMap := make(map[int]string, 0)
- edbMappingMap := make(map[int][]*data_manage.EdbInfoCalculateMappingInfo)
- findIdMap := make(map[int]int)
- existMap := make(map[int]data_manage.TraceEdbInfoResp)
- for _, edbInfo := range edbInfoList {
- findIdMap[edbInfo.EdbInfoId] = edbInfo.EdbInfoId
- //edbInfoRuleMap[edbInfoId] = getEdbRuleTitle(edbInfo)
- traceEdbInfo := data_manage.TraceEdbInfoResp{
- //EdbInfoId: edbInfo.EdbInfoId,
- EdbInfoId: edbInfo.EdbInfoId,
- EdbInfoType: edbInfo.EdbInfoType,
- EdbName: edbInfo.EdbName,
- EdbType: edbInfo.EdbType,
- //Source: edbInfo.Source,
- UniqueCode: edbInfo.UniqueCode,
- ClassifyId: edbInfo.ClassifyId,
- EdbInfo: edbInfo,
- }
- traceEdbInfo.Child, err = traceEdbInfoByEdbInfoId(edbInfo.EdbInfoId, traceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
- traceEdbInfoList = append(traceEdbInfoList, traceEdbInfo)
- }
- //findEdbInfoIdList := make([]int, 0)
- //for _, v := range findIdMap {
- // findEdbInfoIdList = append(findEdbInfoIdList, v)
- //}
- //findEdbInfoList, err := data_manage.GetEdbInfoByIdList(findEdbInfoIdList)
- //if err != nil {
- // return
- //}
- //edbInfoMap := make(map[int]*data_manage.EdbInfo)
- //for _, tmpEdbInfo := range findEdbInfoList {
- // edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo
- //}
- //for k, traceEdbInfo := range traceEdbInfoList {
- // traceEdbInfoList[k], err = handleTraceEdbInfo(traceEdbInfo, 0, edbInfoMap, edbMappingMap)
- //}
- return
- }
- // traceEdbInfoByEdbInfoId
- //
- // @Description: 指标追溯
- // @author: Roc
- // @datetime2023-10-23 11:21:54
- // @param edbInfoId int
- // @param traceEdbInfo data_manage.TraceEdbInfoResp
- // @param edbInfoRuleMap map[int]string
- // @param findIdMap map[int]int
- // @param existMap map[int]data_manage.TraceEdbInfoResp
- // @param edbMappingMap map[int][]*data_manage.EdbInfoCalculateMappingInfo
- // @return child []data_manage.TraceEdbInfoResp
- // @return err error
- 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) {
- traceEdbInfo, ok := existMap[edbInfoId]
- if ok {
- return
- }
- child = make([]data_manage.TraceEdbInfoResp, 0)
- edbInfoMappingList, e := data_manage.GetEdbInfoCalculateMappingListByEdbInfoId(edbInfoId)
- if e != nil {
- err = fmt.Errorf("GetEdbInfoCalculateMappingListByEdbInfoId err: %s", e.Error())
- return
- }
- // 指标信息map
- edbInfoMap := make(map[int]*data_manage.EdbInfo)
- if len(edbInfoMappingList) > 0 {
- fromEdbInfoIdList := make([]int, 0)
- for _, v := range edbInfoMappingList {
- fromEdbInfoIdList = append(fromEdbInfoIdList, v.FromEdbInfoId)
- }
- edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(fromEdbInfoIdList)
- if tmpErr != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId GetEdbInfoByIdList err: %s", tmpErr.Error())
- return
- }
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- }
- }
- edbMappingMap[edbInfoId] = edbInfoMappingList
- for _, v := range edbInfoMappingList {
- tmpEdbInfoId := v.FromEdbInfoId
- tmpTraceEdbInfo := data_manage.TraceEdbInfoResp{
- EdbInfoId: tmpEdbInfoId,
- EdbInfoType: v.FromEdbInfoType,
- EdbType: v.FromEdbType,
- UniqueCode: v.FromUniqueCode,
- ClassifyId: v.FromClassifyId,
- EdbInfo: edbInfoMap[v.FromEdbInfoId],
- }
- // 计算指标/预测指标继续溯源
- if edbInfoId != v.FromEdbInfoId && (v.FromEdbType == 2 || v.FromEdbInfoType == 1) {
- // 查过了就不查了
- if _, ok2 := findIdMap[tmpEdbInfoId]; !ok2 {
- tmpTraceEdbInfo.Child, e = traceEdbInfoByEdbInfoId(tmpEdbInfoId, tmpTraceEdbInfo, edbInfoRuleMap, findIdMap, existMap, edbMappingMap)
- if e != nil {
- err = fmt.Errorf("traceEdbInfoByEdbInfoId err: %s", e.Error())
- return
- }
- }
- }
- child = append(child, tmpTraceEdbInfo)
- findIdMap[tmpEdbInfoId] = tmpEdbInfoId
- }
- existMap[edbInfoId] = traceEdbInfo
- return
- }
- // GetEdbSourceByEdbInfoIdListForExcel 获取关联指标的来源
- func GetEdbSourceByEdbInfoIdListForExcel(edbInfoIdList []int) (sourceNameList, sourceNameEnList []string, err error) {
- sourceNameList = make([]string, 0)
- sourceNameEnList = make([]string, 0)
- sourceMap := make(map[int]string)
- edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for _, v := range edbInfoList {
- // 指标类型:1:基础指标,2:计算指标
- if v.EdbType == 2 {
- //sourceMap[0] = "弘则研究"
- baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
- for _, baseEdbInfo := range baseEdbInfoArr {
- if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
- sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
- }
- }
- } else {
- sourceMap[v.Source] = v.SourceName
- }
- }
- for source, sourceName := range sourceMap {
- if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL, utils.DATA_SOURCE_MYSTEEL_CHEMICAL}, source) {
- continue
- }
- sourceNameList = append(sourceNameList, sourceName)
- sourceNameEn, ok := utils.DataSourceEnMap[source]
- if !ok {
- sourceNameEn = sourceName
- }
- sourceNameEnList = append(sourceNameEnList, sourceNameEn)
- }
- //sourceNameList = append(sourceNameList, utils.ChartDefaultNameCn)
- //sourceNameEnList = append(sourceNameEnList, utils.ChartDefaultNameEn)
- // 图表来源
- conf, e := models.GetBusinessConf()
- if e != nil {
- return
- }
- if conf[models.BusinessConfCompanyName] != "" {
- sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
- sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
- }
- return
- }
- // HandleDataByLinearRegressionToList 插值法补充数据(线性方程式)
- func HandleDataByLinearRegressionToList (edbInfoDataList []*models.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string,valueList []float64, err error) {
- if len(edbInfoDataList) < 2 {
- return
- }
- var startEdbInfoData *models.EdbDataList
- for _, v := range edbInfoDataList {
- handleDataMap[v.DataTime] = v.Value
- dataTimeList = append(dataTimeList, v.DataTime)
- // 第一个数据就给过滤了,给后面的试用
- if startEdbInfoData == nil {
- startEdbInfoData = v
- //startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
- continue
- }
- // 获取两条数据之间相差的天数
- startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
- currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- betweenHour := int(currDataTime.Sub(startDataTime).Hours())
- betweenDay := betweenHour / 24
- // 如果相差一天,那么过滤
- if betweenDay <= 1 {
- startEdbInfoData = v
- continue
- }
- // 生成线性方程式
- var a, b float64
- {
- coordinateData := make([]utils.Coordinate, 0)
- tmpCoordinate1 := utils.Coordinate{
- X: 1,
- Y: startEdbInfoData.Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate1)
- tmpCoordinate2 := utils.Coordinate{
- X: float64(betweenDay) + 1,
- Y: v.Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate2)
- a, b = utils.GetLinearResult(coordinateData)
- if math.IsNaN(a) || math.IsNaN(b) {
- err = errors.New("线性方程公式生成失败")
- return
- }
- }
- // 生成对应的值
- {
- for i := 1; i < betweenDay; i++ {
- tmpDataTime := startDataTime.AddDate(0, 0, i)
- aDecimal := decimal.NewFromFloat(a)
- xDecimal := decimal.NewFromInt(int64(i) + 1)
- bDecimal := decimal.NewFromFloat(b)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
- handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
- dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
- valueList = append(valueList, val)
- }
- }
- startEdbInfoData = v
- }
- return
- }
- // HandleDataByLinearRegressionToList 保证生成365个数据点的线性插值法
- func HandleDataByLinearRegressionToListV2(edbInfoDataList []*models.EdbDataList, handleDataMap map[string]float64) (dataTimeList []string, valueList []float64, err error) {
- if len(edbInfoDataList) < 2 {
- return
- }
- // 确保至少有两天数据来生成线性方程
- if len(edbInfoDataList) < 2 {
- err = errors.New("至少需要两天的数据来执行线性插值")
- return
- }
- // 对数据按日期排序,确保顺序正确
- sort.Slice(edbInfoDataList, func(i, j int) bool {
- t1, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[i].DataTime, time.Local)
- t2, _ := time.ParseInLocation(utils.FormatDate, edbInfoDataList[j].DataTime, time.Local)
- return t1.Before(t2)
- })
- startEdbInfoData := edbInfoDataList[0]
- endEdbInfoData := edbInfoDataList[len(edbInfoDataList)-1]
- // 计算起始和结束日期间实际的天数
- startDate, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
- endDate, _ := time.ParseInLocation(utils.FormatDate, endEdbInfoData.DataTime, time.Local)
- actualDays := endDate.Sub(startDate).Hours() / 24
- // 生成365个数据点,首先处理已有数据
- for _, v := range edbInfoDataList {
- handleDataMap[v.DataTime] = v.Value
- dataTimeList = append(dataTimeList, v.DataTime)
- valueList = append(valueList, v.Value)
- }
- // 如果已有数据跨越天数不足365天,则对缺失的日期进行线性插值
- if actualDays < 365 {
- // 使用已有数据点生成线性方程(这里简化处理,实际可能需更细致处理边界情况)
- var a, b float64
- coordinateData := []utils.Coordinate{
- {X: 1, Y: startEdbInfoData.Value},
- {X: float64(len(edbInfoDataList)), Y: endEdbInfoData.Value},
- }
- a, b = utils.GetLinearResult(coordinateData)
- if math.IsNaN(a) || math.IsNaN(b) {
- err = errors.New("线性方程公式生成失败")
- return
- }
- // 对剩余日期进行插值
- for i := 1; i < 365; i++ {
- day := startDate.AddDate(0, 0, i)
- if _, exists := handleDataMap[day.Format(utils.FormatDate)]; !exists {
- aDecimal := decimal.NewFromFloat(a)
- xDecimal := decimal.NewFromInt(int64(i) + 1)
- bDecimal := decimal.NewFromFloat(b)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
- handleDataMap[day.Format(utils.FormatDate)] = val
- dataTimeList = append(dataTimeList, day.Format(utils.FormatDate))
- valueList = append(valueList, val)
- }
- }
- }
- return
- }
- // HandleDataByLinearRegressionToListV3 插值法补充数据(线性方程式)-直接补充指标起始日期间的所有数据
- func HandleDataByLinearRegressionToListV3 (edbInfoDataList []*models.EdbDataList, handleDataMap map[string]float64) (newEdbInfoDataList []*models.EdbDataList, dataTimeList []string,valueList []float64, err error) {
- if len(edbInfoDataList) < 2 {
- return
- }
- var startEdbInfoData *models.EdbDataList
- for _, v := range edbInfoDataList {
- handleDataMap[v.DataTime] = v.Value
- newEdbInfoDataList = append(newEdbInfoDataList, v)
- dataTimeList = append(dataTimeList, v.DataTime)
- // 第一个数据就给过滤了,给后面的试用
- if startEdbInfoData == nil {
- startEdbInfoData = v
- //startEdbInfoData.DataTime = startEdbInfoData.DataTime[:5]+ "01-01"
- continue
- }
- // 获取两条数据之间相差的天数
- startDataTime, _ := time.ParseInLocation(utils.FormatDate, startEdbInfoData.DataTime, time.Local)
- currDataTime, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- betweenHour := int(currDataTime.Sub(startDataTime).Hours())
- betweenDay := betweenHour / 24
- // 如果相差一天,那么过滤
- if betweenDay <= 1 {
- startEdbInfoData = v
- continue
- }
- // 生成线性方程式
- var a, b float64
- {
- coordinateData := make([]utils.Coordinate, 0)
- tmpCoordinate1 := utils.Coordinate{
- X: 1,
- Y: startEdbInfoData.Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate1)
- tmpCoordinate2 := utils.Coordinate{
- X: float64(betweenDay) + 1,
- Y: v.Value,
- }
- coordinateData = append(coordinateData, tmpCoordinate2)
- a, b = utils.GetLinearResult(coordinateData)
- if math.IsNaN(a) || math.IsNaN(b) {
- err = errors.New("线性方程公式生成失败")
- return
- }
- }
- // 生成对应的值
- {
- for i := 1; i < betweenDay; i++ {
- tmpDataTime := startDataTime.AddDate(0, 0, i)
- aDecimal := decimal.NewFromFloat(a)
- xDecimal := decimal.NewFromInt(int64(i) + 1)
- bDecimal := decimal.NewFromFloat(b)
- val, _ := aDecimal.Mul(xDecimal).Add(bDecimal).Round(4).Float64()
- handleDataMap[tmpDataTime.Format(utils.FormatDate)] = val
- dataTimeList = append(dataTimeList, tmpDataTime.Format(utils.FormatDate))
- valueList = append(valueList, val)
- newEdbInfoDataList = append(newEdbInfoDataList, &models.EdbDataList{
- EdbDataId: v.EdbDataId,
- EdbInfoId: v.EdbInfoId,
- DataTime: tmpDataTime.Format(utils.FormatDate),
- DataTimestamp: tmpDataTime.UnixNano() / 1e6,
- Value: val,
- })
- }
- }
- startEdbInfoData = v
- }
- return
- }
|