edb_data_dl.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package data_manage
  2. import (
  3. "eta_gn/eta_task/global"
  4. "eta_gn/eta_task/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type DlData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. type BaseFromDlDataSimple struct {
  15. Id int `orm:"column(base_from_trade_dalian_index_id);pk"`
  16. DealCode string
  17. BuyCode string
  18. SoldCode string
  19. DataTime string
  20. DealValue string
  21. BuyValue string
  22. SoldValue string
  23. }
  24. type BaseInfoFromDl struct {
  25. DealName string
  26. BuyName string
  27. SoldName string
  28. }
  29. func GetEdbDataDlMaxOrMinDate(edbCode string) (minDate, maxDate string, err error) {
  30. //o := orm.NewOrm()
  31. sql := ` SELECT MIN(data_time) AS minDate,MAX(data_time) AS maxDate FROM edb_data_dl WHERE edb_code=? `
  32. //err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
  33. err = global.DEFAULT_DmSQL.Raw(sql, edbCode).First(&minDate, &maxDate).Error
  34. return
  35. }
  36. // RefreshEdbDataByDl 刷新大商所指标数据
  37. func RefreshEdbDataByDl(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  38. //o := orm.NewOrm()
  39. //tx, err := o.Begin()
  40. tx := global.DEFAULT_DmSQL.Begin()
  41. if err != nil {
  42. return err
  43. }
  44. defer func() {
  45. if err != nil {
  46. tx.Rollback()
  47. } else {
  48. tx.Commit()
  49. }
  50. }()
  51. if err != nil {
  52. return
  53. }
  54. var suffix string
  55. if strings.Contains(edbCode, "deal") {
  56. suffix = "deal"
  57. } else if strings.Contains(edbCode, "buy") {
  58. suffix = "buy"
  59. } else if strings.Contains(edbCode, "sold") {
  60. suffix = "sold"
  61. }
  62. edbInfoIdStr := strconv.Itoa(edbInfoId)
  63. //计算数据
  64. var condition string
  65. var pars []interface{}
  66. if edbCode != "" {
  67. if suffix == "deal" {
  68. condition += " AND deal_code=? "
  69. } else if suffix == "buy" {
  70. condition += " AND buy_code=? "
  71. } else {
  72. condition += " AND sold_code=? "
  73. }
  74. pars = append(pars, edbCode)
  75. }
  76. if startDate != "" {
  77. condition += " AND data_time>=? "
  78. pars = append(pars, startDate)
  79. }
  80. if endDate != "" {
  81. condition += " AND data_time<=? "
  82. pars = append(pars, endDate)
  83. }
  84. glDataList, err := GetDlDataByTradeCode(condition, pars)
  85. addSql := ` INSERT INTO edb_data_dl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  86. var isAdd bool
  87. existMap := make(map[string]string)
  88. for _, v := range glDataList {
  89. var value string
  90. if suffix == "deal" {
  91. value = v.DealValue
  92. } else if suffix == "buy" {
  93. value = v.BuyValue
  94. } else {
  95. value = v.SoldValue
  96. }
  97. item := v
  98. itemValue := value
  99. if _, ok := existMap[v.DataTime]; !ok {
  100. count, err := GetEdbDataDlByCodeAndDate(edbCode, v.DataTime)
  101. if err != nil && err.Error() != utils.ErrNoRow() {
  102. return err
  103. }
  104. if count <= 0 {
  105. eDate := item.DataTime
  106. sValue := itemValue
  107. if sValue != "" {
  108. dataTime, err := time.Parse(utils.FormatDate, eDate)
  109. if err != nil {
  110. return err
  111. }
  112. timestamp := dataTime.UnixNano() / 1e6
  113. timeStr := fmt.Sprintf("%d", timestamp)
  114. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  115. isAdd = true
  116. }
  117. } else {
  118. err = ModifyEdbDataDl(int64(edbInfoId), v.DataTime, value)
  119. if err != nil {
  120. return err
  121. }
  122. }
  123. }
  124. existMap[v.DataTime] = value
  125. }
  126. if isAdd {
  127. addSql = strings.TrimRight(addSql, ",")
  128. //_, err = tx.Raw(addSql).Exec()
  129. //if err != nil {
  130. // return err
  131. //}
  132. if err = tx.Exec(addSql).Error; err != nil {
  133. return
  134. }
  135. }
  136. return
  137. }
  138. // GetBaseInfoFromDalianByIndexCode 获取指标信息
  139. func GetBaseInfoFromDalianByIndexCode(indexCode, suffix string) (list []*BaseInfoFromDl, err error) {
  140. //o := orm.NewOrm()
  141. sql := `SELECT * FROM base_from_trade_dalian_index WHERE %s_code=? `
  142. sql = fmt.Sprintf(sql, suffix)
  143. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  144. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).Find(&list).Error
  145. return
  146. }
  147. func GetDlDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromDlDataSimple, err error) {
  148. sql := ` SELECT * FROM base_from_trade_dalian_index WHERE 1=1 `
  149. //o := orm.NewOrm()
  150. if condition != "" {
  151. sql += condition
  152. }
  153. sql += ` ORDER BY data_time DESC `
  154. //_, err = o.Raw(sql, pars).QueryRows(&item)
  155. err = global.DEFAULT_DmSQL.Raw(sql, pars).Find(&item).Error
  156. return
  157. }
  158. func AddEdbDataDlBySql(sqlStr string) (err error) {
  159. //o := orm.NewOrm()
  160. //_, err = o.Raw(sqlStr).Exec()
  161. err = global.DEFAULT_DmSQL.Exec(sqlStr).Error
  162. return
  163. }
  164. func GetBaseFromDalianDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeDalianIndex, err error) {
  165. //o := orm.NewOrm()
  166. sql := `SELECT * FROM base_from_trade_dalian_index WHERE %s_code=? `
  167. sql = fmt.Sprintf(sql, suffix)
  168. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  169. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).Find(&list).Error
  170. return
  171. }
  172. func GetEdbDataDlByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  173. //o := orm.NewOrm()
  174. sql := ` SELECT COUNT(1) AS count FROM edb_data_dl WHERE edb_code=? AND data_time=? `
  175. //err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  176. err = global.DEFAULT_DmSQL.Raw(sql, edbCode, startDate).Scan(&count).Error
  177. return
  178. }
  179. func ModifyEdbDataDl(edbInfoId int64, dataTime, value string) (err error) {
  180. //o := orm.NewOrm()
  181. sql := ` UPDATE edb_data_dl SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  182. //_, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  183. err = global.DEFAULT_DmSQL.Exec(sql, value, edbInfoId, dataTime).Error
  184. return
  185. }