trade_analysis.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package trade_analysis
  2. import (
  3. "eta_gn/eta_api/models/data_manage/trade_analysis"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "time"
  9. )
  10. func GetClassifyName(lang string) (list trade_analysis.TradeClassifyNameListSort, err error, errMsg string) {
  11. //定义交易所
  12. exchanges := map[string]string{
  13. "zhengzhou": "郑商所",
  14. "dalian": "大商所",
  15. "shanghai": "上期所",
  16. "cffex": "中金所",
  17. "ine": "上期能源",
  18. "guangzhou": "广期所",
  19. }
  20. exchangesEn := map[string]string{
  21. "郑商所": "Zhengzhou ComEx",
  22. "大商所": "Dalian ComEx",
  23. "上期所": "SH Intl Energy Ex",
  24. "中金所": "China Fin Futures Ex",
  25. "上期能源": "SH Energy Ex",
  26. }
  27. exchangesSortMap := map[string]int{
  28. "zhengzhou": 1,
  29. "dalian": 2,
  30. "shanghai": 3,
  31. "cffex": 4,
  32. "ine": 5,
  33. "guangzhou": 6,
  34. }
  35. //查询所有交易所下的分类
  36. classifyExchangeList, tmpErr := trade_analysis.GetAllBaseFromTradeClassify()
  37. if tmpErr != nil {
  38. err = tmpErr
  39. errMsg = "查询交易所最新更新时间失败"
  40. return
  41. }
  42. // 每个交易所的分类信息
  43. classifyExchangeMap := make(map[string][]trade_analysis.TradeClassifyName)
  44. // 每个交易所的最新更新时间
  45. timeLastMap := make(map[string]time.Time)
  46. for _, v := range classifyExchangeList {
  47. tmpExchange := v.Exchange
  48. // 分类
  49. tmpList, ok := classifyExchangeMap[tmpExchange]
  50. if !ok {
  51. tmpList = make([]trade_analysis.TradeClassifyName, 0)
  52. }
  53. tmpList = append(tmpList, trade_analysis.TradeClassifyName{
  54. ClassifyName: v.ClassifyName,
  55. ClassifyType: v.ClassifyType,
  56. LatestDate: v.LatestDate.Format(utils.FormatDate),
  57. })
  58. classifyExchangeMap[v.Exchange] = tmpList
  59. // 时间
  60. if tmpLastTime, ok2 := timeLastMap[tmpExchange]; !ok2 {
  61. timeLastMap[tmpExchange] = v.ModifyTime
  62. } else {
  63. if v.ModifyTime.After(tmpLastTime) {
  64. timeLastMap[tmpExchange] = v.ModifyTime
  65. }
  66. }
  67. }
  68. i := 0
  69. currDate := time.Now().Format(utils.FormatDate)
  70. for k, v := range exchanges {
  71. tmp := trade_analysis.TradeClassifyNameList{
  72. Exchange: v,
  73. ExchangeEn: exchangesEn[v],
  74. Items: nil,
  75. Sort: exchangesSortMap[k],
  76. CurrDate: currDate,
  77. }
  78. nameList, ok := classifyExchangeMap[k]
  79. if !ok {
  80. err = fmt.Errorf("查询交易所分类信息失败")
  81. errMsg = "查询交易所分类信息失败"
  82. return
  83. }
  84. // 查询交易所最新更新时间失败
  85. if timeLast, ok := timeLastMap[k]; ok {
  86. tmp.DataTime = timeLast.Format(utils.FormatDateTime)
  87. } else {
  88. err = fmt.Errorf("查询交易所最新更新时间失败")
  89. errMsg = "查询交易所最新更新时间失败"
  90. return
  91. }
  92. classifyMap := make(map[string][]trade_analysis.TradeClassifyNameListItemItem)
  93. if len(nameList) > 0 {
  94. if k == "zhengzhou" {
  95. for _, item := range nameList {
  96. classifyName := getZhengzhouClassifyName(item.ClassifyName)
  97. tmpItemItem := trade_analysis.TradeClassifyNameListItemItem{
  98. ClassifyType: item.ClassifyName,
  99. }
  100. classifyMap[classifyName] = append(classifyMap[classifyName], tmpItemItem)
  101. }
  102. } else {
  103. for _, item := range nameList {
  104. tmpItemItem := trade_analysis.TradeClassifyNameListItemItem{
  105. ClassifyType: item.ClassifyType,
  106. }
  107. classifyMap[item.ClassifyName] = append(classifyMap[item.ClassifyName], tmpItemItem)
  108. }
  109. }
  110. for n, l := range classifyMap {
  111. sort.Sort(trade_analysis.TradeClassifyNameListItemItemSort(l))
  112. tmpItems := trade_analysis.TradeClassifyNameListItem{
  113. ClassifyName: n,
  114. Items: l,
  115. }
  116. tmp.Items = append(tmp.Items, tmpItems)
  117. tmp.Num++
  118. }
  119. }
  120. sort.Sort(trade_analysis.TradeClassifyNameListItemSort(tmp.Items))
  121. list = append(list, tmp)
  122. i++
  123. }
  124. if len(list) > 0 {
  125. sort.Sort(list)
  126. }
  127. return
  128. }
  129. func getZhengzhouClassifyName(code string) (name string) {
  130. if strings.HasPrefix(code, "PTA") {
  131. name = "PTA"
  132. return
  133. }
  134. if strings.HasPrefix(code, "TA") {
  135. name = "PTA"
  136. return
  137. }
  138. if strings.HasPrefix(code, "ZC") {
  139. name = "动力煤"
  140. return
  141. }
  142. if strings.HasPrefix(code, "WH") {
  143. name = "强麦"
  144. return
  145. }
  146. if strings.HasPrefix(code, "UR") {
  147. name = "尿素"
  148. return
  149. }
  150. if strings.HasPrefix(code, "SR") {
  151. name = "白糖"
  152. return
  153. }
  154. if strings.HasPrefix(code, "SM") {
  155. name = "锰硅"
  156. return
  157. }
  158. if strings.HasPrefix(code, "SF") {
  159. name = "硅铁"
  160. return
  161. }
  162. if strings.HasPrefix(code, "SA") {
  163. name = "纯碱"
  164. return
  165. }
  166. if strings.HasPrefix(code, "RS") {
  167. name = "油菜籽"
  168. return
  169. }
  170. if strings.HasPrefix(code, "RM") {
  171. name = "菜籽粕"
  172. return
  173. }
  174. if strings.HasPrefix(code, "RI") {
  175. name = "早籼稻"
  176. return
  177. }
  178. if strings.HasPrefix(code, "PM") {
  179. name = "普麦"
  180. return
  181. }
  182. if strings.HasPrefix(code, "PK") {
  183. name = "花生"
  184. return
  185. }
  186. if strings.HasPrefix(code, "PF") {
  187. name = "涤纶短纤"
  188. return
  189. }
  190. if strings.HasPrefix(code, "OI") {
  191. name = "菜油"
  192. return
  193. }
  194. if strings.HasPrefix(code, "MA") {
  195. name = "甲醇"
  196. return
  197. }
  198. if strings.HasPrefix(code, "LR") {
  199. name = "晚籼稻"
  200. return
  201. }
  202. if strings.HasPrefix(code, "JR") {
  203. name = "粳稻"
  204. return
  205. }
  206. if strings.HasPrefix(code, "FG") {
  207. name = "玻璃"
  208. return
  209. }
  210. if strings.HasPrefix(code, "CY") {
  211. name = "棉纱"
  212. return
  213. }
  214. if strings.HasPrefix(code, "CJ") {
  215. name = "红枣"
  216. return
  217. }
  218. if strings.HasPrefix(code, "CF") {
  219. name = "棉花"
  220. return
  221. }
  222. if strings.HasPrefix(code, "AP") {
  223. name = "苹果"
  224. return
  225. }
  226. if strings.HasPrefix(code, "PX") {
  227. name = "PX"
  228. return
  229. }
  230. if strings.HasPrefix(code, "SH") {
  231. name = "烧碱"
  232. return
  233. }
  234. return
  235. }
  236. func GetPositionTopDetail(req trade_analysis.GetPositionTopReq) (ret trade_analysis.GetPositionTopResp, err error, errMsg string) {
  237. //定义交易所
  238. exchanges := map[string]string{
  239. "郑商所": "zhengzhou",
  240. "大商所": "dalian",
  241. "上期所": "shanghai",
  242. "中金所": "cffex",
  243. "上期能源": "ine",
  244. "广期所": "guangzhou",
  245. }
  246. exchange, ok := exchanges[req.Exchange]
  247. if !ok {
  248. errMsg = "请输入正确的交易所名称"
  249. err = fmt.Errorf(errMsg)
  250. return
  251. }
  252. dataTimeStr := req.DataTime
  253. var lastDataTime, dataTime time.Time
  254. //查询最新的时间
  255. lastItem, tmpErr := trade_analysis.GetTradeTopLastDataTime(exchange, req.ClassifyName, req.ClassifyType)
  256. if tmpErr != nil {
  257. errMsg = "查询最新的榜单信息失败"
  258. err = tmpErr
  259. return
  260. }
  261. lastDataTime = lastItem.LatestDate
  262. // 如果没有传入日期,那么就用最晚的的日期
  263. if dataTimeStr == "" {
  264. dataTime = lastDataTime
  265. } else {
  266. dataTime, err = time.ParseInLocation(utils.FormatDate, dataTimeStr, time.Local)
  267. if err != nil {
  268. errMsg = "请输入正确的时间格式"
  269. return
  270. }
  271. }
  272. dataTimeStr = dataTime.Format(utils.FormatDate)
  273. ret.DataTime = dataTimeStr
  274. ret.LastDataTime = lastDataTime.Format(utils.FormatDate)
  275. //遇到周末则跳过当天
  276. weekStr := dataTime.Weekday().String()
  277. if weekStr == "Sunday" || weekStr == "Saturday" {
  278. /*errMsg = "日期不正确"
  279. err = fmt.Errorf(errMsg)*/
  280. return
  281. }
  282. classifyName := req.ClassifyName
  283. classifyType := req.ClassifyType
  284. if exchange == "zhengzhou" {
  285. classifyName = classifyType
  286. var typeItem *trade_analysis.TradeClassifyName
  287. typeItem, err = trade_analysis.GetClassifyTypeByClassifyName(exchange, classifyName)
  288. if err != nil {
  289. if utils.IsErrNoRow(err) {
  290. errMsg = "该合约不存在"
  291. return
  292. }
  293. errMsg = "查询类型信息出错"
  294. return
  295. }
  296. classifyType = typeItem.ClassifyType
  297. }
  298. //查询当日榜单列表
  299. dataList, tmpErr := trade_analysis.GetTradePositionTop(exchange, classifyName, classifyType, dataTimeStr)
  300. if tmpErr != nil {
  301. errMsg = "查询榜单列表失败"
  302. err = tmpErr
  303. return
  304. }
  305. if len(dataList) <= 0 {
  306. return
  307. }
  308. totalMap := make(map[int]int)
  309. totalChangeMap := make(map[int]int)
  310. totalTmpMap := make(map[int]int)
  311. totalChangeTmpMap := make(map[int]int)
  312. detailList := make(map[int][]trade_analysis.GetPositionTopListItem)
  313. //统计汇总数据
  314. for _, v := range dataList {
  315. if t, ok1 := totalMap[v.DealType]; ok1 {
  316. totalMap[v.DealType] = t + v.DealValue
  317. } else {
  318. totalMap[v.DealType] = v.DealValue
  319. }
  320. if t, ok1 := totalChangeMap[v.DealType]; ok1 {
  321. totalChangeMap[v.DealType] = t + v.DealChange
  322. } else {
  323. totalChangeMap[v.DealType] = v.DealChange
  324. }
  325. }
  326. _, okTmp1 := totalMap[1]
  327. _, okTmp2 := totalMap[2]
  328. _, okTmp3 := totalMap[3]
  329. _, okTmp4 := totalMap[4]
  330. if !okTmp1 || !okTmp2 || (!okTmp3 && !okTmp4) {
  331. errMsg = "榜单数据缺失"
  332. err = fmt.Errorf(errMsg)
  333. return
  334. }
  335. for k, v := range dataList {
  336. k++
  337. if t, ok1 := totalTmpMap[v.DealType]; ok1 {
  338. totalTmpMap[v.DealType] = t + v.DealValue
  339. } else {
  340. totalTmpMap[v.DealType] = v.DealValue
  341. }
  342. if t, ok1 := totalChangeTmpMap[v.DealType]; ok1 {
  343. totalChangeTmpMap[v.DealType] = t + v.DealChange
  344. } else {
  345. totalChangeTmpMap[v.DealType] = v.DealChange
  346. }
  347. tmp := trade_analysis.GetPositionTopListItem{
  348. DealShortName: v.DealShortName,
  349. DealValue: v.DealValue,
  350. DealChange: v.DealChange,
  351. Rank: v.Rank,
  352. BeforeAllValue: totalTmpMap[v.DealType],
  353. BeforeAllChange: totalChangeTmpMap[v.DealType],
  354. }
  355. //统计占比
  356. rate := fmt.Sprintf("%.2f", float64(tmp.DealValue)/float64(totalMap[v.DealType])) // 保留2位小数
  357. beforeAllRate := fmt.Sprintf("%.2f", float64(tmp.BeforeAllValue)/float64(totalMap[v.DealType])) // 保留2位小数
  358. tmp.Rate = rate
  359. tmp.BeforeAllRate = beforeAllRate
  360. if tmp.DealShortName == "-" {
  361. continue
  362. }
  363. detailList[v.DealType] = append(detailList[v.DealType], tmp)
  364. }
  365. ret.BuyList.TotalDealValue = totalMap[1]
  366. ret.BuyList.TotalDealChange = totalChangeMap[1]
  367. ret.BuyList.List = detailList[1]
  368. ret.SoldList.TotalDealValue = totalMap[2]
  369. ret.SoldList.TotalDealChange = totalChangeMap[2]
  370. ret.SoldList.List = detailList[2]
  371. ret.CleanBuyList.TotalDealValue = totalMap[3]
  372. ret.CleanBuyList.TotalDealChange = totalChangeMap[3]
  373. ret.CleanBuyList.List = detailList[3]
  374. ret.CleanSoldList.TotalDealValue = totalMap[4]
  375. ret.CleanSoldList.TotalDealChange = totalChangeMap[4]
  376. ret.CleanSoldList.List = detailList[4]
  377. return
  378. }