base_from_ths_hf.go 47 KB

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