base_from_cffex.go 9.3 KB

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