123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- package excel
- import (
- "encoding/json"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/models/data_manage"
- excelModel "eta_gn/eta_api/models/data_manage/excel"
- "eta_gn/eta_api/models/data_manage/excel/request"
- "eta_gn/eta_api/services/data"
- "eta_gn/eta_api/services/data/excel"
- "eta_gn/eta_api/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- // EdbList
- // @Title 指标列表
- // @Description 指标列表
- // @Param ExcelInfoId query int true "excel的id"
- // @Param Keyword query string false "指标关键词"
- // @Success 200 {object} []excel.ExcelEdbMappingItem
- // @router /edb/list [get]
- func (c *CustomAnalysisController) EdbList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- excelInfoId, _ := c.GetInt("ExcelInfoId")
- if excelInfoId <= 0 {
- br.Msg = "请选择excel"
- br.IsSendEmail = false
- return
- }
- keyword := c.GetString("Keyword")
- keyword = strings.TrimSpace(keyword)
- if keyword != "" {
- keyword = fmt.Sprint("%", keyword, "%")
- }
- // 获取excel表详情
- excelInfo, err := excelModel.GetExcelInfoById(excelInfoId)
- if err != nil {
- br.Msg = "找不到该EXCEL!"
- br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
- return
- }
- if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
- br.Msg = "EXCEL异常!"
- br.IsSendEmail = false
- return
- }
- list, err := excelModel.GetExcelEdbMappingItemByExcelInfoIdOrKeyword(excelInfo.ExcelInfoId, keyword)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- for k, v := range list {
- var tmpCalculateFormula excelModel.CalculateFormula
- err = json.Unmarshal([]byte(v.CalculateFormula), &tmpCalculateFormula)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "公式转换失败,Err:" + err.Error()
- return
- }
- v.DateSequenceStr = tmpCalculateFormula.DateSequenceStr
- v.DataSequenceStr = tmpCalculateFormula.DataSequenceStr
- list[k] = v
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = list
- }
- // AddEdb
- // @Title 新增指标接口
- // @Description 新增指标接口
- // @Param request body request.AddEdb true "type json string"
- // @Success 200 {object} data_manage.AddEdbInfoResp
- // @router /edb/add [post]
- func (c *CustomAnalysisController) AddEdb() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- deleteCache := true
- cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_EDB_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- var req request.AddEdb
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.EdbName = strings.Trim(req.EdbName, " ")
- if req.EdbName == "" {
- br.Msg = "请填写指标名称!"
- br.IsSendEmail = false
- return
- }
- if req.ExcelInfoId <= 0 {
- br.Msg = "请选择excel!"
- br.IsSendEmail = false
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择指标分类!"
- br.IsSendEmail = false
- return
- }
- excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "找不到该EXCEL!"
- br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
- return
- }
- if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
- br.Msg = "EXCEL异常!"
- br.IsSendEmail = false
- return
- }
- //excel.GetCustomAnalysisExcelData(excelInfo)
- dateList, dataList, err, errMsg := excel.HandleEdbSequenceVal(req.DateSequenceVal, req.DataSequenceVal)
- if err != nil {
- br.Msg = "时间序列或数据序列异常!"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "时间序列或数据序列处理异常!err:" + err.Error()
- return
- }
- type CustomAnalysisData struct {
- ExcelInfoId int `description:"excel的id"`
- DateList []string
- DataList []float64
- }
- type Formula struct {
- DateSequenceStr string `description:"日期序列"`
- DataSequenceStr string `description:"数据序列"`
- }
- formulaByte, err := json.Marshal(Formula{
- DateSequenceStr: req.DateSequenceStr,
- DataSequenceStr: req.DataSequenceStr,
- })
- if err != nil {
- br.Msg = "时间序列或数据序列配置异常!"
- br.ErrMsg = "json序列化时间序列或数据序列异常!err:" + err.Error()
- return
- }
- req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
- AdminId: sysUser.AdminId,
- AdminName: sysUser.RealName,
- EdbName: req.EdbName,
- Frequency: req.Frequency,
- Unit: req.Unit,
- ClassifyId: req.ClassifyId,
- Formula: string(formulaByte), //公式
- Source: utils.DATA_SOURCE_CALCULATE_ZDYFX,
- Data: CustomAnalysisData{
- ExcelInfoId: req.ExcelInfoId,
- DateList: dateList,
- DataList: dataList,
- },
- }
- // 调用指标库去更新
- reqJson, err := json.Marshal(req2)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- respItem, err := data.BatchSaveEdbCalculateData(string(reqJson), c.Lang)
- if err != nil {
- br.Msg = "新增失败"
- br.ErrMsg = "新增失败,Err:" + err.Error()
- return
- }
- if respItem.Ret != 200 {
- br.Msg = respItem.Msg
- br.ErrMsg = respItem.ErrMsg
- return
- }
- resp := respItem.Data
- //添加es
- data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- }
- // EditEdb
- // @Title 编辑指标接口
- // @Description 编辑指标接口
- // @Param request body request.EditEdb true "type json string"
- // @Success 200 {object} data_manage.AddEdbInfoResp
- // @router /edb/edit [post]
- func (c *CustomAnalysisController) EditEdb() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- deleteCache := true
- cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_EDB_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- var req request.EditEdb
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.EdbName = strings.Trim(req.EdbName, " ")
- if req.EdbName == "" {
- br.Msg = "请填写指标名称!"
- br.IsSendEmail = false
- return
- }
- if req.ExcelInfoId <= 0 {
- br.Msg = "请选择excel!"
- br.IsSendEmail = false
- return
- }
- if req.EdbInfoId <= 0 {
- br.Msg = "请选择指标!"
- br.IsSendEmail = false
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择指标分类!"
- br.IsSendEmail = false
- return
- }
- excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "找不到该EXCEL!"
- br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
- return
- }
- if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
- br.Msg = "EXCEL异常!"
- br.IsSendEmail = false
- return
- }
- edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- br.Msg = "指标已被删除,请刷新页面"
- br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
- return
- }
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
- return
- }
- //excel.GetCustomAnalysisExcelData(excelInfo)
- dateList, dataList, err, errMsg := excel.HandleEdbSequenceVal(req.DateSequenceVal, req.DataSequenceVal)
- if err != nil {
- br.Msg = "时间序列或数据序列异常!"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "时间序列或数据序列处理异常!err:" + err.Error()
- return
- }
- type CustomAnalysisData struct {
- ExcelInfoId int `description:"excel的id"`
- DateList []string
- DataList []float64
- }
- type Formula struct {
- DateSequenceStr string `description:"日期序列"`
- DataSequenceStr string `description:"数据序列"`
- }
- formulaByte, err := json.Marshal(Formula{
- DateSequenceStr: req.DateSequenceStr,
- DataSequenceStr: req.DataSequenceStr,
- })
- if err != nil {
- br.Msg = "时间序列或数据序列配置异常!"
- br.ErrMsg = "json序列化时间序列或数据序列异常!err:" + err.Error()
- return
- }
- // 构造请求
- req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
- EdbInfoId: req.EdbInfoId,
- EdbName: req.EdbName,
- Frequency: req.Frequency,
- Unit: req.Unit,
- ClassifyId: req.ClassifyId,
- Formula: string(formulaByte), //公式
- Source: utils.DATA_SOURCE_CALCULATE_ZDYFX,
- Data: CustomAnalysisData{
- ExcelInfoId: req.ExcelInfoId,
- DateList: dateList,
- DataList: dataList,
- },
- }
- // 调用指标库去更新
- reqJson, err := json.Marshal(req2)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- respItem, err := data.BatchEditEdbCalculateData(string(reqJson), c.Lang)
- if err != nil {
- br.Msg = "编辑失败"
- br.ErrMsg = "编辑失败,Err:" + err.Error()
- return
- }
- if respItem.Ret != 200 {
- br.Msg = respItem.Msg
- br.ErrMsg = respItem.ErrMsg
- return
- }
- resp := respItem.Data
- //添加es
- data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
- // 修改关联的预测指标基础信息
- go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfo, req.Frequency, req.Unit)
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- }
- // EdbRefresh
- // @Title 指标列表
- // @Description 指标列表
- // @Param ExcelInfoId query int true "excel的id"
- // @Success 200 {object} []excel.ExcelEdbMappingItem
- // @router /edb/refresh [get]
- func (c *CustomAnalysisController) EdbRefresh() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- excelInfoId, _ := c.GetInt("ExcelInfoId")
- if excelInfoId <= 0 {
- br.Msg = "请选择excel"
- br.IsSendEmail = false
- return
- }
- cacheKey := "CACHE_EXCEL_EDB_REFRESH_" + strconv.Itoa(c.SysUser.AdminId)
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + c.SysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- defer func() {
- _ = utils.Rc.Delete(cacheKey)
- }()
- // 获取excel表详情
- excelInfo, err := excelModel.GetExcelInfoById(excelInfoId)
- if err != nil {
- br.Msg = "找不到该EXCEL!"
- br.ErrMsg = "找不到该EXCEL!err:" + err.Error()
- return
- }
- if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
- br.Msg = "EXCEL异常!"
- br.IsSendEmail = false
- return
- }
- err, errMsg, isSendEmail := excel.Refresh(excelInfo, c.Lang)
- if err != nil {
- br.Msg = "刷新失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "刷新失败,Err:" + err.Error()
- br.IsSendEmail = isSendEmail
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "刷新成功"
- }
- //func init() {
- // excelInfo, err := excelModel.GetExcelInfoById(160)
- // if err != nil {
- // fmt.Println("查找excel失败:", err)
- // return
- // }
- // _, err, _ = excel.GenerateExcelCustomAnalysisExcel(excelInfo)
- // if err != nil {
- // fmt.Println("生成excel失败:", err)
- // return
- // }
- //}
|