trade_analysis.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. package trade_analysis
  2. import (
  3. "eta/eta_api/controllers"
  4. "eta/eta_api/models"
  5. tradeAnalysisModel "eta/eta_api/models/data_manage/trade_analysis"
  6. "eta/eta_api/services/data/trade_analysis"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "strings"
  10. )
  11. // TradeAnalysisController 供应分析
  12. type TradeAnalysisController struct {
  13. controllers.BaseAuthController
  14. }
  15. // GetClassifyName
  16. // @Title 获取交易所合约列表
  17. // @Description 获取交易所合约列表
  18. // @Param request body saModel.SaCompareSaveReq true "type json string"
  19. // @Success 200 {object} trade_analysis.TradeClassifyNameListSort
  20. // @router /classify [get]
  21. func (c *TradeAnalysisController) GetClassifyName() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. if br.ErrMsg == "" {
  25. br.IsSendEmail = false
  26. }
  27. c.Data["json"] = br
  28. c.ServeJSON()
  29. }()
  30. //userinfo := userService.GetInfoByClaims(c)
  31. list, err, errMsg := trade_analysis.GetClassifyName(c.Lang)
  32. if err != nil {
  33. br.Msg = errMsg
  34. br.ErrMsg = "获取失败, ErrMsg: " + err.Error()
  35. return
  36. }
  37. br.Data = list
  38. br.Ret = 200
  39. br.Success = true
  40. br.Msg = "获取成功"
  41. }
  42. // GetPositionTop
  43. // @Title 获取合约榜单详情
  44. // @Description 获取合约榜单详情
  45. // @Param Exchange query string false "交易所"
  46. // @Param ClassifyName query string false "分类名称"
  47. // @Param ClassifyType query string false "具体合约名称"
  48. // @Param DataTime query string false "请求日期,如果为空,则返回最新的榜单日期"
  49. // @Success 200 {object} trade_analysis.GetPositionTopResp
  50. // @Success 200 Ret=200 操作成功
  51. // @router /top [get]
  52. func (c *TradeAnalysisController) GetPositionTop() {
  53. br := new(models.BaseResponse).Init()
  54. defer func() {
  55. if br.ErrMsg == "" {
  56. br.IsSendEmail = false
  57. }
  58. c.Data["json"] = br
  59. c.ServeJSON()
  60. }()
  61. req := tradeAnalysisModel.GetPositionTopReq{
  62. Exchange: c.GetString("Exchange"),
  63. ClassifyName: c.GetString("ClassifyName"),
  64. ClassifyType: c.GetString("ClassifyType"),
  65. DataTime: c.GetString("DataTime"),
  66. }
  67. if req.ClassifyName == "" {
  68. br.Msg = "请输入分类名称"
  69. br.IsSendEmail = false
  70. return
  71. }
  72. if req.ClassifyType == "" {
  73. br.Msg = "请输入合约名称"
  74. br.IsSendEmail = false
  75. return
  76. }
  77. list, err, errMsg := trade_analysis.GetPositionTopDetail(req)
  78. if err != nil {
  79. br.Msg = errMsg
  80. br.ErrMsg = err.Error()
  81. return
  82. }
  83. br.Data = list
  84. br.Ret = 200
  85. br.Success = true
  86. return
  87. }
  88. // GetTradeExchangeList
  89. // @Title 获取交易所列表
  90. // @Description 获取交易所列表
  91. // @Param IsTotal query bool false "是否显示全部"
  92. // @Success 200 {object} data_manage.BaseFromTradeExchangeItem
  93. // @router /exchange_list [get]
  94. func (this *TradeAnalysisController) GetTradeExchangeList() {
  95. br := new(models.BaseResponse).Init()
  96. defer func() {
  97. if br.ErrMsg == "" {
  98. br.IsSendEmail = false
  99. }
  100. this.Data["json"] = br
  101. this.ServeJSON()
  102. }()
  103. sysUser := this.SysUser
  104. if sysUser == nil {
  105. br.Msg = "请登录"
  106. br.ErrMsg = "请登录,SysUser Is Empty"
  107. br.Ret = 408
  108. return
  109. }
  110. isTotal, _ := this.GetBool("IsTotal", false)
  111. var cond string
  112. var pars []interface{}
  113. exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange)
  114. if !isTotal {
  115. cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().AnalysisState)
  116. pars = append(pars, 1)
  117. }
  118. list, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "")
  119. if e != nil {
  120. br.Msg = "获取失败"
  121. br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e)
  122. return
  123. }
  124. resp := make([]*tradeAnalysisModel.BaseFromTradeExchangeItem, 0)
  125. for _, v := range list {
  126. resp = append(resp, v.Format2Item())
  127. }
  128. br.Data = resp
  129. br.Msg = "获取成功"
  130. br.Ret = 200
  131. br.Success = true
  132. }
  133. // GetTradeClassifyList
  134. // @Title 获取品种列表
  135. // @Description 获取品种列表
  136. // @Param Exchange query string false "交易所标识"
  137. // @Param ClassifyName query string false "品种名称"
  138. // @Success 200 {object} tradeAnalysisModel.BaseFromTradeClassifyItem
  139. // @router /classify_list [get]
  140. func (this *TradeAnalysisController) GetTradeClassifyList() {
  141. br := new(models.BaseResponse).Init()
  142. defer func() {
  143. if br.ErrMsg == "" {
  144. br.IsSendEmail = false
  145. }
  146. this.Data["json"] = br
  147. this.ServeJSON()
  148. }()
  149. sysUser := this.SysUser
  150. if sysUser == nil {
  151. br.Msg = "请登录"
  152. br.ErrMsg = "请登录,SysUser Is Empty"
  153. br.Ret = 408
  154. return
  155. }
  156. exchange := this.GetString("Exchange")
  157. exchange = strings.TrimSpace(exchange)
  158. classifyName := this.GetString("ClassifyName")
  159. classifyName = strings.TrimSpace(classifyName)
  160. // 交易所名称
  161. exchangeName := make(map[string]string)
  162. {
  163. var cond string
  164. var pars []interface{}
  165. exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange)
  166. if exchange != "" {
  167. cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().Exchange)
  168. pars = append(pars, exchange)
  169. item, e := exchangeOb.GetItemByCondition(cond, pars, "")
  170. if e != nil {
  171. br.Msg = "获取失败"
  172. br.ErrMsg = fmt.Sprintf("获取交易所信息失败, %v", e)
  173. return
  174. }
  175. exchangeName[item.Exchange] = item.ExchangeName
  176. } else {
  177. list, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "")
  178. if e != nil {
  179. br.Msg = "获取失败"
  180. br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e)
  181. return
  182. }
  183. for _, v := range list {
  184. exchangeName[v.Exchange] = v.ExchangeName
  185. }
  186. }
  187. }
  188. // 搜索品种
  189. var cond string
  190. var pars []interface{}
  191. classifyOb := new(tradeAnalysisModel.BaseFromTradeClassify)
  192. if exchange != "" {
  193. cond += fmt.Sprintf(` AND %s = ?`, classifyOb.Cols().Exchange)
  194. pars = append(pars, exchange)
  195. }
  196. if classifyName != "" {
  197. cond += fmt.Sprintf(` AND %s LIKE ?`, classifyOb.Cols().ClassifyName)
  198. pars = append(pars, fmt.Sprint("%", classifyName, "%"))
  199. }
  200. fields := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().Exchange}
  201. list, e := classifyOb.GetClassifyItemsByCondition(cond, pars, fields, "id ASC")
  202. if e != nil {
  203. br.Msg = "获取失败"
  204. br.ErrMsg = fmt.Sprintf("获取品种列表失败, %v", e)
  205. return
  206. }
  207. resp := make([]*tradeAnalysisModel.BaseFromTradeClassifyItem, 0)
  208. classifyExist := make(map[string]bool)
  209. for _, v := range list {
  210. // 郑商所
  211. if v.Exchange == tradeAnalysisModel.TradeExchangeZhengzhou {
  212. name := trade_analysis.GetZhengzhouClassifyName(v.ClassifyName)
  213. if classifyExist[name] {
  214. continue
  215. }
  216. classifyExist[name] = true
  217. resp = append(resp, &tradeAnalysisModel.BaseFromTradeClassifyItem{
  218. ClassifyName: name,
  219. Exchange: v.Exchange,
  220. ExchangeName: exchangeName[v.Exchange],
  221. })
  222. continue
  223. }
  224. resp = append(resp, &tradeAnalysisModel.BaseFromTradeClassifyItem{
  225. ClassifyName: v.ClassifyName,
  226. Exchange: v.Exchange,
  227. ExchangeName: exchangeName[v.Exchange],
  228. })
  229. }
  230. br.Data = resp
  231. br.Msg = "获取成功"
  232. br.Ret = 200
  233. br.Success = true
  234. }
  235. // GetTradeContractList
  236. // @Title 获取合约列表
  237. // @Description 获取合约列表
  238. // @Param Exchange query string false "交易所标识"
  239. // @Param ClassifyName query string false "品种名称"
  240. // @Param ContractName query string false "合约名称"
  241. // @Param ExceptTop query bool false "排除TOP20: true-不包含; false-包含(默认)"
  242. // @Success 200 {object} tradeAnalysisModel.BaseFromTradeContractItem
  243. // @router /contract_list [get]
  244. func (this *TradeAnalysisController) GetTradeContractList() {
  245. br := new(models.BaseResponse).Init()
  246. defer func() {
  247. if br.ErrMsg == "" {
  248. br.IsSendEmail = false
  249. }
  250. this.Data["json"] = br
  251. this.ServeJSON()
  252. }()
  253. sysUser := this.SysUser
  254. if sysUser == nil {
  255. br.Msg = "请登录"
  256. br.ErrMsg = "请登录,SysUser Is Empty"
  257. br.Ret = 408
  258. return
  259. }
  260. exchange := this.GetString("Exchange")
  261. exchange = strings.TrimSpace(exchange)
  262. classifyName := this.GetString("ClassifyName")
  263. classifyName = strings.TrimSpace(classifyName)
  264. contractName := this.GetString("ContractName")
  265. contractName = strings.TrimSpace(contractName)
  266. exceptTop, _ := this.GetBool("ExceptTop")
  267. // 交易所名称
  268. exchangeName := make(map[string]string)
  269. {
  270. var cond string
  271. var pars []interface{}
  272. exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange)
  273. if exchange != "" {
  274. cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().Exchange)
  275. pars = append(pars, exchange)
  276. item, e := exchangeOb.GetItemByCondition(cond, pars, "")
  277. if e != nil {
  278. br.Msg = "获取失败"
  279. br.ErrMsg = fmt.Sprintf("获取交易所信息失败, %v", e)
  280. return
  281. }
  282. exchangeName[item.Exchange] = item.ExchangeName
  283. } else {
  284. list, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "")
  285. if e != nil {
  286. br.Msg = "获取失败"
  287. br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e)
  288. return
  289. }
  290. for _, v := range list {
  291. exchangeName[v.Exchange] = v.ExchangeName
  292. }
  293. }
  294. }
  295. // 搜索合约
  296. var cond string
  297. var pars []interface{}
  298. classifyOb := new(tradeAnalysisModel.BaseFromTradeClassify)
  299. if exchange != "" {
  300. cond += fmt.Sprintf(` AND %s = ?`, classifyOb.Cols().Exchange)
  301. pars = append(pars, exchange)
  302. }
  303. // 郑商所品种名称在列表中过滤
  304. if classifyName != "" {
  305. cond += fmt.Sprintf(` AND ((%s = ? AND %s <> ?) OR %s = ?)`, classifyOb.Cols().ClassifyName, classifyOb.Cols().Exchange, classifyOb.Cols().Exchange)
  306. pars = append(pars, classifyName, tradeAnalysisModel.TradeExchangeZhengzhou, tradeAnalysisModel.TradeExchangeZhengzhou)
  307. }
  308. if contractName != "" {
  309. cond += fmt.Sprintf(` AND %s LIKE ?`, classifyOb.Cols().ClassifyType)
  310. pars = append(pars, fmt.Sprint("%", contractName, "%"))
  311. }
  312. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s DESC", classifyOb.Cols().Exchange, classifyOb.Cols().LatestDate))
  313. if e != nil {
  314. br.Msg = "获取失败"
  315. br.ErrMsg = fmt.Sprintf("获取合约列表失败, %v", e)
  316. return
  317. }
  318. resp := make([]*tradeAnalysisModel.BaseFromTradeContractItem, 0)
  319. if !exceptTop && contractName == "" {
  320. resp = append(resp, &tradeAnalysisModel.BaseFromTradeContractItem{
  321. ClassifyType: tradeAnalysisModel.TradeFuturesCompanyTop20,
  322. })
  323. }
  324. for _, v := range list {
  325. // 郑商所
  326. classifyType := v.ClassifyType
  327. if classifyName != "" && v.Exchange == tradeAnalysisModel.TradeExchangeZhengzhou {
  328. name := trade_analysis.GetZhengzhouClassifyName(v.ClassifyName)
  329. if classifyName != name {
  330. continue
  331. }
  332. classifyType = v.ClassifyName
  333. }
  334. resp = append(resp, &tradeAnalysisModel.BaseFromTradeContractItem{
  335. ClassifyName: v.ClassifyName,
  336. ClassifyType: classifyType,
  337. Exchange: v.Exchange,
  338. ExchangeName: exchangeName[v.Exchange],
  339. LatestDate: v.LatestDate.Format(utils.FormatDate),
  340. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  341. })
  342. }
  343. br.Data = resp
  344. br.Msg = "获取成功"
  345. br.Ret = 200
  346. br.Success = true
  347. }
  348. // GetTradeFuturesCompanyList
  349. // @Title 获取期货公司列表
  350. // @Description 获取期货公司列表
  351. // @Param CompanyName query string false "公司名称"
  352. // @Success 200 {object} tradeAnalysisModel.BaseFromTradeContractItem
  353. // @router /company_list [get]
  354. func (this *TradeAnalysisController) GetTradeFuturesCompanyList() {
  355. br := new(models.BaseResponse).Init()
  356. defer func() {
  357. if br.ErrMsg == "" {
  358. br.IsSendEmail = false
  359. }
  360. this.Data["json"] = br
  361. this.ServeJSON()
  362. }()
  363. sysUser := this.SysUser
  364. if sysUser == nil {
  365. br.Msg = "请登录"
  366. br.ErrMsg = "请登录,SysUser Is Empty"
  367. br.Ret = 408
  368. return
  369. }
  370. companyName := this.GetString("CompanyName")
  371. companyName = strings.TrimSpace(companyName)
  372. var cond string
  373. var pars []interface{}
  374. companyOb := new(tradeAnalysisModel.TradeFuturesCompany)
  375. if companyName != "" {
  376. cond += fmt.Sprintf(` AND %s LIKE ?`, companyOb.Cols().CompanyName)
  377. pars = append(pars, fmt.Sprint("%", companyName, "%"))
  378. }
  379. list, e := companyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", companyOb.Cols().Sort))
  380. if e != nil {
  381. br.Msg = "获取失败"
  382. br.ErrMsg = fmt.Sprintf("获取期货公司列表失败, %v", e)
  383. return
  384. }
  385. resp := make([]*tradeAnalysisModel.TradeFuturesCompanyItem, 0)
  386. for _, v := range list {
  387. resp = append(resp, &tradeAnalysisModel.TradeFuturesCompanyItem{
  388. CompanyId: v.TradeFuturesCompanyId,
  389. CompanyName: v.CompanyName,
  390. Sort: v.Sort,
  391. })
  392. }
  393. br.Data = resp
  394. br.Msg = "获取成功"
  395. br.Ret = 200
  396. br.Success = true
  397. }
  398. // GetTradeExchangeClassifyTree
  399. // @Title 获取交易所-品种树
  400. // @Description 获取交易所-品种树
  401. // @Param IsTotal query bool false "是否显示全部交易所"
  402. // @Success 200 {object} data_manage.BaseFromTradeExchangeItem
  403. // @router /exchange_classify/tree [get]
  404. func (this *TradeAnalysisController) GetTradeExchangeClassifyTree() {
  405. br := new(models.BaseResponse).Init()
  406. defer func() {
  407. if br.ErrMsg == "" {
  408. br.IsSendEmail = false
  409. }
  410. this.Data["json"] = br
  411. this.ServeJSON()
  412. }()
  413. sysUser := this.SysUser
  414. if sysUser == nil {
  415. br.Msg = "请登录"
  416. br.ErrMsg = "请登录,SysUser Is Empty"
  417. br.Ret = 408
  418. return
  419. }
  420. isTotal, _ := this.GetBool("IsTotal", false)
  421. // 交易所
  422. var cond string
  423. var pars []interface{}
  424. exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange)
  425. if !isTotal {
  426. cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().AnalysisState)
  427. pars = append(pars, 1)
  428. }
  429. exchanges, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "")
  430. if e != nil {
  431. br.Msg = "获取失败"
  432. br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e)
  433. return
  434. }
  435. // 品种
  436. classifyOb := new(tradeAnalysisModel.BaseFromTradeClassify)
  437. fields := []string{classifyOb.Cols().ClassifyName, classifyOb.Cols().Exchange}
  438. classifies, e := classifyOb.GetClassifyItemsByCondition(``, make([]interface{}, 0), fields, "id ASC")
  439. if e != nil {
  440. br.Msg = "获取失败"
  441. br.ErrMsg = fmt.Sprintf("获取品种列表失败, %v", e)
  442. return
  443. }
  444. exchangeClassify := make(map[string][]*tradeAnalysisModel.BaseFromTradeClassify, 0)
  445. for _, v := range classifies {
  446. if exchangeClassify[v.Exchange] == nil {
  447. exchangeClassify[v.Exchange] = make([]*tradeAnalysisModel.BaseFromTradeClassify, 0)
  448. }
  449. exchangeClassify[v.Exchange] = append(exchangeClassify[v.Exchange], v)
  450. }
  451. // 构建树
  452. resp := make([]*tradeAnalysisModel.TradeExchangeClassifyNode, 0)
  453. classifyExist := make(map[string]bool)
  454. for _, v := range exchanges {
  455. t := new(tradeAnalysisModel.TradeExchangeClassifyNode)
  456. t.UniqueFlag = v.Exchange
  457. t.NodeType = 1
  458. t.NodeName = v.ExchangeName
  459. t.NodeValue = v.Exchange
  460. t.Children = make([]*tradeAnalysisModel.TradeExchangeClassifyNode, 0)
  461. for _, c := range exchangeClassify[v.Exchange] {
  462. // 郑商所
  463. classifyName := c.ClassifyName
  464. if v.Exchange == tradeAnalysisModel.TradeExchangeZhengzhou {
  465. classifyName = trade_analysis.GetZhengzhouClassifyName(c.ClassifyName)
  466. if classifyExist[classifyName] {
  467. continue
  468. }
  469. classifyExist[classifyName] = true
  470. }
  471. tc := new(tradeAnalysisModel.TradeExchangeClassifyNode)
  472. tc.UniqueFlag = fmt.Sprintf("%s-%s", v.Exchange, classifyName)
  473. tc.ParentFlag = v.Exchange
  474. tc.NodeType = 2
  475. tc.NodeName = classifyName
  476. tc.NodeValue = classifyName
  477. t.Children = append(t.Children, tc)
  478. }
  479. resp = append(resp, t)
  480. }
  481. br.Data = resp
  482. br.Msg = "获取成功"
  483. br.Ret = 200
  484. br.Success = true
  485. }