base_from_cffex.go 7.9 KB

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