edb_data_cffex.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. func GetEdbDataByCffex(edbCode, suffix, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {
  61. o := orm.NewOrmUsingDB("data")
  62. to, err := o.Begin()
  63. if err != nil {
  64. return
  65. }
  66. searchItem = new(EdbInfoSearch)
  67. searchItem.EdbCode = edbCode
  68. cffexBaseDataAll, err := GetBaseFromCffexDataAllByIndexCode(edbCode, suffix)
  69. if err != nil && err.Error() != utils.ErrNoRow() {
  70. return
  71. }
  72. var isAdd bool
  73. addSql := ` INSERT INTO edb_data_cffex(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  74. dataList := make([]*EdbInfoSearchData, 0)
  75. existMap := make(map[string]string)
  76. for _, sv := range cffexBaseDataAll {
  77. eDate := sv.DataTime
  78. dataTime, err := time.Parse(utils.FormatDate, eDate)
  79. if err != nil {
  80. fmt.Println("time.Parse Err:" + eDate)
  81. return nil, err
  82. }
  83. timestamp := dataTime.UnixNano() / 1e6
  84. timeStr := fmt.Sprintf("%d", timestamp)
  85. if _, ok := existMap[eDate]; !ok {
  86. if suffix == "deal" {
  87. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.DealValue)
  88. } else if suffix == "buy" {
  89. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.BuyValue)
  90. } else {
  91. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.SoldValue)
  92. }
  93. isAdd = true
  94. }
  95. if suffix == "deal" {
  96. existMap[eDate] = sv.DealValue
  97. } else if suffix == "buy" {
  98. existMap[eDate] = sv.BuyValue
  99. } else {
  100. existMap[eDate] = sv.SoldValue
  101. }
  102. }
  103. if isAdd {
  104. addSql = strings.TrimRight(addSql, ",")
  105. utils.FileLog.Info("addSql:" + addSql)
  106. _, err = to.Raw(addSql).Exec()
  107. if err != nil {
  108. return searchItem, err
  109. }
  110. }
  111. if err != nil {
  112. _ = to.Rollback()
  113. } else {
  114. _ = to.Commit()
  115. }
  116. size := utils.EDB_DATA_LIMIT
  117. dataList, err = GetEdbDataAllByEdbCode(edbCode, utils.DATA_SOURCE_CFFEX, size)
  118. if err != nil {
  119. utils.FileLogData.Info("GetEdbDataCffexByCode Err:%s", err.Error())
  120. return searchItem, err
  121. }
  122. minDate, maxDate, err := GetEdbDataCffexMaxOrMinDate(edbCode)
  123. if err != nil {
  124. return searchItem, err
  125. }
  126. searchItem.DataList = dataList
  127. searchItem.StartDate = minDate
  128. searchItem.EndDate = maxDate
  129. if searchItem.DataList == nil {
  130. searchItem.DataList = make([]*EdbInfoSearchData, 0)
  131. }
  132. return
  133. }
  134. // RefreshEdbDataByCffex 刷新中金所指标数据
  135. func RefreshEdbDataByCffex(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  136. o := orm.NewOrmUsingDB("data")
  137. to, err := o.Begin()
  138. if err != nil {
  139. return
  140. }
  141. defer func() {
  142. if err != nil {
  143. _ = to.Rollback()
  144. } else {
  145. _ = to.Commit()
  146. }
  147. }()
  148. if err != nil {
  149. return
  150. }
  151. var suffix string
  152. if strings.Contains(edbCode, "deal") {
  153. suffix = "deal"
  154. } else if strings.Contains(edbCode, "buy") {
  155. suffix = "buy"
  156. } else if strings.Contains(edbCode, "sold") {
  157. suffix = "sold"
  158. }
  159. edbInfoIdStr := strconv.Itoa(edbInfoId)
  160. //计算数据
  161. var condition string
  162. var pars []interface{}
  163. if edbCode != "" {
  164. if suffix == "deal" {
  165. condition += " AND deal_code=? "
  166. } else if suffix == "buy" {
  167. condition += " AND buy_code=? "
  168. } else {
  169. condition += " AND sold_code=? "
  170. }
  171. pars = append(pars, edbCode)
  172. }
  173. if startDate != "" {
  174. condition += " AND data_time>=? "
  175. pars = append(pars, startDate)
  176. }
  177. if endDate != "" {
  178. condition += " AND data_time<=? "
  179. pars = append(pars, endDate)
  180. }
  181. glDataList, err := GetCffexDataByTradeCode(condition, pars)
  182. addSql := ` INSERT INTO edb_data_cffex(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  183. var isAdd bool
  184. existMap := make(map[string]string)
  185. for _, v := range glDataList {
  186. var value string
  187. if suffix == "deal" {
  188. value = v.DealValue
  189. } else if suffix == "buy" {
  190. value = v.BuyValue
  191. } else {
  192. value = v.SoldValue
  193. }
  194. item := v
  195. itemValue := value
  196. if _, ok := existMap[v.DataTime]; !ok {
  197. count, err := GetEdbDataCffexByCodeAndDate(edbCode, v.DataTime)
  198. if err != nil && err.Error() != utils.ErrNoRow() {
  199. return err
  200. }
  201. if count <= 0 {
  202. eDate := item.DataTime
  203. sValue := itemValue
  204. if sValue != "" {
  205. dataTime, err := time.Parse(utils.FormatDate, eDate)
  206. if err != nil {
  207. return err
  208. }
  209. timestamp := dataTime.UnixNano() / 1e6
  210. timeStr := fmt.Sprintf("%d", timestamp)
  211. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  212. isAdd = true
  213. }
  214. } else {
  215. err = ModifyEdbDataCffex(int64(edbInfoId), v.DataTime, value)
  216. if err != nil {
  217. return err
  218. }
  219. }
  220. }
  221. existMap[v.DataTime] = value
  222. }
  223. if isAdd {
  224. addSql = strings.TrimRight(addSql, ",")
  225. _, err = to.Raw(addSql).Exec()
  226. if err != nil {
  227. return err
  228. }
  229. }
  230. return
  231. }
  232. // RefreshAllEdbDataByCffex 全部刷新中金所
  233. func RefreshAllEdbDataByCffex(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
  234. o := orm.NewOrmUsingDB("data")
  235. to, err := o.Begin()
  236. if err != nil {
  237. return
  238. }
  239. defer func() {
  240. if err != nil {
  241. _ = to.Rollback()
  242. } else {
  243. _ = to.Commit()
  244. }
  245. }()
  246. var suffix string
  247. if strings.Contains(edbCode, "deal") {
  248. suffix = "deal"
  249. } else if strings.Contains(edbCode, "buy") {
  250. suffix = "buy"
  251. } else if strings.Contains(edbCode, "sold") {
  252. suffix = "sold"
  253. }
  254. edbInfoIdStr := strconv.Itoa(edbInfoId)
  255. //计算数据
  256. var condition string
  257. var pars []interface{}
  258. if edbCode != "" {
  259. if suffix == "deal" {
  260. condition += " AND deal_code=? "
  261. } else if suffix == "buy" {
  262. condition += " AND buy_code=? "
  263. } else {
  264. condition += " AND sold_code=? "
  265. }
  266. pars = append(pars, edbCode)
  267. }
  268. if startDate != "" {
  269. condition += " AND data_time>=? "
  270. pars = append(pars, startDate)
  271. }
  272. if endDate != "" {
  273. condition += " AND data_time<=? "
  274. pars = append(pars, endDate)
  275. }
  276. glDataList, err := GetCffexDataByTradeCode(condition, pars)
  277. //获取指标所有数据
  278. dataList := make([]*EdbDataBase, 0)
  279. dataTableName := GetEdbDataTableName(source)
  280. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  281. sql = fmt.Sprintf(sql, dataTableName)
  282. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  283. if err != nil {
  284. return err
  285. }
  286. dataMap := make(map[string]string)
  287. for _, v := range dataList {
  288. dataMap[v.DataTime] = v.Value
  289. }
  290. addSql := ` INSERT INTO edb_data_cffex(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  291. var isAdd bool
  292. existMap := make(map[string]string)
  293. for _, v := range glDataList {
  294. var value string
  295. if suffix == "deal" {
  296. value = v.DealValue
  297. } else if suffix == "buy" {
  298. value = v.BuyValue
  299. } else {
  300. value = v.SoldValue
  301. }
  302. item := v
  303. itemValue := value
  304. if _, ok := existMap[v.DataTime]; !ok {
  305. eDate := item.DataTime
  306. sValue := itemValue
  307. if sValue != "" {
  308. dataTime, err := time.Parse(utils.FormatDate, eDate)
  309. if err != nil {
  310. return err
  311. }
  312. timestamp := dataTime.UnixNano() / 1e6
  313. timeStr := fmt.Sprintf("%d", timestamp)
  314. saveValue := sValue
  315. if existVal, ok := dataMap[eDate]; !ok {
  316. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  317. isAdd = true
  318. } else {
  319. if existVal != saveValue {
  320. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  321. sql = fmt.Sprintf(sql, dataTableName)
  322. _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
  323. if err != nil {
  324. return err
  325. }
  326. }
  327. }
  328. }
  329. }
  330. existMap[v.DataTime] = v.DataTime
  331. }
  332. if isAdd {
  333. addSql = strings.TrimRight(addSql, ",")
  334. _, err = to.Raw(addSql).Exec()
  335. if err != nil {
  336. return err
  337. }
  338. }
  339. return
  340. }
  341. // GetBaseInfoFromCffexByIndexCode 获取指标信息
  342. func GetBaseInfoFromCffexByIndexCode(indexCode, suffix string) (list []*BaseInfoFromCffex, err error) {
  343. o := orm.NewOrmUsingDB("data")
  344. sql := `SELECT * FROM base_from_trade_cffex_index WHERE %s_code=? `
  345. sql = fmt.Sprintf(sql, suffix)
  346. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  347. return
  348. }
  349. func GetCffexDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromCffexDataSimple, err error) {
  350. sql := ` SELECT * FROM base_from_trade_cffex_index WHERE 1=1 `
  351. o := orm.NewOrmUsingDB("data")
  352. if condition != "" {
  353. sql += condition
  354. }
  355. sql += ` ORDER BY data_time DESC `
  356. _, err = o.Raw(sql, pars).QueryRows(&item)
  357. return
  358. }
  359. func AddEdbDataCffexBySql(sqlStr string) (err error) {
  360. o := orm.NewOrmUsingDB("data")
  361. _, err = o.Raw(sqlStr).Exec()
  362. return
  363. }
  364. func GetEdbDataCffexByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  365. o := orm.NewOrmUsingDB("data")
  366. sql := ` SELECT * FROM edb_data_cffex WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  367. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  368. return
  369. }
  370. func GetBaseFromCffexDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeCFFEXIndex, err error) {
  371. o := orm.NewOrmUsingDB("data")
  372. sql := `SELECT * FROM base_from_trade_cffex_index WHERE %s_code=? `
  373. sql = fmt.Sprintf(sql, suffix)
  374. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  375. return
  376. }
  377. func GetEdbDataCffexByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  378. o := orm.NewOrmUsingDB("data")
  379. sql := ` SELECT COUNT(1) AS count FROM edb_data_cffex WHERE edb_code=? AND data_time=? `
  380. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  381. return
  382. }
  383. func ModifyEdbDataCffex(edbInfoId int64, dataTime, value string) (err error) {
  384. o := orm.NewOrmUsingDB("data")
  385. sql := ` UPDATE edb_data_cffex SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  386. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  387. return
  388. }