base_from_ths_hf.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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. resp := new(data_manage.ThsHfIndexMultiOptResp)
  1171. // 指标名称
  1172. indexOb := new(data_manage.BaseFromThsHfIndex)
  1173. indexMap := make(map[int]*data_manage.BaseFromThsHfIndex)
  1174. {
  1175. cond := fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(params.IndexIds)))
  1176. pars := make([]interface{}, 0)
  1177. pars = append(pars, params.IndexIds)
  1178. list, e := indexOb.GetItemsByCondition(cond, pars, []string{indexOb.Cols().PrimaryId, indexOb.Cols().IndexCode, indexOb.Cols().IndexName}, "")
  1179. if e != nil {
  1180. br.Msg = "删除失败"
  1181. br.ErrMsg = fmt.Sprintf("获取源指标列表失败, %v", e)
  1182. return
  1183. }
  1184. for _, v := range list {
  1185. indexMap[v.BaseFromThsHfIndexId] = v
  1186. }
  1187. }
  1188. // 批量移动
  1189. if params.OptType == 1 {
  1190. if e := indexOb.UpdateClassifyMulti(params.IndexIds, params.MoveClassifyId); e != nil {
  1191. br.Msg = "移动失败"
  1192. br.ErrMsg = fmt.Sprintf("批量移动分类失败, %v", e)
  1193. return
  1194. }
  1195. }
  1196. // 批量删除
  1197. if params.OptType == 2 {
  1198. // 被引用的指标需要提示出来, 未被引用的直接删除
  1199. usedIndexIds := make([]int, 0)
  1200. {
  1201. edbMappingOb := new(data_manage.BaseFromEdbMapping)
  1202. cond := fmt.Sprintf(" AND %s IN (%s)", edbMappingOb.Cols().BaseFromIndexId, utils.GetOrmInReplace(len(params.IndexIds)))
  1203. pars := make([]interface{}, 0)
  1204. pars = append(pars, params.IndexIds)
  1205. list, e := edbMappingOb.GetItemsByCondition(cond, pars, []string{}, "")
  1206. if e != nil {
  1207. br.Msg = "删除失败"
  1208. br.ErrMsg = fmt.Sprintf("获取源指标关联失败, %v", e)
  1209. return
  1210. }
  1211. for _, v := range list {
  1212. if !utils.InArrayByInt(usedIndexIds, v.BaseFromIndexId) {
  1213. usedIndexIds = append(usedIndexIds, v.BaseFromIndexId)
  1214. }
  1215. }
  1216. }
  1217. // 可删除的指标
  1218. removeIndexIds := params.IndexIds
  1219. if len(usedIndexIds) > 0 {
  1220. removeIndexIds = utils.MinusInt(params.IndexIds, usedIndexIds)
  1221. // 标记不允许删除的
  1222. for _, v := range usedIndexIds {
  1223. t := indexMap[v]
  1224. if t == nil {
  1225. continue
  1226. }
  1227. resp.Fail = append(resp.Fail, &data_manage.ThsHfIndexBaseInfo{
  1228. IndexId: v,
  1229. IndexCode: t.IndexCode,
  1230. IndexName: t.IndexName,
  1231. })
  1232. }
  1233. }
  1234. if len(removeIndexIds) > 0 {
  1235. if e := indexOb.MultiRemove(removeIndexIds); e != nil {
  1236. br.Msg = "删除失败"
  1237. br.ErrMsg = fmt.Sprintf("批量删除指标失败, %v", e)
  1238. return
  1239. }
  1240. dataOb := new(data_manage.BaseFromThsHfData)
  1241. cond := fmt.Sprintf(" %s IN (%s)", dataOb.Cols().BaseFromThsHfIndexId, utils.GetOrmInReplace(len(removeIndexIds)))
  1242. pars := make([]interface{}, 0)
  1243. pars = append(pars, removeIndexIds)
  1244. if e := dataOb.RemoveByCondition(cond, pars); e != nil {
  1245. br.Msg = "删除失败"
  1246. br.ErrMsg = fmt.Sprintf("批量删除指标数据失败, %v", e)
  1247. return
  1248. }
  1249. // 标记删除成功的
  1250. for _, v := range removeIndexIds {
  1251. t := indexMap[v]
  1252. if t == nil {
  1253. continue
  1254. }
  1255. resp.Success = append(resp.Success, &data_manage.ThsHfIndexBaseInfo{
  1256. IndexId: v,
  1257. IndexCode: t.IndexCode,
  1258. IndexName: t.IndexName,
  1259. })
  1260. }
  1261. }
  1262. }
  1263. // 批量刷新
  1264. if params.OptType == 3 {
  1265. isAsync, e := data.RefreshBaseThsHfIndex(params.IndexIds, params.RefreshType)
  1266. if e != nil {
  1267. br.Msg = "刷新失败"
  1268. br.ErrMsg = fmt.Sprintf("批量刷新指标失败, %v", e)
  1269. return
  1270. }
  1271. if isAsync {
  1272. br.Ret = 200
  1273. br.Success = true
  1274. br.Msg = "操作成功, 刷新指标较多, 请10分钟后查看"
  1275. return
  1276. }
  1277. }
  1278. br.Data = resp
  1279. br.Ret = 200
  1280. br.Success = true
  1281. br.Msg = "操作成功"
  1282. }
  1283. // Save2EdbPre
  1284. // @Title 批量新增指标库-前置
  1285. // @Description 批量新增指标库-前置
  1286. // @Param request body data_manage.ThsHfIndexMultiSave2EdbReq true "type json string"
  1287. // @Success 200 string "操作成功"
  1288. // @router /ths_hf/index/save2edb_pre [post]
  1289. func (this *BaseFromThsHfController) Save2EdbPre() {
  1290. br := new(models.BaseResponse).Init()
  1291. defer func() {
  1292. if br.ErrMsg == "" {
  1293. br.IsSendEmail = false
  1294. }
  1295. this.Data["json"] = br
  1296. this.ServeJSON()
  1297. }()
  1298. sysUser := this.SysUser
  1299. if sysUser == nil {
  1300. br.Msg = "请登录"
  1301. br.ErrMsg = "请登录,SysUser Is Empty"
  1302. br.Ret = 408
  1303. return
  1304. }
  1305. var params data_manage.ThsHfIndexMultiSave2EdbPreReq
  1306. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  1307. br.Msg = "参数解析异常"
  1308. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1309. return
  1310. }
  1311. if len(params.IndexIds) == 0 {
  1312. br.Msg = "请选择指标"
  1313. return
  1314. }
  1315. convertRule := params.ConvertRule
  1316. if convertRule.ConvertType != 1 && convertRule.ConvertType != 2 {
  1317. br.Msg = "请选择数据转换方式"
  1318. return
  1319. }
  1320. // 生成的指标名称后缀
  1321. var (
  1322. suffixName string
  1323. startTimeCn string
  1324. endTimeCn string
  1325. calculateCn string
  1326. )
  1327. dayMap := map[int]string{1: "当日", 2: "前日"}
  1328. calculateMap := map[int]string{1: "均值", 2: "最大值", 3: "最小值"}
  1329. if convertRule.ConvertType == 1 {
  1330. if convertRule.ConvertFixed.FixedDay != 1 && convertRule.ConvertFixed.FixedDay != 2 {
  1331. br.Msg = "请选择指定时间"
  1332. return
  1333. }
  1334. if convertRule.ConvertFixed.FixedTime == "" {
  1335. br.Msg = "请选择指定时间"
  1336. return
  1337. }
  1338. timePrefix := time.Now().Local().Format(utils.FormatDate)
  1339. st := fmt.Sprintf("%s %s", timePrefix, convertRule.ConvertFixed.FixedTime)
  1340. startTime, e := time.Parse(utils.FormatDateTime, st)
  1341. if e != nil {
  1342. br.Msg = "指定时间格式有误"
  1343. return
  1344. }
  1345. startTimeCn = fmt.Sprintf("%s%s", dayMap[convertRule.ConvertFixed.FixedDay], startTime.Format("15点04"))
  1346. }
  1347. if convertRule.ConvertType == 2 {
  1348. if convertRule.ConvertArea.StartDay != 1 && convertRule.ConvertArea.StartDay != 2 {
  1349. br.Msg = "请选择起始时间"
  1350. return
  1351. }
  1352. if convertRule.ConvertArea.StartTime == "" {
  1353. br.Msg = "请选择起始时间"
  1354. return
  1355. }
  1356. var startTimePre string
  1357. if convertRule.ConvertArea.StartDay == 1 {
  1358. startTimePre = time.Now().Local().Format(utils.FormatDate)
  1359. }
  1360. if convertRule.ConvertArea.StartDay == 2 {
  1361. startTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1362. }
  1363. st := fmt.Sprintf("%s %s", startTimePre, convertRule.ConvertArea.StartTime)
  1364. startTime, e := time.Parse(utils.FormatDateTime, st)
  1365. if e != nil {
  1366. br.Msg = "起始时间格式有误"
  1367. return
  1368. }
  1369. startTimeCn = fmt.Sprintf("%s%s", dayMap[convertRule.ConvertArea.StartDay], startTime.Format("15点04"))
  1370. if convertRule.ConvertArea.EndDay != 1 && convertRule.ConvertArea.EndDay != 2 {
  1371. br.Msg = "请选择截止时间"
  1372. return
  1373. }
  1374. if convertRule.ConvertArea.EndTime == "" {
  1375. br.Msg = "请选择截止时间"
  1376. return
  1377. }
  1378. var endTimePre string
  1379. if convertRule.ConvertArea.EndDay == 1 {
  1380. endTimePre = time.Now().Local().Format(utils.FormatDate)
  1381. }
  1382. if convertRule.ConvertArea.EndDay == 2 {
  1383. endTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1384. }
  1385. ed := fmt.Sprintf("%s %s", endTimePre, convertRule.ConvertArea.EndTime)
  1386. endTime, e := time.Parse(utils.FormatDateTime, ed)
  1387. if e != nil {
  1388. br.Msg = "截止时间格式有误"
  1389. return
  1390. }
  1391. endTimeCn = fmt.Sprintf("至%s%s", dayMap[convertRule.ConvertArea.EndDay], endTime.Format("15点04"))
  1392. if startTime.After(endTime) {
  1393. br.Msg = "起始日期不可早于截止日期"
  1394. return
  1395. }
  1396. calculateCn = calculateMap[convertRule.ConvertArea.CalculateType]
  1397. }
  1398. suffixName = fmt.Sprint(startTimeCn, endTimeCn, calculateCn)
  1399. // 查询指标
  1400. indexOb := new(data_manage.BaseFromThsHfIndex)
  1401. cond := fmt.Sprintf(" AND %s IN (%s)", indexOb.Cols().PrimaryId, utils.GetOrmInReplace(len(params.IndexIds)))
  1402. pars := make([]interface{}, 0)
  1403. pars = append(pars, params.IndexIds)
  1404. list, e := indexOb.GetItemsByCondition(cond, pars, []string{}, "")
  1405. if e != nil {
  1406. br.Msg = "获取失败"
  1407. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  1408. return
  1409. }
  1410. resp := make([]*data_manage.ThsHfIndexMultiSave2EdbPreItem, 0)
  1411. for _, v := range list {
  1412. t := new(data_manage.ThsHfIndexMultiSave2EdbPreItem)
  1413. t.IndexId = v.BaseFromThsHfIndexId
  1414. t.IndexCode = v.IndexCode
  1415. t.IndexName = v.IndexName
  1416. edbCn := data_manage.ThsHfEdbCodeCn[v.Indicator]
  1417. if edbCn == "" {
  1418. edbCn = v.Indicator
  1419. }
  1420. t.NewIndexName = fmt.Sprint(v.StockCode, v.Frequency, edbCn, suffixName)
  1421. t.StockCode = v.StockCode
  1422. t.EdbCode = v.Indicator
  1423. t.Unit = v.Unit
  1424. t.Frequency = v.Frequency
  1425. t.NewFrequency = "日度"
  1426. resp = append(resp, t)
  1427. }
  1428. br.Data = resp
  1429. br.Ret = 200
  1430. br.Success = true
  1431. br.Msg = "获取成功"
  1432. }
  1433. // Save2Edb
  1434. // @Title 批量新增指标库
  1435. // @Description 批量新增指标库
  1436. // @Param request body data_manage.ThsHfIndexMultiSave2EdbReq true "type json string"
  1437. // @Success 200 string "操作成功"
  1438. // @router /ths_hf/index/save2edb [post]
  1439. func (this *BaseFromThsHfController) Save2Edb() {
  1440. br := new(models.BaseResponse).Init()
  1441. defer func() {
  1442. if br.ErrMsg == "" {
  1443. br.IsSendEmail = false
  1444. }
  1445. this.Data["json"] = br
  1446. this.ServeJSON()
  1447. }()
  1448. sysUser := this.SysUser
  1449. if sysUser == nil {
  1450. br.Msg = "请登录"
  1451. br.ErrMsg = "请登录,SysUser Is Empty"
  1452. br.Ret = 408
  1453. return
  1454. }
  1455. var params data_manage.ThsHfIndexMultiSave2EdbReq
  1456. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &params); e != nil {
  1457. br.Msg = "参数解析异常"
  1458. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1459. return
  1460. }
  1461. if len(params.NewIndexes) == 0 {
  1462. br.Msg = "请选择指标"
  1463. return
  1464. }
  1465. convertRule := params.ConvertRule
  1466. if convertRule.ConvertType != 1 && convertRule.ConvertType != 2 {
  1467. br.Msg = "请选择数据转换方式"
  1468. return
  1469. }
  1470. if convertRule.ConvertType == 1 {
  1471. if convertRule.ConvertFixed.FixedDay != 1 && convertRule.ConvertFixed.FixedDay != 2 {
  1472. br.Msg = "请选择指定时间"
  1473. return
  1474. }
  1475. if convertRule.ConvertFixed.FixedTime == "" {
  1476. br.Msg = "请选择指定时间"
  1477. return
  1478. }
  1479. timePrefix := time.Now().Local().Format(utils.FormatDate)
  1480. st := fmt.Sprintf("%s %s", timePrefix, convertRule.ConvertFixed.FixedTime)
  1481. _, e := time.Parse(utils.FormatDateTime, st)
  1482. if e != nil {
  1483. br.Msg = "指定时间格式有误"
  1484. return
  1485. }
  1486. }
  1487. if convertRule.ConvertType == 2 {
  1488. if convertRule.ConvertArea.StartDay != 1 && convertRule.ConvertArea.StartDay != 2 {
  1489. br.Msg = "请选择起始时间"
  1490. return
  1491. }
  1492. if convertRule.ConvertArea.StartTime == "" {
  1493. br.Msg = "请选择起始时间"
  1494. return
  1495. }
  1496. var startTimePre string
  1497. if convertRule.ConvertArea.StartDay == 1 {
  1498. startTimePre = time.Now().Local().Format(utils.FormatDate)
  1499. }
  1500. if convertRule.ConvertArea.StartDay == 2 {
  1501. startTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1502. }
  1503. st := fmt.Sprintf("%s %s", startTimePre, convertRule.ConvertArea.StartTime)
  1504. startTime, e := time.Parse(utils.FormatDateTime, st)
  1505. if e != nil {
  1506. br.Msg = "起始时间格式有误"
  1507. return
  1508. }
  1509. if convertRule.ConvertArea.EndDay != 1 && convertRule.ConvertArea.EndDay != 2 {
  1510. br.Msg = "请选择截止时间"
  1511. return
  1512. }
  1513. if convertRule.ConvertArea.EndTime == "" {
  1514. br.Msg = "请选择截止时间"
  1515. return
  1516. }
  1517. var endTimePre string
  1518. if convertRule.ConvertArea.EndDay == 1 {
  1519. endTimePre = time.Now().Local().Format(utils.FormatDate)
  1520. }
  1521. if convertRule.ConvertArea.EndDay == 2 {
  1522. endTimePre = time.Now().Local().AddDate(0, 0, -1).Format(utils.FormatDate)
  1523. }
  1524. ed := fmt.Sprintf("%s %s", endTimePre, convertRule.ConvertArea.EndTime)
  1525. endTime, e := time.Parse(utils.FormatDateTime, ed)
  1526. if e != nil {
  1527. br.Msg = "截止时间格式有误"
  1528. return
  1529. }
  1530. if startTime.After(endTime) {
  1531. br.Msg = "起始日期不可早于截止日期"
  1532. return
  1533. }
  1534. }
  1535. resp := new(data_manage.ThsHfIndexMultiSave2EdbResp)
  1536. // 判断指标名称是否重复
  1537. edbNameExist := make(map[string]bool)
  1538. {
  1539. items, e := data_manage.GetEdbInfoFieldList(``, make([]interface{}, 0), []string{"edb_name"})
  1540. if e != nil {
  1541. br.Msg = "操作失败"
  1542. br.ErrMsg = fmt.Sprintf("获取指标名称列表失败, %v", e)
  1543. return
  1544. }
  1545. for _, v := range items {
  1546. edbNameExist[v.EdbName] = true
  1547. }
  1548. }
  1549. for _, v := range params.NewIndexes {
  1550. if edbNameExist[v.NewIndexName] {
  1551. resp.Exist = append(resp.Exist, v)
  1552. }
  1553. }
  1554. if len(resp.Exist) > 0 {
  1555. br.Data = resp
  1556. br.Ret = 200
  1557. br.Success = true
  1558. br.Msg = "含重复指标名称"
  1559. return
  1560. }
  1561. // 请求指标库新增
  1562. for _, v := range params.NewIndexes {
  1563. var r data_manage.ThsHfIndexMultiSave2EdbLibReq
  1564. r.ConvertRule = convertRule
  1565. v.SysAdminId = sysUser.AdminId
  1566. v.SysAdminName = sysUser.RealName
  1567. r.NewIndex = v
  1568. b, e := json.Marshal(r)
  1569. if e != nil {
  1570. v.Tips = "新增失败"
  1571. v.ErrMsg = e.Error()
  1572. resp.Fail = append(resp.Fail, v)
  1573. continue
  1574. }
  1575. res, e := data.AddBaseEdbInfo(string(b), utils.DATA_SOURCE_THS, utils.DATA_SUB_SOURCE_HIGH_FREQUENCY, this.Lang)
  1576. if e != nil {
  1577. v.Tips = res.Msg
  1578. v.ErrMsg = e.Error()
  1579. resp.Fail = append(resp.Fail, v)
  1580. continue
  1581. }
  1582. if res.Ret != 200 {
  1583. v.Tips = res.Msg
  1584. v.ErrMsg = fmt.Sprintf("新增失败, ErrMsg: %s", res.ErrMsg)
  1585. resp.Fail = append(resp.Fail, v)
  1586. continue
  1587. }
  1588. v.Tips = "新增成功"
  1589. resp.Success = append(resp.Success, v)
  1590. }
  1591. br.Data = resp
  1592. br.Ret = 200
  1593. br.Success = true
  1594. br.Msg = "操作成功"
  1595. }