1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705 |
- 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 = "操作成功"
- }
|