edb_data_sh.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package data_manage
  2. import (
  3. "fmt"
  4. "hongze/hongze_task/utils"
  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. // RefreshEdbDataBySh 刷新上期所指标数据
  61. func RefreshEdbDataBySh(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  62. o := orm.NewOrmUsingDB("data")
  63. tx, err := o.Begin()
  64. if err != nil {
  65. return err
  66. }
  67. defer func() {
  68. if err != nil {
  69. tx.Rollback()
  70. } else {
  71. tx.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 := GetShDataByTradeCode(condition, pars)
  108. addSql := ` INSERT INTO edb_data_sh(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 := GetEdbDataShByCodeAndDate(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 = ModifyEdbDataSh(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 = tx.Raw(addSql).Exec()
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. return
  157. }
  158. // RefreshAllEdbDataBySh 全部刷新上期所
  159. func RefreshAllEdbDataBySh(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
  160. o := orm.NewOrmUsingDB("data")
  161. tx,err:=o.Begin()
  162. if err!=nil {
  163. return err
  164. }
  165. defer func() {
  166. if err != nil {
  167. tx.Rollback()
  168. } else {
  169. tx.Commit()
  170. }
  171. }()
  172. if err != nil {
  173. return
  174. }
  175. var suffix string
  176. if strings.Contains(edbCode, "deal") {
  177. suffix = "deal"
  178. } else if strings.Contains(edbCode, "buy") {
  179. suffix = "buy"
  180. } else if strings.Contains(edbCode, "sold") {
  181. suffix = "sold"
  182. }
  183. edbInfoIdStr := strconv.Itoa(edbInfoId)
  184. //计算数据
  185. var condition string
  186. var pars []interface{}
  187. if edbCode != "" {
  188. if suffix == "deal" {
  189. condition += " AND deal_code=? "
  190. } else if suffix == "buy" {
  191. condition += " AND buy_code=? "
  192. } else {
  193. condition += " AND sold_code=? "
  194. }
  195. pars = append(pars, edbCode)
  196. }
  197. if startDate != "" {
  198. condition += " AND data_time>=? "
  199. pars = append(pars, startDate)
  200. }
  201. if endDate != "" {
  202. condition += " AND data_time<=? "
  203. pars = append(pars, endDate)
  204. }
  205. glDataList, err := GetShDataByTradeCode(condition, pars)
  206. //获取指标所有数据
  207. dataList := make([]*EdbDataBase, 0)
  208. dataTableName := GetEdbDataTableName(source)
  209. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  210. sql = fmt.Sprintf(sql, dataTableName)
  211. _, err = o.Raw(sql, edbInfoId).QueryRows(&dataList)
  212. if err != nil {
  213. return err
  214. }
  215. dataMap := make(map[string]string)
  216. for _, v := range dataList {
  217. dataMap[v.DataTime] = v.Value
  218. }
  219. addSql := ` INSERT INTO edb_data_sh(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  220. var isAdd bool
  221. existMap := make(map[string]string)
  222. for _, v := range glDataList {
  223. var value string
  224. if suffix == "deal" {
  225. value = v.DealValue
  226. } else if suffix == "buy" {
  227. value = v.BuyValue
  228. } else {
  229. value = v.SoldValue
  230. }
  231. item := v
  232. itemValue := value
  233. if _, ok := existMap[v.DataTime]; !ok {
  234. eDate := item.DataTime
  235. sValue := itemValue
  236. if sValue != "" {
  237. dataTime, err := time.Parse(utils.FormatDate, eDate)
  238. if err != nil {
  239. return err
  240. }
  241. timestamp := dataTime.UnixNano() / 1e6
  242. timeStr := fmt.Sprintf("%d", timestamp)
  243. saveValue := sValue
  244. if existVal, ok := dataMap[eDate]; !ok {
  245. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  246. isAdd = true
  247. } else {
  248. if existVal != saveValue {
  249. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  250. sql = fmt.Sprintf(sql, dataTableName)
  251. _, err = tx.Raw(sql, sValue, edbInfoId, eDate).Exec()
  252. if err != nil {
  253. return err
  254. }
  255. }
  256. }
  257. }
  258. }
  259. existMap[v.DataTime] = v.DataTime
  260. }
  261. if isAdd {
  262. addSql = strings.TrimRight(addSql, ",")
  263. _, err = tx.Raw(addSql).Exec()
  264. if err != nil {
  265. return err
  266. }
  267. }
  268. return
  269. }
  270. // GetBaseInfoFromShByIndexCode 获取指标信息
  271. func GetBaseInfoFromShByIndexCode(indexCode, suffix string) (list []*BaseInfoFromSh, err error) {
  272. o := orm.NewOrmUsingDB("data")
  273. sql := `SELECT * FROM base_from_trade_shanghai_index WHERE %s_code=? `
  274. sql = fmt.Sprintf(sql, suffix)
  275. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  276. return
  277. }
  278. func GetShDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromShDataSimple, err error) {
  279. sql := ` SELECT * FROM base_from_trade_shanghai_index WHERE 1=1 `
  280. o := orm.NewOrmUsingDB("data")
  281. if condition != "" {
  282. sql += condition
  283. }
  284. sql += ` ORDER BY data_time DESC `
  285. _, err = o.Raw(sql, pars).QueryRows(&item)
  286. return
  287. }
  288. func AddEdbDataShBySql(sqlStr string) (err error) {
  289. o := orm.NewOrmUsingDB("data")
  290. _, err = o.Raw(sqlStr).Exec()
  291. return
  292. }
  293. func GetBaseFromShDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeShIndex, err error) {
  294. o := orm.NewOrmUsingDB("data")
  295. sql := `SELECT * FROM base_from_trade_shanghai_index WHERE %s_code=? `
  296. sql = fmt.Sprintf(sql, suffix)
  297. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  298. return
  299. }
  300. func GetEdbDataShByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  301. o := orm.NewOrmUsingDB("data")
  302. sql := ` SELECT COUNT(1) AS count FROM edb_data_sh WHERE edb_code=? AND data_time=? `
  303. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  304. return
  305. }
  306. func ModifyEdbDataSh(edbInfoId int64, dataTime, value string) (err error) {
  307. o := orm.NewOrmUsingDB("data")
  308. sql := ` UPDATE edb_data_sh SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  309. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  310. return
  311. }