base_from_ths_hf.go 45 KB

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