package data_manage import ( "encoding/json" "eta/eta_api/controllers" "eta/eta_api/models" "eta/eta_api/models/data_manage" "eta/eta_api/models/mgo" "eta/eta_api/services/data" "eta/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "github.com/shopspring/decimal" "go.mongodb.org/mongo-driver/bson" "sort" "strconv" "strings" "time" ) // BaseFromThsHfController 同花顺高频数据 type BaseFromThsHfController struct { controllers.BaseAuthController } // Search // @Title 新增指标-查询指标信息接口 // @Description 新增指标-查询指标信息接口 // @Param request body data_manage.ThsHfSearchEdbReq true "type json string" // @Success 200 {object} data_manage.ThsHfSearchEdbResp // @router /ths_hf/index/search [get] func (this *BaseFromThsHfController) Search() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfSearchEdbReq if e := this.ParseForm(¶ms); e != nil { br.Msg = "参数解析失败" br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e) return } params.StockCode = strings.TrimSpace(params.StockCode) if params.StockCode == "" { br.Msg = "请输入证券代码" return } stockCodes := strings.Split(params.StockCode, ",") if len(stockCodes) == 0 { br.Msg = "请输入证券代码" return } if len(stockCodes) > 10 { br.Msg = "最多输入10个证券代码" return } params.EdbCode = strings.TrimSpace(params.EdbCode) if params.EdbCode == "" { br.Msg = "请输入指标代码" return } edbCodes := strings.Split(params.EdbCode, ",") if len(edbCodes) == 0 { br.Msg = "请输入指标代码" return } if len(edbCodes) > 20 { br.Msg = "最多选择/输入20个指标代码" return } if params.StartTime == "" { br.Msg = "请选择起始时间" return } _, e := time.ParseInLocation(utils.FormatDateTime, params.StartTime, time.Local) if e != nil { br.Msg = "起始时间格式有误" br.ErrMsg = fmt.Sprintf("起始时间格式有误, %v", e) return } // 结束时间选填, 不填则为当前时间 if params.EndTime != "" { _, e := time.ParseInLocation(utils.FormatDateTime, params.EndTime, time.Local) if e != nil { br.Msg = "截止时间格式有误" br.ErrMsg = fmt.Sprintf("截止时间格式有误, %v", e) return } } if params.EndTime == "" { params.EndTime = time.Now().Local().Format(utils.FormatDateTime) } if !utils.InArrayByInt(data_manage.ThsHfPeriodArr, params.Interval) { br.Msg = "时间周期有误" br.ErrMsg = fmt.Sprintf("时间周期有误, Interval: %d", params.Interval) return } if params.CPS != "" && !utils.InArrayByStr(data_manage.ThsHfCPSArr, params.CPS) { br.Msg = "复权方式有误" br.ErrMsg = fmt.Sprintf("复权方式有误, CPS: %s", params.CPS) return } if params.BaseDate != "" { _, e = time.ParseInLocation(utils.FormatDate, params.BaseDate, time.Local) if e != nil { br.Msg = "复权基点格式有误" br.ErrMsg = fmt.Sprintf("复权基点格式有误, %v", e) return } } if params.Fill != "" && !utils.InArrayByStr(data_manage.ThsHfFillArr, params.Fill) { br.Msg = "非交易间隔处理有误" br.ErrMsg = fmt.Sprintf("非交易间隔处理有误, Fill: %s", params.Fill) return } // 校验已入库的指标 checkResp, indexExists, e := data.CheckExistThsHfEdb(stockCodes, edbCodes) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("校验已存在的高频指标失败, %v", e) return } if len(checkResp.ExistIndex) > 0 && checkResp.ExistAll { br.Msg = "指标均已存在" return } resp := make([]*data_manage.ThsHfSearchEdbResp, 0) // 请求API获取数据 indexes, e := data.GetEdbDataThsHf(params) if e != nil { br.Msg = "未搜索到指标" br.ErrMsg = fmt.Sprintf("获取高频指标失败, %v", e) return } if len(indexes) == 0 { br.Msg = "未搜索到指标" return } for _, v := range indexes { // 忽略掉校验出来的已入库指标 k := fmt.Sprintf("%s-%s", v.StockCode, v.EdbCode) if indexExists[k] { continue } // 默认指标名称 suffix := data_manage.ThsHfEdbCodeCn[v.EdbCode] if suffix == "" { suffix = v.EdbCode } indexName := fmt.Sprintf("%s%dm%s", v.StockCode, params.Interval, suffix) item := new(data_manage.ThsHfSearchEdbResp) item.StockCode = v.StockCode item.EdbCode = v.EdbCode item.IndexName = indexName item.Frequency = params.Interval // 搜索只展示100条数据 var limit int sort.Slice(v.IndexData, func(i, j int) bool { return v.IndexData[i].DataTime.After(v.IndexData[j].DataTime) }) for _, d := range v.IndexData { if limit > 100 { break } limit += 1 strVal := decimal.NewFromFloat(d.Value).Round(4).String() item.IndexData = append(item.IndexData, data_manage.ThsHfSearchEdbData{ DataTime: d.DataTime.Format(utils.FormatDateTime), Value: strVal, }) } resp = append(resp, item) } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // ExistCheck // @Title 新增指标-指标存在校验 // @Description 新增指标-指标存在校验 // @Param request body data_manage.ThsHfSearchEdbReq true "type json string" // @Success 200 {object} data_manage.ThsHfExistCheckResp // @router /ths_hf/index/exist_check [get] func (this *BaseFromThsHfController) ExistCheck() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfSearchEdbReq if e := this.ParseForm(¶ms); e != nil { br.Msg = "参数解析失败" br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e) return } params.StockCode = strings.TrimSpace(params.StockCode) if params.StockCode == "" { br.Msg = "请输入证券代码" return } stockCodes := strings.Split(params.StockCode, ",") if len(stockCodes) == 0 { br.Msg = "请输入证券代码" return } params.EdbCode = strings.TrimSpace(params.EdbCode) if params.EdbCode == "" { br.Msg = "请输入指标代码" return } edbCodes := strings.Split(params.EdbCode, ",") if len(edbCodes) == 0 { br.Msg = "请输入指标代码" return } // 校验已存在的高频指标 resp, _, e := data.CheckExistThsHfEdb(stockCodes, edbCodes) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("校验已存在的高频指标失败, %v", e) return } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // List // @Title 列表 // @Description 列表 // @Param request body data_manage.ThsHfIndexListForm true "type json string" // @Success 200 {object} data_manage.ThsHfIndexPageListResp // @router /ths_hf/index/list [get] func (this *BaseFromThsHfController) List() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfIndexListForm if e := this.ParseForm(¶ms); e != nil { br.Msg = "参数解析失败" br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e) return } if params.SortField > 0 && !utils.InArrayByInt([]int{1, 2, 3, 4}, params.SortField) { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, SortField: %d", params.SortField) return } if params.SortType > 0 && !utils.InArrayByInt([]int{1, 2}, params.SortType) { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, SortType: %d", params.SortType) return } resp := new(data_manage.ThsHfIndexPageListResp) resp.List = make([]*data_manage.BaseFromThsHfIndexItem, 0) // 查询所有分类-分类查询/分类完整路径用 classifyOb := new(data_manage.BaseFromThsHfClassify) //classifies := make([]*data_manage.BaseFromThsHfClassify, 0) classifyIdItem := make(map[int]*data_manage.BaseFromThsHfClassify) { list, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName, classifyOb.Cols().ClassifyNameEn, classifyOb.Cols().LevelPath}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e) return } for _, v := range list { classifyIdItem[v.BaseFromThsHfClassifyId] = v } //classifies = list } // 筛选项 var ( cond string pars []interface{} listOrder string ) indexOb := new(data_manage.BaseFromThsHfIndex) { // 分类 if params.ClassifyId != "" { classifyIdArr := strings.Split(params.ClassifyId, ",") classifyIds := make([]int, 0) for _, v := range classifyIdArr { t, _ := strconv.Atoi(v) if t > 0 { classifyIds = append(classifyIds, t) } } if len(classifyIds) == 0 { page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0) resp.Paging = page br.Ret = 200 br.Success = true br.Msg = "获取成功" return } cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(classifyIds))) pars = append(pars, classifyIds) //// 不包含子分类 //if len(classifyIds) > 0 && !params.IncludeChild { // cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(classifyIds))) // pars = append(pars, classifyIds) //} // //// 包含子分类 //if len(classifyIds) > 0 && params.IncludeChild { // queryClassifyIds := make([]int, 0) // queryClassifyExist := make(map[int]bool) // // for _, v := range classifyIds { // // 遍历所有分类从LevelPath中找含有查询分类ID的...=_=! // for _, cv := range classifies { // if queryClassifyExist[cv.BaseFromThsHfClassifyId] { // continue // } // if cv.LevelPath == "" { // continue // } // strArr := strings.Split(cv.LevelPath, ",") // if len(strArr) == 0 { // continue // } // for _, sv := range strArr { // tv, _ := strconv.Atoi(sv) // if tv == v { // queryClassifyIds = append(queryClassifyIds, cv.BaseFromThsHfClassifyId) // queryClassifyExist[cv.BaseFromThsHfClassifyId] = true // break // } // } // } // } // // if len(queryClassifyIds) == 0 { // page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0) // resp.Paging = page // br.Ret = 200 // br.Success = true // br.Msg = "获取成功" // return // } // cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(queryClassifyIds))) // pars = append(pars, queryClassifyIds) //} } if params.Frequency != "" { frequencyArr := strings.Split(params.Frequency, ",") if len(frequencyArr) > 0 { cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().Frequency, utils.GetOrmInReplace(len(frequencyArr))) pars = append(pars, frequencyArr) } } if params.SysAdminId != "" { adminIdArr := strings.Split(params.SysAdminId, ",") adminIds := make([]int, 0) for _, v := range adminIdArr { t, _ := strconv.Atoi(v) if t > 0 { adminIds = append(adminIds, t) } } if len(adminIds) > 0 { cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().SysUserId, utils.GetOrmInReplace(len(adminIds))) pars = append(pars, adminIds) } } params.Keywords = strings.TrimSpace(params.Keywords) if params.Keywords != "" { cond += fmt.Sprintf(" AND (%s LIKE ? OR %s LIKE ?)", indexOb.Cols().IndexCode, indexOb.Cols().IndexName) kw := fmt.Sprint("%", params.Keywords, "%") pars = append(pars, kw, kw) } // 排序 if params.SortField > 0 && params.SortType > 0 { fieldMap := map[int]string{1: indexOb.Cols().StartDate, 2: indexOb.Cols().EndDate, 3: indexOb.Cols().ModifyTime, 4: indexOb.Cols().LatestValue} typeMap := map[int]string{1: "ASC", 2: "DESC"} listOrder = fmt.Sprintf("%s %s", fieldMap[params.SortField], typeMap[params.SortType]) } } // 列表总计 total, e := indexOb.GetCountByCondition(cond, pars) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取指标总数失败, %v", e) return } if total <= 0 { page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0) resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" return } // 分页查询 var startSize int if params.PageSize <= 0 { params.PageSize = utils.PageSize20 } if params.CurrentIndex <= 0 { params.CurrentIndex = 1 } startSize = utils.StartIndex(params.CurrentIndex, params.PageSize) items, e := indexOb.GetPageItemsByCondition(cond, pars, []string{}, listOrder, startSize, params.PageSize) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取指标列表失败, %v", e) return } for _, v := range items { t := v.Format2Item() // 分类完整路径 classify := classifyIdItem[v.BaseFromThsHfClassifyId] levelArr := make([]string, 0) if classify != nil && classify.LevelPath != "" { arr := strings.Split(classify.LevelPath, ",") for _, a := range arr { i, _ := strconv.Atoi(a) if classifyIdItem[i] != nil { if this.Lang == utils.EnLangVersion { levelArr = append(levelArr, classifyIdItem[i].ClassifyNameEn) } else { levelArr = append(levelArr, classifyIdItem[i].ClassifyName) } } } } t.ClassifyPath = strings.Join(levelArr, "/") resp.List = append(resp.List, t) } page := paging.GetPaging(params.CurrentIndex, params.PageSize, total) resp.Paging = page br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Add // @Title 新增指标 // @Description 新增指标 // @Param request body data_manage.ThsHfAddEdbReq true "type json string" // @Success 200 string "操作成功" // @router /ths_hf/index/add [post] func (this *BaseFromThsHfController) Add() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfAddEdbReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if params.StartTime == "" { br.Msg = "请选择起始时间" return } _, e := time.ParseInLocation(utils.FormatDateTime, params.StartTime, time.Local) if e != nil { br.Msg = "起始时间格式有误" br.ErrMsg = fmt.Sprintf("起始时间格式有误, %v", e) return } if params.EndTime != "" { _, e := time.ParseInLocation(utils.FormatDateTime, params.EndTime, time.Local) if e != nil { br.Msg = "截止时间格式有误" br.ErrMsg = fmt.Sprintf("截止时间格式有误, %v", e) return } } if !utils.InArrayByInt(data_manage.ThsHfPeriodArr, params.Interval) { br.Msg = "时间周期有误" br.ErrMsg = fmt.Sprintf("时间周期有误, Interval: %d", params.Interval) return } if params.CPS != "" && !utils.InArrayByStr(data_manage.ThsHfCPSArr, params.CPS) { br.Msg = "复权方式有误" br.ErrMsg = fmt.Sprintf("复权方式有误, CPS: %s", params.CPS) return } if params.BaseDate != "" { _, e = time.ParseInLocation(utils.FormatDate, params.BaseDate, time.Local) if e != nil { br.Msg = "复权基点格式有误" br.ErrMsg = fmt.Sprintf("复权基点格式有误, %v", e) return } } if params.Fill != "" && !utils.InArrayByStr(data_manage.ThsHfFillArr, params.Fill) { br.Msg = "非交易间隔处理有误" br.ErrMsg = fmt.Sprintf("非交易间隔处理有误, Fill: %s", params.Fill) return } if len(params.IndexList) == 0 { br.Msg = "请选择指标" return } formExist := make(map[string]bool) indexNames := make([]string, 0) classifyIds := make([]int, 0) for _, v := range params.IndexList { if v.ClassifyId <= 0 { br.Msg = "请选择分类" return } if !utils.InArrayByInt(classifyIds, v.ClassifyId) { classifyIds = append(classifyIds, v.ClassifyId) } if v.Unit == "" { br.Msg = "请输入单位" return } v.IndexName = strings.TrimSpace(v.IndexName) if v.IndexName == "" { br.Msg = "请输入指标名称" return } if formExist[v.IndexName] { br.Msg = "指标名称重复, 请重新输入" return } formExist[v.IndexName] = true indexNames = append(indexNames, v.IndexName) } // 校验指标分类 classifyOb := new(data_manage.BaseFromThsHfClassify) { cond := fmt.Sprintf(` AND %s IN (%s)`, classifyOb.Cols().PrimaryId, utils.GetOrmInReplace(len(classifyIds))) pars := make([]interface{}, 0) pars = append(pars, classifyIds) list, e := classifyOb.GetItemsByCondition(cond, pars, []string{classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName}, "") if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("查询分类失败, %v", e) return } classifyMap := make(map[int]*data_manage.BaseFromThsHfClassify) for _, v := range list { classifyMap[v.BaseFromThsHfClassifyId] = v } for _, v := range params.IndexList { t := classifyMap[v.ClassifyId] if t == nil { br.Msg = fmt.Sprintf("%s分类不存在, 请重新选择或刷新页面", t.ClassifyName) return } } } // 校验指标名称 indexOb := new(data_manage.BaseFromThsHfIndex) { cond := fmt.Sprintf(` AND %s IN (%s)`, indexOb.Cols().IndexName, utils.GetOrmInReplace(len(indexNames))) pars := make([]interface{}, 0) pars = append(pars, indexNames) list, e := indexOb.GetItemsByCondition(cond, pars, []string{indexOb.Cols().IndexName}, "") if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("查询指标名称失败, %v", e) return } if len(list) > 0 { tips := "以下指标名称重复, 请重新输入: \n" for _, v := range list { tips += fmt.Sprintf("%s\n", v.IndexName) } br.Msg = tips return } } // 新增指标 for _, v := range params.IndexList { var req data_manage.ThsHfBaseAddReq req.StartTime = params.StartTime req.EndTime = params.EndTime req.Interval = params.Interval req.Fill = params.Fill req.CPS = params.CPS req.BaseDate = params.BaseDate req.SysAdminId = sysUser.AdminId req.SysAdminName = sysUser.RealName req.ClassifyId = v.ClassifyId req.Unit = v.Unit req.IndexName = v.IndexName req.Frequency = v.Frequency req.StockCode = v.StockCode req.EdbCode = v.EdbCode _, e = data.BaseAddThsHf(req) if e != nil { utils.FileLog.Info(fmt.Sprintf("BaseAddThsHf err: %v", e)) continue } } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Edit // @Title 编辑指标 // @Description 编辑指标 // @Param request body data_manage.ThsHfIndexEditReq true "type json string" // @Success 200 string "操作成功" // @router /ths_hf/index/edit [post] func (this *BaseFromThsHfController) Edit() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfIndexEditReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if params.IndexId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId) return } params.IndexName = strings.TrimSpace(params.IndexName) if params.IndexName == "" { br.Msg = "请输入指标名称" return } if params.ClassifyId <= 0 { br.Msg = "请选择分类" return } params.Unit = strings.TrimSpace(params.Unit) if params.Unit == "" { br.Msg = "请输入单位" return } indexOb := new(data_manage.BaseFromThsHfIndex) item, e := indexOb.GetItemById(params.IndexId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "指标不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e) return } classifyOb := new(data_manage.BaseFromThsHfClassify) _, e = classifyOb.GetItemById(params.ClassifyId) if e != nil { br.Msg = "分类有误" br.ErrMsg = fmt.Sprintf("分类信息有误, %v", e) return } // 重名验证 { cond := fmt.Sprintf(" AND %s = ? AND %s <> ?", indexOb.Cols().IndexName, indexOb.Cols().PrimaryId) pars := make([]interface{}, 0) pars = append(pars, params.IndexName, params.IndexId) count, e := indexOb.GetCountByCondition(cond, pars) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取重名指标失败, %v", e) return } if count > 0 { br.Msg = "指标名称已存在, 请重新输入" return } } item.IndexName = params.IndexName item.BaseFromThsHfClassifyId = params.ClassifyId item.Unit = params.Unit item.ModifyTime = time.Now().Local() updateCols := []string{item.Cols().IndexName, item.Cols().BaseFromThsHfClassifyId, item.Cols().Unit, item.Cols().ModifyTime} if e = item.Update(updateCols); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("更新指标信息失败, %v", e) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Detail // @Title 详情 // @Description 详情 // @Param IndexId query int true "指标ID" // @Param DataDate query string false "数据日期" // @Success 200 {object} data_manage.ThsHfSearchEdbResp // @router /ths_hf/index/detail [get] func (this *BaseFromThsHfController) Detail() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } indexId, _ := this.GetInt("IndexId") if indexId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", indexId) return } dataDate := this.GetString("DataDate") if dataDate != "" { _, e := time.Parse(utils.FormatDate, dataDate) if e != nil { br.Msg = "数据日期格式有误" br.ErrMsg = fmt.Sprintf("数据日期格式有误, DataDate: %s", dataDate) return } } indexOb := new(data_manage.BaseFromThsHfIndex) item, e := indexOb.GetItemById(indexId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "指标不存在, 请刷新页面" return } br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e) return } indexItem := item.Format2Item() if item.EndDate.IsZero() { item.EndDate = time.Now() } // 默认取有数据的最新日期 if dataDate == "" { dataDate = item.EndDate.Format(utils.FormatDate) } startTime := fmt.Sprintf("%s 00:00:00", dataDate) endTime := fmt.Sprintf("%s 23:59:59", dataDate) // 获取数据 dataList, e := data.GetThsHfBaseIndexData(indexItem.IndexCode, startTime, endTime) if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取指标数据失败, %v", e) return } type DetailResp struct { Index *data_manage.BaseFromThsHfIndexItem DataDate string `description:"数据日期"` DataList []*data_manage.BaseFromThsHfDataItem } resp := new(DetailResp) resp.Index = indexItem resp.DataDate = dataDate resp.DataList = dataList br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Refresh // @Title 刷新指标 // @Description 刷新指标 // @Param request body data_manage.ThsHfIndexOptReq true "type json string" // @Success 200 string "操作成功" // @router /ths_hf/index/refresh [post] func (this *BaseFromThsHfController) Refresh() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfIndexOptReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if params.IndexId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId) return } indexOb := new(data_manage.BaseFromThsHfIndex) _, e := indexOb.GetItemById(params.IndexId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "指标不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e) return } // 指标刷新逻辑 indexIds := []int{params.IndexId} _, e = data.RefreshBaseThsHfIndex(indexIds, 1) if e != nil { br.Msg = "刷新失败" br.ErrMsg = fmt.Sprintf("刷新指标失败, %v", e) return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Remove // @Title 删除指标 // @Description 删除指标 // @Param request body data_manage.ThsHfIndexOptReq true "type json string" // @Success 200 string "操作成功" // @router /ths_hf/index/remove [post] func (this *BaseFromThsHfController) Remove() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfIndexOptReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if params.IndexId <= 0 { br.Msg = "参数有误" br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId) return } indexOb := new(data_manage.BaseFromThsHfIndex) item, e := indexOb.GetItemById(params.IndexId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Ret = 200 br.Success = true br.Msg = "操作成功" return } br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e) return } // 删除校验, 指标库存在不可删除 edbMappingOb := new(data_manage.BaseFromEdbMapping) { cond := fmt.Sprintf(" AND %s = ?", edbMappingOb.Cols().BaseIndexCode) pars := make([]interface{}, 0) pars = append(pars, item.IndexCode) count, e := edbMappingOb.GetCountByCondition(cond, pars) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取源指标关联失败, %v", e) return } if count > 0 { br.Msg = "指标已被引用, 不允许删除" return } } if e = item.Remove(); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("删除指标失败, %v", e) return } if utils.UseMongo { mogDataObj := mgo.BaseFromThsHfData{} if e = mogDataObj.RemoveMany(bson.M{"index_code": item.IndexCode}); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("删除指标数据失败-Mgo, %v", e) return } } else { dataOb := new(data_manage.BaseFromThsHfData) cond := fmt.Sprintf(" %s = ?", dataOb.Cols().BaseFromThsHfIndexId) pars := make([]interface{}, 0) pars = append(pars, params.IndexId) if e = dataOb.RemoveByCondition(cond, pars); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("删除指标数据失败, %v", e) return } } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // ListChoice // @Title 列表选择 // @Description 列表选择 // @Param request body data_manage.ThsHfIndexMultiOptReq true "type json string" // @Success 200 string "获取成功" // @router /ths_hf/index/list_choice [get] func (this *BaseFromThsHfController) ListChoice() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfIndexListChoiceReq if e := this.ParseForm(¶ms); e != nil { br.Msg = "参数解析失败" br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e) return } var listIds []int if params.ListIds != "" { strArr := strings.Split(params.ListIds, ",") for _, v := range strArr { t, _ := strconv.Atoi(v) if t > 0 { listIds = append(listIds, t) } } } var ( cond string pars []interface{} ) indexOb := new(data_manage.BaseFromThsHfIndex) resp := make([]*data_manage.ThsHfIndexListChoiceItem, 0) // 非列表全选 if !params.SelectAll { if len(listIds) > 0 { cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(listIds))) pars = append(pars, listIds) } } // 列表全选, 根据条件筛选列表并过滤params.ListIds中选择的ID if params.SelectAll { // 查询所有分类-分类查询用 classifyOb := new(data_manage.BaseFromThsHfClassify) classifies := make([]*data_manage.BaseFromThsHfClassify, 0) classifyIdItem := make(map[int]*data_manage.BaseFromThsHfClassify) { list, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName, classifyOb.Cols().ClassifyNameEn, classifyOb.Cols().LevelPath}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e) return } for _, v := range list { classifyIdItem[v.BaseFromThsHfClassifyId] = v } classifies = list } // 筛选项 if params.ClassifyId != "" { classifyIdArr := strings.Split(params.ClassifyId, ",") classifyIds := make([]int, 0) for _, v := range classifyIdArr { t, _ := strconv.Atoi(v) if t > 0 { classifyIds = append(classifyIds, t) } } // 不包含子分类 if len(classifyIds) > 0 && !params.IncludeChild { cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(classifyIds))) pars = append(pars, classifyIds) } // 包含子分类 if len(classifyIds) > 0 && params.IncludeChild { queryClassifyIds := make([]int, 0) queryClassifyExist := make(map[int]bool) for _, v := range classifyIds { // 遍历所有分类从LevelPath中找含有查询分类ID的...=_=! for _, cv := range classifies { if queryClassifyExist[cv.BaseFromThsHfClassifyId] { continue } if cv.LevelPath == "" { continue } strArr := strings.Split(cv.LevelPath, ",") if len(strArr) == 0 { continue } for _, sv := range strArr { tv, _ := strconv.Atoi(sv) if tv == v { queryClassifyIds = append(queryClassifyIds, cv.BaseFromThsHfClassifyId) queryClassifyExist[cv.BaseFromThsHfClassifyId] = true break } } } } if len(queryClassifyIds) == 0 { br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" return } cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(queryClassifyIds))) pars = append(pars, queryClassifyIds) } } if params.Frequency != "" { frequencyArr := strings.Split(params.Frequency, ",") if len(frequencyArr) > 0 { cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().Frequency, utils.GetOrmInReplace(len(frequencyArr))) pars = append(pars, frequencyArr) } } if params.SysAdminId != "" { adminIdArr := strings.Split(params.SysAdminId, ",") adminIds := make([]int, 0) for _, v := range adminIdArr { t, _ := strconv.Atoi(v) if t > 0 { adminIds = append(adminIds, t) } } if len(adminIds) > 0 { cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().SysUserId, utils.GetOrmInReplace(len(adminIds))) pars = append(pars, adminIds) } } params.Keywords = strings.TrimSpace(params.Keywords) if params.Keywords != "" { cond += fmt.Sprintf(" AND (%s LIKE ? OR %s LIKE ?)", indexOb.Cols().IndexCode, indexOb.Cols().IndexName) kw := fmt.Sprint("%", params.Keywords, "%") pars = append(pars, kw, kw) } // 过滤掉选择的指标 if len(listIds) > 0 { cond += fmt.Sprintf(" AND %s NOT IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(listIds))) pars = append(pars, listIds) } } fields := []string{indexOb.Cols().PrimaryId, indexOb.Cols().IndexCode, indexOb.Cols().IndexName} list, e := indexOb.GetItemsByCondition(cond, pars, fields, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e) return } if len(list) > 500 { br.Msg = "选择指标超过500个, 请重新选择" return } for _, v := range list { resp = append(resp, &data_manage.ThsHfIndexListChoiceItem{ IndexId: v.BaseFromThsHfIndexId, IndexCode: v.IndexCode, IndexName: v.IndexName, }) } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // MultiOpt // @Title 批量操作-移动分类/删除/刷新 // @Description 批量操作-移动分类/删除/刷新 // @Param request body data_manage.ThsHfIndexMultiOptReq true "type json string" // @Success 200 string "操作成功" // @router /ths_hf/index/multi_opt [post] func (this *BaseFromThsHfController) MultiOpt() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfIndexMultiOptReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if !utils.InArrayByInt([]int{1, 2, 3}, params.OptType) { br.Msg = "请选择操作类型" return } if len(params.IndexIds) == 0 { br.Msg = "请选择指标" return } if params.OptType == 1 && params.MoveClassifyId <= 0 { br.Msg = "请选择分类" return } if params.OptType == 3 && !utils.InArrayByInt([]int{1, 2}, params.RefreshType) { br.Msg = "请选择刷新方式" return } resp := new(data_manage.ThsHfIndexMultiOptResp) // 指标名称 indexOb := new(data_manage.BaseFromThsHfIndex) indexMap := make(map[int]*data_manage.BaseFromThsHfIndex) { cond := fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(params.IndexIds))) pars := make([]interface{}, 0) pars = append(pars, params.IndexIds) list, e := indexOb.GetItemsByCondition(cond, pars, []string{indexOb.Cols().PrimaryId, indexOb.Cols().IndexCode, indexOb.Cols().IndexName}, "") if e != nil { br.Msg = "删除失败" br.ErrMsg = fmt.Sprintf("获取源指标列表失败, %v", e) return } for _, v := range list { indexMap[v.BaseFromThsHfIndexId] = v } } // 批量移动 if params.OptType == 1 { if e := indexOb.UpdateClassifyMulti(params.IndexIds, params.MoveClassifyId); e != nil { br.Msg = "移动失败" br.ErrMsg = fmt.Sprintf("批量移动分类失败, %v", e) return } } // 批量删除 if params.OptType == 2 { // 被引用的指标需要提示出来, 未被引用的直接删除 usedIndexIds := make([]int, 0) { edbMappingOb := new(data_manage.BaseFromEdbMapping) cond := fmt.Sprintf(" AND %s IN (%s)", edbMappingOb.Cols().BaseFromIndexId, utils.GetOrmInReplace(len(params.IndexIds))) pars := make([]interface{}, 0) pars = append(pars, params.IndexIds) list, e := edbMappingOb.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { br.Msg = "删除失败" br.ErrMsg = fmt.Sprintf("获取源指标关联失败, %v", e) return } for _, v := range list { if !utils.InArrayByInt(usedIndexIds, v.BaseFromIndexId) { usedIndexIds = append(usedIndexIds, v.BaseFromIndexId) } } } // 可删除的指标 removeIndexIds := params.IndexIds if len(usedIndexIds) > 0 { removeIndexIds = utils.MinusInt(params.IndexIds, usedIndexIds) // 标记不允许删除的 for _, v := range usedIndexIds { t := indexMap[v] if t == nil { continue } resp.Fail = append(resp.Fail, &data_manage.ThsHfIndexBaseInfo{ IndexId: v, IndexCode: t.IndexCode, IndexName: t.IndexName, }) } } if len(removeIndexIds) > 0 { if e := indexOb.MultiRemove(removeIndexIds); e != nil { br.Msg = "删除失败" br.ErrMsg = fmt.Sprintf("批量删除指标失败, %v", e) return } if utils.UseMongo { mogDataObj := mgo.BaseFromThsHfData{} for _, v := range removeIndexIds { if e := mogDataObj.RemoveMany(bson.M{"base_from_ths_hf_index_id": v}); e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("批量删除指标数据失败-Mgo, %v", e) return } } } else { dataOb := new(data_manage.BaseFromThsHfData) cond := fmt.Sprintf(" %s IN (%s)", dataOb.Cols().BaseFromThsHfIndexId, utils.GetOrmInReplace(len(removeIndexIds))) pars := make([]interface{}, 0) pars = append(pars, removeIndexIds) if e := dataOb.RemoveByCondition(cond, pars); e != nil { br.Msg = "删除失败" br.ErrMsg = fmt.Sprintf("批量删除指标数据失败, %v", e) return } } // 标记删除成功的 for _, v := range removeIndexIds { t := indexMap[v] if t == nil { continue } resp.Success = append(resp.Success, &data_manage.ThsHfIndexBaseInfo{ IndexId: v, IndexCode: t.IndexCode, IndexName: t.IndexName, }) } } } // 批量刷新 if params.OptType == 3 { isAsync, e := data.RefreshBaseThsHfIndex(params.IndexIds, params.RefreshType) if e != nil { br.Msg = "刷新失败" br.ErrMsg = fmt.Sprintf("批量刷新指标失败, %v", e) return } if isAsync { br.Ret = 200 br.Success = true br.Msg = "操作成功, 刷新指标较多, 请10分钟后查看" return } } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Save2EdbPre // @Title 批量新增指标库-前置 // @Description 批量新增指标库-前置 // @Param request body data_manage.ThsHfIndexMultiSave2EdbReq true "type json string" // @Success 200 string "操作成功" // @router /ths_hf/index/save2edb_pre [post] func (this *BaseFromThsHfController) Save2EdbPre() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfIndexMultiSave2EdbPreReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if len(params.IndexIds) == 0 { br.Msg = "请选择指标" return } convertRule := params.ConvertRule if convertRule.ConvertType != 1 && convertRule.ConvertType != 2 { br.Msg = "请选择数据转换方式" return } // 生成的指标名称后缀 var ( suffixName string startTimeCn string endTimeCn string calculateCn string ) dayMap := map[int]string{1: "当日", 2: "前日"} calculateMap := map[int]string{1: "均值", 2: "最大值", 3: "最小值"} if convertRule.ConvertType == 1 { if convertRule.ConvertFixed.FixedDay != 1 && convertRule.ConvertFixed.FixedDay != 2 { br.Msg = "请选择指定时间" return } if convertRule.ConvertFixed.FixedTime == "" { br.Msg = "请选择指定时间" return } timePrefix := time.Now().Local().Format(utils.FormatDate) st := fmt.Sprintf("%s %s", timePrefix, convertRule.ConvertFixed.FixedTime) startTime, e := time.Parse(utils.FormatDateTime, st) if e != nil { br.Msg = "指定时间格式有误" return } startTimeCn = fmt.Sprintf("%s%s", dayMap[convertRule.ConvertFixed.FixedDay], startTime.Format("15点04")) } if convertRule.ConvertType == 2 { if convertRule.ConvertArea.StartDay != 1 && convertRule.ConvertArea.StartDay != 2 { br.Msg = "请选择起始时间" return } if convertRule.ConvertArea.StartTime == "" { br.Msg = "请选择起始时间" return } var startTimePre string if convertRule.ConvertArea.StartDay == 1 { startTimePre = time.Now().Local().Format(utils.FormatDate) } if convertRule.ConvertArea.StartDay == 2 { startTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate) } st := fmt.Sprintf("%s %s", startTimePre, convertRule.ConvertArea.StartTime) startTime, e := time.Parse(utils.FormatDateTime, st) if e != nil { br.Msg = "起始时间格式有误" return } startTimeCn = fmt.Sprintf("%s%s", dayMap[convertRule.ConvertArea.StartDay], startTime.Format("15点04")) if convertRule.ConvertArea.EndDay != 1 && convertRule.ConvertArea.EndDay != 2 { br.Msg = "请选择截止时间" return } if convertRule.ConvertArea.EndTime == "" { br.Msg = "请选择截止时间" return } var endTimePre string if convertRule.ConvertArea.EndDay == 1 { endTimePre = time.Now().Local().Format(utils.FormatDate) } if convertRule.ConvertArea.EndDay == 2 { endTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate) } ed := fmt.Sprintf("%s %s", endTimePre, convertRule.ConvertArea.EndTime) endTime, e := time.Parse(utils.FormatDateTime, ed) if e != nil { br.Msg = "截止时间格式有误" return } endTimeCn = fmt.Sprintf("至%s%s", dayMap[convertRule.ConvertArea.EndDay], endTime.Format("15点04")) if startTime.After(endTime) { br.Msg = "起始日期不可早于截止日期" return } calculateCn = calculateMap[convertRule.ConvertArea.CalculateType] } suffixName = fmt.Sprint(startTimeCn, endTimeCn, calculateCn) // 查询指标 indexOb := new(data_manage.BaseFromThsHfIndex) cond := fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(params.IndexIds))) pars := make([]interface{}, 0) pars = append(pars, params.IndexIds) list, e := indexOb.GetItemsByCondition(cond, pars, []string{}, "") if e != nil { br.Msg = "获取失败" br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e) return } resp := make([]*data_manage.ThsHfIndexMultiSave2EdbPreItem, 0) for _, v := range list { t := new(data_manage.ThsHfIndexMultiSave2EdbPreItem) t.IndexId = v.BaseFromThsHfIndexId t.IndexCode = v.IndexCode t.IndexName = v.IndexName edbCn := data_manage.ThsHfEdbCodeCn[v.Indicator] if edbCn == "" { edbCn = v.Indicator } t.NewIndexName = fmt.Sprint(v.StockCode, v.Frequency, edbCn, suffixName) t.StockCode = v.StockCode t.EdbCode = v.Indicator t.Unit = v.Unit t.Frequency = v.Frequency t.NewFrequency = "日度" resp = append(resp, t) } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Save2Edb // @Title 批量新增指标库 // @Description 批量新增指标库 // @Param request body data_manage.ThsHfIndexMultiSave2EdbReq true "type json string" // @Success 200 string "操作成功" // @router /ths_hf/index/save2edb [post] func (this *BaseFromThsHfController) Save2Edb() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() sysUser := this.SysUser if sysUser == nil { br.Msg = "请登录" br.ErrMsg = "请登录,SysUser Is Empty" br.Ret = 408 return } var params data_manage.ThsHfIndexMultiSave2EdbReq if e := json.Unmarshal(this.Ctx.Input.RequestBody, ¶ms); e != nil { br.Msg = "参数解析异常" br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e) return } if len(params.NewIndexes) == 0 { br.Msg = "请选择指标" return } convertRule := params.ConvertRule if convertRule.ConvertType != 1 && convertRule.ConvertType != 2 { br.Msg = "请选择数据转换方式" return } if convertRule.ConvertType == 1 { if convertRule.ConvertFixed.FixedDay != 1 && convertRule.ConvertFixed.FixedDay != 2 { br.Msg = "请选择指定时间" return } if convertRule.ConvertFixed.FixedTime == "" { br.Msg = "请选择指定时间" return } timePrefix := time.Now().Local().Format(utils.FormatDate) st := fmt.Sprintf("%s %s", timePrefix, convertRule.ConvertFixed.FixedTime) _, e := time.Parse(utils.FormatDateTime, st) if e != nil { br.Msg = "指定时间格式有误" return } } if convertRule.ConvertType == 2 { if convertRule.ConvertArea.StartDay != 1 && convertRule.ConvertArea.StartDay != 2 { br.Msg = "请选择起始时间" return } if convertRule.ConvertArea.StartTime == "" { br.Msg = "请选择起始时间" return } var startTimePre string if convertRule.ConvertArea.StartDay == 1 { startTimePre = time.Now().Local().Format(utils.FormatDate) } if convertRule.ConvertArea.StartDay == 2 { startTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate) } st := fmt.Sprintf("%s %s", startTimePre, convertRule.ConvertArea.StartTime) startTime, e := time.Parse(utils.FormatDateTime, st) if e != nil { br.Msg = "起始时间格式有误" return } if convertRule.ConvertArea.EndDay != 1 && convertRule.ConvertArea.EndDay != 2 { br.Msg = "请选择截止时间" return } if convertRule.ConvertArea.EndTime == "" { br.Msg = "请选择截止时间" return } var endTimePre string if convertRule.ConvertArea.EndDay == 1 { endTimePre = time.Now().Local().Format(utils.FormatDate) } if convertRule.ConvertArea.EndDay == 2 { endTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate) } ed := fmt.Sprintf("%s %s", endTimePre, convertRule.ConvertArea.EndTime) endTime, e := time.Parse(utils.FormatDateTime, ed) if e != nil { br.Msg = "截止时间格式有误" return } if startTime.After(endTime) { br.Msg = "起始日期不可早于截止日期" return } } resp := new(data_manage.ThsHfIndexMultiSave2EdbResp) // 判断指标名称是否重复 edbNameExist := make(map[string]bool) { items, e := data_manage.GetEdbInfoFieldList(``, make([]interface{}, 0), []string{"edb_name"}) if e != nil { br.Msg = "操作失败" br.ErrMsg = fmt.Sprintf("获取指标名称列表失败, %v", e) return } for _, v := range items { edbNameExist[v.EdbName] = true } } for _, v := range params.NewIndexes { if edbNameExist[v.NewIndexName] { resp.Exist = append(resp.Exist, v) } } if len(resp.Exist) > 0 { br.Data = resp br.Ret = 200 br.Success = true br.Msg = "含重复指标名称" return } // 请求指标库新增 for _, v := range params.NewIndexes { var r data_manage.ThsHfIndexMultiSave2EdbLibReq r.ConvertRule = convertRule v.SysAdminId = sysUser.AdminId v.SysAdminName = sysUser.RealName r.NewIndex = v b, e := json.Marshal(r) if e != nil { v.Tips = "新增失败" v.ErrMsg = e.Error() resp.Fail = append(resp.Fail, v) continue } res, e := data.AddBaseEdbInfo(string(b), utils.DATA_SOURCE_THS, utils.DATA_SUB_SOURCE_HIGH_FREQUENCY, this.Lang) if e != nil { v.Tips = "新增失败" v.ErrMsg = fmt.Sprintf("AddBaseEdbInfo, err: %v", e) resp.Fail = append(resp.Fail, v) continue } if res.Ret != 200 { v.Tips = res.Msg v.ErrMsg = fmt.Sprintf("AddBaseEdbInfo, Ret: %d, ErrMsg: %s", res.Ret, res.ErrMsg) resp.Fail = append(resp.Fail, v) continue } v.Tips = "新增成功" resp.Success = append(resp.Success, v) } br.Data = resp br.Ret = 200 br.Success = true br.Msg = "操作成功" }