package models import ( "errors" "eta_gn/eta_index_lib/global" "eta_gn/eta_index_lib/utils" "fmt" "gorm.io/gorm" "strings" "time" ) // EdbDataPredictCalculateZjpj 直接拼接数据结构体 type EdbDataPredictCalculateZjpj struct { EdbDataId int `gorm:"primaryKey;autoIncrement;column:edb_data_id"` EdbInfoId int EdbCode string DataTime string Value float64 Status int CreateTime time.Time ModifyTime time.Time DataTimestamp int64 } // RefreshAllPredictCalculateZjpj 刷新所有 直接拼接 数据 func RefreshAllPredictCalculateZjpj(edbInfo *EdbInfo) (latestDateStr string, latestValue float64, err error) { to := global.DEFAULT_DmSQL.Begin() defer func() { if err != nil { to.Rollback() } else { to.Commit() } }() //查询关联指标信息 var existCondition string var existPars []interface{} existCondition += " AND edb_info_id=? " existPars = append(existPars, edbInfo.EdbInfoId) existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars) if err != nil { err = fmt.Errorf("判断指标是否改变失败,Err:" + err.Error()) return } var existItemA, existItemB *EdbInfoCalculateMapping for _, existItem := range existList { if existItem.FromTag == "A" { existItemA = existItem } else if existItem.FromTag == "B" { existItemB = existItem } } if existItemA == nil { err = errors.New("原拼接日期之前的指标不存在") return } if existItemB == nil { err = errors.New("原拼接日期之后的指标不存在") return } fromEdbInfo, err := GetEdbInfoById(existItemA.FromEdbInfoId) if err != nil { err = fmt.Errorf("GetEdbInfoById Err:" + err.Error()) return } secondEdbInfo, err := GetEdbInfoById(existItemB.FromEdbInfoId) if err != nil { err = fmt.Errorf("GetEdbInfoById Err:" + err.Error()) return } // 刷新数据 latestDateStr, latestValue, err = refreshAllPredictCalculateZjpj(to, edbInfo, fromEdbInfo, secondEdbInfo) return } // refreshAllPredictCalculateZjpj 刷新所有 直接拼接 数据 func refreshAllPredictCalculateZjpj(to *gorm.DB, edbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (latestDateStr string, latestValue float64, err error) { //查询当前指标现有的数据 var dataList []*EdbDataPredictCalculateZjpj sql := ` SELECT * FROM edb_data_predict_calculate_zjpj WHERE edb_info_id=? ORDER BY data_time DESC ` err = to.Raw(sql, edbInfo.EdbInfoId).Scan(&dataList).Error if err != nil { return } if edbInfo.CalculateFormula <= secondEdbInfo.LatestDate { latestDateStr = secondEdbInfo.LatestDate } else { if edbInfo.CalculateFormula >= firstEdbInfo.LatestDate { latestDateStr = firstEdbInfo.LatestDate } else { latestDateStr = edbInfo.CalculateFormula } } var dateArr []string dataMap := make(map[string]*EdbDataPredictCalculateZjpj) removeDataTimeMap := make(map[string]int) //需要移除的日期数据 for _, v := range dataList { dateArr = append(dateArr, v.DataTime) dataMap[v.DataTime] = v removeDataTimeMap[v.DataTime] = 1 } addDataList := make([]*EdbDataPredictCalculateZjpj, 0) //第一个指标 { var firstDataList []*EdbInfoSearchData firstDataList, err = GetPredictEdbDataListAllByStartDate(firstEdbInfo, 0, "") if err != nil { return } for _, v := range firstDataList { if v.DataTime >= edbInfo.CalculateFormula { continue } //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素 if _, ok := removeDataTimeMap[v.DataTime]; ok { delete(removeDataTimeMap, v.DataTime) } if latestDateStr == v.DataTime { latestValue = v.Value } //时间戳 if edbData, ok := dataMap[v.DataTime]; ok { if edbData.Value != v.Value { //更新指标数据 edbData.Value = v.Value _ = to.Model(edbData).Select([]string{"Value"}).Updates(edbData).Error } } else { //时间戳 currentDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local) timestamp := currentDate.UnixNano() / 1e6 edbDataZjpj := &EdbDataPredictCalculateZjpj{ EdbInfoId: edbInfo.EdbInfoId, EdbCode: edbInfo.EdbCode, DataTime: v.DataTime, Value: v.Value, Status: 1, CreateTime: time.Now(), ModifyTime: time.Now(), DataTimestamp: timestamp, } addDataList = append(addDataList, edbDataZjpj) } } } //第二个指标 { var secondDataList []*EdbInfoSearchData secondDataList, err = GetPredictEdbDataListAllByStartDate(secondEdbInfo, 0, edbInfo.CalculateFormula) if err != nil { return } for _, v := range secondDataList { //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素 if _, ok := removeDataTimeMap[v.DataTime]; ok { delete(removeDataTimeMap, v.DataTime) } if latestDateStr == v.DataTime { latestValue = v.Value } if edbData, ok := dataMap[v.DataTime]; ok { if edbData.Value != v.Value { //更新指标数据 edbData.Value = v.Value edbData.ModifyTime = time.Now() tmpErr := to.Model(edbData).Select([]string{"Value", "ModifyTime"}).Updates(edbData).Error if tmpErr != nil { fmt.Println("tmpErr:", tmpErr) err = tmpErr return } } } else { //时间戳 currentDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local) timestamp := currentDate.UnixNano() / 1e6 edbDataZjpj := &EdbDataPredictCalculateZjpj{ EdbInfoId: edbInfo.EdbInfoId, EdbCode: edbInfo.EdbCode, DataTime: v.DataTime, Value: v.Value, Status: 1, CreateTime: time.Now(), ModifyTime: time.Now(), DataTimestamp: timestamp, } addDataList = append(addDataList, edbDataZjpj) } } } //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了) { removeDateList := make([]string, 0) for dateTime := range removeDataTimeMap { removeDateList = append(removeDateList, dateTime) } if len(removeDateList) > 0 { removeDateStr := strings.Join(removeDateList, `','`) removeDateStr = `'` + removeDateStr + `'` //如果拼接指标变更了,那么需要删除所有的指标数据 tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource) sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr) err = to.Exec(sql, edbInfo.EdbInfoId).Error if err != nil { err = fmt.Errorf("删除不存在的直接拼接指标数据失败,Err:" + err.Error()) return } } } //数据入库 if len(addDataList) > 0 { tmpErr := to.CreateInBatches(addDataList, 500).Error if tmpErr != nil { err = tmpErr return } } return }