edb_info.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package data_manage
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_task/models/data_manage"
  7. "hongze/hongze_task/utils"
  8. "rdluck_tools/http"
  9. "time"
  10. )
  11. //刷新同花顺数据
  12. func RefreshDataFromThs() (err error) {
  13. defer func() {
  14. if err != nil {
  15. fmt.Println("RefreshDataFromThs Err:" + err.Error())
  16. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromThs ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  17. }
  18. }()
  19. var condition string
  20. var pars []interface{}
  21. condition += " AND source=? "
  22. pars = append(pars, utils.DATA_SOURCE_THS)
  23. items, err := data_manage.GetEdbInfoByCondition(condition, pars)
  24. if err != nil {
  25. return errors.New("GetEdbInfoByCondition:" + err.Error())
  26. }
  27. now := time.Now()
  28. var endDate string
  29. if now.Hour() == 0 {
  30. endDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  31. } else {
  32. endDate = time.Now().Format(utils.FormatDate)
  33. }
  34. for _, v := range items {
  35. startDate := v.EndDate.AddDate(0, 0, -3).Format(utils.FormatDate)
  36. err = SyncEdbDataByThs(int64(v.EdbInfoId), v.EdbCode, startDate, endDate)
  37. if err != nil {
  38. return errors.New("SyncEdbDataByThs:" + err.Error())
  39. }
  40. item, err := data_manage.GetEdbInfoMaxAndMinInfo(v.Source, v.EdbCode)
  41. if err != nil {
  42. return err
  43. }
  44. if item != nil {
  45. err = data_manage.ModifyEdbInfoMaxAndMinInfo(v.EdbInfoId, item)
  46. if err != nil {
  47. return err
  48. }
  49. }
  50. //minDate, maxDate, err := data_manage.GetEdbDataThsMaxOrMinDate(v.EdbInfoId)
  51. //if err != nil {
  52. // return errors.New("GetEdbDataThsMaxOrMinDate:" + err.Error())
  53. //}
  54. //fmt.Println("minDate:" + minDate + ";maxDate:" + maxDate)
  55. //err = data_manage.ModifyEdbDataInfoDate(v.EdbInfoId, maxDate)
  56. //if err != nil {
  57. // return errors.New("ModifyEdbDataInfoDate:" + err.Error())
  58. //}
  59. }
  60. return err
  61. }
  62. //刷新万得数据
  63. func RefreshDataFromWind() (err error) {
  64. defer func() {
  65. if err != nil {
  66. fmt.Println("RefreshDataFromWind Err:" + err.Error())
  67. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromWind ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  68. }
  69. }()
  70. var condition string
  71. var pars []interface{}
  72. condition += " AND source=? "
  73. pars = append(pars, utils.DATA_SOURCE_WIND)
  74. items, err := data_manage.GetEdbInfoByCondition(condition, pars)
  75. if err != nil {
  76. return err
  77. }
  78. now := time.Now()
  79. var endDate string
  80. if now.Hour() == 0 {
  81. endDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  82. } else {
  83. endDate = time.Now().Format(utils.FormatDate)
  84. }
  85. for _, v := range items {
  86. startDate := v.EndDate.AddDate(0, 0, -3).Format(utils.FormatDate)
  87. err = SyncEdbDataByWind(int64(v.EdbInfoId), v.EdbCode, startDate, endDate)
  88. if err != nil {
  89. return err
  90. }
  91. item, err := data_manage.GetEdbInfoMaxAndMinInfo(v.Source, v.EdbCode)
  92. if err != nil {
  93. return err
  94. }
  95. if item != nil {
  96. err = data_manage.ModifyEdbInfoMaxAndMinInfo(v.EdbInfoId, item)
  97. if err != nil {
  98. return err
  99. }
  100. }
  101. }
  102. return err
  103. }
  104. //刷新彭博数据
  105. func RefreshDataFromPb() (err error) {
  106. defer func() {
  107. if err != nil {
  108. fmt.Println("RefreshDataFromPb Err:" + err.Error())
  109. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromPb ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  110. }
  111. }()
  112. var condition string
  113. var pars []interface{}
  114. condition += " AND source=? "
  115. pars = append(pars, utils.DATA_SOURCE_PB)
  116. items, err := data_manage.GetEdbInfoByCondition(condition, pars)
  117. if err != nil {
  118. return errors.New("GetEdbInfoByCondition:" + err.Error())
  119. }
  120. now := time.Now()
  121. var endDate string
  122. if now.Hour() == 0 {
  123. endDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  124. } else {
  125. endDate = time.Now().Format(utils.FormatDate)
  126. }
  127. for _, v := range items {
  128. startDate := v.EndDate.AddDate(0, 0, -3).Format(utils.FormatDateUnSpace)
  129. fmt.Println(startDate, endDate)
  130. err = SyncEdbDataByPb(int64(v.EdbInfoId), v.EdbCode, startDate, endDate)
  131. if err != nil {
  132. return errors.New("SyncEdbDataByPb:" + err.Error())
  133. }
  134. item, err := data_manage.GetEdbInfoMaxAndMinInfo(v.Source, v.EdbCode)
  135. if err != nil {
  136. return err
  137. }
  138. if item != nil {
  139. err = data_manage.ModifyEdbInfoMaxAndMinInfo(v.EdbInfoId, item)
  140. if err != nil {
  141. return err
  142. }
  143. }
  144. //minDate, maxDate, err := data_manage.GetEdbDataPbMaxOrMinDate(v.EdbInfoId)
  145. //if err != nil {
  146. // return errors.New("GetEdbDataThsMaxOrMinDate:" + err.Error())
  147. //}
  148. //fmt.Println("minDate:" + minDate + ";maxDate:" + maxDate)
  149. //err = data_manage.ModifyEdbDataInfoDate(v.EdbInfoId, maxDate)
  150. //if err != nil {
  151. // return errors.New("ModifyEdbDataInfoDate:" + err.Error())
  152. //}
  153. }
  154. return err
  155. }
  156. func CheckDataInterface(cont context.Context) (err error) {
  157. go func() {
  158. checkUrl := utils.Hz_Data_Url + `hz_server`
  159. body, err := http.Get(checkUrl)
  160. if err != nil {
  161. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"检测数据接口:失败提醒", "CheckDataInterface ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  162. }
  163. result := string(body)
  164. if result != "1" {
  165. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"检测数据接口:失败提醒", "CheckDataInterface ErrMsg:"+string(body), utils.EmailSendToUsers)
  166. }
  167. }()
  168. return
  169. }
  170. func CheckPbDataInterface(cont context.Context) (err error) {
  171. go func() {
  172. checkUrl := utils.Hz_Data_PB_Url + `hz_server`
  173. body, err := http.Get(checkUrl)
  174. if err != nil {
  175. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"检测数据接口:失败提醒", "CheckPbDataInterface ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  176. }
  177. result := string(body)
  178. if result != "1" {
  179. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"检测数据接口:失败提醒", "CheckPbDataInterface ErrMsg:"+string(body), utils.EmailSendToUsers)
  180. }
  181. }()
  182. return
  183. }
  184. //刷新计算指标数据
  185. func RefreshDataFromCalculate() (err error) {
  186. defer func() {
  187. if err != nil {
  188. fmt.Println("RefreshDataFromCalculate Err:" + err.Error())
  189. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshDataFromCalculate ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  190. }
  191. }()
  192. var condition string
  193. var pars []interface{}
  194. condition += " AND source=? "
  195. pars = append(pars, utils.DATA_SOURCE_CALCULATE)
  196. items, err := data_manage.GetEdbInfoByCondition(condition, pars)
  197. if err != nil {
  198. return err
  199. }
  200. now := time.Now()
  201. var endDate string
  202. if now.Hour() == 0 {
  203. endDate = time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  204. } else {
  205. endDate = time.Now().Format(utils.FormatDate)
  206. }
  207. for _, v := range items {
  208. startDate := v.EndDate.AddDate(0, 0, -3).Format(utils.FormatDate)
  209. var edbInfoIdBytes []string
  210. calculateMap, err := data_manage.GetEdbInfoCalculateDetail(int(v.EdbInfoId))
  211. if err != nil {
  212. return err
  213. }
  214. var formulaStr string
  215. edbInfoList := make([]*data_manage.EdbInfo, 0)
  216. for _, v := range calculateMap {
  217. formulaStr += v.FromTag + ","
  218. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  219. edbInfo, _ := data_manage.GetEdbInfoById(v.FromEdbInfoId)
  220. edbInfoList = append(edbInfoList, edbInfo)
  221. }
  222. err = RefreshCalculate(edbInfoList, v.EdbInfoId, v.EdbCode, v.CalculateFormula, startDate, endDate, edbInfoIdBytes)
  223. if err != nil {
  224. return err
  225. }
  226. }
  227. return err
  228. }