123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_index_lib/logic"
- "eta/eta_index_lib/models"
- "eta/eta_index_lib/utils"
- "fmt"
- "strconv"
- "time"
- )
- type SciHqController struct {
- BaseAuthController
- }
- // Add
- // @Title 新增卓创红期指标接口
- // @Description 新增卓创红期指标接口
- // @Success 200 {object} models.AddEdbInfoReq
- // @router /add [post]
- func (this *SciHqController) Add() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- utils.Rc.Delete(cacheKey)
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.AddEdbInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbCode == "" {
- br.Msg = "请输入指标编码!"
- br.ErrMsg = "请输入指标编码,指标编码为空"
- return
- }
- cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(req.Source) + "_" + req.EdbCode
- if !utils.Rc.IsExist(cacheKey) {
- utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
- err = models.AddEdbDataFromSciHq(req.EdbCode)
- if err != nil {
- br.Msg = "获取指标信息失败!"
- br.ErrMsg = "获取指标信息失败 AddEdbDataFromSci,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- } else {
- br.Ret = 501
- br.Success = true
- br.Msg = "系统处理中,请稍后重试"
- }
- }
- // Refresh
- // @Title 刷新卓创红期指标接口
- // @Description 刷新卓创红期指标接口
- // @Success 200 {object} models.RefreshEdbInfoReq
- // @router /refresh [post]
- func (this *SciHqController) Refresh() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.RefreshEdbInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbCode == "" {
- br.Msg = "请输入指标编码!"
- br.ErrMsg = "请输入指标编码,指标编码为空"
- return
- }
- if req.EdbInfoId < 0 {
- br.Msg = "请输入指标ID!"
- br.ErrMsg = "请输入指标ID"
- return
- }
- // 获取指标详情
- edbInfo, err := models.GetEdbInfoByEdbCode(req.Source, req.EdbCode)
- if err != nil {
- br.Msg = "指标不存在!"
- br.ErrMsg = "指标不存在"
- return
- }
- isHandling, errMsg, err := logic.RefreshBaseEdbInfo(edbInfo, req.StartDate)
- if isHandling {
- br.Ret = 501
- br.Success = true
- br.Msg = "系统处理中,请稍后重试"
- return
- }
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // HandleExcelData
- // @Title 处理卓创红期指标的接口
- // @Description 处理卓创红期指标的接口
- // @Success 200 {object} models.HandleMysteelIndexResp
- // @router /handle/excel_data [post]
- func (this *SciHqController) HandleExcelData() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req []*models.HandleSciHqExcelDataReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- indexList, err := models.GetAllBaseFromSciHqIndex()
- if err != nil {
- br.Msg = "获取指标列表失败!"
- br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
- return
- }
- allIndexMap := make(map[string]*models.BaseFromSciHqIndex)
- for _, v := range indexList {
- allIndexMap[v.IndexCode] = v
- }
- addSciHqIndexList := make([]*models.BaseFromSciHqIndex, 0)
- for _, v := range req {
- t := new(models.BaseFromSciHqIndex)
- if _, ok := allIndexMap[v.ExcelIndexCode]; !ok {
- t.ClassifyId = 0
- t.IndexName = v.IndexName
- t.IndexCode = v.ExcelIndexCode
- t.Frequency = v.Frequency
- t.Unit = v.Unit
- t.TerminalCode = v.TerminalCode
- t.FilePath = v.FilePath
- t.CreateTime = time.Now()
- t.ModifyTime = time.Now()
- addSciHqIndexList = append(addSciHqIndexList, t)
- }
- }
- if len(addSciHqIndexList) > 0 {
- err = models.BatchAddBaseFromSciHqIndex(addSciHqIndexList)
- if err != nil {
- br.Msg = "添加指标失败!"
- br.ErrMsg = "添加指标失败,Err:" + err.Error()
- return
- }
- }
- indexList, err = models.GetAllBaseFromSciHqIndex()
- if err != nil {
- br.Msg = "获取指标列表失败!"
- br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
- return
- }
- allIndexMap = make(map[string]*models.BaseFromSciHqIndex)
- for _, v := range indexList {
- allIndexMap[v.IndexCode] = v
- }
- for _, v := range req {
- if indexInfo, ok := allIndexMap[v.ExcelIndexCode]; !ok {
- utils.FileLog.Info("指标不存在,indexCode:" + v.ExcelIndexCode)
- continue
- } else {
- indexDataList, err := models.GetBaseFromSciHqDataByIndexCode(indexInfo.IndexCode)
- if err != nil {
- br.Msg = "获取指标数据失败!"
- br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
- return
- }
- indexDataExistMap := make(map[string]*models.BaseFromSciHqData)
- for _, indexData := range indexDataList {
- indexDataExistMap[indexData.DataTime] = indexData
- }
- addSciHqDataList := make([]*models.BaseFromSciHqData, 0)
- for dataDate, dataVal := range v.Data {
- currDataTime, err := time.ParseInLocation(utils.FormatDate, dataDate, time.Local)
- if err != nil {
- utils.FileLog.Info("时间格式化失败,indexCode:" + v.ExcelIndexCode + "Err:" + err.Error())
- continue
- }
- timestamp := currDataTime.UnixNano() / 1e6
- sciHqData, ok := indexDataExistMap[dataDate]
- if !ok {
- tmpBaseFromSciData := &models.BaseFromSciHqData{
- BaseFromSciHqIndexId: indexInfo.BaseFromSciHqIndexId,
- IndexCode: indexInfo.IndexCode,
- DataTime: dataDate,
- Value: dataVal,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- DataTimestamp: timestamp,
- }
- addSciHqDataList = append(addSciHqDataList, tmpBaseFromSciData)
- } else {
- existValue := sciHqData.Value
- if dataVal != "" && existValue != dataVal {
- sciHqData.Value = dataVal
- sciHqData.ModifyTime = time.Now()
- if sciHqData.SciHqDataId > 0 {
- err = sciHqData.Update([]string{"value", "modify_time"})
- if err != nil {
- utils.FileLog.Info("更新指标数据失败,indexCode:" + v.ExcelIndexCode + "Err:" + err.Error())
- continue
- }
- }
- }
- }
- }
- if len(addSciHqDataList) > 0 {
- err = models.BatchAddBaseFromSciHqData(addSciHqDataList)
- if err != nil {
- utils.FileLog.Info("添加指标数据失败,indexCode:" + v.ExcelIndexCode + "Err:" + err.Error())
- continue
- }
- }
- //修改最大最小日期
- sciHqIndexMaxItem, err := models.GetSciHqIndexInfoMaxAndMinInfo(indexInfo.IndexCode)
- if err == nil && sciHqIndexMaxItem != nil {
- e := models.ModifySciHqIndexMaxAndMinInfo(indexInfo.IndexCode, sciHqIndexMaxItem)
- if e != nil {
- fmt.Println("ModifySciHqIndexMaxAndMinInfo Err:" + e.Error())
- }
- }
- // 同步刷新ETA图库卓创红期的指标
- {
- // 获取指标详情
- edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_SCI_HQ, indexInfo.IndexCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Info("刷新指标异常,indexCode:" + v.ExcelIndexCode + "Err:" + err.Error())
- continue
- }
- // 已经加入到指标库的话,那么就去更新ETA指标库吧
- if edbInfo != nil {
- go logic.RefreshBaseEdbInfo(edbInfo, ``)
- }
- }
- }
- }
- br.Msg = "处理成功"
- br.Success = true
- br.Ret = 200
- }
|