trade_analysis.go 9.5 KB

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