edb_data_zz.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 ZzData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. type BaseFromZzDataSimple struct {
  15. Id int `orm:"column(base_from_trade_zhengzhou_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 BaseInfoFromZz struct {
  25. DealName string
  26. BuyName string
  27. SoldName string
  28. }
  29. func GetEdbDataZzMaxOrMinDate(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_zz 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. // RefreshEdbDataByZz 刷新郑商所指标数据
  37. func RefreshEdbDataByZz(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 := GetZzDataByTradeCode(condition, pars)
  85. addSql := ` INSERT INTO edb_data_zz(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 := GetEdbDataZzByCodeAndDate(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 = ModifyEdbDataZz(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. err = tx.Exec(addSql).Error
  130. if err != nil {
  131. return err
  132. }
  133. }
  134. return
  135. }
  136. // GetBaseInfoFromZhengzhouByIndexCode 获取指标信息
  137. func GetBaseInfoFromZhengzhouByIndexCode(indexCode, suffix string) (list []*BaseInfoFromZz, err error) {
  138. //o := orm.NewOrm()
  139. sql := `SELECT * FROM base_from_trade_zhengzhou_index WHERE %s_code=? `
  140. sql = fmt.Sprintf(sql, suffix)
  141. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  142. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).Find(&list).Error
  143. return
  144. }
  145. func GetZzDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromZzDataSimple, err error) {
  146. sql := ` SELECT * FROM base_from_trade_zhengzhou_index WHERE 1=1 `
  147. //o := orm.NewOrm()
  148. if condition != "" {
  149. sql += condition
  150. }
  151. sql += ` ORDER BY data_time DESC `
  152. //_, err = o.Raw(sql, pars).QueryRows(&item)
  153. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  154. return
  155. }
  156. func AddEdbDataZzBySql(sqlStr string) (err error) {
  157. //o := orm.NewOrm()
  158. //_, err = o.Raw(sqlStr).Exec()
  159. err = global.DEFAULT_DmSQL.Exec(sqlStr).Error
  160. return
  161. }
  162. func GetBaseFromZhengzhouDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeZhengzhouIndex, err error) {
  163. //o := orm.NewOrm()
  164. sql := `SELECT * FROM base_from_trade_zhengzhou_index WHERE %s_code=? `
  165. sql = fmt.Sprintf(sql, suffix)
  166. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  167. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).Find(&list).Error
  168. return
  169. }
  170. func GetEdbDataZzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  171. //o := orm.NewOrm()
  172. sql := ` SELECT COUNT(1) AS count FROM edb_data_zz WHERE edb_code=? AND data_time=? `
  173. //err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  174. err = global.DEFAULT_DmSQL.Raw(sql, edbCode, startDate).Scan(&count).Error
  175. return
  176. }
  177. func ModifyEdbDataZz(edbInfoId int64, dataTime, value string) (err error) {
  178. //o := orm.NewOrm()
  179. sql := ` UPDATE edb_data_zz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  180. //_, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  181. err = global.DEFAULT_DmSQL.Exec(sql, value, edbInfoId, dataTime).Error
  182. return
  183. }