base_from_ths_hf.go 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. dataSourceModel "eta/eta_api/models/data_source"
  8. "eta/eta_api/models/mgo"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/services/elastic"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "github.com/rdlucklib/rdluck_tools/paging"
  14. "github.com/shopspring/decimal"
  15. "go.mongodb.org/mongo-driver/bson"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // BaseFromThsHfController 同花顺高频数据
  22. type BaseFromThsHfController struct {
  23. controllers.BaseAuthController
  24. }
  25. // Search
  26. // @Title 新增指标-查询指标信息接口
  27. // @Description 新增指标-查询指标信息接口
  28. // @Param request body data_manage.ThsHfSearchEdbReq true "type json string"
  29. // @Success 200 {object} data_manage.ThsHfSearchEdbResp
  30. // @router /ths_hf/index/search [get]
  31. func (this *BaseFromThsHfController) Search() {
  32. br := new(models.BaseResponse).Init()
  33. defer func() {
  34. if br.ErrMsg == "" {
  35. br.IsSendEmail = false
  36. }
  37. this.Data["json"] = br
  38. this.ServeJSON()
  39. }()
  40. sysUser := this.SysUser
  41. if sysUser == nil {
  42. br.Msg = "请登录"
  43. br.ErrMsg = "请登录,SysUser Is Empty"
  44. br.Ret = 408
  45. return
  46. }
  47. var params data_manage.ThsHfSearchEdbReq
  48. if e := this.ParseForm(&params); e != nil {
  49. br.Msg = "参数解析失败"
  50. br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e)
  51. return
  52. }
  53. params.StockCode = strings.TrimSpace(params.StockCode)
  54. if params.StockCode == "" {
  55. br.Msg = "请输入证券代码"
  56. return
  57. }
  58. stockCodes := strings.Split(params.StockCode, ",")
  59. if len(stockCodes) == 0 {
  60. br.Msg = "请输入证券代码"
  61. return
  62. }
  63. if len(stockCodes) > 10 {
  64. br.Msg = "最多输入10个证券代码"
  65. return
  66. }
  67. params.EdbCode = strings.TrimSpace(params.EdbCode)
  68. if params.EdbCode == "" {
  69. br.Msg = "请输入指标代码"
  70. return
  71. }
  72. edbCodes := strings.Split(params.EdbCode, ",")
  73. if len(edbCodes) == 0 {
  74. br.Msg = "请输入指标代码"
  75. return
  76. }
  77. if len(edbCodes) > 20 {
  78. br.Msg = "最多选择/输入20个指标代码"
  79. return
  80. }
  81. if params.StartTime == "" {
  82. br.Msg = "请选择起始时间"
  83. return
  84. }
  85. _, e := time.ParseInLocation(utils.FormatDateTime, params.StartTime, time.Local)
  86. if e != nil {
  87. br.Msg = "起始时间格式有误"
  88. br.ErrMsg = fmt.Sprintf("起始时间格式有误, %v", e)
  89. return
  90. }
  91. // 结束时间选填, 不填则为当前时间
  92. if params.EndTime != "" {
  93. _, e := time.ParseInLocation(utils.FormatDateTime, params.EndTime, time.Local)
  94. if e != nil {
  95. br.Msg = "截止时间格式有误"
  96. br.ErrMsg = fmt.Sprintf("截止时间格式有误, %v", e)
  97. return
  98. }
  99. }
  100. if params.EndTime == "" {
  101. params.EndTime = time.Now().Local().Format(utils.FormatDateTime)
  102. }
  103. if !utils.InArrayByInt(data_manage.ThsHfPeriodArr, params.Interval) {
  104. br.Msg = "时间周期有误"
  105. br.ErrMsg = fmt.Sprintf("时间周期有误, Interval: %d", params.Interval)
  106. return
  107. }
  108. if params.CPS != "" && !utils.InArrayByStr(data_manage.ThsHfCPSArr, params.CPS) {
  109. br.Msg = "复权方式有误"
  110. br.ErrMsg = fmt.Sprintf("复权方式有误, CPS: %s", params.CPS)
  111. return
  112. }
  113. if params.BaseDate != "" {
  114. _, e = time.ParseInLocation(utils.FormatDate, params.BaseDate, time.Local)
  115. if e != nil {
  116. br.Msg = "复权基点格式有误"
  117. br.ErrMsg = fmt.Sprintf("复权基点格式有误, %v", e)
  118. return
  119. }
  120. }
  121. if params.Fill != "" && !utils.InArrayByStr(data_manage.ThsHfFillArr, params.Fill) {
  122. br.Msg = "非交易间隔处理有误"
  123. br.ErrMsg = fmt.Sprintf("非交易间隔处理有误, Fill: %s", params.Fill)
  124. return
  125. }
  126. // 校验已入库的指标
  127. checkResp, indexExists, e := data.CheckExistThsHfEdb(stockCodes, edbCodes)
  128. if e != nil {
  129. br.Msg = "获取失败"
  130. br.ErrMsg = fmt.Sprintf("校验已存在的高频指标失败, %v", e)
  131. return
  132. }
  133. if len(checkResp.ExistIndex) > 0 && checkResp.ExistAll {
  134. br.Msg = "指标均已存在"
  135. return
  136. }
  137. resp := make([]*data_manage.ThsHfSearchEdbResp, 0)
  138. // 请求API获取数据
  139. indexes, e := data.GetEdbDataThsHf(params)
  140. if e != nil {
  141. br.Msg = "未搜索到指标"
  142. br.ErrMsg = fmt.Sprintf("获取高频指标失败, %v", e)
  143. return
  144. }
  145. if len(indexes) == 0 {
  146. br.Msg = "未搜索到指标"
  147. return
  148. }
  149. for _, v := range indexes {
  150. // 忽略掉校验出来的已入库指标
  151. k := fmt.Sprintf("%s-%s", v.StockCode, v.EdbCode)
  152. if indexExists[k] {
  153. continue
  154. }
  155. // 默认指标名称
  156. suffix := data_manage.ThsHfEdbCodeCn[v.EdbCode]
  157. if suffix == "" {
  158. suffix = v.EdbCode
  159. }
  160. indexName := fmt.Sprintf("%s%dm%s", v.StockCode, params.Interval, suffix)
  161. item := new(data_manage.ThsHfSearchEdbResp)
  162. item.StockCode = v.StockCode
  163. item.EdbCode = v.EdbCode
  164. item.IndexName = indexName
  165. item.Frequency = params.Interval
  166. // 搜索只展示100条数据
  167. var limit int
  168. sort.Slice(v.IndexData, func(i, j int) bool {
  169. return v.IndexData[i].DataTime.After(v.IndexData[j].DataTime)
  170. })
  171. for _, d := range v.IndexData {
  172. if limit > 100 {
  173. break
  174. }
  175. limit += 1
  176. strVal := decimal.NewFromFloat(d.Value).Round(4).String()
  177. item.IndexData = append(item.IndexData, data_manage.ThsHfSearchEdbData{
  178. DataTime: d.DataTime.Format(utils.FormatDateTime),
  179. Value: strVal,
  180. })
  181. }
  182. resp = append(resp, item)
  183. }
  184. br.Data = resp
  185. br.Ret = 200
  186. br.Success = true
  187. br.Msg = "获取成功"
  188. }
  189. // ExistCheck
  190. // @Title 新增指标-指标存在校验
  191. // @Description 新增指标-指标存在校验
  192. // @Param request body data_manage.ThsHfSearchEdbReq true "type json string"
  193. // @Success 200 {object} data_manage.ThsHfExistCheckResp
  194. // @router /ths_hf/index/exist_check [get]
  195. func (this *BaseFromThsHfController) ExistCheck() {
  196. br := new(models.BaseResponse).Init()
  197. defer func() {
  198. if br.ErrMsg == "" {
  199. br.IsSendEmail = false
  200. }
  201. this.Data["json"] = br
  202. this.ServeJSON()
  203. }()
  204. sysUser := this.SysUser
  205. if sysUser == nil {
  206. br.Msg = "请登录"
  207. br.ErrMsg = "请登录,SysUser Is Empty"
  208. br.Ret = 408
  209. return
  210. }
  211. var params data_manage.ThsHfSearchEdbReq
  212. if e := this.ParseForm(&params); e != nil {
  213. br.Msg = "参数解析失败"
  214. br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e)
  215. return
  216. }
  217. params.StockCode = strings.TrimSpace(params.StockCode)
  218. if params.StockCode == "" {
  219. br.Msg = "请输入证券代码"
  220. return
  221. }
  222. stockCodes := strings.Split(params.StockCode, ",")
  223. if len(stockCodes) == 0 {
  224. br.Msg = "请输入证券代码"
  225. return
  226. }
  227. params.EdbCode = strings.TrimSpace(params.EdbCode)
  228. if params.EdbCode == "" {
  229. br.Msg = "请输入指标代码"
  230. return
  231. }
  232. edbCodes := strings.Split(params.EdbCode, ",")
  233. if len(edbCodes) == 0 {
  234. br.Msg = "请输入指标代码"
  235. return
  236. }
  237. // 校验已存在的高频指标
  238. resp, _, e := data.CheckExistThsHfEdb(stockCodes, edbCodes)
  239. if e != nil {
  240. br.Msg = "获取失败"
  241. br.ErrMsg = fmt.Sprintf("校验已存在的高频指标失败, %v", e)
  242. return
  243. }
  244. br.Data = resp
  245. br.Ret = 200
  246. br.Success = true
  247. br.Msg = "获取成功"
  248. }
  249. // List
  250. // @Title 列表
  251. // @Description 列表
  252. // @Param request body data_manage.ThsHfIndexListForm true "type json string"
  253. // @Success 200 {object} data_manage.ThsHfIndexPageListResp
  254. // @router /ths_hf/index/list [get]
  255. func (this *BaseFromThsHfController) List() {
  256. br := new(models.BaseResponse).Init()
  257. defer func() {
  258. if br.ErrMsg == "" {
  259. br.IsSendEmail = false
  260. }
  261. this.Data["json"] = br
  262. this.ServeJSON()
  263. }()
  264. sysUser := this.SysUser
  265. if sysUser == nil {
  266. br.Msg = "请登录"
  267. br.ErrMsg = "请登录,SysUser Is Empty"
  268. br.Ret = 408
  269. return
  270. }
  271. var params data_manage.ThsHfIndexListForm
  272. if e := this.ParseForm(&params); e != nil {
  273. br.Msg = "参数解析失败"
  274. br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e)
  275. return
  276. }
  277. if params.SortField > 0 && !utils.InArrayByInt([]int{1, 2, 3, 4}, params.SortField) {
  278. br.Msg = "参数有误"
  279. br.ErrMsg = fmt.Sprintf("参数有误, SortField: %d", params.SortField)
  280. return
  281. }
  282. if params.SortType > 0 && !utils.InArrayByInt([]int{1, 2}, params.SortType) {
  283. br.Msg = "参数有误"
  284. br.ErrMsg = fmt.Sprintf("参数有误, SortType: %d", params.SortType)
  285. return
  286. }
  287. params.Keywords = strings.TrimSpace(params.Keywords)
  288. resp := new(data_manage.ThsHfIndexPageListResp)
  289. resp.List = make([]*data_manage.BaseFromThsHfIndexItem, 0)
  290. // 查询所有分类-分类查询/分类完整路径用
  291. classifyOb := new(data_manage.BaseFromThsHfClassify)
  292. //classifies := make([]*data_manage.BaseFromThsHfClassify, 0)
  293. classifyIdItem := make(map[int]*data_manage.BaseFromThsHfClassify)
  294. {
  295. list, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName, classifyOb.Cols().ClassifyNameEn, classifyOb.Cols().LevelPath}, "")
  296. if e != nil {
  297. br.Msg = "获取失败"
  298. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  299. return
  300. }
  301. for _, v := range list {
  302. classifyIdItem[v.BaseFromThsHfClassifyId] = v
  303. }
  304. //classifies = list
  305. }
  306. // 分页查询
  307. var startSize int
  308. if params.PageSize <= 0 {
  309. params.PageSize = utils.PageSize20
  310. }
  311. if params.CurrentIndex <= 0 {
  312. params.CurrentIndex = 1
  313. }
  314. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  315. // 筛选项
  316. var (
  317. cond string
  318. pars []interface{}
  319. listOrder string
  320. classifyIds []int
  321. adminIds []int
  322. frequencyArr []string
  323. )
  324. indexOb := new(data_manage.BaseFromThsHfIndex)
  325. {
  326. // 分类
  327. if params.ClassifyId != "" {
  328. classifyIdArr := strings.Split(params.ClassifyId, ",")
  329. for _, v := range classifyIdArr {
  330. t, _ := strconv.Atoi(v)
  331. if t > 0 {
  332. classifyIds = append(classifyIds, t)
  333. }
  334. }
  335. if len(classifyIds) == 0 {
  336. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  337. resp.Paging = page
  338. br.Ret = 200
  339. br.Success = true
  340. br.Msg = "获取成功"
  341. return
  342. }
  343. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(classifyIds)))
  344. pars = append(pars, classifyIds)
  345. }
  346. if params.Frequency != "" {
  347. frequencyArr = strings.Split(params.Frequency, ",")
  348. if len(frequencyArr) == 0 {
  349. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  350. resp.Paging = page
  351. br.Ret = 200
  352. br.Success = true
  353. br.Msg = "获取成功"
  354. return
  355. }
  356. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().Frequency, utils.GetOrmInReplace(len(frequencyArr)))
  357. pars = append(pars, frequencyArr)
  358. }
  359. if params.SysAdminId != "" {
  360. adminIdArr := strings.Split(params.SysAdminId, ",")
  361. for _, v := range adminIdArr {
  362. t, _ := strconv.Atoi(v)
  363. if t > 0 {
  364. adminIds = append(adminIds, t)
  365. }
  366. }
  367. if len(adminIds) > 0 {
  368. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  369. resp.Paging = page
  370. br.Ret = 200
  371. br.Success = true
  372. br.Msg = "获取成功"
  373. return
  374. }
  375. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().SysUserId, utils.GetOrmInReplace(len(adminIds)))
  376. pars = append(pars, adminIds)
  377. }
  378. // 关键词空格拆分
  379. if params.Keywords != "" {
  380. indexCodeCol := indexOb.Cols().IndexCode
  381. indexNameCol := indexOb.Cols().IndexName
  382. keywordArr := strings.Split(params.Keywords, " ")
  383. if len(keywordArr) > 1 {
  384. sliceArr := make([]string, 0)
  385. sliceArr = append(sliceArr, fmt.Sprintf(` %s LIKE ? OR %s LIKE ? `, indexCodeCol, indexNameCol))
  386. pars = utils.GetLikeKeywordPars(pars, params.Keywords, 2)
  387. for _, v := range keywordArr {
  388. if v == ` ` || v == `` {
  389. continue
  390. }
  391. sliceArr = append(sliceArr, fmt.Sprintf(` %s LIKE ? OR %s LIKE ? `, indexCodeCol, indexNameCol))
  392. pars = utils.GetLikeKeywordPars(pars, v, 2)
  393. }
  394. cond += ` AND (` + strings.Join(sliceArr, " OR ") + `)`
  395. } else {
  396. cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ?)`, indexCodeCol, indexNameCol)
  397. pars = utils.GetLikeKeywordPars(pars, params.Keywords, 2)
  398. }
  399. // ES关键词搜索
  400. //_, list, e := elastic.SearchDataSourceIndex(utils.EsDataSourceIndexName, params.Keywords, utils.DATA_SOURCE_THS, utils.DATA_SUB_SOURCE_HIGH_FREQUENCY, []int{}, []int{}, []string{}, startSize, params.PageSize)
  401. //if e != nil {
  402. // br.Msg = "获取失败"
  403. // br.ErrMsg = fmt.Sprintf("ES-搜索高频数据指标失败, %v", e)
  404. // return
  405. //}
  406. //if len(list) == 0 {
  407. // page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  408. // resp.Paging = page
  409. // br.Ret = 200
  410. // br.Success = true
  411. // br.Msg = "获取成功"
  412. // return
  413. //}
  414. //var indexIds []int
  415. //for _, v := range list {
  416. // indexIds = append(indexIds, v.PrimaryId)
  417. //}
  418. //cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(indexIds)))
  419. //pars = append(pars, indexIds)
  420. }
  421. // 排序
  422. if params.SortField > 0 && params.SortType > 0 {
  423. fieldMap := map[int]string{1: indexOb.Cols().StartDate, 2: indexOb.Cols().EndDate, 3: indexOb.Cols().ModifyTime, 4: indexOb.Cols().LatestValue}
  424. typeMap := map[int]string{1: "ASC", 2: "DESC"}
  425. listOrder = fmt.Sprintf("%s %s", fieldMap[params.SortField], typeMap[params.SortType])
  426. }
  427. }
  428. // 列表总计
  429. total, e := indexOb.GetCountByCondition(cond, pars)
  430. if e != nil {
  431. br.Msg = "获取失败"
  432. br.ErrMsg = fmt.Sprintf("获取指标总数失败, %v", e)
  433. return
  434. }
  435. if total <= 0 {
  436. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  437. resp.Paging = page
  438. br.Data = resp
  439. br.Ret = 200
  440. br.Success = true
  441. br.Msg = "获取成功"
  442. return
  443. }
  444. items, e := indexOb.GetPageItemsByCondition(cond, pars, []string{}, listOrder, startSize, params.PageSize)
  445. if e != nil {
  446. br.Msg = "获取失败"
  447. br.ErrMsg = fmt.Sprintf("获取指标列表失败, %v", e)
  448. return
  449. }
  450. for _, v := range items {
  451. t := v.Format2Item()
  452. // 分类完整路径
  453. classify := classifyIdItem[v.BaseFromThsHfClassifyId]
  454. levelArr := make([]string, 0)
  455. if classify != nil && classify.LevelPath != "" {
  456. arr := strings.Split(classify.LevelPath, ",")
  457. for _, a := range arr {
  458. i, _ := strconv.Atoi(a)
  459. if classifyIdItem[i] != nil {
  460. if this.Lang == utils.EnLangVersion {
  461. levelArr = append(levelArr, classifyIdItem[i].ClassifyNameEn)
  462. } else {
  463. levelArr = append(levelArr, classifyIdItem[i].ClassifyName)
  464. }
  465. }
  466. }
  467. }
  468. t.ClassifyPath = strings.Join(levelArr, "/")
  469. resp.List = append(resp.List, t)
  470. }
  471. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  472. resp.Paging = page
  473. br.Data = resp
  474. br.Ret = 200
  475. br.Success = true
  476. br.Msg = "获取成功"
  477. }
  478. // Add
  479. // @Title 新增指标
  480. // @Description 新增指标
  481. // @Param request body data_manage.ThsHfAddEdbReq true "type json string"
  482. // @Success 200 string "操作成功"
  483. // @router /ths_hf/index/add [post]
  484. func (this *BaseFromThsHfController) Add() {
  485. br := new(models.BaseResponse).Init()
  486. defer func() {
  487. if br.ErrMsg == "" {
  488. br.IsSendEmail = false
  489. }
  490. this.Data["json"] = br
  491. this.ServeJSON()
  492. }()
  493. sysUser := this.SysUser
  494. if sysUser == nil {
  495. br.Msg = "请登录"
  496. br.ErrMsg = "请登录,SysUser Is Empty"
  497. br.Ret = 408
  498. return
  499. }
  500. var params data_manage.ThsHfAddEdbReq
  501. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  502. br.Msg = "参数解析异常"
  503. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  504. return
  505. }
  506. if params.StartTime == "" {
  507. br.Msg = "请选择起始时间"
  508. return
  509. }
  510. _, e := time.ParseInLocation(utils.FormatDateTime, params.StartTime, time.Local)
  511. if e != nil {
  512. br.Msg = "起始时间格式有误"
  513. br.ErrMsg = fmt.Sprintf("起始时间格式有误, %v", e)
  514. return
  515. }
  516. if params.EndTime != "" {
  517. _, e := time.ParseInLocation(utils.FormatDateTime, params.EndTime, time.Local)
  518. if e != nil {
  519. br.Msg = "截止时间格式有误"
  520. br.ErrMsg = fmt.Sprintf("截止时间格式有误, %v", e)
  521. return
  522. }
  523. }
  524. if !utils.InArrayByInt(data_manage.ThsHfPeriodArr, params.Interval) {
  525. br.Msg = "时间周期有误"
  526. br.ErrMsg = fmt.Sprintf("时间周期有误, Interval: %d", params.Interval)
  527. return
  528. }
  529. if params.CPS != "" && !utils.InArrayByStr(data_manage.ThsHfCPSArr, params.CPS) {
  530. br.Msg = "复权方式有误"
  531. br.ErrMsg = fmt.Sprintf("复权方式有误, CPS: %s", params.CPS)
  532. return
  533. }
  534. if params.BaseDate != "" {
  535. _, e = time.ParseInLocation(utils.FormatDate, params.BaseDate, time.Local)
  536. if e != nil {
  537. br.Msg = "复权基点格式有误"
  538. br.ErrMsg = fmt.Sprintf("复权基点格式有误, %v", e)
  539. return
  540. }
  541. }
  542. if params.Fill != "" && !utils.InArrayByStr(data_manage.ThsHfFillArr, params.Fill) {
  543. br.Msg = "非交易间隔处理有误"
  544. br.ErrMsg = fmt.Sprintf("非交易间隔处理有误, Fill: %s", params.Fill)
  545. return
  546. }
  547. if len(params.IndexList) == 0 {
  548. br.Msg = "请选择指标"
  549. return
  550. }
  551. formExist := make(map[string]bool)
  552. indexNames := make([]string, 0)
  553. classifyIds := make([]int, 0)
  554. for _, v := range params.IndexList {
  555. if v.ClassifyId <= 0 {
  556. br.Msg = "请选择分类"
  557. return
  558. }
  559. if !utils.InArrayByInt(classifyIds, v.ClassifyId) {
  560. classifyIds = append(classifyIds, v.ClassifyId)
  561. }
  562. if v.Unit == "" {
  563. br.Msg = "请输入单位"
  564. return
  565. }
  566. v.IndexName = strings.TrimSpace(v.IndexName)
  567. if v.IndexName == "" {
  568. br.Msg = "请输入指标名称"
  569. return
  570. }
  571. if formExist[v.IndexName] {
  572. br.Msg = "指标名称重复, 请重新输入"
  573. return
  574. }
  575. formExist[v.IndexName] = true
  576. indexNames = append(indexNames, v.IndexName)
  577. }
  578. // 校验指标分类
  579. classifyOb := new(data_manage.BaseFromThsHfClassify)
  580. {
  581. cond := fmt.Sprintf(` AND %s IN (%s)`, classifyOb.Cols().PrimaryId, utils.GetOrmInReplace(len(classifyIds)))
  582. pars := make([]interface{}, 0)
  583. pars = append(pars, classifyIds)
  584. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName}, "")
  585. if e != nil {
  586. br.Msg = "操作失败"
  587. br.ErrMsg = fmt.Sprintf("查询分类失败, %v", e)
  588. return
  589. }
  590. classifyMap := make(map[int]*data_manage.BaseFromThsHfClassify)
  591. for _, v := range list {
  592. classifyMap[v.BaseFromThsHfClassifyId] = v
  593. }
  594. for _, v := range params.IndexList {
  595. t := classifyMap[v.ClassifyId]
  596. if t == nil {
  597. br.Msg = fmt.Sprintf("%s分类不存在, 请重新选择或刷新页面", t.ClassifyName)
  598. return
  599. }
  600. }
  601. }
  602. // 校验指标名称
  603. indexOb := new(data_manage.BaseFromThsHfIndex)
  604. {
  605. cond := fmt.Sprintf(` AND %s IN (%s)`, indexOb.Cols().IndexName, utils.GetOrmInReplace(len(indexNames)))
  606. pars := make([]interface{}, 0)
  607. pars = append(pars, indexNames)
  608. list, e := indexOb.GetItemsByCondition(cond, pars, []string{indexOb.Cols().IndexName}, "")
  609. if e != nil {
  610. br.Msg = "操作失败"
  611. br.ErrMsg = fmt.Sprintf("查询指标名称失败, %v", e)
  612. return
  613. }
  614. if len(list) > 0 {
  615. tips := "以下指标名称重复, 请重新输入: \n"
  616. for _, v := range list {
  617. tips += fmt.Sprintf("%s\n", v.IndexName)
  618. }
  619. br.Msg = tips
  620. return
  621. }
  622. }
  623. // 新增指标
  624. for _, v := range params.IndexList {
  625. var req data_manage.ThsHfBaseAddReq
  626. req.StartTime = params.StartTime
  627. req.EndTime = params.EndTime
  628. req.Interval = params.Interval
  629. req.Fill = params.Fill
  630. req.CPS = params.CPS
  631. req.BaseDate = params.BaseDate
  632. req.SysAdminId = sysUser.AdminId
  633. req.SysAdminName = sysUser.RealName
  634. req.ClassifyId = v.ClassifyId
  635. req.Unit = v.Unit
  636. req.IndexName = v.IndexName
  637. req.Frequency = v.Frequency
  638. req.StockCode = v.StockCode
  639. req.EdbCode = v.EdbCode
  640. _, e = data.BaseAddThsHf(req)
  641. if e != nil {
  642. utils.FileLog.Info(fmt.Sprintf("BaseAddThsHf err: %v", e))
  643. continue
  644. }
  645. }
  646. br.Ret = 200
  647. br.Success = true
  648. br.Msg = "操作成功"
  649. }
  650. // Edit
  651. // @Title 编辑指标
  652. // @Description 编辑指标
  653. // @Param request body data_manage.ThsHfIndexEditReq true "type json string"
  654. // @Success 200 string "操作成功"
  655. // @router /ths_hf/index/edit [post]
  656. func (this *BaseFromThsHfController) Edit() {
  657. br := new(models.BaseResponse).Init()
  658. defer func() {
  659. if br.ErrMsg == "" {
  660. br.IsSendEmail = false
  661. }
  662. this.Data["json"] = br
  663. this.ServeJSON()
  664. }()
  665. sysUser := this.SysUser
  666. if sysUser == nil {
  667. br.Msg = "请登录"
  668. br.ErrMsg = "请登录,SysUser Is Empty"
  669. br.Ret = 408
  670. return
  671. }
  672. var params data_manage.ThsHfIndexEditReq
  673. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  674. br.Msg = "参数解析异常"
  675. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  676. return
  677. }
  678. if params.IndexId <= 0 {
  679. br.Msg = "参数有误"
  680. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId)
  681. return
  682. }
  683. params.IndexName = strings.TrimSpace(params.IndexName)
  684. if params.IndexName == "" {
  685. br.Msg = "请输入指标名称"
  686. return
  687. }
  688. if params.ClassifyId <= 0 {
  689. br.Msg = "请选择分类"
  690. return
  691. }
  692. params.Unit = strings.TrimSpace(params.Unit)
  693. if params.Unit == "" {
  694. br.Msg = "请输入单位"
  695. return
  696. }
  697. indexOb := new(data_manage.BaseFromThsHfIndex)
  698. item, e := indexOb.GetItemById(params.IndexId)
  699. if e != nil {
  700. if e.Error() == utils.ErrNoRow() {
  701. br.Msg = "指标不存在, 请刷新页面"
  702. return
  703. }
  704. br.Msg = "操作失败"
  705. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  706. return
  707. }
  708. classifyOb := new(data_manage.BaseFromThsHfClassify)
  709. _, e = classifyOb.GetItemById(params.ClassifyId)
  710. if e != nil {
  711. br.Msg = "分类有误"
  712. br.ErrMsg = fmt.Sprintf("分类信息有误, %v", e)
  713. return
  714. }
  715. // 重名验证
  716. {
  717. cond := fmt.Sprintf(" AND %s = ? AND %s <> ?", indexOb.Cols().IndexName, indexOb.Cols().PrimaryId)
  718. pars := make([]interface{}, 0)
  719. pars = append(pars, params.IndexName, params.IndexId)
  720. count, e := indexOb.GetCountByCondition(cond, pars)
  721. if e != nil {
  722. br.Msg = "操作失败"
  723. br.ErrMsg = fmt.Sprintf("获取重名指标失败, %v", e)
  724. return
  725. }
  726. if count > 0 {
  727. br.Msg = "指标名称已存在, 请重新输入"
  728. return
  729. }
  730. }
  731. item.IndexName = params.IndexName
  732. item.BaseFromThsHfClassifyId = params.ClassifyId
  733. item.Unit = params.Unit
  734. item.ModifyTime = time.Now().Local()
  735. updateCols := []string{item.Cols().IndexName, item.Cols().BaseFromThsHfClassifyId, item.Cols().Unit, item.Cols().ModifyTime}
  736. if e = item.Update(updateCols); e != nil {
  737. br.Msg = "操作失败"
  738. br.ErrMsg = fmt.Sprintf("更新指标信息失败, %v", e)
  739. return
  740. }
  741. br.Ret = 200
  742. br.Success = true
  743. br.Msg = "操作成功"
  744. }
  745. // Detail
  746. // @Title 详情
  747. // @Description 详情
  748. // @Param IndexId query int true "指标ID"
  749. // @Param DataDate query string false "数据日期"
  750. // @Success 200 {object} data_manage.ThsHfSearchEdbResp
  751. // @router /ths_hf/index/detail [get]
  752. func (this *BaseFromThsHfController) Detail() {
  753. br := new(models.BaseResponse).Init()
  754. defer func() {
  755. if br.ErrMsg == "" {
  756. br.IsSendEmail = false
  757. }
  758. this.Data["json"] = br
  759. this.ServeJSON()
  760. }()
  761. sysUser := this.SysUser
  762. if sysUser == nil {
  763. br.Msg = "请登录"
  764. br.ErrMsg = "请登录,SysUser Is Empty"
  765. br.Ret = 408
  766. return
  767. }
  768. indexId, _ := this.GetInt("IndexId")
  769. if indexId <= 0 {
  770. br.Msg = "参数有误"
  771. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", indexId)
  772. return
  773. }
  774. dataDate := this.GetString("DataDate")
  775. if dataDate != "" {
  776. _, e := time.Parse(utils.FormatDate, dataDate)
  777. if e != nil {
  778. br.Msg = "数据日期格式有误"
  779. br.ErrMsg = fmt.Sprintf("数据日期格式有误, DataDate: %s", dataDate)
  780. return
  781. }
  782. }
  783. indexOb := new(data_manage.BaseFromThsHfIndex)
  784. item, e := indexOb.GetItemById(indexId)
  785. if e != nil {
  786. if e.Error() == utils.ErrNoRow() {
  787. br.Msg = "指标不存在, 请刷新页面"
  788. return
  789. }
  790. br.Msg = "获取失败"
  791. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  792. return
  793. }
  794. indexItem := item.Format2Item()
  795. if item.EndDate.IsZero() {
  796. item.EndDate = time.Now()
  797. }
  798. // 默认取有数据的最新日期
  799. if dataDate == "" {
  800. dataDate = item.EndDate.Format(utils.FormatDate)
  801. }
  802. startTime := fmt.Sprintf("%s 00:00:00", dataDate)
  803. endTime := fmt.Sprintf("%s 23:59:59", dataDate)
  804. // 获取数据
  805. dataList, e := data.GetThsHfBaseIndexData(indexItem.IndexCode, startTime, endTime)
  806. if e != nil {
  807. br.Msg = "获取失败"
  808. br.ErrMsg = fmt.Sprintf("获取指标数据失败, %v", e)
  809. return
  810. }
  811. type DetailResp struct {
  812. Index *data_manage.BaseFromThsHfIndexItem
  813. DataDate string `description:"数据日期"`
  814. DataList []*data_manage.BaseFromThsHfDataItem
  815. }
  816. resp := new(DetailResp)
  817. resp.Index = indexItem
  818. resp.DataDate = dataDate
  819. resp.DataList = dataList
  820. br.Data = resp
  821. br.Ret = 200
  822. br.Success = true
  823. br.Msg = "获取成功"
  824. }
  825. // Refresh
  826. // @Title 刷新指标
  827. // @Description 刷新指标
  828. // @Param request body data_manage.ThsHfIndexOptReq true "type json string"
  829. // @Success 200 string "操作成功"
  830. // @router /ths_hf/index/refresh [post]
  831. func (this *BaseFromThsHfController) Refresh() {
  832. br := new(models.BaseResponse).Init()
  833. defer func() {
  834. if br.ErrMsg == "" {
  835. br.IsSendEmail = false
  836. }
  837. this.Data["json"] = br
  838. this.ServeJSON()
  839. }()
  840. sysUser := this.SysUser
  841. if sysUser == nil {
  842. br.Msg = "请登录"
  843. br.ErrMsg = "请登录,SysUser Is Empty"
  844. br.Ret = 408
  845. return
  846. }
  847. var params data_manage.ThsHfIndexOptReq
  848. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  849. br.Msg = "参数解析异常"
  850. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  851. return
  852. }
  853. if params.IndexId <= 0 {
  854. br.Msg = "参数有误"
  855. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId)
  856. return
  857. }
  858. indexOb := new(data_manage.BaseFromThsHfIndex)
  859. _, e := indexOb.GetItemById(params.IndexId)
  860. if e != nil {
  861. if e.Error() == utils.ErrNoRow() {
  862. br.Msg = "指标不存在, 请刷新页面"
  863. return
  864. }
  865. br.Msg = "操作失败"
  866. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  867. return
  868. }
  869. // 指标刷新逻辑
  870. indexIds := []int{params.IndexId}
  871. _, e = data.RefreshBaseThsHfIndex(indexIds, 1)
  872. if e != nil {
  873. br.Msg = "刷新失败"
  874. br.ErrMsg = fmt.Sprintf("刷新指标失败, %v", e)
  875. return
  876. }
  877. br.Ret = 200
  878. br.Success = true
  879. br.Msg = "操作成功"
  880. }
  881. // Remove
  882. // @Title 删除指标
  883. // @Description 删除指标
  884. // @Param request body data_manage.ThsHfIndexOptReq true "type json string"
  885. // @Success 200 string "操作成功"
  886. // @router /ths_hf/index/remove [post]
  887. func (this *BaseFromThsHfController) Remove() {
  888. br := new(models.BaseResponse).Init()
  889. defer func() {
  890. if br.ErrMsg == "" {
  891. br.IsSendEmail = false
  892. }
  893. this.Data["json"] = br
  894. this.ServeJSON()
  895. }()
  896. sysUser := this.SysUser
  897. if sysUser == nil {
  898. br.Msg = "请登录"
  899. br.ErrMsg = "请登录,SysUser Is Empty"
  900. br.Ret = 408
  901. return
  902. }
  903. var params data_manage.ThsHfIndexOptReq
  904. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  905. br.Msg = "参数解析异常"
  906. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  907. return
  908. }
  909. if params.IndexId <= 0 {
  910. br.Msg = "参数有误"
  911. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId)
  912. return
  913. }
  914. indexOb := new(data_manage.BaseFromThsHfIndex)
  915. item, e := indexOb.GetItemById(params.IndexId)
  916. if e != nil {
  917. if e.Error() == utils.ErrNoRow() {
  918. br.Ret = 200
  919. br.Success = true
  920. br.Msg = "操作成功"
  921. return
  922. }
  923. br.Msg = "操作失败"
  924. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  925. return
  926. }
  927. // 删除校验, 指标库存在不可删除
  928. edbMappingOb := new(data_manage.BaseFromEdbMapping)
  929. {
  930. cond := fmt.Sprintf(" AND %s = ?", edbMappingOb.Cols().BaseIndexCode)
  931. pars := make([]interface{}, 0)
  932. pars = append(pars, item.IndexCode)
  933. count, e := edbMappingOb.GetCountByCondition(cond, pars)
  934. if e != nil {
  935. br.Msg = "操作失败"
  936. br.ErrMsg = fmt.Sprintf("获取源指标关联失败, %v", e)
  937. return
  938. }
  939. if count > 0 {
  940. br.Msg = "指标已被引用, 不允许删除"
  941. return
  942. }
  943. }
  944. if e = item.Remove(); e != nil {
  945. br.Msg = "操作失败"
  946. br.ErrMsg = fmt.Sprintf("删除指标失败, %v", e)
  947. return
  948. }
  949. if utils.UseMongo {
  950. mogDataObj := mgo.BaseFromThsHfData{}
  951. if e = mogDataObj.RemoveMany(bson.M{"index_code": item.IndexCode}); e != nil {
  952. br.Msg = "操作失败"
  953. br.ErrMsg = fmt.Sprintf("删除指标数据失败-Mgo, %v", e)
  954. return
  955. }
  956. } else {
  957. dataOb := new(data_manage.BaseFromThsHfData)
  958. cond := fmt.Sprintf(" %s = ?", dataOb.Cols().BaseFromThsHfIndexId)
  959. pars := make([]interface{}, 0)
  960. pars = append(pars, params.IndexId)
  961. if e = dataOb.RemoveByCondition(cond, pars); e != nil {
  962. br.Msg = "操作失败"
  963. br.ErrMsg = fmt.Sprintf("删除指标数据失败, %v", e)
  964. return
  965. }
  966. }
  967. // ES标记删除
  968. go func() {
  969. indexItem := new(dataSourceModel.SearchDataSource)
  970. indexItem.PrimaryId = item.BaseFromThsHfIndexId
  971. indexItem.IndexName = item.IndexName
  972. indexItem.IndexCode = item.IndexCode
  973. indexItem.ClassifyId = item.BaseFromThsHfClassifyId
  974. indexItem.Source = utils.DATA_SOURCE_THS
  975. indexItem.SubSource = utils.DATA_SUB_SOURCE_HIGH_FREQUENCY
  976. indexItem.SourceName = "同花顺高频"
  977. indexItem.IsDeleted = 1
  978. indexItem.CreateTime = utils.TimeTransferString(utils.FormatDateTime, item.CreateTime)
  979. indexItem.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, item.ModifyTime)
  980. docId := fmt.Sprintf("%d-%d", indexItem.Source, indexItem.PrimaryId)
  981. if e := elastic.EsAddOrEditDataSourceIndex(utils.EsDataSourceIndexName, docId, indexItem); e != nil {
  982. utils.FileLog.Info("同花顺高频-标记删除es失败, %v", e)
  983. return
  984. }
  985. }()
  986. br.Ret = 200
  987. br.Success = true
  988. br.Msg = "操作成功"
  989. }
  990. // ListChoice
  991. // @Title 列表选择
  992. // @Description 列表选择
  993. // @Param request body data_manage.ThsHfIndexMultiOptReq true "type json string"
  994. // @Success 200 string "获取成功"
  995. // @router /ths_hf/index/list_choice [get]
  996. func (this *BaseFromThsHfController) ListChoice() {
  997. br := new(models.BaseResponse).Init()
  998. defer func() {
  999. if br.ErrMsg == "" {
  1000. br.IsSendEmail = false
  1001. }
  1002. this.Data["json"] = br
  1003. this.ServeJSON()
  1004. }()
  1005. sysUser := this.SysUser
  1006. if sysUser == nil {
  1007. br.Msg = "请登录"
  1008. br.ErrMsg = "请登录,SysUser Is Empty"
  1009. br.Ret = 408
  1010. return
  1011. }
  1012. var params data_manage.ThsHfIndexListChoiceReq
  1013. if e := this.ParseForm(&params); e != nil {
  1014. br.Msg = "参数解析失败"
  1015. br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e)
  1016. return
  1017. }
  1018. var listIds []int
  1019. if params.ListIds != "" {
  1020. strArr := strings.Split(params.ListIds, ",")
  1021. for _, v := range strArr {
  1022. t, _ := strconv.Atoi(v)
  1023. if t > 0 {
  1024. listIds = append(listIds, t)
  1025. }
  1026. }
  1027. }
  1028. var (
  1029. cond string
  1030. pars []interface{}
  1031. )
  1032. indexOb := new(data_manage.BaseFromThsHfIndex)
  1033. resp := make([]*data_manage.ThsHfIndexListChoiceItem, 0)
  1034. // 非列表全选
  1035. if !params.SelectAll {
  1036. if len(listIds) > 0 {
  1037. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(listIds)))
  1038. pars = append(pars, listIds)
  1039. }
  1040. }
  1041. // 列表全选, 根据条件筛选列表并过滤params.ListIds中选择的ID
  1042. if params.SelectAll {
  1043. // 查询所有分类-分类查询用
  1044. classifyOb := new(data_manage.BaseFromThsHfClassify)
  1045. classifies := make([]*data_manage.BaseFromThsHfClassify, 0)
  1046. classifyIdItem := make(map[int]*data_manage.BaseFromThsHfClassify)
  1047. {
  1048. list, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName, classifyOb.Cols().ClassifyNameEn, classifyOb.Cols().LevelPath}, "")
  1049. if e != nil {
  1050. br.Msg = "获取失败"
  1051. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  1052. return
  1053. }
  1054. for _, v := range list {
  1055. classifyIdItem[v.BaseFromThsHfClassifyId] = v
  1056. }
  1057. classifies = list
  1058. }
  1059. // 筛选项
  1060. if params.ClassifyId != "" {
  1061. classifyIdArr := strings.Split(params.ClassifyId, ",")
  1062. classifyIds := make([]int, 0)
  1063. for _, v := range classifyIdArr {
  1064. t, _ := strconv.Atoi(v)
  1065. if t > 0 {
  1066. classifyIds = append(classifyIds, t)
  1067. }
  1068. }
  1069. // 不包含子分类
  1070. if len(classifyIds) > 0 && !params.IncludeChild {
  1071. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(classifyIds)))
  1072. pars = append(pars, classifyIds)
  1073. }
  1074. // 包含子分类
  1075. if len(classifyIds) > 0 && params.IncludeChild {
  1076. queryClassifyIds := make([]int, 0)
  1077. queryClassifyExist := make(map[int]bool)
  1078. for _, v := range classifyIds {
  1079. // 遍历所有分类从LevelPath中找含有查询分类ID的...=_=!
  1080. for _, cv := range classifies {
  1081. if queryClassifyExist[cv.BaseFromThsHfClassifyId] {
  1082. continue
  1083. }
  1084. if cv.LevelPath == "" {
  1085. continue
  1086. }
  1087. strArr := strings.Split(cv.LevelPath, ",")
  1088. if len(strArr) == 0 {
  1089. continue
  1090. }
  1091. for _, sv := range strArr {
  1092. tv, _ := strconv.Atoi(sv)
  1093. if tv == v {
  1094. queryClassifyIds = append(queryClassifyIds, cv.BaseFromThsHfClassifyId)
  1095. queryClassifyExist[cv.BaseFromThsHfClassifyId] = true
  1096. break
  1097. }
  1098. }
  1099. }
  1100. }
  1101. if len(queryClassifyIds) == 0 {
  1102. br.Data = resp
  1103. br.Ret = 200
  1104. br.Success = true
  1105. br.Msg = "获取成功"
  1106. return
  1107. }
  1108. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(queryClassifyIds)))
  1109. pars = append(pars, queryClassifyIds)
  1110. }
  1111. }
  1112. if params.Frequency != "" {
  1113. frequencyArr := strings.Split(params.Frequency, ",")
  1114. if len(frequencyArr) > 0 {
  1115. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().Frequency, utils.GetOrmInReplace(len(frequencyArr)))
  1116. pars = append(pars, frequencyArr)
  1117. }
  1118. }
  1119. if params.SysAdminId != "" {
  1120. adminIdArr := strings.Split(params.SysAdminId, ",")
  1121. adminIds := make([]int, 0)
  1122. for _, v := range adminIdArr {
  1123. t, _ := strconv.Atoi(v)
  1124. if t > 0 {
  1125. adminIds = append(adminIds, t)
  1126. }
  1127. }
  1128. if len(adminIds) > 0 {
  1129. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().SysUserId, utils.GetOrmInReplace(len(adminIds)))
  1130. pars = append(pars, adminIds)
  1131. }
  1132. }
  1133. params.Keywords = strings.TrimSpace(params.Keywords)
  1134. if params.Keywords != "" {
  1135. cond += fmt.Sprintf(" AND (%s LIKE ? OR %s LIKE ?)", indexOb.Cols().IndexCode, indexOb.Cols().IndexName)
  1136. kw := fmt.Sprint("%", params.Keywords, "%")
  1137. pars = append(pars, kw, kw)
  1138. }
  1139. // 过滤掉选择的指标
  1140. if len(listIds) > 0 {
  1141. cond += fmt.Sprintf(" AND %s NOT IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(listIds)))
  1142. pars = append(pars, listIds)
  1143. }
  1144. }
  1145. fields := []string{indexOb.Cols().PrimaryId, indexOb.Cols().IndexCode, indexOb.Cols().IndexName}
  1146. list, e := indexOb.GetItemsByCondition(cond, pars, fields, "")
  1147. if e != nil {
  1148. br.Msg = "获取失败"
  1149. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  1150. return
  1151. }
  1152. if len(list) > 500 {
  1153. br.Msg = "选择指标超过500个, 请重新选择"
  1154. return
  1155. }
  1156. for _, v := range list {
  1157. resp = append(resp, &data_manage.ThsHfIndexListChoiceItem{
  1158. IndexId: v.BaseFromThsHfIndexId,
  1159. IndexCode: v.IndexCode,
  1160. IndexName: v.IndexName,
  1161. })
  1162. }
  1163. br.Data = resp
  1164. br.Ret = 200
  1165. br.Success = true
  1166. br.Msg = "获取成功"
  1167. }
  1168. // MultiOpt
  1169. // @Title 批量操作-移动分类/删除/刷新
  1170. // @Description 批量操作-移动分类/删除/刷新
  1171. // @Param request body data_manage.ThsHfIndexMultiOptReq true "type json string"
  1172. // @Success 200 string "操作成功"
  1173. // @router /ths_hf/index/multi_opt [post]
  1174. func (this *BaseFromThsHfController) MultiOpt() {
  1175. br := new(models.BaseResponse).Init()
  1176. defer func() {
  1177. if br.ErrMsg == "" {
  1178. br.IsSendEmail = false
  1179. }
  1180. this.Data["json"] = br
  1181. this.ServeJSON()
  1182. }()
  1183. sysUser := this.SysUser
  1184. if sysUser == nil {
  1185. br.Msg = "请登录"
  1186. br.ErrMsg = "请登录,SysUser Is Empty"
  1187. br.Ret = 408
  1188. return
  1189. }
  1190. var params data_manage.ThsHfIndexMultiOptReq
  1191. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  1192. br.Msg = "参数解析异常"
  1193. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1194. return
  1195. }
  1196. if !utils.InArrayByInt([]int{1, 2, 3}, params.OptType) {
  1197. br.Msg = "请选择操作类型"
  1198. return
  1199. }
  1200. if len(params.IndexIds) == 0 {
  1201. br.Msg = "请选择指标"
  1202. return
  1203. }
  1204. if params.OptType == 1 && params.MoveClassifyId <= 0 {
  1205. br.Msg = "请选择分类"
  1206. return
  1207. }
  1208. if params.OptType == 3 && !utils.InArrayByInt([]int{1, 2}, params.RefreshType) {
  1209. br.Msg = "请选择刷新方式"
  1210. return
  1211. }
  1212. resp := new(data_manage.ThsHfIndexMultiOptResp)
  1213. // 指标名称
  1214. indexOb := new(data_manage.BaseFromThsHfIndex)
  1215. indexMap := make(map[int]*data_manage.BaseFromThsHfIndex)
  1216. {
  1217. cond := fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(params.IndexIds)))
  1218. pars := make([]interface{}, 0)
  1219. pars = append(pars, params.IndexIds)
  1220. list, e := indexOb.GetItemsByCondition(cond, pars, []string{indexOb.Cols().PrimaryId, indexOb.Cols().IndexCode, indexOb.Cols().IndexName}, "")
  1221. if e != nil {
  1222. br.Msg = "删除失败"
  1223. br.ErrMsg = fmt.Sprintf("获取源指标列表失败, %v", e)
  1224. return
  1225. }
  1226. for _, v := range list {
  1227. indexMap[v.BaseFromThsHfIndexId] = v
  1228. }
  1229. }
  1230. // 批量移动
  1231. if params.OptType == 1 {
  1232. if e := indexOb.UpdateClassifyMulti(params.IndexIds, params.MoveClassifyId); e != nil {
  1233. br.Msg = "移动失败"
  1234. br.ErrMsg = fmt.Sprintf("批量移动分类失败, %v", e)
  1235. return
  1236. }
  1237. }
  1238. // 批量删除
  1239. if params.OptType == 2 {
  1240. // 被引用的指标需要提示出来, 未被引用的直接删除
  1241. usedIndexIds := make([]int, 0)
  1242. {
  1243. edbMappingOb := new(data_manage.BaseFromEdbMapping)
  1244. cond := fmt.Sprintf(" AND %s IN (%s)", edbMappingOb.Cols().BaseFromIndexId, utils.GetOrmInReplace(len(params.IndexIds)))
  1245. pars := make([]interface{}, 0)
  1246. pars = append(pars, params.IndexIds)
  1247. list, e := edbMappingOb.GetItemsByCondition(cond, pars, []string{}, "")
  1248. if e != nil {
  1249. br.Msg = "删除失败"
  1250. br.ErrMsg = fmt.Sprintf("获取源指标关联失败, %v", e)
  1251. return
  1252. }
  1253. for _, v := range list {
  1254. if !utils.InArrayByInt(usedIndexIds, v.BaseFromIndexId) {
  1255. usedIndexIds = append(usedIndexIds, v.BaseFromIndexId)
  1256. }
  1257. }
  1258. }
  1259. // 可删除的指标
  1260. removeIndexIds := params.IndexIds
  1261. if len(usedIndexIds) > 0 {
  1262. removeIndexIds = utils.MinusInt(params.IndexIds, usedIndexIds)
  1263. // 标记不允许删除的
  1264. for _, v := range usedIndexIds {
  1265. t := indexMap[v]
  1266. if t == nil {
  1267. continue
  1268. }
  1269. resp.Fail = append(resp.Fail, &data_manage.ThsHfIndexBaseInfo{
  1270. IndexId: v,
  1271. IndexCode: t.IndexCode,
  1272. IndexName: t.IndexName,
  1273. })
  1274. }
  1275. }
  1276. if len(removeIndexIds) > 0 {
  1277. if e := indexOb.MultiRemove(removeIndexIds); e != nil {
  1278. br.Msg = "删除失败"
  1279. br.ErrMsg = fmt.Sprintf("批量删除指标失败, %v", e)
  1280. return
  1281. }
  1282. if utils.UseMongo {
  1283. mogDataObj := mgo.BaseFromThsHfData{}
  1284. for _, v := range removeIndexIds {
  1285. if e := mogDataObj.RemoveMany(bson.M{"base_from_ths_hf_index_id": v}); e != nil {
  1286. br.Msg = "操作失败"
  1287. br.ErrMsg = fmt.Sprintf("批量删除指标数据失败-Mgo, %v", e)
  1288. return
  1289. }
  1290. }
  1291. } else {
  1292. dataOb := new(data_manage.BaseFromThsHfData)
  1293. cond := fmt.Sprintf(" %s IN (%s)", dataOb.Cols().BaseFromThsHfIndexId, utils.GetOrmInReplace(len(removeIndexIds)))
  1294. pars := make([]interface{}, 0)
  1295. pars = append(pars, removeIndexIds)
  1296. if e := dataOb.RemoveByCondition(cond, pars); e != nil {
  1297. br.Msg = "删除失败"
  1298. br.ErrMsg = fmt.Sprintf("批量删除指标数据失败, %v", e)
  1299. return
  1300. }
  1301. }
  1302. // 标记删除成功的
  1303. for _, v := range removeIndexIds {
  1304. t := indexMap[v]
  1305. if t == nil {
  1306. continue
  1307. }
  1308. resp.Success = append(resp.Success, &data_manage.ThsHfIndexBaseInfo{
  1309. IndexId: v,
  1310. IndexCode: t.IndexCode,
  1311. IndexName: t.IndexName,
  1312. })
  1313. }
  1314. }
  1315. }
  1316. // 批量刷新
  1317. if params.OptType == 3 {
  1318. isAsync, e := data.RefreshBaseThsHfIndex(params.IndexIds, params.RefreshType)
  1319. if e != nil {
  1320. br.Msg = "刷新失败"
  1321. br.ErrMsg = fmt.Sprintf("批量刷新指标失败, %v", e)
  1322. return
  1323. }
  1324. if isAsync {
  1325. br.Ret = 200
  1326. br.Success = true
  1327. br.Msg = "操作成功, 刷新指标较多, 请10分钟后查看"
  1328. return
  1329. }
  1330. }
  1331. br.Data = resp
  1332. br.Ret = 200
  1333. br.Success = true
  1334. br.Msg = "操作成功"
  1335. }
  1336. // Save2EdbPre
  1337. // @Title 批量新增指标库-前置
  1338. // @Description 批量新增指标库-前置
  1339. // @Param request body data_manage.ThsHfIndexMultiSave2EdbReq true "type json string"
  1340. // @Success 200 string "操作成功"
  1341. // @router /ths_hf/index/save2edb_pre [post]
  1342. func (this *BaseFromThsHfController) Save2EdbPre() {
  1343. br := new(models.BaseResponse).Init()
  1344. defer func() {
  1345. if br.ErrMsg == "" {
  1346. br.IsSendEmail = false
  1347. }
  1348. this.Data["json"] = br
  1349. this.ServeJSON()
  1350. }()
  1351. sysUser := this.SysUser
  1352. if sysUser == nil {
  1353. br.Msg = "请登录"
  1354. br.ErrMsg = "请登录,SysUser Is Empty"
  1355. br.Ret = 408
  1356. return
  1357. }
  1358. var params data_manage.ThsHfIndexMultiSave2EdbPreReq
  1359. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  1360. br.Msg = "参数解析异常"
  1361. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1362. return
  1363. }
  1364. if len(params.IndexIds) == 0 {
  1365. br.Msg = "请选择指标"
  1366. return
  1367. }
  1368. convertRule := params.ConvertRule
  1369. if convertRule.ConvertType != 1 && convertRule.ConvertType != 2 {
  1370. br.Msg = "请选择数据转换方式"
  1371. return
  1372. }
  1373. // 生成的指标名称后缀
  1374. var (
  1375. suffixName string
  1376. startTimeCn string
  1377. endTimeCn string
  1378. calculateCn string
  1379. )
  1380. dayMap := map[int]string{1: "当日", 2: "前日"}
  1381. calculateMap := map[int]string{1: "均值", 2: "最大值", 3: "最小值"}
  1382. if convertRule.ConvertType == 1 {
  1383. if convertRule.ConvertFixed.FixedDay != 1 && convertRule.ConvertFixed.FixedDay != 2 {
  1384. br.Msg = "请选择指定时间"
  1385. return
  1386. }
  1387. if convertRule.ConvertFixed.FixedTime == "" {
  1388. br.Msg = "请选择指定时间"
  1389. return
  1390. }
  1391. timePrefix := time.Now().Local().Format(utils.FormatDate)
  1392. st := fmt.Sprintf("%s %s", timePrefix, convertRule.ConvertFixed.FixedTime)
  1393. startTime, e := time.Parse(utils.FormatDateTime, st)
  1394. if e != nil {
  1395. br.Msg = "指定时间格式有误"
  1396. return
  1397. }
  1398. startTimeCn = fmt.Sprintf("%s%s", dayMap[convertRule.ConvertFixed.FixedDay], startTime.Format("15点04"))
  1399. }
  1400. if convertRule.ConvertType == 2 {
  1401. if convertRule.ConvertArea.StartDay != 1 && convertRule.ConvertArea.StartDay != 2 {
  1402. br.Msg = "请选择起始时间"
  1403. return
  1404. }
  1405. if convertRule.ConvertArea.StartTime == "" {
  1406. br.Msg = "请选择起始时间"
  1407. return
  1408. }
  1409. var startTimePre string
  1410. if convertRule.ConvertArea.StartDay == 1 {
  1411. startTimePre = time.Now().Local().Format(utils.FormatDate)
  1412. }
  1413. if convertRule.ConvertArea.StartDay == 2 {
  1414. startTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1415. }
  1416. st := fmt.Sprintf("%s %s", startTimePre, convertRule.ConvertArea.StartTime)
  1417. startTime, e := time.Parse(utils.FormatDateTime, st)
  1418. if e != nil {
  1419. br.Msg = "起始时间格式有误"
  1420. return
  1421. }
  1422. startTimeCn = fmt.Sprintf("%s%s", dayMap[convertRule.ConvertArea.StartDay], startTime.Format("15点04"))
  1423. if convertRule.ConvertArea.EndDay != 1 && convertRule.ConvertArea.EndDay != 2 {
  1424. br.Msg = "请选择截止时间"
  1425. return
  1426. }
  1427. if convertRule.ConvertArea.EndTime == "" {
  1428. br.Msg = "请选择截止时间"
  1429. return
  1430. }
  1431. var endTimePre string
  1432. if convertRule.ConvertArea.EndDay == 1 {
  1433. endTimePre = time.Now().Local().Format(utils.FormatDate)
  1434. }
  1435. if convertRule.ConvertArea.EndDay == 2 {
  1436. endTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1437. }
  1438. ed := fmt.Sprintf("%s %s", endTimePre, convertRule.ConvertArea.EndTime)
  1439. endTime, e := time.Parse(utils.FormatDateTime, ed)
  1440. if e != nil {
  1441. br.Msg = "截止时间格式有误"
  1442. return
  1443. }
  1444. endTimeCn = fmt.Sprintf("至%s%s", dayMap[convertRule.ConvertArea.EndDay], endTime.Format("15点04"))
  1445. if startTime.After(endTime) {
  1446. br.Msg = "起始日期不可早于截止日期"
  1447. return
  1448. }
  1449. calculateCn = calculateMap[convertRule.ConvertArea.CalculateType]
  1450. }
  1451. suffixName = fmt.Sprint(startTimeCn, endTimeCn, calculateCn)
  1452. // 查询指标
  1453. indexOb := new(data_manage.BaseFromThsHfIndex)
  1454. cond := fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(params.IndexIds)))
  1455. pars := make([]interface{}, 0)
  1456. pars = append(pars, params.IndexIds)
  1457. list, e := indexOb.GetItemsByCondition(cond, pars, []string{}, "")
  1458. if e != nil {
  1459. br.Msg = "获取失败"
  1460. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  1461. return
  1462. }
  1463. resp := make([]*data_manage.ThsHfIndexMultiSave2EdbPreItem, 0)
  1464. for _, v := range list {
  1465. t := new(data_manage.ThsHfIndexMultiSave2EdbPreItem)
  1466. t.IndexId = v.BaseFromThsHfIndexId
  1467. t.IndexCode = v.IndexCode
  1468. t.IndexName = v.IndexName
  1469. edbCn := data_manage.ThsHfEdbCodeCn[v.Indicator]
  1470. if edbCn == "" {
  1471. edbCn = v.Indicator
  1472. }
  1473. t.NewIndexName = fmt.Sprint(v.StockCode, v.Frequency, edbCn, suffixName)
  1474. t.StockCode = v.StockCode
  1475. t.EdbCode = v.Indicator
  1476. t.Unit = v.Unit
  1477. t.Frequency = v.Frequency
  1478. t.NewFrequency = "日度"
  1479. resp = append(resp, t)
  1480. }
  1481. br.Data = resp
  1482. br.Ret = 200
  1483. br.Success = true
  1484. br.Msg = "获取成功"
  1485. }
  1486. // Save2Edb
  1487. // @Title 批量新增指标库
  1488. // @Description 批量新增指标库
  1489. // @Param request body data_manage.ThsHfIndexMultiSave2EdbReq true "type json string"
  1490. // @Success 200 string "操作成功"
  1491. // @router /ths_hf/index/save2edb [post]
  1492. func (this *BaseFromThsHfController) Save2Edb() {
  1493. br := new(models.BaseResponse).Init()
  1494. defer func() {
  1495. if br.ErrMsg == "" {
  1496. br.IsSendEmail = false
  1497. }
  1498. this.Data["json"] = br
  1499. this.ServeJSON()
  1500. }()
  1501. sysUser := this.SysUser
  1502. if sysUser == nil {
  1503. br.Msg = "请登录"
  1504. br.ErrMsg = "请登录,SysUser Is Empty"
  1505. br.Ret = 408
  1506. return
  1507. }
  1508. var params data_manage.ThsHfIndexMultiSave2EdbReq
  1509. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  1510. br.Msg = "参数解析异常"
  1511. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1512. return
  1513. }
  1514. if len(params.NewIndexes) == 0 {
  1515. br.Msg = "请选择指标"
  1516. return
  1517. }
  1518. convertRule := params.ConvertRule
  1519. if convertRule.ConvertType != 1 && convertRule.ConvertType != 2 {
  1520. br.Msg = "请选择数据转换方式"
  1521. return
  1522. }
  1523. if convertRule.ConvertType == 1 {
  1524. if convertRule.ConvertFixed.FixedDay != 1 && convertRule.ConvertFixed.FixedDay != 2 {
  1525. br.Msg = "请选择指定时间"
  1526. return
  1527. }
  1528. if convertRule.ConvertFixed.FixedTime == "" {
  1529. br.Msg = "请选择指定时间"
  1530. return
  1531. }
  1532. timePrefix := time.Now().Local().Format(utils.FormatDate)
  1533. st := fmt.Sprintf("%s %s", timePrefix, convertRule.ConvertFixed.FixedTime)
  1534. _, e := time.Parse(utils.FormatDateTime, st)
  1535. if e != nil {
  1536. br.Msg = "指定时间格式有误"
  1537. return
  1538. }
  1539. }
  1540. if convertRule.ConvertType == 2 {
  1541. if convertRule.ConvertArea.StartDay != 1 && convertRule.ConvertArea.StartDay != 2 {
  1542. br.Msg = "请选择起始时间"
  1543. return
  1544. }
  1545. if convertRule.ConvertArea.StartTime == "" {
  1546. br.Msg = "请选择起始时间"
  1547. return
  1548. }
  1549. var startTimePre string
  1550. if convertRule.ConvertArea.StartDay == 1 {
  1551. startTimePre = time.Now().Local().Format(utils.FormatDate)
  1552. }
  1553. if convertRule.ConvertArea.StartDay == 2 {
  1554. startTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1555. }
  1556. st := fmt.Sprintf("%s %s", startTimePre, convertRule.ConvertArea.StartTime)
  1557. startTime, e := time.Parse(utils.FormatDateTime, st)
  1558. if e != nil {
  1559. br.Msg = "起始时间格式有误"
  1560. return
  1561. }
  1562. if convertRule.ConvertArea.EndDay != 1 && convertRule.ConvertArea.EndDay != 2 {
  1563. br.Msg = "请选择截止时间"
  1564. return
  1565. }
  1566. if convertRule.ConvertArea.EndTime == "" {
  1567. br.Msg = "请选择截止时间"
  1568. return
  1569. }
  1570. var endTimePre string
  1571. if convertRule.ConvertArea.EndDay == 1 {
  1572. endTimePre = time.Now().Local().Format(utils.FormatDate)
  1573. }
  1574. if convertRule.ConvertArea.EndDay == 2 {
  1575. endTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1576. }
  1577. ed := fmt.Sprintf("%s %s", endTimePre, convertRule.ConvertArea.EndTime)
  1578. endTime, e := time.Parse(utils.FormatDateTime, ed)
  1579. if e != nil {
  1580. br.Msg = "截止时间格式有误"
  1581. return
  1582. }
  1583. if startTime.After(endTime) {
  1584. br.Msg = "起始日期不可早于截止日期"
  1585. return
  1586. }
  1587. }
  1588. resp := new(data_manage.ThsHfIndexMultiSave2EdbResp)
  1589. // 判断指标名称是否重复
  1590. edbNameExist := make(map[string]bool)
  1591. {
  1592. items, e := data_manage.GetEdbInfoFieldList(``, make([]interface{}, 0), []string{"edb_name"})
  1593. if e != nil {
  1594. br.Msg = "操作失败"
  1595. br.ErrMsg = fmt.Sprintf("获取指标名称列表失败, %v", e)
  1596. return
  1597. }
  1598. for _, v := range items {
  1599. edbNameExist[v.EdbName] = true
  1600. }
  1601. }
  1602. for _, v := range params.NewIndexes {
  1603. if edbNameExist[v.NewIndexName] {
  1604. resp.Exist = append(resp.Exist, v)
  1605. }
  1606. }
  1607. if len(resp.Exist) > 0 {
  1608. br.Data = resp
  1609. br.Ret = 200
  1610. br.Success = true
  1611. br.Msg = "含重复指标名称"
  1612. return
  1613. }
  1614. // 请求指标库新增
  1615. for _, v := range params.NewIndexes {
  1616. var r data_manage.ThsHfIndexMultiSave2EdbLibReq
  1617. r.ConvertRule = convertRule
  1618. v.SysAdminId = sysUser.AdminId
  1619. v.SysAdminName = sysUser.RealName
  1620. r.NewIndex = v
  1621. b, e := json.Marshal(r)
  1622. if e != nil {
  1623. v.Tips = "新增失败"
  1624. v.ErrMsg = e.Error()
  1625. resp.Fail = append(resp.Fail, v)
  1626. continue
  1627. }
  1628. res, e := data.AddBaseEdbInfo(string(b), utils.DATA_SOURCE_THS, utils.DATA_SUB_SOURCE_HIGH_FREQUENCY, this.Lang)
  1629. if e != nil {
  1630. v.Tips = "新增失败"
  1631. v.ErrMsg = fmt.Sprintf("AddBaseEdbInfo, err: %v", e)
  1632. resp.Fail = append(resp.Fail, v)
  1633. continue
  1634. }
  1635. if res.Ret != 200 {
  1636. v.Tips = res.Msg
  1637. v.ErrMsg = fmt.Sprintf("AddBaseEdbInfo, Ret: %d, ErrMsg: %s", res.Ret, res.ErrMsg)
  1638. resp.Fail = append(resp.Fail, v)
  1639. continue
  1640. }
  1641. v.Tips = "新增成功"
  1642. resp.Success = append(resp.Success, v)
  1643. }
  1644. br.Data = resp
  1645. br.Ret = 200
  1646. br.Success = true
  1647. br.Msg = "操作成功"
  1648. }