123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- package models
- import (
- "encoding/json"
- "errors"
- "eta/eta_index_lib/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/shopspring/decimal"
- "strconv"
- "strings"
- "time"
- )
- type PredictCalculateRangeAnalysis struct {
- }
- func (obj PredictCalculateRangeAnalysis) Add(params BatchSaveCalculateBatchParams) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error, errMsg string) {
- req := params.Req
- edbCode := params.EdbCode
- uniqueCode := params.UniqueCode
- sysUserId := params.SysUserId
- sysUserRealName := params.SysUserRealName
- //req *EdbInfoCalculateBatchSaveReq, edbCode, uniqueCode string, sysUserId int, sysUserRealName string
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("AddCalculateRangeAnalysis,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- if req.EdbInfoId > 0 {
- err = errors.New("无法新增")
- return
- }
- edbInfo = new(EdbInfo)
- edbInfo.Source = obj.GetSource()
- edbInfo.SourceName = obj.GetSourceName()
- edbInfo.EdbCode = edbCode
- edbInfo.EdbName = req.EdbName
- edbInfo.EdbNameSource = req.EdbName
- edbInfo.Frequency = req.Frequency
- edbInfo.Unit = req.Unit
- edbInfo.ClassifyId = req.ClassifyId
- edbInfo.SysUserId = sysUserId
- edbInfo.SysUserRealName = sysUserRealName
- edbInfo.CreateTime = time.Now()
- edbInfo.ModifyTime = time.Now()
- edbInfo.UniqueCode = uniqueCode
- edbInfo.CalculateFormula = req.CalculateFormula
- edbInfo.EdbNameEn = req.EdbName
- edbInfo.UnitEn = req.Unit
- edbInfo.EdbType = obj.GetEdbType()
- edbInfo.EdbInfoType = 1
- newEdbInfoId, tmpErr := to.Insert(edbInfo)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- edbInfo.EdbInfoId = int(newEdbInfoId)
- //关联关系
- fromEdbInfo, e := GetEdbInfoById(req.FromEdbInfoId)
- if e != nil {
- err = fmt.Errorf("获取来源指标失败,Err:%s", e.Error())
- return
- }
- calculateMappingItem := new(EdbInfoCalculateMapping)
- calculateMappingItem.CreateTime = time.Now()
- calculateMappingItem.ModifyTime = time.Now()
- calculateMappingItem.Sort = 1
- calculateMappingItem.EdbCode = edbCode
- calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
- calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
- calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
- calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
- calculateMappingItem.FromSource = fromEdbInfo.Source
- calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
- calculateMappingItem.FromTag = ""
- calculateMappingItem.Source = edbInfo.Source
- calculateMappingItem.SourceName = edbInfo.SourceName
- calculateMappingItem.FromSubSource = edbInfo.SubSource
- _, err = to.Insert(calculateMappingItem)
- if err != nil {
- return
- }
- //计算数据
- latestDateStr, latestValue, err, errMsg = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, edbInfo.EdbCode, edbInfo.CalculateFormula)
- return
- }
- func (obj PredictCalculateRangeAnalysis) Edit(params BatchSaveCalculateBatchParams) (latestDateStr string, latestValue float64, err error, errMsg string) {
- edbInfo := params.EdbInfo
- req := params.Req
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("EditCalculateRangeAnalysis,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- //修改指标信息
- edbInfo.EdbName = req.EdbName
- edbInfo.EdbNameSource = req.EdbName
- edbInfo.Frequency = req.Frequency
- edbInfo.Unit = req.Unit
- edbInfo.ClassifyId = req.ClassifyId
- edbInfo.CalculateFormula = req.CalculateFormula
- //修改指标信息
- switch params.Lang {
- case utils.EnLangVersion:
- edbInfo.EdbNameEn = req.EdbName
- edbInfo.UnitEn = req.Unit
- default:
- edbInfo.EdbName = req.EdbName
- edbInfo.Unit = req.Unit
- edbInfo.EdbNameSource = req.EdbName
- }
- edbInfo.ModifyTime = time.Now()
- _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime", "EdbNameEn", "UnitEn")
- if err != nil {
- return
- }
- //删除,计算指标关联的,基础指标的关联关系
- sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
- return
- }
- //清空原有数据
- tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
- sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- fromEdbInfo, e := GetEdbInfoById(req.FromEdbInfoId)
- if e != nil {
- err = fmt.Errorf("获取来源指标失败,Err:%s", e.Error())
- return
- }
- calculateMappingItem := new(EdbInfoCalculateMapping)
- calculateMappingItem.CreateTime = time.Now()
- calculateMappingItem.ModifyTime = time.Now()
- calculateMappingItem.Sort = 1
- calculateMappingItem.EdbCode = edbInfo.EdbCode
- calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
- calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
- calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
- calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
- calculateMappingItem.FromSource = fromEdbInfo.Source
- calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
- calculateMappingItem.FromTag = ""
- calculateMappingItem.Source = edbInfo.Source
- calculateMappingItem.SourceName = edbInfo.SourceName
- calculateMappingItem.FromSubSource = edbInfo.SubSource
- _, err = to.Insert(calculateMappingItem)
- if err != nil {
- return
- }
- //计算数据
- latestDateStr, latestValue, err, errMsg = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, edbInfo.EdbCode, edbInfo.CalculateFormula)
- return
- }
- func (obj PredictCalculateRangeAnalysis) Refresh(params RefreshParams) (latestDateStr string, latestValue float64, err error, errMsg string) {
- edbInfo := params.EdbInfo
- edbInfoCalculateDetailList, err := GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
- if err != nil {
- return
- }
- var fromEdbInfo *EdbInfo
- for _, v := range edbInfoCalculateDetailList {
- fromEdbInfo, _ = GetEdbInfoById(v.FromEdbInfoId)
- break
- }
- if fromEdbInfo == nil {
- errMsg = "指标异常"
- err = errors.New(errMsg)
- return
- }
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("RefreshAllCalculateRangeAnalysis,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- // 计算数据
- latestDateStr, latestValue, err, errMsg = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, edbInfo.EdbCode, edbInfo.CalculateFormula)
- return
- }
- func (obj PredictCalculateRangeAnalysis) refresh(to orm.TxOrmer, edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, calculateFormula string) (latestDateStr string, latestValue float64, err error, errMsg string) {
- edbInfoIdStr := strconv.Itoa(edbInfoId)
- tableName := GetEdbDataTableName(obj.GetSource(), utils.DATA_SUB_SOURCE_EDB)
- //获取扩散指数指标所有数据
- existDataList, err := GetAllEdbDataListByTo(to, edbInfoId, source, subSource)
- if err != nil {
- return
- }
- latestDateStr = fromEdbInfo.LatestDate
- //计算指标的map
- existDataMap := make(map[string]*EdbData, 0)
- removeDateMap := make(map[string]string)
- for _, v := range existDataList {
- existDataMap[v.DataTime] = v
- removeDateMap[v.DataTime] = ``
- }
- var rangeAnalysisConf RangeAnalysisCalculateFormula
- //fmt.Println("calculateFormula:", calculateFormula)
- err = json.Unmarshal([]byte(calculateFormula), &rangeAnalysisConf)
- if err != nil {
- err = fmt.Errorf("解析区间计算公式失败 %s", err.Error())
- return
- }
- rangeAnalysisChartData, err := GetRangeAnalysisChartDataByEdbInfo(fromEdbInfo, rangeAnalysisConf)
- if err != nil {
- err = fmt.Errorf("获取区间计算数据失败 %s", err.Error())
- return
- }
- addSql := ` INSERT INTO ` + tableName + ` (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
- var isAdd bool
- for _, item := range rangeAnalysisChartData {
- currDateStr := item.DataTime
- currVal := item.Value
- // 判断扩散指数指标是否存在数据
- if existData, ok := existDataMap[currDateStr]; ok {
- // 处理扩散指数数据的值
- existValStr := existData.Value
- existValDeci, tmpErr := decimal.NewFromString(existValStr)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- existVal, _ := existValDeci.Round(4).Float64()
- // 判断扩散指数数据的值 与 当前计算出来的结果, 如果两个数据结果不相等的话,那么就修改咯
- if existVal != currVal {
- err = ModifyEdbDataById(source, subSource, existData.EdbDataId, fmt.Sprint(currVal))
- if err != nil {
- return
- }
- }
- } else {
- // 直接入库
- timestamp := item.DataTimestamp
- timestampStr := fmt.Sprintf("%d", timestamp)
- addSql += GetAddSql(edbInfoIdStr, edbCode, currDateStr, timestampStr, fmt.Sprint(currVal))
- isAdd = true
- }
- delete(removeDateMap, currDateStr)
- }
- // 数据入库
- {
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = to.Raw(addSql).Exec()
- }
- // 移除不存在的日期数据
- if len(removeDateMap) > 0 {
- removeDateList := make([]string, 0) //需要移除的日期
- for k := range removeDateMap {
- removeDateList = append(removeDateList, k)
- }
- removeDateStr := strings.Join(removeDateList, `","`)
- removeDateStr = `"` + removeDateStr + `"`
- sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
- _, err = to.Raw(sql, edbInfoId).Exec()
- if err != nil {
- err = fmt.Errorf("删除扩散指数指标数据失败,Err:" + err.Error())
- return
- }
- }
- }
- //确定实际数据的最终值
- {
- finalLast, tmpErr := GetFinalLastByTo(to, edbInfoId, obj.GetSource(), utils.DATA_SUB_SOURCE_EDB, fromEdbInfo.LatestDate)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- return
- }
- if tmpErr == nil {
- latestDateStr = finalLast.DataTime
- latestValue = finalLast.Value
- }
- }
- return
- }
- // GetSource 获取来源编码id
- func (obj PredictCalculateRangeAnalysis) GetSource() int {
- return utils.DATA_SOURCE_PREDICT_CALCULATE_RANGEANLYSIS
- }
- // GetSourceName 获取来源名称
- func (obj PredictCalculateRangeAnalysis) GetSourceName() string {
- return utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_RANGEANLYSIS
- }
- // GetEdbType 获取指标类型
- func (obj PredictCalculateRangeAnalysis) GetEdbType() int {
- return utils.CALCULATE_EDB_TYPE
- }
|