base_from_ths_hf.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. // 不包含子分类
  320. if len(classifyIds) > 0 && !params.IncludeChild {
  321. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(classifyIds)))
  322. pars = append(pars, classifyIds)
  323. }
  324. // 包含子分类
  325. if len(classifyIds) > 0 && params.IncludeChild {
  326. queryClassifyIds := make([]int, 0)
  327. queryClassifyExist := make(map[int]bool)
  328. for _, v := range classifyIds {
  329. // 遍历所有分类从LevelPath中找含有查询分类ID的...=_=!
  330. for _, cv := range classifies {
  331. if queryClassifyExist[cv.BaseFromThsHfClassifyId] {
  332. continue
  333. }
  334. if cv.LevelPath == "" {
  335. continue
  336. }
  337. strArr := strings.Split(cv.LevelPath, ",")
  338. if len(strArr) == 0 {
  339. continue
  340. }
  341. for _, sv := range strArr {
  342. tv, _ := strconv.Atoi(sv)
  343. if tv == v {
  344. queryClassifyIds = append(queryClassifyIds, cv.BaseFromThsHfClassifyId)
  345. queryClassifyExist[cv.BaseFromThsHfClassifyId] = true
  346. break
  347. }
  348. }
  349. }
  350. }
  351. if len(queryClassifyIds) == 0 {
  352. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  353. resp.Paging = page
  354. br.Ret = 200
  355. br.Success = true
  356. br.Msg = "获取成功"
  357. return
  358. }
  359. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(queryClassifyIds)))
  360. pars = append(pars, queryClassifyIds)
  361. }
  362. }
  363. if params.Frequency != "" {
  364. frequencyArr := strings.Split(params.Frequency, ",")
  365. if len(frequencyArr) > 0 {
  366. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().Frequency, utils.GetOrmInReplace(len(frequencyArr)))
  367. pars = append(pars, frequencyArr)
  368. }
  369. }
  370. if params.SysAdminId != "" {
  371. adminIdArr := strings.Split(params.SysAdminId, ",")
  372. adminIds := make([]int, 0)
  373. for _, v := range adminIdArr {
  374. t, _ := strconv.Atoi(v)
  375. if t > 0 {
  376. adminIds = append(adminIds, t)
  377. }
  378. }
  379. if len(adminIds) > 0 {
  380. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().SysUserId, utils.GetOrmInReplace(len(adminIds)))
  381. pars = append(pars, adminIds)
  382. }
  383. }
  384. params.Keywords = strings.TrimSpace(params.Keywords)
  385. if params.Keywords != "" {
  386. cond += fmt.Sprintf(" AND (%s LIKE ? OR %s LIKE ?)", indexOb.Cols().IndexCode, indexOb.Cols().IndexName)
  387. kw := fmt.Sprint("%", params.Keywords, "%")
  388. pars = append(pars, kw, kw)
  389. }
  390. // 排序
  391. if params.SortField > 0 && params.SortType > 0 {
  392. fieldMap := map[int]string{1: indexOb.Cols().StartDate, 2: indexOb.Cols().EndDate, 3: indexOb.Cols().ModifyTime, 4: indexOb.Cols().LatestValue}
  393. typeMap := map[int]string{1: "ASC", 2: "DESC"}
  394. listOrder = fmt.Sprintf("%s %s", fieldMap[params.SortField], typeMap[params.SortType])
  395. }
  396. }
  397. // 列表总计
  398. total, e := indexOb.GetCountByCondition(cond, pars)
  399. if e != nil {
  400. br.Msg = "获取失败"
  401. br.ErrMsg = fmt.Sprintf("获取指标总数失败, %v", e)
  402. return
  403. }
  404. if total <= 0 {
  405. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  406. resp.Paging = page
  407. br.Data = resp
  408. br.Ret = 200
  409. br.Success = true
  410. br.Msg = "获取成功"
  411. return
  412. }
  413. // 分页查询
  414. var startSize int
  415. if params.PageSize <= 0 {
  416. params.PageSize = utils.PageSize20
  417. }
  418. if params.CurrentIndex <= 0 {
  419. params.CurrentIndex = 1
  420. }
  421. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  422. items, e := indexOb.GetPageItemsByCondition(cond, pars, []string{}, listOrder, startSize, params.PageSize)
  423. if e != nil {
  424. br.Msg = "获取失败"
  425. br.ErrMsg = fmt.Sprintf("获取指标列表失败, %v", e)
  426. return
  427. }
  428. for _, v := range items {
  429. t := v.Format2Item()
  430. // 分类完整路径
  431. classify := classifyIdItem[v.BaseFromThsHfClassifyId]
  432. levelArr := make([]string, 0)
  433. if classify != nil && classify.LevelPath != "" {
  434. arr := strings.Split(classify.LevelPath, ",")
  435. for _, a := range arr {
  436. i, _ := strconv.Atoi(a)
  437. if classifyIdItem[i] != nil {
  438. if this.Lang == utils.EnLangVersion {
  439. levelArr = append(levelArr, classifyIdItem[i].ClassifyNameEn)
  440. } else {
  441. levelArr = append(levelArr, classifyIdItem[i].ClassifyName)
  442. }
  443. }
  444. }
  445. }
  446. t.ClassifyPath = strings.Join(levelArr, "/")
  447. resp.List = append(resp.List, t)
  448. }
  449. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  450. resp.Paging = page
  451. br.Data = resp
  452. br.Ret = 200
  453. br.Success = true
  454. br.Msg = "获取成功"
  455. }
  456. // Add
  457. // @Title 新增指标
  458. // @Description 新增指标
  459. // @Param request body data_manage.ThsHfAddEdbReq true "type json string"
  460. // @Success 200 string "操作成功"
  461. // @router /ths_hf/index/add [post]
  462. func (this *BaseFromThsHfController) Add() {
  463. br := new(models.BaseResponse).Init()
  464. defer func() {
  465. if br.ErrMsg == "" {
  466. br.IsSendEmail = false
  467. }
  468. this.Data["json"] = br
  469. this.ServeJSON()
  470. }()
  471. sysUser := this.SysUser
  472. if sysUser == nil {
  473. br.Msg = "请登录"
  474. br.ErrMsg = "请登录,SysUser Is Empty"
  475. br.Ret = 408
  476. return
  477. }
  478. var params data_manage.ThsHfAddEdbReq
  479. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  480. br.Msg = "参数解析异常"
  481. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  482. return
  483. }
  484. if params.StartTime == "" {
  485. br.Msg = "请选择起始时间"
  486. return
  487. }
  488. _, e := time.ParseInLocation(utils.FormatDateTime, params.StartTime, time.Local)
  489. if e != nil {
  490. br.Msg = "起始时间格式有误"
  491. br.ErrMsg = fmt.Sprintf("起始时间格式有误, %v", e)
  492. return
  493. }
  494. if params.EndTime != "" {
  495. _, e := time.ParseInLocation(utils.FormatDateTime, params.EndTime, time.Local)
  496. if e != nil {
  497. br.Msg = "截止时间格式有误"
  498. br.ErrMsg = fmt.Sprintf("截止时间格式有误, %v", e)
  499. return
  500. }
  501. }
  502. if !utils.InArrayByInt(data_manage.ThsHfPeriodArr, params.Interval) {
  503. br.Msg = "时间周期有误"
  504. br.ErrMsg = fmt.Sprintf("时间周期有误, Interval: %d", params.Interval)
  505. return
  506. }
  507. if params.CPS != "" && !utils.InArrayByStr(data_manage.ThsHfCPSArr, params.CPS) {
  508. br.Msg = "复权方式有误"
  509. br.ErrMsg = fmt.Sprintf("复权方式有误, CPS: %s", params.CPS)
  510. return
  511. }
  512. if params.BaseDate != "" {
  513. _, e = time.ParseInLocation(utils.FormatDate, params.BaseDate, time.Local)
  514. if e != nil {
  515. br.Msg = "复权基点格式有误"
  516. br.ErrMsg = fmt.Sprintf("复权基点格式有误, %v", e)
  517. return
  518. }
  519. }
  520. if params.Fill != "" && !utils.InArrayByStr(data_manage.ThsHfFillArr, params.Fill) {
  521. br.Msg = "非交易间隔处理有误"
  522. br.ErrMsg = fmt.Sprintf("非交易间隔处理有误, Fill: %s", params.Fill)
  523. return
  524. }
  525. if len(params.IndexList) == 0 {
  526. br.Msg = "请选择指标"
  527. return
  528. }
  529. formExist := make(map[string]bool)
  530. indexNames := make([]string, 0)
  531. classifyIds := make([]int, 0)
  532. for _, v := range params.IndexList {
  533. if v.ClassifyId <= 0 {
  534. br.Msg = "请选择分类"
  535. return
  536. }
  537. if !utils.InArrayByInt(classifyIds, v.ClassifyId) {
  538. classifyIds = append(classifyIds, v.ClassifyId)
  539. }
  540. if v.Unit == "" {
  541. br.Msg = "请输入单位"
  542. return
  543. }
  544. v.IndexName = strings.TrimSpace(v.IndexName)
  545. if v.IndexName == "" {
  546. br.Msg = "请输入指标名称"
  547. return
  548. }
  549. if formExist[v.IndexName] {
  550. br.Msg = "指标名称重复, 请重新输入"
  551. return
  552. }
  553. formExist[v.IndexName] = true
  554. indexNames = append(indexNames, v.IndexName)
  555. }
  556. // 校验指标分类
  557. classifyOb := new(data_manage.BaseFromThsHfClassify)
  558. {
  559. cond := fmt.Sprintf(` AND %s IN (%s)`, classifyOb.Cols().PrimaryId, utils.GetOrmInReplace(len(classifyIds)))
  560. pars := make([]interface{}, 0)
  561. pars = append(pars, classifyIds)
  562. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName}, "")
  563. if e != nil {
  564. br.Msg = "操作失败"
  565. br.ErrMsg = fmt.Sprintf("查询分类失败, %v", e)
  566. return
  567. }
  568. classifyMap := make(map[int]*data_manage.BaseFromThsHfClassify)
  569. for _, v := range list {
  570. classifyMap[v.BaseFromThsHfClassifyId] = v
  571. }
  572. for _, v := range params.IndexList {
  573. t := classifyMap[v.ClassifyId]
  574. if t == nil {
  575. br.Msg = fmt.Sprintf("%s分类不存在, 请重新选择或刷新页面", t.ClassifyName)
  576. return
  577. }
  578. }
  579. }
  580. // 校验指标名称
  581. indexOb := new(data_manage.BaseFromThsHfIndex)
  582. {
  583. cond := fmt.Sprintf(` AND %s IN (%s)`, indexOb.Cols().IndexName, utils.GetOrmInReplace(len(indexNames)))
  584. pars := make([]interface{}, 0)
  585. pars = append(pars, indexNames)
  586. list, e := indexOb.GetItemsByCondition(cond, pars, []string{indexOb.Cols().IndexName}, "")
  587. if e != nil {
  588. br.Msg = "操作失败"
  589. br.ErrMsg = fmt.Sprintf("查询指标名称失败, %v", e)
  590. return
  591. }
  592. if len(list) > 0 {
  593. tips := "以下指标名称重复, 请重新输入: \n"
  594. for _, v := range list {
  595. tips += fmt.Sprintf("%s\n", v.IndexName)
  596. }
  597. br.Msg = tips
  598. return
  599. }
  600. }
  601. // 新增指标
  602. for _, v := range params.IndexList {
  603. var req data_manage.ThsHfBaseAddReq
  604. req.StartTime = params.StartTime
  605. req.EndTime = params.EndTime
  606. req.Interval = params.Interval
  607. req.Fill = params.Fill
  608. req.CPS = params.CPS
  609. req.BaseDate = params.BaseDate
  610. req.SysAdminId = sysUser.AdminId
  611. req.SysAdminName = sysUser.RealName
  612. req.ClassifyId = v.ClassifyId
  613. req.Unit = v.Unit
  614. req.IndexName = v.IndexName
  615. req.Frequency = v.Frequency
  616. req.StockCode = v.StockCode
  617. req.EdbCode = v.EdbCode
  618. _, e = data.BaseAddThsHf(req)
  619. if e != nil {
  620. utils.FileLog.Info(fmt.Sprintf("BaseAddThsHf err: %v", e))
  621. continue
  622. }
  623. }
  624. br.Ret = 200
  625. br.Success = true
  626. br.Msg = "操作成功"
  627. }
  628. // Edit
  629. // @Title 编辑指标
  630. // @Description 编辑指标
  631. // @Param request body data_manage.ThsHfIndexEditReq true "type json string"
  632. // @Success 200 string "操作成功"
  633. // @router /ths_hf/index/edit [post]
  634. func (this *BaseFromThsHfController) Edit() {
  635. br := new(models.BaseResponse).Init()
  636. defer func() {
  637. if br.ErrMsg == "" {
  638. br.IsSendEmail = false
  639. }
  640. this.Data["json"] = br
  641. this.ServeJSON()
  642. }()
  643. sysUser := this.SysUser
  644. if sysUser == nil {
  645. br.Msg = "请登录"
  646. br.ErrMsg = "请登录,SysUser Is Empty"
  647. br.Ret = 408
  648. return
  649. }
  650. var params data_manage.ThsHfIndexEditReq
  651. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  652. br.Msg = "参数解析异常"
  653. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  654. return
  655. }
  656. if params.IndexId <= 0 {
  657. br.Msg = "参数有误"
  658. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId)
  659. return
  660. }
  661. params.IndexName = strings.TrimSpace(params.IndexName)
  662. if params.IndexName == "" {
  663. br.Msg = "请输入指标名称"
  664. return
  665. }
  666. if params.ClassifyId <= 0 {
  667. br.Msg = "请选择分类"
  668. return
  669. }
  670. params.Unit = strings.TrimSpace(params.Unit)
  671. if params.Unit == "" {
  672. br.Msg = "请输入单位"
  673. return
  674. }
  675. indexOb := new(data_manage.BaseFromThsHfIndex)
  676. item, e := indexOb.GetItemById(params.IndexId)
  677. if e != nil {
  678. if e.Error() == utils.ErrNoRow() {
  679. br.Msg = "指标不存在, 请刷新页面"
  680. return
  681. }
  682. br.Msg = "操作失败"
  683. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  684. return
  685. }
  686. classifyOb := new(data_manage.BaseFromThsHfClassify)
  687. _, e = classifyOb.GetItemById(params.ClassifyId)
  688. if e != nil {
  689. br.Msg = "分类有误"
  690. br.ErrMsg = fmt.Sprintf("分类信息有误, %v", e)
  691. return
  692. }
  693. // 重名验证
  694. {
  695. cond := fmt.Sprintf(" AND %s = ? AND %s <> ?", indexOb.Cols().IndexName, indexOb.Cols().PrimaryId)
  696. pars := make([]interface{}, 0)
  697. pars = append(pars, params.IndexName, params.IndexId)
  698. count, e := indexOb.GetCountByCondition(cond, pars)
  699. if e != nil {
  700. br.Msg = "操作失败"
  701. br.ErrMsg = fmt.Sprintf("获取重名指标失败, %v", e)
  702. return
  703. }
  704. if count > 0 {
  705. br.Msg = "指标名称已存在, 请重新输入"
  706. return
  707. }
  708. }
  709. item.IndexName = params.IndexName
  710. item.BaseFromThsHfClassifyId = params.ClassifyId
  711. item.Unit = params.Unit
  712. item.ModifyTime = time.Now().Local()
  713. updateCols := []string{item.Cols().IndexName, item.Cols().BaseFromThsHfClassifyId, item.Cols().Unit, item.Cols().ModifyTime}
  714. if e = item.Update(updateCols); e != nil {
  715. br.Msg = "操作失败"
  716. br.ErrMsg = fmt.Sprintf("更新指标信息失败, %v", e)
  717. return
  718. }
  719. br.Ret = 200
  720. br.Success = true
  721. br.Msg = "操作成功"
  722. }
  723. // Detail
  724. // @Title 详情
  725. // @Description 详情
  726. // @Param IndexId query int true "指标ID"
  727. // @Param DataDate query string false "数据日期"
  728. // @Success 200 {object} data_manage.ThsHfSearchEdbResp
  729. // @router /ths_hf/index/detail [get]
  730. func (this *BaseFromThsHfController) Detail() {
  731. br := new(models.BaseResponse).Init()
  732. defer func() {
  733. if br.ErrMsg == "" {
  734. br.IsSendEmail = false
  735. }
  736. this.Data["json"] = br
  737. this.ServeJSON()
  738. }()
  739. sysUser := this.SysUser
  740. if sysUser == nil {
  741. br.Msg = "请登录"
  742. br.ErrMsg = "请登录,SysUser Is Empty"
  743. br.Ret = 408
  744. return
  745. }
  746. indexId, _ := this.GetInt("IndexId")
  747. if indexId <= 0 {
  748. br.Msg = "参数有误"
  749. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", indexId)
  750. return
  751. }
  752. dataDate := this.GetString("DataDate")
  753. if dataDate != "" {
  754. _, e := time.Parse(utils.FormatDate, dataDate)
  755. if e != nil {
  756. br.Msg = "数据日期格式有误"
  757. br.ErrMsg = fmt.Sprintf("数据日期格式有误, DataDate: %s", dataDate)
  758. return
  759. }
  760. }
  761. indexOb := new(data_manage.BaseFromThsHfIndex)
  762. item, e := indexOb.GetItemById(indexId)
  763. if e != nil {
  764. if e.Error() == utils.ErrNoRow() {
  765. br.Msg = "指标不存在, 请刷新页面"
  766. return
  767. }
  768. br.Msg = "获取失败"
  769. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  770. return
  771. }
  772. indexItem := item.Format2Item()
  773. if item.EndDate.IsZero() {
  774. item.EndDate = time.Now()
  775. }
  776. // 默认取有数据的最新日期
  777. if dataDate == "" {
  778. dataDate = item.EndDate.Format(utils.FormatDate)
  779. }
  780. dataList := make([]*data_manage.BaseFromThsHfDataItem, 0)
  781. {
  782. dataOb := new(data_manage.BaseFromThsHfData)
  783. cond := fmt.Sprintf(" AND %s = ? AND (%s BETWEEN ? AND ?)", dataOb.Cols().IndexCode, dataOb.Cols().DataTime)
  784. pars := make([]interface{}, 0)
  785. pars = append(pars, indexItem.IndexCode, fmt.Sprintf("%s 00:00:00", dataDate), fmt.Sprintf("%s 23:59:59", dataDate))
  786. list, e := dataOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s DESC", dataOb.Cols().DataTime))
  787. if e != nil {
  788. br.Msg = "获取失败"
  789. br.ErrMsg = fmt.Sprintf("获取指标数据失败, %v", e)
  790. return
  791. }
  792. for _, v := range list {
  793. dataList = append(dataList, v.Format2Item())
  794. }
  795. }
  796. type DetailResp struct {
  797. Index *data_manage.BaseFromThsHfIndexItem
  798. DataDate string `description:"数据日期"`
  799. DataList []*data_manage.BaseFromThsHfDataItem
  800. }
  801. resp := new(DetailResp)
  802. resp.Index = indexItem
  803. resp.DataDate = dataDate
  804. resp.DataList = dataList
  805. br.Data = resp
  806. br.Ret = 200
  807. br.Success = true
  808. br.Msg = "获取成功"
  809. }
  810. // Refresh
  811. // @Title 刷新指标
  812. // @Description 刷新指标
  813. // @Param request body data_manage.ThsHfIndexOptReq true "type json string"
  814. // @Success 200 string "操作成功"
  815. // @router /ths_hf/index/refresh [post]
  816. func (this *BaseFromThsHfController) Refresh() {
  817. br := new(models.BaseResponse).Init()
  818. defer func() {
  819. if br.ErrMsg == "" {
  820. br.IsSendEmail = false
  821. }
  822. this.Data["json"] = br
  823. this.ServeJSON()
  824. }()
  825. sysUser := this.SysUser
  826. if sysUser == nil {
  827. br.Msg = "请登录"
  828. br.ErrMsg = "请登录,SysUser Is Empty"
  829. br.Ret = 408
  830. return
  831. }
  832. var params data_manage.ThsHfIndexOptReq
  833. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  834. br.Msg = "参数解析异常"
  835. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  836. return
  837. }
  838. if params.IndexId <= 0 {
  839. br.Msg = "参数有误"
  840. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId)
  841. return
  842. }
  843. indexOb := new(data_manage.BaseFromThsHfIndex)
  844. _, e := indexOb.GetItemById(params.IndexId)
  845. if e != nil {
  846. if e.Error() == utils.ErrNoRow() {
  847. br.Msg = "指标不存在, 请刷新页面"
  848. return
  849. }
  850. br.Msg = "操作失败"
  851. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  852. return
  853. }
  854. // 指标刷新逻辑
  855. indexIds := []int{params.IndexId}
  856. _, e = data.RefreshBaseThsHfIndex(indexIds, 1)
  857. if e != nil {
  858. br.Msg = "刷新失败"
  859. br.ErrMsg = fmt.Sprintf("刷新指标失败, %v", e)
  860. return
  861. }
  862. br.Ret = 200
  863. br.Success = true
  864. br.Msg = "操作成功"
  865. }
  866. // Remove
  867. // @Title 删除指标
  868. // @Description 删除指标
  869. // @Param request body data_manage.ThsHfIndexOptReq true "type json string"
  870. // @Success 200 string "操作成功"
  871. // @router /ths_hf/index/remove [post]
  872. func (this *BaseFromThsHfController) Remove() {
  873. br := new(models.BaseResponse).Init()
  874. defer func() {
  875. if br.ErrMsg == "" {
  876. br.IsSendEmail = false
  877. }
  878. this.Data["json"] = br
  879. this.ServeJSON()
  880. }()
  881. sysUser := this.SysUser
  882. if sysUser == nil {
  883. br.Msg = "请登录"
  884. br.ErrMsg = "请登录,SysUser Is Empty"
  885. br.Ret = 408
  886. return
  887. }
  888. var params data_manage.ThsHfIndexOptReq
  889. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  890. br.Msg = "参数解析异常"
  891. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  892. return
  893. }
  894. if params.IndexId <= 0 {
  895. br.Msg = "参数有误"
  896. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", params.IndexId)
  897. return
  898. }
  899. indexOb := new(data_manage.BaseFromThsHfIndex)
  900. item, e := indexOb.GetItemById(params.IndexId)
  901. if e != nil {
  902. if e.Error() == utils.ErrNoRow() {
  903. br.Ret = 200
  904. br.Success = true
  905. br.Msg = "操作成功"
  906. return
  907. }
  908. br.Msg = "操作失败"
  909. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  910. return
  911. }
  912. // 删除校验, 指标库存在不可删除
  913. edbMappingOb := new(data_manage.BaseFromEdbMapping)
  914. {
  915. cond := fmt.Sprintf(" AND %s = ?", edbMappingOb.Cols().BaseIndexCode)
  916. pars := make([]interface{}, 0)
  917. pars = append(pars, item.IndexCode)
  918. count, e := edbMappingOb.GetCountByCondition(cond, pars)
  919. if e != nil {
  920. br.Msg = "操作失败"
  921. br.ErrMsg = fmt.Sprintf("获取源指标关联失败, %v", e)
  922. return
  923. }
  924. if count > 0 {
  925. br.Msg = "指标已被引用, 不允许删除"
  926. return
  927. }
  928. }
  929. // TODO:同步Mongo
  930. if e = item.Remove(); e != nil {
  931. br.Msg = "操作失败"
  932. br.ErrMsg = fmt.Sprintf("删除指标失败, %v", e)
  933. return
  934. }
  935. dataOb := new(data_manage.BaseFromThsHfData)
  936. cond := fmt.Sprintf(" %s = ?", dataOb.Cols().BaseFromThsHfIndexId)
  937. pars := make([]interface{}, 0)
  938. pars = append(pars, params.IndexId)
  939. if e = dataOb.RemoveByCondition(cond, pars); e != nil {
  940. br.Msg = "操作失败"
  941. br.ErrMsg = fmt.Sprintf("删除指标数据失败, %v", e)
  942. return
  943. }
  944. br.Ret = 200
  945. br.Success = true
  946. br.Msg = "操作成功"
  947. }
  948. // ListChoice
  949. // @Title 列表选择
  950. // @Description 列表选择
  951. // @Param request body data_manage.ThsHfIndexMultiOptReq true "type json string"
  952. // @Success 200 string "获取成功"
  953. // @router /ths_hf/index/list_choice [get]
  954. func (this *BaseFromThsHfController) ListChoice() {
  955. br := new(models.BaseResponse).Init()
  956. defer func() {
  957. if br.ErrMsg == "" {
  958. br.IsSendEmail = false
  959. }
  960. this.Data["json"] = br
  961. this.ServeJSON()
  962. }()
  963. sysUser := this.SysUser
  964. if sysUser == nil {
  965. br.Msg = "请登录"
  966. br.ErrMsg = "请登录,SysUser Is Empty"
  967. br.Ret = 408
  968. return
  969. }
  970. var params data_manage.ThsHfIndexListChoiceReq
  971. if e := this.ParseForm(&params); e != nil {
  972. br.Msg = "参数解析失败"
  973. br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e)
  974. return
  975. }
  976. var listIds []int
  977. if params.ListIds != "" {
  978. strArr := strings.Split(params.ListIds, ",")
  979. for _, v := range strArr {
  980. t, _ := strconv.Atoi(v)
  981. if t > 0 {
  982. listIds = append(listIds, t)
  983. }
  984. }
  985. }
  986. var (
  987. cond string
  988. pars []interface{}
  989. )
  990. indexOb := new(data_manage.BaseFromThsHfIndex)
  991. resp := make([]*data_manage.ThsHfIndexListChoiceItem, 0)
  992. // 非列表全选
  993. if !params.SelectAll {
  994. if len(listIds) > 0 {
  995. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(listIds)))
  996. pars = append(pars, listIds)
  997. }
  998. }
  999. // 列表全选, 根据条件筛选列表并过滤params.ListIds中选择的ID
  1000. if params.SelectAll {
  1001. // 查询所有分类-分类查询用
  1002. classifyOb := new(data_manage.BaseFromThsHfClassify)
  1003. classifies := make([]*data_manage.BaseFromThsHfClassify, 0)
  1004. classifyIdItem := make(map[int]*data_manage.BaseFromThsHfClassify)
  1005. {
  1006. list, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{classifyOb.Cols().PrimaryId, classifyOb.Cols().ClassifyName, classifyOb.Cols().ClassifyNameEn, classifyOb.Cols().LevelPath}, "")
  1007. if e != nil {
  1008. br.Msg = "获取失败"
  1009. br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
  1010. return
  1011. }
  1012. for _, v := range list {
  1013. classifyIdItem[v.BaseFromThsHfClassifyId] = v
  1014. }
  1015. classifies = list
  1016. }
  1017. // 筛选项
  1018. if params.ClassifyId != "" {
  1019. classifyIdArr := strings.Split(params.ClassifyId, ",")
  1020. classifyIds := make([]int, 0)
  1021. for _, v := range classifyIdArr {
  1022. t, _ := strconv.Atoi(v)
  1023. if t > 0 {
  1024. classifyIds = append(classifyIds, t)
  1025. }
  1026. }
  1027. // 不包含子分类
  1028. if len(classifyIds) > 0 && !params.IncludeChild {
  1029. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(classifyIds)))
  1030. pars = append(pars, classifyIds)
  1031. }
  1032. // 包含子分类
  1033. if len(classifyIds) > 0 && params.IncludeChild {
  1034. queryClassifyIds := make([]int, 0)
  1035. queryClassifyExist := make(map[int]bool)
  1036. for _, v := range classifyIds {
  1037. // 遍历所有分类从LevelPath中找含有查询分类ID的...=_=!
  1038. for _, cv := range classifies {
  1039. if queryClassifyExist[cv.BaseFromThsHfClassifyId] {
  1040. continue
  1041. }
  1042. if cv.LevelPath == "" {
  1043. continue
  1044. }
  1045. strArr := strings.Split(cv.LevelPath, ",")
  1046. if len(strArr) == 0 {
  1047. continue
  1048. }
  1049. for _, sv := range strArr {
  1050. tv, _ := strconv.Atoi(sv)
  1051. if tv == v {
  1052. queryClassifyIds = append(queryClassifyIds, cv.BaseFromThsHfClassifyId)
  1053. queryClassifyExist[cv.BaseFromThsHfClassifyId] = true
  1054. break
  1055. }
  1056. }
  1057. }
  1058. }
  1059. if len(queryClassifyIds) == 0 {
  1060. br.Data = resp
  1061. br.Ret = 200
  1062. br.Success = true
  1063. br.Msg = "获取成功"
  1064. return
  1065. }
  1066. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().BaseFromThsHfClassifyId, utils.GetOrmInReplace(len(queryClassifyIds)))
  1067. pars = append(pars, queryClassifyIds)
  1068. }
  1069. }
  1070. if params.Frequency != "" {
  1071. frequencyArr := strings.Split(params.Frequency, ",")
  1072. if len(frequencyArr) > 0 {
  1073. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().Frequency, utils.GetOrmInReplace(len(frequencyArr)))
  1074. pars = append(pars, frequencyArr)
  1075. }
  1076. }
  1077. if params.SysAdminId != "" {
  1078. adminIdArr := strings.Split(params.SysAdminId, ",")
  1079. adminIds := make([]int, 0)
  1080. for _, v := range adminIdArr {
  1081. t, _ := strconv.Atoi(v)
  1082. if t > 0 {
  1083. adminIds = append(adminIds, t)
  1084. }
  1085. }
  1086. if len(adminIds) > 0 {
  1087. cond += fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().SysUserId, utils.GetOrmInReplace(len(adminIds)))
  1088. pars = append(pars, adminIds)
  1089. }
  1090. }
  1091. params.Keywords = strings.TrimSpace(params.Keywords)
  1092. if params.Keywords != "" {
  1093. cond += fmt.Sprintf(" AND (%s LIKE ? OR %s LIKE ?)", indexOb.Cols().IndexCode, indexOb.Cols().IndexName)
  1094. kw := fmt.Sprint("%", params.Keywords, "%")
  1095. pars = append(pars, kw, kw)
  1096. }
  1097. // 过滤掉选择的指标
  1098. if len(listIds) > 0 {
  1099. cond += fmt.Sprintf(" AND %s NOT IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(listIds)))
  1100. pars = append(pars, listIds)
  1101. }
  1102. }
  1103. fields := []string{indexOb.Cols().PrimaryId, indexOb.Cols().IndexCode, indexOb.Cols().IndexName}
  1104. list, e := indexOb.GetItemsByCondition(cond, pars, fields, "")
  1105. if e != nil {
  1106. br.Msg = "获取失败"
  1107. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  1108. return
  1109. }
  1110. if len(list) > 500 {
  1111. br.Msg = "选择指标超过500个, 请重新选择"
  1112. return
  1113. }
  1114. for _, v := range list {
  1115. resp = append(resp, &data_manage.ThsHfIndexListChoiceItem{
  1116. IndexId: v.BaseFromThsHfIndexId,
  1117. IndexCode: v.IndexCode,
  1118. IndexName: v.IndexName,
  1119. })
  1120. }
  1121. br.Data = resp
  1122. br.Ret = 200
  1123. br.Success = true
  1124. br.Msg = "获取成功"
  1125. }
  1126. // MultiOpt
  1127. // @Title 批量操作-移动分类/删除/刷新
  1128. // @Description 批量操作-移动分类/删除/刷新
  1129. // @Param request body data_manage.ThsHfIndexMultiOptReq true "type json string"
  1130. // @Success 200 string "操作成功"
  1131. // @router /ths_hf/index/multi_opt [post]
  1132. func (this *BaseFromThsHfController) MultiOpt() {
  1133. br := new(models.BaseResponse).Init()
  1134. defer func() {
  1135. if br.ErrMsg == "" {
  1136. br.IsSendEmail = false
  1137. }
  1138. this.Data["json"] = br
  1139. this.ServeJSON()
  1140. }()
  1141. sysUser := this.SysUser
  1142. if sysUser == nil {
  1143. br.Msg = "请登录"
  1144. br.ErrMsg = "请登录,SysUser Is Empty"
  1145. br.Ret = 408
  1146. return
  1147. }
  1148. var params data_manage.ThsHfIndexMultiOptReq
  1149. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  1150. br.Msg = "参数解析异常"
  1151. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1152. return
  1153. }
  1154. if !utils.InArrayByInt([]int{1, 2, 3}, params.OptType) {
  1155. br.Msg = "请选择操作类型"
  1156. return
  1157. }
  1158. if len(params.IndexIds) == 0 {
  1159. br.Msg = "请选择指标"
  1160. return
  1161. }
  1162. if params.OptType == 1 && params.MoveClassifyId <= 0 {
  1163. br.Msg = "请选择分类"
  1164. return
  1165. }
  1166. if params.OptType == 3 && !utils.InArrayByInt([]int{1, 2}, params.RefreshType) {
  1167. br.Msg = "请选择刷新方式"
  1168. return
  1169. }
  1170. // 批量移动
  1171. indexOb := new(data_manage.BaseFromThsHfIndex)
  1172. if params.OptType == 1 {
  1173. if e := indexOb.UpdateClassifyMulti(params.IndexIds, params.MoveClassifyId); e != nil {
  1174. br.Msg = "移动失败"
  1175. br.ErrMsg = fmt.Sprintf("批量移动分类失败, %v", e)
  1176. return
  1177. }
  1178. }
  1179. // 批量删除
  1180. if params.OptType == 2 {
  1181. // 校验是否被引用
  1182. edbMappingOb := new(data_manage.BaseFromEdbMapping)
  1183. {
  1184. cond := fmt.Sprintf(" AND %s IN (%s)", edbMappingOb.Cols().BaseFromIndexId, utils.GetOrmInReplace(len(params.IndexIds)))
  1185. pars := make([]interface{}, 0)
  1186. pars = append(pars, params.IndexIds)
  1187. count, e := edbMappingOb.GetCountByCondition(cond, pars)
  1188. if e != nil {
  1189. br.Msg = "删除失败"
  1190. br.ErrMsg = fmt.Sprintf("获取源指标关联失败, %v", e)
  1191. return
  1192. }
  1193. if count > 0 {
  1194. br.Msg = "存在指标被引用, 不允许删除"
  1195. return
  1196. }
  1197. }
  1198. if e := indexOb.MultiRemove(params.IndexIds); e != nil {
  1199. br.Msg = "删除失败"
  1200. br.ErrMsg = fmt.Sprintf("批量删除指标失败, %v", e)
  1201. return
  1202. }
  1203. dataOb := new(data_manage.BaseFromThsHfData)
  1204. cond := fmt.Sprintf(" %s IN (%s)", dataOb.Cols().BaseFromThsHfIndexId, utils.GetOrmInReplace(len(params.IndexIds)))
  1205. pars := make([]interface{}, 0)
  1206. pars = append(pars, params.IndexIds)
  1207. if e := dataOb.RemoveByCondition(cond, pars); e != nil {
  1208. br.Msg = "删除失败"
  1209. br.ErrMsg = fmt.Sprintf("批量删除指标数据失败, %v", e)
  1210. return
  1211. }
  1212. }
  1213. // 批量刷新
  1214. if params.OptType == 3 {
  1215. isAsync, e := data.RefreshBaseThsHfIndex(params.IndexIds, params.RefreshType)
  1216. if e != nil {
  1217. br.Msg = "刷新失败"
  1218. br.ErrMsg = fmt.Sprintf("批量刷新指标失败, %v", e)
  1219. return
  1220. }
  1221. if isAsync {
  1222. br.Ret = 200
  1223. br.Success = true
  1224. br.Msg = "操作成功, 刷新指标较多, 请10分钟后查看"
  1225. return
  1226. }
  1227. }
  1228. br.Ret = 200
  1229. br.Success = true
  1230. br.Msg = "操作成功"
  1231. }
  1232. // Save2EdbPre
  1233. // @Title 批量新增指标库-前置
  1234. // @Description 批量新增指标库-前置
  1235. // @Param request body data_manage.ThsHfIndexMultiSave2EdbReq true "type json string"
  1236. // @Success 200 string "操作成功"
  1237. // @router /ths_hf/index/save2edb_pre [post]
  1238. func (this *BaseFromThsHfController) Save2EdbPre() {
  1239. br := new(models.BaseResponse).Init()
  1240. defer func() {
  1241. if br.ErrMsg == "" {
  1242. br.IsSendEmail = false
  1243. }
  1244. this.Data["json"] = br
  1245. this.ServeJSON()
  1246. }()
  1247. sysUser := this.SysUser
  1248. if sysUser == nil {
  1249. br.Msg = "请登录"
  1250. br.ErrMsg = "请登录,SysUser Is Empty"
  1251. br.Ret = 408
  1252. return
  1253. }
  1254. var params data_manage.ThsHfIndexMultiSave2EdbPreReq
  1255. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  1256. br.Msg = "参数解析异常"
  1257. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1258. return
  1259. }
  1260. if len(params.IndexIds) == 0 {
  1261. br.Msg = "请选择指标"
  1262. return
  1263. }
  1264. convertRule := params.ConvertRule
  1265. if convertRule.ConvertType != 1 && convertRule.ConvertType != 2 {
  1266. br.Msg = "请选择数据转换方式"
  1267. return
  1268. }
  1269. // 生成的指标名称后缀
  1270. var (
  1271. suffixName string
  1272. startTimeCn string
  1273. endTimeCn string
  1274. calculateCn string
  1275. )
  1276. dayMap := map[int]string{1: "当日", 2: "前日"}
  1277. calculateMap := map[int]string{1: "均值", 2: "最大值", 3: "最小值"}
  1278. if convertRule.ConvertType == 1 {
  1279. if convertRule.ConvertFixed.FixedDay != 1 && convertRule.ConvertFixed.FixedDay != 2 {
  1280. br.Msg = "请选择指定时间"
  1281. return
  1282. }
  1283. if convertRule.ConvertFixed.FixedTime == "" {
  1284. br.Msg = "请选择指定时间"
  1285. return
  1286. }
  1287. timePrefix := time.Now().Local().Format(utils.FormatDate)
  1288. st := fmt.Sprintf("%s %s", timePrefix, convertRule.ConvertFixed.FixedTime)
  1289. startTime, e := time.Parse(utils.FormatDateTime, st)
  1290. if e != nil {
  1291. br.Msg = "指定时间格式有误"
  1292. return
  1293. }
  1294. startTimeCn = fmt.Sprintf("%s%s", dayMap[convertRule.ConvertFixed.FixedDay], startTime.Format("15点04"))
  1295. }
  1296. if convertRule.ConvertType == 2 {
  1297. if convertRule.ConvertArea.StartDay != 1 && convertRule.ConvertArea.StartDay != 2 {
  1298. br.Msg = "请选择起始时间"
  1299. return
  1300. }
  1301. if convertRule.ConvertArea.StartTime == "" {
  1302. br.Msg = "请选择起始时间"
  1303. return
  1304. }
  1305. var startTimePre string
  1306. if convertRule.ConvertArea.StartDay == 1 {
  1307. startTimePre = time.Now().Local().Format(utils.FormatDate)
  1308. }
  1309. if convertRule.ConvertArea.StartDay == 2 {
  1310. startTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1311. }
  1312. st := fmt.Sprintf("%s %s", startTimePre, convertRule.ConvertArea.StartTime)
  1313. startTime, e := time.Parse(utils.FormatDateTime, st)
  1314. if e != nil {
  1315. br.Msg = "起始时间格式有误"
  1316. return
  1317. }
  1318. startTimeCn = fmt.Sprintf("%s%s", dayMap[convertRule.ConvertArea.StartDay], startTime.Format("15点04"))
  1319. if convertRule.ConvertArea.EndDay != 1 && convertRule.ConvertArea.EndDay != 2 {
  1320. br.Msg = "请选择截止时间"
  1321. return
  1322. }
  1323. if convertRule.ConvertArea.EndTime == "" {
  1324. br.Msg = "请选择截止时间"
  1325. return
  1326. }
  1327. var endTimePre string
  1328. if convertRule.ConvertArea.EndDay == 1 {
  1329. endTimePre = time.Now().Local().Format(utils.FormatDate)
  1330. }
  1331. if convertRule.ConvertArea.EndDay == 2 {
  1332. endTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1333. }
  1334. ed := fmt.Sprintf("%s %s", endTimePre, convertRule.ConvertArea.EndTime)
  1335. endTime, e := time.Parse(utils.FormatDateTime, ed)
  1336. if e != nil {
  1337. br.Msg = "截止时间格式有误"
  1338. return
  1339. }
  1340. endTimeCn = fmt.Sprintf("至%s%s", dayMap[convertRule.ConvertArea.EndDay], endTime.Format("15点04"))
  1341. if startTime.After(endTime) {
  1342. br.Msg = "起始日期不可早于截止日期"
  1343. return
  1344. }
  1345. calculateCn = calculateMap[convertRule.ConvertArea.CalculateType]
  1346. }
  1347. suffixName = fmt.Sprint(startTimeCn, endTimeCn, calculateCn)
  1348. // 查询指标
  1349. indexOb := new(data_manage.BaseFromThsHfIndex)
  1350. cond := fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(params.IndexIds)))
  1351. pars := make([]interface{}, 0)
  1352. pars = append(pars, params.IndexIds)
  1353. list, e := indexOb.GetItemsByCondition(cond, pars, []string{}, "")
  1354. if e != nil {
  1355. br.Msg = "获取失败"
  1356. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  1357. return
  1358. }
  1359. resp := make([]*data_manage.ThsHfIndexMultiSave2EdbPreItem, 0)
  1360. for _, v := range list {
  1361. t := new(data_manage.ThsHfIndexMultiSave2EdbPreItem)
  1362. t.IndexId = v.BaseFromThsHfIndexId
  1363. t.IndexCode = v.IndexCode
  1364. t.IndexName = v.IndexName
  1365. edbCn := data_manage.ThsHfEdbCodeCn[v.Indicator]
  1366. if edbCn == "" {
  1367. edbCn = v.Indicator
  1368. }
  1369. t.NewIndexName = fmt.Sprint(v.StockCode, v.Frequency, edbCn, suffixName)
  1370. t.StockCode = v.StockCode
  1371. t.EdbCode = v.Indicator
  1372. t.Unit = v.Unit
  1373. t.Frequency = v.Frequency
  1374. t.NewFrequency = "日度"
  1375. resp = append(resp, t)
  1376. }
  1377. br.Data = resp
  1378. br.Ret = 200
  1379. br.Success = true
  1380. br.Msg = "获取成功"
  1381. }
  1382. // Save2Edb
  1383. // @Title 批量新增指标库
  1384. // @Description 批量新增指标库
  1385. // @Param request body data_manage.ThsHfIndexMultiSave2EdbReq true "type json string"
  1386. // @Success 200 string "操作成功"
  1387. // @router /ths_hf/index/save2edb [post]
  1388. func (this *BaseFromThsHfController) Save2Edb() {
  1389. br := new(models.BaseResponse).Init()
  1390. defer func() {
  1391. if br.ErrMsg == "" {
  1392. br.IsSendEmail = false
  1393. }
  1394. this.Data["json"] = br
  1395. this.ServeJSON()
  1396. }()
  1397. sysUser := this.SysUser
  1398. if sysUser == nil {
  1399. br.Msg = "请登录"
  1400. br.ErrMsg = "请登录,SysUser Is Empty"
  1401. br.Ret = 408
  1402. return
  1403. }
  1404. var params data_manage.ThsHfIndexMultiSave2EdbReq
  1405. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  1406. br.Msg = "参数解析异常"
  1407. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1408. return
  1409. }
  1410. if len(params.NewIndexes) == 0 {
  1411. br.Msg = "请选择指标"
  1412. return
  1413. }
  1414. convertRule := params.ConvertRule
  1415. if convertRule.ConvertType != 1 && convertRule.ConvertType != 2 {
  1416. br.Msg = "请选择数据转换方式"
  1417. return
  1418. }
  1419. if convertRule.ConvertType == 1 {
  1420. if convertRule.ConvertFixed.FixedDay != 1 && convertRule.ConvertFixed.FixedDay != 2 {
  1421. br.Msg = "请选择指定时间"
  1422. return
  1423. }
  1424. if convertRule.ConvertFixed.FixedTime == "" {
  1425. br.Msg = "请选择指定时间"
  1426. return
  1427. }
  1428. timePrefix := time.Now().Local().Format(utils.FormatDate)
  1429. st := fmt.Sprintf("%s %s", timePrefix, convertRule.ConvertFixed.FixedTime)
  1430. _, e := time.Parse(utils.FormatDateTime, st)
  1431. if e != nil {
  1432. br.Msg = "指定时间格式有误"
  1433. return
  1434. }
  1435. }
  1436. if convertRule.ConvertType == 2 {
  1437. if convertRule.ConvertArea.StartDay != 1 && convertRule.ConvertArea.StartDay != 2 {
  1438. br.Msg = "请选择起始时间"
  1439. return
  1440. }
  1441. if convertRule.ConvertArea.StartTime == "" {
  1442. br.Msg = "请选择起始时间"
  1443. return
  1444. }
  1445. var startTimePre string
  1446. if convertRule.ConvertArea.StartDay == 1 {
  1447. startTimePre = time.Now().Local().Format(utils.FormatDate)
  1448. }
  1449. if convertRule.ConvertArea.StartDay == 2 {
  1450. startTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1451. }
  1452. st := fmt.Sprintf("%s %s", startTimePre, convertRule.ConvertArea.StartTime)
  1453. startTime, e := time.Parse(utils.FormatDateTime, st)
  1454. if e != nil {
  1455. br.Msg = "起始时间格式有误"
  1456. return
  1457. }
  1458. if convertRule.ConvertArea.EndDay != 1 && convertRule.ConvertArea.EndDay != 2 {
  1459. br.Msg = "请选择截止时间"
  1460. return
  1461. }
  1462. if convertRule.ConvertArea.EndTime == "" {
  1463. br.Msg = "请选择截止时间"
  1464. return
  1465. }
  1466. var endTimePre string
  1467. if convertRule.ConvertArea.EndDay == 1 {
  1468. endTimePre = time.Now().Local().Format(utils.FormatDate)
  1469. }
  1470. if convertRule.ConvertArea.EndDay == 2 {
  1471. endTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1472. }
  1473. ed := fmt.Sprintf("%s %s", endTimePre, convertRule.ConvertArea.EndTime)
  1474. endTime, e := time.Parse(utils.FormatDateTime, ed)
  1475. if e != nil {
  1476. br.Msg = "截止时间格式有误"
  1477. return
  1478. }
  1479. if startTime.After(endTime) {
  1480. br.Msg = "起始日期不可早于截止日期"
  1481. return
  1482. }
  1483. }
  1484. resp := new(data_manage.ThsHfIndexMultiSave2EdbResp)
  1485. // 判断指标名称是否重复
  1486. edbNameExist := make(map[string]bool)
  1487. {
  1488. items, e := data_manage.GetEdbInfoFieldList(``, make([]interface{}, 0), []string{"edb_name"})
  1489. if e != nil {
  1490. br.Msg = "操作失败"
  1491. br.ErrMsg = fmt.Sprintf("获取指标名称列表失败, %v", e)
  1492. return
  1493. }
  1494. for _, v := range items {
  1495. edbNameExist[v.EdbName] = true
  1496. }
  1497. }
  1498. for _, v := range params.NewIndexes {
  1499. if edbNameExist[v.NewIndexName] {
  1500. resp.Exist = append(resp.Exist, v)
  1501. }
  1502. }
  1503. if len(resp.Exist) > 0 {
  1504. br.Data = resp
  1505. br.Ret = 200
  1506. br.Success = true
  1507. br.Msg = "含重复指标名称"
  1508. return
  1509. }
  1510. // 请求指标库新增
  1511. for _, v := range params.NewIndexes {
  1512. var r data_manage.ThsHfIndexMultiSave2EdbLibReq
  1513. r.ConvertRule = convertRule
  1514. v.SysAdminId = sysUser.AdminId
  1515. v.SysAdminName = sysUser.RealName
  1516. r.NewIndex = v
  1517. b, e := json.Marshal(r)
  1518. if e != nil {
  1519. v.Tips = "新增失败"
  1520. v.ErrMsg = e.Error()
  1521. resp.Fail = append(resp.Fail, v)
  1522. continue
  1523. }
  1524. res, e := data.AddBaseEdbInfo(string(b), utils.DATA_SOURCE_THS, utils.DATA_SUB_SOURCE_HIGH_FREQUENCY, this.Lang)
  1525. if e != nil {
  1526. v.Tips = res.Msg
  1527. v.ErrMsg = e.Error()
  1528. resp.Fail = append(resp.Fail, v)
  1529. continue
  1530. }
  1531. if res.Ret != 200 {
  1532. v.Tips = res.Msg
  1533. v.ErrMsg = fmt.Sprintf("新增失败, ErrMsg: %s", res.ErrMsg)
  1534. resp.Fail = append(resp.Fail, v)
  1535. continue
  1536. }
  1537. v.Tips = "新增成功"
  1538. resp.Success = append(resp.Success, v)
  1539. }
  1540. br.Data = resp
  1541. br.Ret = 200
  1542. br.Success = true
  1543. br.Msg = "操作成功"
  1544. }