123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- package services
- import (
- "eta/eta_forum_hub/models"
- "eta/eta_forum_hub/models/mgodb"
- "eta/eta_forum_hub/utils"
- "fmt"
- "strconv"
- "time"
- )
- func BatchAddOrUpdateEdbData(req []*models.AddEdbDataReq) (err error) {
- for _, reqData := range req {
- if reqData.EdbType == 1 {
- err = AddOrUpdateEdbData(reqData.EdbCode, reqData.DataList)
- if err != nil {
- return
- }
- } else {
- err = AddOrUpdateEdbDataCalculate(reqData.EdbCode, reqData.DataList)
- if err != nil {
- return
- }
- }
- }
- return
- }
- func AddOrUpdateEdbData(edbCode string, dataList []*models.EdbDataBase) (err error) {
- addList := make([]interface{}, 0)
- existList, err := mgodb.GetEdbDataBaseByEdbCode(edbCode)
- if err != nil {
- err = fmt.Errorf("查询指标数据出错 error, %v", err)
- return
- }
- existMap := make(map[string]*mgodb.EdbDataBase, len(existList))
- deleteMap := make(map[string]struct{}, len(existList))
- for _, exist := range existList {
- tmp := exist.DataTime.Format(utils.FormatDate)
- existMap[tmp] = exist
- deleteMap[tmp] = struct{}{}
- }
- for _, v := range dataList {
- dataTime, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if e != nil {
- err = fmt.Errorf("时间格式化出错 error, %v", e)
- return
- }
- val, e := strconv.ParseFloat(v.Value, 64)
- if e != nil {
- err = fmt.Errorf("指标数据格式化出错 error, %v", e)
- return
- }
- if oldObj, ok := existMap[v.DataTime]; !ok {
- tmp := &mgodb.EdbDataBase{
- EdbInfoId: 0,
- EdbCode: v.EdbCode,
- DataTime: dataTime,
- Value: val,
- Status: v.Status,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- DataTimestamp: v.DataTimestamp,
- }
- addList = append(addList, tmp)
- if len(addList) >= 500 {
- err = mgodb.InsertEdbDataBatch(addList)
- if err != nil {
- err = fmt.Errorf("批量新增指标数据失败 error, %v", e)
- return
- }
- addList = make([]interface{}, 0)
- }
- } else {
- delete(deleteMap, v.DataTime)
- if val != oldObj.Value {
- err = mgodb.ModifyValueEdbDataValue(oldObj.EdbDataId, val)
- if err != nil {
- err = fmt.Errorf("更新指标数据出错 error, %v", err)
- return
- }
- }
- }
- }
- //遍历deletemap,找出需要删除的日期
- {
- removeDateList := make([]time.Time, 0)
- for dateTime := range deleteMap {
- dateT, e := time.ParseInLocation(utils.FormatDate, dateTime, time.Local)
- if e != nil {
- err = fmt.Errorf("日期格式错误 error, %v", e)
- return
- }
- removeDateList = append(removeDateList, dateT)
- }
- removeNum := len(removeDateList)
- if removeNum > 0 {
- err = mgodb.DeleteEdbInfoDataByEdbInfoIdAndDate(edbCode, removeDateList)
- if err != nil {
- err = fmt.Errorf("删除多余日期失败,error, %v", err)
- return
- }
- }
- }
- if len(addList) > 0 {
- err = mgodb.InsertEdbDataBatch(addList)
- }
- return
- }
- func AddOrUpdateEdbDataCalculate(edbCode string, dataList []*models.EdbDataBase) (err error) {
- addList := make([]interface{}, 0)
- existList, err := mgodb.GetEdbDataCalculateByEdbCode(edbCode)
- if err != nil {
- err = fmt.Errorf("查询指标数据出错 error, %v", err)
- return
- }
- existMap := make(map[string]*mgodb.EdbDataBase, len(existList))
- deleteMap := make(map[string]struct{}, len(existList))
- for _, exist := range existList {
- tmp := exist.DataTime.Format(utils.FormatDate)
- existMap[tmp] = exist
- deleteMap[tmp] = struct{}{}
- }
- for _, v := range dataList {
- dataTime, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if e != nil {
- err = fmt.Errorf("时间格式化出错 error, %v", e)
- return
- }
- val, e := strconv.ParseFloat(v.Value, 64)
- if e != nil {
- err = fmt.Errorf("指标数据格式化出错 error, %v", e)
- return
- }
- if oldObj, ok := existMap[v.DataTime]; !ok {
- tmp := &mgodb.EdbDataBase{
- EdbInfoId: 0,
- EdbCode: v.EdbCode,
- DataTime: dataTime,
- Value: val,
- Status: v.Status,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- DataTimestamp: v.DataTimestamp,
- }
- addList = append(addList, tmp)
- if len(addList) >= 500 {
- err = mgodb.InsertEdbCalculateDataBatch(addList)
- if err != nil {
- err = fmt.Errorf("批量新增指标数据失败 error, %v", e)
- return
- }
- addList = make([]interface{}, 0)
- }
- } else {
- delete(deleteMap, v.DataTime)
- if val != oldObj.Value {
- err = mgodb.ModifyValueEdbCalculateDataValue(oldObj.EdbDataId, val)
- if err != nil {
- err = fmt.Errorf("更新指标数据出错 error, %v", err)
- return
- }
- }
- }
- }
- //遍历deleteMap,找出需要删除的日期
- {
- removeDateList := make([]time.Time, 0)
- for dateTime := range deleteMap {
- dateT, e := time.ParseInLocation(utils.FormatDate, dateTime, time.Local)
- if e != nil {
- err = fmt.Errorf("日期格式错误 error, %v", e)
- return
- }
- removeDateList = append(removeDateList, dateT)
- }
- removeNum := len(removeDateList)
- if removeNum > 0 {
- err = mgodb.DeleteEdbCalculateDataByEdbInfoIdAndDate(edbCode, removeDateList)
- if err != nil {
- err = fmt.Errorf("删除多余日期失败,error, %v", err)
- return
- }
- }
- }
- if len(addList) > 0 {
- err = mgodb.InsertEdbCalculateDataBatch(addList)
- }
- return
- }
|