123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- package services
- import (
- "eta/eta_index_lib/logic"
- "eta/eta_index_lib/models"
- "eta/eta_index_lib/services/alarm_msg"
- "eta/eta_index_lib/utils"
- "fmt"
- "strings"
- "time"
- )
- // HandleSciIndex 处理卓创(红桃3)的Excel数据
- func HandleSciIndex(indexNameList, thirdIndexIdList, frequencyList, unitList []string, dataMap map[string]map[string]string) {
- // 卓创(红桃3)指标id列表
- sciIndexModel := new(models.BaseFromSciIndex)
- list, err := sciIndexModel.GetAllIndex()
- if err != nil {
- fmt.Println("获取指标失败:", err)
- return
- }
- allIndexMap := make(map[string]string)
- for _, v := range list {
- allIndexMap[v.IndexCode] = ``
- }
- // 需要入库的指标下标
- needAddIndexKeyList := make([]int, 0)
- needAddIndexMap := make(map[string]int, 0)
- for key, v := range thirdIndexIdList {
- if _, ok := allIndexMap[v]; !ok {
- if _, ok2 := needAddIndexMap[v]; !ok2 {
- needAddIndexKeyList = append(needAddIndexKeyList, key)
- needAddIndexMap[v] = key
- }
- }
- }
- //fmt.Println(needAddIndexKeyList)
- //return
- addSciIndexList := make([]*models.BaseFromSciIndex, 0)
- // 新的指标入库
- for _, key := range needAddIndexKeyList {
- tmpSciIndex := &models.BaseFromSciIndex{
- //BaseFromSciIndexId: 0,
- //ClassifyId: 0,
- IndexCode: thirdIndexIdList[key],
- IndexName: indexNameList[key],
- Frequency: frequencyList[key],
- Unit: unitList[key],
- //StartDate: time.Time{},
- //EndDate: time.Time{},
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- addSciIndexList = append(addSciIndexList, tmpSciIndex)
- }
- //fmt.Println(addSciIndexList)
- if len(addSciIndexList) > 0 {
- err = sciIndexModel.BatchAdd(addSciIndexList)
- if err != nil {
- fmt.Println("批量添加指标失败:", err)
- return
- }
- fmt.Println("添加成功")
- }
- // 红桃3实际数据处理
- HandleSciData(dataMap)
- }
- // HandleData 红桃3实际数据处理
- func HandleSciData(dataMap map[string]map[string]string) {
- errMsgList := make([]string, 0)
- defer func() {
- if len(errMsgList) > 0 {
- go alarm_msg.SendAlarmMsg(fmt.Sprint("红桃3实际数据处理失败,err:", strings.Join(errMsgList, "\n")), 3)
- }
- }()
- // 获取所有的指标
- sciIndexModel := new(models.BaseFromSciIndex)
- list, err := sciIndexModel.GetAllIndex()
- if err != nil {
- fmt.Println("获取指标失败:", err)
- return
- }
- allIndexMap := make(map[string]*models.BaseFromSciIndex)
- for _, v := range list {
- allIndexMap[v.IndexCode] = v
- }
- sciIndexDataModel := new(models.BaseFromSciData)
- for indexCode, data := range dataMap {
- indexInfo, ok := allIndexMap[indexCode]
- if !ok {
- fmt.Println("找不到该指标:", indexCode)
- continue
- }
- indexDataList, err := sciIndexDataModel.GetIndexDataList(indexCode)
- if err != nil {
- errMsgList = append(errMsgList, fmt.Sprint("查找卓创基础指标失败,指标编码:", indexCode, ";错误原因:", err.Error()))
- continue
- }
- indexDataExistMap := make(map[string]*models.BaseFromSciData)
- for _, indexData := range indexDataList {
- indexDataExistMap[indexData.DataTime] = indexData
- }
- addSciDataList := make([]*models.BaseFromSciData, 0)
- for currDate, currVal := range data {
- currDataTime, tmpErr := time.ParseInLocation(utils.FormatDate, currDate, time.Local)
- if tmpErr != nil {
- errMsgList = append(errMsgList, fmt.Sprint("时间格式化失败,指标编码:", currDate, ";错误原因:", tmpErr.Error()))
- continue
- }
- timestamp := currDataTime.UnixNano() / 1e6
- sciData, ok := indexDataExistMap[currDate]
- //判断是否存在数据,如果不存在,那么插入数据,存在的话那么修改数据
- if !ok {
- tmpBaseFromSciData := &models.BaseFromSciData{
- //SciDataId: 0,
- BaseFromSciIndexId: indexInfo.BaseFromSciIndexId,
- IndexCode: indexInfo.IndexCode,
- DataTime: currDate,
- Value: currVal,
- DataTimestamp: timestamp,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- indexDataExistMap[currDate] = tmpBaseFromSciData
- addSciDataList = append(addSciDataList, tmpBaseFromSciData)
- } else {
- // 更新数据
- existValue := sciData.Value
- if existValue != currVal {
- //fmt.Println("existValue:", existValue, ";====;currVal:", currVal)
- sciData.Value = currVal
- sciData.ModifyTime = time.Now()
- // 如果是已经入库了数据,那么就更新,否则只是单纯更改内存数据,而不去更新数据库
- if sciData.SciDataId > 0 {
- tmpErr = sciData.Update([]string{"Value", "ModifyTime"})
- if tmpErr != nil {
- errMsgList = append(errMsgList, fmt.Sprint("指标数据更新失败,指标编码:", indexCode, ",时间:", currDate, ";错误原因:", tmpErr.Error()))
- continue
- }
- }
- }
- }
- }
- if len(addSciDataList) > 0 {
- err = sciIndexDataModel.BatchAdd(addSciDataList)
- if err != nil {
- errMsgList = append(errMsgList, fmt.Sprint("批量添加数据失败,指标编码:", indexCode, err.Error()))
- continue
- }
- }
- indexInfo.ModifyTime = time.Now()
- indexInfo.Update([]string{"ModifyTime"})
- // 同步刷新ETA图库红桃3的指标
- {
- // 获取指标详情
- edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI, indexInfo.IndexCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsgList = append(errMsgList, fmt.Sprint("刷新ETA指标异常,指标编码:", indexCode, err.Error()))
- continue
- }
- // 已经加入到指标库的话,那么就去更新ETA指标库吧
- if edbInfo != nil {
- go logic.RefreshBaseEdbInfo(edbInfo, ``)
- }
- }
- }
- }
|