12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625 |
- package data_manage
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/services/data"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/shopspring/decimal"
- "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 && !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)
- }
- dataList := make([]*data_manage.BaseFromThsHfDataItem, 0)
- {
- dataOb := new(data_manage.BaseFromThsHfData)
- cond := fmt.Sprintf(" AND %s = ? AND (%s BETWEEN ? AND ?)", dataOb.Cols().IndexCode, dataOb.Cols().DataTime)
- pars := make([]interface{}, 0)
- pars = append(pars, indexItem.IndexCode, fmt.Sprintf("%s 00:00:00", dataDate), fmt.Sprintf("%s 23:59:59", dataDate))
- list, e := dataOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s DESC", dataOb.Cols().DataTime))
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取指标数据失败, %v", e)
- return
- }
- for _, v := range list {
- dataList = append(dataList, v.Format2Item())
- }
- }
- 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
- }
- }
- // TODO:同步Mongo
- if e = item.Remove(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = fmt.Sprintf("删除指标失败, %v", e)
- return
- }
- 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
- }
- // 批量移动
- indexOb := new(data_manage.BaseFromThsHfIndex)
- 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 {
- // 校验是否被引用
- 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)
- 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 := indexOb.MultiRemove(params.IndexIds); e != nil {
- br.Msg = "删除失败"
- br.ErrMsg = fmt.Sprintf("批量删除指标失败, %v", e)
- return
- }
- dataOb := new(data_manage.BaseFromThsHfData)
- cond := fmt.Sprintf(" %s IN (%s)", dataOb.Cols().BaseFromThsHfIndexId, utils.GetOrmInReplace(len(params.IndexIds)))
- pars := make([]interface{}, 0)
- pars = append(pars, params.IndexIds)
- if e := dataOb.RemoveByCondition(cond, pars); e != nil {
- br.Msg = "删除失败"
- br.ErrMsg = fmt.Sprintf("批量删除指标数据失败, %v", e)
- return
- }
- }
- // 批量刷新
- 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.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 = res.Msg
- v.ErrMsg = e.Error()
- resp.Fail = append(resp.Fail, v)
- continue
- }
- if res.Ret != 200 {
- v.Tips = res.Msg
- v.ErrMsg = fmt.Sprintf("新增失败, ErrMsg: %s", 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 = "操作成功"
- }
|