123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_index_lib/logic"
- "eta/eta_index_lib/models"
- "eta/eta_index_lib/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- type ClarksonsDataController struct {
- BaseAuthController
- }
- // @Title 克拉克森
- // @Description 刷新克拉克森数据接口
- // @Param request body models.CoalMineDataReq true "type json string"
- // @Success 200 {object} models.EdbClassify
- // @router /data [post]
- func (this *ClarksonsDataController) ClarksonsData() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.CoalMineDataReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- var dateMap = make(map[int]string)
- var indexMap = make(map[string]*models.BaseFromClarksonsIndex)
- //var indexMap = make(map[string]string)
- var itemsIndex []*models.BaseFromClarksonsData
- codeList, err := models.GetBaseFromClarksonsIndex()
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Info("获取克拉克森指标失败:", err)
- return
- }
- if len(codeList) > 0 {
- for _, v := range codeList {
- indexMap[v.IndexCode] = v
- }
- }
- for _, sheet := range req.SheetData {
- //遍历行读取
- maxRow := sheet.MaxRow
- var indexId int64
- index := new(models.BaseFromClarksonsIndex)
- indexCode := ""
- for i := 3; i < maxRow; i++ {
- //获取样本情况
- row := sheet.Rows[i]
- cells := row.Cells
- // indexCode
- if i == 3 {
- indexCode = cells[1].Value
- continue
- }
- if indexItem, ok := indexMap[indexCode]; !ok {
- // indexName
- if i == 4 {
- index.IndexName = cells[1].Value
- continue
- }
- // unit
- if i == 5 {
- index.Unit = cells[1].Value
- continue
- }
- index.IndexCode = indexCode
- index.CreateTime = time.Now()
- index.ModifyTime = time.Now()
- indexId, err = models.AddBaseFromClarksonsIndex(index)
- if err != nil {
- utils.FileLog.Info("添加指标名称错误", err.Error())
- return
- }
- index.BaseFromClarksonsIndexId = int(indexId)
- indexMap[indexCode] = index
- } else {
- index = indexItem
- }
- // 数据及日期
- if i > 5 {
- var date string
- if len(cells) == 0 {
- break
- }
- for k, cell := range cells {
- text := cell.Value
- text = strings.Replace(text, "\\", "", -1)
- if text != "" {
- // 时间
- if k == 0 {
- if text != "" {
- dataTime, err := utils.ParseDateTime(text)
- if err != nil {
- utils.FileLog.Info("解析时间字符串出错:", err)
- fmt.Println("解析时间字符串出错:", err)
- return
- }
- date = dataTime.Format(utils.FormatDate)
- }
- dateMap[k] = date
- }
- // 数据
- if k == 1 {
- item := new(models.BaseFromClarksonsData)
- item.IndexCode = indexCode
- item.Value = text
- item.DataTime = date
- item.BaseFromClarksonsIndexId = index.BaseFromClarksonsIndexId
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- itemsIndex = append(itemsIndex, item)
- continue
- }
- }
- }
- }
- }
- if len(itemsIndex) > 2 {
- time1, _ := time.Parse(utils.FormatDate, itemsIndex[0].DataTime)
- time2, _ := time.Parse(utils.FormatDate, itemsIndex[1].DataTime)
- diff := time2.Sub(time1).Hours() / 24
- if diff < 5 {
- index.Frequency = "日度"
- } else if diff > 5 && diff < 10 {
- index.Frequency = "周度"
- } else if diff > 20 && diff < 50 {
- index.Frequency = "月度"
- } else if diff > 50 && diff < 100 {
- index.Frequency = "季度"
- } else if diff > 100 {
- index.Frequency = "年度"
- }
- }
- err = models.UpdateBaseFromClarksonsIndex(index)
- if err != nil {
- fmt.Println("修改指标频度错误", err)
- return
- } else {
- fmt.Println("修改指标频度成功 ", index.IndexCode, index.Frequency)
- }
- }
- count, err := models.AddBaseFromClarksonsDataMuti(itemsIndex)
- if err != nil {
- fmt.Println("添加数据错误", err.Error())
- } else {
- fmt.Println("添加数据成功", count)
- }
- fmt.Println("数据操作完成")
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- // @Title 新增克拉克森指标接口
- // @Description 新增克拉克森指标接口
- // @Success 200 {object} models.AddEdbInfoReq
- // @router /add [post]
- func (this *ClarksonsDataController) Add() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- utils.Rc.Delete(cacheKey)
- this.Data["json"] = br
- this.ServeJSON()
- }()
- source := utils.DATA_SOURCE_CLARKSONS
- 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(source) + "_" + req.EdbCode
- if !utils.Rc.IsExist(cacheKey) {
- utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
- err = models.AddEdbDataFromClarksons(req.EdbCode)
- if err != nil {
- br.Msg = "获取指标信息失败!"
- br.ErrMsg = "获取指标信息失败 AddEdbDataFromClarksons,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- } else {
- br.Ret = 501
- br.Success = true
- br.Msg = "系统处理中,请稍后重试"
- }
- }
- // @Title 刷新克拉克森指标接口
- // @Description 刷新克拉克森指标接口
- // @Success 200 {object} models.RefreshEdbInfoReq
- // @router /refresh [post]
- func (this *ClarksonsDataController) Refresh() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- utils.Rc.Delete(cacheKey)
- this.Data["json"] = br
- this.ServeJSON()
- }()
- source := utils.DATA_SOURCE_CLARKSONS
- 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
- }
- cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
- if utils.Rc.IsExist(cacheKey) {
- br.Ret = 501
- br.Success = true
- br.Msg = "系统处理中,请稍后重试"
- return
- }
- utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
- // 获取指标详情
- edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- br.Msg = "指标不存在!"
- br.ErrMsg = "指标不存在"
- return
- }
- err = models.RefreshEdbDataFromClarksons(req.EdbInfoId, req.EdbCode, req.StartDate)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "刷新指标信息失败!"
- br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromCoal,Err:" + err.Error()
- return
- }
- // 更新指标最大最小值
- err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- // 更新ES
- go logic.UpdateEs(edbInfo.EdbInfoId)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
|