trade_analysis.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 name == "" {
  214. continue
  215. }
  216. if classifyExist[name] {
  217. continue
  218. }
  219. classifyExist[name] = true
  220. resp = append(resp, &tradeAnalysisModel.BaseFromTradeClassifyItem{
  221. ClassifyName: name,
  222. Exchange: v.Exchange,
  223. ExchangeName: exchangeName[v.Exchange],
  224. })
  225. continue
  226. }
  227. resp = append(resp, &tradeAnalysisModel.BaseFromTradeClassifyItem{
  228. ClassifyName: v.ClassifyName,
  229. Exchange: v.Exchange,
  230. ExchangeName: exchangeName[v.Exchange],
  231. })
  232. }
  233. br.Data = resp
  234. br.Msg = "获取成功"
  235. br.Ret = 200
  236. br.Success = true
  237. }
  238. // GetTradeContractList
  239. // @Title 获取合约列表
  240. // @Description 获取合约列表
  241. // @Param Exchange query string false "交易所标识"
  242. // @Param ClassifyName query string false "品种名称"
  243. // @Param ContractName query string false "合约名称"
  244. // @Param ExceptTop query bool false "排除TOP20: true-不包含; false-包含(默认)"
  245. // @Success 200 {object} tradeAnalysisModel.BaseFromTradeContractItem
  246. // @router /contract_list [get]
  247. func (this *TradeAnalysisController) GetTradeContractList() {
  248. br := new(models.BaseResponse).Init()
  249. defer func() {
  250. if br.ErrMsg == "" {
  251. br.IsSendEmail = false
  252. }
  253. this.Data["json"] = br
  254. this.ServeJSON()
  255. }()
  256. sysUser := this.SysUser
  257. if sysUser == nil {
  258. br.Msg = "请登录"
  259. br.ErrMsg = "请登录,SysUser Is Empty"
  260. br.Ret = 408
  261. return
  262. }
  263. exchange := this.GetString("Exchange")
  264. exchange = strings.TrimSpace(exchange)
  265. classifyName := this.GetString("ClassifyName")
  266. classifyName = strings.TrimSpace(classifyName)
  267. contractName := this.GetString("ContractName")
  268. contractName = strings.TrimSpace(contractName)
  269. exceptTop, _ := this.GetBool("ExceptTop")
  270. // 交易所名称
  271. exchangeName := make(map[string]string)
  272. {
  273. var cond string
  274. var pars []interface{}
  275. exchangeOb := new(tradeAnalysisModel.BaseFromTradeExchange)
  276. if exchange != "" {
  277. cond += fmt.Sprintf(` AND %s = ?`, exchangeOb.Cols().Exchange)
  278. pars = append(pars, exchange)
  279. item, e := exchangeOb.GetItemByCondition(cond, pars, "")
  280. if e != nil {
  281. br.Msg = "获取失败"
  282. br.ErrMsg = fmt.Sprintf("获取交易所信息失败, %v", e)
  283. return
  284. }
  285. exchangeName[item.Exchange] = item.ExchangeName
  286. } else {
  287. list, e := exchangeOb.GetItemsByCondition(cond, pars, []string{}, "")
  288. if e != nil {
  289. br.Msg = "获取失败"
  290. br.ErrMsg = fmt.Sprintf("获取交易所列表失败, %v", e)
  291. return
  292. }
  293. for _, v := range list {
  294. exchangeName[v.Exchange] = v.ExchangeName
  295. }
  296. }
  297. }
  298. // 搜索合约
  299. var cond string
  300. var pars []interface{}
  301. classifyOb := new(tradeAnalysisModel.BaseFromTradeClassify)
  302. if exchange != "" {
  303. cond += fmt.Sprintf(` AND %s = ?`, classifyOb.Cols().Exchange)
  304. pars = append(pars, exchange)
  305. }
  306. // 郑商所品种名称在列表中过滤
  307. if classifyName != "" {
  308. cond += fmt.Sprintf(` AND ((%s = ? AND %s <> ?) OR %s = ?)`, classifyOb.Cols().ClassifyName, classifyOb.Cols().Exchange, classifyOb.Cols().Exchange)
  309. pars = append(pars, classifyName, tradeAnalysisModel.TradeExchangeZhengzhou, tradeAnalysisModel.TradeExchangeZhengzhou)
  310. }
  311. if contractName != "" {
  312. cond += fmt.Sprintf(` AND %s LIKE ?`, classifyOb.Cols().ClassifyType)
  313. pars = append(pars, fmt.Sprint("%", contractName, "%"))
  314. }
  315. list, e := classifyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s DESC", classifyOb.Cols().Exchange, classifyOb.Cols().LatestDate))
  316. if e != nil {
  317. br.Msg = "获取失败"
  318. br.ErrMsg = fmt.Sprintf("获取合约列表失败, %v", e)
  319. return
  320. }
  321. resp := make([]*tradeAnalysisModel.BaseFromTradeContractItem, 0)
  322. if !exceptTop && contractName == "" {
  323. resp = append(resp, &tradeAnalysisModel.BaseFromTradeContractItem{
  324. ClassifyType: tradeAnalysisModel.TradeFuturesCompanyTop20,
  325. })
  326. }
  327. for _, v := range list {
  328. // 郑商所
  329. classifyType := v.ClassifyType
  330. if classifyName != "" && v.Exchange == tradeAnalysisModel.TradeExchangeZhengzhou {
  331. name := trade_analysis.GetZhengzhouClassifyName(v.ClassifyName)
  332. if classifyName != name {
  333. continue
  334. }
  335. classifyType = v.ClassifyName
  336. }
  337. resp = append(resp, &tradeAnalysisModel.BaseFromTradeContractItem{
  338. ClassifyName: v.ClassifyName,
  339. ClassifyType: classifyType,
  340. Exchange: v.Exchange,
  341. ExchangeName: exchangeName[v.Exchange],
  342. LatestDate: v.LatestDate.Format(utils.FormatDate),
  343. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  344. })
  345. }
  346. br.Data = resp
  347. br.Msg = "获取成功"
  348. br.Ret = 200
  349. br.Success = true
  350. }
  351. // GetTradeFuturesCompanyList
  352. // @Title 获取期货公司列表
  353. // @Description 获取期货公司列表
  354. // @Param CompanyName query string false "公司名称"
  355. // @Success 200 {object} tradeAnalysisModel.BaseFromTradeContractItem
  356. // @router /company_list [get]
  357. func (this *TradeAnalysisController) GetTradeFuturesCompanyList() {
  358. br := new(models.BaseResponse).Init()
  359. defer func() {
  360. if br.ErrMsg == "" {
  361. br.IsSendEmail = false
  362. }
  363. this.Data["json"] = br
  364. this.ServeJSON()
  365. }()
  366. sysUser := this.SysUser
  367. if sysUser == nil {
  368. br.Msg = "请登录"
  369. br.ErrMsg = "请登录,SysUser Is Empty"
  370. br.Ret = 408
  371. return
  372. }
  373. companyName := this.GetString("CompanyName")
  374. companyName = strings.TrimSpace(companyName)
  375. var cond string
  376. var pars []interface{}
  377. companyOb := new(tradeAnalysisModel.TradeFuturesCompany)
  378. if companyName != "" {
  379. cond += fmt.Sprintf(` AND %s LIKE ?`, companyOb.Cols().CompanyName)
  380. pars = append(pars, fmt.Sprint("%", companyName, "%"))
  381. }
  382. list, e := companyOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", companyOb.Cols().Sort))
  383. if e != nil {
  384. br.Msg = "获取失败"
  385. br.ErrMsg = fmt.Sprintf("获取期货公司列表失败, %v", e)
  386. return
  387. }
  388. resp := make([]*tradeAnalysisModel.TradeFuturesCompanyItem, 0)
  389. for _, v := range list {
  390. resp = append(resp, &tradeAnalysisModel.TradeFuturesCompanyItem{
  391. CompanyId: v.TradeFuturesCompanyId,
  392. CompanyName: v.CompanyName,
  393. Sort: v.Sort,
  394. })
  395. }
  396. br.Data = resp
  397. br.Msg = "获取成功"
  398. br.Ret = 200
  399. br.Success = true
  400. }