base_from_cffex.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package models
  2. import (
  3. "eta/eta_index_lib/global"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type BaseFromTradeCFFEXIndex struct {
  12. //BaseFromTradeCffexIndexId int `orm:"column(base_from_trade_cffex_index_id);pk"`
  13. BaseFromTradeCffexIndexId int `gorm:"column:base_from_trade_cffex_index_id;primaryKey"`
  14. Rank int
  15. DealShortName string
  16. DealName string
  17. DealCode string
  18. DealValue string
  19. DealChange int
  20. BuyShortName string
  21. BuyName string
  22. BuyCode string
  23. BuyValue string
  24. BuyChange int
  25. SoldShortName string
  26. SoldName string
  27. SoldCode string
  28. SoldValue string
  29. SoldChange int
  30. Frequency string
  31. ClassifyName string
  32. ClassifyType string
  33. CreateTime time.Time
  34. ModifyTime time.Time
  35. DataTime string
  36. }
  37. func GetBaseFromCffexDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeCFFEXIndex, err error) {
  38. //o := orm.NewOrm()
  39. sql := `SELECT * FROM base_from_trade_cffex_index WHERE %s_code=? `
  40. sql = fmt.Sprintf(sql, suffix)
  41. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  42. err = global.DEFAULT_DB.Raw(sql, indexCode).Find(&list).Error
  43. return
  44. }
  45. type BaseFromCffexDataSimple struct {
  46. Id int `gorm:"column:base_from_trade_cffex_index_id;primaryKey"`
  47. //Id int `orm:"column(base_from_trade_cffex_index_id);pk"`
  48. DealCode string
  49. BuyCode string
  50. SoldCode string
  51. DataTime string
  52. DealValue string
  53. BuyValue string
  54. SoldValue string
  55. }
  56. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  57. func (m *BaseFromCffexDataSimple) AfterFind(db *gorm.DB) (err error) {
  58. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  59. return
  60. }
  61. func GetCffexDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromCffexDataSimple, err error) {
  62. sql := ` SELECT * FROM base_from_trade_cffex_index WHERE 1=1 `
  63. //o := orm.NewOrm()
  64. if condition != "" {
  65. sql += condition
  66. }
  67. sql += ` ORDER BY data_time DESC `
  68. //_, err = o.Raw(sql, pars).QueryRows(&item)
  69. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&item).Error
  70. return
  71. }
  72. // 新增中金所指标数据
  73. func AddEdbDataFromCffex(edbCode string) (err error) {
  74. var suffix string
  75. if strings.Contains(edbCode, "deal") {
  76. suffix = "deal"
  77. } else if strings.Contains(edbCode, "buy") {
  78. suffix = "buy"
  79. } else if strings.Contains(edbCode, "sold") {
  80. suffix = "sold"
  81. }
  82. //o := orm.NewOrm()
  83. cffexBaseDataAll, err := GetBaseFromCffexDataAllByIndexCode(edbCode, suffix)
  84. //if err != nil && !utils.IsErrNoRow(err) {
  85. // return
  86. //}
  87. if err != nil && !utils.IsErrNoRow(err) {
  88. return
  89. }
  90. var isAdd bool
  91. addSql := ` INSERT INTO edb_data_cffex(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  92. addSql = utils.ReplaceDriverKeywords("", addSql)
  93. existMap := make(map[string]string)
  94. for _, sv := range cffexBaseDataAll {
  95. eDate := sv.DataTime
  96. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  97. if err != nil {
  98. fmt.Println("time.Parse Err:" + eDate)
  99. return err
  100. }
  101. timestamp := dataTime.UnixNano() / 1e6
  102. timeStr := fmt.Sprintf("%d", timestamp)
  103. if _, ok := existMap[eDate]; !ok {
  104. if suffix == "deal" {
  105. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.DealValue)
  106. } else if suffix == "buy" {
  107. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.BuyValue)
  108. } else {
  109. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.SoldValue)
  110. }
  111. isAdd = true
  112. }
  113. if suffix == "deal" {
  114. existMap[eDate] = sv.DealValue
  115. } else if suffix == "buy" {
  116. existMap[eDate] = sv.BuyValue
  117. } else {
  118. existMap[eDate] = sv.SoldValue
  119. }
  120. }
  121. if isAdd {
  122. addSql = strings.TrimRight(addSql, ",")
  123. utils.FileLog.Info("addSql:" + addSql)
  124. //_, err = o.Raw(addSql).Exec()
  125. err = global.DEFAULT_DB.Exec(addSql).Error
  126. if err != nil {
  127. return err
  128. }
  129. }
  130. return
  131. }
  132. // 刷新中金所指标数据
  133. func RefreshEdbDataFromCffex(edbInfoId int, edbCode, startDate string) (err error) {
  134. source := utils.DATA_SOURCE_CFFEX
  135. subSource := utils.DATA_SUB_SOURCE_EDB
  136. var suffix string
  137. if strings.Contains(edbCode, "deal") {
  138. suffix = "deal"
  139. } else if strings.Contains(edbCode, "buy") {
  140. suffix = "buy"
  141. } else if strings.Contains(edbCode, "sold") {
  142. suffix = "sold"
  143. }
  144. //o := orm.NewOrm()
  145. if err != nil {
  146. return
  147. }
  148. edbInfoIdStr := strconv.Itoa(edbInfoId)
  149. //计算数据
  150. var condition string
  151. var pars []interface{}
  152. if edbCode != "" {
  153. if suffix == "deal" {
  154. condition += " AND deal_code=? "
  155. } else if suffix == "buy" {
  156. condition += " AND buy_code=? "
  157. } else {
  158. condition += " AND sold_code=? "
  159. }
  160. pars = append(pars, edbCode)
  161. }
  162. if startDate != "" {
  163. condition += " AND data_time>=? "
  164. pars = append(pars, startDate)
  165. }
  166. glDataList, err := GetCffexDataByTradeCode(condition, pars)
  167. if err != nil {
  168. return
  169. }
  170. // 真实数据的最大日期 , 插入规则配置的日期
  171. var realDataMaxDate, edbDataInsertConfigDate time.Time
  172. var edbDataInsertConfig *EdbDataInsertConfig
  173. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  174. {
  175. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  176. if err != nil && !utils.IsErrNoRow(err) {
  177. return
  178. }
  179. if edbDataInsertConfig != nil {
  180. edbDataInsertConfigDate = edbDataInsertConfig.Date
  181. }
  182. }
  183. //获取指标所有数据
  184. var existCondition string
  185. var existPars []interface{}
  186. existCondition += " AND edb_info_id=? "
  187. existPars = append(existPars, edbInfoId)
  188. if startDate != "" {
  189. existCondition += " AND data_time>=? "
  190. existPars = append(existPars, startDate)
  191. }
  192. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  193. if err != nil {
  194. return err
  195. }
  196. existMap := make(map[string]*EdbInfoSearchData)
  197. for _, v := range existList {
  198. existMap[v.DataTime] = v
  199. }
  200. addSql := ` INSERT INTO edb_data_cffex(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  201. addSql = utils.ReplaceDriverKeywords("", addSql)
  202. var isAdd bool
  203. for _, v := range glDataList {
  204. var value string
  205. if suffix == "deal" {
  206. value = v.DealValue
  207. } else if suffix == "buy" {
  208. value = v.BuyValue
  209. } else {
  210. value = v.SoldValue
  211. }
  212. item := v
  213. eDate := item.DataTime
  214. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  215. if err != nil {
  216. return err
  217. }
  218. // 下面代码主要目的是处理掉手动插入的数据判断
  219. {
  220. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  221. realDataMaxDate = dataTime
  222. }
  223. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  224. isFindConfigDateRealData = true
  225. }
  226. }
  227. itemValue := value
  228. if _, ok := existMap[v.DataTime]; !ok {
  229. sValue := itemValue
  230. if sValue != "" {
  231. timestamp := dataTime.UnixNano() / 1e6
  232. timeStr := fmt.Sprintf("%d", timestamp)
  233. saveValue := sValue
  234. if findItem, ok := existMap[eDate]; !ok {
  235. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  236. isAdd = true
  237. } else {
  238. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  239. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  240. if err != nil {
  241. return err
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. // 处理手工数据补充的配置
  249. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  250. if isAdd {
  251. addSql = strings.TrimRight(addSql, ",")
  252. //_, err = o.Raw(addSql).Exec()
  253. err = global.DEFAULT_DB.Exec(addSql).Error
  254. if err != nil {
  255. return err
  256. }
  257. }
  258. return
  259. }