edb_data_sh.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type ShData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. type BaseFromShDataSimple struct {
  15. Id int `orm:"column(base_from_trade_shanghai_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 BaseInfoFromSh struct {
  25. DealName string
  26. BuyName string
  27. SoldName string
  28. }
  29. type BaseFromTradeShIndex struct {
  30. BaseFromTradeShIndexId int `orm:"column(base_from_trade_shanghai_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 GetEdbDataShMaxOrMinDate(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_sh WHERE edb_code=? `
  57. err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
  58. return
  59. }
  60. func GetEdbDataBySh(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. shBaseDataAll, err := GetBaseFromShDataAllByIndexCode(edbCode, suffix)
  69. if err != nil && err.Error() != utils.ErrNoRow() {
  70. return
  71. }
  72. var isAdd bool
  73. addSql := ` INSERT INTO edb_data_sh(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 shBaseDataAll {
  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_SH, utils.DATA_SUB_SOURCE_EDB, size)
  118. if err != nil {
  119. utils.FileLogData.Info("GetEdbDataShByCode Err:%s", err.Error())
  120. return searchItem, err
  121. }
  122. minDate, maxDate, err := GetEdbDataShMaxOrMinDate(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. // RefreshEdbDataBySh 刷新上期所指标数据
  135. func RefreshEdbDataBySh(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. var suffix string
  149. if strings.Contains(edbCode, "deal") {
  150. suffix = "deal"
  151. } else if strings.Contains(edbCode, "buy") {
  152. suffix = "buy"
  153. } else if strings.Contains(edbCode, "sold") {
  154. suffix = "sold"
  155. }
  156. edbInfoIdStr := strconv.Itoa(edbInfoId)
  157. //计算数据
  158. var condition string
  159. var pars []interface{}
  160. if edbCode != "" {
  161. if suffix == "deal" {
  162. condition += " AND deal_code=? "
  163. } else if suffix == "buy" {
  164. condition += " AND buy_code=? "
  165. } else {
  166. condition += " AND sold_code=? "
  167. }
  168. pars = append(pars, edbCode)
  169. }
  170. if startDate != "" {
  171. condition += " AND data_time>=? "
  172. pars = append(pars, startDate)
  173. }
  174. if endDate != "" {
  175. condition += " AND data_time<=? "
  176. pars = append(pars, endDate)
  177. }
  178. glDataList, err := GetShDataByTradeCode(condition, pars)
  179. addSql := ` INSERT INTO edb_data_sh(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  180. var isAdd bool
  181. existMap := make(map[string]string)
  182. for _, v := range glDataList {
  183. var value string
  184. if suffix == "deal" {
  185. value = v.DealValue
  186. } else if suffix == "buy" {
  187. value = v.BuyValue
  188. } else {
  189. value = v.SoldValue
  190. }
  191. item := v
  192. itemValue := value
  193. if _, ok := existMap[v.DataTime]; !ok {
  194. count, err := GetEdbDataShByCodeAndDate(edbCode, v.DataTime)
  195. if err != nil && err.Error() != utils.ErrNoRow() {
  196. return err
  197. }
  198. if count <= 0 {
  199. eDate := item.DataTime
  200. sValue := itemValue
  201. if sValue != "" {
  202. dataTime, err := time.Parse(utils.FormatDate, eDate)
  203. if err != nil {
  204. return err
  205. }
  206. timestamp := dataTime.UnixNano() / 1e6
  207. timeStr := fmt.Sprintf("%d", timestamp)
  208. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  209. isAdd = true
  210. }
  211. } else {
  212. err = ModifyEdbDataSh(int64(edbInfoId), v.DataTime, value)
  213. if err != nil {
  214. return err
  215. }
  216. }
  217. }
  218. existMap[v.DataTime] = value
  219. }
  220. if isAdd {
  221. addSql = strings.TrimRight(addSql, ",")
  222. _, err = to.Raw(addSql).Exec()
  223. if err != nil {
  224. return err
  225. }
  226. }
  227. return
  228. }
  229. // RefreshAllEdbDataBySh 全部刷新上期所
  230. func RefreshAllEdbDataBySh(edbInfoId, source, subSource int, edbCode, startDate, endDate string) (err error) {
  231. o := orm.NewOrmUsingDB("data")
  232. to, err := o.Begin()
  233. if err != nil {
  234. return
  235. }
  236. defer func() {
  237. if err != nil {
  238. _ = to.Rollback()
  239. } else {
  240. _ = to.Commit()
  241. }
  242. }()
  243. var suffix string
  244. if strings.Contains(edbCode, "deal") {
  245. suffix = "deal"
  246. } else if strings.Contains(edbCode, "buy") {
  247. suffix = "buy"
  248. } else if strings.Contains(edbCode, "sold") {
  249. suffix = "sold"
  250. }
  251. edbInfoIdStr := strconv.Itoa(edbInfoId)
  252. //计算数据
  253. var condition string
  254. var pars []interface{}
  255. if edbCode != "" {
  256. if suffix == "deal" {
  257. condition += " AND deal_code=? "
  258. } else if suffix == "buy" {
  259. condition += " AND buy_code=? "
  260. } else {
  261. condition += " AND sold_code=? "
  262. }
  263. pars = append(pars, edbCode)
  264. }
  265. if startDate != "" {
  266. condition += " AND data_time>=? "
  267. pars = append(pars, startDate)
  268. }
  269. if endDate != "" {
  270. condition += " AND data_time<=? "
  271. pars = append(pars, endDate)
  272. }
  273. glDataList, err := GetShDataByTradeCode(condition, pars)
  274. //获取指标所有数据
  275. dataList := make([]*EdbDataBase, 0)
  276. dataTableName := GetEdbDataTableName(source, subSource)
  277. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  278. sql = fmt.Sprintf(sql, dataTableName)
  279. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  280. if err != nil {
  281. return err
  282. }
  283. dataMap := make(map[string]string)
  284. for _, v := range dataList {
  285. dataMap[v.DataTime] = v.Value
  286. }
  287. addSql := ` INSERT INTO edb_data_sh(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  288. var isAdd bool
  289. existMap := make(map[string]string)
  290. for _, v := range glDataList {
  291. var value string
  292. if suffix == "deal" {
  293. value = v.DealValue
  294. } else if suffix == "buy" {
  295. value = v.BuyValue
  296. } else {
  297. value = v.SoldValue
  298. }
  299. item := v
  300. itemValue := value
  301. if _, ok := existMap[v.DataTime]; !ok {
  302. eDate := item.DataTime
  303. sValue := itemValue
  304. if sValue != "" {
  305. dataTime, err := time.Parse(utils.FormatDate, eDate)
  306. if err != nil {
  307. return err
  308. }
  309. timestamp := dataTime.UnixNano() / 1e6
  310. timeStr := fmt.Sprintf("%d", timestamp)
  311. saveValue := sValue
  312. if existVal, ok := dataMap[eDate]; !ok {
  313. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  314. isAdd = true
  315. } else {
  316. if existVal != saveValue {
  317. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  318. sql = fmt.Sprintf(sql, dataTableName)
  319. _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
  320. if err != nil {
  321. return err
  322. }
  323. }
  324. }
  325. }
  326. }
  327. existMap[v.DataTime] = v.DataTime
  328. }
  329. if isAdd {
  330. addSql = strings.TrimRight(addSql, ",")
  331. _, err = to.Raw(addSql).Exec()
  332. if err != nil {
  333. return err
  334. }
  335. }
  336. return
  337. }
  338. // GetBaseInfoFromShByIndexCode 获取指标信息
  339. func GetBaseInfoFromShByIndexCode(indexCode, suffix string) (list []*BaseInfoFromSh, err error) {
  340. o := orm.NewOrmUsingDB("data")
  341. sql := `SELECT * FROM base_from_trade_shanghai_index WHERE %s_code=? `
  342. sql = fmt.Sprintf(sql, suffix)
  343. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  344. return
  345. }
  346. func GetShDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromShDataSimple, err error) {
  347. sql := ` SELECT * FROM base_from_trade_shanghai_index WHERE 1=1 `
  348. o := orm.NewOrmUsingDB("data")
  349. if condition != "" {
  350. sql += condition
  351. }
  352. sql += ` ORDER BY data_time DESC `
  353. _, err = o.Raw(sql, pars).QueryRows(&item)
  354. return
  355. }
  356. func AddEdbDataShBySql(sqlStr string) (err error) {
  357. o := orm.NewOrmUsingDB("data")
  358. _, err = o.Raw(sqlStr).Exec()
  359. return
  360. }
  361. func GetEdbDataShByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  362. o := orm.NewOrmUsingDB("data")
  363. sql := ` SELECT * FROM edb_data_sh WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  364. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  365. return
  366. }
  367. func GetBaseFromShDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeShIndex, err error) {
  368. o := orm.NewOrmUsingDB("data")
  369. sql := `SELECT * FROM base_from_trade_shanghai_index WHERE %s_code=? `
  370. sql = fmt.Sprintf(sql, suffix)
  371. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  372. return
  373. }
  374. func GetEdbDataShByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  375. o := orm.NewOrmUsingDB("data")
  376. sql := ` SELECT COUNT(1) AS count FROM edb_data_sh WHERE edb_code=? AND data_time=? `
  377. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  378. return
  379. }
  380. func ModifyEdbDataSh(edbInfoId int64, dataTime, value string) (err error) {
  381. o := orm.NewOrmUsingDB("data")
  382. sql := ` UPDATE edb_data_sh SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  383. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  384. return
  385. }