base_from_ths_hf.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/mgo"
  7. "eta_gn/eta_api/services/alarm_msg"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "time"
  12. )
  13. // CheckExistThsHfEdb 校验已存在的同花顺高频指标
  14. func CheckExistThsHfEdb(stockCodes, edbCodes []string) (checkResp data_manage.ThsHfExistCheckResp, existMap map[string]bool, err error) {
  15. // 待校验的指标编码
  16. var indexCode []string
  17. prefix := utils.ThsHf
  18. for _, sv := range stockCodes {
  19. for _, ev := range edbCodes {
  20. t := fmt.Sprintf("%s%s%s", prefix, sv, ev)
  21. indexCode = append(indexCode, t)
  22. }
  23. }
  24. baseIndexes := make([]*data_manage.BaseFromThsHfIndex, 0)
  25. {
  26. ob := new(data_manage.BaseFromThsHfIndex)
  27. fields := []string{ob.Cols().PrimaryId, ob.Cols().IndexCode, ob.Cols().IndexName, ob.Cols().StockCode, ob.Cols().Indicator}
  28. list, e := ob.GetItemsByCondition(``, make([]interface{}, 0), fields, "")
  29. if e != nil {
  30. err = fmt.Errorf("获取高频指标列表失败, %v", e)
  31. return
  32. }
  33. baseIndexes = list
  34. }
  35. var existNum int
  36. existMap = make(map[string]bool)
  37. for _, bv := range baseIndexes {
  38. code := fmt.Sprintf("%s%s%s", prefix, bv.StockCode, bv.Indicator)
  39. for _, iv := range indexCode {
  40. if code != iv {
  41. continue
  42. }
  43. existMap[fmt.Sprintf("%s-%s", bv.StockCode, bv.Indicator)] = true
  44. existNum += 1
  45. checkResp.ExistIndex = append(checkResp.ExistIndex, data_manage.ThsHfExistCheckIndex{
  46. IndexId: bv.BaseFromThsHfIndexId,
  47. IndexCode: bv.IndexCode,
  48. IndexName: bv.IndexName,
  49. })
  50. break
  51. }
  52. }
  53. if existNum > 0 && existNum == len(indexCode) {
  54. checkResp.ExistAll = true
  55. }
  56. return
  57. }
  58. // GetEdbDataThsHf 获取同花顺高频数据指标
  59. func GetEdbDataThsHf(req data_manage.ThsHfSearchEdbReq) (indexes []*data_manage.ThsHfIndexWithData, err error) {
  60. param := make(map[string]interface{})
  61. param["StockCode"] = req.StockCode
  62. param["EdbCode"] = req.EdbCode
  63. param["StartTime"] = req.StartTime
  64. param["EndTime"] = req.EndTime
  65. param["Interval"] = req.Interval
  66. param["Fill"] = req.Fill
  67. param["CPS"] = req.CPS
  68. param["BaseDate"] = req.BaseDate
  69. uri := `ths/hf/edb_data`
  70. resp, e := postThsHfEdbData(param, uri)
  71. if e != nil {
  72. err = fmt.Errorf("postThsHfEdbData, %v", e)
  73. return
  74. }
  75. if resp.Ret == 200 {
  76. indexes = resp.Data
  77. }
  78. return
  79. }
  80. // postThsHfEdbData 刷新指标数据
  81. func postThsHfEdbData(param map[string]interface{}, urlStr string) (resp *data_manage.ThsHfIndexDataLibResp, err error) {
  82. postUrl := utils.EDB_LIB_URL + urlStr
  83. postData, err := json.Marshal(param)
  84. if err != nil {
  85. return
  86. }
  87. result, err := HttpPost(postUrl, string(postData), utils.ZhLangVersion, "application/json")
  88. if err != nil {
  89. return
  90. }
  91. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  92. err = json.Unmarshal(result, &resp)
  93. if err != nil {
  94. return
  95. }
  96. return resp, nil
  97. }
  98. // BaseAddThsHf 新增数据源
  99. func BaseAddThsHf(req data_manage.ThsHfBaseAddReq) (resp *models.BaseResponse, err error) {
  100. param := make(map[string]interface{})
  101. param["StartTime"] = req.StartTime
  102. param["EndTime"] = req.EndTime
  103. param["Interval"] = req.Interval
  104. param["Fill"] = req.Fill
  105. param["CPS"] = req.CPS
  106. param["BaseDate"] = req.BaseDate
  107. param["SysAdminId"] = req.SysAdminId
  108. param["SysAdminName"] = req.SysAdminName
  109. param["ClassifyId"] = req.ClassifyId
  110. param["Unit"] = req.Unit
  111. param["IndexName"] = req.IndexName
  112. param["Frequency"] = req.Frequency
  113. param["StockCode"] = req.StockCode
  114. param["EdbCode"] = req.EdbCode
  115. uri := `ths/hf/base/add`
  116. res, e := postRefreshEdbData(param, uri)
  117. if e != nil {
  118. err = fmt.Errorf("postRefreshEdbData, %v", e)
  119. return
  120. }
  121. resp = res
  122. return
  123. }
  124. // RefreshBaseThsHfIndex 刷新源指标
  125. func RefreshBaseThsHfIndex(indexIds []int, refreshType int) (isAsync bool, err error) {
  126. if len(indexIds) == 0 {
  127. return
  128. }
  129. defer func() {
  130. if err != nil {
  131. tips := fmt.Sprintf("RefreshBaseThsHfIndex-刷新同花顺高频指标失败, %v", err)
  132. utils.FileLog.Info(tips)
  133. go alarm_msg.SendAlarmMsg(tips, 3)
  134. }
  135. }()
  136. indexes := make([]*data_manage.BaseFromThsHfIndex, 0)
  137. {
  138. ob := new(data_manage.BaseFromThsHfIndex)
  139. cond := fmt.Sprintf(" AND %s IN (%s)", ob.Cols().PrimaryId, utils.GetOrmInReplace(len(indexIds)))
  140. pars := make([]interface{}, 0)
  141. pars = append(pars, indexIds)
  142. list, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
  143. if e != nil {
  144. err = fmt.Errorf("获取源指标失败, %v", e)
  145. return
  146. }
  147. if len(list) == 0 {
  148. return
  149. }
  150. indexes = list
  151. }
  152. refreshUrl := "ths/hf/base/refresh"
  153. // 异步刷新
  154. if len(indexes) > 10 {
  155. isAsync = true
  156. go func() {
  157. for _, v := range indexes {
  158. param := make(map[string]interface{})
  159. param["BaseIndexCode"] = v.IndexCode
  160. param["RefreshType"] = refreshType
  161. resp, e := postRefreshEdbData(param, refreshUrl)
  162. if e != nil {
  163. utils.FileLog.Info(fmt.Sprintf("thsHf-postRefreshEdbData, code: %s, err: %v", v.IndexCode, e))
  164. continue
  165. }
  166. if resp != nil && resp.Ret != 200 {
  167. utils.FileLog.Info(fmt.Sprintf("thsHf-postRefreshEdbData, code: %s, Ret: %d, ErrMsg: %s", v.IndexCode, resp.Ret, resp.ErrMsg))
  168. continue
  169. }
  170. }
  171. }()
  172. return
  173. }
  174. // 同步刷新
  175. for _, v := range indexes {
  176. param := make(map[string]interface{})
  177. param["BaseIndexCode"] = v.IndexCode
  178. param["RefreshType"] = refreshType
  179. resp, e := postRefreshEdbData(param, refreshUrl)
  180. if e != nil {
  181. utils.FileLog.Info(fmt.Sprintf("thsHf-postRefreshEdbData, code: %s, err: %v", v.IndexCode, e))
  182. continue
  183. }
  184. if resp.Ret != 200 {
  185. utils.FileLog.Info(fmt.Sprintf("thsHf-postRefreshEdbData, code: %s, Ret: %d, ErrMsg: %s", v.IndexCode, resp.Ret, resp.ErrMsg))
  186. continue
  187. }
  188. }
  189. return
  190. }
  191. // GetThsHfBaseIndexData 获取指标数据
  192. func GetThsHfBaseIndexData(indexCode, startTime, endTime string) (dataList []*data_manage.BaseFromThsHfDataItem, err error) {
  193. if utils.UseMongo {
  194. list, e := getThsHfBaseIndexDataByMongo(indexCode, startTime, endTime)
  195. if e != nil {
  196. err = fmt.Errorf("获取指标数据失败-Mongo, %v", e)
  197. return
  198. }
  199. dataList = list
  200. return
  201. }
  202. // MySQL
  203. var (
  204. cond string
  205. pars []interface{}
  206. )
  207. dataOb := new(data_manage.BaseFromThsHfData)
  208. if startTime != "" && endTime != "" {
  209. cond += fmt.Sprintf(" AND %s = ? AND (%s BETWEEN ? AND ?)", dataOb.Cols().IndexCode, dataOb.Cols().DataTime)
  210. pars = append(pars, indexCode, startTime, endTime)
  211. }
  212. if startTime != "" && endTime == "" {
  213. cond += fmt.Sprintf(" AND %s = ? AND %s > ?)", dataOb.Cols().IndexCode, dataOb.Cols().DataTime)
  214. pars = append(pars, indexCode, startTime)
  215. }
  216. list, e := dataOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s DESC", dataOb.Cols().DataTime))
  217. if e != nil {
  218. err = fmt.Errorf("获取指标数据失败-MySQL, %v", e)
  219. return
  220. }
  221. for _, v := range list {
  222. dataList = append(dataList, v.Format2Item())
  223. }
  224. return
  225. }
  226. // getThsHfBaseIndexDataByMongo
  227. func getThsHfBaseIndexDataByMongo(indexCode, startTime, endTime string) (dataList []*data_manage.BaseFromThsHfDataItem, err error) {
  228. dataList = make([]*data_manage.BaseFromThsHfDataItem, 0)
  229. mogDataObj := mgo.BaseFromThsHfData{}
  230. // 构建查询条件
  231. queryConditions := bson.M{
  232. "index_code": indexCode,
  233. }
  234. // 时间区间
  235. if startTime != "" && endTime != "" {
  236. st, e := time.ParseInLocation(utils.FormatDateTime, startTime, time.Local)
  237. if e != nil {
  238. err = fmt.Errorf("start time parse err: %v", e)
  239. return
  240. }
  241. ed, e := time.ParseInLocation(utils.FormatDateTime, endTime, time.Local)
  242. if e != nil {
  243. err = fmt.Errorf("end time parse err: %v", e)
  244. return
  245. }
  246. queryConditions["data_time"] = bson.M{
  247. "$gte": st,
  248. "$lte": ed,
  249. }
  250. }
  251. if startTime != "" && endTime == "" {
  252. st, e := time.ParseInLocation(utils.FormatDateTime, startTime, time.Local)
  253. if e != nil {
  254. err = fmt.Errorf("start time parse err: %v", e)
  255. return
  256. }
  257. queryConditions["data_time"] = bson.M{
  258. "$gte": st,
  259. }
  260. }
  261. // 获取列表数据
  262. list, e := mogDataObj.GetAllDataList(queryConditions, []string{"-data_time"})
  263. if e != nil {
  264. err = fmt.Errorf("GetAllDataList err: %v", e)
  265. return
  266. }
  267. for _, v := range list {
  268. dataList = append(dataList, formatMgoBaseThsHfData2Item(v))
  269. }
  270. return
  271. }
  272. func formatMgoBaseThsHfData2Item(origin *mgo.BaseFromThsHfData) (item *data_manage.BaseFromThsHfDataItem) {
  273. if origin == nil {
  274. return
  275. }
  276. item = new(data_manage.BaseFromThsHfDataItem)
  277. item.DataId = int(origin.BaseFromThsHfDataId)
  278. item.IndexId = int(origin.BaseFromThsHfIndexId)
  279. item.IndexCode = origin.IndexCode
  280. item.DataTime = utils.TimeTransferString(utils.FormatDateTime, origin.DataTime)
  281. item.Value = origin.Value
  282. item.UniqueCode = origin.UniqueCode
  283. return
  284. }