base_from_ths.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_edb_lib/services"
  6. "hongze/hongze_edb_lib/utils"
  7. "strings"
  8. "time"
  9. )
  10. func AddEdbDataFromThs(edbCode string, item *services.EdbDataFromThs) (err error) {
  11. var errMsg string
  12. o := orm.NewOrm()
  13. defer func() {
  14. if err != nil {
  15. go utils.SendEmail(utils.APP_NAME_CN+"【"+utils.RunMode+"】"+"失败提醒", " 同花顺数据获取失败:err:"+errMsg, utils.EmailSendToUsers)
  16. }
  17. }()
  18. if len(item.Tables) > 0 {
  19. table := item.Tables[0]
  20. dataLen := len(table.Time)
  21. var isAdd bool
  22. addSql := ` INSERT INTO %s(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  23. tableName := GetEdbDataTableName(utils.DATA_SOURCE_THS)
  24. addSql = fmt.Sprintf(addSql, tableName)
  25. for i := 0; i < dataLen; i++ {
  26. eDate := table.Time[i]
  27. sValue := table.Value[i]
  28. dataTime, err := time.Parse(utils.FormatDate, eDate)
  29. if err != nil {
  30. errMsg = " time.Parse :" + err.Error()
  31. return err
  32. }
  33. timestamp := dataTime.UnixNano() / 1e6
  34. timeStr := fmt.Sprintf("%d", timestamp)
  35. addSql += GetAddSql("0", edbCode, eDate, timeStr, utils.SubFloatToString(sValue, 20))
  36. isAdd = true
  37. }
  38. if isAdd {
  39. addSql = strings.TrimRight(addSql, ",")
  40. _, err = o.Raw(addSql).Exec()
  41. if err != nil {
  42. errMsg = " tx.Exec Err :" + err.Error()
  43. return
  44. }
  45. }
  46. }
  47. return
  48. }
  49. func GetEdbDataByThs(edbCode string, item *services.EdbDataFromThs) (dataList []*EdbInfoSearchData, err error) {
  50. AddEdbDataFromThs(edbCode, item)
  51. size := utils.EDB_DATA_LIMIT
  52. dataList, err = GetEdbDataAllByEdbCode(edbCode, utils.DATA_SOURCE_THS, size)
  53. if err != nil {
  54. //utils.FileLogData.Info("GetEdbDataThsByCode Err:%s", err.Error())
  55. return
  56. }
  57. if len(dataList) < 0 {
  58. dataList = make([]*EdbInfoSearchData, 0)
  59. }
  60. return
  61. }