edb_data_zz.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 ZzData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. type BaseFromZzDataSimple struct {
  15. Id int `orm:"column(base_from_trade_zhengzhou_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 BaseInfoFromZz struct {
  25. DealName string
  26. BuyName string
  27. SoldName string
  28. }
  29. type BaseFromTradeZhengzhouIndex struct {
  30. BaseFromTradeZhengzhouIndexId int `orm:"column(base_from_trade_zhengzhou_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 GetEdbDataZzMaxOrMinDate(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_zz WHERE edb_code=? `
  57. err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
  58. return
  59. }
  60. func GetEdbDataByZz(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. zzBaseDataAll, err := GetBaseFromZhengzhouDataAllByIndexCode(edbCode, suffix)
  69. if err != nil && err.Error() != utils.ErrNoRow() {
  70. return
  71. }
  72. var isAdd bool
  73. addSql := ` INSERT INTO edb_data_zz(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 zzBaseDataAll {
  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_ZZ, size)
  118. if err != nil {
  119. utils.FileLogData.Info("GetEdbDataZzByCode Err:%s", err.Error())
  120. return searchItem, err
  121. }
  122. minDate, maxDate, err := GetEdbDataZzMaxOrMinDate(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. // RefreshEdbDataByZz 刷新郑商所指标数据
  135. func RefreshEdbDataByZz(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 := GetZzDataByTradeCode(condition, pars)
  182. addSql := ` INSERT INTO edb_data_zz(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 := GetEdbDataZzByCodeAndDate(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 = ModifyEdbDataZz(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. // RefreshAllEdbDataByZz 全部刷新郑商所
  233. func RefreshAllEdbDataByZz(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. if err != nil {
  247. return
  248. }
  249. var suffix string
  250. if strings.Contains(edbCode, "deal") {
  251. suffix = "deal"
  252. } else if strings.Contains(edbCode, "buy") {
  253. suffix = "buy"
  254. } else if strings.Contains(edbCode, "sold") {
  255. suffix = "sold"
  256. }
  257. edbInfoIdStr := strconv.Itoa(edbInfoId)
  258. //计算数据
  259. var condition string
  260. var pars []interface{}
  261. if edbCode != "" {
  262. if suffix == "deal" {
  263. condition += " AND deal_code=? "
  264. } else if suffix == "buy" {
  265. condition += " AND buy_code=? "
  266. } else {
  267. condition += " AND sold_code=? "
  268. }
  269. pars = append(pars, edbCode)
  270. }
  271. if startDate != "" {
  272. condition += " AND data_time>=? "
  273. pars = append(pars, startDate)
  274. }
  275. if endDate != "" {
  276. condition += " AND data_time<=? "
  277. pars = append(pars, endDate)
  278. }
  279. glDataList, err := GetZzDataByTradeCode(condition, pars)
  280. //获取指标所有数据
  281. dataList := make([]*EdbDataBase, 0)
  282. dataTableName := GetEdbDataTableName(source)
  283. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  284. sql = fmt.Sprintf(sql, dataTableName)
  285. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  286. if err != nil {
  287. return err
  288. }
  289. dataMap := make(map[string]string)
  290. for _, v := range dataList {
  291. dataMap[v.DataTime] = v.Value
  292. }
  293. addSql := ` INSERT INTO edb_data_zz(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  294. var isAdd bool
  295. existMap := make(map[string]string)
  296. for _, v := range glDataList {
  297. var value string
  298. if suffix == "deal" {
  299. value = v.DealValue
  300. } else if suffix == "buy" {
  301. value = v.BuyValue
  302. } else {
  303. value = v.SoldValue
  304. }
  305. item := v
  306. itemValue := value
  307. if _, ok := existMap[v.DataTime]; !ok {
  308. eDate := item.DataTime
  309. sValue := itemValue
  310. if sValue != "" {
  311. dataTime, err := time.Parse(utils.FormatDate, eDate)
  312. if err != nil {
  313. return err
  314. }
  315. timestamp := dataTime.UnixNano() / 1e6
  316. timeStr := fmt.Sprintf("%d", timestamp)
  317. saveValue := sValue
  318. if existVal, ok := dataMap[eDate]; !ok {
  319. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  320. isAdd = true
  321. } else {
  322. if existVal != saveValue {
  323. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  324. sql = fmt.Sprintf(sql, dataTableName)
  325. _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
  326. if err != nil {
  327. return err
  328. }
  329. }
  330. }
  331. }
  332. }
  333. existMap[v.DataTime] = v.DataTime
  334. }
  335. if isAdd {
  336. addSql = strings.TrimRight(addSql, ",")
  337. _, err = to.Raw(addSql).Exec()
  338. if err != nil {
  339. return err
  340. }
  341. }
  342. return
  343. }
  344. // GetBaseInfoFromZhengzhouByIndexCode 获取指标信息
  345. func GetBaseInfoFromZhengzhouByIndexCode(indexCode, suffix string) (list []*BaseInfoFromZz, err error) {
  346. o := orm.NewOrmUsingDB("data")
  347. sql := `SELECT * FROM base_from_trade_zhengzhou_index WHERE %s_code=? `
  348. sql = fmt.Sprintf(sql, suffix)
  349. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  350. return
  351. }
  352. func GetZzDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromZzDataSimple, err error) {
  353. sql := ` SELECT * FROM base_from_trade_zhengzhou_index WHERE 1=1 `
  354. o := orm.NewOrmUsingDB("data")
  355. if condition != "" {
  356. sql += condition
  357. }
  358. sql += ` ORDER BY data_time DESC `
  359. _, err = o.Raw(sql, pars).QueryRows(&item)
  360. return
  361. }
  362. func AddEdbDataZzBySql(sqlStr string) (err error) {
  363. o := orm.NewOrmUsingDB("data")
  364. _, err = o.Raw(sqlStr).Exec()
  365. return
  366. }
  367. func GetEdbDataZzByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  368. o := orm.NewOrmUsingDB("data")
  369. sql := ` SELECT * FROM edb_data_zz WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  370. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  371. return
  372. }
  373. func GetBaseFromZhengzhouDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeZhengzhouIndex, err error) {
  374. o := orm.NewOrmUsingDB("data")
  375. sql := `SELECT * FROM base_from_trade_zhengzhou_index WHERE %s_code=? `
  376. sql = fmt.Sprintf(sql, suffix)
  377. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  378. return
  379. }
  380. func GetEdbDataZzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  381. o := orm.NewOrmUsingDB("data")
  382. sql := ` SELECT COUNT(1) AS count FROM edb_data_zz WHERE edb_code=? AND data_time=? `
  383. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  384. return
  385. }
  386. func ModifyEdbDataZz(edbInfoId int64, dataTime, value string) (err error) {
  387. o := orm.NewOrmUsingDB("data")
  388. sql := ` UPDATE edb_data_zz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  389. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  390. return
  391. }