123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- 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"
- )
- func HandleMysteelIndex(req *models.HandleMysteelIndexResp) (err error) {
- for _, v := range req.List {
- if v.IndexName == "" || v.IndexCode == "" {
- continue
- }
- err = handleIndex(v)
- if err != nil {
- return
- }
- }
- _ = SetMysteelChemicalEdbInfoUpdateStat(false)
- _ = SetEdbSourceStat(false)
- return
- }
- func handleIndex(indexItem *models.HandleMysteelIndex) (err error) {
- defer func() {
- if err != nil {
- // 添加刷新失败日志
- dataUpdateResult := 2
- dataUpdateFailedReason := "服务异常"
- edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexItem.IndexCode)
- if e == nil {
- //查询指标存在,才添加刷新日志
- _ = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, err.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
- }
- }
- }()
- indexObj := new(models.BaseFromMysteelChemicalIndex)
- var indexId int64
- addDataList := make([]models.BaseFromMysteelChemicalData, 0)
- exitDataMap := make(map[string]*models.BaseFromMysteelChemicalData)
- //判断指标是否存在
- var isAdd int
- item, err := indexObj.GetIndexItem(indexItem.IndexCode)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- isAdd = 1
- } else {
- isAdd = -1
- return
- }
- }
- nameChange := false
- if item != nil && item.BaseFromMysteelChemicalIndexId > 0 {
- isAdd = 2
- if item.IndexName != indexItem.IndexName {
- nameChange = true
- }
- } else {
- isAdd = 1
- }
- fmt.Println("isAdd:", isAdd)
- if !strings.Contains(indexItem.Frequency, "度") {
- indexItem.Frequency = indexItem.Frequency + "度"
- }
- if isAdd == 1 {
- indexObj.IndexCode = indexItem.IndexCode
- indexObj.IndexName = indexItem.IndexName
- indexObj.Unit = indexItem.Unit
- indexObj.Source = indexItem.Source
- indexObj.Describe = indexItem.Describe
- indexObj.StartDate, err = utils.DealExcelDate(indexItem.StartDate)
- if err != nil {
- fmt.Println("utils.DealExcelDate err:" + indexItem.StartDate + err.Error())
- return
- }
- indexObj.EndDate, err = utils.DealExcelDate(indexItem.EndDate)
- if err != nil {
- fmt.Println("utils.DealExcelDate err:" + indexItem.EndDate + err.Error())
- return
- }
- indexObj.Frequency = indexItem.Frequency
- //indexObj.CreateTime = time.Now().Local()
- //indexObj.ModifyTime = time.Now().Local()
- err = indexObj.Add()
- if err != nil {
- fmt.Println("add err:" + err.Error())
- return
- }
- indexId = indexObj.BaseFromMysteelChemicalIndexId
- } else if isAdd == 2 {
- indexObj.BaseFromMysteelChemicalIndexId = item.BaseFromMysteelChemicalIndexId
- indexObj.IndexCode = indexItem.IndexCode
- indexObj.IndexName = indexItem.IndexName
- indexObj.Unit = indexItem.Unit
- indexObj.Source = indexItem.Source
- indexObj.Describe = indexItem.Describe
- indexObj.StartDate, err = utils.DealExcelDate(indexItem.StartDate)
- if err != nil {
- fmt.Println("utils.DealExcelDate err:" + indexItem.StartDate + err.Error())
- return
- }
- indexObj.EndDate, err = utils.DealExcelDate(indexItem.EndDate)
- if err != nil {
- fmt.Println("utils.DealExcelDate err:" + indexItem.EndDate + err.Error())
- return
- }
- indexObj.Frequency = indexItem.Frequency
- indexObj.ModifyTime = time.Now()
- indexId = item.BaseFromMysteelChemicalIndexId
- var isStop int
- if strings.Contains(indexItem.IndexName, "停") {
- isStop = 1
- }
- indexObj.IsStop = isStop
- //修改数据
- updateColsArr := make([]string, 0)
- updateColsArr = append(updateColsArr, "index_name")
- updateColsArr = append(updateColsArr, "unit")
- updateColsArr = append(updateColsArr, "source")
- updateColsArr = append(updateColsArr, "frequency")
- updateColsArr = append(updateColsArr, "start_date")
- updateColsArr = append(updateColsArr, "end_date")
- updateColsArr = append(updateColsArr, "describe")
- updateColsArr = append(updateColsArr, "end_date")
- updateColsArr = append(updateColsArr, "is_stop")
- updateColsArr = append(updateColsArr, "modify_time")
- e := indexObj.Update(updateColsArr)
- if e != nil {
- fmt.Println("Index Update Err:" + e.Error())
- return
- }
- dataObj := new(models.BaseFromMysteelChemicalData)
- //获取已存在的所有数据
- exitDataList, err := dataObj.GetIndexDataList(indexItem.IndexCode)
- if err != nil {
- fmt.Println("GetIndexDataList Err:" + err.Error())
- return err
- }
- fmt.Println("exitDataListLen:", len(exitDataList))
- for _, v := range exitDataList {
- dateStr := v.DataTime.Format(utils.FormatDate)
- exitDataMap[dateStr] = v
- }
- }
- dataObj := new(models.BaseFromMysteelChemicalData)
- var hasUpdate bool
- // 遍历excel数据,然后跟现有的数据做校验,不存在则入库
- for date, value := range indexItem.ExcelDataMap {
- dateTime, err := utils.DealExcelDate(date)
- if err != nil {
- fmt.Println("time.ParseInLocation Err:" + err.Error())
- return err
- }
- date = dateTime.Format(utils.FormatDate)
- if findData, ok := exitDataMap[date]; !ok {
- if !strings.Contains(value, "#N/A") {
- dataItem := new(models.BaseFromMysteelChemicalData)
- dataItem.BaseFromMysteelChemicalIndexId = indexId
- dataItem.IndexCode = indexItem.IndexCode
- dataItem.DataTime = dateTime
- dataItem.Value = value
- dataItem.UpdateDate = indexItem.UpdateDate
- dataItem.CreateTime = time.Now()
- dataItem.ModifyTime = time.Now()
- addDataList = append(addDataList, *dataItem)
- }
- } else {
- if findData != nil && findData.Value != value && !strings.Contains(value, "#N/A") { //修改数据
- dataObj.BaseFromMysteelChemicalDataId = findData.BaseFromMysteelChemicalDataId
- dataObj.Value = value
- dataObj.ModifyTime = time.Now()
- updateDataColsArr := make([]string, 0)
- updateDataColsArr = append(updateDataColsArr, "value")
- updateDataColsArr = append(updateDataColsArr, "modify_time")
- dataObj.Update(updateDataColsArr)
- hasUpdate = true
- }
- }
- }
- if len(addDataList) > 0 {
- err = dataObj.Add(addDataList)
- if err != nil {
- fmt.Println("dataObj.Add() Err:" + err.Error())
- return
- }
- hasUpdate = true
- }
- //修改最大最小日期
- mysteelIndexMaxItem, err := dataObj.GetMysteelIndexInfoMaxAndMinInfo(indexItem.IndexCode)
- if err == nil && mysteelIndexMaxItem != nil {
- e := dataObj.ModifyMysteelIndexMaxAndMinInfo(indexItem.IndexCode, mysteelIndexMaxItem)
- if e != nil {
- fmt.Println("ModifyMysteelIndexMaxAndMinInfo Err:" + e.Error())
- }
- }
- // 同步刷新图库钢联的指标
- //go func() {
- var indexErr error
- var lErr error
- defer func() {
- if indexErr != nil {
- tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新异常, 指标编码: %s, err: %s", indexObj.IndexCode, indexErr.Error())
- alarm_msg.SendAlarmMsg(tips, 3)
- }
- if lErr != nil {
- tips := fmt.Sprintf("钢联数据刷新-ETA指标刷新统计异常, 指标编码: %s, err: %s", indexObj.IndexCode, lErr.Error())
- alarm_msg.SendAlarmMsg(tips, 3)
- }
- }()
- edbInfo, e := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, indexObj.IndexCode)
- if e != nil && e.Error() != utils.ErrNoRow() {
- indexErr = e
- return
- }
- if edbInfo != nil {
- dataUpdateResult := 2
- dataUpdateFailedReason := "服务异常"
- _, logErrMsg, logErr := logic.RefreshBaseEdbInfo(edbInfo, ``)
- if logErr != nil {
- lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 2, logErrMsg+logErr.Error(), dataUpdateResult, dataUpdateFailedReason, 1, 0)
- return
- }
- if hasUpdate {
- dataUpdateResult = 1
- dataUpdateFailedReason = ""
- } else {
- dataUpdateFailedReason = "未刷新到数据"
- }
- // 添加刷新成功日志
- lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", dataUpdateResult, dataUpdateFailedReason, 1, 0)
- if lErr != nil {
- return
- }
- //如果变更了指标名称,则添加指标信息变更日志
- if nameChange {
- edbInfo.SourceIndexName = indexItem.IndexName
- lErr = edbInfo.Update([]string{"SourceIndexName"})
- if lErr != nil {
- return
- }
- lErr = AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 0, "", 0, 1)
- }
- }
- //}()
- return
- }
|