base_from_cffex.go 7.4 KB

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