123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488 |
- package data
- import (
- "errors"
- "eta_gn/eta_chart_lib/models"
- "eta_gn/eta_chart_lib/models/data_manage"
- "eta_gn/eta_chart_lib/services/alarm_msg"
- "eta_gn/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
- }
|