trade_analysis.go 9.6 KB

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