edb_data_cffex.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type CffexData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. type BaseFromCffexDataSimple struct {
  15. Id int `orm:"column(base_from_trade_cffex_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 BaseInfoFromCffex struct {
  25. DealName string
  26. BuyName string
  27. SoldName string
  28. }
  29. type BaseFromTradeCFFEXIndex struct {
  30. BaseFromTradeCffexIndexId int `orm:"column(base_from_trade_cffex_index_id);pk"`
  31. Rank int
  32. DealShortName string
  33. DealName string
  34. DealCode string
  35. DealValue string
  36. DealChange int
  37. BuyShortName string
  38. BuyName string
  39. BuyCode string
  40. BuyValue string
  41. BuyChange int
  42. SoldShortName string
  43. SoldName string
  44. SoldCode string
  45. SoldValue string
  46. SoldChange int
  47. Frequency string
  48. ClassifyName string
  49. ClassifyType string
  50. CreateTime time.Time
  51. ModifyTime time.Time
  52. DataTime string
  53. }
  54. func GetEdbDataCffexMaxOrMinDate(edbCode string) (minDate, maxDate string, err error) {
  55. o := orm.NewOrmUsingDB("data")
  56. sql := ` SELECT MIN(data_time) AS minDate,MAX(data_time) AS maxDate FROM edb_data_cffex WHERE edb_code=? `
  57. err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
  58. return
  59. }
  60. // RefreshEdbDataByCffex 刷新中金所指标数据
  61. func RefreshEdbDataByCffex(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  62. o := orm.NewOrmUsingDB("data")
  63. to, err := o.Begin()
  64. if err != nil {
  65. return
  66. }
  67. defer func() {
  68. if err != nil {
  69. _ = to.Rollback()
  70. } else {
  71. _ = to.Commit()
  72. }
  73. }()
  74. if err != nil {
  75. return
  76. }
  77. var suffix string
  78. if strings.Contains(edbCode, "deal") {
  79. suffix = "deal"
  80. } else if strings.Contains(edbCode, "buy") {
  81. suffix = "buy"
  82. } else if strings.Contains(edbCode, "sold") {
  83. suffix = "sold"
  84. }
  85. edbInfoIdStr := strconv.Itoa(edbInfoId)
  86. //计算数据
  87. var condition string
  88. var pars []interface{}
  89. if edbCode != "" {
  90. if suffix == "deal" {
  91. condition += " AND deal_code=? "
  92. } else if suffix == "buy" {
  93. condition += " AND buy_code=? "
  94. } else {
  95. condition += " AND sold_code=? "
  96. }
  97. pars = append(pars, edbCode)
  98. }
  99. if startDate != "" {
  100. condition += " AND data_time>=? "
  101. pars = append(pars, startDate)
  102. }
  103. if endDate != "" {
  104. condition += " AND data_time<=? "
  105. pars = append(pars, endDate)
  106. }
  107. glDataList, err := GetCffexDataByTradeCode(condition, pars)
  108. addSql := ` INSERT INTO edb_data_cffex(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  109. var isAdd bool
  110. existMap := make(map[string]string)
  111. for _, v := range glDataList {
  112. var value string
  113. if suffix == "deal" {
  114. value = v.DealValue
  115. } else if suffix == "buy" {
  116. value = v.BuyValue
  117. } else {
  118. value = v.SoldValue
  119. }
  120. item := v
  121. itemValue := value
  122. if _, ok := existMap[v.DataTime]; !ok {
  123. count, err := GetEdbDataCffexByCodeAndDate(edbCode, v.DataTime)
  124. if err != nil && err.Error() != utils.ErrNoRow() {
  125. return err
  126. }
  127. if count <= 0 {
  128. eDate := item.DataTime
  129. sValue := itemValue
  130. if sValue != "" {
  131. dataTime, err := time.Parse(utils.FormatDate, eDate)
  132. if err != nil {
  133. return err
  134. }
  135. timestamp := dataTime.UnixNano() / 1e6
  136. timeStr := fmt.Sprintf("%d", timestamp)
  137. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  138. isAdd = true
  139. }
  140. } else {
  141. err = ModifyEdbDataCffex(int64(edbInfoId), v.DataTime, value)
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. }
  147. existMap[v.DataTime] = value
  148. }
  149. if isAdd {
  150. addSql = strings.TrimRight(addSql, ",")
  151. _, err = to.Raw(addSql).Exec()
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. return
  157. }
  158. // RefreshAllEdbDataByCffex 全部刷新中金所
  159. func RefreshAllEdbDataByCffex(edbInfoId, source, subSource int, edbCode, startDate, endDate string) (err error) {
  160. o := orm.NewOrmUsingDB("data")
  161. to, err := o.Begin()
  162. if err != nil {
  163. return
  164. }
  165. defer func() {
  166. if err != nil {
  167. _ = to.Rollback()
  168. } else {
  169. _ = to.Commit()
  170. }
  171. }()
  172. var suffix string
  173. if strings.Contains(edbCode, "deal") {
  174. suffix = "deal"
  175. } else if strings.Contains(edbCode, "buy") {
  176. suffix = "buy"
  177. } else if strings.Contains(edbCode, "sold") {
  178. suffix = "sold"
  179. }
  180. edbInfoIdStr := strconv.Itoa(edbInfoId)
  181. //计算数据
  182. var condition string
  183. var pars []interface{}
  184. if edbCode != "" {
  185. if suffix == "deal" {
  186. condition += " AND deal_code=? "
  187. } else if suffix == "buy" {
  188. condition += " AND buy_code=? "
  189. } else {
  190. condition += " AND sold_code=? "
  191. }
  192. pars = append(pars, edbCode)
  193. }
  194. if startDate != "" {
  195. condition += " AND data_time>=? "
  196. pars = append(pars, startDate)
  197. }
  198. if endDate != "" {
  199. condition += " AND data_time<=? "
  200. pars = append(pars, endDate)
  201. }
  202. glDataList, err := GetCffexDataByTradeCode(condition, pars)
  203. //获取指标所有数据
  204. dataList := make([]*EdbDataBase, 0)
  205. dataTableName := GetEdbDataTableName(source, subSource)
  206. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  207. sql = fmt.Sprintf(sql, dataTableName)
  208. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  209. if err != nil {
  210. return err
  211. }
  212. dataMap := make(map[string]string)
  213. for _, v := range dataList {
  214. dataMap[v.DataTime] = v.Value
  215. }
  216. addSql := ` INSERT INTO edb_data_cffex(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  217. var isAdd bool
  218. existMap := make(map[string]string)
  219. for _, v := range glDataList {
  220. var value string
  221. if suffix == "deal" {
  222. value = v.DealValue
  223. } else if suffix == "buy" {
  224. value = v.BuyValue
  225. } else {
  226. value = v.SoldValue
  227. }
  228. item := v
  229. itemValue := value
  230. if _, ok := existMap[v.DataTime]; !ok {
  231. eDate := item.DataTime
  232. sValue := itemValue
  233. if sValue != "" {
  234. dataTime, err := time.Parse(utils.FormatDate, eDate)
  235. if err != nil {
  236. return err
  237. }
  238. timestamp := dataTime.UnixNano() / 1e6
  239. timeStr := fmt.Sprintf("%d", timestamp)
  240. saveValue := sValue
  241. if existVal, ok := dataMap[eDate]; !ok {
  242. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  243. isAdd = true
  244. } else {
  245. if existVal != saveValue {
  246. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  247. sql = fmt.Sprintf(sql, dataTableName)
  248. _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
  249. if err != nil {
  250. return err
  251. }
  252. }
  253. }
  254. }
  255. }
  256. existMap[v.DataTime] = v.DataTime
  257. }
  258. if isAdd {
  259. addSql = strings.TrimRight(addSql, ",")
  260. _, err = to.Raw(addSql).Exec()
  261. if err != nil {
  262. return err
  263. }
  264. }
  265. return
  266. }
  267. // GetBaseInfoFromCffexByIndexCode 获取指标信息
  268. func GetBaseInfoFromCffexByIndexCode(indexCode, suffix string) (list []*BaseInfoFromCffex, err error) {
  269. o := orm.NewOrmUsingDB("data")
  270. sql := `SELECT * FROM base_from_trade_cffex_index WHERE %s_code=? `
  271. sql = fmt.Sprintf(sql, suffix)
  272. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  273. return
  274. }
  275. func GetCffexDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromCffexDataSimple, err error) {
  276. sql := ` SELECT * FROM base_from_trade_cffex_index WHERE 1=1 `
  277. o := orm.NewOrmUsingDB("data")
  278. if condition != "" {
  279. sql += condition
  280. }
  281. sql += ` ORDER BY data_time DESC `
  282. _, err = o.Raw(sql, pars).QueryRows(&item)
  283. return
  284. }
  285. func AddEdbDataCffexBySql(sqlStr string) (err error) {
  286. o := orm.NewOrmUsingDB("data")
  287. _, err = o.Raw(sqlStr).Exec()
  288. return
  289. }
  290. func GetEdbDataCffexByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  291. o := orm.NewOrmUsingDB("data")
  292. sql := ` SELECT * FROM edb_data_cffex WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  293. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  294. return
  295. }
  296. func GetBaseFromCffexDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeCFFEXIndex, err error) {
  297. o := orm.NewOrmUsingDB("data")
  298. sql := `SELECT * FROM base_from_trade_cffex_index WHERE %s_code=? `
  299. sql = fmt.Sprintf(sql, suffix)
  300. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  301. return
  302. }
  303. func GetEdbDataCffexByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  304. o := orm.NewOrmUsingDB("data")
  305. sql := ` SELECT COUNT(1) AS count FROM edb_data_cffex WHERE edb_code=? AND data_time=? `
  306. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  307. return
  308. }
  309. func ModifyEdbDataCffex(edbInfoId int64, dataTime, value string) (err error) {
  310. o := orm.NewOrmUsingDB("data")
  311. sql := ` UPDATE edb_data_cffex SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  312. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  313. return
  314. }