edb_data_gie.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 GieData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. func GetEdbDataGieMaxOrMinDate(edbCode string) (minDate, maxDate string, err error) {
  15. o := orm.NewOrmUsingDB("data")
  16. sql := ` SELECT MIN(data_time) AS minDate,MAX(data_time) AS maxDate FROM edb_data_gie WHERE edb_code=? `
  17. err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
  18. return
  19. }
  20. //func GetEdbDataByGie(edbCode, suffix, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {
  21. // o := orm.NewOrmUsingDB("data")
  22. // to, err := o.Begin()
  23. // if err != nil {
  24. // return
  25. // }
  26. // searchItem = new(EdbInfoSearch)
  27. // searchItem.EdbCode = edbCode
  28. // eicBaseDataAll, err := GetBaseFromEicDataAllByIndexCode(edbCode, suffix)
  29. // if err != nil && err.Error() != utils.ErrNoRow() {
  30. // fmt.Println("GetBaseFromEicDataAllByIndexCode err:", err)
  31. // return
  32. // }
  33. //
  34. // var isAdd bool
  35. // addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  36. // dataList := make([]*EdbInfoSearchData, 0)
  37. // existMap := make(map[string]string)
  38. //
  39. // for _, sv := range eicBaseDataAll {
  40. // eDate := sv.GasDayStartedOn
  41. // dataTime, err := time.Parse(utils.FormatDate, eDate)
  42. // if err != nil {
  43. // fmt.Println("time.Parse Err:" + eDate)
  44. // return nil, err
  45. // }
  46. // timestamp := dataTime.UnixNano() / 1e6
  47. // timeStr := fmt.Sprintf("%d", timestamp)
  48. // //var name string
  49. // if _, ok := existMap[eDate]; !ok {
  50. // if suffix == "GS" {
  51. // //name = "gas_in_storage"
  52. // addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.GasInStorage)
  53. // existMap[eDate] = sv.GasInStorage
  54. // } else if suffix == "F" {
  55. // //name = "full"
  56. // addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Full)
  57. // existMap[eDate] = sv.Full
  58. // } else if suffix == "T" {
  59. // //name = "trend"
  60. // addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Trend)
  61. // existMap[eDate] = sv.Trend
  62. // } else if suffix == "In" {
  63. // //name = "injection"
  64. // addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Injection)
  65. // existMap[eDate] = sv.Injection
  66. // } else if suffix == "Out" {
  67. // //name = "withdrawal"
  68. // addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Withdrawal)
  69. // existMap[eDate] = sv.Withdrawal
  70. // } else if suffix == "WGV" {
  71. // //name = "working_gas_volume"
  72. // addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.WorkingGasVolume)
  73. // existMap[eDate] = sv.WorkingGasVolume
  74. // } else if suffix == "IC" {
  75. // //name = "injection_capacity"
  76. // addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.InjectionCapacity)
  77. // existMap[eDate] = sv.InjectionCapacity
  78. // } else if suffix == "WC" {
  79. // //name = "withdrawal_capacity"
  80. // addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.WithdrawalCapacity)
  81. // existMap[eDate] = sv.WithdrawalCapacity
  82. // }
  83. // isAdd = true
  84. // }
  85. // }
  86. // if isAdd {
  87. // addSql = strings.TrimRight(addSql, ",")
  88. // utils.FileLog.Info("addSql:" + addSql)
  89. // _, err = to.Raw(addSql).Exec()
  90. // if err != nil {
  91. // fmt.Println("addSql err:", err)
  92. // return searchItem, err
  93. // }
  94. // }
  95. // if err != nil {
  96. // _ = to.Rollback()
  97. // } else {
  98. // _ = to.Commit()
  99. // }
  100. // size := utils.EDB_DATA_LIMIT
  101. // dataList, err = GetEdbDataAllByEdbCode(edbCode, utils.DATA_SOURCE_GIE, size)
  102. // if err != nil {
  103. // utils.FileLogData.Info("GetEdbDataGieByCode Err:%s", err.Error())
  104. // return searchItem, err
  105. // }
  106. // minDate, maxDate, err := GetEdbDataGieMaxOrMinDate(edbCode)
  107. // if err != nil {
  108. // return searchItem, err
  109. // }
  110. // searchItem.DataList = dataList
  111. // searchItem.StartDate = minDate
  112. // searchItem.EndDate = maxDate
  113. // if searchItem.DataList == nil {
  114. // searchItem.DataList = make([]*EdbInfoSearchData, 0)
  115. // }
  116. // return
  117. //}
  118. // RefreshEdbDataByGie 刷新欧洲天然气指标数据
  119. func RefreshEdbDataByGie(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  120. o := orm.NewOrmUsingDB("data")
  121. to, err := o.Begin()
  122. if err != nil {
  123. return
  124. }
  125. defer func() {
  126. if err != nil {
  127. _ = to.Rollback()
  128. } else {
  129. _ = to.Commit()
  130. }
  131. }()
  132. if err != nil {
  133. fmt.Println("refresh err:", err)
  134. return
  135. }
  136. var suffix string
  137. l := len(edbCode)
  138. if strings.Contains(edbCode[l-2:], "GS") {
  139. suffix = "GS"
  140. } else if strings.Contains(edbCode[l-2:], "CF") {
  141. suffix = "CF"
  142. } else if strings.Contains(edbCode[l-1:], "T") {
  143. suffix = "T"
  144. } else if strings.Contains(edbCode[l-2:], "In") {
  145. suffix = "In"
  146. } else if strings.Contains(edbCode[l-3:], "Out") {
  147. suffix = "Out"
  148. } else if strings.Contains(edbCode[l-3:], "WGV") {
  149. suffix = "WGV"
  150. } else if strings.Contains(edbCode[l-2:], "IC") {
  151. suffix = "IC"
  152. } else if strings.Contains(edbCode[l-2:], "WC") {
  153. suffix = "WC"
  154. } else if strings.Contains(edbCode[l-1:], "F") {
  155. suffix = "F"
  156. } else if strings.Contains(edbCode[l-1:], "C") {
  157. suffix = "C"
  158. } else {
  159. suffix = ""
  160. }
  161. edbInfoIdStr := strconv.Itoa(edbInfoId)
  162. //计算数据
  163. var condition string
  164. var pars []interface{}
  165. if edbCode != "" {
  166. condition += " AND eic_code=? "
  167. pars = append(pars, edbCode[:l-len(suffix)])
  168. }
  169. if startDate != "" {
  170. condition += " AND gas_day_started_on>=? "
  171. pars = append(pars, startDate)
  172. }
  173. if endDate != "" {
  174. condition += " AND gas_day_started_on<=? "
  175. pars = append(pars, endDate)
  176. }
  177. eicDataList, err := GetGieDataByTradeCodeV2(condition, pars)
  178. fmt.Println("eicDataList", len(eicDataList))
  179. addSql := ` INSERT INTO edb_data_gie(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 eicDataList {
  183. var value string
  184. if suffix == "GS" {
  185. value = v.GasInStorage
  186. } else if suffix == "C" {
  187. value = v.Consumption
  188. } else if suffix == "CF" {
  189. value = v.ConsumptionFull
  190. } else if suffix == "F" {
  191. value = v.Full
  192. } else if suffix == "T" {
  193. value = v.Trend
  194. } else if suffix == "In" {
  195. value = v.Injection
  196. } else if suffix == "Out" {
  197. value = v.Withdrawal
  198. } else if suffix == "WGV" {
  199. value = v.WorkingGasVolume
  200. } else if suffix == "IC" {
  201. value = v.InjectionCapacity
  202. } else if suffix == "WC" {
  203. value = v.WithdrawalCapacity
  204. }
  205. item := v
  206. itemValue := value
  207. if _, ok := existMap[v.GasDayStart]; !ok {
  208. count, err := GetEdbDataGieByCodeAndDate(edbCode, v.GasDayStart)
  209. if err != nil && err.Error() != utils.ErrNoRow() {
  210. return err
  211. }
  212. if count <= 0 {
  213. eDate := item.GasDayStart
  214. sValue := itemValue
  215. if sValue != "" {
  216. dataTime, err := time.Parse(utils.FormatDate, eDate)
  217. if err != nil {
  218. return err
  219. }
  220. timestamp := dataTime.UnixNano() / 1e6
  221. timeStr := fmt.Sprintf("%d", timestamp)
  222. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  223. isAdd = true
  224. }
  225. } else {
  226. err = ModifyEdbDataGie(int64(edbInfoId), v.GasDayStart, value)
  227. if err != nil {
  228. return err
  229. }
  230. }
  231. }
  232. existMap[v.GasDayStart] = value
  233. }
  234. if isAdd {
  235. addSql = strings.TrimRight(addSql, ",")
  236. _, err = to.Raw(addSql).Exec()
  237. if err != nil {
  238. return err
  239. }
  240. }
  241. return
  242. }
  243. // RefreshAllEdbDataByGie 全部刷新欧洲天然气
  244. func RefreshAllEdbDataByGie(edbInfoId, source, subSource int, edbCode, startDate, endDate string) (err error) {
  245. o := orm.NewOrmUsingDB("data")
  246. to, err := o.Begin()
  247. if err != nil {
  248. return
  249. }
  250. defer func() {
  251. if err != nil {
  252. _ = to.Rollback()
  253. } else {
  254. _ = to.Commit()
  255. }
  256. }()
  257. if err != nil {
  258. return
  259. }
  260. var suffix string
  261. l := len(edbCode)
  262. if strings.Contains(edbCode[l-2:], "GS") {
  263. suffix = "GS"
  264. } else if strings.Contains(edbCode[l-2:], "CF") {
  265. suffix = "CF"
  266. } else if strings.Contains(edbCode[l-1:], "T") {
  267. suffix = "T"
  268. } else if strings.Contains(edbCode[l-2:], "In") {
  269. suffix = "In"
  270. } else if strings.Contains(edbCode[l-3:], "Out") {
  271. suffix = "Out"
  272. } else if strings.Contains(edbCode[l-3:], "WGV") {
  273. suffix = "WGV"
  274. } else if strings.Contains(edbCode[l-2:], "IC") {
  275. suffix = "IC"
  276. } else if strings.Contains(edbCode[l-2:], "WC") {
  277. suffix = "WC"
  278. } else if strings.Contains(edbCode[l-1:], "F") {
  279. suffix = "F"
  280. } else if strings.Contains(edbCode[l-1:], "C") {
  281. suffix = "C"
  282. } else {
  283. suffix = ""
  284. }
  285. edbInfoIdStr := strconv.Itoa(edbInfoId)
  286. //计算数据
  287. var condition string
  288. var pars []interface{}
  289. if edbCode != "" {
  290. condition += " AND eic_code=? "
  291. pars = append(pars, edbCode[:l-len(suffix)])
  292. }
  293. if startDate != "" {
  294. condition += " AND gas_day_start>=? "
  295. pars = append(pars, startDate)
  296. }
  297. if endDate != "" {
  298. condition += " AND gas_day_start<=? "
  299. pars = append(pars, endDate)
  300. }
  301. eicDataList, err := GetGieDataByTradeCodeV2(condition, pars)
  302. fmt.Println("all eicDataList", len(eicDataList))
  303. //获取指标所有数据
  304. dataList := make([]*EdbDataBase, 0)
  305. dataTableName := GetEdbDataTableName(source, subSource)
  306. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  307. sql = fmt.Sprintf(sql, dataTableName)
  308. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  309. if err != nil {
  310. return err
  311. }
  312. dataMap := make(map[string]string)
  313. for _, v := range dataList {
  314. dataMap[v.DataTime] = v.Value
  315. }
  316. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  317. var isAdd bool
  318. existMap := make(map[string]string)
  319. for _, v := range eicDataList {
  320. var value string
  321. if suffix == "GS" {
  322. value = v.GasInStorage
  323. } else if suffix == "C" {
  324. value = v.Consumption
  325. } else if suffix == "CF" {
  326. value = v.ConsumptionFull
  327. } else if suffix == "F" {
  328. value = v.Full
  329. } else if suffix == "T" {
  330. value = v.Trend
  331. } else if suffix == "In" {
  332. value = v.Injection
  333. } else if suffix == "Out" {
  334. value = v.Withdrawal
  335. } else if suffix == "WGV" {
  336. value = v.WorkingGasVolume
  337. } else if suffix == "IC" {
  338. value = v.InjectionCapacity
  339. } else if suffix == "WC" {
  340. value = v.WithdrawalCapacity
  341. }
  342. item := v
  343. itemValue := value
  344. if _, ok := existMap[v.GasDayStart]; !ok {
  345. eDate := item.GasDayStart
  346. sValue := itemValue
  347. if sValue != "" {
  348. dataTime, err := time.Parse(utils.FormatDate, eDate)
  349. if err != nil {
  350. return err
  351. }
  352. timestamp := dataTime.UnixNano() / 1e6
  353. timeStr := fmt.Sprintf("%d", timestamp)
  354. saveValue := sValue
  355. if existVal, ok := dataMap[eDate]; !ok {
  356. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  357. isAdd = true
  358. } else {
  359. if existVal != saveValue {
  360. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  361. sql = fmt.Sprintf(sql, dataTableName)
  362. _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
  363. if err != nil {
  364. return err
  365. }
  366. }
  367. }
  368. }
  369. }
  370. existMap[v.GasDayStart] = v.GasDayStart
  371. }
  372. if isAdd {
  373. addSql = strings.TrimRight(addSql, ",")
  374. _, err = to.Raw(addSql).Exec()
  375. if err != nil {
  376. return err
  377. }
  378. }
  379. return
  380. }
  381. // GetBaseInfoFromEicByIndexCode 获取指标信息
  382. func GetBaseInfoFromEicByIndexCode(indexCode, suffix string) (list []*BaseFromTradeEicIndex, err error) {
  383. o := orm.NewOrmUsingDB("data")
  384. sql := `SELECT * FROM base_from_trade_eic_index WHERE %s_code=? `
  385. sql = fmt.Sprintf(sql, suffix)
  386. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  387. return
  388. }
  389. func GetGieDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromTradeEicIndex, err error) {
  390. sql := ` SELECT * FROM base_from_trade_eic_index WHERE 1=1 `
  391. o := orm.NewOrmUsingDB("data")
  392. if condition != "" {
  393. sql += condition
  394. }
  395. sql += ` ORDER BY gas_day_started_on DESC `
  396. fmt.Println(sql, pars)
  397. _, err = o.Raw(sql, pars).QueryRows(&item)
  398. return
  399. }
  400. type EicIndexV2 struct {
  401. BaseFromEicIndexId int `orm:"column(base_from_eic_index_id);pk"`
  402. Type string
  403. EicCode string
  404. Name string
  405. Status string
  406. GasDayStart string
  407. GasInStorage string
  408. GasInStorageCode string
  409. Consumption string
  410. ConsumptionCode string
  411. ConsumptionFull string
  412. ConsumptionFullCode string
  413. Full string
  414. FullCode string
  415. Trend string
  416. TrendCode string
  417. Injection string
  418. InjectionCode string
  419. Withdrawal string
  420. WithdrawalCode string
  421. WorkingGasVolume string
  422. WorkingGasVolumeCode string
  423. InjectionCapacity string
  424. InjectionCapacityCode string
  425. WithdrawalCapacity string
  426. WithdrawalCapacityCode string
  427. Info string
  428. Parent string
  429. CreateTime time.Time
  430. ModifyTime time.Time
  431. Children []BaseFromTradeEicIndexV2
  432. }
  433. func GetGieDataByTradeCodeV2(condition string, pars []interface{}) (item []*EicIndexV2, err error) {
  434. sql := ` SELECT * FROM base_from_trade_eic_index_v2 WHERE 1=1 `
  435. o := orm.NewOrmUsingDB("data")
  436. if condition != "" {
  437. sql += condition
  438. }
  439. sql += ` ORDER BY gas_day_start DESC `
  440. fmt.Println(sql, pars)
  441. _, err = o.Raw(sql, pars).QueryRows(&item)
  442. return
  443. }
  444. func AddEdbDataGieBySql(sqlStr string) (err error) {
  445. o := orm.NewOrmUsingDB("data")
  446. _, err = o.Raw(sqlStr).Exec()
  447. return
  448. }
  449. func GetEdbDataGieByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  450. o := orm.NewOrmUsingDB("data")
  451. sql := ` SELECT * FROM edb_data_gie WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  452. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  453. return
  454. }
  455. func GetBaseFromEicDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeEicIndex, err error) {
  456. o := orm.NewOrmUsingDB("data")
  457. var name string
  458. if suffix == "" {
  459. name = "eic_code"
  460. } else if suffix == "GS" {
  461. name = "gas_in_storage_code"
  462. } else if suffix == "C" {
  463. name = "consumption_code"
  464. } else if suffix == "CF" {
  465. name = "consumption_full_code"
  466. } else if suffix == "F" {
  467. name = "full_code"
  468. } else if suffix == "T" {
  469. name = "trend_code"
  470. } else if suffix == "In" {
  471. name = "injection_code"
  472. } else if suffix == "Out" {
  473. name = "withdrawal_code"
  474. } else if suffix == "WGV" {
  475. name = "working_gas_volume_code"
  476. } else if suffix == "IC" {
  477. name = "injection_capacity_code"
  478. } else if suffix == "WC" {
  479. name = "withdrawal_capacity_code"
  480. }
  481. sql := `SELECT * FROM base_from_trade_eic_index_v2 WHERE %s=? `
  482. sql = fmt.Sprintf(sql, name)
  483. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  484. return
  485. }
  486. func GetBaseFromEicDataAllByIndexCodeV2(indexCode, suffix string) (list []*BaseFromTradeEicIndexV2, err error) {
  487. o := orm.NewOrmUsingDB("data")
  488. var name string
  489. if suffix == "" {
  490. name = "eic_code"
  491. } else if suffix == "GS" {
  492. name = "gas_in_storage_code"
  493. } else if suffix == "C" {
  494. name = "consumption_code"
  495. } else if suffix == "CF" {
  496. name = "consumption_full_code"
  497. } else if suffix == "F" {
  498. name = "full_code"
  499. } else if suffix == "T" {
  500. name = "trend_code"
  501. } else if suffix == "In" {
  502. name = "injection_code"
  503. } else if suffix == "Out" {
  504. name = "withdrawal_code"
  505. } else if suffix == "WGV" {
  506. name = "working_gas_volume_code"
  507. } else if suffix == "IC" {
  508. name = "injection_capacity_code"
  509. } else if suffix == "WC" {
  510. name = "withdrawal_capacity_code"
  511. }
  512. sql := `SELECT * FROM base_from_trade_eic_index_v2 WHERE %s=? `
  513. sql = fmt.Sprintf(sql, name)
  514. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  515. return
  516. }
  517. func GetEdbDataGieByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  518. o := orm.NewOrmUsingDB("data")
  519. sql := ` SELECT COUNT(1) AS count FROM edb_data_gie WHERE edb_code=? AND data_time=? `
  520. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  521. return
  522. }
  523. func ModifyEdbDataGie(edbInfoId int64, dataTime, value string) (err error) {
  524. o := orm.NewOrmUsingDB("data")
  525. sql := ` UPDATE edb_data_gie SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  526. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  527. return
  528. }